org.apache.zookeeper.ZooDefs.Ids Java Examples

The following examples show how to use org.apache.zookeeper.ZooDefs.Ids. 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: PulsarClusterMetadataSetup.java    From pulsar with Apache License 2.0 6 votes vote down vote up
static void createPartitionedTopic(ZooKeeper configStoreZk, TopicName topicName, int numPartitions) throws KeeperException, InterruptedException, IOException {
    String partitionedTopicPath = ZkAdminPaths.partitionedTopicPath(topicName);
    Stat stat = configStoreZk.exists(partitionedTopicPath, false);
    PartitionedTopicMetadata metadata = new PartitionedTopicMetadata(numPartitions);
    if (stat == null) {
        createZkNode(
            configStoreZk,
            partitionedTopicPath,
            ObjectMapperFactory.getThreadLocal().writeValueAsBytes(metadata),
            ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT
        );
    } else {
        byte[] content = configStoreZk.getData(partitionedTopicPath, false, null);
        PartitionedTopicMetadata existsMeta = ObjectMapperFactory.getThreadLocal().readValue(content, PartitionedTopicMetadata.class);

        // Only update z-node if the partitions should be modified
        if (existsMeta.partitions < numPartitions) {
            configStoreZk.setData(
                    partitionedTopicPath,
                    ObjectMapperFactory.getThreadLocal().writeValueAsBytes(metadata),
                    stat.getVersion()
            );
        }
    }
}
 
Example #2
Source File: EditLogLedgerMetadata.java    From hadoop with Apache License 2.0 6 votes vote down vote up
void write(ZooKeeper zkc, String path)
    throws IOException, KeeperException.NodeExistsException {
  this.zkPath = path;

  EditLogLedgerProto.Builder builder = EditLogLedgerProto.newBuilder();
  builder.setDataLayoutVersion(dataLayoutVersion)
    .setLedgerId(ledgerId).setFirstTxId(firstTxId);

  if (!inprogress) {
    builder.setLastTxId(lastTxId);
  }
  try {
    zkc.create(path, TextFormat.printToString(builder.build()).getBytes(UTF_8),
               Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  } catch (KeeperException.NodeExistsException nee) {
    throw nee;
  } catch (KeeperException e) {
    throw new IOException("Error creating ledger znode", e);
  } catch (InterruptedException ie) {
    throw new IOException("Interrupted creating ledger znode", ie);
  }
}
 
Example #3
Source File: Writer.java    From zkcopy with Apache License 2.0 6 votes vote down vote up
/**
 * Updates or creates the given node.
 * 
 * @param node
 *            The node to copy
 * @throws KeeperException
 *             If the server signals an error
 * @throws InterruptedException
 *             If the server transaction is interrupted
 */
private void upsertNode(Node node) throws KeeperException, InterruptedException {
    String nodePath = node.getAbsolutePath();
    // 1. Update or create current node
    Stat stat = zk.exists(nodePath, false);
    if (stat != null) {
        logger.debug("Attempting to update " + nodePath);
        transaction.setData(nodePath, node.getData(), -1);
        nodesUpdated++;
    } else {
        logger.debug("Attempting to create " + nodePath);
        transaction.create(nodePath, node.getData(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        nodesCreated++;
    }
    if (nodesUpdated % 100 == 0) {
        logger.debug(String.format("Updated: %s, current node mtime %s", nodesUpdated, node.getMtime()));
    }
}
 
Example #4
Source File: ZookeeperUseSimple.java    From game-server with MIT License 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	ZooKeeper zookeeper = new ZooKeeper("127.0.0.1:2181", 5000, new ZookeeperUseSimple());
	System.out.println(zookeeper.getState());
	try {
		connectedSemaphore.await();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
	System.out.println("Zookeeper session established");
	
	//创建节点
	String path1 = zookeeper.create("/zk-test-ephemeral-", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);    
       System.out.println("Success create znode: " + path1);
       
       String path2 = zookeeper.create("/zk-test-ephemeral-", "".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);    
       System.out.println("Success create znode: " + path2);
}
 
Example #5
Source File: TestSplitLogWorker.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testAcquireTaskAtStartup() throws Exception {
  LOG.info("testAcquireTaskAtStartup");
  SplitLogCounters.resetCounters();
  final String TATAS = "tatas";
  final ServerName RS = ServerName.valueOf("rs,1,1");
  RegionServerServices mockedRS = getRegionServer(RS);
  zkw.getRecoverableZooKeeper().create(ZKSplitLog.getEncodedNodeName(zkw, TATAS),
    new SplitLogTask.Unassigned(ServerName.valueOf("mgr,1,1")).toByteArray(),
      Ids.OPEN_ACL_UNSAFE,
      CreateMode.PERSISTENT);

  SplitLogWorker slw =
      new SplitLogWorker(ds, TEST_UTIL.getConfiguration(), mockedRS, neverEndingTask);
  slw.start();
  try {
    waitForCounter(SplitLogCounters.tot_wkr_task_acquired, 0, 1, WAIT_TIME);
    byte [] bytes = ZKUtil.getData(zkw, ZKSplitLog.getEncodedNodeName(zkw, TATAS));
    SplitLogTask slt = SplitLogTask.parseFrom(bytes);
    assertTrue(slt.isOwned(RS));
  } finally {
   stopSplitLogWorker(slw);
  }
}
 
Example #6
Source File: ZKManager.java    From tbschedule with Apache License 2.0 6 votes vote down vote up
private void createZookeeper(final CountDownLatch connectionLatch) throws Exception {
    zk = new ZooKeeper(this.properties.getProperty(keys.zkConnectString.toString()),
        Integer.parseInt(this.properties.getProperty(keys.zkSessionTimeout.toString())),
        new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                sessionEvent(connectionLatch, event);
            }
        });
    String authString = this.properties.getProperty(keys.userName.toString()) + ":" + this.properties
        .getProperty(keys.password.toString());
    this.isCheckParentPath = Boolean
        .parseBoolean(this.properties.getProperty(keys.isCheckParentPath.toString(), "true"));
    zk.addAuthInfo("digest", authString.getBytes());
    acl.clear();
    acl.add(new ACL(ZooDefs.Perms.ALL, new Id("digest", DigestAuthenticationProvider.generateDigest(authString))));
    acl.add(new ACL(ZooDefs.Perms.READ, Ids.ANYONE_ID_UNSAFE));
}
 
Example #7
Source File: ZKManager.java    From uncode-schedule with Apache License 2.0 6 votes vote down vote up
private void createZookeeper(final CountDownLatch connectionLatch) throws Exception {
  zk = new ZooKeeper(this.properties.getProperty(keys.zkConnectString
      .toString()), Integer.parseInt(this.properties
      .getProperty(keys.zkSessionTimeout.toString())),
      new Watcher() {
        public void process(WatchedEvent event) {
          sessionEvent(connectionLatch, event);
        }
      });
  String authString = this.properties.getProperty(keys.userName.toString())
      + ":" + this.properties.getProperty(keys.password.toString());
  zk.addAuthInfo("digest", authString.getBytes());
  acl.clear();
  acl.add(new ACL(ZooDefs.Perms.ALL, new Id("digest",
      DigestAuthenticationProvider.generateDigest(authString))));
  acl.add(new ACL(ZooDefs.Perms.READ, Ids.ANYONE_ID_UNSAFE));
}
 
Example #8
Source File: TestApi.java    From jframe with Apache License 2.0 6 votes vote down vote up
public void testCreate() {
    class CreateCallback implements StringCallback {
        @Override
        public void processResult(int rc, String path, Object ctx, String name) {
            LOG.info("code->{} path->{}", rc, path);
            switch (Code.get(rc)) {
            case CONNECTIONLOSS:
                // TODO re-create
                break;
            case OK:
                break;
            case NODEEXISTS:
                break;
            default:
                LOG.error("error code->{} path->{}", rc, path);
            }
        }

    }

    if (zk != null)
        zk.create("/test", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new CreateCallback(), null);
}
 
Example #9
Source File: ZKManager.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
private void createZookeeper(final CountDownLatch connectionLatch) throws Exception {
	zk = new ZooKeeper(this.properties.getProperty(keys.zkConnectString
			.toString()), Integer.parseInt(this.properties
			.getProperty(keys.zkSessionTimeout.toString())),
			new Watcher() {
				public void process(WatchedEvent event) {
					sessionEvent(connectionLatch, event);
				}
			});
	String authString = this.properties.getProperty(keys.userName.toString())
			+ ":"+ this.properties.getProperty(keys.password.toString());
	this.isCheckParentPath = Boolean.parseBoolean(this.properties.getProperty(keys.isCheckParentPath.toString(),"true"));
	zk.addAuthInfo("digest", authString.getBytes());
	acl.clear();
	acl.add(new ACL(ZooDefs.Perms.ALL, new Id("digest",
			DigestAuthenticationProvider.generateDigest(authString))));
	acl.add(new ACL(ZooDefs.Perms.READ, Ids.ANYONE_ID_UNSAFE));
}
 
Example #10
Source File: ZkConnect.java    From bidder with Apache License 2.0 6 votes vote down vote up
public void createRegion(String path, String region) throws Exception {
	String spath = path + "/regions/" + region;
	zk.create(spath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	spath += "/bidders";
	zk.create(spath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	zk.create(spath + CONFIG, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	updateNode(spath + CONFIG, "".getBytes());
	zk.create(spath + DATABASE, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	updateNode(spath + DATABASE, "".getBytes());

	spath = path + "/regions/" + region + "/crosstalk";
	zk.create(spath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	zk.create(spath + CONFIG, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	updateNode(spath + CONFIG, "".getBytes());
	zk.create(spath + STATUS, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	updateNode(spath + STATUS, "".getBytes());
}
 
Example #11
Source File: TestZKLogStreamMetadataStore.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Test(timeout = 60000, expected = LockingException.class)
public void testRenameLockedLog() throws Exception {
    String logName = testName.getMethodName();
    String logIdentifier = "<default>";
    int numSegments = 5;
    createLog(
        zkc,
        uri,
        logName,
        logIdentifier,
        numSegments);

    // create a lock
    String logRootPath = getLogRootPath(uri, logName, logIdentifier);
    String lockPath = logRootPath + LOCK_PATH;
    zkc.get().create(lockPath + "/test", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);

    String newLogName = "path/to/new/" + logName;
    FutureUtils.result(metadataStore.renameLog(uri, logName, newLogName));
}
 
Example #12
Source File: LocalZooKeeperConnectionService.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static String createIfAbsent(ZooKeeper zk, String path, byte[] data, CreateMode createMode, boolean gc)
        throws KeeperException, InterruptedException {
    String pathCreated = null;
    try {
        pathCreated = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode);
    } catch (NodeExistsException e) {
        // OK
        LOG.debug("Create skipped for existing znode: path={}", path);
    }
    // reset if what exists is the ephemeral garbage.
    if (gc && (pathCreated == null) && CreateMode.EPHEMERAL.equals(createMode)) {
        Stat stat = zk.exists(path, false);
        if (stat != null && zk.getSessionId() != stat.getEphemeralOwner()) {
            deleteIfExists(zk, path, -1);
            pathCreated = zk.create(path, data, Ids.OPEN_ACL_UNSAFE, createMode);
        }
    }
    return pathCreated;
}
 
Example #13
Source File: TestActiveStandbyElector.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that, when the callback fails to enter active state,
 * the elector rejoins the election after sleeping for a short period.
 */
@Test
public void testFailToBecomeActive() throws Exception {
  mockNoPriorActive();
  elector.joinElection(data);
  Assert.assertEquals(0, elector.sleptFor);
  
  Mockito.doThrow(new ServiceFailedException("failed to become active"))
      .when(mockApp).becomeActive();
  elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  // Should have tried to become active
  Mockito.verify(mockApp).becomeActive();
  
  // should re-join
  Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data,
      Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);
  Assert.assertEquals(2, count);
  Assert.assertTrue(elector.sleptFor > 0);
}
 
Example #14
Source File: TestActiveStandbyElector.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * verify that if create znode results in nodeexists and that znode is deleted
 * before exists() watch is set then the return of the exists() method results
 * in attempt to re-create the znode and become active
 */
@Test
public void testCreateNodeResultRetryNoNode() {
  elector.joinElection(data);

  elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  verifyExistCall(1);

  elector.processResult(Code.NONODE.intValue(), ZK_LOCK_NAME, mockZK,
      (Stat) null);
  Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();
  Mockito.verify(mockZK, Mockito.times(4)).create(ZK_LOCK_NAME, data,
      Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);
}
 
Example #15
Source File: ZooKeeperEndpointGroupTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
private static void setCuratorXNodeChildren(List<Endpoint> children, int startingIdNumber)
        throws Throwable {
    final String servicePath = Z_NODE + '/' + CURATOR_X_SERVICE_NAME;
    try (CloseableZooKeeper zk = zkInstance.connection()) {
        // If the parent node does not exist, create it.
        if (!zk.exists(Z_NODE).get()) {
            zk.create(Z_NODE, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        if (!zk.exists(servicePath).get()) {
            zk.create(servicePath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }

        final ObjectMapper mapper = new ObjectMapper();
        // Register all child nodes.
        for (int i = 0; i < children.size(); i++) {
            zk.create(servicePath + '/' + (i + startingIdNumber),
                      mapper.writeValueAsBytes(serviceInstance(children.get(i), i + startingIdNumber)),
                      Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    }

    for (int i = 0; i < children.size(); i++) {
        zkInstance.assertExists(servicePath + '/' + (i + startingIdNumber));
    }
}
 
Example #16
Source File: TestActiveStandbyElector.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Test for a bug encountered during development of HADOOP-8163:
 * ensureBaseNode() should throw an exception if it has to retry
 * more than 3 times to create any part of the path.
 */
@Test
public void testEnsureBaseNodeFails() throws Exception {
  Mockito.doThrow(new KeeperException.ConnectionLossException())
    .when(mockZK).create(
        Mockito.eq(ZK_PARENT_NAME), Mockito.<byte[]>any(),
        Mockito.eq(Ids.OPEN_ACL_UNSAFE), Mockito.eq(CreateMode.PERSISTENT));
  try {
    elector.ensureParentZNode();
    Assert.fail("Did not throw!");
  } catch (IOException ioe) {
    if (!(ioe.getCause() instanceof KeeperException.ConnectionLossException)) {
      throw ioe;
    }
  }
  // Should have tried three times
  Mockito.verify(mockZK, Mockito.times(3)).create(
      Mockito.eq(ZK_PARENT_NAME), Mockito.<byte[]>any(),
      Mockito.eq(Ids.OPEN_ACL_UNSAFE), Mockito.eq(CreateMode.PERSISTENT));
}
 
Example #17
Source File: TestActiveStandbyElector.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * verify that if create znode results in nodeexists and that znode is deleted
 * before exists() watch is set then the return of the exists() method results
 * in attempt to re-create the znode and become active
 */
@Test
public void testCreateNodeResultRetryNoNode() {
  elector.joinElection(data);

  elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  verifyExistCall(1);

  elector.processResult(Code.NONODE.intValue(), ZK_LOCK_NAME, mockZK,
      (Stat) null);
  Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();
  Mockito.verify(mockZK, Mockito.times(4)).create(ZK_LOCK_NAME, data,
      Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);
}
 
Example #18
Source File: TestActiveStandbyElector.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuitElectionRemovesBreadcrumbNode() throws Exception {
  mockNoPriorActive();
  elector.joinElection(data);
  elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  // Writes its own active info
  Mockito.verify(mockZK, Mockito.times(1)).create(
      Mockito.eq(ZK_BREADCRUMB_NAME), Mockito.eq(data),
      Mockito.eq(Ids.OPEN_ACL_UNSAFE),
      Mockito.eq(CreateMode.PERSISTENT));
  mockPriorActive(data);
  
  elector.quitElection(false);
  
  // Deletes its own active data
  Mockito.verify(mockZK, Mockito.times(1)).delete(
      Mockito.eq(ZK_BREADCRUMB_NAME), Mockito.eq(0));
}
 
Example #19
Source File: ZooKeeperService.java    From blog with MIT License 6 votes vote down vote up
String createNode(String node, boolean ephemeral) {
    String createdNodePath;
    try {

        final Stat nodeStat = zooKeeper.exists(node, false);

        if (nodeStat == null) {
            createdNodePath = zooKeeper.create(node, new byte[0], Ids.OPEN_ACL_UNSAFE, (ephemeral ? CreateMode.EPHEMERAL_SEQUENTIAL : CreateMode.PERSISTENT));
        } else {
            createdNodePath = node;
        }

    } catch (KeeperException | InterruptedException e) {
        throw new IllegalStateException(e);
    }

    return createdNodePath;
}
 
Example #20
Source File: TestActiveStandbyElector.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Verify that, when the callback fails to enter active state,
 * the elector rejoins the election after sleeping for a short period.
 */
@Test
public void testFailToBecomeActive() throws Exception {
  mockNoPriorActive();
  elector.joinElection(data);
  Assert.assertEquals(0, elector.sleptFor);
  
  Mockito.doThrow(new ServiceFailedException("failed to become active"))
      .when(mockApp).becomeActive();
  elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK,
      ZK_LOCK_NAME);
  // Should have tried to become active
  Mockito.verify(mockApp).becomeActive();
  
  // should re-join
  Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data,
      Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);
  Assert.assertEquals(2, count);
  Assert.assertTrue(elector.sleptFor > 0);
}
 
Example #21
Source File: TestZookeper.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static void test(ZooKeeper zk) throws KeeperException, InterruptedException {
	// 创建一个节点root,数据是mydata,不进行ACL权限控制,节点为永久性的(即客户端shutdown了也不会消失)
	zk.create("/root", "mydata".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	// /在root下面创建一个childone znode,数据为childone,不进行ACL权限控制,节点为永久性的
	zk.create("/root/childone", "childone".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	// 取得/root节点下的子节点名称,返回List<String>
	List<String> listStr = zk.getChildren("/root", true);
	for (String string : listStr)
		log.info(string);
	
	// 删除创建过的
	zk.delete("/root/childone", -1);
	zk.delete("/root", -1);
	
}
 
Example #22
Source File: TestSplitLogManager.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testOrphanTaskAcquisition() throws Exception {
  LOG.info("TestOrphanTaskAcquisition");

  String tasknode = ZKSplitLog.getEncodedNodeName(zkw, "orphan/test/slash");
  SplitLogTask slt = new SplitLogTask.Owned(master.getServerName());
  zkw.getRecoverableZooKeeper().create(tasknode, slt.toByteArray(), Ids.OPEN_ACL_UNSAFE,
      CreateMode.PERSISTENT);

  slm = new SplitLogManager(master, conf);
  waitForCounter(tot_mgr_orphan_task_acquired, 0, 1, to/2);
  Task task = findOrCreateOrphanTask(tasknode);
  assertTrue(task.isOrphan());
  waitForCounter(tot_mgr_heartbeat, 0, 1, to/2);
  assertFalse(task.isUnassigned());
  long curt = System.currentTimeMillis();
  assertTrue((task.last_update <= curt) &&
      (task.last_update > (curt - 1000)));
  LOG.info("waiting for manager to resubmit the orphan task");
  waitForCounter(tot_mgr_resubmit, 0, 1, to + to/2);
  assertTrue(task.isUnassigned());
  waitForCounter(tot_mgr_rescan, 0, 1, to + to/2);
}
 
Example #23
Source File: SafeMode.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
private void register(String node, byte[] data) throws KeeperException, InterruptedException {
  String p = _nodePath + "/" + node;
  long start = System.nanoTime();
  while (_zooKeeper.exists(p, false) != null) {
    if (start + _duplicateNodeTimeout < System.nanoTime()) {
      throw new RuntimeException("Node [" + node + "] cannot be registered, check to make sure a "
          + "process has not already been started or that server" + " names have not been duplicated.");
    }
    LOG.info("Node [{0}] already registered, waiting for path [{1}] to be released", node, p);
    String tmpPath = p + "_" + UUID.randomUUID();
    _zooKeeper.create(tmpPath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    Thread.sleep(1000);
    _zooKeeper.delete(tmpPath, -1);
  }
  _zooKeeper.create(p, data, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
}
 
Example #24
Source File: ZookeeperUtil.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @return
 */
public List<ACL> getCreateNodeAcls() {
    List<ACL> listAcls = new ArrayList<ACL>(3);
    try {
        Id id = new Id(PropertiesDynLoading.authScheme,
                DigestAuthenticationProvider.generateDigest(PropertiesDynLoading.accessKey));
        ACL acl = new ACL(Perms.CREATE, id);
        listAcls.add(acl);

    } catch (NoSuchAlgorithmException e) {

        e.printStackTrace();
        return Ids.OPEN_ACL_UNSAFE;
    }
    return listAcls;
}
 
Example #25
Source File: ZkCopyIT.java    From zkcopy with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimpleCopy() throws KeeperException, InterruptedException, IOException {
    ZooKeeper dest = connectTo(zkDest);
    ZooKeeper src = connectTo(zkSource);
    
    for (int i = 0; i < 10; i++) {
        src.create(String.format("/itest/ephemeral/node-%s", i), Integer.toString(i).getBytes(), Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL);
    }

    ZkCopy.main(new String[] {"-s", connectionString(zkSource, "itest"), "-t", connectionString(zkDest, "itest"), "--ignoreEphemeralNodes", "false"});
    
    List<String> persistentChildren = dest.getChildren("/itest/persistent", false);
    List<String> ephemeralChildren = dest.getChildren("/itest/ephemeral", false);
    dest.close();
    src.close();
    
    assertEquals("There should be 200 persistent nodes under /itest/persistent", 200, persistentChildren.size());
    assertEquals("There should be 10 ephemeral nodes under /itest/ephemeral", 10, ephemeralChildren.size());
}
 
Example #26
Source File: ZclientServerPub.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static void test() throws InterruptedException {
	final ZkClient zkClient4subChild = new ZkClient("localhost:2181");
	
	zkClient4subChild.create("/serverroot", "serverroot", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(4000);
	zkClient4subChild.create("/serverroot/server1", "server1", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(4000);
	zkClient4subChild.create("/serverroot/server2", "server2", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(20000);
	zkClient4subChild.delete("/serverroot/server1");
	zkClient4subChild.delete("/serverroot/server2");
	zkClient4subChild.delete("/serverroot");
}
 
Example #27
Source File: ZKBasedMasterElectionUtil.java    From phoenix with Apache License 2.0 6 votes vote down vote up
public static boolean acquireLock(ZKWatcher zooKeeperWatcher, String parentNode,
        String lockName) throws KeeperException, InterruptedException {
    // Create the parent node as Persistent
    LOGGER.info("Creating the parent lock node:" + parentNode);
    ZKUtil.createWithParents(zooKeeperWatcher, parentNode);

    // Create the ephemeral node
    String lockNode = parentNode + "/" + lockName;
    String nodeValue = getHostName() + "_" + UUID.randomUUID().toString();
    LOGGER.info("Trying to acquire the lock by creating node:" + lockNode + " value:" + nodeValue);
    // Create the ephemeral node
    try {
        zooKeeperWatcher.getRecoverableZooKeeper().create(lockNode, Bytes.toBytes(nodeValue),
            Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    } catch (KeeperException.NodeExistsException e) {
        LOGGER.info("Could not acquire lock. Another process had already acquired the lock on Node "
                + lockName);
        return false;
    }
    LOGGER.info("Obtained the lock :" + lockNode);
    return true;
}
 
Example #28
Source File: ZKSplitLogManagerCoordination.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * signal the workers that a task was resubmitted by creating the RESCAN node.
 */
private void rescan(long retries) {
  // The RESCAN node will be deleted almost immediately by the
  // SplitLogManager as soon as it is created because it is being
  // created in the DONE state. This behavior prevents a buildup
  // of RESCAN nodes. But there is also a chance that a SplitLogWorker
  // might miss the watch-trigger that creation of RESCAN node provides.
  // Since the TimeoutMonitor will keep resubmitting UNASSIGNED tasks
  // therefore this behavior is safe.
  SplitLogTask slt = new SplitLogTask.Done(this.details.getServerName());
  this.watcher
      .getRecoverableZooKeeper()
      .getZooKeeper()
      .create(ZKSplitLog.getRescanNode(watcher), slt.toByteArray(), Ids.OPEN_ACL_UNSAFE,
        CreateMode.EPHEMERAL_SEQUENTIAL, new CreateRescanAsyncCallback(), Long.valueOf(retries));
}
 
Example #29
Source File: ServiceUnitZkUtils.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private static void createZnodeOptimistic(ZooKeeper zkc, String path, String data, CreateMode mode)
        throws KeeperException, InterruptedException {
    try {
        // create node optimistically
        checkNotNull(LocalZooKeeperConnectionService.createIfAbsent(zkc, path, data, mode));
    } catch (NoNodeException e) {
        // if path contains multiple levels after the root, create the intermediate nodes first
        String[] parts = path.split("/");
        if (parts.length > 3) {
            String int_path = path.substring(0, path.lastIndexOf("/"));
            if (zkc.exists(int_path, false) == null) {
                // create the intermediate nodes
                try {
                    ZkUtils.createFullPathOptimistic(zkc, int_path, new byte[0], Ids.OPEN_ACL_UNSAFE,
                            CreateMode.PERSISTENT);
                } catch (KeeperException.NodeExistsException nee) {
                    LOG.debug(
                            "Other broker preempted the full intermediate path [{}] already. Continue for acquiring the leaf ephemeral node.",
                            int_path);
                }
            }
            checkNotNull(LocalZooKeeperConnectionService.createIfAbsent(zkc, path, data, mode));
        } else {
            // If failed to create immediate child of root node, throw exception
            throw e;
        }
    }
}
 
Example #30
Source File: TestActiveStandbyElector.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws IOException, KeeperException {
  count = 0;
  mockZK = Mockito.mock(ZooKeeper.class);
  mockApp = Mockito.mock(ActiveStandbyElectorCallback.class);
  elector = new ActiveStandbyElectorTester("hostPort", 1000, ZK_PARENT_NAME,
      Ids.OPEN_ACL_UNSAFE, mockApp);
}