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

The following examples show how to use org.I0Itec.zkclient.ZkClient#createPersistent() . 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: KafkaStarterUtils.java    From uReplicator with Apache License 2.0 6 votes vote down vote up
public static KafkaServerStartable startServer(final int port, final int brokerId,
    final String zkStr, final Properties configuration) {
  // Create the ZK nodes for Kafka, if needed
  int indexOfFirstSlash = zkStr.indexOf('/');
  if (indexOfFirstSlash != -1) {
    String bareZkUrl = zkStr.substring(0, indexOfFirstSlash);
    String zkNodePath = zkStr.substring(indexOfFirstSlash);
    ZkClient client = new ZkClient(bareZkUrl);
    client.createPersistent(zkNodePath, true);
    client.close();
  }

  File logDir = new File("/tmp/kafka-" + Double.toHexString(Math.random()));
  logDir.mkdirs();
  logDir.deleteOnExit();
  kafkaDataDir = logDir.toString();

  configureKafkaPort(configuration, port);
  configureZkConnectionString(configuration, zkStr);
  configureBrokerId(configuration, brokerId);
  configureKafkaLogDirectory(configuration, logDir);
  KafkaConfig config = new KafkaConfig(configuration);
  KafkaServerStartable serverStartable = new KafkaServerStartable(config);
  serverStartable.startup();
  return serverStartable;
}
 
Example 2
Source File: KafkaDataServerStartable.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
public void init(Properties props) {
  zkStr = props.getProperty(ZOOKEEPER_CONNECT);
  logDirPath = props.getProperty(LOG_DIRS);

  // Create the ZK nodes for Kafka, if needed
  int indexOfFirstSlash = zkStr.indexOf('/');
  if (indexOfFirstSlash != -1) {
    String bareZkUrl = zkStr.substring(0, indexOfFirstSlash);
    String zkNodePath = zkStr.substring(indexOfFirstSlash);
    ZkClient client = new ZkClient(bareZkUrl);
    client.createPersistent(zkNodePath, true);
    client.close();
  }

  File logDir = new File(logDirPath);
  logDir.mkdirs();

  props.put("zookeeper.session.timeout.ms", "60000");
  KafkaConfig config = new KafkaConfig(props);

  serverStartable = new KafkaServerStartable(config);
}
 
Example 3
Source File: KafkaDataServerStartable.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
public void init(Properties props) {
  port = (int) props.get(PORT);
  zkStr = props.getProperty(ZOOKEEPER_CONNECT);
  logDirPath = props.getProperty(LOG_DIRS);

  // Create the ZK nodes for Kafka, if needed
  int indexOfFirstSlash = zkStr.indexOf('/');
  if (indexOfFirstSlash != -1) {
    String bareZkUrl = zkStr.substring(0, indexOfFirstSlash);
    String zkNodePath = zkStr.substring(indexOfFirstSlash);
    ZkClient client = new ZkClient(bareZkUrl);
    client.createPersistent(zkNodePath, true);
    client.close();
  }

  File logDir = new File(logDirPath);
  logDir.mkdirs();

  props.put("zookeeper.session.timeout.ms", "60000");
  serverStartable = new KafkaServerStartable(new KafkaConfig(props));
  final Map<String, Object> config = new HashMap<>();
  config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:" + port);
  config.put(AdminClientConfig.CLIENT_ID_CONFIG, "Kafka2AdminClient-" + UUID.randomUUID().toString());
  config.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, 15000);
  adminClient = KafkaAdminClient.create(config);
}
 
Example 4
Source File: ZkUtils.java    From learning-hadoop with Apache License 2.0 6 votes vote down vote up
public static void mkPaths(ZkClient client, String path) {
	String[] subs = path.split("\\/");
	if (subs.length < 2) {
		return;
	}
	String curPath = "";
	for (int i = 1; i < subs.length; i++) {
		curPath = curPath + "/" + subs[i];
		if (!client.exists(curPath)) {
			if (logger.isDebugEnabled()) {
				logger.debug("Trying to create zk node: " + curPath);
			}
			client.createPersistent(curPath);
			if (logger.isDebugEnabled())
				logger.debug("Zk node created successfully: " + curPath);
		}
	}
}
 
Example 5
Source File: TestUtils.java    From uReplicator with Apache License 2.0 6 votes vote down vote up
public static ZKHelixAdmin initHelixClustersForWorkerTest(Properties properties, String route1,
    String route2) throws InterruptedException {
  String zkRoot = properties.getProperty("zkServer");
  Thread.sleep(500);
  ZkClient zkClient = ZkUtils.createZkClient(ZkStarter.DEFAULT_ZK_STR, 1000, 1000);
  zkClient.createPersistent("/ureplicator");
  zkClient.close();
  ZKHelixAdmin helixAdmin = new ZKHelixAdmin(zkRoot);
  String deployment = properties.getProperty("federated.deployment.name");
  String managerHelixClusterName = WorkerUtils.getManagerWorkerHelixClusterName(deployment);
  String controllerHelixClusterName = WorkerUtils.getControllerWorkerHelixClusterName(route1);
  if (StringUtils.isNotBlank(route2)) {
    String controllerHelixClusterName2 = WorkerUtils.getControllerWorkerHelixClusterName(route2);
    HelixSetupUtils.setup(controllerHelixClusterName2, zkRoot, "0");
  }

  HelixSetupUtils.setup(managerHelixClusterName, zkRoot, "0");
  HelixSetupUtils.setup(controllerHelixClusterName, zkRoot, "0");

  return helixAdmin;
}
 
Example 6
Source File: ZkClientHelper.java    From sumk with Apache License 2.0 6 votes vote down vote up
public static void makeSure(ZkClient client, final String dataPath) {
	int start = 0, index;
	while (true) {
		index = dataPath.indexOf("/", start + 1);

		if (index == start + 1) {
			return;
		}
		String path = dataPath;
		if (index > 0) {
			path = dataPath.substring(0, index);
			start = index;
		}
		if (!client.exists(path)) {
			try {
				client.createPersistent(path);
			} catch (Exception e) {
				Logs.system().warn(path + " create failed.", e);
			}
		}

		if (index < 0 || index == dataPath.length() - 1) {
			return;
		}
	}
}
 
Example 7
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 8
Source File: ConfigStateHolder.java    From jeesuite-config with Apache License 2.0 6 votes vote down vote up
private static void syncConfigIfMiss(String profile,ZkClient zkClient){

	if(!zkClient.exists(ZK_ROOT_PATH)){
		zkClient.createPersistent(ZK_ROOT_PATH, true);
		return;
	}
	
	String parentPath = ZK_ROOT_PATH + "/" + profile;
	if(!zkClient.exists(parentPath)){
		zkClient.createPersistent(parentPath, true);
		return;
	}
	List<String> apps = zkClient.getChildren(parentPath);
	for (String app : apps) {
		parentPath = ZK_ROOT_PATH + "/" + profile + "/" + app + "/nodes";
		
		int activeNodeCount = zkClient.countChildren(parentPath);
		if(activeNodeCount == 0)continue;
		List<ConfigState> localCacheConfigs = configStates.get(app + "#" + profile);
		
		if(activeNodeCount > 0 && (localCacheConfigs == null || localCacheConfigs.size() < activeNodeCount)){
			zkClient.writeData(parentPath, NOTIFY_UPLOAD_CMD);
			logger.info("send cmd[{}] on path[{}]",NOTIFY_UPLOAD_CMD,parentPath);
		}
	}
}
 
Example 9
Source File: HttpServiceDiscovery.java    From soul with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() {
    Boolean register = env.getProperty("soul.http.register", Boolean.class, false);
    if (!register) {
        return;
    }
    String zookeeperUrl = env.getProperty("soul.http.zookeeperUrl", "");
    if (StringUtils.isNoneBlank(zookeeperUrl)) {
        zkClient = new ZkClient(zookeeperUrl, 5000, 2000);
        boolean exists = zkClient.exists(ROOT);
        if (!exists) {
            zkClient.createPersistent(ROOT, true);
        }
        contextPathList = zkClient.getChildren(ROOT);
        updateServerNode(contextPathList);
        zkClient.subscribeChildChanges(ROOT, (parentPath, childs) -> {
            final List<String> addSubscribePath = addSubscribePath(contextPathList, childs);
            updateServerNode(addSubscribePath);
            contextPathList = childs;
        });
    }
}
 
Example 10
Source File: ZookeeperRegistry.java    From SkyEye with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 向注册中心进行注册,生成该服务的编号并返回
 * @param registerDto
 * @return
 */
@Override
public String register(RegisterDto registerDto) {
    String host = registerDto.getHost();
    String app = registerDto.getApp();

    // 向注册中心注册
    ZkClient zkClient = registerDto.getZkClient();
    zkClient.createPersistent(Constants.ZK_REGISTRY_SERVICE_ROOT_PATH + Constants.SLASH + app, true);
    IdGen idGen = new IncrementIdGen(registerDto);
    String id = idGen.nextId();
    zkClient.createEphemeral(Constants.ZK_REGISTRY_SERVICE_ROOT_PATH + Constants.SLASH + app + Constants.SLASH + host, id);

    return id;
}
 
Example 11
Source File: SendCounterHandler.java    From azeroth with Apache License 2.0 5 votes vote down vote up
public SendCounterHandler(String producerGroup, String zkServers) {
    this.producerGroup = producerGroup;
    int sessionTimeoutMs = 10000;
    int connectionTimeoutMs = 10000;
    zkClient = new ZkClient(zkServers, sessionTimeoutMs, connectionTimeoutMs,
        ZKStringSerializer$.MODULE$);
    //
    groupPath = ROOT + "/" + producerGroup;
    if (!zkClient.exists(groupPath)) {
        zkClient.createPersistent(groupPath, true);
    }
    initCollectionTimer();
}
 
Example 12
Source File: ZkRegister.java    From netty-pubsub with MIT License 5 votes vote down vote up
/**
 * zookeeper�ڵ�ע��
 * @param path  �ӽڵ����·��
 * @param data  �ӽڵ�ip:port
 */
public void register(String path,Object data){
	  zkc=new ZkClient(new ZkConnection(CONNECT_ADDR_SINGLE),SESSION_OUTTIME);
	  if(!zkc.exists(ROOT_PATH)){
        	zkc.createPersistent(ROOT_PATH,"brokerList");
        	LOGGER.info("��zk�ڵ��ʼ���ɹ���");
        };
        //������ʱ�ڵ�
        String createEphemeralSequential = zkc.createEphemeralSequential(path, data);
        //���õ�ǰzk·��
        this.currentZKPath=createEphemeralSequential;
        LOGGER.info("��brokerע�᡿path->"+createEphemeralSequential+" data->"+data+" status=SUCCESS");
}
 
Example 13
Source File: ZookeeperCheckpointManagerTest.java    From uReplicator with Apache License 2.0 5 votes vote down vote up
@BeforeTest
public void setup() {
  ZkStarter.startLocalZkServer();
  ZkClient zkClient = ZkUtils.createZkClient(ZkStarter.DEFAULT_ZK_STR, 1000, 1000);
  zkClient.createPersistent("/" + TestUtils.SRC_CLUSTER);
  zkClient.close();
  KafkaUReplicatorMetricsReporter
      .init(new MetricsReporterConf("dca1", new ArrayList<>(), "localhost", null, null));

  for (int i = 0; i < 50; i++) {
    offsetCommitMap
        .put(new TopicPartition(TEST_TOPIC_PREFIX + String.valueOf(i / 10), i % 10), i * 10l);
  }
}
 
Example 14
Source File: ZclientNormal.java    From javabase with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: zkClient主要做了两件事情。
 * @see:一件是在session loss和session expire时自动创建新的ZooKeeper实例进行重连。
 * @see:一件是将一次性watcher包装为持久watcher。
 * @see:后者的具体做法是简单的在watcher回调中,重新读取数据的同时再注册相同的watcher实例。
 */
private static void test() {
	final ZkClient zc = new ZkClient("123.56.118.135:2181");
	// 创建根节点
	/* Persistent与Ephemeral 就是持久化保存到本地和不持久化的区别 ,不能再临时节点下面创建子节点 */
	
	zc.createPersistent("/testroot");
	// zc.createEphemeral("/testroot");
	// 创建子节点
	zc.create("/testroot/node1", "node1", CreateMode.EPHEMERAL);
	zc.createPersistent("/testroot/node2");
	zc.create("/testroot/node2/test", "node1", CreateMode.EPHEMERAL);
	List<String> children = zc.getChildren("/");
	log.info("根节点下面的字节点个数" + children.size());
	
	// 获得子节点个数
	
	int chidrenNumbers = zc.countChildren("/testroot");
	log.info("子节点个数" + chidrenNumbers);
	
	zc.writeData("/testroot/node2/test", "给节点写数据");
	
	// 删除节点
	zc.delete("/testroot/node2/test");
	zc.delete("/testroot/node2");
	zc.delete("/testroot/node1");
	zc.delete("/testroot");
}
 
Example 15
Source File: SendCounterHandler.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
public SendCounterHandler(String producerGroup, ZkClient zkClient) {
	this.zkClient = zkClient;
	this.producerGroup =producerGroup;
	//
	groupPath = ROOT + "/" + producerGroup;
	if(!zkClient.exists(groupPath)){
		zkClient.createPersistent(groupPath, true);
	}
	initCollectionTimer();
}
 
Example 16
Source File: TestZkNamespace.java    From samza with Apache License 2.0 5 votes vote down vote up
private void createNamespace(String pathToCreate) {
  if (Strings.isNullOrEmpty(pathToCreate)) {
    return;
  }

  String zkConnect = "127.0.0.1:" + zkServer.getPort();
  try {
    zkClient1 = new ZkClient(new ZkConnection(zkConnect, SESSION_TIMEOUT_MS), CONNECTION_TIMEOUT_MS);
  } catch (Exception e) {
    Assert.fail("Client connection setup failed. Aborting tests..");
  }
  zkClient1.createPersistent(pathToCreate, true);
}
 
Example 17
Source File: LocalCacheUtil.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Notify resource changed.
 * 向zookeeper注册中心广播 notifyName 对应的资源改变
 * 
 * @param notifyName the resource name
 */
public static void notifyResourceChanged(String notifyName) {
    ZkClient zClient = getZkClient();
    Long nanoTime = System.nanoTime();
    String resourcePath = getResourcePath(notifyName);
    if (!zClient.exists(resourcePath)) {
        zClient.createPersistent(resourcePath, true);
    }
    zClient.writeData(resourcePath, nanoTime);
}
 
Example 18
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 19
Source File: ZkUtil.java    From java-study with Apache License 2.0 2 votes vote down vote up
/**
 * 创建目录不添加参数
 * 也可以创建层级目录,格式: /test/test1/test1-1
 * @param zk
 * @param path
 * @return
 */
public static void create(ZkClient zk,String path) throws ZkInterruptedException, IllegalArgumentException, ZkException, RuntimeException{
	if(!zk.exists(path)){
		zk.createPersistent(path,true);
	}
}