org.I0Itec.zkclient.ZkClient Java Examples

The following examples show how to use org.I0Itec.zkclient.ZkClient. 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: KafkaAppender.java    From SkyEye with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void stop() {
    super.stop();

    // 停止心跳
    this.heartbeatStop();

    // 关闭KafkaProuder
    if (LazySingletonProducer.isInstanced()) {
        // producer实际上已经初始化
        LazySingletonProducer.getInstance(this.config).close();
    }

    // 关闭client,临时节点消失,监控系统进行感知报警
    ZkClient client = this.zkRegister == null ? null : this.zkRegister.getClient();
    if (null != client) {
        client.close();
    }
}
 
Example #2
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 #3
Source File: PepperBoxSamplerTest.java    From pepper-box with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws IOException {

    zkServer = new EmbeddedZookeeper();

    String zkConnect = ZKHOST + ":" + zkServer.port();
    zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$);
    ZkUtils zkUtils = ZkUtils.apply(zkClient, false);

    Properties brokerProps = new Properties();
    brokerProps.setProperty("zookeeper.connect", zkConnect);
    brokerProps.setProperty("broker.id", "0");
    brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafka-").toAbsolutePath().toString());
    brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKERHOST +":" + BROKERPORT);
    KafkaConfig config = new KafkaConfig(brokerProps);
    Time mock = new MockTime();
    kafkaServer = TestUtils.createServer(config, mock);
    //AdminUtils.createTopic(zkUtils, TOPIC, 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);

    JMeterContext jmcx = JMeterContextService.getContext();
    jmcx.setVariables(new JMeterVariables());

}
 
Example #4
Source File: KafkaTestEnvironmentImpl.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteTestTopic(String topic) {
	ZkUtils zkUtils = getZkUtils();
	try {
		LOG.info("Deleting topic {}", topic);

		ZkClient zk = new ZkClient(zookeeperConnectionString, Integer.valueOf(standardProps.getProperty("zookeeper.session.timeout.ms")),
			Integer.valueOf(standardProps.getProperty("zookeeper.connection.timeout.ms")), new ZooKeeperStringSerializer());

		AdminUtils.deleteTopic(zkUtils, topic);

		zk.close();
	} finally {
		zkUtils.close();
	}
}
 
Example #5
Source File: PepperBoxLoadGenTest.java    From pepper-box with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws IOException {

    zkServer = new EmbeddedZookeeper();

    String zkConnect = ZKHOST + ":" + zkServer.port();
    zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$);
    ZkUtils zkUtils = ZkUtils.apply(zkClient, false);

    Properties brokerProps = new Properties();
    brokerProps.setProperty("zookeeper.connect", zkConnect);
    brokerProps.setProperty("broker.id", "0");
    brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafka-").toAbsolutePath().toString());
    brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKERHOST +":" + BROKERPORT);
    KafkaConfig config = new KafkaConfig(brokerProps);
    Time mock = new MockTime();
    kafkaServer = TestUtils.createServer(config, mock);
    //AdminUtils.createTopic(zkUtils, TOPIC, 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);

    JMeterContext jmcx = JMeterContextService.getContext();
    jmcx.setVariables(new JMeterVariables());

}
 
Example #6
Source File: AutoTopicWhitelistingManager.java    From uReplicator with Apache License 2.0 6 votes vote down vote up
public AutoTopicWhitelistingManager(KafkaBrokerTopicObserver srcKafkaTopicObserver,
    KafkaBrokerTopicObserver destKafkaTopicObserver,
    HelixMirrorMakerManager helixMirrorMakerManager,
    String patternToExcludeTopics,
    int refreshTimeInSec,
    int initWaitTimeInSec) {
  _srcKafkaTopicObserver = srcKafkaTopicObserver;
  _destKafkaTopicObserver = destKafkaTopicObserver;
  _helixMirrorMakerManager = helixMirrorMakerManager;
  _patternToExcludeTopics = patternToExcludeTopics;
  _refreshTimeInSec = refreshTimeInSec;
  _initWaitTimeInSec = initWaitTimeInSec;
  _zkClient = new ZkClient(_helixMirrorMakerManager.getHelixZkURL(), 30000, 30000, ZKStringSerializer$.MODULE$);
  _zkUtils = ZkUtils.apply(_zkClient, false);
  _blacklistedTopicsZPath = String.format("/%s/BLACKLISTED_TOPICS", _helixMirrorMakerManager.getHelixClusterName());
}
 
Example #7
Source File: KafkaMonitor.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
public KafkaMonitor(String zkServers,String kafkaServers,int latThreshold) {
	Validate.notBlank(zkServers);
	Validate.notBlank(kafkaServers);
	this.latThreshold = latThreshold;
	
	zkClient = new ZkClient(zkServers, 10000, 10000, new ZKStringSerializer());
	
	try {			
		zkConsumerCommand = new ZkConsumerCommand(zkClient,zkServers, kafkaServers);
		kafkaConsumerCommand = new KafkaConsumerCommand(kafkaServers);
	} catch (Exception e) {
		e.printStackTrace();
	}
	// 
	initCollectionTimer();
}
 
Example #8
Source File: KafkaTestBase.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
public static void startServer() throws RuntimeException {
  if (serverStarted && serverClosed) {
    throw new RuntimeException("Kafka test server has already been closed. Cannot generate Kafka server twice.");
  }

  if (!serverStarted) {
    serverStarted = true;
    zkConnect = TestZKUtils.zookeeperConnect();
    zkServer = new EmbeddedZookeeper(zkConnect);
    zkClient = new ZkClient(zkServer.connectString(), 30000, 30000, ZKStringSerializer$.MODULE$);

    kafkaPort = TestUtils.choosePort();
    Properties props = TestUtils.createBrokerConfig(brokerId, kafkaPort, true);

    KafkaConfig config = new KafkaConfig(props);
    Time mock = new MockTime();
    kafkaServer = TestUtils.createServer(config, mock);
  }
}
 
Example #9
Source File: KafkaTopics.java    From rya with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a set of Kafka topics for each topic that does not already exist.
 *
 * @param zookeeperServers - The Zookeeper servers that are used by the Kafka Streams program. (not null)
 * @param topicNames - The topics that will be created. (not null)
 * @param partitions - The number of partitions that each of the topics will have.
 * @param replicationFactor - The replication factor of the topics that are created.
 * @param topicProperties - The optional properties of the topics to create.
 */
public static void createTopics(
        final String zookeeperServers,
        final Set<String> topicNames,
        final int partitions,
        final int replicationFactor,
        final Optional<Properties> topicProperties) {
    requireNonNull(zookeeperServers);
    requireNonNull(topicNames);

    ZkUtils zkUtils = null;
    try {
        zkUtils = ZkUtils.apply(new ZkClient(zookeeperServers, 30000, 30000, ZKStringSerializer$.MODULE$), false);
        for(final String topicName : topicNames) {
            if(!AdminUtils.topicExists(zkUtils, topicName)) {
                AdminUtils.createTopic(zkUtils, topicName, partitions, replicationFactor, topicProperties.orElse(new Properties()), RackAwareMode.Disabled$.MODULE$);
            }
        }
    }
    finally {
        if(zkUtils != null) {
            zkUtils.close();
        }
    }
}
 
Example #10
Source File: ZkclientZookeeperClient.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
public ZkclientZookeeperClient(URL url) {
	super(url);
	client = new ZkClient(
               url.getBackupAddress(),
               url.getParameter(Constants.SESSION_TIMEOUT_KEY, Constants.DEFAULT_SESSION_TIMEOUT),
               url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_REGISTRY_CONNECT_TIMEOUT));
	client.subscribeStateChanges(new IZkStateListener() {
		public void handleStateChanged(KeeperState state) throws Exception {
			ZkclientZookeeperClient.this.state = state;
			if (state == KeeperState.Disconnected) {
				stateChanged(StateListener.DISCONNECTED);
			} else if (state == KeeperState.SyncConnected) {
				stateChanged(StateListener.CONNECTED);
			}
		}
		public void handleNewSession() throws Exception {
			stateChanged(StateListener.RECONNECTED);
		}
	});
}
 
Example #11
Source File: KafkaResourceController.java    From pubsub with Apache License 2.0 6 votes vote down vote up
@Override
protected void startAction() throws Exception {
  ZkClient zookeeperClient =
      new ZkClient(
          KafkaFlags.getInstance().zookeeperIp, 15000, 10000, ZKStringSerializer$.MODULE$);
  ZkUtils zookeeperUtils =
      new ZkUtils(zookeeperClient, new ZkConnection(KafkaFlags.getInstance().zookeeperIp), false);
  try {
    deleteTopic(zookeeperUtils);
    AdminUtils.createTopic(
        zookeeperUtils,
        topic,
        KafkaFlags.getInstance().partitions,
        KafkaFlags.getInstance().replicationFactor,
        AdminUtils.createTopic$default$5(),
        AdminUtils.createTopic$default$6());
    log.info("Created topic " + topic + ".");
  } finally {
    zookeeperClient.close();
  }
}
 
Example #12
Source File: KafkaTestEnvironmentImpl.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void deleteTestTopic(String topic) {
	ZkUtils zkUtils = getZkUtils();
	try {
		LOG.info("Deleting topic {}", topic);

		ZkClient zk = new ZkClient(zookeeperConnectionString, Integer.valueOf(standardProps.getProperty("zookeeper.session.timeout.ms")),
			Integer.valueOf(standardProps.getProperty("zookeeper.connection.timeout.ms")), new ZooKeeperStringSerializer());

		AdminUtils.deleteTopic(zkUtils, topic);

		zk.close();
	} finally {
		zkUtils.close();
	}
}
 
Example #13
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 #14
Source File: ZkCoordinationUtilsFactory.java    From samza with Apache License 2.0 6 votes vote down vote up
/**
 * if ZkConnectString contains namespace path at the end, but it does not exist we should fail
 * @param zkConnect - connect string
 * @param zkClient - zkClient object to talk to the ZK
 */
public static void validateZkNameSpace(String zkConnect, ZkClient zkClient) {
  ConnectStringParser parser = new ConnectStringParser(zkConnect);

  String path = parser.getChrootPath();
  if (Strings.isNullOrEmpty(path)) {
    return; // no namespace path
  }

  LOG.info("connectString = " + zkConnect + "; path =" + path);

  // if namespace specified (path above) but "/" does not exists, we will fail
  if (!zkClient.exists("/")) {
    throw new SamzaException("Zookeeper namespace: " + path + " does not exist for zk at " + zkConnect);
  }
}
 
Example #15
Source File: KafkaRpcTopicService.java    From devicehive-java-server with Apache License 2.0 6 votes vote down vote up
public void createTopic(String topic) {
    ZkClient zkClient = new ZkClient(
            kafkaRpcConfig.getZookeeperConnect(),
            kafkaRpcConfig.getSessionTimeout(),
            kafkaRpcConfig.getConnectionTimeout(),
            ZKStringSerializer$.MODULE$);
    try {
        ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(kafkaRpcConfig.getZookeeperConnect()), false);
        Properties topicConfig = kafkaRpcConfig.topicProps();
        if (!AdminUtils.topicExists(zkUtils, topic)) {
            AdminUtils.createTopic(zkUtils, topic, kafkaRpcConfig.getNumPartitions(), 
                    kafkaRpcConfig.getReplicationFactor(), topicConfig, RackAwareMode.Enforced$.MODULE$);
        }
    } finally {
        zkClient.close();
    }
}
 
Example #16
Source File: KafkaTestBase.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
void start()
  throws RuntimeException {
  if (_numStarted.incrementAndGet() == 1) {
    log.warn("Starting up Kafka server suite. Zk at " + _zkConnectString + "; Kafka server at " + _kafkaServerPort);
    _zkServer = new EmbeddedZookeeper(_zkConnectString);
    _zkClient = new ZkClient(_zkConnectString, 30000, 30000, ZKStringSerializer$.MODULE$);


    Properties props = kafka.utils.TestUtils.createBrokerConfig(_brokerId, _kafkaServerPort, true);
    props.setProperty("zookeeper.connect", _zkConnectString);

    KafkaConfig config = new KafkaConfig(props);
    Time mock = new MockTime();
    _kafkaServer = kafka.utils.TestUtils.createServer(config, mock);
  }
  else
  {
    log.info("Kafka server suite already started... continuing");
  }
}
 
Example #17
Source File: ZclientServerPub.java    From javabase with Apache License 2.0 6 votes vote down vote up
private static void test() throws InterruptedException {
	final ZkClient zkClient4subChild = new ZkClient("localhost:2181");
	
	zkClient4subChild.create("/serverroot", "serverroot", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(4000);
	zkClient4subChild.create("/serverroot/server1", "server1", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(4000);
	zkClient4subChild.create("/serverroot/server2", "server2", Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
	
	Thread.sleep(20000);
	zkClient4subChild.delete("/serverroot/server1");
	zkClient4subChild.delete("/serverroot/server2");
	zkClient4subChild.delete("/serverroot");
}
 
Example #18
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 #19
Source File: ClientProcessor.java    From kob with Apache License 2.0 6 votes vote down vote up
private void heartbeat() {
    CLIENT_HEARTBEAT.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            try {
                heartbeat0();
            } catch (Exception e) {
                log.error(ClientLogConstant.error503(), e);
            }
        }

        private void heartbeat0() throws UnsupportedEncodingException {
            ZkClient zkClient = clientContext.getZkClient();
            String path = clientContext.getClientNodePath()+ ZkPathConstant.BACKSLASH + clientContext.getData().getClientPath();
            if (zkClient.exists(path)) {
                zkClient.writeData(path, clientContext.getData());
            } else {
                zkClient.create(path, clientContext.getData(), CreateMode.EPHEMERAL);
            }
            log.info(ClientLogConstant.info102(60L, clientContext.getData().getWorkers(), 1));
        }
    }, 15, 60, TimeUnit.SECONDS);
}
 
Example #20
Source File: KafkaUtilsTest.java    From incubator-samoa with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpClass() throws IOException {
    // setup Zookeeper
    zkServer = new EmbeddedZookeeper();
    zkConnect = ZKHOST + ":" + zkServer.port();
    zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$);
    ZkUtils zkUtils = ZkUtils.apply(zkClient, false);

    // setup Broker
    Properties brokerProps = new Properties();
    brokerProps.setProperty("zookeeper.connect", zkConnect);
    brokerProps.setProperty("broker.id", "0");
    brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafkaUtils-").toAbsolutePath().toString());
    brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKERHOST + ":" + BROKERPORT);
    KafkaConfig config = new KafkaConfig(brokerProps);
    Time mock = new MockTime();
    kafkaServer = TestUtils.createServer(config, mock);

    // create topics
    AdminUtils.createTopic(zkUtils, TOPIC_R, 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);
    AdminUtils.createTopic(zkUtils, TOPIC_S, 1, 1, new Properties(), RackAwareMode.Disabled$.MODULE$);

}
 
Example #21
Source File: KafkaTool.java    From Scribengin with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Create a topic. This method will not create a topic that is currently scheduled for deletion.
 * For valid configs see https://cwiki.apache.org/confluence/display/KAFKA/Replication+tools#Replicationtools-Howtousethetool?.3
 *
 * @See https://kafka.apache.org/documentation.html#topic-config 
 * for more valid configs
 * */
public void createTopic(String[] args) throws Exception {
  int sessionTimeoutMs = 10000;
  int connectionTimeoutMs = 10000;

  TopicCommandOptions options = new TopicCommandOptions(args);
  ZkClient client = new ZkClient(zkConnects, sessionTimeoutMs, connectionTimeoutMs, ZKStringSerializer$.MODULE$);
  if (topicExits(name)) {
    TopicCommand.deleteTopic(client, options);
  }

  TopicCommand.createTopic(client, options);
  try {
    Thread.sleep(3000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }
  client.close();
}
 
Example #22
Source File: TestZkJobCoordinator.java    From samza with Apache License 2.0 5 votes vote down vote up
@Test
public void testShouldStartPartitionCountMonitorOnBecomingLeader() {
  ZkKeyBuilder keyBuilder = Mockito.mock(ZkKeyBuilder.class);
  ZkClient mockZkClient = Mockito.mock(ZkClient.class);
  when(keyBuilder.getJobModelVersionBarrierPrefix()).thenReturn(TEST_BARRIER_ROOT);

  ZkUtils zkUtils = Mockito.mock(ZkUtils.class);
  when(zkUtils.getKeyBuilder()).thenReturn(keyBuilder);
  when(zkUtils.getZkClient()).thenReturn(mockZkClient);
  when(zkUtils.getJobModel(TEST_JOB_MODEL_VERSION)).thenReturn(new JobModel(new MapConfig(), new HashMap<>()));

  ScheduleAfterDebounceTime mockDebounceTimer = Mockito.mock(ScheduleAfterDebounceTime.class);

  ZkJobCoordinator zkJobCoordinator = Mockito.spy(new ZkJobCoordinator("TEST_PROCESSOR_ID", new MapConfig(),
      new NoOpMetricsRegistry(), zkUtils, zkMetadataStore, coordinatorStreamStore));

  StreamPartitionCountMonitor monitor = Mockito.mock(StreamPartitionCountMonitor.class);
  zkJobCoordinator.debounceTimer = mockDebounceTimer;
  zkJobCoordinator.streamPartitionCountMonitor = monitor;
  when(zkJobCoordinator.getPartitionCountMonitor()).thenReturn(monitor);

  ZkJobCoordinator.LeaderElectorListenerImpl listener = zkJobCoordinator.new LeaderElectorListenerImpl();

  listener.onBecomingLeader();

  Mockito.verify(monitor).start();
}
 
Example #23
Source File: ZclientSub.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 zkClient4subChild = new ZkClient("localhost:2181");
	zkClient4subChild.subscribeChildChanges("/serverroot", new IZkChildListener() {
		public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
			System.out.println("=======test===");
			for (String string : currentChilds) {
				System.out.print(zkClient4subChild.readData("/serverroot/" + string, false) + ";");
			}
		}
	});
}
 
Example #24
Source File: ZookeeperConfiguration.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
public ZkUtils newConnection() {
    int sessionTimeoutMs = sessionTimeoutSec * 1000;
    int connectionTimeoutMs = connectionTimeoutSec * 1000;
    String zookeeperConnect = bootstrapServers;

    ZkClient zkClient = new ZkClient(
            zookeeperConnect,
            sessionTimeoutMs,
            connectionTimeoutMs,
            ZKStringSerializer$.MODULE$);
    return new ZkUtils(zkClient, new ZkConnection(zookeeperConnect), topicSecured);
}
 
Example #25
Source File: LocalCacheUtil.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 删除notifyName 对应的监听,基本没用到.
 *
 * @param notifyName the resource name
 * @return true, if successful
 */
public static boolean deleteResourceNode(String notifyName) {
    ZkClient zClient = getZkClient();
    //删除单独一个节点,返回true表示成功  
    //        boolean e1 = zkClient.delete("/testUserNode");  
    //删除含有子节点的节点  
    String resourcePath = getResourcePath(notifyName);
    return zClient.deleteRecursive(resourcePath);
}
 
Example #26
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 #27
Source File: ControllerHelixManager.java    From uReplicator with Apache License 2.0 5 votes vote down vote up
public ControllerHelixManager(
    KafkaClusterValidationManager kafkaValidationManager,
    ManagerConf managerConf) {
  _conf = managerConf;
  _kafkaValidationManager = kafkaValidationManager;
  _initMaxNumPartitionsPerRoute = managerConf.getInitMaxNumPartitionsPerRoute();
  _maxNumPartitionsPerRoute = managerConf.getMaxNumPartitionsPerRoute();
  _initMaxNumWorkersPerRoute = managerConf.getInitMaxNumWorkersPerRoute();
  _maxNumWorkersPerRoute = managerConf.getMaxNumWorkersPerRoute();
  _workloadRefreshPeriodInSeconds = managerConf.getWorkloadRefreshPeriodInSeconds();
  _workerHelixManager = new WorkerHelixManager(managerConf);
  _pipelineWorkloadMap = new ConcurrentHashMap<>();
  _helixZkURL = HelixUtils.getAbsoluteZkPathForHelix(managerConf.getManagerZkStr());
  _helixClusterName = MANAGER_CONTROLLER_HELIX_PREFIX + "-" + managerConf.getManagerDeployment();
  _instanceId = managerConf.getManagerInstanceId();
  _topicToPipelineInstanceMap = new ConcurrentHashMap<>();
  _pipelineToInstanceMap = new ConcurrentHashMap<>();
  _availableControllerList = new ArrayList<>();
  _routeToCounterMap = new ConcurrentHashMap<>();
  _zkClient = new ZkClient(_helixZkURL, 30000, 30000, ZKStringSerializer$.MODULE$);
  registerMetrics();

  PoolingHttpClientConnectionManager limitedConnMgr = new PoolingHttpClientConnectionManager();
  // TODO: make it configurable
  limitedConnMgr.setDefaultMaxPerRoute(100);
  limitedConnMgr.setMaxTotal(100);
  _httpClient = HttpClients.createMinimal(limitedConnMgr);
  // requestConfig is immutable. These three timeouts are for
  // 1. getting connection from connection manager;
  // 2. establishing connection with server;
  // 3. getting next data snippet from server.
  _requestConfig = RequestConfig.custom()
      .setConnectionRequestTimeout(30000)
      .setConnectTimeout(30000)
      .setSocketTimeout(30000)
      .build();
}
 
Example #28
Source File: ProfileZkClient.java    From jeesuite-config with Apache License 2.0 5 votes vote down vote up
@Override
public void run(String... args) throws Exception {
	List<ProfileEntity> profiles = profileMapper.findAllEnabledProfiles();
	for (ProfileEntity profile : profiles) {
		String zkServers = profileMapper.findExtrAttr(profile.getName(), ProfileExtrAttrName.ZK_SERVERS.name());
		if(zkServers != null){
			try {	
				if(profileZkServersMapping.values().contains(zkServers)){
					inner:for (String sameProfileName : profileZkServersMapping.keySet()) {
						if(profileZkServersMapping.get(sameProfileName).equals(zkServers)){								
							profileZkClientMapping.put(profile.getName(), profileZkClientMapping.get(sameProfileName));
							logger.info("create zkClient ok -> profile:{},sameProfileName:{}",profile,sameProfileName);
							break inner;
						}
					}
				}else{
					ZkConnection zkConnection = new ZkConnection(zkServers);
					ZkClient zkClient = new ZkClient(zkConnection, 3000);
					profileZkClientMapping.put(profile.getName(), zkClient);
					profileZkServersMapping.put(profile.getName(), zkServers);
					logger.info("create zkClient ok -> profile:{},zkServers:{}",profile.getName(),zkServers);
				}
				
			} catch (Exception e) {
				logger.error("create zkClient:" + zkServers,e);
			}
		}
	}
	//
	ConfigStateHolder.setProfileZkClient(this);
}
 
Example #29
Source File: NodeDAO.java    From shepher with Apache License 2.0 5 votes vote down vote up
public List<String> getChildren(String cluster, String path) throws ShepherException {
    ZkClient zkClient = ZkPool.getZkClient(cluster);
    try {
        if (zkClient == null) {
            return Collections.emptyList();
        }
        return zkClient.getChildren(path);
    } catch (Exception e) {
        LOGGER.warn("Fail to get children, Exception:", e);
        throw ShepherException.createUnknownException();
    } finally {
        ZkPool.releaseZkClient(cluster, zkClient);
    }
}
 
Example #30
Source File: TollboothApp.java    From data-highway with Apache License 2.0 5 votes vote down vote up
private void checkAndCreateTopic(String zkConnect, String topic, int partitions, int replicas) {
  ZkClient zkClient = new ZkClient(zkConnect, SESSION_TIMEOUT_MS, CONNECTION_TIMEOUT_MS, ZKStringSerializer$.MODULE$);
  ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(zkConnect), IS_SECURE_KAFKA_CLUSTER);

  if (!AdminUtils.topicExists(zkUtils, topic)) {
    AdminUtils.createTopic(zkUtils, topic, partitions, replicas, new Properties(), RackAwareMode.Enforced$.MODULE$);
  }

  zkUtils.close();
  zkClient.close();
}