public static void main(String[] args) { TestingServer server = null; CuratorFramework client = null; CuratorFramework optionsClient = null; try { server = new TestingServer(); client = createSimple(server.getConnectString()); client.start(); client.create().creatingParentsIfNeeded().forPath(PATH, "test".getBytes()); optionsClient = createWithOptions( server.getConnectString(), new ExponentialBackoffRetry(1000, 3), 1000, 1000); optionsClient.start(); log.info("[{}]", new String(optionsClient.getData().forPath(PATH))); } catch (Exception e) { log.info("exception throws cause {}", Throwables.getStackTraceAsString(e)); } finally { if (client != null) { CloseableUtils.closeQuietly(client); } if (optionsClient != null) { CloseableUtils.closeQuietly(optionsClient); } } }
@Test public void testMultiClient() throws Exception { CuratorFramework client1 = null; CuratorFramework client2 = null; try { { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); DistributedBarrier barrier = new DistributedBarrier(client, "/barrier"); barrier.setBarrier(); } finally { CloseableUtils.closeQuietly(client); } } client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); List<Future<Object>> futures = Lists.newArrayList(); ExecutorService service = Executors.newCachedThreadPool(); for (final CuratorFramework c : new CuratorFramework[] {client1, client2}) { Future<Object> future = service.submit( new Callable<Object>() { @Override public Object call() throws Exception { c.start(); DistributedBarrier barrier = new DistributedBarrier(c, "/barrier"); barrier.waitOnBarrier(10, TimeUnit.MILLISECONDS); return null; } }); futures.add(future); } Thread.sleep(1000); { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); DistributedBarrier barrier = new DistributedBarrier(client, "/barrier"); barrier.removeBarrier(); } finally { CloseableUtils.closeQuietly(client); } } for (Future<Object> f : futures) { f.get(); } } finally { CloseableUtils.closeQuietly(client1); CloseableUtils.closeQuietly(client2); } }
public static void main(String[] args) throws Exception { CuratorFramework client = null; DistributedDelayQueue<String> queue = null; try { client = CuratorClientFactory.newClient(); client .getCuratorListenable() .addListener( new CuratorListener() { @Override public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { System.out.println("CuratorEvent: " + event.getType().name()); } }); client.start(); QueueConsumer<String> consumer = createQueueConsumer(); QueueBuilder<String> builder = QueueBuilder.builder(client, consumer, createQueueSerializer(), PATH); queue = builder.buildDelayQueue(); queue.start(); for (int i = 0; i < 10; i++) { queue.put("test-" + i, System.currentTimeMillis() + 10000); } System.out.println(getCurrentTimeStr() + ": 所有的数据已经生产完毕"); Thread.sleep(20000); } finally { CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(queue); } }
@Bean(destroyMethod = "close") @ConditionalOnMissingBean @SneakyThrows public CuratorFramework curatorFramework( RetryPolicy retryPolicy, ZookeeperDiscoveryProperties properties) { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); if (ensembleProvider != null) { builder.ensembleProvider(ensembleProvider); } CuratorFramework curator = builder .retryPolicy(retryPolicy) .connectString(zookeeperProperties.getConnectString()) .build(); curator.start(); log.trace( "blocking until connected to zookeeper for " + properties.getBlockUntilConnectedWait() + properties.getBlockUntilConnectedUnit()); curator.blockUntilConnected( properties.getBlockUntilConnectedWait(), properties.getBlockUntilConnectedUnit()); log.trace("connected to zookeeper"); return curator; }
/** * connect ZK, register Watch/unhandle Watch * * @return */ public CuratorFramework mkClient( Map conf, List<String> servers, Object port, String root, final WatcherCallBack watcher) { CuratorFramework fk = Utils.newCurator(conf, servers, port, root); fk.getCuratorListenable() .addListener( new CuratorListener() { @Override public void eventReceived(CuratorFramework _fk, CuratorEvent e) throws Exception { if (e.getType().equals(CuratorEventType.WATCHED)) { WatchedEvent event = e.getWatchedEvent(); watcher.execute(event.getState(), event.getType(), event.getPath()); } } }); fk.getUnhandledErrorListenable() .addListener( new UnhandledErrorListener() { @Override public void unhandledError(String msg, Throwable error) { String errmsg = "Unrecoverable Zookeeper error, halting process: " + msg; LOG.error(errmsg, error); JStormUtils.halt_process(1, "Unrecoverable Zookeeper error"); } }); fk.start(); return fk; }
/** * 连接 * * @return */ public boolean connect() { try { client .getConnectionStateListenable() .addListener( new ConnectionStateListener() { public void stateChanged(CuratorFramework framework, ConnectionState state) { logger.info("Zookeeper状态:" + state.name()); if (state.equals(ConnectionState.RECONNECTED)) { logger.info("Zookeeper状态:检查节点"); for (String key : backup.keySet()) { String value = backup.get(key); createNode(key, value); } } } }); client.start(); client.getZookeeperClient().blockUntilConnectedOrTimedOut(); createRoomProcessorNode(); return true; } catch (Exception e) { logger.error("ZooKeeperWrapper.connect出现异常", e); return false; } }
public ZKNamedMutexFactory( String connectString, int connectionTimeout, int sessionTimeout, String namespace) throws GlobalMutexAcquireException { this.connectString = connectString; this.namespace = namespace; try { curatorClient = CuratorFrameworkFactory.builder() .connectString(connectString) .retryPolicy(retryPolicy) .connectionTimeoutMs(connectionTimeout) .sessionTimeoutMs(sessionTimeout) .namespace(namespace) .build(); curatorClient.start(); if (curatorClient.checkExists().forPath(BASEDIR) == null) { curatorClient.create().forPath(BASEDIR); } } catch (Exception e) { throw new GlobalMutexAcquireException("Unable to : ", e); } }
public ZookeeperOffsetHandler(Properties props) { this.groupId = props.getProperty(ConsumerConfig.GROUP_ID_CONFIG); if (this.groupId == null) { throw new IllegalArgumentException( "Required property '" + ConsumerConfig.GROUP_ID_CONFIG + "' has not been set"); } String zkConnect = props.getProperty("zookeeper.connect"); if (zkConnect == null) { throw new IllegalArgumentException("Required property 'zookeeper.connect' has not been set"); } // we use Curator's default timeouts int sessionTimeoutMs = Integer.valueOf(props.getProperty("zookeeper.session.timeout.ms", "60000")); int connectionTimeoutMs = Integer.valueOf(props.getProperty("zookeeper.connection.timeout.ms", "15000")); // undocumented config options allowing users to configure the retry policy. (they are "flink." // prefixed as they are no official kafka configs) int backoffBaseSleepTime = Integer.valueOf(props.getProperty("flink.zookeeper.base-sleep-time.ms", "100")); int backoffMaxRetries = Integer.valueOf(props.getProperty("flink.zookeeper.max-retries", "10")); RetryPolicy retryPolicy = new ExponentialBackoffRetry(backoffBaseSleepTime, backoffMaxRetries); curatorClient = CuratorFrameworkFactory.newClient( zkConnect, sessionTimeoutMs, connectionTimeoutMs, retryPolicy); curatorClient.start(); }
public RunningJobManager( String zkQuorum, int zkSessionTimeoutMs, int zkRetryTimes, int zkRetryInterval, String zkRoot, String lockPath) { this.zkRoot = zkRoot; curator = newCurator(zkQuorum, zkSessionTimeoutMs, zkRetryTimes, zkRetryInterval); try { curator.start(); } catch (Exception e) { LOG.error("curator start error {}", e); } LOG.info("InterProcessMutex lock path is " + lockPath); lock = new InterProcessMutex(curator, lockPath); try { if (curator.checkExists().forPath(this.zkRoot) == null) { curator .create() .creatingParentsIfNeeded() .withMode(CreateMode.PERSISTENT) .forPath(this.zkRoot); } } catch (Exception e) { LOG.warn("{}", e); } }
@Test public void testNoBarrier() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); final DistributedBarrier barrier = new DistributedBarrier(client, "/barrier"); Assert.assertTrue(barrier.waitOnBarrier(10, TimeUnit.SECONDS)); // just for grins, test the infinite wait ExecutorService service = Executors.newSingleThreadExecutor(); Future<Object> future = service.submit( new Callable<Object>() { @Override public Object call() throws Exception { barrier.waitOnBarrier(); return ""; } }); Assert.assertTrue(future.get(10, TimeUnit.SECONDS) != null); } finally { client.close(); } }
@Test public void test() throws Exception { Timing timing = new Timing(); LeaderSelector leaderSelector = null; CuratorFramework client = CuratorFrameworkFactory.builder() .retryPolicy(new ExponentialBackoffRetry(100, 3)) .connectString(server.getConnectString()) .sessionTimeoutMs(timing.session()) .connectionTimeoutMs(timing.connection()) .build(); try { client.start(); MyLeaderSelectorListener listener = new MyLeaderSelectorListener(); ExecutorService executorPool = Executors.newFixedThreadPool(20); leaderSelector = new LeaderSelector(client, "/test", threadFactory, executorPool, listener); leaderSelector.autoRequeue(); leaderSelector.start(); timing.sleepABit(); Assert.assertEquals(listener.getLeaderCount(), 1); } finally { CloseableUtils.closeQuietly(leaderSelector); CloseableUtils.closeQuietly(client); } }
public static void main(String[] args) throws Exception { // RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); // RetryPolicy retryPolicy = new RetryNTimes(5, 1000); RetryPolicy retryPolicy = new RetryUntilElapsed(5000, 1000); // CuratorFramework client = CuratorFrameworkFactory // .newClient("192.168.1.105:2181",5000,5000, retryPolicy); CuratorFramework client = CuratorFrameworkFactory.builder() .connectString("192.168.1.105:2181") .sessionTimeoutMs(5000) .connectionTimeoutMs(5000) .retryPolicy(retryPolicy) .build(); client.start(); String path = client .create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL) .forPath("/jike/1", "123".getBytes()); System.out.println(path); Thread.sleep(Integer.MAX_VALUE); }
@Test public void testBasic() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); final DistributedBarrier barrier = new DistributedBarrier(client, "/barrier"); barrier.setBarrier(); ExecutorService service = Executors.newSingleThreadExecutor(); service.submit( new Callable<Object>() { @Override public Object call() throws Exception { Thread.sleep(1000); barrier.removeBarrier(); return null; } }); Assert.assertTrue(barrier.waitOnBarrier(10, TimeUnit.SECONDS)); } finally { client.close(); } }
@Before public void setUp() throws Exception { sfb = new ZKServerFactoryBean(); delete(sfb.getDataDir()); delete(sfb.getDataLogDir()); sfb.afterPropertiesSet(); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString("localhost:" + sfb.getClientPortAddress().getPort()) .retryPolicy(new RetryOneTime(1000)) .connectionTimeoutMs(360000); curator = builder.build(); curator.start(); curator.getZookeeperClient().blockUntilConnectedOrTimedOut(); // setup a local and remote git repo basedir = System.getProperty("basedir", "."); File root = new File(basedir + "/target/git").getCanonicalFile(); delete(root); new File(root, "remote").mkdirs(); remote = Git.init().setDirectory(new File(root, "remote")).call(); remote.commit().setMessage("First Commit").setCommitter("fabric", "user@fabric").call(); String remoteUrl = "file://" + new File(root, "remote").getCanonicalPath(); new File(root, "local").mkdirs(); git = Git.init().setDirectory(new File(root, "local")).call(); git.commit().setMessage("First Commit").setCommitter("fabric", "user@fabric").call(); StoredConfig config = git.getRepository().getConfig(); config.setString("remote", "origin", "url", remoteUrl); config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*"); config.save(); DefaultRuntimeProperties sysprops = new DefaultRuntimeProperties(); sysprops.setProperty(SystemProperties.KARAF_DATA, "target/data"); FabricGitServiceImpl gitService = new FabricGitServiceImpl(); gitService.bindRuntimeProperties(sysprops); gitService.activate(); gitService.setGitForTesting(git); DataStoreTemplateRegistry registrationHandler = new DataStoreTemplateRegistry(); registrationHandler.activateComponent(); dataStore = new CachingGitDataStore(); dataStore.bindCurator(curator); dataStore.bindGitService(gitService); dataStore.bindRegistrationHandler(registrationHandler); dataStore.bindRuntimeProperties(sysprops); dataStore.bindConfigurer( new Configurer() { @Override public <T> void configure(Map<String, ?> configuration, T target) throws Exception {} }); Map<String, String> datastoreProperties = new HashMap<String, String>(); datastoreProperties.put(GitDataStore.GIT_REMOTE_URL, remoteUrl); dataStore.activate(datastoreProperties); }
/** Only for the 0.8 server we need access to the zk client. */ public CuratorFramework createCuratorClient() { RetryPolicy retryPolicy = new ExponentialBackoffRetry(100, 10); CuratorFramework curatorClient = CuratorFrameworkFactory.newClient( standardProps.getProperty("zookeeper.connect"), retryPolicy); curatorClient.start(); return curatorClient; }
public static void main(String[] args) { /* * 在curator中,CuratorFramework作为zookeeper的client接口 */ String connectString = ZKCommons.ZK_SERVER_IP + ":" + ZKCommons.ZK_SERVER_PORT; RetryPolicy retryPolicy = new ExponentialBackoffRetry(5000, 3); // CuratorFrameworkFactory.newClient(connectString, 5000, 5000, retryPolicy); final CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(connectString) .sessionTimeoutMs(5000) .connectionTimeoutMs(5000) .retryPolicy(retryPolicy) .build(); client.start(); try { AfterConnectionEstablished.execute( client, new Runnable() { @Override public void run() { try { Stat stat = new Stat(); byte[] data = client.getData().storingStatIn(stat).forPath("/"); System.out.println(new String(data)); System.out.println(stat); } catch (Exception e) { e.printStackTrace(); } System.out.println("do something after get connect"); } }); } catch (Exception e1) { e1.printStackTrace(); } try { States stat = client.getZookeeperClient().getZooKeeper().getState(); // CuratorFrameworkState stat = client.getState(); System.out.println(stat); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try { Thread.sleep(Integer.MAX_VALUE); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override public void afterPropertiesSet() { // 1000ms 是重试间隔时间基数,3 是重试次数 RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); // 创建客户端 zkClient = createWithOptions(zkConnectionString, retryPolicy, 2000, 10000); // 向zk注册观察者 registerListeners(zkClient); zkClient.start(); }
public ZkState(Map stateConf) { stateConf = new HashMap(stateConf); try { _curator = newCurator(stateConf); _curator.start(); } catch (Exception e) { throw new RuntimeException(e); } }
public static void main(String[] args) throws Exception { TestingServer server = new TestingServer(); CuratorFramework client = null; DistributedQueue<String> queue = null; try { client = CuratorFrameworkFactory.newClient( server.getConnectString(), new ExponentialBackoffRetry(1000, 3)); client .getCuratorListenable() .addListener( new CuratorListener() { @Override public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception { System.out.println("CuratorEvent: " + event.getType().name()); } }); client.start(); AkinDistributedBlockingQueue<String> consumerQueue = new AkinDistributedBlockingQueue<String>( null, new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) {} }); QueueBuilder<String> builder = QueueBuilder.builder(client, consumerQueue, createQueueSerializer(), PATH); queue = builder.buildQueue(); consumerQueue.setDistributedQueue(queue); queue.start(); for (int i = 0; i < 10; i++) { queue.put(" test-" + i); Thread.sleep((long) (3 * Math.random())); } Thread.sleep(20000); for (Object object : consumerQueue) { System.out.println(consumerQueue.poll()); } } catch (Exception ex) { } finally { CloseableUtils.closeQuietly(queue); CloseableUtils.closeQuietly(client); CloseableUtils.closeQuietly(server); } }
private CuratorFramework connectToZookeeper(String zookeeperInstance) { CuratorFramework zkClient = CuratorFrameworkFactory.builder() .connectString(zookeeperInstance) .namespace(namespace) .retryPolicy( new BoundedExponentialBackoffRetry( BASE_SLEEP_TIME_MS, MAX_SLEEP_TIME_MS, MAX_RETRIES)) .build(); zkClient.start(); return zkClient; }
public static void main(String[] args) throws Exception { List<LeaderLatch> leaders = new ArrayList<LeaderLatch>(); List<CuratorFramework> clients = new ArrayList<CuratorFramework>(); TestingServer server = new TestingServer(); try { for (int i = 0; i < 10; i++) { CuratorFramework client = CuratorFrameworkFactory.newClient( "192.168.50.202:2181,192.168.50.203:2181,192.168.50.204:2181", new ExponentialBackoffRetry(20000, 3)); clients.add(client); LeaderLatch leader = new LeaderLatch(client, "/francis/leader"); leader.addListener( new LeaderLatchListener() { @Override public void isLeader() { // TODO Auto-generated method stub System.out.println("I am Leader"); } @Override public void notLeader() { // TODO Auto-generated method stub System.out.println("I am not Leader"); } }); leaders.add(leader); client.start(); leader.start(); } Thread.sleep(Integer.MAX_VALUE); } finally { for (CuratorFramework client : clients) { CloseableUtils.closeQuietly(client); } for (LeaderLatch leader : leaders) { CloseableUtils.closeQuietly(leader); } // CloseableUtils.closeQuietly(server); } Thread.sleep(Integer.MAX_VALUE); }
public static void main(String[] args) throws Exception { client.start(); String result = client .create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL) .forPath(path, "init".getBytes()); // Path /zk-book/c1 is returned System.out.println(result); }
@Override public void start() { if (serverState == ServerState.STARTING || serverState == ServerState.STARTED) { LOG.warn("Already at work!"); return; } serverState = ServerState.STARTING; client.start(); leaderSelector.start(); serverState = ServerState.STARTED; }
public ServiceConsumer() { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); client = CuratorFrameworkFactory.builder() .connectString(connectString) .sessionTimeoutMs(10000) .retryPolicy(retryPolicy) .namespace("webServiceCenter") .build(); client.start(); // try { // List<String> children = client.getChildren().forPath("/"); // System.out.println("-----"+children.size()); // } catch (Exception e) { // e.printStackTrace(); // } }
public static void main(String[] args) throws Exception { CuratorFramework client = null; PathChildrenCache cache = null; try { client = CuratorFrameworkFactory.newClient("localhost:2181", new ExponentialBackoffRetry(1000, 3)); client.start(); // in this example we will cache data. Notice that this is optional. cache = new PathChildrenCache(client, PATH, true); cache.start(); processCommands(client, cache); } finally { Closeables.closeQuietly(cache); Closeables.closeQuietly(client); } }
/** Start the registration of the {@link #candidate} for leader election. */ @Override public synchronized void start() { if (!running) { if (client.getState() != CuratorFrameworkState.STARTED) { // we want to do curator start here because it needs to // be started before leader selector and it gets a little // complicated to control ordering via beans so that // curator is fully started. client.start(); } leaderSelector = new LeaderSelector(client, buildLeaderPath(), new LeaderListener()); leaderSelector.setId(candidate.getId()); leaderSelector.autoRequeue(); leaderSelector.start(); running = true; } }
public static void main(String[] args) throws Exception { client.start(); ZooKeeper zookeeper = client.getZookeeperClient().getZooKeeper(); System.out.println(ZKPaths.fixForNamespace(path, "sub")); System.out.println(ZKPaths.makePath(path, "sub")); System.out.println(ZKPaths.getNodeFromPath("/curator_zkpath_sample/sub1")); PathAndNode pn = ZKPaths.getPathAndNode("/curator_zkpath_sample/sub1"); System.out.println(pn.getPath()); System.out.println(pn.getNode()); String dir1 = path + "/child1"; String dir2 = path + "/child2"; ZKPaths.mkdirs(zookeeper, dir1); ZKPaths.mkdirs(zookeeper, dir2); System.out.println(ZKPaths.getSortedChildren(zookeeper, path)); ZKPaths.deleteChildren(client.getZookeeperClient().getZooKeeper(), path, true); }
void doEvaluate(Statement base) throws Throwable { try { cluster = new TestingCluster(3); cluster.start(); client = newClient(cluster.getConnectString(), new RetryOneTime(200 /* ms */)); client.start(); checkState( client.blockUntilConnected(5, TimeUnit.SECONDS), "failed to connect to zookeeper in 5 seconds"); base.evaluate(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IllegalStateException("Interrupted while connecting to ZooKeeper", e); } finally { client.close(); cluster.close(); } }
@Test public void testServerCrash() throws Exception { final int TIMEOUT = 1000; final CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(server.getConnectString()) .connectionTimeoutMs(TIMEOUT) .retryPolicy(new RetryOneTime(1)) .build(); try { client.start(); final DistributedBarrier barrier = new DistributedBarrier(client, "/barrier"); barrier.setBarrier(); final ExecutorService service = Executors.newSingleThreadExecutor(); Future<Object> future = service.submit( new Callable<Object>() { @Override public Object call() throws Exception { Thread.sleep(TIMEOUT / 2); server.stop(); return null; } }); barrier.waitOnBarrier(TIMEOUT * 2, TimeUnit.SECONDS); future.get(); Assert.fail(); } catch (KeeperException.ConnectionLossException expected) { // expected } finally { client.close(); } }
@Before public void setUp() throws Exception { testingCluster = new TestingCluster(1); testingCluster.start(); cf = CuratorFrameworkFactory.builder() .connectString(testingCluster.getConnectString()) .retryPolicy(new ExponentialBackoffRetry(1, 10)) .compressionProvider(new PotentiallyGzippedCompressionProvider(false)) .build(); cf.start(); cf.create().creatingParentsIfNeeded().forPath(basePath); worker = new Worker("worker", "localhost", 3, "0"); workerCuratorCoordinator = new WorkerCuratorCoordinator( jsonMapper, new IndexerZkConfig( new ZkPathsConfig() { @Override public String getBase() { return basePath; } }, null, null, null, null, null), new TestRemoteTaskRunnerConfig(new Period("PT1S")), cf, worker); workerCuratorCoordinator.start(); // Start a task monitor workerTaskMonitor = createTaskMonitor(); jsonMapper.registerSubtypes(new NamedType(TestMergeTask.class, "test")); jsonMapper.registerSubtypes(new NamedType(TestRealtimeTask.class, "test_realtime")); workerTaskMonitor.start(); task = TestMergeTask.createDummyTask("test"); }