@Provides @LazySingleton public EnsembleProvider makeEnsembleProvider(CuratorConfig config, ExhibitorConfig exConfig) { if (exConfig.getHosts().isEmpty()) { return new FixedEnsembleProvider(config.getZkHosts()); } return new ExhibitorEnsembleProvider( new Exhibitors( exConfig.getHosts(), exConfig.getRestPort(), newBackupProvider(config.getZkHosts()) ), new DefaultExhibitorRestClient(exConfig.getUseSsl()), exConfig.getRestUriPath(), exConfig.getPollingMs(), new BoundedExponentialBackoffRetry(BASE_SLEEP_TIME_MS, MAX_SLEEP_TIME_MS, MAX_RETRIES) ) { @Override public void start() throws Exception { log.info("Poll the list of zookeeper servers for initial ensemble"); this.pollForInitialEnsemble(); super.start(); } }; }
.ensembleProvider(ensembleProvider) .sessionTimeoutMs(config.getZkSessionTimeoutMs()) .retryPolicy(new BoundedExponentialBackoffRetry(BASE_SLEEP_TIME_MS, MAX_SLEEP_TIME_MS, MAX_RETRIES)) .compressionProvider(new PotentiallyGzippedCompressionProvider(config.getEnableCompression())) .aclProvider(config.getEnableAcl() ? new SecuredACLProvider() : new DefaultACLProvider())
protected static CuratorFramework createNewClient() throws InterruptedException { CuratorFramework client = CuratorFrameworkFactory.newClient(testingServer.getConnectString(), new BoundedExponentialBackoffRetry(100, 1000, 3)); client.start(); return client; }
private RetryPolicy createBoundedExponentialBackoffRetry(int baseSleepTimeMs, int maxSleepTimeMs, int maxRetries) { RetryPolicy retryPolicy = new BoundedExponentialBackoffRetry(baseSleepTimeMs, maxSleepTimeMs, maxRetries); return retryPolicy; }
public RetryPolicy createBoundedExponentialBackoffRetry(int baseSleepTimeMs, int maxSleepTimeMs, int maxRetries) { RetryPolicy retryPolicy = new BoundedExponentialBackoffRetry(baseSleepTimeMs, maxSleepTimeMs, maxRetries); return retryPolicy; }
@Override RetryPolicy build(Config config) { return new BoundedExponentialBackoffRetry( getMillis(config, "baseSleepDuration"), getMillis(config, "maxSleepDuration"), config.getInt("maxRetries")); } },
public void init(Map<String, Object> conf, String participantId) { Preconditions.checkNotNull(participantId, "participantId can not be null"); Preconditions.checkNotNull(conf, "conf can not be null"); this.conf = conf; this.serverUrl = participantId; this.leaderLatchListener = createLeaderLatchListener(); LOG.info("Received configuration : [{}]", conf); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); String url = (String) conf.get(CONNECT_URL); String rootPrefix = (String) conf.get("root"); builder.connectString(url); builder.connectionTimeoutMs((Integer) conf.getOrDefault(CONNECTION_TIMEOUT_MS, DEFAULT_CONN_TIMOUT)); builder.sessionTimeoutMs((Integer) conf.getOrDefault(SESSION_TIMEOUT_MS, DEFAULT_SESSION_TIMEOUT)); builder.retryPolicy( new BoundedExponentialBackoffRetry( (Integer) conf.getOrDefault(RETRY_BASE_SLEEP_TIME_MS, DEFAULT_BASE_SLEEP_TIME), (Integer) conf.getOrDefault(RETRY_MAX_SLEEP_TIME_MS, DEFAULT_MAX_SLEEP_TIME), (Integer) conf.getOrDefault(RETRY_LIMIT, DEFAULT_RETRY_LIMIT) )); curatorFramework = builder.build(); leaderLatchPath = rootPrefix + LEADER_LOCK_NODE_PATH; leaderLatchRef = new AtomicReference<>(createLeaderLatch()); curatorFramework.start(); }
public void init(Map<String, Object> conf, String participantId) { Preconditions.checkNotNull(participantId, "participantId can not be null"); Preconditions.checkNotNull(conf, "conf can not be null"); this.conf = conf; this.serverUrl = participantId; this.leaderLatchListener = createLeaderLatchListener(); LOG.info("Received configuration : [{}]", conf); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); String url = (String) conf.get(CONNECT_URL); String rootPrefix = (String) conf.get("root"); builder.connectString(url); builder.connectionTimeoutMs((Integer) conf.getOrDefault(CONNECTION_TIMEOUT_MS, DEFAULT_CONN_TIMOUT)); builder.sessionTimeoutMs((Integer) conf.getOrDefault(SESSION_TIMEOUT_MS, DEFAULT_SESSION_TIMEOUT)); builder.retryPolicy( new BoundedExponentialBackoffRetry( (Integer) conf.getOrDefault(RETRY_BASE_SLEEP_TIME_MS, DEFAULT_BASE_SLEEP_TIME), (Integer) conf.getOrDefault(RETRY_MAX_SLEEP_TIME_MS, DEFAULT_MAX_SLEEP_TIME), (Integer) conf.getOrDefault(RETRY_LIMIT, DEFAULT_RETRY_LIMIT) )); curatorFramework = builder.build(); leaderLatchPath = rootPrefix + LEADER_LOCK_NODE_PATH; leaderLatchRef = new AtomicReference<>(createLeaderLatch()); curatorFramework.start(); }
public ZkRegistry(ZkConfig zkConfig) { zkClient = CuratorFrameworkFactory.builder().connectString(zkConfig.getZkAddress()) .sessionTimeoutMs(zkConfig.getZkTimeout()) .retryPolicy(new BoundedExponentialBackoffRetry(zkConfig.getBaseSleepTimeMs(), zkConfig.getMaxSleepTimeMs(), zkConfig.getMaxRetries())) .build(); zkClient.start(); }
public LockManager(@NonNull final Configuration conf) { this.lockConf = new LockManagerConfiguration(conf); this.isEnabled = lockConf.isEnabled(); this.lockMap = new ConcurrentHashMap(); if (this.isEnabled) { final ZookeeperConfiguration zkConf = new ZookeeperConfiguration(conf); this.client = Optional.of(CuratorFrameworkFactory.builder() .connectString(zkConf.getZkQuorum()) .retryPolicy(new BoundedExponentialBackoffRetry(1000, 5000, 3)) .namespace(lockConf.getZkBasePath()) .sessionTimeoutMs(lockConf.getZkSessionTimeoutMs()) .connectionTimeoutMs(lockConf.getZkConnectionTimeoutMs()) .build()); this.client.get().start(); } else { this.client = Optional.absent(); } }
.retryPolicy(new BoundedExponentialBackoffRetry(100, 1000, 10)) .threadFactory(new ThreadFactoryBuilder().setNameFormat("emo-zookeeper-%d").build()) .namespace(namespace)
zkRootClient = CuratorFrameworkFactory.builder() .connectString(server.getConnectString()) .retryPolicy(new BoundedExponentialBackoffRetry(10, 100, 7)) .build(); zkRootClient.start();
.connectString(config.getZkHosts()) .sessionTimeoutMs(config.getZkSessionTimeoutMs()) .retryPolicy(new BoundedExponentialBackoffRetry(1000, 45000, 30)) .compressionProvider(new PotentiallyGzippedCompressionProvider(config.enableCompression())) .build();
.ensembleProvider(ensembleProvider) .sessionTimeoutMs(config.getZkSessionTimeoutMs()) .retryPolicy(new BoundedExponentialBackoffRetry( BASE_SLEEP_TIME_MS, MAX_SLEEP_TIME_MS, MAX_RETRIES)) .compressionProvider(new PotentiallyGzippedCompressionProvider(config.getEnableCompression()))
@Provides @LazySingleton public EnsembleProvider makeEnsembleProvider(CuratorConfig config, ExhibitorConfig exConfig) { if (exConfig.getHosts().isEmpty()) { return new FixedEnsembleProvider(config.getZkHosts()); } return new ExhibitorEnsembleProvider( new Exhibitors( exConfig.getHosts(), exConfig.getRestPort(), newBackupProvider(config.getZkHosts()) ), new DefaultExhibitorRestClient(exConfig.getUseSsl()), exConfig.getRestUriPath(), exConfig.getPollingMs(), new BoundedExponentialBackoffRetry(BASE_SLEEP_TIME_MS, MAX_SLEEP_TIME_MS, MAX_RETRIES) ) { @Override public void start() throws Exception { log.info("Poll the list of zookeeper servers for initial ensemble"); this.pollForInitialEnsemble(); super.start(); } }; }
@Provides @LazySingleton public EnsembleProvider makeEnsembleProvider(CuratorConfig config, ExhibitorConfig exConfig) { if (exConfig.getHosts().isEmpty()) { return new FixedEnsembleProvider(config.getZkHosts()); } return new ExhibitorEnsembleProvider( new Exhibitors( exConfig.getHosts(), exConfig.getRestPort(), newBackupProvider(config.getZkHosts()) ), new DefaultExhibitorRestClient(exConfig.getUseSsl()), exConfig.getRestUriPath(), exConfig.getPollingMs(), new BoundedExponentialBackoffRetry(BASE_SLEEP_TIME_MS, MAX_SLEEP_TIME_MS, MAX_RETRIES) ) { @Override public void start() throws Exception { log.info("Poll the list of zookeeper servers for initial ensemble"); this.pollForInitialEnsemble(); super.start(); } }; }
/** * {@inheritDoc} */ @Override public long nextId(final String namespace) { final String[] paths = calcPathIdAndPathLock(namespace); final String pathId = paths[0]; final String pathLock = paths[1]; RetryPolicy retryPolicyMutex = new BoundedExponentialBackoffRetry(10, 1000, 5); PromotedToLock promotedToLock = PromotedToLock.builder().retryPolicy(retryPolicyMutex) .lockPath(pathLock).build(); RetryPolicy retryPolicyOptimistic = new RetryNTimes(3, 100); DistributedAtomicLong dal = new DistributedAtomicLong(curatorFramework, pathId, retryPolicyOptimistic, promotedToLock); semaphore.acquireUninterruptibly(); try { AtomicValue<Long> value = dal.increment(); if (value != null && value.succeeded()) { return value.postValue(); } return -1; } catch (Exception e) { throw e instanceof IdException ? (IdException) e : new IdException(e); } finally { semaphore.release(); } }
final String pathLock = paths[1]; RetryPolicy retryPolicyMutex = new BoundedExponentialBackoffRetry(10, 1000, 5); PromotedToLock promotedToLock = PromotedToLock.builder().retryPolicy(retryPolicyMutex) .lockPath(pathLock).build();
@Test public void testLockInfo() throws Exception { final String LOCK_INFO = "This is the information in the lock"; final String key1 = LockManager.getLockKey("test_lock", "key1"); try (final LockManager lockManager = new LockManager(conf)) { assertTrue(lockManager.lock(key1, LOCK_INFO)); final LockManagerConfiguration lockConf = new LockManagerConfiguration(conf); final ZookeeperConfiguration zkConf = new ZookeeperConfiguration(conf); final CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(zkConf.getZkQuorum()) .retryPolicy(new BoundedExponentialBackoffRetry(1000, 5000, 3)) .namespace(lockConf.getZkBasePath()) .sessionTimeoutMs(lockConf.getZkSessionTimeoutMs()) .connectionTimeoutMs(lockConf.getZkConnectionTimeoutMs()) .build(); client.start(); final String node = client.getChildren().forPath(key1).get(0); final String data = new String(client.getData().forPath(key1 + "/" + node)); assertEquals(LOCK_INFO, data); client.close(); lockManager.unlock(key1); } }
/** * {@inheritDoc} */ @Override public long currentId(final String namespace) { final String[] paths = calcPathIdAndPathLock(namespace); final String pathId = paths[0]; final String pathLock = paths[1]; RetryPolicy retryPolicyMutex = new BoundedExponentialBackoffRetry(10, 1000, 5); PromotedToLock promotedToLock = PromotedToLock.builder().retryPolicy(retryPolicyMutex) .lockPath(pathLock).build(); RetryPolicy retryPolicyOptimistic = new RetryNTimes(3, 100); DistributedAtomicLong dal = new DistributedAtomicLong(curatorFramework, pathId, retryPolicyOptimistic, promotedToLock); try { AtomicValue<Long> value = dal.get(); if (value != null && value.succeeded()) { return value.postValue(); } throw new IdException("Operation was not successful!"); } catch (Exception e) { throw e instanceof IdException ? (IdException) e : new IdException(e); } }