Java Code Examples for org.apache.zookeeper.ZooKeeper#create()

The following examples show how to use org.apache.zookeeper.ZooKeeper#create() . 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: TestReadOnlyZKClient.java    From hbase with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
  final int port = UTIL.startMiniZKCluster().getClientPort();
  String hostPort = UTIL.getZkCluster().getAddress().toString();

  ZooKeeper zk = ZooKeeperHelper.getConnectedZooKeeper(hostPort, 10000);
  DATA = new byte[10];
  ThreadLocalRandom.current().nextBytes(DATA);
  zk.create(PATH, DATA, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  for (int i = 0; i < CHILDREN; i++) {
    zk.create(PATH + "/c" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
  }
  zk.close();
  Configuration conf = UTIL.getConfiguration();
  conf.set(HConstants.ZOOKEEPER_QUORUM, hostPort);
  conf.setInt(ReadOnlyZKClient.RECOVERY_RETRY, 3);
  conf.setInt(ReadOnlyZKClient.RECOVERY_RETRY_INTERVAL_MILLIS, 100);
  conf.setInt(ReadOnlyZKClient.KEEPALIVE_MILLIS, 3000);
  RO_ZK = new ReadOnlyZKClient(conf);
  // only connect when necessary
  assertNull(RO_ZK.zookeeper);
}
 
Example 2
Source File: BrokerBookieIsolationTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void setDefaultIsolationGroup(String brokerBookkeeperClientIsolationGroups, ZooKeeper zkClient,
        Set<BookieSocketAddress> bookieAddresses) throws Exception {
    BookiesRackConfiguration bookies = null;
    try {
        byte[] data = zkClient.getData(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, false, null);
        System.out.println(new String(data));
        bookies = jsonMapper.readValue(data, BookiesRackConfiguration.class);
    } catch (KeeperException.NoNodeException e) {
        // Ok.. create new bookie znode
        zkClient.create(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, "".getBytes(), Acl,
                CreateMode.PERSISTENT);
    }
    if (bookies == null) {
        bookies = new BookiesRackConfiguration();
    }

    Map<String, BookieInfo> bookieInfoMap = Maps.newHashMap();
    for (BookieSocketAddress bkSocket : bookieAddresses) {
        BookieInfo info = new BookieInfo("use", bkSocket.getHostName() + ":" + bkSocket.getPort());
        bookieInfoMap.put(bkSocket.toString(), info);
    }
    bookies.put(brokerBookkeeperClientIsolationGroups, bookieInfoMap);

    zkClient.setData(ZkBookieRackAffinityMapping.BOOKIE_INFO_ROOT_PATH, jsonMapper.writeValueAsBytes(bookies), -1);
}
 
Example 3
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 4
Source File: WriteLock.java    From disconf with Apache License 2.0 6 votes vote down vote up
/**
 * find if we have been created earler if not create our node
 *
 * @param prefix    the prefix node
 * @param zookeeper teh zookeeper client
 * @param dir       the dir paretn
 *
 * @throws KeeperException
 * @throws InterruptedException
 */
private void findPrefixInChildren(String prefix, ZooKeeper zookeeper, String dir)
    throws KeeperException, InterruptedException {
    List<String> names = zookeeper.getChildren(dir, false);
    for (String name : names) {
        if (name.startsWith(prefix)) {
            id = name;
            if (LOG.isDebugEnabled()) {
                LOG.debug("Found id created last time: " + id);
            }
            break;
        }
    }
    if (id == null) {
        id = zookeeper.create(dir + "/" + prefix, data, getAcl(), EPHEMERAL_SEQUENTIAL);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Created id: " + id);
        }
    }

}
 
Example 5
Source File: ZookeeperCoordinatorRepository.java    From hmily with Apache License 2.0 6 votes vote down vote up
private void connect(final HmilyZookeeperConfig config) {
    try {
        zooKeeper = new ZooKeeper(config.getHost(), config.getSessionTimeOut(), watchedEvent -> {
            if (watchedEvent.getState() == Watcher.Event.KeeperState.SyncConnected) {
                LATCH.countDown();
            }
        });
        LATCH.await();
        Stat stat = zooKeeper.exists(rootPathPrefix, false);
        if (stat == null) {
            zooKeeper.create(rootPathPrefix, rootPathPrefix.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
    } catch (Exception e) {
        throw new HmilyRuntimeException(e);
    }
}
 
Example 6
Source File: TestDistributedLogBase.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
protected void ensureURICreated(ZooKeeper zkc, URI uri) throws Exception {
    try {
        zkc.create(uri.getPath(), new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException nee) {
        // ignore
    }
}
 
Example 7
Source File: ZookeeperTest.java    From uncode-schedule with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCreateTask() throws Exception {
	ZooKeeper zk = new ZooKeeper("localhost:2181", 3000, null);
	List<ACL> acls = new ArrayList<ACL>();
	zk.addAuthInfo("digest", "ScheduleAdmin:password".getBytes());
	acls.add(new ACL(ZooDefs.Perms.ALL, new Id("digest",
			DigestAuthenticationProvider.generateDigest("ScheduleAdmin:password"))));
	acls.add(new ACL(ZooDefs.Perms.READ, Ids.ANYONE_ID_UNSAFE));
	zk.create("/uncode/schedule/task/taskObj#print", new byte[0], acls, CreateMode.PERSISTENT);
	zk.getData("/uncode/schedule/task/taskObj#print", false, null);
}
 
Example 8
Source File: BlurUtil.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
public static String lockForSafeMode(ZooKeeper zookeeper, String nodeName, String cluster) throws KeeperException,
    InterruptedException {
  LOG.info("Getting safe mode lock.");
  final Object lock = new Object();
  String blurSafemodePath = ZookeeperPathConstants.getSafemodePath(cluster);
  String newPath = zookeeper.create(blurSafemodePath + "/safemode-", nodeName.getBytes(), Ids.OPEN_ACL_UNSAFE,
      CreateMode.EPHEMERAL_SEQUENTIAL);
  Watcher watcher = new Watcher() {
    @Override
    public void process(WatchedEvent event) {
      synchronized (lock) {
        lock.notifyAll();
      }
    }
  };
  while (true) {
    synchronized (lock) {
      List<String> children = new ArrayList<String>(zookeeper.getChildren(blurSafemodePath, watcher));
      Collections.sort(children);
      if (newPath.equals(blurSafemodePath + "/" + children.get(0))) {
        LOG.info("Lock aquired.");
        return newPath;
      } else {
        lock.wait(BlurConstants.ZK_WAIT_TIME);
      }
    }
  }
}
 
Example 9
Source File: SnowflakeGenerator.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
/**
 * 需要zookeeper保存节点信息
 */
public SnowflakeGenerator() {
	try {
		String appName = ResourceUtils.getProperty("spring.application.name", ResourceUtils.getProperty("jeesuite.configcenter.appName"));
		Validate.notBlank(appName, "config[spring.application.name] not found");
		String zkServer = ResourceUtils.getAndValidateProperty("zookeeper.servers");
		
		zk = new ZooKeeper(zkServer, 10000, this);
		String path = String.format(ROOT_PATH, appName);
		
		String[] parts = StringUtils.split(path, "/");
		String tmpParent = "";
		Stat stat;
		for (int i = 0; i < parts.length; i++) {
			tmpParent = tmpParent + "/" + parts[i];
			stat = zk.exists(tmpParent, false);
			if (stat == null) {
				zk.create(tmpParent, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
			}
		}
		String nodePath = path + "/" + NodeNameHolder.getNodeId();
		zk.create(nodePath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
		int workerId = zk.getChildren(path, false).size();
		if (workerId > maxWorkerId || workerId < 0) {
			throw new IllegalArgumentException(
					String.format("worker Id can't be greater than %d or less than 0", maxWorkerId));
		}
		this.workerId = workerId;
	} catch (Exception e) {
		this.workerId = RandomUtils.nextInt(1, 31);
	}
	this.datacenterId = 1;
}
 
Example 10
Source File: ZKTools.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void createPath(ZooKeeper zk, String path,CreateMode createMode, List<ACL> acl) throws Exception {
	String[] list = path.split("/");
	String zkPath = "";
	for (String str : list) {
		if (str.equals("") == false) {
			zkPath = zkPath + "/" + str;
			if (zk.exists(zkPath, false) == null) {
				zk.create(zkPath, null, acl, createMode);
			}
		}
	}
}
 
Example 11
Source File: ManagedLedgerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@Test(timeOut = 20000)
public void testEmptyManagedLedgerContent() throws Exception {
    ZooKeeper zk = bkc.getZkHandle();
    zk.create("/managed-ledger", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.create("/managed-ledger/my_test_ledger", " ".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);

    ManagedLedger ledger = factory.open("my_test_ledger");
    ledger.openCursor("test");

    ledger.addEntry("entry-1".getBytes(Encoding));
    assertEquals(ledger.getNumberOfEntries(), 1);
}
 
Example 12
Source File: ZookeeperResourceTest.java    From spring-zookeeper with Apache License 2.0 5 votes vote down vote up
@Ignore
@Test
public void insertData2Zk() throws KeeperException, InterruptedException, IOException {
    ZookeeperConfigurer zkConf = (ZookeeperConfigurer) ctx.getBean("zkPropConfigurer");
    ZookeeperResource zkResource = (ZookeeperResource) zkConf.getZkResoucre();
    ZooKeeper zk = zkResource.getZk();
    try {
        zk.delete("/cn_dev", -1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    String fileName = "/zk_cnfig_cn_test.txt";
    String fileContent = getFileContent(fileName);
    zk.create("/cn_dev", fileContent.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
 
Example 13
Source File: ZooKeeperOutput.java    From envelope with Apache License 2.0 5 votes vote down vote up
private void prepareZnode(ZooKeeper zk, String znode) throws KeeperException, InterruptedException {
  String[] znodeParts = znode.split(Pattern.quote("/"));
  
  String znodePrefix = "";
  for (String znodePart : znodeParts) {
    if (znodePart.length() > 0) {
      znodePrefix += "/" + znodePart;
      
      if (zk.exists(znodePrefix, false) == null) {
        zk.create(znodePrefix, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
      }
    }
  }
}
 
Example 14
Source File: ZkCopyIT.java    From zkcopy with Apache License 2.0 5 votes vote down vote up
@Before
public void addData() throws IOException, KeeperException, InterruptedException {
    ZooKeeper zk = connectTo(zkSource);
    for(String path : new String[] {"/itest", "/itest/persistent", "/itest/ephemeral"}) {
        zk.create(path, path.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    for (int i = 0; i < 200; i++) {
        zk.create(String.format("/itest/persistent/node-%s", i), Integer.toString(i).getBytes(),
                Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    zk.close();
}
 
Example 15
Source File: TestDistributedLock.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private static void createLockPath(ZooKeeper zk, String lockPath) throws Exception {
    zk.create(lockPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
 
Example 16
Source File: TestZKSessionLock.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private static String createLockNodeV1(ZooKeeper zk, String lockPath, String clientId) throws Exception {
    return zk.create(getLockPathPrefixV1(lockPath), serializeClientId(clientId),
            ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
}
 
Example 17
Source File: DistributedQueue.java    From yuzhouwan with Apache License 2.0 4 votes vote down vote up
private static void produce(ZooKeeper zk, int zNode) throws KeeperException, InterruptedException {

        System.out.println("Adding\t" + zNode + "\tinto queue [/distributedQueue] ...");
        zk.create("/distributedQueue/" + zNode, (zNode + "").getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL_SEQUENTIAL);
    }
 
Example 18
Source File: ZKPaths.java    From xian with Apache License 2.0 4 votes vote down vote up
/**
 * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish
 * between directories and files. So, every node in the path is created. The data for each node is an empty blob
 *
 * @param zookeeper    the client
 * @param path         path to ensure
 * @param makeLastNode if true, all nodes are created. If false, only the parent nodes are created
 * @param aclProvider  if not null, the ACL provider to use when creating parent nodes
 * @param asContainers if true, nodes are created as  CreateMode#CONTAINER
 * @throws InterruptedException                 thread interruption
 * @throws org.apache.zookeeper.KeeperException Zookeeper errors
 */
public static void mkdirs(ZooKeeper zookeeper, String path, boolean makeLastNode, InternalACLProvider aclProvider, boolean asContainers) throws InterruptedException, KeeperException
{
    PathUtils.validatePath(path);

    int pos = 1; // skip first slash, root is guaranteed to exist
    do
    {
        pos = path.indexOf(PATH_SEPARATOR, pos + 1);

        if ( pos == -1 )
        {
            if ( makeLastNode )
            {
                pos = path.length();
            }
            else
            {
                break;
            }
        }

        String subPath = path.substring(0, pos);
        if ( zookeeper.exists(subPath, false) == null )
        {
            try
            {
                List<ACL> acl = null;
                if ( aclProvider != null )
                {
                    acl = aclProvider.getAclForPath(subPath);
                    if ( acl == null )
                    {
                        acl = aclProvider.getDefaultAcl();
                    }
                }
                if ( acl == null )
                {
                    acl = ZooDefs.Ids.OPEN_ACL_UNSAFE;
                }
                zookeeper.create(subPath, new byte[0], acl, getCreateMode(asContainers));
            }
            catch ( KeeperException.NodeExistsException e )
            {
                // ignore... someone else has created it since we checked
            }
        }

    }
    while ( pos < path.length() );
}
 
Example 19
Source File: ZnodeService.java    From shark with Apache License 2.0 3 votes vote down vote up
/**
 * 检查childrenNode节点是否存在
 * 
 * @author gaoxianglong
 * 
 * @param zk_client
 *            session会话
 * 
 * @param childrenNodePath
 *            zk子节点
 * 
 * @param memData
 *            内存占位数
 * 
 * @return void
 */
private void isChildrenNode(ZooKeeper zk_client, String childrenNodePath, long memData) throws Exception {
	if (null == zk_client.exists(childrenNodePath, false)) {
		zk_client.create(childrenNodePath, String.valueOf(memData).getBytes(), Ids.OPEN_ACL_UNSAFE,
				CreateMode.PERSISTENT);
	}
}
 
Example 20
Source File: ZookeeperClientTest.java    From ignite with Apache License 2.0 3 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testSaveLargeValue() throws Exception {
    startZK(1);

    final ZookeeperClient client = createClient(SES_TIMEOUT);

    byte[] data = new byte[1024 * 1024];

    String basePath = "/ignite";

    assertTrue(client.needSplitNodeData(basePath, data, 2));

    List<byte[]> parts = client.splitNodeData(basePath, data, 2);

    assertTrue(parts.size() > 1);

    ZooKeeper zk = client.zk();

    for (int i = 0; i < parts.size(); i++) {
        byte[] part = parts.get(i);

        assertTrue(part.length > 0);

        String path0 = basePath + ":" + i;

        zk.create(path0, part, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
}