org.apache.curator.framework.recipes.nodes.PersistentNode Java Examples

The following examples show how to use org.apache.curator.framework.recipes.nodes.PersistentNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZKJobMasterRegistrar.java    From twister2 with Apache License 2.0 6 votes vote down vote up
/**
 * create the znode for the job master
 */
private boolean createJobMasterZnode() {
  String jobMasterIPandPort = jobMasterIP + ":" + jobMasterPort;
  try {
    jobMasterNode = new PersistentNode(
        client, CreateMode.EPHEMERAL, false, jobMasterPath, jobMasterIPandPort.getBytes());
    jobMasterNode.start();
    jobMasterNode.waitForInitialCreate(10000, TimeUnit.MILLISECONDS);
    jobMasterPath = jobMasterNode.getActualPath();
    LOG.info("An ephemeral znode is created for the Job Master: " + jobMasterPath);
    return true;
  } catch (Exception e) {
    LOG.log(Level.SEVERE, "Could not create znode for the Job Master: " + jobMasterIPandPort, e);
    return false;
  }
}
 
Example #2
Source File: CuratorUtil.java    From fluo with Apache License 2.0 6 votes vote down vote up
/**
 * Starts the ephemeral node and waits for it to be created
 *
 * @param node Node to start
 * @param maxWaitSec Maximum time in seconds to wait
 */
public static void startAndWait(PersistentNode node, int maxWaitSec) {
  node.start();
  int waitTime = 0;
  try {
    while (node.waitForInitialCreate(1, TimeUnit.SECONDS) == false) {
      waitTime += 1;
      log.info("Waited " + waitTime + " sec for ephemeral node to be created");
      if (waitTime > maxWaitSec) {
        throw new IllegalStateException("Failed to create ephemeral node");
      }
    }
  } catch (InterruptedException e) {
    throw new IllegalStateException(e);
  }
}
 
Example #3
Source File: ZKEphemStateManager.java    From twister2 with Apache License 2.0 5 votes vote down vote up
public static PersistentNode createWorkerZnode(CuratorFramework client,
                                               String rootPath,
                                               String jobID,
                                               int workerID) {

  String ephemDirPath = ZKUtils.ephemDir(rootPath, jobID);
  String workerPath = ZKUtils.workerPath(ephemDirPath, workerID);
  byte[] znodeBody = ("" + workerID).getBytes(StandardCharsets.UTF_8);

  // it is ephemeral and persistent
  // ephemeral: it will be deleted after the worker leaves or fails
  // persistent: it will be persistent for occasional network problems

  return new PersistentNode(client, CreateMode.PERSISTENT, true, workerPath, znodeBody);
}
 
Example #4
Source File: ClusterZKImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Register Host to cluster.
 *
 * @param host Host to be part of cluster.
 */
@Override
@Synchronized
public void registerHost(Host host) {
    Preconditions.checkNotNull(host, "host");
    Exceptions.checkArgument(!entryMap.containsKey(host), "host", "host is already registered to cluster.");

    String hostPath = ZKPaths.makePath(getPathPrefix(), host.toString());
    PersistentNode node = new PersistentNode(client, CreateMode.EPHEMERAL, false, hostPath,
            host.toBytes());

    node.start(); //start creation of ephemeral node in background.
    entryMap.put(host, node);
}
 
Example #5
Source File: ClusterZKImpl.java    From pravega with Apache License 2.0 5 votes vote down vote up
/**
 * Remove Host from cluster.
 *
 * @param host Host to be removed from cluster.
 */
@Override
@Synchronized
public void deregisterHost(Host host) {
    Preconditions.checkNotNull(host, "host");
    PersistentNode node = entryMap.get(host);
    Preconditions.checkNotNull(node, "Host is not present in cluster.");
    entryMap.remove(host);
    close(node);
}
 
Example #6
Source File: DefaultCurrentClusterServer.java    From x-pipe with Apache License 2.0 5 votes vote down vote up
@Override
protected void doStart() throws Exception {
	
	CuratorFramework client = zkClient.get();
	
	if(client.checkExists().forPath(serverPath) != null){ 
		
		byte []data = client.getData().forPath(serverPath);
		throw new IllegalStateException("server already exist:" + new String(data));
	}

	persistentNode = new PersistentNode(zkClient.get(), CreateMode.EPHEMERAL, false, serverPath, Codec.DEFAULT.encodeAsBytes(getClusterInfo()));
	persistentNode.start();
}
 
Example #7
Source File: CuratorSingletonService.java    From attic-aurora with Apache License 2.0 5 votes vote down vote up
void advertise(
    Closer closer,
    InetSocketAddress endpoint,
    Map<String, InetSocketAddress> additionalEndpoints)
    throws AdvertiseException, InterruptedException {

  byte[] nodeData = serializeAdvertisement(endpoint, additionalEndpoints);
  PersistentNode persistentNode =
      new PersistentNode(
          client,
          CreateMode.EPHEMERAL_SEQUENTIAL,

          // TODO(John Sirois): Enable GUID protection once clients are updated to support
          // its effects on group member node naming.  We get nodes like:
          //   4f5f98c4-8e71-41e3-8c8d-1c9a1f5f5df9-member_000000001
          // Clients expect member_ is the prefix and are not prepared for the GUID.
          false /* GUID protection */,

          ZKPaths.makePath(groupPath, memberToken),
          nodeData);
  persistentNode.start();
  closer.register(persistentNode);

  // NB: This blocks on initial server set node population to emulate legacy
  // SingletonService.LeaderControl.advertise (Group.join) behavior. Asynchronous
  // population is an option though, we simply need to remove this wait.
  if (!persistentNode.waitForInitialCreate(Long.MAX_VALUE, TimeUnit.DAYS)) {
    throw new AdvertiseException("Timed out waiting for leader advertisement.");
  }
}
 
Example #8
Source File: TransactorNode.java    From fluo with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a transactor node using given transactor id
 *
 * @param env Environment
 * @param tid Transactor ID used to create node
 */
public TransactorNode(Environment env, TransactorID tid) {
  this.env = env;
  this.tid = tid;
  node = new PersistentNode(env.getSharedResources().getCurator(), CreateMode.EPHEMERAL, false,
      getNodePath(), tid.toString().getBytes());
  CuratorUtil.startAndWait(node, 10);
  status = TrStatus.OPEN;
}
 
Example #9
Source File: PartitionManager.java    From fluo with Apache License 2.0 5 votes vote down vote up
PartitionManager(Environment env, long minSleepTime, long maxSleepTime) {
  try {
    this.curator = env.getSharedResources().getCurator();
    this.env = env;

    this.minSleepTime = minSleepTime;
    this.maxSleepTime = maxSleepTime;
    this.retrySleepTime = minSleepTime;

    groupSize = env.getConfiguration().getInt(FluoConfigurationImpl.WORKER_PARTITION_GROUP_SIZE,
        FluoConfigurationImpl.WORKER_PARTITION_GROUP_SIZE_DEFAULT);

    myESNode = new PersistentNode(curator, CreateMode.EPHEMERAL_SEQUENTIAL, false,
        ZookeeperPath.FINDERS + "/" + ZK_FINDER_PREFIX, ("" + groupSize).getBytes(UTF_8));
    myESNode.start();
    myESNode.waitForInitialCreate(1, TimeUnit.MINUTES);

    childrenCache = new PathChildrenCache(curator, ZookeeperPath.FINDERS, true);
    childrenCache.getListenable().addListener(new FindersListener());
    childrenCache.start(StartMode.BUILD_INITIAL_CACHE);

    schedExecutor = Executors.newScheduledThreadPool(1,
        new FluoThreadFactory("Fluo worker partition manager"));
    schedExecutor.scheduleWithFixedDelay(new CheckTabletsTask(), 0, maxSleepTime,
        TimeUnit.MILLISECONDS);

    scheduleUpdate();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #10
Source File: ZKUtils.java    From twister2 with Apache License 2.0 4 votes vote down vote up
/**
 * create a PersistentNode object in the given path
 * it needs to be deleted explicitly, not ephemeral
 * it will be persistent for occasional network problems
 */
public static PersistentNode createPersistentZnode(String path,
                                                   byte[] payload) {

  return new PersistentNode(client, CreateMode.PERSISTENT, true, path, payload);
}
 
Example #11
Source File: PersistentNodeTest.java    From x-pipe with Apache License 2.0 3 votes vote down vote up
@Test
public void testNode() throws IOException{
	
	String path = "/" + getTestName();
	PersistentNode persistentNode = new PersistentNode(client, CreateMode.EPHEMERAL, false, path, "123456".getBytes());
	persistentNode.start();
	
	waitForAnyKeyToExit();
	
	persistentNode.close();
	
}
 
Example #12
Source File: ZKUtils.java    From twister2 with Apache License 2.0 2 votes vote down vote up
/**
 * create a PersistentNode object in the given path
 * it is ephemeral and persistent
 * it will be deleted after the worker leaves or fails
 * it will be persistent for occasional network problems
 */
public static PersistentNode createPersistentEphemeralZnode(String path,
                                                            byte[] payload) {

  return new PersistentNode(client, CreateMode.EPHEMERAL, true, path, payload);
}