org.apache.curator.framework.CuratorFramework Java Examples
The following examples show how to use
org.apache.curator.framework.CuratorFramework.
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: TestCompressionInTransactionOld.java From curator with Apache License 2.0 | 6 votes |
@Test public void testSetData() throws Exception { final String path = "/a"; final byte[] data = "here's a string".getBytes(); CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); //Create uncompressed data in a transaction client.inTransaction().create().forPath(path, data).and().commit(); Assert.assertEquals(data, client.getData().forPath(path)); //Create compressed data in transaction client.inTransaction().setData().compressed().forPath(path, data).and().commit(); Assert.assertEquals(data, client.getData().decompressed().forPath(path)); } finally { CloseableUtils.closeQuietly(client); } }
Example #2
Source File: AsyncExamples.java From curator with Apache License 2.0 | 6 votes |
public static void create(CuratorFramework client, String path, byte[] payload) { AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); // normally you'd wrap early in your app and reuse the instance // create a node at the given path with the given payload asynchronously async.create().forPath(path, payload).whenComplete((name, exception) -> { if ( exception != null ) { // there was a problem exception.printStackTrace(); } else { System.out.println("Created node name is: " + name); } }); }
Example #3
Source File: ZookeeperRegistryCenterModifyTest.java From shardingsphere-elasticjob-lite with Apache License 2.0 | 6 votes |
@Test public void assertPersistSequential() throws Exception { assertThat(zkRegCenter.persistSequential("/sequential/test_sequential", "test_value"), startsWith("/sequential/test_sequential")); assertThat(zkRegCenter.persistSequential("/sequential/test_sequential", "test_value"), startsWith("/sequential/test_sequential")); CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000)); client.start(); client.blockUntilConnected(); List<String> actual = client.getChildren().forPath("/" + ZookeeperRegistryCenterModifyTest.class.getName() + "/sequential"); assertThat(actual.size(), is(2)); for (String each : actual) { assertThat(each, startsWith("test_sequential")); assertThat(zkRegCenter.get("/sequential/" + each), startsWith("test_value")); } zkRegCenter.remove("/sequential"); assertFalse(zkRegCenter.isExisted("/sequential")); }
Example #4
Source File: BalancedMetricResolver.java From timely with Apache License 2.0 | 6 votes |
private void startLeaderLatch(CuratorFramework curatorFramework) { try { this.leaderLatch = new LeaderLatch(curatorFramework, LEADER_LATCH_PATH); this.leaderLatch.start(); this.leaderLatch.addListener(new LeaderLatchListener() { @Override public void isLeader() { LOG.info("this balancer is the leader"); isLeader.set(true); writeAssignmentsToHdfs(); } @Override public void notLeader() { LOG.info("this balancer is not the leader"); isLeader.set(false); } }); } catch (Exception e) { LOG.error(e.getMessage(), e); } }
Example #5
Source File: TestPersistentEphemeralNode.java From xian with Apache License 2.0 | 6 votes |
/** * Test that if a persistent ephemeral node is created and the node already exists * that if data is present in the PersistentEphermalNode that it is still set. * @throws Exception */ @Test public void testSetDataWhenNodeExists() throws Exception { CuratorFramework curator = newCurator(); curator.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(PATH, "InitialData".getBytes()); byte[] data = "Hello World".getBytes(); PersistentEphemeralNode node = new PersistentEphemeralNode(curator, PersistentEphemeralNode.Mode.EPHEMERAL, PATH, data); node.start(); try { node.waitForInitialCreate(timing.forWaiting().seconds(), TimeUnit.SECONDS); assertTrue(Arrays.equals(curator.getData().forPath(node.getActualPath()), data)); } finally { node.close(); } }
Example #6
Source File: ParserFunctions.java From metron with Apache License 2.0 | 6 votes |
private SensorParserConfig readFromZookeeper(Context context, String sensorType) throws ParseException { SensorParserConfig config; try { CuratorFramework zkClient = getZookeeperClient(context); config = readSensorParserConfigFromZookeeper(sensorType, zkClient); } catch(Exception e) { throw new ParseException(ExceptionUtils.getRootCauseMessage(e), e); } if(config == null) { throw new ParseException("Unable to read configuration from Zookeeper; sensorType = " + sensorType); } return config; }
Example #7
Source File: ServiceCacheLeakTester.java From curator with Apache License 2.0 | 6 votes |
private static void doWork(CuratorFramework curatorFramework) throws Exception { ServiceInstance<Void> thisInstance = ServiceInstance.<Void>builder().name("myservice").build(); final ServiceDiscovery<Void> serviceDiscovery = ServiceDiscoveryBuilder.builder(Void.class).client(curatorFramework.usingNamespace("dev")).basePath("/instances").thisInstance(thisInstance).build(); serviceDiscovery.start(); for ( int i = 0; i < 100000; i++ ) { final ServiceProvider<Void> s = serviceProvider(serviceDiscovery, "myservice"); s.start(); try { s.getInstance().buildUriSpec(); } finally { s.close(); } } }
Example #8
Source File: ExampleClient.java From curator with Apache License 2.0 | 6 votes |
@Override public void takeLeadership(CuratorFramework client) throws Exception { // we are now the leader. This method should not return until we want to relinquish leadership final int waitSeconds = (int)(5 * Math.random()) + 1; System.out.println(name + " is now the leader. Waiting " + waitSeconds + " seconds..."); System.out.println(name + " has been leader " + leaderCount.getAndIncrement() + " time(s) before."); try { Thread.sleep(TimeUnit.SECONDS.toMillis(waitSeconds)); } catch ( InterruptedException e ) { System.err.println(name + " was interrupted."); Thread.currentThread().interrupt(); } finally { System.out.println(name + " relinquishing leadership.\n"); } }
Example #9
Source File: ClusterPathChildrenCacheListener.java From Decision with Apache License 2.0 | 6 votes |
@Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { String node; String nodeId; try { node = ZKPaths.getNodeFromPath(event.getData().getPath()); nodeId = node.substring(node.indexOf("_") + 1); clusterSyncManagerInstance.updateNodeStatus(nodeId, event.getType()); }catch (Exception e){ logger.error("Exception receiving event {}: {}", event, e.getMessage()); } }
Example #10
Source File: BookKeeperLog.java From pravega with Apache License 2.0 | 6 votes |
/** * Creates a new instance of the BookKeeper log class. * * @param containerId The Id of the Container whose BookKeeperLog to open. * @param zkClient A reference to the CuratorFramework client to use. * @param bookKeeper A reference to the BookKeeper client to use. * @param config Configuration to use. * @param executorService An Executor to use for async operations. */ BookKeeperLog(int containerId, CuratorFramework zkClient, BookKeeper bookKeeper, BookKeeperConfig config, ScheduledExecutorService executorService) { Preconditions.checkArgument(containerId >= 0, "containerId must be a non-negative integer."); this.logId = containerId; this.zkClient = Preconditions.checkNotNull(zkClient, "zkClient"); this.bookKeeper = Preconditions.checkNotNull(bookKeeper, "bookKeeper"); this.config = Preconditions.checkNotNull(config, "config"); this.executorService = Preconditions.checkNotNull(executorService, "executorService"); this.closed = new AtomicBoolean(); this.logNodePath = HierarchyUtils.getPath(containerId, this.config.getZkHierarchyDepth()); this.traceObjectId = String.format("Log[%d]", containerId); this.writes = new WriteQueue(); val retry = createRetryPolicy(this.config.getMaxWriteAttempts(), this.config.getBkWriteTimeoutMillis()); this.writeProcessor = new SequentialAsyncProcessor(this::processWritesSync, retry, this::handleWriteProcessorFailures, this.executorService); this.rolloverProcessor = new SequentialAsyncProcessor(this::rollover, retry, this::handleRolloverFailure, this.executorService); this.metrics = new BookKeeperMetrics.BookKeeperLog(containerId); this.metricReporter = this.executorService.scheduleWithFixedDelay(this::reportMetrics, REPORT_INTERVAL, REPORT_INTERVAL, TimeUnit.MILLISECONDS); this.queueStateChangeListeners = new HashSet<>(); }
Example #11
Source File: TestBlockUntilConnected.java From curator with Apache License 2.0 | 6 votes |
/** * Test the case where we are not currently connected and never have been */ @Test public void testBlockUntilConnectedCurrentlyNeverConnected() { CuratorFramework client = CuratorFrameworkFactory.builder(). connectString(server.getConnectString()). retryPolicy(new RetryOneTime(1)). build(); try { client.start(); Assert.assertTrue(client.blockUntilConnected(5, TimeUnit.SECONDS), "Not connected"); } catch ( InterruptedException e ) { Assert.fail("Unexpected interruption"); } finally { CloseableUtils.closeQuietly(client); } }
Example #12
Source File: CreateKeyspacesCommand.java From emodb with Apache License 2.0 | 6 votes |
@Override protected void run(Bootstrap<EmoConfiguration> bootstrap, Namespace namespace, EmoConfiguration emoConfiguration) throws Exception { _outputOnly = namespace.getBoolean("output_only"); DdlConfiguration ddlConfiguration = parseDdlConfiguration(toFile(namespace.getString("config-ddl"))); CuratorFramework curator = null; if (!_outputOnly) { curator = emoConfiguration.getZooKeeperConfiguration().newCurator(); curator.start(); } try { createKeyspacesIfNecessary(emoConfiguration, ddlConfiguration, curator, bootstrap.getMetricRegistry()); } finally { Closeables.close(curator, true); } }
Example #13
Source File: PersistentEphemeralNode.java From curator-extensions with Apache License 2.0 | 6 votes |
/** * Create the ephemeral node in ZooKeeper. If the node cannot be created in a timely fashion then an exception will * be thrown. * * @param curator Client to manage ZooKeeper nodes with. * @param basePath Path to parent node this node should be created in. * @param data Data to store in the node. * @param mode Node creation mode. */ public PersistentEphemeralNode(CuratorFramework curator, String basePath, byte[] data, CreateMode mode) { Objects.requireNonNull(curator); checkArgument(curator.getState() == CuratorFrameworkState.STARTED); Objects.requireNonNull(basePath); Objects.requireNonNull(data); Objects.requireNonNull(mode); checkArgument(mode == CreateMode.EPHEMERAL || mode == CreateMode.EPHEMERAL_SEQUENTIAL); // TODO: Share this executor across multiple persistent ephemeral nodes in a way that guarantees that it is a // TODO: single thread executor. _executor = Executors.newSingleThreadScheduledExecutor(THREAD_FACTORY); _async = new Async(_executor, new Sync(curator, basePath, data, mode)); CountDownLatch latch = new CountDownLatch(1); _async.createNode(latch); await(latch, CREATION_WAIT_IN_SECONDS, TimeUnit.SECONDS); }
Example #14
Source File: DataStoreCache.java From timely with Apache License 2.0 | 6 votes |
private void testIPRWLock(CuratorFramework curatorFramework, InterProcessReadWriteLock lock, String path) { try { lock.writeLock().acquire(10, TimeUnit.SECONDS); } catch (Exception e1) { try { curatorFramework.delete().deletingChildrenIfNeeded().forPath(path); curatorFramework.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT) .forPath(path); } catch (Exception e2) { LOG.info(e2.getMessage(), e2); } } finally { try { lock.writeLock().release(); } catch (Exception e3) { LOG.error(e3.getMessage()); } } }
Example #15
Source File: TestFrameworkEdges.java From curator with Apache License 2.0 | 6 votes |
@Test public void testMissedResponseOnESCreate() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1)); client.start(); try { CreateBuilderImpl createBuilder = (CreateBuilderImpl)client.create(); createBuilder.failNextCreateForTesting = true; String ourPath = createBuilder.withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath("/"); Assert.assertTrue(ourPath.startsWith(ZKPaths.makePath("/", ProtectedUtils.PROTECTED_PREFIX))); Assert.assertFalse(createBuilder.failNextCreateForTesting); } finally { CloseableUtils.closeQuietly(client); } }
Example #16
Source File: MasterRespondsWithNoZkTest.java From helios with Apache License 2.0 | 5 votes |
@Override public CuratorFramework newClient(final String connectString, final int sessionTimeoutMs, final int connectionTimeoutMs, final RetryPolicy retryPolicy, final ACLProvider aclProvider, final List<AuthInfo> authorization) { final CuratorFramework curator = mock(CuratorFramework.class); final RetryLoop retryLoop = mock(RetryLoop.class); when(retryLoop.shouldContinue()).thenReturn(false); final CuratorZookeeperClient czkClient = mock(CuratorZookeeperClient.class); when(czkClient.newRetryLoop()).thenReturn(retryLoop); when(curator.getZookeeperClient()).thenReturn(czkClient); @SuppressWarnings("unchecked") final Listenable<ConnectionStateListener> mockListener = (Listenable<ConnectionStateListener>) mock(Listenable.class); when(curator.getConnectionStateListenable()).thenReturn(mockListener); final GetChildrenBuilder builder = mock(GetChildrenBuilder.class); when(curator.getChildren()).thenReturn(builder); try { when(builder.forPath(anyString())).thenThrow( new KeeperException.ConnectionLossException()); } catch (Exception ignored) { // never throws } when(curator.newNamespaceAwareEnsurePath(anyString())).thenReturn(mock(EnsurePath.class)); return curator; }
Example #17
Source File: TestFramework.java From curator with Apache License 2.0 | 5 votes |
@Test public void testOverrideCreateParentContainers() throws Exception { if ( !checkForContainers() ) { return; } CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(server.getConnectString()) .retryPolicy(new RetryOneTime(1)) .dontUseContainerParents() .build(); try { client.start(); client.create().creatingParentContainersIfNeeded().forPath("/one/two/three", "foo".getBytes()); byte[] data = client.getData().forPath("/one/two/three"); Assert.assertEquals(data, "foo".getBytes()); client.delete().forPath("/one/two/three"); new Timing().sleepABit(); Assert.assertNotNull(client.checkExists().forPath("/one/two")); new Timing().sleepABit(); Assert.assertNotNull(client.checkExists().forPath("/one")); } finally { CloseableUtils.closeQuietly(client); } }
Example #18
Source File: TestTransactionsOld.java From curator with Apache License 2.0 | 5 votes |
@Test public void testBasic() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); try { client.start(); Collection<CuratorTransactionResult> results = client.inTransaction() .create().forPath("/foo") .and() .create().forPath("/foo/bar", "snafu".getBytes()) .and() .commit(); Assert.assertTrue(client.checkExists().forPath("/foo/bar") != null); Assert.assertEquals(client.getData().forPath("/foo/bar"), "snafu".getBytes()); CuratorTransactionResult fooResult = Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/foo")); CuratorTransactionResult fooBarResult = Iterables.find(results, CuratorTransactionResult.ofTypeAndPath(OperationType.CREATE, "/foo/bar")); Assert.assertNotNull(fooResult); Assert.assertNotNull(fooBarResult); Assert.assertNotSame(fooResult, fooBarResult); Assert.assertEquals(fooResult.getResultPath(), "/foo"); Assert.assertEquals(fooBarResult.getResultPath(), "/foo/bar"); } finally { CloseableUtils.closeQuietly(client); } }
Example #19
Source File: ZKUtils.java From kafka-graphs with Apache License 2.0 | 5 votes |
public static void removeChild(CuratorFramework curator, String rootPath, String child) throws Exception { String path = ZKPaths.makePath(rootPath, child); try { log.debug("removing child {}", path); curator.delete().guaranteed().forPath(path); } catch (KeeperException.NoNodeException e) { // ignore } }
Example #20
Source File: TestServiceProvider.java From curator with Apache License 2.0 | 5 votes |
@Test public void testBasic() throws Exception { List<Closeable> closeables = Lists.newArrayList(); try { CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); closeables.add(client); client.start(); ServiceInstance<String> instance = ServiceInstance.<String>builder().payload("thing").name("test").port(10064).build(); ServiceDiscovery<String> discovery = ServiceDiscoveryBuilder.builder(String.class).basePath("/test").client(client).thisInstance(instance).build(); closeables.add(discovery); discovery.start(); ServiceProvider<String> provider = discovery.serviceProviderBuilder().serviceName("test").build(); closeables.add(provider); provider.start(); Assert.assertEquals(provider.getInstance(), instance); List<ServiceInstance<String>> list = Lists.newArrayList(); list.add(instance); Assert.assertEquals(provider.getAllInstances(), list); } finally { Collections.reverse(closeables); for ( Closeable c : closeables ) { CloseableUtils.closeQuietly(c); } } }
Example #21
Source File: SharedReentrantLock.java From micro-service with MIT License | 5 votes |
public static void main(String[] args) { CuratorFramework client = CuratorFrameworkFactory.newClient(ZOOKEEPER_CONNECT_STRING, new ExponentialBackoffRetry(1000, 3)); client.start(); for (int i = 0; i < 5; i++) { SharedReentrantLock sharedReentrantLock = new SharedReentrantLock(client, i + "_client"); Thread thread = new Thread(sharedReentrantLock); thread.start(); } }
Example #22
Source File: ZKUtils.java From kafka-graphs with Apache License 2.0 | 5 votes |
public static void removeRoot(CuratorFramework curator, String id) throws Exception { String path = PREGEL_PATH + id; try { log.debug("removing root {}", path); curator.delete().guaranteed().deletingChildrenIfNeeded().forPath(path); } catch (KeeperException.NoNodeException e) { // ignore } }
Example #23
Source File: TestLeaderSelectorEdges.java From curator with Apache License 2.0 | 5 votes |
@Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if ( newState == ConnectionState.RECONNECTED ) { reconnected.countDown(); } }
Example #24
Source File: ZookeeperTests.java From spring-cloud-cluster with Apache License 2.0 | 5 votes |
@Bean(destroyMethod = "close") public CuratorFramework curatorClient() throws Exception { CuratorFramework client = CuratorFrameworkFactory.builder().defaultData(new byte[0]) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .connectString("localhost:" + testingServerWrapper.getPort()).build(); // for testing we start it here, thought initiator // is trying to start it if not already done client.start(); return client; }
Example #25
Source File: ZKUtil.java From codes-scratch-zookeeper-netty with Apache License 2.0 | 5 votes |
public static List<String> getChilds(CuratorFramework client, String path, CuratorWatcher watcher) { try { if (watcher != null) { return (List) ((BackgroundPathable) client.getChildren().usingWatcher(watcher)).forPath(path); } return (List) client.getChildren().forPath(path); } catch (Exception e) { LOGGER.error("ZKUtil-->>getChilds(CuratorFramework client, String path, CuratorWatcher watcher) error,", e); } return null; }
Example #26
Source File: SchemasxmlTozkLoader.java From dble with GNU General Public License v2.0 | 5 votes |
public SchemasxmlTozkLoader(ZookeeperProcessListen zookeeperListen, CuratorFramework curator, XmlProcessBase xmlParseBase) { this.setCurator(curator); currZkPath = KVPathUtil.getConfSchemaPath(); zookeeperListen.addToInit(this); this.parseSchemaXmlService = new SchemasParseXmlImpl(xmlParseBase); }
Example #27
Source File: NodeCacheExample.java From ZKRecipesByExample with Apache License 2.0 | 5 votes |
private static void processCommands(CuratorFramework client, NodeCache cache) throws Exception { printHelp(); try { addListener(cache); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); boolean done = false; while (!done) { System.out.print("> "); String line = in.readLine(); if (line == null) { break; } String command = line.trim(); String[] parts = command.split("\\s"); if (parts.length == 0) { continue; } String operation = parts[0]; String args[] = Arrays.copyOfRange(parts, 1, parts.length); if (operation.equalsIgnoreCase("help") || operation.equalsIgnoreCase("?")) { printHelp(); } else if (operation.equalsIgnoreCase("q") || operation.equalsIgnoreCase("quit")) { done = true; } else if (operation.equals("set")) { setValue(client, command, args); } else if (operation.equals("remove")) { remove(client); } else if (operation.equals("show")) { show(cache); } Thread.sleep(1000); // just to allow the console output to catch // up } } catch (Exception ex) { ex.printStackTrace(); } finally { } }
Example #28
Source File: CuratorConnector.java From jumbune with GNU Lesser General Public License v3.0 | 5 votes |
/** * Gets the children. * * @param path the path * @return the children * @throws Exception the exception */ public List<String> getChildren(String path) throws Exception { CuratorFramework client = getOrCreateClient(); client.start(); List<String> children = client.getChildren().forPath(path); client.close(); return children; }
Example #29
Source File: ZookeeperDistributedLock.java From kylin-on-parquet-v2 with Apache License 2.0 | 5 votes |
@Override public Closeable watchLocks(String lockPathRoot, Executor executor, final Watcher watcher) { PathChildrenCache cache = new PathChildrenCache(curator, lockPathRoot, true); try { cache.start(); cache.getListenable().addListener(new PathChildrenCacheListener() { @Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { switch (event.getType()) { case CHILD_ADDED: watcher.onLock(event.getData().getPath(), new String(event.getData().getData(), StandardCharsets.UTF_8)); break; case CHILD_REMOVED: watcher.onUnlock(event.getData().getPath(), new String(event.getData().getData(), StandardCharsets.UTF_8)); break; default: break; } } }, executor); } catch (Exception ex) { logger.error("Error to watch lock path " + lockPathRoot, ex); } return cache; }
Example #30
Source File: EventContainer.java From DBus with Apache License 2.0 | 5 votes |
protected void saveZk(String node, String packet) { try { CuratorFramework curator = CuratorContainer.getInstance().getCurator(); if (curator.getState() == CuratorFrameworkState.STOPPED) { LOG.info("[EventContainer] CuratorFrameworkState:{}", CuratorFrameworkState.STOPPED.name()); } else { curator.setData().forPath(node, packet.getBytes()); } } catch (Exception e) { LOG.error("[control-event] 报错znode: " + node + ",数据包:" + packet + "失败!", e); } }