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; } }
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)); if ( r.nextBoolean() ) client.create().forPath("/test/" + Integer.toString(i) + "/foo"); ++nonEmptyNodes; CloseableUtils.closeQuietly(reaper); CloseableUtils.closeQuietly(client);
@Override public Object call() throws Exception { LOG.info("Using local ZKClient"); managedZkClient = true; CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(System.getProperty("zookeeper.url", "localhost:2181")) .retryPolicy(new RetryOneTime(1000)) .connectionTimeoutMs(10000); String password = System.getProperty("zookeeper.password", "admin"); if (password != null && !password.isEmpty()) { builder.aclProvider(new CuratorACLManager()); builder.authorization("digest", ("fabric:"+password).getBytes()); } CuratorFramework client = builder.build(); client.start(); client.getZookeeperClient().blockUntilConnectedOrTimedOut(); return client; }
@Test public void testUpdaterAfterStart() throws Exception { try (TestingServer server = new TestingServer()) { server.start(); RetryPolicy retryPolicy = new RetryOneTime(1); try (CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy)) { client.start(); String path = "/xio/watched/node-init"; client.create().orSetData().creatingParentsIfNeeded().forPath(path, payload.getBytes());
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);
@Test public void testWriteIpAddressDeterministicRuleEngineConfig() throws Exception { try (TestingServer server = new TestingServer()) { server.start(); RetryPolicy retryPolicy = new RetryOneTime(1); try (CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy)) { client.start(); String path = "/some/path/to/nodes/ipRules"; byte[] data = client.getData().forPath(path); ThriftUnmarshaller unmarshaller = new ThriftUnmarshaller(); IpAddressDeterministicRuleEngineConfig read = new IpAddressDeterministicRuleEngineConfig();
/** * 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 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(); } }
@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()); } } }
CuratorFramework curator = CuratorFrameworkFactory.builder().connectString(zookeeperUrl).retryPolicy(new RetryOneTime(500)) .aclProvider(aclManager).authorization("digest", ("fabric:" + oldPassword).getBytes()).sessionTimeoutMs(30000) .build(); curator.start(); try { curator.setData().forPath(ZkPath.CONFIG_ENSEMBLE_PASSWORD.getPath(), PasswordEncoder.encode(newPassword).getBytes(Charsets.UTF_8)); } finally { curator.close(); CuratorFramework curator = CuratorFrameworkFactory.builder().connectString(fabricService.getZookeeperUrl()).retryPolicy(new RetryOneTime(500)) .aclProvider(aclManager).authorization("digest", ("fabric:" + fabricService.getZookeeperPassword()).getBytes()).sessionTimeoutMs(30000) .build();
@Override protected void doStart() throws Exception { CuratorFramework curatorFramework = getCurator(); if (curatorFramework == null) { String connectStr = getZkConnectStr(); if (connectStr == null || connectStr.isEmpty()) { connectStr = Systems.getEnvVarOrSystemProperty("ZK_CONNECT_STR", "127.0.0.1:2181"); setZkConnectStr(connectStr); } int retryTime = getZkRetryTime(); if (retryTime == 0) { retryTime = Systems.getEnvVarOrSystemProperty("ZK_RETRY_TIME", 2000).intValue(); setZkRetryTime(retryTime); } curatorFramework = CuratorFrameworkFactory.newClient(getZkConnectStr(), new RetryOneTime(getZkRetryTime())); curatorFramework.start(); setCurator(curatorFramework); } String zkPath = Systems.getEnvVarOrSystemProperty("ZK_PATH", getZkPath()); setZkPath(zkPath); brokerPath = ZKPaths.makePath(getZkPath(), "brokers"); ZKPaths.mkdirs(getCurator().getZookeeperClient().getZooKeeper(), brokerPath); interProcessLock = new InterProcessSemaphoreV2(getCurator(), brokerPath, 1); } }
public ZookeeperClient(String zkUrl) { zookeeperConnection = zkUrl; LOG.info("Initialize curator with zkurl:" + zkUrl); curator = CuratorFrameworkFactory.newClient(zookeeperConnection, new RetryOneTime(3000)); curator.start(); }
/** * Invoke the zookeeper test, ensure zookeeper can be accessed. * The test fails in case zookeeper cannot be accessed. * */ @Test public void zkTest() { log.info("Start execution of ZkTest"); Service zk = Utils.createZookeeperService(); URI zkUri = zk.getServiceDetails().get(0); CuratorFramework curatorFrameworkClient = CuratorFrameworkFactory.newClient(zkUri.getHost() + ":2181", new RetryOneTime(5000)); curatorFrameworkClient.start(); log.info("CuratorFramework status {} ", curatorFrameworkClient.getState()); assertEquals("Connection to zk client ", STARTED, curatorFrameworkClient.getState()); log.info("Zookeeper Service URI : {} ", zkUri); log.info("ZkTest execution completed"); } }
CuratorFramework curator = CuratorFrameworkFactory.newClient("localhost:" + TestConstants.TEST_ZOOKEEPER_PORT, new RetryOneTime(100)); curator.start(); curator.blockUntilConnected();
TestingServer zkTestServer; @Before public void startZookeeper() throws Exception { zkTestServer = new TestingServer(2181); cli = CuratorFrameworkFactory.newClient(zkTestServer.getConnectString(), new RetryOneTime(2000)); } @After public void stopZookeeper() throws IOException { cli.close(); zkTestServer.stop(); }
public static CuratorFramework createClient(final TestingServer server) { return CuratorFrameworkFactory.newClient( server.getConnectString(), new RetryOneTime(1)); }
@Override protected void before() throws Throwable { testingServer = new TestingServer(true); zkClient = new CuratorZookeeperClient(testingServer.getConnectString(), 5000, 5000, null, new RetryOneTime(1000)); zkClient.start(); zkClient.blockUntilConnectedOrTimedOut(); }
@Override public void run(T configuration, Environment environment) throws Exception { CuratorConfiguration curatorConfiguration = ComposedConfigurationAccessor.access(configuration, environment, CuratorConfiguration.class); // TODO more config final CuratorFramework curator = CuratorFrameworkFactory.newClient(curatorConfiguration.getConnectionString(), new RetryOneTime(1)); Managed managed = new Managed() { @Override public void start() throws Exception { curator.start(); } @Override public void stop() throws Exception { CloseableUtils.closeQuietly(curator); } }; environment.lifecycle().manage(managed); SoaBundle.getFeatures(environment).putNamed(curator, CuratorFramework.class, curatorConfiguration.getCuratorName()); }