private static CuratorFramework getZkClient(JdbcConnectionParams connParams) throws Exception { String zooKeeperEnsemble = connParams.getZooKeeperEnsemble(); CuratorFramework zooKeeperClient = CuratorFrameworkFactory.builder().connectString(zooKeeperEnsemble) .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build(); zooKeeperClient.start(); return zooKeeperClient; }
public static CuratorFramework createZkClient(TestingServer testingZKServer, Closer closer) throws InterruptedException { CuratorFramework curatorFramework = closer.register(CuratorFrameworkFactory.newClient(testingZKServer.getConnectString(), new RetryOneTime(2000))); curatorFramework.start(); if (! curatorFramework.blockUntilConnected(60, TimeUnit.SECONDS)) { throw new RuntimeException("Time out waiting to connect to ZK!"); } return curatorFramework; } }
/** * Open a ZooKeeper connection for the JobState. */ public static CuratorFramework zkOpen(String zkHosts, int zkSessionTimeoutMs) throws IOException { //do we need to add a connection status listener? What will that do? ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3); CuratorFramework zk = CuratorFrameworkFactory.newClient(zkHosts, zkSessionTimeoutMs, CuratorFrameworkFactory.builder().getConnectionTimeoutMs(), retryPolicy); zk.start(); return zk; }
@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); } }
public ZkBasedSnowFlakeIdGenerator(String zooKeeperUrl, String applicationName) { CuratorFramework client = CuratorFrameworkFactory.newClient(zooKeeperUrl, new RetryForever(1000)); client.start(); try { String path = "/EasyTransIdGen/" + applicationName + "/P"; String nodeName = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(path); String sequenceStr = nodeName.replaceAll(path, ""); hostSeq = Long.parseLong(sequenceStr) % (2^SnowFlake.MACHINE_BIT); client.close();//do not need to keep connection, hostSeq will not change } catch (Exception e) { throw new RuntimeException("create Id generator failed",e); } }
CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(server.getConnectString()) .sessionTimeoutMs(timing.session()) .connectionTimeoutMs(timing.connection()) .retryPolicy(new RetryOneTime(1)) .namespace("foo") .build(); try client.start(); client.create().creatingParentsIfNeeded().forPath("/test/" + Integer.toString(i)); Stat stat = client.checkExists().forPath("/test"); Assert.assertEquals(stat.getNumChildren(), 0); CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client);
protected void setupServerAndCurator() throws Exception { server = new TestingServer(); timing = new Timing(); curator = CuratorFrameworkFactory .builder() .connectString(server.getConnectString()) .sessionTimeoutMs(timing.session()) .connectionTimeoutMs(timing.connection()) .retryPolicy(new RetryOneTime(1)) .compressionProvider(new PotentiallyGzippedCompressionProvider(true)) .build(); }
private static void checkRootAcls(Configuration conf, String path, String user) { int stime = conf.getInt(ZK_DTSM_ZK_SESSION_TIMEOUT, ZK_DTSM_ZK_SESSION_TIMEOUT_DEFAULT), ctime = conf.getInt(ZK_DTSM_ZK_CONNECTION_TIMEOUT, ZK_DTSM_ZK_CONNECTION_TIMEOUT_DEFAULT); CuratorFramework zkClient = CuratorFrameworkFactory.builder().namespace(null) .retryPolicy(new RetryOneTime(10)).sessionTimeoutMs(stime).connectionTimeoutMs(ctime) .ensembleProvider(new FixedEnsembleProvider(conf.get(ZK_DTSM_ZK_CONNECTION_STRING))) .build(); // Hardcoded from a private field in ZKDelegationTokenSecretManager. // We need to check the path under what it sets for namespace, since the namespace is // created with world ACLs. String nsPath = "/" + path + "/ZKDTSMRoot"; Id currentUser = new Id("sasl", user); try { zkClient.start(); List<String> children = zkClient.getChildren().forPath(nsPath); for (String child : children) { String childPath = nsPath + "/" + child; checkAcls(zkClient, currentUser, childPath); } } catch (Exception e) { throw new RuntimeException(e); } finally { zkClient.close(); } }
public static void clear(TestingServer testingServer) throws Exception { CuratorFramework client = CuratorFrameworkFactory.newClient( testingServer.getConnectString(), new RetryOneTime(RETRY_DELAY_MS)); client.start(); for (String rootNode : client.getChildren().forPath("/")) { if (!rootNode.equals(ZOOKEEPER_ROOT_NODE_NAME)) { client.delete().deletingChildrenIfNeeded().forPath("/" + rootNode); } } client.close(); } }
protected static CuratorFramework createNewClient() throws InterruptedException { CuratorFramework client = CuratorFrameworkFactory.newClient(testingServer.getConnectString(), new BoundedExponentialBackoffRetry(100, 1000, 3)); client.start(); return client; }
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(); }
@Test public void testDeployment() throws Exception { awsDeployment.start(); RetryPolicy retryPolicy = new RetryOneTime(1); try (CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeper.getConnectString(), retryPolicy)) { client.start(); client.blockUntilConnected(); String path = "/test-path/i-1234567890abcdef0"; Thread.sleep(2000); assertNotNull(client.checkExists().forPath(path)); String data = new String(client.getData().forPath(path)); ObjectMapper objectMapper = new ObjectMapper(); DeploymentPayload payload = objectMapper.readValue(data, new TypeReference<DeploymentPayload>() {}); assertEquals("10.158.112.84", payload.getHost()); assertEquals(443, payload.getPort()); assertEquals("us-west-2b", payload.getAvailabilityZone()); } } }
private void startClient() { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); curator = builder.connectString(zkServers).retryPolicy(new ExponentialBackoffRetry(1000,100)).build(); curator.getConnectionStateListenable().addListener(this); curator.start(); }
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.blockUntilConnected(); cf.create().creatingParentsIfNeeded().forPath(basePath); cf.create().creatingParentsIfNeeded().forPath(tasksPath); }
/** * Returns a new client for the zookeeper connection. The client is already started before * returning. * * @return a new {@link CuratorFramework} client to use for leader selection */ private CuratorFramework getNewCuratorClient() { CuratorFramework client = CuratorFrameworkFactory.newClient(mZookeeperAddress, (int) ServerConfiguration.getMs(PropertyKey.ZOOKEEPER_SESSION_TIMEOUT), (int) ServerConfiguration.getMs(PropertyKey.ZOOKEEPER_CONNECTION_TIMEOUT), new ExponentialBackoffRetry(Constants.SECOND_MS, 3)); client.start(); // Sometimes, if the master crashes and restarts too quickly (faster than the zookeeper // timeout), zookeeper thinks the new client is still an old one. In order to ensure a clean // state, explicitly close the "old" client and recreate a new one. client.close(); client = CuratorFrameworkFactory.newClient(mZookeeperAddress, (int) ServerConfiguration.getMs(PropertyKey.ZOOKEEPER_SESSION_TIMEOUT), (int) ServerConfiguration.getMs(PropertyKey.ZOOKEEPER_CONNECTION_TIMEOUT), new ExponentialBackoffRetry(Constants.SECOND_MS, 3)); client.start(); return client; } }
protected static CuratorFramework newClient() { CuratorFramework newClient = CuratorFrameworkFactory.builder() .connectString(zookeeperServer.getConnectString()) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .build(); newClient.start(); wait.untilZookeeperClientStarted(newClient); return newClient; }
/** * create zookeeper client via config. * @param config The Gondola config * @return The ZookeeperRegistryClient instance */ public static RegistryClient createZookeeperClient(Config config) throws IOException { CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(getZookeeperConnectionString(config)) .retryPolicy(new RetryOneTime(1000)) .build(); client.start(); return new ZookeeperRegistryClient(client, new ObjectMapper(), config); }
private CuratorFramework getSession() { if (zkSession == null || zkSession.getState() == CuratorFrameworkState.STOPPED) { synchronized (this) { if (zkSession == null || zkSession.getState() == CuratorFrameworkState.STOPPED) { zkSession = CuratorFrameworkFactory.builder().connectString(zkConnectString) .connectionTimeoutMs(connectTimeoutMillis).aclProvider(aclDefaultProvider) .retryPolicy(new ExponentialBackoffRetry(1000, 3)).build(); zkSession.start(); } } } return zkSession; }
int connectTimeout = url.getParameter("config.connect.timeout", 10 * 1000); String connectString = url.getBackupAddress(); client = newClient(connectString, sessionTimeout, connectTimeout, policy); client.start(); boolean connected = client.blockUntilConnected(3 * connectTimeout, TimeUnit.MILLISECONDS); if (!connected) { if (url.getParameter(Constants.CONFIG_CHECK_KEY, true)) {
public ZookeeperClient(String zkUrl) { zookeeperConnection = zkUrl; LOG.info("Initialize curator with zkurl:" + zkUrl); curator = CuratorFrameworkFactory.newClient(zookeeperConnection, new RetryOneTime(3000)); curator.start(); }