gpt4 book ai didi

co.cask.cdap.common.zookeeper.ZKExtOperations.createOrSet()方法的使用及代码示例

转载 作者:知者 更新时间:2024-03-16 17:11:31 25 4
gpt4 key购买 nike

本文整理了Java中co.cask.cdap.common.zookeeper.ZKExtOperations.createOrSet()方法的一些代码示例,展示了ZKExtOperations.createOrSet()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZKExtOperations.createOrSet()方法的具体详情如下:
包路径:co.cask.cdap.common.zookeeper.ZKExtOperations
类名称:ZKExtOperations
方法名:createOrSet

ZKExtOperations.createOrSet介绍

[英]Attempts to create a persistent node with the given content. If creation failed because the node already exists ( KeeperException.NodeExistsException), the node will be set with the given content. This method is suitable for cases where the node expected to be non-existed.
[中]尝试使用给定内容创建持久节点。如果创建失败是因为节点已经存在(KeeperException.NodeExistsException),那么将使用给定的内容设置节点。这种方法适用于节点不存在的情况。

代码示例

代码示例来源:origin: caskdata/cdap

/**
 * Submits the given {@link ResourceRequirement} for allocation.
 *
 * @param requirement The requirement to be submitted.
 * @return A {@link ListenableFuture} that will be completed when submission is completed and it'll carry the
 *         submitted requirement as result. The future will fail if failed to submit the requirement. Calling
 *         {@link ListenableFuture#cancel(boolean)} has no effect.
 */
public ListenableFuture<ResourceRequirement> submitRequirement(ResourceRequirement requirement) {
 String zkPath = CoordinationConstants.REQUIREMENTS_PATH + "/" + requirement.getName();
 return ZKExtOperations.createOrSet(zkClient, zkPath, Suppliers.ofInstance(requirement),
                   CoordinationConstants.RESOURCE_REQUIREMENT_CODEC,
                   CoordinationConstants.MAX_ZK_FAILURE_RETRY);
}

代码示例来源:origin: co.cask.cdap/cdap-common

/**
 * Submits the given {@link ResourceRequirement} for allocation.
 *
 * @param requirement The requirement to be submitted.
 * @return A {@link ListenableFuture} that will be completed when submission is completed and it'll carry the
 *         submitted requirement as result. The future will fail if failed to submit the requirement. Calling
 *         {@link ListenableFuture#cancel(boolean)} has no effect.
 */
public ListenableFuture<ResourceRequirement> submitRequirement(ResourceRequirement requirement) {
 String zkPath = CoordinationConstants.REQUIREMENTS_PATH + "/" + requirement.getName();
 return ZKExtOperations.createOrSet(zkClient, zkPath, Suppliers.ofInstance(requirement),
                   CoordinationConstants.RESOURCE_REQUIREMENT_CODEC,
                   CoordinationConstants.MAX_ZK_FAILURE_RETRY);
}

代码示例来源:origin: co.cask.cdap/cdap-app-fabric

@Override
public void store(final ProgramId serviceId, final RouteConfig routeConfig) {
 Supplier<RouteConfig> supplier = Suppliers.ofInstance(routeConfig);
 SettableFuture<RouteConfig> oldConfigFuture = routeConfigMap.get(serviceId);
 Future<RouteConfig> future = ZKExtOperations.createOrSet(zkClient, getZKPath(serviceId), supplier,
                              ROUTE_CONFIG_CODEC, 10);
 try {
  future.get(ZK_TIMEOUT_SECS, TimeUnit.SECONDS);
  SettableFuture<RouteConfig> newFuture = SettableFuture.create();
  newFuture.set(routeConfig);
  if (oldConfigFuture != null) {
   routeConfigMap.replace(serviceId, oldConfigFuture, newFuture);
  } else {
   routeConfigMap.putIfAbsent(serviceId, newFuture);
  }
 } catch (ExecutionException | InterruptedException | TimeoutException ex) {
  throw Throwables.propagate(ex);
 }
}

代码示例来源:origin: cdapio/cdap

@Override
public void store(final ProgramId serviceId, final RouteConfig routeConfig) {
 Supplier<RouteConfig> supplier = Suppliers.ofInstance(routeConfig);
 SettableFuture<RouteConfig> oldConfigFuture = routeConfigMap.get(serviceId);
 Future<RouteConfig> future = ZKExtOperations.createOrSet(zkClient, getZKPath(serviceId), supplier,
                              ROUTE_CONFIG_CODEC, 10);
 try {
  future.get(ZK_TIMEOUT_SECS, TimeUnit.SECONDS);
  SettableFuture<RouteConfig> newFuture = SettableFuture.create();
  newFuture.set(routeConfig);
  if (oldConfigFuture != null) {
   routeConfigMap.replace(serviceId, oldConfigFuture, newFuture);
  } else {
   routeConfigMap.putIfAbsent(serviceId, newFuture);
  }
 } catch (ExecutionException | InterruptedException | TimeoutException ex) {
  throw Throwables.propagate(ex);
 }
}

代码示例来源:origin: cdapio/cdap

@Override
public void put(final String name, final T instance) {
 final String znode = joinZNode(parentZnode, name);
 try {
  LOG.debug("Setting value for node {}", znode);
  final SettableFuture<T> completion = SettableFuture.create();
  Futures.addCallback(
   ZKExtOperations.createOrSet(zookeeper, znode, Suppliers.ofInstance(instance), codec, MAX_RETRIES, znodeACL),
   new FutureCallback<T>() {
    @Override
    public void onSuccess(T result) {
     LOG.debug("Created or set node {}", znode);
     resources.put(name, result);
     completion.set(result);
    }
    @Override
    public void onFailure(Throwable t) {
     LOG.error("Failed to set value for node {}", znode, t);
     listeners.notifyError(name, t);
     completion.setException(t);
    }
   }
  );
  // Block until it is done
  completion.get();
 } catch (Exception ioe) {
  throw Throwables.propagate(ioe);
 }
}

代码示例来源:origin: cdapio/cdap

@Test
public void testCreateOrSet() throws Exception {
 String path = "/parent/testCreateOrSet";
 ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build();
 zkClient.startAndWait();
 // Create with "1"
 Assert.assertEquals(1, ZKExtOperations.createOrSet(zkClient, path,
                           Suppliers.ofInstance(1), INT_CODEC, 0).get().intValue());
 // Should get "1" back
 Assert.assertEquals(1, INT_CODEC.decode(zkClient.getData(path).get().getData()).intValue());
 // Set with "2"
 Assert.assertEquals(2, ZKExtOperations.createOrSet(zkClient, path,
                           Suppliers.ofInstance(2), INT_CODEC, 0).get().intValue());
 // Should get "2" back
 Assert.assertEquals(2, INT_CODEC.decode(zkClient.getData(path).get().getData()).intValue());
 zkClient.stopAndWait();
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com