use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project sofa-ark by alipay.
the class ZookeeperConfigActivator method start.
@Override
public void start(final PluginContext context) {
if (!enableZkServer) {
LOGGER.warn("config server is disabled.");
return;
}
String config = ArkConfigs.getStringValue(Constants.CONFIG_SERVER_ADDRESS);
RegistryConfig registryConfig = ZookeeperConfigurator.buildConfig(config);
String address = registryConfig.getAddress();
int idx = address.indexOf(Constants.ZOOKEEPER_CONTEXT_SPLIT);
if (idx != -1) {
rootPath = address.substring(idx);
if (!rootPath.endsWith(Constants.ZOOKEEPER_CONTEXT_SPLIT)) {
rootPath += Constants.ZOOKEEPER_CONTEXT_SPLIT;
}
address = address.substring(0, idx);
}
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFrameworkFactory.Builder zkClientBuilder = CuratorFrameworkFactory.builder().connectString(address).sessionTimeoutMs(3 * registryConfig.getConnectTimeout()).connectionTimeoutMs(registryConfig.getConnectTimeout()).canBeReadOnly(false).retryPolicy(retryPolicy).defaultData(null);
List<AuthInfo> authInfos = buildAuthInfo(registryConfig);
if (!authInfos.isEmpty()) {
zkClientBuilder = zkClientBuilder.aclProvider(getDefaultAclProvider()).authorization(authInfos);
}
zkClient = zkClientBuilder.build();
zkClient.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
LOGGER.info("Reconnect to zookeeper, re-register config resource.");
if (newState == ConnectionState.RECONNECTED) {
unSubscribeIpConfig();
registryResource(ipResourcePath, CreateMode.EPHEMERAL);
subscribeIpConfig();
}
}
});
zkClient.start();
registryResource(bizResourcePath, CreateMode.PERSISTENT);
registryResource(ipResourcePath, CreateMode.EPHEMERAL);
subscribeIpConfig();
subscribeBizConfig();
registerEventHandler(context);
}
use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project Saturn by vipshop.
the class ZookeeperRegistryCenterTest method testZkClientConfig.
@Test
public void testZkClientConfig() throws Exception {
// default settings
CuratorFramework client = initZk("127.0.0.1:" + PORT, "ut-ns");
assertEquals(20000L, client.getZookeeperClient().getConnectionTimeoutMs());
assertEquals(20000L, client.getZookeeperClient().getZooKeeper().getSessionTimeout());
ExponentialBackoffRetry retryPolicy = (ExponentialBackoffRetry) client.getZookeeperClient().getRetryPolicy();
assertEquals(3, retryPolicy.getN());
// set VIP_SATURN_ZK_CLIENT_CONNECTION_TIMEOUT = true
System.setProperty("VIP_SATURN_USE_UNSTABLE_NETWORK_SETTING", "true");
SystemEnvProperties.loadProperties();
client = initZk("127.0.0.1:" + PORT, "ut-ns");
assertEquals(40000L, client.getZookeeperClient().getConnectionTimeoutMs());
assertEquals(40000L, client.getZookeeperClient().getZooKeeper().getSessionTimeout());
retryPolicy = (ExponentialBackoffRetry) client.getZookeeperClient().getRetryPolicy();
assertEquals(9, retryPolicy.getN());
}
use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project Saturn by vipshop.
the class ZookeeperRegistryCenter method buildZkClient.
private CuratorFramework buildZkClient() {
if (zkConfig.isUseNestedZookeeper()) {
NestedZookeeperServers.getInstance().startServerIfNotStarted(zkConfig.getNestedPort(), zkConfig.getNestedDataDir());
}
Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getServerLists()).retryPolicy(new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMilliseconds(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepTimeMilliseconds())).namespace(zkConfig.getNamespace());
if (0 != zkConfig.getSessionTimeoutMilliseconds()) {
sessionTimeout = zkConfig.getSessionTimeoutMilliseconds();
} else {
sessionTimeout = calculateSessionTimeout();
}
builder.sessionTimeoutMs(sessionTimeout);
int connectionTimeout;
if (0 != zkConfig.getConnectionTimeoutMilliseconds()) {
connectionTimeout = zkConfig.getConnectionTimeoutMilliseconds();
} else {
connectionTimeout = calculateConnectionTimeout();
}
builder.connectionTimeoutMs(connectionTimeout);
if (!Strings.isNullOrEmpty(zkConfig.getDigest())) {
builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName(Constant.CHARSET_UTF8))).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;
}
});
}
LogUtils.info(log, LogEvents.ExecutorEvent.COMMON, "Saturn job: zookeeper registry center init, server lists is: {}, connection_timeout: {}, session_timeout: {}, retry_times: {}", zkConfig.getServerLists(), connectionTimeout, sessionTimeout, zkConfig.getMaxRetries());
return builder.build();
}
use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project Saturn by vipshop.
the class DemoJavaJob method main.
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181").namespace("mydomain").sessionTimeoutMs(10000).retryPolicy(retryPolicy).build();
client.start();
addJavaJob(client, "demoJavaJob");
System.out.println("done add a java-job.");
}
use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project Saturn by vipshop.
the class CuratorRepositoryImpl method connect.
@Override
public CuratorFramework connect(final String connectString, final String namespace, final String digest) {
Builder builder = CuratorFrameworkFactory.builder().connectString(connectString).sessionTimeoutMs(SESSION_TIMEOUT).connectionTimeoutMs(CONNECTION_TIMEOUT).retryPolicy(new ExponentialBackoffRetry(1000, 3, 3000));
if (namespace != null) {
builder.namespace(namespace);
}
if (!Strings.isNullOrEmpty(digest)) {
builder.authorization("digest", digest.getBytes(Charset.forName("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;
}
});
}
CuratorFramework client = builder.build();
client.start();
boolean established = false;
try {
established = client.blockUntilConnected(WAITING_SECONDS, TimeUnit.SECONDS);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
}
if (established) {
return client;
}
CloseableUtils.closeQuietly(client);
return null;
}
Aggregations