@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(); } }
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); } }
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); } }
builder.connectionTimeoutMs(zkConfig.getConnectionTimeout());
/** * Return a connection ot the ZK instance (creating it if needed) * * @return connection * @throws IOException errors */ public synchronized CuratorFramework getLocalConnection() throws IOException { if ( localConnection == null ) { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString("localhost:" + configManager.getConfig().getInt(IntConfigs.CLIENT_PORT)) .sessionTimeoutMs(arguments.connectionTimeOutMs * 10) .connectionTimeoutMs(arguments.connectionTimeOutMs) .retryPolicy(new ExponentialBackoffRetry(1000, 3)); if ( arguments.aclProvider != null ) { builder = builder.aclProvider(arguments.aclProvider); } localConnection = builder.build(); localConnection.start(); } return localConnection; }
builder.connectionTimeoutMs(zkConfig.getConnectionTimeoutMilliseconds());
public CuratorZkClient(Config config) { String registryAddress = NodeRegistryUtils.getRealRegistryAddress(config.getRegistryAddress()); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(registryAddress) .retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000)) .connectionTimeoutMs(5000); client = builder.build(); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { public void stateChanged(CuratorFramework client, ConnectionState state) { if (state == ConnectionState.LOST) { CuratorZkClient.this.stateChanged(StateListener.DISCONNECTED); } else if (state == ConnectionState.CONNECTED) { CuratorZkClient.this.stateChanged(StateListener.CONNECTED); } else if (state == ConnectionState.RECONNECTED) { CuratorZkClient.this.stateChanged(StateListener.RECONNECTED); } else if (state == ConnectionState.SUSPENDED) { CuratorZkClient.this.stateChanged(StateListener.DISCONNECTED); } } }); zkSerializer = new SerializableSerializer(); client.start(); }
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(); } }
private CuratorFramework getZookeeperClient(Configuration conf, String namespace, ACLProvider zooKeeperAclProvider) { String zkEnsemble = getQuorumServers(conf); int sessionTimeout = (int) HiveConf.getTimeVar(conf, ConfVars.HIVE_ZOOKEEPER_SESSION_TIMEOUT, TimeUnit.MILLISECONDS); int connectionTimeout = (int) HiveConf.getTimeVar(conf, ConfVars.HIVE_ZOOKEEPER_CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS); int baseSleepTime = (int) HiveConf.getTimeVar(conf, ConfVars.HIVE_ZOOKEEPER_CONNECTION_BASESLEEPTIME, TimeUnit.MILLISECONDS); int maxRetries = HiveConf.getIntVar(conf, ConfVars.HIVE_ZOOKEEPER_CONNECTION_MAX_RETRIES); LOG.info("Creating curator client with connectString: {} sessionTimeoutMs: {} connectionTimeoutMs: {}" + " namespace: {} exponentialBackoff - sleepTime: {} maxRetries: {}", zkEnsemble, sessionTimeout, connectionTimeout, namespace, baseSleepTime, maxRetries); // Create a CuratorFramework instance to be used as the ZooKeeper client // Use the zooKeeperAclProvider to create appropriate ACLs return CuratorFrameworkFactory.builder() .connectString(zkEnsemble) .sessionTimeoutMs(sessionTimeout) .connectionTimeoutMs(connectionTimeout) .aclProvider(zooKeeperAclProvider) .namespace(namespace) .retryPolicy(new ExponentialBackoffRetry(baseSleepTime, maxRetries)) .build(); }
/** * Returns a newly-created {@link ZooKeeperUpdatingListener} instance that registers the server to * ZooKeeper when the server starts. */ public ZooKeeperUpdatingListener build() { final boolean internalClient; if (client == null) { client = CuratorFrameworkFactory.builder() .connectString(connectionStr) .retryPolicy(ZooKeeperDefaults.DEFAULT_RETRY_POLICY) .connectionTimeoutMs(connectTimeoutMillis) .sessionTimeoutMs(sessionTimeoutMillis) .build(); internalClient = true; } else { internalClient = false; } return new ZooKeeperUpdatingListener(client, zNodePath, nodeValueCodec, endpoint, internalClient); } }
public CuratorZkClient(Config config) { String registryAddress = NodeRegistryUtils.getRealRegistryAddress(config.getRegistryAddress()); CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(registryAddress) .retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000)) .connectionTimeoutMs(5000); client = builder.build(); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { public void stateChanged(CuratorFramework client, ConnectionState state) { if (state == ConnectionState.LOST) { CuratorZkClient.this.stateChanged(StateListener.DISCONNECTED); } else if (state == ConnectionState.CONNECTED) { CuratorZkClient.this.stateChanged(StateListener.CONNECTED); } else if (state == ConnectionState.RECONNECTED) { CuratorZkClient.this.stateChanged(StateListener.RECONNECTED); } else if (state == ConnectionState.SUSPENDED) { CuratorZkClient.this.stateChanged(StateListener.DISCONNECTED); } } }); zkSerializer = new SerializableSerializer(); client.start(); }
@Override public CuratorFramework newClient(String connectString, int sessionTimeoutMs, int connectionTimeoutMs, RetryPolicy retryPolicy, final ACLProvider aclProvider, final List<AuthInfo> authorization) { final Builder builder = CuratorFrameworkFactory.builder() .connectString(connectString) .sessionTimeoutMs(sessionTimeoutMs) .connectionTimeoutMs(connectionTimeoutMs) .retryPolicy(retryPolicy); if (aclProvider != null) { builder.aclProvider(aclProvider); } if (authorization != null && !authorization.isEmpty()) { builder.authorization(authorization); } return builder.build(); } }
public ZKClusterCoordinator(String connect, String zkRoot, String clusterId, int retryCount, int retryDelayMs, int connectTimeoutMs) throws IOException { logger.debug("ZK connect: " + connect + ", zkRoot: " + zkRoot + ", clusterId: " + clusterId); this.serviceName = clusterId; RetryPolicy rp = new RetryNTimes(retryCount, retryDelayMs); curator = CuratorFrameworkFactory.builder().namespace(zkRoot) .connectionTimeoutMs(connectTimeoutMs).retryPolicy(rp) .connectString(connect).build(); curator.getConnectionStateListenable() .addListener(new InitialConnectionListener()); curator.start(); discovery = newDiscovery(); factory = CachingTransientStoreFactory .of(new ZkTransientStoreFactory(curator)); }
private static CuratorFramework getZKClient(KylinConfig config) { CuratorFramework zkClient = CACHE.get(config); if (zkClient == null) { synchronized (ZookeeperDistributedLock.class) { zkClient = CACHE.get(config); if (zkClient == null) { RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); String zkConnectString = getZKConnectString(config); ZookeeperAclBuilder zookeeperAclBuilder = new ZookeeperAclBuilder().invoke(); zkClient = zookeeperAclBuilder.setZKAclBuilder(CuratorFrameworkFactory.builder()).connectString(zkConnectString).sessionTimeoutMs(120000).connectionTimeoutMs(15000).retryPolicy(retryPolicy).build(); zkClient.start(); CACHE.put(config, zkClient); if (CACHE.size() > 1) { logger.warn("More than one singleton exist"); } } } } return zkClient; }
private CuratorFramework createClient() { // Create a new client because we don't want to try indefinitely for this to occur. final RetryPolicy retryPolicy = new RetryNTimes(1, 100); final CuratorACLProviderFactory aclProviderFactory = new CuratorACLProviderFactory(); final CuratorFramework client = CuratorFrameworkFactory.builder() .connectString(zkConfig.getConnectString()) .sessionTimeoutMs(zkConfig.getSessionTimeoutMillis()) .connectionTimeoutMs(zkConfig.getConnectionTimeoutMillis()) .retryPolicy(retryPolicy) .aclProvider(aclProviderFactory.create(zkConfig)) .defaultData(new byte[0]) .build(); client.start(); return client; }
private CuratorFramework buildCuratorClient(final RegistryCenterConfiguration config) { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder() .connectString(config.getServerLists()) .retryPolicy(new ExponentialBackoffRetry(config.getRetryIntervalMilliseconds(), config.getMaxRetries(), config.getRetryIntervalMilliseconds() * config.getMaxRetries())) .namespace(config.getNamespace()); if (0 != config.getTimeToLiveSeconds()) { builder.sessionTimeoutMs(config.getTimeToLiveSeconds() * 1000); } if (0 != config.getOperationTimeoutMilliseconds()) { builder.connectionTimeoutMs(config.getOperationTimeoutMilliseconds()); } if (!Strings.isNullOrEmpty(config.getDigest())) { builder.authorization("digest", config.getDigest().getBytes(Charsets.UTF_8)) .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; } }); } return builder.build(); }
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; }
protected static void setupBuilder(CuratorFrameworkFactory.Builder builder, String zkStr, Map conf, ZookeeperAuthInfo auth) { builder.connectString(zkStr) .connectionTimeoutMs(Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_CONNECTION_TIMEOUT))) .sessionTimeoutMs(Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_SESSION_TIMEOUT))) .retryPolicy( new StormBoundedExponentialBackoffRetry(Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_INTERVAL)), Utils.getInt(conf .get(Config.STORM_ZOOKEEPER_RETRY_INTERVAL_CEILING)), Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_TIMES)))); if (auth != null && auth.scheme != null && auth.payload != null) { builder = builder.authorization(auth.scheme, auth.payload); } }
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(); }
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(); }