private void initCuratorClient(final RegistryCenterConfiguration config) { client.start(); try { if (!client.blockUntilConnected(config.getRetryIntervalMilliseconds() * config.getMaxRetries(), TimeUnit.MILLISECONDS)) { client.close(); throw new OperationTimeoutException(); } } catch (final InterruptedException | OperationTimeoutException ex) { CuratorZookeeperExceptionHandler.handleException(ex); } }
private static PathAndBytesable<?> createOrUpdate(CuratorFramework curator, String path) throws Exception { if (curator.checkExists().forPath(path) == null) { return curator.create().creatingParentsIfNeeded(); } else { return curator.setData(); } }
private boolean isAccumuloTable(SchemaTableName tableName) { try { String path = getTablePath(tableName); return curator.checkExists().forPath(path) != null && isAccumuloTable(curator.getData().forPath(path)); } catch (Exception e) { // Capture race condition between checkExists and getData if (e instanceof KeeperException && ((KeeperException) e).code() == NONODE) { return false; } throw new PrestoException(ZOOKEEPER_ERROR, "Error checking if path %s is an AccumuloTable object", e); } }
public List<String> list(String path) { path = "/" + path; try { if (_curator.checkExists().forPath(path) == null) { return new ArrayList<>(); } else { return _curator.getChildren().forPath(path); } } catch (Exception e) { throw new RuntimeException(e); } }
public List<TaskAnnouncement> getAnnouncements() throws Exception { final List<TaskAnnouncement> announcements = new ArrayList<>(); for (String id : curatorFramework.getChildren().forPath(getStatusPathForWorker())) { announcements.add( jsonMapper.readValue( curatorFramework.getData().forPath(getStatusPathForId(id)), TaskAnnouncement.class ) ); } return announcements; }
@Test public void testCreateHelixCluster() throws Exception { // This is tested here instead of in HelixUtilsTest to avoid setting up yet another testing ZooKeeper server. HelixUtils .createGobblinHelixCluster(this.config.getString(GobblinClusterConfigurationKeys.ZK_CONNECTION_STRING_KEY), this.config.getString(GobblinClusterConfigurationKeys.HELIX_CLUSTER_NAME_KEY)); // Assert to check if there is no pre-existing cluster Assert.assertEquals(this.curatorFramework.checkExists().forPath(String.format("/%s", this.helixClusterName)).getVersion(), 0); Assert.assertEquals(this.curatorFramework.checkExists().forPath(String.format("/%s/CONTROLLER", this.helixClusterName)).getVersion(), 0); }
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; } }
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(); } }
@BeforeClass public void setUp() throws Exception { TestingServer testingZKServer = this.closer.register(new TestingServer(-1)); LOG.info("Testing ZK Server listening on: " + testingZKServer.getConnectString()); CuratorFrameworkFactory.newClient(testingZKServer.getConnectString(), new RetryOneTime(2000))); this.curatorFramework.start(); Assert.assertNotNull(url, "Could not find resource " + url);
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); }
void mockWorkerRunningTask(final String workerId, final Task task) throws Exception { cf.delete().forPath(joiner.join(tasksPath, workerId, task.getId())); final String taskStatusPath = joiner.join(statusPath, workerId, task.getId()); TaskAnnouncement taskAnnouncement = TaskAnnouncement.create(task, TaskStatus.running(task.getId()), DUMMY_LOCATION); cf.create() .creatingParentsIfNeeded() .forPath(taskStatusPath, jsonMapper.writeValueAsBytes(taskAnnouncement)); Preconditions.checkNotNull( cf.checkExists().forPath(taskStatusPath), "Failed to write status on [%s]", taskStatusPath ); }
@Override public boolean checkExists(String path) { try { if (client.checkExists().forPath(path) != null) { return true; } } catch (Exception e) { } return false; }
@Before public void setup() throws Exception { testZkServer = new TestingServer(true); zookeeperUrl = testZkServer.getConnectString(); RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); client = CuratorFrameworkFactory.newClient(zookeeperUrl, retryPolicy); client.start(); discoverer = new ServiceDiscoverer(client, "/maas/discover"); discoverer.start(); }
public CuratorZookeeperClient(URL url) { super(url); try { int timeout = url.getParameter(Constants.TIMEOUT_KEY, 5000); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(url.getBackupAddress()) .retryPolicy(new RetryNTimes(1, 1000)) .connectionTimeoutMs(timeout); String authority = url.getAuthority(); if (authority != null && authority.length() > 0) { builder = builder.authorization("digest", authority.getBytes()); } client = builder.build(); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState state) { if (state == ConnectionState.LOST) { CuratorZookeeperClient.this.stateChanged(StateListener.DISCONNECTED); } else if (state == ConnectionState.CONNECTED) { CuratorZookeeperClient.this.stateChanged(StateListener.CONNECTED); } else if (state == ConnectionState.RECONNECTED) { CuratorZookeeperClient.this.stateChanged(StateListener.RECONNECTED); } } }); client.start(); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
@Before public void setup() { conf = new HiveConf(); lockObjData = new HiveLockObjectData("1", "10", "SHARED", "show tables", conf); hiveLock = new HiveLockObject(TABLE, lockObjData); zLock = new ZooKeeperHiveLock(TABLE_LOCK_PATH, hiveLock, HiveLockMode.SHARED); while (server == null) { try { server = new TestingServer(); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); client = builder.connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build(); client.start(); } catch (Exception e) { System.err.println("Getting bind exception - retrying to allocate server"); server = null; } } }
@Before public void setUp() throws Exception { setupServerAndCurator(); curator.start(); curator.blockUntilConnected(); }
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)) {
@PostConstruct public void init() { if (adapterCanalConfig.getZookeeperHosts() != null) { curator = CuratorFrameworkFactory.builder() .connectString(adapterCanalConfig.getZookeeperHosts()) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .sessionTimeoutMs(6000) .connectionTimeoutMs(3000) .namespace("canal-adapter") .build(); curator.start(); } }
@Override public void doClose() { client.close(); }
public void setData(String path, Object obj) { path = "/" + path; byte[] ser = _ser.serializeObject(obj); try { if (_curator.checkExists().forPath(path) != null) { _curator.setData().forPath(path, ser); } else { TransactionalState.createNode(_curator, path, ser, _zkAcls, CreateMode.PERSISTENT); } } catch (Exception e) { throw new RuntimeException(e); } }