Java Code Examples for org.apache.curator.framework.CuratorFramework#blockUntilConnected()
The following examples show how to use
org.apache.curator.framework.CuratorFramework#blockUntilConnected() .
These examples are extracted from open source projects.
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 Project: rdf-delta File: Zk.java License: Apache License 2.0 | 6 votes |
/** Connect a curator client to a running ZooKepper server. */ public static void connect(CuratorFramework client) { switch(client.getState()) { case LATENT : client.start(); try { client.blockUntilConnected(); } catch (InterruptedException ex) { throw new RuntimeException(ex); } return; case STARTED : //LOG.warn("CuratorFramework already started"); return ; case STOPPED : throw new DeltaException("CuratorFramework closed"); default : break; } }
Example 2
Source Project: xian File: TestBlockUntilConnected.java License: Apache License 2.0 | 6 votes |
/** * Test that we are actually connected every time that we block until connection is established in a tight loop. */ @Test public void testBlockUntilConnectedTightLoop() throws InterruptedException { CuratorFramework client; for(int i = 0 ; i < 50 ; i++) { client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(100)); try { client.start(); client.blockUntilConnected(); Assert.assertTrue(client.getZookeeperClient().isConnected(), "Not connected after blocking for connection #" + i); } finally { client.close(); } } }
Example 3
Source Project: shardingsphere-elasticjob-lite File: ZookeeperRegistryCenterModifyTest.java License: 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 Project: shardingsphere-elasticjob-cloud File: ZookeeperElectionServiceTest.java License: Apache License 2.0 | 6 votes |
@Test @Ignore public void assertContend() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000)); client.start(); client.blockUntilConnected(); ZookeeperElectionService service = new ZookeeperElectionService(HOST_AND_PORT, client, ELECTION_PATH, electionCandidate); service.start(); ElectionCandidate anotherElectionCandidate = mock(ElectionCandidate.class); CuratorFramework anotherClient = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000)); ZookeeperElectionService anotherService = new ZookeeperElectionService("ANOTHER_CLIENT:8899", anotherClient, ELECTION_PATH, anotherElectionCandidate); anotherClient.start(); anotherClient.blockUntilConnected(); anotherService.start(); KillSession.kill(client.getZookeeperClient().getZooKeeper(), EmbedTestingServer.getConnectionString()); service.stop(); verify(anotherElectionCandidate).startLeadership(); }
Example 5
Source Project: Mycat2 File: ZKUtils.java License: GNU General Public License v3.0 | 6 votes |
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 6
Source Project: x-pipe File: DefaultZkConfig.java License: Apache License 2.0 | 6 votes |
@Override public CuratorFramework create(String address) throws InterruptedException { Builder builder = CuratorFrameworkFactory.builder(); builder.connectionTimeoutMs(getZkConnectionTimeoutMillis()); builder.connectString(address); builder.maxCloseWaitMs(getZkCloseWaitMillis()); builder.namespace(getZkNamespace()); builder.retryPolicy(new RetryNTimes(getZkRetries(), getSleepMsBetweenRetries())); builder.sessionTimeoutMs(getZkSessionTimeoutMillis()); builder.threadFactory(XpipeThreadFactory.create("Xpipe-ZK-" + address, true)); logger.info("[create]{}, {}", Codec.DEFAULT.encode(this), address); CuratorFramework curatorFramework = builder.build(); curatorFramework.start(); curatorFramework.blockUntilConnected(waitForZkConnectedMillis(), TimeUnit.MILLISECONDS); return curatorFramework; }
Example 7
Source Project: dble File: ZKUtils.java License: GNU General Public License v2.0 | 6 votes |
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 8
Source Project: phoenix-omid File: TestEndToEndScenariosWithHA.java License: Apache License 2.0 | 6 votes |
private static CuratorFramework provideInitializedZookeeperClient(String zkConnection) throws Exception { LOG.info("Creating Zookeeper Client connecting to {}", zkConnection); RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework zkClient = CuratorFrameworkFactory .builder() .namespace(NAMESPACE) .connectString(zkConnection) .retryPolicy(retryPolicy).build(); LOG.info("Connecting to ZK cluster {}", zkClient.getState()); zkClient.start(); zkClient.blockUntilConnected(); LOG.info("Connection to ZK cluster {}", zkClient.getState()); return zkClient; }
Example 9
Source Project: xian File: AfterConnectionEstablished.java License: Apache License 2.0 | 5 votes |
/** * Spawns a new new background thread that will block until a connection is available and * then execute the 'runAfterConnection' logic * * @param client The curator client * @param runAfterConnection The logic to run * @return future of the task so it can be canceled, etc. if needed */ public static Future<?> execute(final CuratorFramework client, final Runnable runAfterConnection) throws Exception { //Block until connected final ExecutorService executor = ThreadUtils.newSingleThreadExecutor(ThreadUtils.getProcessName(runAfterConnection.getClass())); Runnable internalCall = new Runnable() { @Override public void run() { try { client.blockUntilConnected(); runAfterConnection.run(); } catch ( Exception e ) { ThreadUtils.checkInterrupted(e); log.error("An error occurred blocking until a connection is available", e); } finally { executor.shutdown(); } } }; return executor.submit(internalCall); }
Example 10
Source Project: xian File: TestSharedCount.java License: Apache License 2.0 | 5 votes |
@Test public void testDisconnectEventOnWatcherDoesNotRetry() throws Exception { final CountDownLatch gotSuspendEvent = new CountDownLatch(1); CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryNTimes(10, 1000)); curatorFramework.start(); curatorFramework.blockUntilConnected(); SharedCount sharedCount = new SharedCount(curatorFramework, "/count", 10); sharedCount.start(); curatorFramework.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if (newState == ConnectionState.SUSPENDED) { gotSuspendEvent.countDown(); } } }); try { server.stop(); // if watcher goes into 10second retry loop we won't get timely notification Assert.assertTrue(gotSuspendEvent.await(5, TimeUnit.SECONDS)); } finally { CloseableUtils.closeQuietly(sharedCount); CloseableUtils.closeQuietly(curatorFramework); } }
Example 11
Source Project: myriad File: MesosMasterHealthCheck.java License: Apache License 2.0 | 5 votes |
@Override protected Result check() throws Exception { String mesosMaster = cfg.getMesosMaster(); int zkIndex = mesosMaster.indexOf("zk://", 0); if (zkIndex >= 0) { String zkHostPorts = mesosMaster.substring(5, mesosMaster.indexOf("/", 5)); String[] hostPorts = zkHostPorts.split(","); for (String hostPort : hostPorts) { CuratorFramework client = CuratorFrameworkFactory.newClient( hostPort, new ExponentialBackoffRetry(1000, 3)); client.start(); client.blockUntilConnected(5, TimeUnit.SECONDS); switch (client.getState()) { case STARTED: return Result.healthy(); case STOPPED: LOGGER.fine("Unable to reach: " + hostPort); case LATENT: LOGGER.fine("Unable to reach: " + hostPort); default: LOGGER.fine("Unable to reach: " + hostPort); } } } else { if (HealthCheckUtils.checkHostPort(mesosMaster)) { return Result.healthy(); } } return Result.unhealthy("Unable to connect to: " + mesosMaster); }
Example 12
Source Project: spring-cloud-zookeeper File: ZookeeperAutoConfiguration.java License: Apache License 2.0 | 5 votes |
@Bean(destroyMethod = "close") @ConditionalOnMissingBean public CuratorFramework curatorFramework(RetryPolicy retryPolicy, ZookeeperProperties properties, ObjectProvider<CuratorFrameworkCustomizer> optionalCuratorFrameworkCustomizerProvider, ObjectProvider<EnsembleProvider> optionalEnsembleProvider, ObjectProvider<TracerDriver> optionalTracerDriverProvider) throws Exception { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); EnsembleProvider ensembleProvider = optionalEnsembleProvider.getIfAvailable(); if (ensembleProvider != null) { builder.ensembleProvider(ensembleProvider); } else { builder.connectString(properties.getConnectString()); } builder.sessionTimeoutMs((int) properties.getSessionTimeout().toMillis()) .connectionTimeoutMs((int) properties.getConnectionTimeout().toMillis()) .retryPolicy(retryPolicy); optionalCuratorFrameworkCustomizerProvider.orderedStream() .forEach(curatorFrameworkCustomizer -> curatorFrameworkCustomizer .customize(builder)); CuratorFramework curator = builder.build(); optionalTracerDriverProvider.ifAvailable(tracerDriver -> { if (curator.getZookeeperClient() != null) { curator.getZookeeperClient().setTracerDriver(tracerDriver); } }); 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; }
Example 13
Source Project: shardingsphere-elasticjob-cloud File: ZookeeperRegistryCenterForAuthTest.java License: Apache License 2.0 | 5 votes |
@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 14
Source Project: shardingsphere-elasticjob-cloud File: ZookeeperRegistryCenterForAuthTest.java License: Apache License 2.0 | 5 votes |
@Test(expected = NoAuthException.class) public void assertInitWithDigestFailure() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000)); client.start(); client.blockUntilConnected(); client.getData().forPath("/" + ZookeeperRegistryCenterForAuthTest.class.getName() + "/test/deep/nested"); }
Example 15
Source Project: shardingsphere-elasticjob-cloud File: ZookeeperRegistryCenterModifyTest.java License: Apache License 2.0 | 5 votes |
@Test public void assertPersistEphemeral() throws Exception { zkRegCenter.persist("/persist", "persist_value"); zkRegCenter.persistEphemeral("/ephemeral", "ephemeral_value"); assertThat(zkRegCenter.get("/persist"), is("persist_value")); assertThat(zkRegCenter.get("/ephemeral"), is("ephemeral_value")); zkRegCenter.close(); CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000)); client.start(); client.blockUntilConnected(); assertThat(client.getData().forPath("/" + ZookeeperRegistryCenterModifyTest.class.getName() + "/persist"), is("persist_value".getBytes())); assertNull(client.checkExists().forPath("/" + ZookeeperRegistryCenterModifyTest.class.getName() + "/ephemeral")); zkRegCenter.init(); }
Example 16
Source Project: shardingsphere-elasticjob-lite File: ZookeeperRegistryCenterForAuthTest.java License: Apache License 2.0 | 5 votes |
@Test(expected = NoAuthException.class) public void assertInitWithDigestFailure() throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000)); client.start(); client.blockUntilConnected(); client.getData().forPath("/" + ZookeeperRegistryCenterForAuthTest.class.getName() + "/test/deep/nested"); }
Example 17
Source Project: phoenix-omid File: TestUtils.java License: Apache License 2.0 | 5 votes |
public static CuratorFramework provideConnectedZKClient(String zkCluster) throws Exception { LOG.info("Creating Zookeeper Client connecting to {}", zkCluster); RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework zkClient = CuratorFrameworkFactory.builder().namespace("omid") .connectString(zkCluster).retryPolicy(retryPolicy).build(); LOG.info("Connecting to ZK cluster {}", zkClient.getState()); zkClient.start(); zkClient.blockUntilConnected(); LOG.info("Connection to ZK cluster {}", zkClient.getState()); return zkClient; }
Example 18
Source Project: vertx-service-discovery File: ZookeeperBackendServiceTest.java License: Apache License 2.0 | 4 votes |
@Test public void testReconnection() throws Exception { Record record = new Record().setName("my-service").setStatus(Status.UP); assertThat(record.getRegistration()).isNull(); // Insertion AtomicReference<Record> reference = new AtomicReference<>(); backend.store(record, ar -> { if (!ar.succeeded()) { ar.cause().printStackTrace(); } reference.set(ar.result()); }); await().until(() -> reference.get() != null); assertThat(reference.get().getName()).isEqualToIgnoringCase("my-service"); assertThat(reference.get().getRegistration()).isNotNull(); record = reference.get(); // Retrieve reference.set(null); backend.getRecord(record.getRegistration(), ar -> reference.set(ar.result())); await().until(() -> reference.get() != null); assertThat(reference.get().getName()).isEqualToIgnoringCase("my-service"); assertThat(reference.get().getRegistration()).isNotNull(); server.stop(); AtomicReference<Throwable> error = new AtomicReference<>(); backend.getRecord(record.getRegistration(), ar -> error.set(ar.cause())); await().until(() -> error.get() != null); assertThat(error.get()).isInstanceOf(KeeperException.ConnectionLossException.class); server.restart(); CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(server.getConnectString()) .retryPolicy(new ExponentialBackoffRetry(10, 3)) .build(); client.start(); // Wait until we have been connected to the server client.blockUntilConnected(); reference.set(null); backend.getRecord(record.getRegistration(), ar -> { if (ar.failed()) { ar.cause().printStackTrace(); } reference.set(ar.result()); }); await().until(() -> reference.get() != null); assertThat(reference.get().getName()).isEqualToIgnoringCase("my-service"); assertThat(reference.get().getRegistration()).isNotNull(); server.stop(); // Remove AtomicBoolean completed = new AtomicBoolean(); completed.set(false); backend.remove(record, ar -> completed.set(ar.failed())); await().untilAtomic(completed, is(true)); server.restart(); completed.set(false); backend.remove(record, ar -> completed.set(ar.succeeded())); await().untilAtomic(completed, is(true)); client.close(); }
Example 19
Source Project: syncope File: ZookeeperKeymasterClientContext.java License: Apache License 2.0 | 4 votes |
@ConditionalOnExpression("#{'${keymaster.address}' " + "matches '^((\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})|[a-z\\.]+):[0-9]+$'}") @Bean public CuratorFramework curatorFramework() throws InterruptedException { if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) { javax.security.auth.login.Configuration.setConfiguration(new javax.security.auth.login.Configuration() { private final AppConfigurationEntry[] entries = { new AppConfigurationEntry( DigestLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, Map.of( "username", username, "password", password )) }; @Override public AppConfigurationEntry[] getAppConfigurationEntry(final String name) { return entries; } }); } CuratorFrameworkFactory.Builder clientBuilder = CuratorFrameworkFactory.builder(). connectString(address). retryPolicy(new ExponentialBackoffRetry(baseSleepTimeMs, maxRetries)); if (StringUtils.isNotBlank(username)) { clientBuilder.authorization("digest", username.getBytes()).aclProvider(new ACLProvider() { @Override public List<ACL> getDefaultAcl() { return ZooDefs.Ids.CREATOR_ALL_ACL; } @Override public List<ACL> getAclForPath(final String path) { return ZooDefs.Ids.CREATOR_ALL_ACL; } }); } CuratorFramework client = clientBuilder.build(); client.start(); client.blockUntilConnected(3, TimeUnit.SECONDS); return client; }
Example 20
Source Project: xian File: TestBlockUntilConnected.java License: Apache License 2.0 | 4 votes |
/** * Test the case where we are not currently connected and the thread gets interrupted * prior to a connection becoming available */ @Test public void testBlockUntilConnectedInterrupt() { //Kill the server CloseableUtils.closeQuietly(server); final CuratorFramework client = CuratorFrameworkFactory.builder(). connectString(server.getConnectString()). retryPolicy(new RetryOneTime(1)). build(); try { client.start(); final Thread threadToInterrupt = Thread.currentThread(); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { threadToInterrupt.interrupt(); } }, 3000); client.blockUntilConnected(5, TimeUnit.SECONDS); Assert.fail("Expected interruption did not occur"); } catch ( InterruptedException e ) { //This is expected } finally { CloseableUtils.closeQuietly(client); } }