Java Code Examples for org.apache.curator.framework.CuratorFramework

The following are top voted examples for showing how to use org.apache.curator.framework.CuratorFramework. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: dble   File: ProxyMetaManager.java   Source Code and License 7 votes vote down vote up
public void notifyClusterDDL(String schema, String table, String sql, DDLInfo.DDLStatus ddlStatus, boolean needNotifyOther) throws Exception {
    CuratorFramework zkConn = ZKUtils.getConnection();
    DDLInfo ddlInfo = new DDLInfo(schema, sql, ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_MYID), ddlStatus);
    String nodeName = StringUtil.getFullName(schema, table);
    String nodePath = ZKPaths.makePath(KVPathUtil.getDDLPath(), nodeName);
    if (zkConn.checkExists().forPath(nodePath) == null) {
        zkConn.create().forPath(nodePath, ddlInfo.toString().getBytes(StandardCharsets.UTF_8));
    } else {
        String instancePath = ZKPaths.makePath(nodePath, KVPathUtil.DDL_INSTANCE);
        String thisNode = ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_MYID);
        ZKUtils.createTempNode(instancePath, thisNode);
        if (needNotifyOther) {
            //this node is ddl sender
            zkConn.setData().forPath(nodePath, ddlInfo.toString().getBytes(StandardCharsets.UTF_8));
            while (true) {
                List<String> preparedList = zkConn.getChildren().forPath(instancePath);
                List<String> onlineList = zkConn.getChildren().forPath(KVPathUtil.getOnlinePath());
                if (preparedList.size() >= onlineList.size()) {
                    zkConn.delete().deletingChildrenIfNeeded().forPath(nodePath);
                    break;
                }
                LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(100));
            }
        }
    }
}
 
Example 2
Project: stroom-stats   File: ServiceDiscoveryCuratorFrameworkProvider.java   Source Code and License 7 votes vote down vote up
@Override
public CuratorFramework get() {
    String quorum = zookeeperConfig.getQuorum();
    String serviceDiscoveryPath = zookeeperConfig.getServiceDiscoveryPath();
    String connectionString = quorum + (serviceDiscoveryPath == null ? "" : serviceDiscoveryPath);

    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);

    LOGGER.info("Initiating Curator connection to Zookeeper using: [{}]", connectionString);
    // Use chroot so all subsequent paths are below /stroom-services to avoid conflicts with hbase/zookeeper/kafka etc.
    CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
    client.start();

    try {
        //Ensure the chrooted path for stroom-services exists
        Stat stat = client.checkExists().forPath("/");
        if (stat == null) {
            LOGGER.info("Creating chroot-ed root node inside " + serviceDiscoveryPath);
            client.create().forPath("/");
        }
    } catch (Exception e) {
        throw new RuntimeException("Error connecting to zookeeper using connection String: " + connectionString, e);
    }
    return client;
}
 
Example 3
Project: dble   File: ZKUtils.java   Source Code and License 6 votes vote down vote up
private static CuratorFramework createConnection() {
    String url = ZkConfig.getInstance().getZkURL();
    CuratorFramework framework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));
    // start connection
    framework.start();
    // wait 3 second to establish connect
    try {
        framework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (framework.getZookeeperClient().isConnected()) {
            LOGGER.info("CuratorFramework createConnection success");
            return framework;
        }
    } catch (InterruptedException ignored) {
        LOGGER.info("CuratorFramework createConnection error", ignored);
        Thread.currentThread().interrupt();
    }
    // fail situation
    framework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 4
Project: mycat-src-1.6.1-RELEASE   File: ZKUtils.java   Source Code and License 6 votes vote down vote up
private static CuratorFramework createConnection() {
       String url= ZkConfig.getInstance().getZkURL();

    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));

    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework;
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }

    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 5
Project: zoomap   File: ZooMap.java   Source Code and License 6 votes vote down vote up
private ZooMap(Builder builder) {
    this.connectionString = builder.connectionString;
    ConnectStringParser connectStringParser = new ConnectStringParser(connectionString);
    if(connectStringParser.getChrootPath() != null) {
        final String connectionStringForChrootCreation = connectStringParser.getServerAddresses().stream().map(InetSocketAddress::toString).collect(Collectors.joining(","));
        try(final CuratorFramework clientForChrootCreation = newCuratorFrameworkClient(builder, connectionStringForChrootCreation)) {
            startAndBlock(clientForChrootCreation);
            tryIt(() -> clientForChrootCreation.createContainers(connectStringParser.getChrootPath()));
        }
    }
    client = newCuratorFrameworkClient(builder, connectionString);
    this.root = builder.root;
    startAndBlock(client);
    if(!root.isEmpty()) {
        tryIt(() -> client.createContainers(root));
    }
}
 
Example 6
Project: fastmq   File: ZkOffsetStorageImplTest.java   Source Code and License 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    Configurator
        .initialize("FastMQ", Thread.currentThread().getContextClassLoader(), "log4j2.xml");
    Log log = new Log();
    LogSegment logSegment = new LogSegment();
    logSegment.setLedgerId(ledgerId);
    logSegment.setTimestamp(System.currentTimeMillis());
    log.setSegments(Collections.singletonList(logSegment));
    when(logInfoStorage.getLogInfo(any())).thenReturn(log);

    CuratorFramework curatorFramework = CuratorFrameworkFactory
        .newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));
    curatorFramework.start();
    asyncCuratorFramework = AsyncCuratorFramework.wrap(curatorFramework);
    offsetStorage = new ZkOffsetStorageImpl(logInfoStorage, asyncCuratorFramework);
}
 
Example 7
Project: QDrill   File: ZkAbstractStore.java   Source Code and License 6 votes vote down vote up
public ZkAbstractStore(CuratorFramework framework, PStoreConfig<V> config)
    throws IOException {
  this.parent = "/" + config.getName();
  this.prefix = parent + "/";
  this.framework = framework;
  this.config = config;

  // make sure the parent node exists.
  try {
    if (framework.checkExists().forPath(parent) == null) {
      framework.create().withMode(CreateMode.PERSISTENT).forPath(parent);
    }

    this.childrenCache = new PathChildrenCache(framework, parent, true);
    this.childrenCache.start(StartMode.BUILD_INITIAL_CACHE);

  } catch (Exception e) {
    throw new RuntimeException("Failure while accessing Zookeeper for PStore: " + e.getMessage(), e);
  }

}
 
Example 8
Project: snowflake   File: TestSeqBase.java   Source Code and License 6 votes vote down vote up
public ZkRangeStore getZkRangeStore(int clientIndex) {
	String lockPath = "/snowflake/locks";
	String storePath = "/snowflake/idstore";
	CuratorFramework curatorFramework = ZkUtils.create("127.0.0.1:2181", 1000, 10000);
	curatorFramework.start();
	for (String client : clients) {
		try {
			curatorFramework.setData().forPath(ZKPaths.makePath(storePath, client), "0".getBytes());
		} catch (Exception e) {
			if (e instanceof KeeperException.NoNodeException) {
				try {
					curatorFramework.create().creatingParentsIfNeeded().forPath(storePath, "0".getBytes());
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		}
	}
	return new ZkRangeStore(clients.get(clientIndex), curatorFramework, lockPath, storePath, 1, TimeUnit.SECONDS, 0, 10);
}
 
Example 9
Project: flume-release-1.7.0   File: StaticZooKeeperConfigurationProvider.java   Source Code and License 6 votes vote down vote up
@Override
protected FlumeConfiguration getFlumeConfiguration() {
  try {
    CuratorFramework cf = createClient();
    cf.start();
    try {
      byte[] data = cf.getData().forPath(basePath + "/" + getAgentName());
      return configFromBytes(data);
    } finally {
      cf.close();
    }
  } catch (Exception e) {
    LOGGER.error("Error getting configuration info from Zookeeper", e);
    throw new FlumeException(e);
  }
}
 
Example 10
Project: trellis-rosid-file   File: FileResourceService.java   Source Code and License 6 votes vote down vote up
/**
 * Create a File-based repository service
 * @param partitionData the partition data configuration
 * @param partitionUrls the partition URL configuration
 * @param curator the curator framework
 * @param producer the kafka producer
 * @param notifications the notification service
 * @param idSupplier an identifier supplier for new resources
 * @param async generate cached resources asynchronously if true, synchonously if false
 * @throws IOException if the directory is not writable
 */
public FileResourceService(final Map<String, String> partitionData, final Map<String, String> partitionUrls,
        final CuratorFramework curator, final Producer<String, String> producer, final EventService notifications,
        final Supplier<String> idSupplier, final Boolean async) throws IOException {
    super(partitionUrls, producer, curator, notifications, idSupplier, async);

    requireNonNull(partitionData, "partition data configuration may not be null!");

    RESERVED_PARTITION_NAMES.stream().filter(partitionData::containsKey).findAny().ifPresent(name -> {
        throw new IllegalArgumentException("Invalid partition name: " + name);
    });

    this.partitionData = partitionData;

    init();
}
 
Example 11
Project: redirector   File: AbstractCommonBeans.java   Source Code and License 6 votes vote down vote up
@Bean
public CuratorFramework curatorFramework() {
    ZKConfig config = config();

    if (config.useZooKeeperWaitTimePolicy()) {
        return new RedirectorCuratorFramework(config);
    }

    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
            .connectString(config.getZooKeeperConnection())
            .connectionTimeoutMs(config.getZooKeeperConnectionTimeout())
            .sessionTimeoutMs(config.getZooKeeperSessionTimeout())
            .retryPolicy(new RetryNTimes(config.getZooKeeperRetryAttempts(), config.getZooKeeperRetryInterval()))
            .compressionProvider(new GzipCompressionProvider());

    return builder.build();
}
 
Example 12
Project: jigsaw-payment   File: HelloServerConfig.java   Source Code and License 6 votes vote down vote up
@Bean(name = "curator-framework")
public CuratorFramework curatorFramework() {
	return CuratorFrameworkFactory
			.builder()
			.connectString(
					env.getProperty("rpc.server.zookeeper.connect.string"))
			.sessionTimeoutMs(
					Integer.parseInt(env.getProperty(
							"rpc.server.zookeeper.session.timeout.ms",
							"10000")))
			.connectionTimeoutMs(
					Integer.parseInt(env.getProperty(
							"rpc.server.zookeeper.connection.timeout.ms",
							"10000"))).retryPolicy(this.retryPolicy())
			.aclProvider(this.aclProvider()).authorization(this.authInfo())
			.build();
}
 
Example 13
Project: dble   File: ViewChildListener.java   Source Code and License 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData childData = event.getData();
    switch (event.getType()) {
        case CHILD_ADDED:
            createOrUpdateViewMeta(childData, false);
            break;
        case CHILD_UPDATED:
            createOrUpdateViewMeta(childData, true);
            break;
        case CHILD_REMOVED:
            deleteNode(childData);
            break;
        default:
            break;
    }
}
 
Example 14
Project: GoPush   File: ZkUtils.java   Source Code and License 6 votes vote down vote up
/**
     * 设置子节点更改监听
     *
     * @param path
     * @throws Exception
     */
    public boolean listenerPathChildrenCache(String path, BiConsumer<CuratorFramework, PathChildrenCacheEvent> biConsumer) {

        if (!ObjectUtils.allNotNull(zkClient, path, biConsumer)) {
            return Boolean.FALSE;
        }
        try {
            Stat stat = exists(path);
            if (stat != null) {
                PathChildrenCache watcher = new PathChildrenCache(zkClient, path, true);
                watcher.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
                //该模式下 watcher在重连的时候会自动 rebuild 否则需要重新rebuild
                watcher.getListenable().addListener(biConsumer::accept, pool);
                if (!pathChildrenCaches.contains(watcher)) {
                    pathChildrenCaches.add(watcher);
                }
//                else{
//                    watcher.rebuild();
//                }
                return Boolean.TRUE;
            }
        } catch (Exception e) {
            log.error("listen path children cache fail! path:{} , error:{}", path, e);
        }
        return Boolean.FALSE;
    }
 
Example 15
Project: mycat-src-1.6.1-RELEASE   File: RulesxmlTozkLoader.java   Source Code and License 6 votes vote down vote up
public RulesxmlTozkLoader(ZookeeperProcessListen zookeeperListen, CuratorFramework curator,
        XmlProcessBase xmlParseBase) {

    this.setCurator(curator);

    // 获得当前集群的名称
    String schemaPath = zookeeperListen.getBasePath();
    schemaPath = schemaPath + ZookeeperPath.ZK_SEPARATOR.getKey() + ZookeeperPath.FLOW_ZK_PATH_RULE.getKey();

    currZkPath = schemaPath;
    // 将当前自己注册为事件接收对象
    zookeeperListen.addListen(schemaPath, this);

    // 生成xml与类的转换信息
    parseRulesXMl = new RuleParseXmlImpl(xmlParseBase);
}
 
Example 16
Project: dble   File: DbleServer.java   Source Code and License 6 votes vote down vote up
private void initZkDnindex() {
    //upload the dnindex data to zk
    try {
        if (dnIndexLock.acquire(30, TimeUnit.SECONDS)) {
            try {
                File file = new File(SystemConfig.getHomePath(), "conf" + File.separator + "dnindex.properties");
                String path = KVPathUtil.getDnIndexNode();
                CuratorFramework zk = ZKUtils.getConnection();
                if (zk.checkExists().forPath(path) == null) {
                    zk.create().creatingParentsIfNeeded().forPath(path, Files.toByteArray(file));
                }
            } finally {
                dnIndexLock.release();
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 17
Project: DBus   File: EventContainer.java   Source Code and License 6 votes vote down vote up
/**
 * 在拉完全量后将此schema的kafka consumer的offset设置为最新
 * @param dbSchema
 */
/*public void setKafkaOffsetToLargest(String targetTopic){
	if(targetTopic==null)
		return;
	TopicPartition partition0 = new TopicPartition(targetTopic, 0);
	KafkaConsumerContainer.getInstances().getConsumer(targetTopic).seekToEnd(Arrays.asList(partition0));
}*/

protected <T> T deserialize(String path, Class<T> clazz) throws Exception {
    T packet = null;
    CuratorFramework curator = CuratorContainer.getInstance().getCurator();
    if (curator.getState() == CuratorFrameworkState.STOPPED) {
        LOG.info("[EventContainer] CuratorFrameworkState:{}", CuratorFrameworkState.STOPPED.name());
    } else {
        byte[] bytes = curator.getData().forPath(path);
        if (bytes != null && bytes.length != 0) {
            packet = JsonUtil.fromJson(new String(bytes, Charset.forName("UTF-8")),  clazz);
        }
    }
    return packet;
}
 
Example 18
Project: stroom-stats   File: ServiceDiscoveryManager.java   Source Code and License 6 votes vote down vote up
@Inject
    public ServiceDiscoveryManager(final @ServiceDiscoveryCuratorFramework CuratorFramework curatorFramework)
            throws Exception {

        LOGGER.info("ServiceDiscoveryManager starting...");
        this.curatorFrameworkRef.set(curatorFramework);


//        // First register this service
//        serviceDiscovery = ServiceDiscoveryBuilder
//                .builder(String.class)
//                .client(curatorFramework)
//                .basePath("/")
//                .thisInstance(getThisServiceInstance(config))
//                .build();
//        serviceDiscovery.start();

        // Then register services this service depends on
//        Arrays.stream(ExternalService.values()).forEach(externalService ->
//            serviceProviders.put(externalService, createProvider(externalService.get())));
    }
 
Example 19
Project: redirector   File: RedirectorCuratorFramework.java   Source Code and License 6 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
    if (newState == ConnectionState.CONNECTED) {
        isConnected.set(true);
        if (!isFirstConnection.get()) {
            for (ConnectionStateListener listener : listenerStateProxy.getListeners()) {
                listener.stateChanged(client, ConnectionState.RECONNECTED);
            }
        }
        return;
    }

    if (newState == ConnectionState.LOST) {
        isConnected.set(false);
        isFirstConnection.set(false);
        retryConnection();
    }
}
 
Example 20
Project: mycat-src-1.6.1-RELEASE   File: RuleDataPathChildrenCacheListener.java   Source Code and License 6 votes vote down vote up
@Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData data = event.getData();
    switch (event.getType()) {

        case CHILD_ADDED:

            add(data.getPath().substring(data.getPath().lastIndexOf("/")+1),event.getData().getData()) ;
            break;
        case CHILD_REMOVED:
            delete(data.getPath().substring(data.getPath().lastIndexOf("/")+1),event.getData().getData()); ;
            break;
        case CHILD_UPDATED:
            add(data.getPath().substring(data.getPath().lastIndexOf("/")+1),event.getData().getData()) ;
            break;
        default:
            break;
    }
}
 
Example 21
Project: mycat-src-1.6.1-RELEASE   File: EcacheszkToxmlLoader.java   Source Code and License 6 votes vote down vote up
public EcacheszkToxmlLoader(ZookeeperProcessListen zookeeperListen, CuratorFramework curator,
        XmlProcessBase xmlParseBase) {

    this.setCurator(curator);

    this.zookeeperListen = zookeeperListen;

    // 获得当前集群的名称
    String schemaPath = zookeeperListen.getBasePath();
    schemaPath = schemaPath + ZookeeperPath.ZK_SEPARATOR.getKey() + ZookeeperPath.FLOW_ZK_PATH_CACHE.getKey();

    currZkPath = schemaPath;
    // 将当前自己注册为事件接收对象
    this.zookeeperListen.addListen(schemaPath, this);

    // 生成xml与类的转换信息
    parseEcacheXMl = new EhcacheParseXmlImpl(xmlParseBase);
}
 
Example 22
Project: mycat-src-1.6.1-RELEASE   File: ServerzkToxmlLoader.java   Source Code and License 6 votes vote down vote up
public ServerzkToxmlLoader(ZookeeperProcessListen zookeeperListen, CuratorFramework curator,
        XmlProcessBase xmlParseBase) {

    this.setCurator(curator);

    this.zookeeperListen = zookeeperListen;

    // 获得当前集群的名称
    String serverPath = zookeeperListen.getBasePath();
    serverPath = serverPath + ZookeeperPath.ZK_SEPARATOR.getKey() + ZookeeperPath.FLOW_ZK_PATH_SERVER.getKey();

    currZkPath = serverPath;
    // 将当前自己注册为事件接收对象
    this.zookeeperListen.addListen(serverPath, this);

    // 生成xml与类的转换信息
    parseServerXMl = new ServerParseXmlImpl(xmlParseBase);
}
 
Example 23
Project: mycat-src-1.6.1-RELEASE   File: XmltoZkMain.java   Source Code and License 6 votes vote down vote up
private static CuratorFramework buildConnection(String url) {
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));

    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework.usingNamespace("");
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }

    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 24
Project: mycat-src-1.6.1-RELEASE   File: EcachesxmlTozkLoader.java   Source Code and License 6 votes vote down vote up
public EcachesxmlTozkLoader(ZookeeperProcessListen zookeeperListen, CuratorFramework curator,
        XmlProcessBase xmlParseBase) {

    this.setCurator(curator);

    // 获得当前集群的名称
    String schemaPath = zookeeperListen.getBasePath();
    schemaPath = schemaPath + ZookeeperPath.ZK_SEPARATOR.getKey() + ZookeeperPath.FLOW_ZK_PATH_CACHE.getKey();

    currZkPath = schemaPath;
    // 将当前自己注册为事件接收对象
    zookeeperListen.addListen(schemaPath, this);

    // 生成xml与类的转换信息
    parseEcacheXMl = new EhcacheParseXmlImpl(xmlParseBase);
}
 
Example 25
Project: mycat-src-1.6.1-RELEASE   File: ServerxmlTozkLoader.java   Source Code and License 6 votes vote down vote up
public ServerxmlTozkLoader(ZookeeperProcessListen zookeeperListen, CuratorFramework curator,
        XmlProcessBase xmlParseBase) {

    this.setCurator(curator);

    // 获得当前集群的名称
    String schemaPath = zookeeperListen.getBasePath();
    schemaPath = schemaPath + ZookeeperPath.ZK_SEPARATOR.getKey() + ZookeeperPath.FLOW_ZK_PATH_SERVER.getKey();

    currZkPath = schemaPath;
    // 将当前自己注册为事件接收对象
    zookeeperListen.addListen(schemaPath, this);

    // 生成xml与类的转换信息
    parseServerXMl = new ServerParseXmlImpl(xmlParseBase);
}
 
Example 26
Project: mycat-src-1.6.1-RELEASE   File: SchemasxmlTozkLoader.java   Source Code and License 6 votes vote down vote up
public SchemasxmlTozkLoader(ZookeeperProcessListen zookeeperListen, CuratorFramework curator,
        XmlProcessBase xmlParseBase) {

    this.setCurator(curator);

    // 获得当前集群的名称
    String schemaPath = zookeeperListen.getBasePath();
    schemaPath = schemaPath + ZookeeperPath.ZK_SEPARATOR.getKey() + ZookeeperPath.FOW_ZK_PATH_SCHEMA.getKey();

    currZkPath = schemaPath;
    // 将当前自己注册为事件接收对象
    zookeeperListen.addListen(schemaPath, this);

    // 生成xml与类的转换信息
    this.parseSchemaXmlService = new SchemasParseXmlImpl(xmlParseBase);
}
 
Example 27
Project: light-tram-4j   File: CdcServerStartupHookProvider.java   Source Code and License 5 votes vote down vote up
CuratorFramework makeStartedCuratorClient(String connectionString) {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(2000, 6, 2000);
    CuratorFramework client = CuratorFrameworkFactory.
            builder().connectString(connectionString)
            .retryPolicy(retryPolicy)
            .build();
    client.start();
    return client;
}
 
Example 28
Project: fastmq   File: LogManagerFactoryImpl.java   Source Code and License 5 votes vote down vote up
public LogManagerFactoryImpl(ClientConfiguration clientConfiguration, BookKeeperConfig config)
	throws Exception {
	bookKeeperConfig = config;
	checkNotNull(clientConfiguration);
	String servers = clientConfiguration.getZkServers();
	checkNotNull(servers);
	final CountDownLatch countDownLatch = new CountDownLatch(1);

	zooKeeper = new ZooKeeper(servers, clientConfiguration.getZkTimeout(), event -> {
		if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
			logger.info("Connected to zookeeper ,connectString = {}", servers);
			countDownLatch.countDown();
		} else {
			logger.error("Failed to connect zookeeper,connectString = {}", servers);
		}
	});
	if (!countDownLatch.await(clientConfiguration.getZkTimeout(), TimeUnit.MILLISECONDS)
		|| zooKeeper.getState() != ZooKeeper.States.CONNECTED) {
		throw new LedgerStorageException(
			"Error connecting to zookeeper server ,connectString = " + servers + ".");
	}

	this.bookKeeper = new BookKeeper(clientConfiguration, zooKeeper);
	RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
	CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(servers, retryPolicy);
	curatorFramework.start();
	asyncCuratorFramework = AsyncCuratorFramework.wrap(curatorFramework);
	logInfoStorage = new LogInfoStorageImpl(asyncCuratorFramework);
	offsetStorage = new ZkOffsetStorageImpl(logInfoStorage, asyncCuratorFramework);
}
 
Example 29
Project: zoomap   File: ZooMap.java   Source Code and License 5 votes vote down vote up
private static CuratorFramework newCuratorFrameworkClient(Builder builder, String connectionString) {
    return CuratorFrameworkFactory.builder()
            .connectString(connectionString)
            .retryPolicy(builder.retryPolicy)
            .connectionTimeoutMs((int) builder.duration.toMillis())
            .build();
}
 
Example 30
Project: Equella   File: ClusterMessagingServiceImpl.java   Source Code and License 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception
{
	Type type = event.getType();
	if( type.equals(Type.CHILD_ADDED) || type.equals(Type.CHILD_UPDATED) || type.equals(Type.CHILD_REMOVED) )
	{
		String remoteId = ZKPaths.getNodeFromPath(event.getData().getPath());
		String[] clientInfo = new String(event.getData().getData()).split(":");
		if( !isThisNode(remoteId) && !hasSameInfo(clientInfo) )
		{
			if( type.equals(Type.CHILD_ADDED) )
			{
				senders.get(remoteId);
				addReceiver(remoteId, clientInfo);
			}
			else if( type.equals(Type.CHILD_UPDATED) )
			{
				senders.get(remoteId);
				removeReceiver(remoteId);
				addReceiver(remoteId, clientInfo);
			}
			else
			{
				removeReceiver(remoteId);
			}
		}
	}
}
 
Example 31
Project: trellis-rosid   File: TrellisUtils.java   Source Code and License 5 votes vote down vote up
public static CuratorFramework getCuratorClient(final TrellisConfiguration config) {
    final CuratorFramework curator = newClient(config.getZookeeper().getEnsembleServers(),
            new BoundedExponentialBackoffRetry(config.getZookeeper().getRetryMs(),
                config.getZookeeper().getRetryMaxMs(), config.getZookeeper().getRetryMax()));
    curator.start();
    return curator;
}
 
Example 32
Project: redant   File: DefaultServiceDiscovery.java   Source Code and License 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData data = event.getData();
    if(data==null || data.getData()==null){
        return;
    }
    SlaveNode slaveNode = SlaveNode.parse(JSON.parseObject(data.getData(),JSONObject.class));
    if(slaveNode==null){
        LOGGER.error("get a null slaveNode with eventType={},path={},data={}",event.getType(),data.getPath(),data.getData());
    }else {
        switch (event.getType()) {
            case CHILD_ADDED:
                slaveNodeMap.put(slaveNode.getId(), slaveNode);
                LOGGER.info("CHILD_ADDED with path={},data={},current slaveNode size={}", data.getPath(), new String(data.getData(),CharsetUtil.UTF_8),slaveNodeMap.size());
                break;
            case CHILD_REMOVED:
                slaveNodeMap.remove(slaveNode.getId());
                LOGGER.info("CHILD_REMOVED with path={},data={},current slaveNode size={}", data.getPath(), new String(data.getData(),CharsetUtil.UTF_8),slaveNodeMap.size());
                break;
            case CHILD_UPDATED:
                slaveNodeMap.replace(slaveNode.getId(), slaveNode);
                LOGGER.info("CHILD_UPDATED with path={},data={},current slaveNode size={}", data.getPath(), new String(data.getData(),CharsetUtil.UTF_8),slaveNodeMap.size());
                break;
            default:
                break;
        }
    }
}
 
Example 33
Project: leaf-snowflake   File: Zookeeper.java   Source Code and License 5 votes vote down vote up
public void deletereRcursive(CuratorFramework zk , String path) throws Exception
{
	String npath = PathUtils.normalize_path(path);
	if (existsNode(zk,npath,false))
	{
		zk.delete().guaranteed().deletingChildrenIfNeeded().forPath(npath);
	}
}
 
Example 34
Project: monica   File: RouteHandler.java   Source Code and License 5 votes vote down vote up
private void rulesInit() {
	final CuratorFramework zkClient = (CuratorFramework) RegistryContext.clientCache.get("curatorClient");
	try {
		if (null != zkClient.checkExists().forPath(RULES_PATH)) {
			List<String> childList = zkClient.getChildren().forPath(RULES_PATH);
			for (String rule : childList) {
				StringToObjectParser stringParser = new StringToObjectParser();
				rulesList.add(stringParser.parseRuleStringToRule(URLDecoder.decode(rule, "UTF-8")));
			}
		}
		filterRulesForConsumer();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example 35
Project: redirector   File: RedirectorCuratorFramework.java   Source Code and License 5 votes vote down vote up
private synchronized CuratorFramework buildCuratorFramework(final ZKConfig config) {

        CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
                .connectString(config.getZooKeeperConnection())
                .connectionTimeoutMs(config.getZooKeeperConnectionTimeout())
                .sessionTimeoutMs(config.getZooKeeperSessionTimeout())
                .retryPolicy(new RetryNTimes(config.getZooKeeperRetryAttempts(), config.getZooKeeperRetryInterval()))
                .compressionProvider(new GzipCompressionProvider());

        CuratorFramework framework = builder.build();
        listenerStateProxy.updateCurator(framework);
        listenerProxy.updateCurator(framework);
        return framework;
    }
 
Example 36
Project: dble   File: RuleszkToxmlLoader.java   Source Code and License 5 votes vote down vote up
public RuleszkToxmlLoader(ZookeeperProcessListen zookeeperListen, CuratorFramework curator,
                          XmlProcessBase xmlParseBase, ConfigStatusListener confListener) {
    this.setCurator(curator);
    currZkPath = KVPathUtil.getConfRulePath();
    zookeeperListen.addToInit(this);
    parseRulesXMl = new RuleParseXmlImpl(xmlParseBase);
    confListener.addChild(this);
}
 
Example 37
Project: idworker   File: ZookeeperConnectionStateListener.java   Source Code and License 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
	switch (newState) {
	case LOST:
		logger.warn("zookeeper connection session lost, try to register new worker id.");
		doReconnecting();
		break;
	case SUSPENDED:
		logger.warn("zookeeper suspended, try to register new worker id.");
		doReconnecting();
		break;
	default:
		break;
	}
}
 
Example 38
Project: redirector   File: ZookeeperConnector.java   Source Code and License 5 votes vote down vote up
private CuratorFramework obtainClient() {
    lock.lock();
    try {
        if (client == null) {
            connect();
        }
        return client;
    } finally {
        lock.unlock();
    }
}
 
Example 39
Project: snowflake   File: SnowflakeServer.java   Source Code and License 5 votes vote down vote up
@Override
public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
	if (curatorFramework.getConnectionStateErrorPolicy().isErrorState(connectionState)) {
		reset();
		throw new CancelLeadershipException();
	}
}
 
Example 40
Project: trellis-rosid   File: NamespacesTest.java   Source Code and License 5 votes vote down vote up
@Test
public void testNamespaces() throws Exception {
    final URL res = Namespaces.class.getResource(nsDoc);
    final CuratorFramework zk = newClient(curator.getConnectString(), new RetryNTimes(10, 1000));
    zk.start();
    final TreeCache cache = new TreeCache(zk, ZNODE_NAMESPACES);
    cache.start();

    final NamespaceService svc1 = new Namespaces(zk, cache, res.getPath() + randomFilename());

    assertEquals(0, svc1.getNamespaces().size());

    final NamespaceService svc2 = new Namespaces(zk, cache, res.getPath());

    assertEquals(2, svc2.getNamespaces().size());
    assertEquals(LDP.URI, svc2.getNamespace("ldp").get());
    assertEquals("ldp", svc2.getPrefix(LDP.URI).get());

    assertFalse(svc2.getNamespace("jsonld").isPresent());
    assertFalse(svc2.getPrefix(JSONLD.URI).isPresent());
    assertTrue(svc2.setPrefix("jsonld", JSONLD.URI));
    assertEquals(3, svc2.getNamespaces().size());
    assertEquals(JSONLD.URI, svc2.getNamespace("jsonld").get());
    assertEquals("jsonld", svc2.getPrefix(JSONLD.URI).get());

    final Namespaces svc3 = new Namespaces(zk, cache);
    await().atMost(5, SECONDS).until(() -> 3 == svc3.getNamespaces().size());
    assertEquals(JSONLD.URI, svc3.getNamespace("jsonld").get());
    assertFalse(svc3.setPrefix("jsonld", JSONLD.URI));
}
 
Example 41
Project: redirector   File: CustomServiceCache.java   Source Code and License 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    try {
        super.childEvent(client, event); // throws Exception
    } catch (Exception e) {
        log.error("Issue receiving ZkEventType={} for zkStackPath={}", event.getType(), stackPath, e);
    } finally {
        if (EVENTS_TO_LISTEN.contains(event.getType())) {
            switch (event.getType()) {
                case CHILD_REMOVED:
                    eventFromClient(event.getData().getPath(), getInstances(), Action.UNREGISTERED);
                    break;
                case INITIALIZED:
                    List<String> paths = new ArrayList<>();
                    List<ChildData> childData = getCache().getCurrentData();
                    if (CollectionUtils.isNotEmpty(childData)) {
                        for (ChildData data : childData) {
                            paths.add(data.getPath());
                        }
                    }
                    log.info("Dynamic Service Discovery cache for {} is initialized. Stacks.json will be written of FS", stackPath);
                    eventFromClient(paths, getInstances(), Action.INITIALIZED);
                    break;
                default:
                    eventFromClient(event.getData().getPath(), getInstances(), Action.REGISTERED);
            }
        }
    }
}
 
Example 42
Project: redirector   File: DataStoreSupport.java   Source Code and License 5 votes vote down vote up
private CuratorFramework createCurator(ZKConfig config) {
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
        .connectString(config.getZooKeeperConnection())
        .connectionTimeoutMs(config.getZooKeeperConnectionTimeout())
        .sessionTimeoutMs(config.getZooKeeperSessionTimeout())
        .retryPolicy(new RetryNTimes(config.getZooKeeperRetryAttempts(), config.getZooKeeperRetryInterval()));

    builder.compressionProvider(new GzipCompressionProvider());
    return builder.build();
}
 
Example 43
Project: hadoop   File: TestChildReaper.java   Source Code and License 5 votes vote down vote up
@Test
public void     testNamespace() throws Exception
{
  Timing                  timing = new Timing();
  ChildReaper             reaper = null;
  CuratorFramework        client = CuratorFrameworkFactory.builder()
      .connectString(server.getConnectString())
      .sessionTimeoutMs(timing.session())
      .connectionTimeoutMs(timing.connection())
      .retryPolicy(new RetryOneTime(1))
      .namespace("foo")
      .build();
  try
  {
    client.start();

    for ( int i = 0; i < 10; ++i )
    {
      client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
    }

    reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
    reaper.start();

    timing.forWaiting().sleepABit();

    Stat    stat = client.checkExists().forPath("/test");
    Assert.assertEquals(stat.getNumChildren(), 0);

    stat = client.usingNamespace(null).checkExists().forPath("/foo/test");
    Assert.assertNotNull(stat);
    Assert.assertEquals(stat.getNumChildren(), 0);
  }
  finally
  {
    CloseableUtils.closeQuietly(reaper);
    CloseableUtils.closeQuietly(client);
  }
}
 
Example 44
Project: jigsaw-payment   File: RefreshableTransportPool.java   Source Code and License 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event)
        throws Exception {
    PathChildrenCacheEvent.Type eventType = event.getType();
    switch (eventType) {
    case INITIALIZED:
        LOG.debug("initailize the service instance list from zookeeper.");
        break;
    case CHILD_ADDED:
        LOG.debug("add new service instance from zookeeper.");
        this.addChild(event.getData());
        break;
    case CHILD_UPDATED:
        LOG.debug("update service instance  from zookeeper.");
        this.addChild(event.getData());
        break;
    case CONNECTION_RECONNECTED:
        this.cache.rebuild();
        break;
    case CHILD_REMOVED:
    case CONNECTION_SUSPENDED:
    case CONNECTION_LOST:
        LOG.debug("remove service instance  from zookeeper.");
        this.removeChild(event.getData());
        break;
    default:
        LOG.debug("Ignore PathChildrenCache event : {path:"
                + event.getData().getPath() + " data:"
                + new String(event.getData().getData()) + "}");
    }
}
 
Example 45
Project: DBus   File: AbstractEvent.java   Source Code and License 5 votes vote down vote up
protected <T> T deserialize(String path, Class<T> clazz) throws Exception {
    T packet = null;
    CuratorFramework curator = CuratorContainer.getInstance().getCurator();
    if (curator.getState() == CuratorFrameworkState.STOPPED) {
        LOG.info("[control-event] CuratorFrameworkState:{}", CuratorFrameworkState.STOPPED.name());
    } else {
        byte[] bytes = curator.getData().forPath(path);
        if (bytes != null && bytes.length != 0) {
            packet = JsonUtil.fromJson(new String(bytes, Charset.forName("UTF-8")),  clazz);
        }
    }
    return packet;
}
 
Example 46
Project: DBus   File: DeleteFullPullOldVersionEvent.java   Source Code and License 5 votes vote down vote up
private void getChildrenPath(CuratorFramework curator, List<String> lst, String parent) throws Exception{
  	List<String> lstPath = curator.getChildren().forPath(parent);
for(String path:lstPath){
	getChildrenPath(curator,lst,parent+"/"+path);
}
if(lstPath.size()==0)
	lst.add(parent);
  }
 
Example 47
Project: DBus   File: CheckFullPullEvent.java   Source Code and License 5 votes vote down vote up
private void fetchZkNodeRecursively(String parentNode, List<String> resultNodesInfo, CuratorFramework curator) throws Exception {
    List<String> children = curator.getChildren().forPath(parentNode);
    if (!children.isEmpty()) {
        for (String curNode : children) {
            fetchZkNodeRecursively(parentNode + "/" + curNode, resultNodesInfo, curator);
        }
    } else {
        String ver = StringUtils.substringAfterLast(parentNode, "/");
        if(isStr2Num(ver)) {
            resultNodesInfo.add(parentNode);
        }
    }
}
 
Example 48
Project: DBus   File: CheckFullPullEvent.java   Source Code and License 5 votes vote down vote up
/**
 * 获取全量拉取的最新节点
 * @param fullPullerRootNode
 * @param curator
 * @return 子节点的全路径,之前非全路径,后面的path部分进行了拼接
 * @throws Exception
 */
private List<String> filter(String fullPullerRootNode, CuratorFramework curator) throws Exception {
    List<String> flattedFullpullerNodeName = new ArrayList<>();

    //get all node list
    fetchZkNodeRecursively(fullPullerRootNode,flattedFullpullerNodeName, curator);

    HashMap<String, Integer> map = new HashMap<String, Integer>();
    for (String znode : flattedFullpullerNodeName) {
        String key = StringUtils.substringBeforeLast(znode, "/");
        String ver = StringUtils.substringAfterLast(znode, "/");
        Integer version = Integer.parseInt(ver);
        if (map.containsKey(key)) {
            Integer tempVersion = map.get(key);
            if (version > tempVersion) {
                map.put(key, version);
            }
        } else {
            map.put(key, version);
        }
    }

    List<String> wkList = new ArrayList<String>();
    for (Map.Entry<String, Integer> entry : map.entrySet()) {
        wkList.add(StringUtils.join(new String[] {entry.getKey(), String.valueOf(entry.getValue())}, "/"));
    }
    return wkList;
}
 
Example 49
Project: iotplatform   File: ZkDiscoveryService.java   Source Code and License 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent)
    throws Exception {
  ChildData data = pathChildrenCacheEvent.getData();
  if (data == null) {
    log.debug("Ignoring {} due to empty child data", pathChildrenCacheEvent);
    return;
  } else if (data.getData() == null) {
    log.debug("Ignoring {} due to empty child's data", pathChildrenCacheEvent);
    return;
  } else if (nodePath != null && nodePath.equals(data.getPath())) {
    log.debug("Ignoring event about current server {}", pathChildrenCacheEvent);
    return;
  }
  ServerInstance instance;
  try {
    instance = new ServerInstance(ServerInfo.parseFrom(data.getData()));
  } catch (IOException e) {
    log.error("Failed to decode server instance for node {}", data.getPath(), e);
    throw e;
  }
  log.info("Processing [{}] event for [{}:{}]", pathChildrenCacheEvent.getType(), instance.getHost(),
      instance.getPort());
  switch (pathChildrenCacheEvent.getType()) {
  case CHILD_ADDED:
    listeners.forEach(listener -> listener.onServerAdded(instance));
    break;
  case CHILD_UPDATED:
    listeners.forEach(listener -> listener.onServerUpdated(instance));
    break;
  case CHILD_REMOVED:
    listeners.forEach(listener -> listener.onServerRemoved(instance));
    break;
  }
}
 
Example 50
Project: elastic-job-cloud   File: ZookeeperRegistryCenterForAuthTest.java   Source Code and License 5 votes vote down vote up
@Test
public void assertInitWithDigestSuccess() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.builder()
        .connectString(EmbedTestingServer.getConnectionString())
        .retryPolicy(new RetryOneTime(2000))
        .authorization("digest", "digest:password".getBytes()).build();
    client.start();
    client.blockUntilConnected();
    assertThat(client.getData().forPath("/" + ZookeeperRegistryCenterForAuthTest.class.getName() + "/test/deep/nested"), is("deepNested".getBytes()));
}
 
Example 51
Project: stroom-stats   File: StroomPropertyServiceImpl.java   Source Code and License 5 votes vote down vote up
private void treeCacheChangeHandler(CuratorFramework curatorFramework, TreeCacheEvent treeCacheEvent) {
    switch (treeCacheEvent.getType()) {
        case INITIALIZED: {
            //cache is ready so release a permit allowing the application to continue initialising
            initialisedSemaphore.release();
            break;
        }
        case NODE_ADDED: {
            if (!treeCacheEvent.getData().getPath().equals(pathToCache)) {
                LOGGER.info("Curator TreeCache property added: " + childDataToLogString(treeCacheEvent.getData()));
            } else {
                LOGGER.info("Curator TreeCache path added: " + treeCacheEvent.getData().getPath());
            }
            break;
        }
        case NODE_REMOVED: {
            LOGGER.info("Curator TreeCache property removed: " + childDataToLogString(treeCacheEvent.getData()));
            break;
        }
        case NODE_UPDATED: {
            LOGGER.info("Curator TreeCache property updated: " + childDataToLogString(treeCacheEvent.getData()));
            break;
        }
        case CONNECTION_LOST: {
            LOGGER.debug("Connection to Zookeeper lost");
            break;
        }
        case CONNECTION_RECONNECTED: {
            LOGGER.debug("Connection to Zookeeper re-established");
            break;
        }
        case CONNECTION_SUSPENDED: {
            LOGGER.debug("Connection to Zookeeper suspended");
            break;
        }
    }
}
 
Example 52
Project: redirector   File: ListenerProxy.java   Source Code and License 5 votes vote down vote up
public void updateCurator(CuratorFramework curator) {
    this.curator = curator;
    for (CuratorListener curatorListener : listeners) {
        if (this.curator != null) {
            this.curator.getCuratorListenable().addListener(curatorListener);
        }
    }
}
 
Example 53
Project: stroom-stats   File: StroomStatsEmbeddedOverrideModule.java   Source Code and License 5 votes vote down vote up
@Override
protected void configure() {
    //singletons
    bind(UniqueIdCache.class).to(MockUniqueIdCache.class).asEagerSingleton();

    //TODO For the moment, just a mock that does nothing. In furture will either need
    //to capture calls made to it for asserts or be replaced with the HBaseTestingUtility
    //if that can be made to work with the hbase-shaded-client
    bind(EventStoreTableFactory.class).toInstance(mockEventStoreTableFactory);


    //TODO need to replace this with a proper mock, or expose this mock somehow to allow capturing of input
    //to it.  Think the above TabFac binding is not needed as it will never get taht far
    bind(StatisticsService.class).toInstance(mockStatisticsService);


    //Don't need curator framework as we are using a mocked StroomPropertyService
    bind(CuratorFramework.class).toInstance(mockCuratorFramework);

    bind(StroomPropertyService.class).toInstance(mockStroomPropertyService);

    bind(StatisticConfigurationService.class).toInstance(mockStatisticConfigurationService);

    //don't want an actual hbase so just mock the classes
    bindToMock(HBaseConnection.class);
    bindToMock(HBaseEventStoreTableFactory.class);
    bindToMock(UniqueIdGenerator.class);
}
 
Example 54
Project: mumu-zookeeper   File: CuratorCreate.java   Source Code and License 5 votes vote down vote up
/**
 * 创建节点
 * @return
 */
public String create(String path,byte[] bs){
    CuratorFramework curatorFramework = new CuratorConnect().connect();
    try {
        String result = curatorFramework.
                create().
                creatingParentsIfNeeded().
                withMode(CreateMode.PERSISTENT_SEQUENTIAL).
                forPath(path,bs);
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
 
Example 55
Project: hadoop   File: TestChildReaper.java   Source Code and License 5 votes vote down vote up
@Test
public void     testSimple() throws Exception
{
  Timing                  timing = new Timing();
  ChildReaper             reaper = null;
  CuratorFramework        client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
  try
  {
    client.start();

    for ( int i = 0; i < 10; ++i )
    {
      client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i));
    }

    reaper = new ChildReaper(client, "/test", Reaper.Mode.REAP_UNTIL_DELETE, 1);
    reaper.start();

    timing.forWaiting().sleepABit();

    Stat    stat = client.checkExists().forPath("/test");
    Assert.assertEquals(stat.getNumChildren(), 0);
  }
  finally
  {
    CloseableUtils.closeQuietly(reaper);
    CloseableUtils.closeQuietly(client);
  }
}
 
Example 56
Project: mumu-zookeeper   File: CuratorConnect.java   Source Code and License 5 votes vote down vote up
/**
 * 获取curator对象
 * @return
 */
public CuratorFramework connect(){
    //声明一个重试策略
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient(CuratorConfiguration.address, retryPolicy);
    client.start();
    return client;
}
 
Example 57
Project: centraldogma   File: ZooKeeperCommandExecutor.java   Source Code and License 5 votes vote down vote up
public ZooKeeperCommandExecutor build() {
    requireNonNull(replicaId, "replicaId");
    requireNonNull(delegate, "delegate");
    requireNonNull(connectionString, "connectionString");
    requireNonNull(path, "path");
    requireNonNull(revisionFile, "revisionFile");

    final CuratorFramework curator = CuratorFrameworkFactory.newClient(
            connectionString, new ExponentialBackoffRetry(timeoutMillis, 3));

    return new ZooKeeperCommandExecutor(
            replicaId, delegate, curator, path, createPathIfNotExist,
            revisionFile, numWorkers, maxLogCount, minLogAgeMillis);
}
 
Example 58
Project: hadoop   File: DelegationTokenAuthenticationFilter.java   Source Code and License 5 votes vote down vote up
@Override
protected void initializeAuthHandler(String authHandlerClassName,
    FilterConfig filterConfig) throws ServletException {
  // A single CuratorFramework should be used for a ZK cluster.
  // If the ZKSignerSecretProvider has already created it, it has to
  // be set here... to be used by the ZKDelegationTokenSecretManager
  ZKDelegationTokenSecretManager.setCurator((CuratorFramework)
      filterConfig.getServletContext().getAttribute(ZKSignerSecretProvider.
          ZOOKEEPER_SIGNER_SECRET_PROVIDER_CURATOR_CLIENT_ATTRIBUTE));
  super.initializeAuthHandler(authHandlerClassName, filterConfig);
  ZKDelegationTokenSecretManager.setCurator(null);
}
 
Example 59
Project: hadoop   File: ChildReaper.java   Source Code and License 5 votes vote down vote up
/**
 * @param client the client
 * @param path path to reap children from
 * @param executor executor to use for background tasks
 * @param reapingThresholdMs threshold in milliseconds that determines that a path can be deleted
 * @param mode reaping mode
 * @param leaderPath if not null, uses a leader selection so that only 1 reaper is active in the cluster
 */
public ChildReaper(CuratorFramework client, String path, Reaper.Mode mode, ScheduledExecutorService executor, int reapingThresholdMs, String leaderPath)
{
  this.client = client;
  this.mode = mode;
  this.executor = new CloseableScheduledExecutorService(executor);
  this.reapingThresholdMs = reapingThresholdMs;
  this.reaper = new Reaper(client, executor, reapingThresholdMs, leaderPath);
  addPath(path);
}
 
Example 60
Project: redirector   File: ZookeeperConnector.java   Source Code and License 5 votes vote down vote up
public ZookeeperConnector(CuratorFramework client, String basePath, boolean cacheHosts,
                          Supplier<IStacksCache> stacksCacheFactory, Function<IServiceDiscovery, Boolean> stacksRefreshListener) {
    this();
    if (client == null) {
        throw new IllegalStateException("Curator and config should not be null");
    }
    this.client = client;
    this.basePath = basePath;
    this.cacheHosts = cacheHosts;
    this.stacksCacheFactory = stacksCacheFactory;

    init(stacksRefreshListener);
}