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

The following examples show how to use org.apache.zookeeper.ZooKeeper#getData() . 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: ZKManager.java    From uncode-schedule with Apache License 2.0 6 votes vote down vote up
public static void checkParent(ZooKeeper zk, String path) throws Exception {
  String[] list = path.split("/");
  String zkPath = "";
  for (int i = 0; i < list.length - 1; i++) {
    String str = list[i];
    if (str.equals("") == false) {
      zkPath = zkPath + "/" + str;
      if (zk.exists(zkPath, false) != null) {
        byte[] value = zk.getData(zkPath, false, null);
        if (value != null) {
          String tmpVersion = new String(value);
          if (tmpVersion.indexOf("uncode-schedule-") >= 0) {
            throw new Exception("\"" + zkPath + "\"  is already a schedule instance's root directory, its any subdirectory cannot as the root directory of others");
          }
        }
      }
    }
  }
}
 
Example 2
Source File: ZKManager.java    From tbschedule with Apache License 2.0 6 votes vote down vote up
public static void checkParent(ZooKeeper zk, String path) throws Exception {
    String[] list = path.split("/");
    String zkPath = "";
    for (int i = 0; i < list.length - 1; i++) {
        String str = list[i];
        if (str.equals("") == false) {
            zkPath = zkPath + "/" + str;
            if (zk.exists(zkPath, false) != null) {
                byte[] value = zk.getData(zkPath, false, null);
                if (value != null) {
                    String tmpVersion = new String(value);
                    if (tmpVersion.indexOf("taobao-pamirs-schedule-") >= 0) {
                        throw new Exception("\"" + zkPath
                            + "\"  is already a schedule instance's root directory, its any subdirectory cannot as the root directory of others");
                    }
                }
            }
        }
    }
}
 
Example 3
Source File: ServiceDiscovery.java    From Tatala-RPC with Apache License 2.0 6 votes vote down vote up
private static void watchNode(final ZooKeeper zk) {
	try {
		List<String> nodeList = zk.getChildren(ServiceRegistry.ZK_REGISTRY_PATH, new Watcher() {
			@Override
			public void process(WatchedEvent event) {
				if (event.getType() == Event.EventType.NodeChildrenChanged) {
					watchNode(zk);
				}
			}
		});
		List<String> dataList = new ArrayList<>();
		for (String node : nodeList) {
			byte[] bytes = zk.getData(ServiceRegistry.ZK_REGISTRY_PATH + "/" + node, false, null);
			dataList.add(new String(bytes));
		}
		ServiceDiscovery.dataList = dataList;
	} catch (Exception e) {
		log.error("ServiceDiscovery.watchNode: ", e);
	}
}
 
Example 4
Source File: ServiceDiscovery.java    From rpc4j with MIT License 6 votes vote down vote up
private void watchNode(final ZooKeeper zk) {
    try {
        List<String> nodeList = zk.getChildren(Constant.ZK_REGISTRY_PATH, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getType() == Event.EventType.NodeChildrenChanged) {
                    watchNode(zk);
                }
            }
        });
        List<String> dataList = new ArrayList<>();
        for (String node : nodeList) {
            byte[] bytes = zk.getData(Constant.ZK_REGISTRY_PATH + "/" + node, false, null);
            dataList.add(new String(bytes));
        }
        Logger.info("node data: {}", dataList);
        this.dataList = dataList;
    } catch (KeeperException | InterruptedException e) {
        Logger.error("", e);
    }
}
 
Example 5
Source File: Kafka09TopicProvisionTest.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Test (enabled=false)
 public void testCluster()
throws IOException, InterruptedException, KeeperException {
  int clusterCount = _kafkaTestHelper.getClusterCount();
  Assert.assertEquals(clusterCount,testClusterCount);
  int zkPort = _kafkaTestHelper.getZookeeperPort();
     String kafkaBrokerPortList = _kafkaTestHelper.getKafkaBrokerPortList().toString();
     System.out.println("kafkaBrokerPortList : " + kafkaBrokerPortList);
  ZooKeeper zk = new ZooKeeper("localhost:"+zkPort, 10000, new ByPassWatcher());
  List<Integer> brokerPortList = new ArrayList<Integer>();
     List<String> ids = zk.getChildren("/brokers/ids", false);
     for (String id : ids) {
         String brokerInfo = new String(zk.getData("/brokers/ids/" + id, false, null));
         JSONObject obj = new JSONObject(brokerInfo);
         int brokerPort = obj.getInt("port");
         System.out.println(brokerPort);
         brokerPortList.add(brokerPort);
     }
     Assert.assertTrue(_kafkaTestHelper.getKafkaBrokerPortList().equals(brokerPortList));
 }
 
Example 6
Source File: TestZKFailoverController.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Test that, if ACLs are specified in the configuration, that
 * it sets the ACLs when formatting the parent node.
 */
@Test(timeout=15000)
public void testFormatSetsAcls() throws Exception {
  // Format the base dir, should succeed
  DummyHAService svc = cluster.getService(1);
  assertEquals(0, runFC(svc, "-formatZK"));

  ZooKeeper otherClient = createClient();
  try {
    // client without auth should not be able to read it
    Stat stat = new Stat();
    otherClient.getData(ZKFailoverController.ZK_PARENT_ZNODE_DEFAULT,
        false, stat);
    fail("Was able to read data without authenticating!");
  } catch (KeeperException.NoAuthException nae) {
    // expected
  }
}
 
Example 7
Source File: ServiceDiscovery.java    From lionrpc with Apache License 2.0 6 votes vote down vote up
private void watchNode(final ZooKeeper zk) {
    try {
        List<String> nodeList = zk.getChildren(Constant.ZK_REGISTRY_PATH, new Watcher() {

            public void process(WatchedEvent event) {
                if (event.getType() == Event.EventType.NodeChildrenChanged) {
                    watchNode(zk);
                }
            }
        });
        List<String> dataList = new ArrayList<>();
        for (String node : nodeList) {
            byte[] bytes = zk.getData(Constant.ZK_REGISTRY_PATH + "/" + node, false, null);
            dataList.add(new String(bytes));
        }
        LOGGER.debug("node data: {}", dataList);
        this.dataList = dataList;

        LOGGER.debug("Service discovery triggered updating connected server node.");
        updateConnectedServer();
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
 
Example 8
Source File: LoadBalancerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testLoadReportsWrittenOnZK() throws Exception {
    ZooKeeper zkc = bkEnsemble.getZkClient();
    try {
        for (int i = 0; i < BROKER_COUNT; i++) {
            String znodePath = String.format("%s/%s", SimpleLoadManagerImpl.LOADBALANCE_BROKERS_ROOT,
                    lookupAddresses[i]);
            byte[] loadReportData = zkc.getData(znodePath, false, null);
            assert (loadReportData.length > 0);
            log.info("LoadReport {}, {}", lookupAddresses[i], new String(loadReportData));

            LoadReport loadReport = ObjectMapperFactory.getThreadLocal().readValue(loadReportData, LoadReport.class);
            assert (loadReport.getName().equals(lookupAddresses[i]));

            // Check Initial Ranking is populated in both the brokers
            Field ranking = ((SimpleLoadManagerImpl) pulsarServices[i].getLoadManager().get()).getClass()
                    .getDeclaredField("sortedRankings");
            ranking.setAccessible(true);
            AtomicReference<Map<Long, Set<ResourceUnit>>> sortedRanking = (AtomicReference<Map<Long, Set<ResourceUnit>>>) ranking
                    .get(pulsarServices[i].getLoadManager().get());
            printSortedRanking(sortedRanking);

            // all brokers have same rank to it would be 0 --> set-of-all-the-brokers
            int brokerCount = 0;
            for (Map.Entry<Long, Set<ResourceUnit>> entry : sortedRanking.get().entrySet()) {
                brokerCount += entry.getValue().size();
            }
            assertEquals(brokerCount, BROKER_COUNT);
            TopicName topicName = TopicName.get("persistent://pulsar/use/primary-ns/test-topic");
            ResourceUnit found = pulsarServices[i].getLoadManager().get()
                    .getLeastLoaded(pulsarServices[i].getNamespaceService().getBundle(topicName)).get();
            assertNotNull(found);
        }
    } catch (InterruptedException | KeeperException e) {
        fail("Unable to read the data from Zookeeper - [{}]", e);
    }
}
 
Example 9
Source File: ZooKeeperOutput.java    From envelope with Apache License 2.0 5 votes vote down vote up
@Override
public Iterable<Row> getExistingForFilters(Iterable<Row> filters) throws Exception {
  ZooKeeper zk;
  try {
    zk = connection.getZooKeeper();
  } catch (Exception e) {
    throw new RuntimeException("Could not connect to ZooKeeper output", e);
  }
  
  Set<Row> existing = Sets.newHashSet();
  
  for (Row filter : filters) {
    List<String> znodes = znodesForFilter(zk, filter);
    
    for (String znode : znodes) {
      if (zk.exists(znode, false) != null) {
        byte[] serialized = zk.getData(znode, false, null);
        
        if (serialized.length > 0) {
          Row existingRow = toFullRow(znode, serialized);
          
          if (matchesValueFilter(existingRow, filter)) {
            existing.add(existingRow);
          }
        }
      }
    }
  }
  
  return existing;
}
 
Example 10
Source File: ZKTools.java    From uncode-schedule with Apache License 2.0 5 votes vote down vote up
public static void printTree(ZooKeeper zk, String path, Writer writer, String lineSplitChar) throws Exception {
  String[] list = getSortedTree(zk, path);
  Stat stat = new Stat();
  for (String name : list) {
    byte[] value = zk.getData(name, false, stat);
    if (value == null) {
      writer.write(name + lineSplitChar);
    } else {
      writer.write(name + "[v." + stat.getVersion() + "]" + "[" + new String(value) + "]" + lineSplitChar);
    }
  }
}
 
Example 11
Source File: ZookeeperClientSideMetadataProvider.java    From herddb with Apache License 2.0 5 votes vote down vote up
private String readAsNode(ZooKeeper zooKeeper, String tableSpace) throws IOException, InterruptedException, KeeperException {
    tableSpace = tableSpace.toLowerCase();
    Stat stat = new Stat();
    byte[] result = zooKeeper.getData(basePath + "/nodes/" + tableSpace, false, stat);
    NodeMetadata md = NodeMetadata.deserialize(result, stat.getVersion());
    String leader = md.nodeId;
    tableSpaceLeaders.put(tableSpace, leader);
    return leader;
}
 
Example 12
Source File: ZookeeperClientSideMetadataProvider.java    From herddb with Apache License 2.0 5 votes vote down vote up
private String readAsTableSpace(ZooKeeper zooKeeper, String tableSpace) throws IOException, InterruptedException, KeeperException {
    tableSpace = tableSpace.toLowerCase();
    Stat stat = new Stat();
    byte[] result = zooKeeper.getData(basePath + "/tableSpaces/" + tableSpace, false, stat);
    String leader = TableSpace.deserialize(result, stat.getVersion(), stat.getCtime()).leaderId;
    tableSpaceLeaders.put(tableSpace, leader);
    return leader;
}
 
Example 13
Source File: ZookeeperTestServerTest.java    From kafka-junit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Helper method to read data from zookeeper.
 */
private String readZkString(final ZooKeeper zkClient, final String path) throws KeeperException, InterruptedException {
    return new String(
        zkClient.getData(path, null, null),
        StandardCharsets.UTF_8
    );
}
 
Example 14
Source File: ZookeeperDriverImpl.java    From disconf with Apache License 2.0 5 votes vote down vote up
/**
 * 获取指定 配置数据
 *
 * @return
 *
 * @throws InterruptedException
 * @throws KeeperException
 */
private ZkDisconfData getDisconfData(String path, String keyName, ZooKeeper zooKeeper)
        throws KeeperException, InterruptedException {

    String curPath = path + "/" + keyName;

    if (zooKeeper.exists(curPath, false) == null) {
        return null;
    }

    ZkDisconfData zkDisconfData = new ZkDisconfData();
    zkDisconfData.setKey(keyName);

    List<String> secChiList = zooKeeper.getChildren(curPath, false);
    List<ZkDisconfData.ZkDisconfDataItem> zkDisconfDataItems = new ArrayList<ZkDisconfData.ZkDisconfDataItem>();

    // list
    for (String secKey : secChiList) {

        // machine
        ZkDisconfData.ZkDisconfDataItem zkDisconfDataItem = new ZkDisconfData.ZkDisconfDataItem();
        zkDisconfDataItem.setMachine(secKey);

        String thirdPath = curPath + "/" + secKey;

        // value
        byte[] data = zooKeeper.getData(thirdPath, null, null);
        if (data != null) {
            zkDisconfDataItem.setValue(new String(data, CHARSET));
        }

        // add
        zkDisconfDataItems.add(zkDisconfDataItem);
    }

    zkDisconfData.setData(zkDisconfDataItems);

    return zkDisconfData;
}
 
Example 15
Source File: ZKTools.java    From tbschedule with Apache License 2.0 5 votes vote down vote up
public static void printTree(ZooKeeper zk, String path, Writer writer, String lineSplitChar) throws Exception {
    String[] list = getTree(zk, path);
    Stat stat = new Stat();
    for (String name : list) {
        byte[] value = zk.getData(name, false, stat);
        if (value == null) {
            writer.write(name + lineSplitChar);
        } else {
            writer.write(name + "[v." + stat.getVersion() + "]" + "[" + new String(value) + "]" + lineSplitChar);
        }
    }
}
 
Example 16
Source File: EditLogLedgerMetadata.java    From big-c with Apache License 2.0 5 votes vote down vote up
static EditLogLedgerMetadata read(ZooKeeper zkc, String path)
    throws IOException, KeeperException.NoNodeException  {
  try {
    byte[] data = zkc.getData(path, false, null);

    EditLogLedgerProto.Builder builder = EditLogLedgerProto.newBuilder();
    if (LOG.isDebugEnabled()) {
      LOG.debug("Reading " + path + " data: " + new String(data, UTF_8));
    }
    TextFormat.merge(new String(data, UTF_8), builder);
    if (!builder.isInitialized()) {
      throw new IOException("Invalid/Incomplete data in znode");
    }
    EditLogLedgerProto ledger = builder.build();

    int dataLayoutVersion = ledger.getDataLayoutVersion();
    long ledgerId = ledger.getLedgerId();
    long firstTxId = ledger.getFirstTxId();
    if (ledger.hasLastTxId()) {
      long lastTxId = ledger.getLastTxId();
      return new EditLogLedgerMetadata(path, dataLayoutVersion,
                                       ledgerId, firstTxId, lastTxId);
    } else {
      return new EditLogLedgerMetadata(path, dataLayoutVersion,
                                       ledgerId, firstTxId);
    }
  } catch(KeeperException.NoNodeException nne) {
    throw nne;
  } catch(KeeperException ke) {
    throw new IOException("Error reading from zookeeper", ke);
  } catch (InterruptedException ie) {
    throw new IOException("Interrupted reading from zookeeper", ie);
  }
}
 
Example 17
Source File: ZKTool.java    From Scribengin with GNU Affero General Public License v3.0 5 votes vote down vote up
static public <T> List<T> getChildrenDataAs(ZooKeeper zkClient, String path, Class<T> type) throws KeeperException, InterruptedException {
  List<T> holder = new ArrayList<T>();
  List<String> children = zkClient.getChildren(path, false);
  for(int i = 0; i < children.size(); i++) {
    byte[] data = zkClient.getData(path + "/" + children.get(i), false, new Stat());
    T obj = JSONSerializer.INSTANCE.fromBytes(data, type);
    holder.add(obj);
  }
  return holder;
}
 
Example 18
Source File: ZKTool.java    From Scribengin with GNU Affero General Public License v3.0 4 votes vote down vote up
static public <T> T getDataAs(ZooKeeper zkClient, String path, Class<T> type) throws KeeperException, InterruptedException {
  byte[] data = zkClient.getData(path, false, new Stat());
  return JSONSerializer.INSTANCE.fromBytes(data, type);
}
 
Example 19
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 20
Source File: ZookeeperDriverImpl.java    From disconf with Apache License 2.0 4 votes vote down vote up
private void getConf(ZooKeeper zk, String groupName, String displayName, List<String> retList)
        throws KeeperException, InterruptedException {
    try {

        StringBuffer sb = new StringBuffer();

        int pathLength = StringUtils.countMatches(groupName, "/");
        for (int i = 0; i < pathLength - 2; ++i) {
            sb.append("\t");
        }

        List<String> children = zk.getChildren(groupName, false);

        if (!"/".equals(groupName)) {

            sb.append("|----" + displayName);
            Stat stat = new Stat();
            byte[] data = zk.getData(groupName, null, stat);

            if (data != null && children.size() == 0) {
                sb.append("\t" + new String(data, CHARSET));
            }
        } else {
            sb.append(groupName);
        }
        retList.add(sb.toString());

        //
        //
        //
        Collections.sort(children, Collator.getInstance(java.util.Locale.CHINA));
        for (String child : children) {

            String nextName = "";

            if (!"/".equals(groupName)) {

                nextName = groupName + "/" + child;

            } else {
                nextName = groupName + "/" + child;
            }

            String node = StringUtils.substringAfterLast(nextName, "/");

            getConf(zk, groupName + "/" + child, node, retList);
        }

    } catch (KeeperException.NoNodeException e) {
        LOG.error("Group " + groupName + " does not exist\n");
    }

}