Java Code Examples for org.I0Itec.zkclient.ZkClient#readData()

The following examples show how to use org.I0Itec.zkclient.ZkClient#readData() . 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: IncrementIdGen.java    From SkyEye with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 利用zookeeper
 * @return
 */
@Override
public String nextId() {
    String app = this.registerDto.getApp();
    String host = this.registerDto.getHost();
    ZkClient zkClient = this.registerDto.getZkClient();
    String path = Constants.ZK_REGISTRY_ID_ROOT_PATH + Constants.SLASH + app + Constants.SLASH + host;
    if (zkClient.exists(path)) {
        // 如果已经有该节点,表示已经为当前的host上部署的该app分配的编号(应对某个服务重启之后编号不变的问题),直接获取该id,而无需生成
        return zkClient.readData(Constants.ZK_REGISTRY_ID_ROOT_PATH + Constants.SLASH + app + Constants.SLASH + host);
    } else {
        // 节点不存在,那么需要生成id,利用zk节点的版本号每写一次就自增的机制来实现
        Stat stat = zkClient.writeDataReturnStat(Constants.ZK_REGISTRY_SEQ, new byte[0], -1);
        // 生成id
        String id = String.valueOf(stat.getVersion());
        // 将数据写入节点
        zkClient.createPersistent(path, true);
        zkClient.writeData(path, id);
        return id;
    }
}
 
Example 2
Source File: SafeZclient.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static void client() {
	try {
		ZkClient zc = new ZkClient("localhost:2181");
		String userTwo = "gao:gao";
		zc.addAuthInfo("digest", userTwo.getBytes());

           //使用admin就可以创建
		// String userOne = "admin:admin";
		// zc.addAuthInfo("digest",userOne.getBytes());
		
		String data = zc.readData("/safe");
		log.info("只读用户 " + userTwo + "读取safe节点返回数据:" + data);
		
		zc.create("/safe2", "test", CreateMode.EPHEMERAL);
		log.info("只读用户创建节点safe2成功:" + zc.readData("/safe2"));
		
		zc.create("/safe/test", "zookeeper加密是针对节点加密的", CreateMode.EPHEMERAL);
		log.info("只读用户创建节点失败:" + zc.readData("/safe/test"));
		
	} catch (Exception e) {
		e.printStackTrace();
	}
	
}
 
Example 3
Source File: LocalCacheUtil.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Long getResourceNano(String notifyName) {
    String resourcePath = getResourcePath(notifyName);
    ZkClient zClient = getZkClient();
    if (zClient.exists(resourcePath)) {
        Stat stat = new Stat();
        Long nanoTime = zClient.readData(resourcePath, stat);
        return nanoTime;
    }
    return null;
}
 
Example 4
Source File: ZkUtil.java    From java-study with Apache License 2.0 5 votes vote down vote up
/**
 * 获取数据
 * @param zk
 * @param path
 * @return
 */
public static <T extends Object> T getData(ZkClient zk,String path){
	T t=null;
	if(zk.exists(path)){
	   t=zk.readData(path);
	}
	return t;
}
 
Example 5
Source File: ZkRouteParser.java    From sumk with Apache License 2.0 5 votes vote down vote up
private RouteInfo getZkNodeData(ZkClient zk, String path) {
	byte[] data = zk.readData(SOA_ROOT + "/" + path);
	try {
		return ZkDataOperators.inst().deserialize(new ZKPathData(path, (byte[]) data));
	} catch (Exception e) {
		logger.error("解析" + path + "的zk数据失败", e);
		return null;
	}
}
 
Example 6
Source File: ZkConfigSaver.java    From learning-hadoop with Apache License 2.0 5 votes vote down vote up
private static void saveConfigs(ZkClient client, String rootNode,
		File confDir) {
	List<String> configs = client.getChildren(rootNode);
	for (String config : configs) {
		String content = (String) client.readData(rootNode + "/" + config);
		File confFile = new File(confDir, config);
		try {
			FileUtils.writeStringToFile(confFile, content, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("配置成功保存到本地: " + confFile.getAbsolutePath());
	}
}
 
Example 7
Source File: PinotHelixResourceManagerTest.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
private void modifyExistingInstanceConfig(ZkClient zkClient)
    throws InterruptedException {
  String instanceName = "Server_localhost_" + new Random().nextInt(NUM_INSTANCES);
  String instanceConfigPath = PropertyPathBuilder.instanceConfig(getHelixClusterName(), instanceName);
  Assert.assertTrue(zkClient.exists(instanceConfigPath));
  ZNRecord znRecord = zkClient.readData(instanceConfigPath, null);

  InstanceConfig cachedInstanceConfig = _helixResourceManager.getHelixInstanceConfig(instanceName);
  String originalPort = cachedInstanceConfig.getPort();
  Assert.assertNotNull(originalPort);
  String newPort = Long.toString(System.currentTimeMillis());
  Assert.assertTrue(!newPort.equals(originalPort));

  // Set new port to this instance config.
  znRecord.setSimpleField(InstanceConfig.InstanceConfigProperty.HELIX_PORT.toString(), newPort);
  zkClient.writeData(instanceConfigPath, znRecord);

  long maxTime = System.currentTimeMillis() + MAX_TIMEOUT_IN_MILLISECOND;
  InstanceConfig latestCachedInstanceConfig = _helixResourceManager.getHelixInstanceConfig(instanceName);
  String latestPort = latestCachedInstanceConfig.getPort();
  while (!newPort.equals(latestPort) && System.currentTimeMillis() < maxTime) {
    Thread.sleep(100L);
    latestCachedInstanceConfig = _helixResourceManager.getHelixInstanceConfig(instanceName);
    latestPort = latestCachedInstanceConfig.getPort();
  }
  Assert.assertTrue(System.currentTimeMillis() < maxTime, "Timeout when waiting for adding instance config");

  // Set original port back to this instance config.
  znRecord.setSimpleField(InstanceConfig.InstanceConfigProperty.HELIX_PORT.toString(), originalPort);
  zkClient.writeData(instanceConfigPath, znRecord);
}
 
Example 8
Source File: OffsetMonitor.java    From uReplicator with Apache License 2.0 4 votes vote down vote up
private Runnable updateOffsetTask(final String leaderBroker, final TopicAndPartition tp) {
  return new Runnable() {
    @Override
    public void run() {
      if (StringUtils.isEmpty(consumerOffsetPath)) {
        logger.warn("No consumer group id, skip updateOffsetTask");
        return;
      }
      ZkClient zk = zkClientQueue.poll();
      try {
        Object obj = zk.readData(consumerOffsetPath + tp.topic() + "/" + tp.partition(), true);
        long commitOffset = obj == null ? -1 : Long.valueOf(String.valueOf(obj));

        SimpleConsumer consumer = getSimpleConsumer(leaderBroker);
        long latestOffset = getLatestOffset(consumer, tp);
        if (latestOffset < 0) {
          latestOffset = -1;
        }

        TopicPartitionLag previousOffset = topicPartitionToOffsetMap
            .put(tp,
                new TopicPartitionLag(tp.topic(), tp.partition(), latestOffset, commitOffset));
        logger
            .debug("Get latest offset={} committed offset={} for {}", latestOffset, commitOffset,
                tp);
        if (latestOffset > 0 && commitOffset > 0) {
          if (latestOffset - commitOffset > 0 && previousOffset != null
              && previousOffset.getCommitOffset() == commitOffset) {
            TopicPartitionLag oldLag = noProgressMap.get(tp);
            // keep the oldest record (the time began to have no progress) in
            // order to measure whether the time larger than the threshold,
            // therefore we do not overwrite the old record if the commit
            // offset is the same as current
            if (oldLag == null || oldLag.getCommitOffset() != commitOffset) {
              noProgressMap.put(tp, previousOffset);
            }
          } else {
            noProgressMap.remove(tp);
          }
        }
      } catch (Exception e) {
        offsetMonitorFailureCount.getAndAdd(1);
        logger.warn("Got exception to get offset for TopicPartition=" + tp, e);
      } finally {
        zkClientQueue.add(zk);
      }
    }
  };
}
 
Example 9
Source File: ZkConfigUtil.java    From zkconfigutil with Apache License 2.0 4 votes vote down vote up
public final synchronized void register(Class<?> cla,
		boolean isCreateIfNUll, String rootPath)
		throws NotRegistedException, InstantiationException,
		IllegalAccessException {
	// if (!cla.isAnnotationPresent(TypeZkConfigurable.class)) {
	// throw new NotRegistedException();
	// }
	final ZkClient zkClient;
	if ("".equals(this.globalZkServer)) {
		logger.error("please set globalZkServer or set typeZkConfigurable.useOwnZkServer()=false to use own zkserver system will exit!!!");
		System.exit(0);
	}
	zkClient = this.makeZkClient(this.globalZkServer);
	// String packagePath = cla.getPackage().getName();
	// packagePath = packagePath.replaceAll("\\.", "/");
	//
	// rootPath = this.makeZkPath(rootPath, packagePath);
	String path = this.makeZkPath(rootPath, cla.getName());

	path = path.replace("$", "/"); // inclass

	final Field[] fields = cla.getDeclaredFields();

	for (Field field : fields) {
		if (!field.isAnnotationPresent(FieldZkConfigurable.class))
			continue;
		logger.info("field : " + field.getName() + "   type : "
				+ field.getType().getSimpleName());
		FieldZkConfigurable fieldZkConfigurable = field
				.getAnnotation(FieldZkConfigurable.class);

		final String fieldPath = this.makeZkPath(path, field.getName());

		String value = zkClient.readData(fieldPath, true);
		logger.info(fieldPath + " : " + value);

		Class<? extends AbstractResolve> resolve = fieldZkConfigurable
				.resolve();
		Resolve resolveInstance;
		if (resolve == ReflectResolve.class) {
			resolveInstance = new ReflectResolve(cla, field);
		} else {
			resolveInstance = resolve.newInstance();
		}

		/**
		 * Dosen't have value
		 */
		if (value == null && !isCreateIfNUll) {
			continue;
		} else if (value == null && isCreateIfNUll) {
			zkClient.createPersistent(fieldPath, true);
			String defaultValue = resolveInstance.resolve();
			zkClient.writeData(fieldPath, defaultValue);
		} else {
			/**
			 * have value
			 */
			resolveInstance.dResolve(value);
		}

		/**
		 * for USR2 signal
		 */
		SignalHelper.mark(cla, fieldPath, resolveInstance,
				fieldZkConfigurable.dynamicUpdate());

		if (fieldZkConfigurable.dynamicUpdate()) {
			logger.info("dynamicUpdate " + fieldPath);
			zkClient.subscribeDataChanges(fieldPath, this);
			Updater.register(fieldPath, resolveInstance);
		}
	}
}
 
Example 10
Source File: BenchmarkQueryEngine.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
@Setup
public void startPinot()
    throws Exception {
  System.out.println("Using table name " + TABLE_NAME);
  System.out.println("Using data directory " + DATA_DIRECTORY);
  System.out.println("Starting pinot");

  PerfBenchmarkDriverConf conf = new PerfBenchmarkDriverConf();
  conf.setStartBroker(true);
  conf.setStartController(true);
  conf.setStartServer(true);
  conf.setStartZookeeper(true);
  conf.setRunQueries(false);
  conf.setServerInstanceSegmentTarDir(null);
  conf.setServerInstanceDataDir(DATA_DIRECTORY);
  conf.setConfigureResources(false);
  _perfBenchmarkDriver = new PerfBenchmarkDriver(conf);
  _perfBenchmarkDriver.run();

  File[] segments = new File(DATA_DIRECTORY, TABLE_NAME).listFiles();
  for (File segmentDir : segments) {
    SegmentMetadataImpl segmentMetadata = new SegmentMetadataImpl(segmentDir);
    _perfBenchmarkDriver.configureTable(TABLE_NAME);
    System.out.println("Adding segment " + segmentDir.getAbsolutePath());
    _perfBenchmarkDriver.addSegment(TABLE_NAME, segmentMetadata);
  }

  ZkClient client = new ZkClient("localhost:2191", 10000, 10000, new ZNRecordSerializer());

  ZNRecord record = client.readData("/PinotPerfTestCluster/EXTERNALVIEW/" + TABLE_NAME);
  while (true) {
    System.out.println("record = " + record);
    Uninterruptibles.sleepUninterruptibly(10, TimeUnit.SECONDS);

    int onlineSegmentCount = 0;
    for (Map<String, String> instancesAndStates : record.getMapFields().values()) {
      for (String state : instancesAndStates.values()) {
        if (state.equals("ONLINE")) {
          onlineSegmentCount++;
          break;
        }
      }
    }

    System.out.println(onlineSegmentCount + " segments online out of " + segments.length);

    if (onlineSegmentCount == segments.length) {
      break;
    }

    record = client.readData("/PinotPerfTestCluster/EXTERNALVIEW/" + TABLE_NAME);
  }

  ranOnce = false;

  System.out.println(_perfBenchmarkDriver.postQuery(QUERY_PATTERNS[queryPattern], optimizationFlags).toString());
}