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

The following examples show how to use org.apache.zookeeper.ZooKeeper#setData() . 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: ZooKeeperMigrator.java    From localization_nifi with Apache License 2.0 6 votes vote down vote up
private Stat transmitNode(ZooKeeper zooKeeper, DataStatAclNode node) {
    Preconditions.checkNotNull(zooKeeper, "zooKeeper must not be null");
    Preconditions.checkNotNull(node, "node must not be null");
    try {
        LOGGER.debug("attempting to transfer node to {} with ACL {}: {}", zooKeeperEndpointConfig, node.getAcls(), node);
        // set data without caring what the previous version of the data at that path
        zooKeeper.setData(node.getPath(), node.getData(), -1);
        zooKeeper.setACL(node.getPath(), node.getAcls(), -1);
        LOGGER.info("transferred node {} in {}", node, zooKeeperEndpointConfig);
    } catch (InterruptedException | KeeperException e) {
        if (e instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        throw new RuntimeException(String.format("unable to transmit data to %s for path %s", zooKeeper, node.getPath()), e);
    }
    return node.getStat();
}
 
Example 3
Source File: SimpleLoadManagerImplTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void createNamespacePolicies(PulsarService pulsar) throws Exception {
    NamespaceIsolationPolicies policies = new NamespaceIsolationPolicies();
    // set up policy that use this broker as primary
    NamespaceIsolationData policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/primary-ns.*");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add(pulsar1.getAdvertisedAddress() + "*");
    policyData.secondary = new ArrayList<String>();
    policyData.secondary.add("prod2-broker([78]).messaging.usw.example.co.*");
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "1");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "100");
    policies.setPolicy("primaryBrokerPolicy", policyData);

    ObjectMapper jsonMapper = ObjectMapperFactory.create();
    ZooKeeper globalZk = pulsar.getGlobalZkCache().getZooKeeper();
    ZkUtils.createFullPathOptimistic(globalZk, AdminResource.path("clusters", "use", "namespaceIsolationPolicies"),
            new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    byte[] content = jsonMapper.writeValueAsBytes(policies.getPolicies());
    globalZk.setData(AdminResource.path("clusters", "use", "namespaceIsolationPolicies"), content, -1);

}
 
Example 4
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 5
Source File: CuratorZookeeperClientTest.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@Test
public void getTest() throws Exception {
    ZooKeeper zooKeeper = createZookeeper();
    try {
        String testNodePath = createTestNodePath();

        curatorZookeeperClient.createNode(new CreateNodeMessage(testNodePath, "".getBytes()));
        Assert.assertTrue(isExistNode(zooKeeper, testNodePath));

        curatorZookeeperClient.getData(testNodePath, true);

        String message = createTestMessage();
        zooKeeper.setData(testNodePath, message.getBytes(), -1);
        assertGetWatchedEvent(testNodePath, message);

        message = createTestMessage();
        curatorZookeeperClient.createOrSetNode(new CreateNodeMessage(testNodePath, message.getBytes(), true));
        assertGetWatchedEvent(testNodePath, message);
    } finally {
        if (zooKeeper != null) {
            zooKeeper.close();
        }
    }
}
 
Example 6
Source File: ZooKeeperMigrator.java    From nifi with Apache License 2.0 6 votes vote down vote up
private Stat transmitNode(ZooKeeper zooKeeper, DataStatAclNode node) {
    Preconditions.checkNotNull(zooKeeper, "zooKeeper must not be null");
    Preconditions.checkNotNull(node, "node must not be null");
    try {
        LOGGER.debug("attempting to transfer node to {} with ACL {}: {}", zooKeeperEndpointConfig, node.getAcls(), node);
        // set data without caring what the previous version of the data at that path
        zooKeeper.setData(node.getPath(), node.getData(), -1);
        zooKeeper.setACL(node.getPath(), node.getAcls(), -1);
        LOGGER.info("transferred node {} in {}", node, zooKeeperEndpointConfig);
    } catch (InterruptedException | KeeperException e) {
        if (e instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        }
        throw new RuntimeException(String.format("unable to transmit data to %s for path %s", zooKeeper, node.getPath()), e);
    }
    return node.getStat();
}
 
Example 7
Source File: ZookeeperResourceTest.java    From spring-zookeeper with Apache License 2.0 5 votes vote down vote up
private void setData2ZkDev() throws FileNotFoundException, IOException, KeeperException, InterruptedException {
    log.info("set data to zk dev.");
    ZookeeperResource zkResource = getZkResource();
    ZooKeeper zk = zkResource.getZk();
    String fileName = "/zk_cnfig_cn_test_2.txt";
    String fileContent = getFileContent(fileName);
    zk.setData("/cn_dev", fileContent.getBytes(), -1);

}
 
Example 8
Source File: ZookeeperResourceTest.java    From spring-zookeeper with Apache License 2.0 5 votes vote down vote up
private void setData2ZkDevBack() throws FileNotFoundException, IOException, KeeperException, InterruptedException {
    log.info("set data to zk dev back.");
    ZookeeperResource zkResource = getZkResource();
    ZooKeeper zk = zkResource.getZk();
    String fileName = "/zk_cnfig_cn_test.txt";
    String fileContent = getFileContent(fileName);
    zk.setData("/cn_dev", fileContent.getBytes(), -1);
}
 
Example 9
Source File: ZkSetData.java    From shark with Apache License 2.0 5 votes vote down vote up
public @Test void testSetData() {
	try (BufferedReader reader = new BufferedReader(new FileReader("c:/shark-datasource.xml"))) {
		StringBuffer str = new StringBuffer();
		String value = "";
		while (null != (value = reader.readLine()))
			str.append(value);
		final CountDownLatch countDownLatch = new CountDownLatch(1);
		ZooKeeper zk_client = new ZooKeeper("ip:port", 30000, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				final KeeperState STATE = event.getState();
				switch (STATE) {
				case SyncConnected:
					countDownLatch.countDown();
					logger.info("connection zookeeper success");
					break;
				case Disconnected:
					logger.warn("zookeeper connection is disconnected");
					break;
				case Expired:
					logger.error("zookeeper session expired");
					break;
				case AuthFailed:
					logger.error("authentication failure");
				default:
					break;
				}
			}
		});
		countDownLatch.await();
		zk_client.setData("/info/shark", str.toString().getBytes(), -1);
		logger.info("insert success");
	} catch (Exception e) {
		logger.error("insert fail", e);
	}
}
 
Example 10
Source File: ResourceQuotaCache.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void saveQuotaToZnode(String zpath, ResourceQuota quota) throws Exception {
    ZooKeeper zk = this.localZkCache.getZooKeeper();
    if (zk.exists(zpath, false) == null) {
        try {
            ZkUtils.createFullPathOptimistic(zk, zpath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException e) {
        }
    }
    zk.setData(zpath, this.jsonMapper.writeValueAsBytes(quota), -1);
}
 
Example 11
Source File: PulsarClusterMetadataSetup.java    From pulsar with Apache License 2.0 5 votes vote down vote up
static void createNamespaceIfAbsent(ZooKeeper configStoreZk, NamespaceName namespaceName, String cluster)
        throws KeeperException, InterruptedException, IOException {
    String namespacePath = POLICIES_ROOT + "/" +namespaceName.toString();
    Policies policies;
    Stat stat = configStoreZk.exists(namespacePath, false);
    if (stat == null) {
        policies = new Policies();
        policies.bundles = getBundles(16);
        policies.replication_clusters = Collections.singleton(cluster);

        createZkNode(
            configStoreZk,
            namespacePath,
            ObjectMapperFactory.getThreadLocal().writeValueAsBytes(policies),
            ZooDefs.Ids.OPEN_ACL_UNSAFE,
            CreateMode.PERSISTENT);
    } else {
        byte[] content = configStoreZk.getData(namespacePath, false, null);
        policies = ObjectMapperFactory.getThreadLocal().readValue(content, Policies.class);

        // Only update z-node if the list of clusters should be modified
        if (!policies.replication_clusters.contains(cluster)) {
            policies.replication_clusters.add(cluster);

            configStoreZk.setData(namespacePath, ObjectMapperFactory.getThreadLocal().writeValueAsBytes(policies),
                    stat.getVersion());
        }
    }
}
 
Example 12
Source File: TestZooKeeperScanPolicyObserver.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void setExpireBefore(long time)
    throws KeeperException, InterruptedException, IOException {
  ZooKeeper zk = UTIL.getZooKeeperWatcher().getRecoverableZooKeeper().getZooKeeper();
  if (zk.exists(ZooKeeperScanPolicyObserver.NODE, false) == null) {
    zk.create(ZooKeeperScanPolicyObserver.NODE, Bytes.toBytes(time), ZooDefs.Ids.OPEN_ACL_UNSAFE,
      CreateMode.PERSISTENT);
  } else {
    zk.setData(ZooKeeperScanPolicyObserver.NODE, Bytes.toBytes(time), -1);
  }
}
 
Example 13
Source File: LoadBalancerTest.java    From pulsar with Apache License 2.0 4 votes vote down vote up
private void createNamespacePolicies(PulsarService pulsar) throws Exception {
    // // prepare three policies for the namespace isolation
    NamespaceIsolationPolicies policies = new NamespaceIsolationPolicies();

    // set up policy that use this broker as primary
    NamespaceIsolationData policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/primary-ns.*");
    policyData.primary = new ArrayList<String>();
    for (int i = 0; i < BROKER_COUNT; i++) {
        policyData.primary.add(pulsarServices[i].getAdvertisedAddress());
    }
    policyData.secondary = new ArrayList<String>();
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "1");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "100");
    policies.setPolicy("primaryBrokerPolicy", policyData);

    // set up policy that use this broker as secondary
    policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/secondary-ns.*");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add(pulsarServices[0].getAdvertisedAddress());
    policyData.secondary = new ArrayList<String>();
    for (int i = 1; i < BROKER_COUNT; i++) {
        policyData.secondary.add(pulsarServices[i].getAdvertisedAddress());
    }
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "1");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "100");
    policies.setPolicy("secondaryBrokerPolicy", policyData);

    // set up policy that do not use this broker (neither primary nor secondary)
    policyData = new NamespaceIsolationData();
    policyData.namespaces = new ArrayList<String>();
    policyData.namespaces.add("pulsar/use/shared-ns.*");
    policyData.primary = new ArrayList<String>();
    policyData.primary.add(pulsarServices[0].getAdvertisedAddress());
    policyData.secondary = new ArrayList<String>();
    for (int i = 1; i < BROKER_COUNT; i++) {
        policyData.secondary.add(pulsarServices[i].getAdvertisedAddress());
    }
    policyData.auto_failover_policy = new AutoFailoverPolicyData();
    policyData.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
    policyData.auto_failover_policy.parameters = new HashMap<String, String>();
    policyData.auto_failover_policy.parameters.put("min_limit", "1");
    policyData.auto_failover_policy.parameters.put("usage_threshold", "100");
    policies.setPolicy("otherBrokerPolicy", policyData);

    ObjectMapper jsonMapper = ObjectMapperFactory.create();
    ZooKeeper globalZk = pulsar.getGlobalZkCache().getZooKeeper();
    ZkUtils.createFullPathOptimistic(globalZk, AdminResource.path("clusters", "use", "namespaceIsolationPolicies"),
            new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    byte[] content = jsonMapper.writeValueAsBytes(policies.getPolicies());
    globalZk.setData(AdminResource.path("clusters", "use", "namespaceIsolationPolicies"), content, -1);

}
 
Example 14
Source File: ZookeeperResourceTest.java    From spring-zookeeper with Apache License 2.0 4 votes vote down vote up
private void reverseTestSub(Boolean isTestSub) throws KeeperException, InterruptedException {
    ZookeeperResource zkResource = getZkResource();
    ZooKeeper zk = zkResource.getZk();
    zk.setData("/cn_dev/test_sub", ("testSub=" + !isTestSub).toString().getBytes(), -1);
}
 
Example 15
Source File: ZooKeeperOutput.java    From envelope with Apache License 2.0 4 votes vote down vote up
@Override
public void applyRandomMutations(List<Row> planned) throws Exception {
  if (planned.size() > 1000) {
    throw new RuntimeException(
        "ZooKeeper output does not support applying more than 1000 mutations at a time. " +
        "This is to prevent misuse of ZooKeeper as a regular data store. " + 
        "Do not use ZooKeeper for storing anything more than small pieces of metadata.");
  }

  ZooKeeper zk;
  try {
    zk = connection.getZooKeeper();
  } catch (Exception e) {
    throw new RuntimeException("Could not connect to ZooKeeper output", e);
  }
  
  for (Row plan : planned) {
    if (plan.schema() == null) {
      throw new RuntimeException("Mutation row provided to ZooKeeper output must contain a schema");
    }
    
    MutationType mutationType = PlannerUtils.getMutationType(plan);
    plan = PlannerUtils.removeMutationTypeField(plan);
    
    Row key = RowUtils.subsetRow(plan, SchemaUtils.subsetSchema(plan.schema(), keyFieldNames));
    String znode = znodesForFilter(zk, key).iterator().next(); // There can only be one znode per full key
    byte[] value = serializeRow(RowUtils.subsetRow(plan, SchemaUtils.subtractSchema(plan.schema(), keyFieldNames)));
    
    switch (mutationType) {
      case DELETE:
        zk.delete(znode, -1);
        break;
      case UPSERT:
        prepareZnode(zk, znode);
        zk.setData(znode, value, -1);
        break;
      default:
        throw new RuntimeException("ZooKeeper output does not support mutation type: " + PlannerUtils.getMutationType(plan));
    }
  }
}
 
Example 16
Source File: ConfigurationGenerator.java    From examples with Apache License 2.0 4 votes vote down vote up
private ConfigurationGenerator() {
    try {
        ZooKeeper zk = new ZooKeeper(Constants.ZOOKEEPER_CONNECTION_STRING, 5000, this);
        // Wait for the zookeeper servers to get ready... which can several seconds when
        // using docker
        while(zk.getState() != ZooKeeper.States.CONNECTED) {
            synchronized (mutex) {
                mutex.wait(5000);
            }
        }

        // With docker we will always need to create the root, but with reconfiguration
        // you might first need to lookup the currently saved configuration in zookeeper
        Stat s = zk.exists(Constants.CONFIGURATION_PATH, false);
        int version = configVersion.get();
        if (s == null) {
            zk.create(Constants.CONFIGURATION_PATH,
                      Integer.toString(version).getBytes(),
                      Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        }
        else {
            byte[] rawData = zk.getData(Constants.CONFIGURATION_PATH, false, s);
            configVersion.set(new Integer(new String(rawData)).intValue());
        }

        // Every 5-10 seconds, create a new configuration
        Random rand = new Random();
        while (true) {
            synchronized (mutex) {
                mutex.wait(5000 + rand.nextInt(5000));
            }

            version = configVersion.incrementAndGet();
            System.out.println("ConfigurationGenerator: Updating version to " + version);
            // the order of these two calls is important
            zk.create(Constants.CONFIGURATION_PATH + "/" + version,
                      ConfigurationGenerator.createConfiguration(version).encode().getBytes(),
                      Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            zk.setData(Constants.CONFIGURATION_PATH,
                       Integer.toString(version).getBytes(),
                       version - 1);
            // Delete configuration versions while keeping one prior version.
            if(version > 2) {
                zk.delete(Constants.CONFIGURATION_PATH + "/" + (version - 2), -1);
            }
        }
    } catch (IOException | InterruptedException | KeeperException e) {
        e.printStackTrace();
    }
}
 
Example 17
Source File: ZookeeperTest.java    From java-study with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
	// 创建一个与服务器的连接
	 zk = new ZooKeeper(url , 
			 CONNECTION_TIMEOUT, new Watcher() { 
	            // 监控所有被触发的事件
	            public void process(WatchedEvent event) {
	                System.out.println(event.getPath()+"已经触发了" + event.getType() + "事件!"); 
	            } 
	        }); 
	 
	 /*
	  * 创建一个给定的目录节点 path, 并给它设置数据,
	  * CreateMode 标识有四种形式的目录节点,分别是
	  *  PERSISTENT:持久化目录节点,这个目录节点存储的数据不会丢失;
	  *  PERSISTENT_SEQUENTIAL:顺序自动编号的目录节点,这种目录节点会根据当前已近存在的节点数自动加 1,
	  *  然后返回给客户端已经成功创建的目录节点名;
	  *  EPHEMERAL:临时目录节点,一旦创建这个节点的客户端与服务器端口也就是 session 超时,这种节点会被自动删除;
	  *  EPHEMERAL_SEQUENTIAL:临时自动编号节点
	  */
	 
	 // 创建一个父级目录节点
	 if(zk.exists("/test", true)==null){
		 //参数说明:目录,参数,参数权限,节点类型
		 zk.create("/test", "data1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 
	 }
	 if(zk.exists("/test/test1", true)==null){
		 // 创建一个子目录节点
		 zk.create("/test/test1", "data2".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); 
	 }
	 
	 System.out.println("="+new String(zk.getData("/test",false,null))); 
	 // 取出子目录节点列表
	 System.out.println("=="+zk.getChildren("/test",true)); 
	
	 if(zk.exists("/test/test1", true)!=null){
		 // 修改子目录节点数据
		 zk.setData("/test/test1","testOne".getBytes(),-1); 
	 }
	 System.out.println("目录节点状态:["+zk.exists("/test",true)+"]"); 
	 if(zk.exists("/test/test1", true)!=null){
	  // 创建另外一个子目录节点
	  zk.create("/test/test2", "test2".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT); 
	 }
	 System.out.println("==="+new String(zk.getData("/test/test2",true,null))); 

	 /*
	  * 删除 path 对应的目录节点,version 为 -1 可以匹配任何版本,也就删除了这个目录节点所有数据
	  */
	 // 删除子目录节点
	 zk.delete("/test/test2",-1); 
	 zk.delete("/test/test1",-1); 
	 
	 // 删除父目录节点
	 zk.delete("/test",-1); 
	 // 关闭连接
	 zk.close();
}