Java Code Examples for org.apache.curator.retry.ExponentialBackoffRetry

The following are top voted examples for showing how to use org.apache.curator.retry.ExponentialBackoffRetry. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: fastmq   File: LogStorageImplTest.java   View source code 8 votes vote down vote up
@Before
public void setUp() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    zookeeper = new ZooKeeper("127.0.0.1:2181", 10000, event -> {
        if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
            System.out.println("Zookeeper connected.");
        } else {
            throw new RuntimeException("Error connecting to zookeeper");
        }
        latch.countDown();
    });
    latch.await();
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));
    curatorFramework.start();
    AsyncCuratorFramework asyncCuratorFramework = AsyncCuratorFramework.wrap(curatorFramework);
    logInfoStorage = new LogInfoStorageImpl(asyncCuratorFramework);
}
 
Example 2
Project: mycat-src-1.6.1-RELEASE   File: ZktoXmlMain.java   View source code 7 votes vote down vote up
private static CuratorFramework buildConnection(String url) {
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));

    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework.usingNamespace("");
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }

    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 3
Project: Mastering-Mesos   File: SingularityCuratorProvider.java   View source code 7 votes vote down vote up
@Inject
public SingularityCuratorProvider(final SingularityConfiguration configuration, final Set<ConnectionStateListener> connectionStateListeners) {

  checkNotNull(configuration, "configuration is null");
  checkNotNull(connectionStateListeners, "connectionStateListeners is null");

  ZooKeeperConfiguration zookeeperConfig = configuration.getZooKeeperConfiguration();

  this.curatorFramework = CuratorFrameworkFactory.builder()
      .defaultData(null)
      .sessionTimeoutMs(zookeeperConfig.getSessionTimeoutMillis())
      .connectionTimeoutMs(zookeeperConfig.getConnectTimeoutMillis())
      .connectString(zookeeperConfig.getQuorum())
      .retryPolicy(new ExponentialBackoffRetry(zookeeperConfig.getRetryBaseSleepTimeMilliseconds(), zookeeperConfig.getRetryMaxTries()))
      .namespace(zookeeperConfig.getZkNamespace()).build();

  for (ConnectionStateListener connectionStateListener : connectionStateListeners) {
    curatorFramework.getConnectionStateListenable().addListener(connectionStateListener);
  }
}
 
Example 4
Project: simpleJobScheduler   File: Application.java   View source code 7 votes vote down vote up
@Override
public void run(String... strings) throws Exception {
    client = CuratorFrameworkFactory.newClient(zookeeperConnString,
            new ExponentialBackoffRetry(1000, Integer.MAX_VALUE));
    client.start();
    client.getZookeeperClient().blockUntilConnectedOrTimedOut();

    leaderLatch = new LeaderLatch(client, "/http-job-scheduler/leader",
            ManagementFactory.getRuntimeMXBean().getName());

    leaderLatch.addListener(new LeaderLatchListener() {
        @Override
        public void isLeader() {
            setMaster(true);
            masterJobScheduler.resume();
        }

        @Override
        public void notLeader() {
            setMaster(false);
            masterJobScheduler.pause();
        }
    });
    leaderLatch.start();
}
 
Example 5
Project: fastmq   File: ZkOffsetStorageImplTest.java   View source code 6 votes vote down vote up
@Before
public void setUp() throws Exception {
    Configurator
        .initialize("FastMQ", Thread.currentThread().getContextClassLoader(), "log4j2.xml");
    Log log = new Log();
    LogSegment logSegment = new LogSegment();
    logSegment.setLedgerId(ledgerId);
    logSegment.setTimestamp(System.currentTimeMillis());
    log.setSegments(Collections.singletonList(logSegment));
    when(logInfoStorage.getLogInfo(any())).thenReturn(log);

    CuratorFramework curatorFramework = CuratorFrameworkFactory
        .newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));
    curatorFramework.start();
    asyncCuratorFramework = AsyncCuratorFramework.wrap(curatorFramework);
    offsetStorage = new ZkOffsetStorageImpl(logInfoStorage, asyncCuratorFramework);
}
 
Example 6
Project: Juice   File: CuratorUtils.java   View source code 6 votes vote down vote up
public CuratorUtils(String connectString, Host host) {
    this.host = host;

    client = CuratorFrameworkFactory.builder()
            .connectString(connectString)
            .sessionTimeoutMs(10000)
            .connectionTimeoutMs(10000)
            .retryPolicy(new ExponentialBackoffRetry(1000, 3))
            .build();
    client.start();
    try {
        path = generatePath();
        if (null == path) {
            log.error("init curator failed due to path is null!");
            throw new DriverException("init curator failed due to path is null, servie will down");
        }
    } catch (Exception e) {
        log.error("CuratorUtils construct failed, service will down!");
        client.close();
        System.exit(-1);
    }
}
 
Example 7
Project: stroom-stats   File: ServiceDiscoveryCuratorFrameworkProvider.java   View source code 6 votes vote down vote up
@Override
public CuratorFramework get() {
    String quorum = zookeeperConfig.getQuorum();
    String serviceDiscoveryPath = zookeeperConfig.getServiceDiscoveryPath();
    String connectionString = quorum + (serviceDiscoveryPath == null ? "" : serviceDiscoveryPath);

    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);

    LOGGER.info("Initiating Curator connection to Zookeeper using: [{}]", connectionString);
    // Use chroot so all subsequent paths are below /stroom-services to avoid conflicts with hbase/zookeeper/kafka etc.
    CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
    client.start();

    try {
        //Ensure the chrooted path for stroom-services exists
        Stat stat = client.checkExists().forPath("/");
        if (stat == null) {
            LOGGER.info("Creating chroot-ed root node inside " + serviceDiscoveryPath);
            client.create().forPath("/");
        }
    } catch (Exception e) {
        throw new RuntimeException("Error connecting to zookeeper using connection String: " + connectionString, e);
    }
    return client;
}
 
Example 8
Project: stroom-stats   File: StroomPropertyServiceCuratorFrameworkProvider.java   View source code 6 votes vote down vote up
@Override
public CuratorFramework get() {
    String quorum = zookeeperConfig.getQuorum();
    String statsPath = zookeeperConfig.getPropertyServicePath();
    String connectionString = quorum + (statsPath == null ? "" : statsPath);

    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);

    LOGGER.info("Initiating Curator connection to Zookeeper using: [{}]", connectionString);
    // Use chroot so all subsequent paths are below /stroom-stats to avoid conflicts with hbase/zookeeper/kafka etc.
    CuratorFramework client = CuratorFrameworkFactory.newClient(connectionString, retryPolicy);
    client.start();

    try {
        // Ensure the chrooted root path exists (i.e. /stroom-stats)
        Stat stat = client.checkExists().forPath("/");
        if (stat == null) {
            LOGGER.info("Creating chroot-ed root node inside " + statsPath);
            client.create().forPath("/");
        }
    } catch (Exception e) {
        throw new RuntimeException("Error connecting to zookeeper using connection String: " + connectionString, e);
    }
    return client;
}
 
Example 9
Project: mycat-src-1.6.1-RELEASE   File: XmltoZkMain.java   View source code 6 votes vote down vote up
private static CuratorFramework buildConnection(String url) {
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));

    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework.usingNamespace("");
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }

    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 10
Project: mycat-src-1.6.1-RELEASE   File: ZKUtils.java   View source code 6 votes vote down vote up
private static CuratorFramework createConnection() {
       String url= ZkConfig.getInstance().getZkURL();

    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));

    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework;
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }

    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 11
Project: Cobweb   File: SelfTest.java   View source code 6 votes vote down vote up
/**
 * 检查ZooKeeper的连接状态和它的Znode目录树
 *
 * @param
 * @return
 */
public static CuratorFramework checkAndGetZK() {
    CuratorFramework client;
    try {
        RetryPolicy retryPolicy =
                new ExponentialBackoffRetry(configuration.ZK_RETRY_INTERVAL, configuration.ZK_RETRY_TIMES);
        client = CuratorFrameworkFactory
                .newClient(configuration.ZK_CONNECT_STRING
                        , configuration.ZK_SESSION_TIMEOUT, configuration.ZK_INIT_TIMEOUT, retryPolicy);
        client.start();
        client.checkExists().forPath(ZNodeStaticSetting.TASKS_PATH);
        client.checkExists().forPath(ZNodeStaticSetting.MANAGERS_PATH);
        client.checkExists().forPath(ZNodeStaticSetting.WORKERS_PATH);
        client.checkExists().forPath(ZNodeStaticSetting.FILTERS_ROOT);
    } catch (Throwable e) {
        client = null;
        logger.error(e.getMessage());
    }
    return client;
}
 
Example 12
Project: dble   File: ZKUtils.java   View source code 6 votes vote down vote up
private static CuratorFramework createConnection() {
    String url = ZkConfig.getInstance().getZkURL();
    CuratorFramework framework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));
    // start connection
    framework.start();
    // wait 3 second to establish connect
    try {
        framework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (framework.getZookeeperClient().isConnected()) {
            LOGGER.info("CuratorFramework createConnection success");
            return framework;
        }
    } catch (InterruptedException ignored) {
        LOGGER.info("CuratorFramework createConnection error", ignored);
        Thread.currentThread().interrupt();
    }
    // fail situation
    framework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
 
Example 13
Project: Distributed-Kit   File: ZkReentrantLockTemplateTest.java   View source code 6 votes vote down vote up
public static void main(String[] args){
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
    client.start();

    final ZkDistributedLockTemplate template=new ZkDistributedLockTemplate(client);//本类多线程安全,可通过spring注入
    template.execute("订单流水号", 5000, new Callback() {
        @Override
        public Object onGetLock() throws InterruptedException {
            //TODO 获得锁后要做的事
            return null;
        }

        @Override
        public Object onTimeout() throws InterruptedException {
            //TODO 获得锁超时后要做的事
            return null;
        }
    });
}
 
Example 14
Project: distributed-search-cache   File: CacheClusterViewer.java   View source code 6 votes vote down vote up
public CacheClusterViewer(Config config) throws RuntimeException {
    try {
        if (config == null) {
            config = ConfigFactory.load();
        }

        this.zookeeperConnectionUrl = config.getString("zookeeper.connection_url");

        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        zkClient = CuratorFrameworkFactory.newClient(zookeeperConnectionUrl, retryPolicy);
        zkClient.start();

        clusterGlobalLock = new InterProcessReadWriteLock(zkClient, Constants.CACHE_CLUSTER_PATH);

        cacheCluster = doGetCacheClusterMeta();

        monitorThread = new Thread(new ClusterStatusMonitor());
        monitorThread.start();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 15
Project: nnproxy   File: MountsManager.java   View source code 6 votes vote down vote up
@Override
protected void serviceInit(Configuration conf) throws Exception {
    super.serviceInit(conf);
    String zkConnectString = conf.get(ProxyConfig.MOUNT_TABLE_ZK_QUORUM);
    zkMountTablePath = conf.get(ProxyConfig.MOUNT_TABLE_ZK_PATH);
    int sessionTimeout = conf.getInt(ProxyConfig.MOUNT_TABLE_ZK_SESSION_TIMEOUT,
            ProxyConfig.MOUNT_TABLE_ZK_SESSION_TIMEOUT_DEFAULT);
    int connectionTimeout = conf.getInt(ProxyConfig.MOUNT_TABLE_ZK_CONNECTION_TIMEOUT,
            ProxyConfig.MOUNT_TABLE_ZK_CONNECTION_TIMEOUT_DEFAULT);
    int maxRetries = conf.getInt(ProxyConfig.MOUNT_TABLE_ZK_MAX_RETRIES,
            ProxyConfig.MOUNT_TABLE_ZK_MAX_RETRIES_DEFAULT);
    int retryBaseSleep = conf.getInt(ProxyConfig.MOUNT_TABLE_ZK_RETRY_BASE_SLEEP,
            ProxyConfig.MOUNT_TABLE_ZK_RETRY_BASE_SLEEP_DEFAULT);
    framework = CuratorFrameworkFactory.newClient(
            zkConnectString, sessionTimeout, connectionTimeout,
            new ExponentialBackoffRetry(retryBaseSleep, maxRetries));
    installed = false;
}
 
Example 16
Project: dmaap-framework   File: DMaaPCuratorFactory.java   View source code 6 votes vote down vote up
/**
 * 
 * method provide CuratorFramework object
 * 
 * @param settings
 * @return
 * 
 * 
 * 
 */
public static CuratorFramework getCurator(rrNvReadable settings) {
	String Setting_ZkConfigDbServers =com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, CambriaConstants.kSetting_ZkConfigDbServers);
	 
	if(null==Setting_ZkConfigDbServers)
		 Setting_ZkConfigDbServers =CambriaConstants.kDefault_ZkConfigDbServers; 
	
	String strSetting_ZkSessionTimeoutMs = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, CambriaConstants.kSetting_ZkSessionTimeoutMs);
	if (strSetting_ZkSessionTimeoutMs==null) strSetting_ZkSessionTimeoutMs = CambriaConstants.kDefault_ZkSessionTimeoutMs+"";
	int Setting_ZkSessionTimeoutMs = Integer.parseInt(strSetting_ZkSessionTimeoutMs);
	
	String str_ZkConnectionTimeoutMs = com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop, CambriaConstants.kSetting_ZkSessionTimeoutMs);
	if (str_ZkConnectionTimeoutMs==null) str_ZkConnectionTimeoutMs = CambriaConstants.kDefault_ZkConnectionTimeoutMs+"";
	int setting_ZkConnectionTimeoutMs = Integer.parseInt(str_ZkConnectionTimeoutMs);
	
	
	CuratorFramework curator = CuratorFrameworkFactory.newClient(
			Setting_ZkConfigDbServers,Setting_ZkSessionTimeoutMs,setting_ZkConnectionTimeoutMs
			,new ExponentialBackoffRetry(1000, 5));
	return curator;
}
 
Example 17
Project: Hvalspik   File: CuratorWait.java   View source code 6 votes vote down vote up
@Override
public WaitResult wait(DockerFacade dockerClient, Container container) {
  return new PortWait().wait(ZOOKEEPER_PORT, dockerClient, container, (ipAddress, externalPort) -> {
    String zookeeperConnectionString = String.format("%s:%s", ipAddress, externalPort);
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperConnectionString, retryPolicy);
    client.start();
    try {
      client.blockUntilConnected();
      CuratorFrameworkState status = client.getState();
      if (!status.equals(CuratorFrameworkState.STARTED)) {
        return WaitResult.failure("Status [%s] does not match expected [STARTED]", status);
      }
      return WaitResult.success();
    } catch (InterruptedException e) {
      throw new RuntimeException("Could not connect to Zookeeper", e);
    } finally {
      client.close();
    }

  });
}
 
Example 18
Project: elastic-config   File: ElasticConfigInitialScript.java   View source code 6 votes vote down vote up
public static void main(String[] args) throws Exception {

		@Cleanup
		CuratorFramework client = CuratorFrameworkFactory.newClient(ZK,
				new ExponentialBackoffRetry(100, 2));
		client.start();

		for (Entry<String, String> item : data.entrySet()) {
			Stat stat = client.checkExists().forPath(item.getKey());
			if (stat == null) {
				client.create().creatingParentsIfNeeded()
						.forPath(item.getKey(), item.getValue().getBytes());
			}
		}

		client.setData().forPath("/github/projectname",
				sha1Digest("123456").getBytes());

	}
 
Example 19
Project: replicator   File: ZookeeperCoordinator.java   View source code 6 votes vote down vote up
/**
 * Zookeeper-based Coordinator implementation.
 *
 * @param configuration Replicator configuration
 */
public ZookeeperCoordinator(Configuration configuration)  {
    this.configuration = configuration;
    this.checkPointPath = String.format("%s/checkpoint", configuration.getZookeeperPath());

    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);

    String cluster = configuration.getZookeeperQuorum();

    client = CuratorFrameworkFactory.newClient(cluster, retryPolicy);
    client.start();

    try {
        client.createContainers(configuration.getZookeeperPath());
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(String.format(
            "Failed to create Zookeeper Coordinator container at path: %s (Because of: %s)",
            configuration.getZookeeperPath(),
            e.getMessage()
        ));
    }
}
 
Example 20
Project: vertx-service-discovery   File: ZookeeperBackendService.java   View source code 6 votes vote down vote up
@Override
public void init(Vertx vertx, JsonObject configuration) {
  this.vertx = vertx;
  String connection = Objects.requireNonNull(configuration.getString("connection"));
  int maxRetries = configuration.getInteger("maxRetries", 3);
  int baseGraceBetweenRetries = configuration
      .getInteger("baseSleepTimeBetweenRetries", 1000);
  canBeReadOnly = configuration.getBoolean("canBeReadOnly", false);
  connectionTimeoutMs = configuration.getInteger("connectionTimeoutMs", 1000);
  basePath = configuration.getString("basePath", "/services");
  ephemeral = configuration.getBoolean("ephemeral", false);
  guaranteed = configuration.getBoolean("guaranteed", false);

  client = CuratorFrameworkFactory.builder()
      .canBeReadOnly(canBeReadOnly)
      .connectString(connection)
      .connectionTimeoutMs(connectionTimeoutMs)
      .retryPolicy(new ExponentialBackoffRetry(baseGraceBetweenRetries, maxRetries))
      .build();
  client.getConnectionStateListenable().addListener(this);
  client.start();
}
 
Example 21
Project: maelstrom   File: OffsetManager.java   View source code 6 votes vote down vote up
public static CuratorFramework createCurator(final String zookeeper) {
    CuratorFramework curator = CuratorFrameworkFactory.builder().
            connectString(zookeeper).
            sessionTimeoutMs(120000).
            connectionTimeoutMs(120000).
            retryPolicy(new ExponentialBackoffRetry(1000, 29))
            .threadFactory(new ThreadFactory() {
                @Override
                public Thread newThread(@SuppressWarnings("NullableProblems") Runnable r) {
                    Thread t = new Thread(r);
                    t.setDaemon(true);
                    return t;
                }
            }).build();
    curator.start();

    return curator;
}
 
Example 22
Project: ibole-microservice   File: ZkServiceDiscovery.java   View source code 6 votes vote down vote up
@Override
public void start() {
  try {
    // 1000ms - initial amount of time to wait between retries
    // 3 times - max number of times to retry
    client = CuratorFrameworkFactory.newClient(getIdentifier().getConnectionString(),
        new ExponentialBackoffRetry(1000, 3));
    client.start();
    client.getZookeeperClient().blockUntilConnectedOrTimedOut();
    cache = TreeCache.newBuilder(client, buildBasePath()).build();
    serializer = new JsonInstanceSerializer<HostMetadata>(HostMetadata.class);

  } catch (Exception e) {
    logger.error("Service registry start error for server identifier '{}' !",
        getIdentifier().getConnectionString(), e);
    throw new ServiceDiscoveryException(e);
  }
}
 
Example 23
Project: HeliosStreams   File: AdminFinder.java   View source code 6 votes vote down vote up
/**
 * Creates a new AdminFinder
 * @param args the command line args
 */
public AdminFinder(final String[] args) {
	zookeepConnect = findArg(ZOOKEEP_CONNECT_ARG, ConfigurationHelper.getSystemThenEnvProperty("zookeep.connect", DEFAULT_ZOOKEEP_CONNECT), args);
	zookeepTimeout = findArg(ZOOKEEP_TIMEOUT_ARG, DEFAULT_ZOOKEEP_TIMEOUT, args);
	connectTimeout = findArg(CONNECT_TIMEOUT_ARG, DEFAULT_CONNECT_TIMEOUT, args);
	retryPauseTime = findArg(RETRY_ARG, DEFAULT_RETRY, args);
	cf = CuratorFrameworkFactory.builder()
			.canBeReadOnly(false)
			.connectionTimeoutMs(connectTimeout)
			.sessionTimeoutMs(zookeepTimeout)
			.connectString(zookeepConnect)
			.retryPolicy(new ExponentialBackoffRetry(5000, 200))
			//.retryPolicy(this)
			.threadFactory(threadFactory)
			.build();	
	cf.getConnectionStateListenable().addListener(this);		
}
 
Example 24
Project: easycode   File: ZooKeeperTest.java   View source code 6 votes vote down vote up
public static void main(String[] args) {
	
	String connectString = "192.168.1.101:2181,192.168.1.102:2181,192.168.1.103:2181/conf/easycode/auth";
	CuratorFramework client = null;
	RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
	try {
		client = CuratorFrameworkFactory.newClient(connectString, retryPolicy);
		client.start();
		ZooKeeperTest test = new ZooKeeperTest(client);
		
		test.addCfgFile2LatentIoNode("", Envs.DEV, new String[] {
				"zk-test.properties"
		});
		
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		CloseableUtils.closeQuietly(client);
	}
}
 
Example 25
Project: flink   File: ZookeeperOffsetHandler.java   View source code 6 votes vote down vote up
public ZookeeperOffsetHandler(Properties props) {
	this.groupId = props.getProperty(ConsumerConfig.GROUP_ID_CONFIG);
	if (this.groupId == null) {
		throw new IllegalArgumentException("Required property '"
				+ ConsumerConfig.GROUP_ID_CONFIG + "' has not been set");
	}
	
	String zkConnect = props.getProperty("zookeeper.connect");
	if (zkConnect == null) {
		throw new IllegalArgumentException("Required property 'zookeeper.connect' has not been set");
	}

	// we use Curator's default timeouts
	int sessionTimeoutMs =  Integer.valueOf(props.getProperty("zookeeper.session.timeout.ms", "60000"));
	int connectionTimeoutMs = Integer.valueOf(props.getProperty("zookeeper.connection.timeout.ms", "15000"));
	
	// undocumented config options allowing users to configure the retry policy. (they are "flink." prefixed as they are no official kafka configs)
	int backoffBaseSleepTime = Integer.valueOf(props.getProperty("flink.zookeeper.base-sleep-time.ms", "100"));
	int backoffMaxRetries =  Integer.valueOf(props.getProperty("flink.zookeeper.max-retries", "10"));
	
	RetryPolicy retryPolicy = new ExponentialBackoffRetry(backoffBaseSleepTime, backoffMaxRetries);
	curatorClient = CuratorFrameworkFactory.newClient(zkConnect, sessionTimeoutMs, connectionTimeoutMs, retryPolicy);
	curatorClient.start();
}
 
Example 26
Project: Cluster-Partition-Rebalancer-For-Kafka   File: ZookeeperBackedAdoptionLogicImpl.java   View source code 6 votes vote down vote up
public ZookeeperBackedAdoptionLogicImpl() throws Exception {
    rand = new Random(System.currentTimeMillis());

    props = new Properties();
    props.load(new FileReader("settings.properties"));

    String zkConnString = Utils.getZookeeperConnectionString(2181);
    logger.info("Zookeeper connection string is {}", zkConnString);

    client = CuratorFrameworkFactory.newClient(
            zkConnString,
            new ExponentialBackoffRetry(1000, 30)
    );
    client.start();
    try {
        client.create().creatingParentsIfNeeded().forPath(ADOPTION_ADS_ROOT_ZNODE);
        client.create().creatingParentsIfNeeded().forPath(ADOPTION_ADS_LOCK_ROOT_ZNODE);
    }
    catch(KeeperException.NodeExistsException nodeExistsEx) {
        logger.debug("Ignoring Node Exists Exception for {}", ADOPTION_ADS_ROOT_ZNODE);
    }

    sleepIntervalInMs = Long.parseLong(props.getProperty("sleep.interval.ms", "120000"));
}
 
Example 27
Project: bd-codes   File: CuratorTestingServer.java   View source code 6 votes vote down vote up
/**
 * 测试入口
 */
public static void main(String[] args) throws Exception {
	String path = "/zookeeper";

	TestingServer server = new TestingServer(2181, new File(
			"/download/zk/data"));

	CuratorFramework client = CuratorFrameworkFactory.builder()
			.connectString(server.getConnectString())
			.sessionTimeoutMs(50000).connectionTimeoutMs(100000)
			.retryPolicy(new ExponentialBackoffRetry(1000, 5)).build();
	client.start();

	System.out.println(client.getChildren().forPath(path));

	server.close();
}
 
Example 28
Project: exhibitor   File: Exhibitor.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 29
Project: pravega   File: StreamTransactionMetadataTasksTest.java   View source code 6 votes vote down vote up
@Before
public void setup() {
    try {
        zkServer = new TestingServerStarter().start();
    } catch (Exception e) {
        log.error("Error starting ZK server", e);
    }
    zkClient = CuratorFrameworkFactory.newClient(zkServer.getConnectString(),
            new ExponentialBackoffRetry(200, 10, 5000));
    zkClient.start();

    streamStore = StreamStoreFactory.createZKStore(zkClient, executor);
    TaskMetadataStore taskMetadataStore = TaskStoreFactory.createZKStore(zkClient, executor);
    hostStore = HostStoreFactory.createInMemoryStore(HostMonitorConfigImpl.dummyConfig());
    segmentHelperMock = SegmentHelperMock.getSegmentHelperMock();
    connectionFactory = Mockito.mock(ConnectionFactory.class);
    streamMetadataTasks = new StreamMetadataTasks(streamStore, hostStore, taskMetadataStore, segmentHelperMock,
            executor, "host", connectionFactory);
}
 
Example 30
Project: pravega   File: InProcPravegaCluster.java   View source code 6 votes vote down vote up
private void cleanUpZK() {
    String[] pathsTobeCleaned = {"/pravega", "/hostIndex", "/store", "/taskIndex"};

    RetryPolicy rp = new ExponentialBackoffRetry(1000, 3);
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
            .connectString(zkUrl)
            .connectionTimeoutMs(5000)
            .sessionTimeoutMs(5000)
            .retryPolicy(rp);
    @Cleanup
    CuratorFramework zclient = builder.build();
    zclient.start();
    for ( String path : pathsTobeCleaned ) {
        try {
            zclient.delete().guaranteed().deletingChildrenIfNeeded()
                    .forPath(path);
        } catch (Exception e) {
            log.warn("Not able to delete path {} . Exception {}", path, e.getMessage());
        }
    }
    zclient.close();
}
 
Example 31
Project: pravega   File: SegmentStoreAdapter.java   View source code 6 votes vote down vote up
private ServiceBuilder attachDataLogFactory(ServiceBuilder builder) {
    if (this.config.getBookieCount() > 0) {
        // We were instructed to start at least one Bookie.
        this.zkClient = CuratorFrameworkFactory
                .builder()
                .connectString("localhost:" + this.config.getZkPort())
                .namespace("pravega")
                .retryPolicy(new ExponentialBackoffRetry(1000, 5))
                .sessionTimeoutMs(5000)
                .connectionTimeoutMs(5000)
                .build();
        this.zkClient.start();
        return builder.withDataLogFactory(setup -> {
            BookKeeperConfig bkConfig = setup.getConfig(BookKeeperConfig::builder);
            return new BookKeeperLogFactory(bkConfig, this.zkClient, setup.getCoreExecutor());
        });
    } else {
        // No Bookies -> InMemory Tier1.
        return builder.withDataLogFactory(setup -> new InMemoryDurableDataLogFactory(setup.getCoreExecutor()));
    }
}
 
Example 32
Project: niubi-job-examples   File: CacheTest.java   View source code 6 votes vote down vote up
public void testNodeCache() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181,localhost:3181,localhost:4181", new ExponentialBackoffRetry(1000,4));
    client.start();

    final NodeCache nodeCache = new NodeCache(client , "/nodecache/xiayiji");
    nodeCache.getListenable().addListener(new NodeCacheListener() {
        public void nodeChanged() throws Exception {
            if (nodeCache.getCurrentData() == null) {
                System.out.println("节点被删除了");
            } else {
                System.out.println(nodeCache.getCurrentData().getPath());
                System.out.println(nodeCache.getCurrentData().getStat());
                System.out.println(new String(nodeCache.getCurrentData().getData()));
                System.out.println("------------------------");
            }
        }
    });
    nodeCache.start(true);

    new BufferedReader(new InputStreamReader(System.in)).readLine();
}
 
Example 33
Project: OSP-sample   File: CacheTest.java   View source code 6 votes vote down vote up
public void testNodeCache() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181,localhost:3181,localhost:4181", new ExponentialBackoffRetry(1000,4));
    client.start();

    final NodeCache nodeCache = new NodeCache(client , "/nodecache/xiayiji");
    nodeCache.getListenable().addListener(new NodeCacheListener() {
        public void nodeChanged() throws Exception {
            if (nodeCache.getCurrentData() == null) {
                System.out.println("节点被删除了");
            } else {
                System.out.println(nodeCache.getCurrentData().getPath());
                System.out.println(nodeCache.getCurrentData().getStat());
                System.out.println(new String(nodeCache.getCurrentData().getData()));
                System.out.println("------------------------");
            }
        }
    });
    nodeCache.start(true);

    new BufferedReader(new InputStreamReader(System.in)).readLine();
}
 
Example 34
Project: id_center   File: ZkManager.java   View source code 5 votes vote down vote up
/**
 * 创建Zookeeper连接
 *
 * @throws InterruptedException
 * @throws UnsupportedEncodingException
 */
@PostConstruct
public void connect() throws InterruptedException, UnsupportedEncodingException {
    client = CuratorFrameworkFactory.builder()
            .connectString(connectString)
            .retryPolicy(new ExponentialBackoffRetry(DEFAULT_BASE_SLEEP_TIME_MS, DEFAULT_MAX_RETRIES))
            .namespace(DEFAULT_NAMESPACE)
            .authorization(DEFAULT_ACL_SCHEME, DEFAULT_ACL_AUTH.getBytes(DEFAULT_CHARSET)) // 设置权限
            .aclProvider(new ACLProvider() { // 设置ACL规则
                @Override
                public List<ACL> getDefaultAcl() {
                    return DEFAULT_ACL_LIST;
                }
                
                @Override
                public List<ACL> getAclForPath(String path) {
                    return DEFAULT_ACL_LIST;
                }
            })
            .build();
    
    if (CuratorFrameworkState.STARTED != client.getState()) {
        client.start();
    }
    
    while (!client.blockUntilConnected(MAX_WAIT_SECONDS, TimeUnit.SECONDS)) {
        log.info("can not connect to zookeeper , retry again!!");
    }
}
 
Example 35
Project: light-tram-4j   File: CdcServerStartupHookProvider.java   View source code 5 votes vote down vote up
CuratorFramework makeStartedCuratorClient(String connectionString) {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(2000, 6, 2000);
    CuratorFramework client = CuratorFrameworkFactory.
            builder().connectString(connectionString)
            .retryPolicy(retryPolicy)
            .build();
    client.start();
    return client;
}
 
Example 36
Project: light-tram-4j   File: CdcServerStartupHookProvider.java   View source code 5 votes vote down vote up
CuratorFramework makeStartedCuratorClient(String connectionString) {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(2000, 6, 2000);
    CuratorFramework client = CuratorFrameworkFactory.
            builder().connectString(connectionString)
            .retryPolicy(retryPolicy)
            .build();
    client.start();
    return client;
}
 
Example 37
Project: light-tram-4j   File: CdcServerStartupHookProvider.java   View source code 5 votes vote down vote up
CuratorFramework makeStartedCuratorClient(String connectionString) {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(2000, 6, 2000);
    CuratorFramework client = CuratorFrameworkFactory.
            builder().connectString(connectionString)
            .retryPolicy(retryPolicy)
            .build();
    client.start();
    return client;
}
 
Example 38
Project: fastmq   File: LogManagerFactoryImpl.java   View source code 5 votes vote down vote up
public LogManagerFactoryImpl(ClientConfiguration clientConfiguration, BookKeeperConfig config)
	throws Exception {
	bookKeeperConfig = config;
	checkNotNull(clientConfiguration);
	String servers = clientConfiguration.getZkServers();
	checkNotNull(servers);
	final CountDownLatch countDownLatch = new CountDownLatch(1);

	zooKeeper = new ZooKeeper(servers, clientConfiguration.getZkTimeout(), event -> {
		if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
			logger.info("Connected to zookeeper ,connectString = {}", servers);
			countDownLatch.countDown();
		} else {
			logger.error("Failed to connect zookeeper,connectString = {}", servers);
		}
	});
	if (!countDownLatch.await(clientConfiguration.getZkTimeout(), TimeUnit.MILLISECONDS)
		|| zooKeeper.getState() != ZooKeeper.States.CONNECTED) {
		throw new LedgerStorageException(
			"Error connecting to zookeeper server ,connectString = " + servers + ".");
	}

	this.bookKeeper = new BookKeeper(clientConfiguration, zooKeeper);
	RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
	CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(servers, retryPolicy);
	curatorFramework.start();
	asyncCuratorFramework = AsyncCuratorFramework.wrap(curatorFramework);
	logInfoStorage = new LogInfoStorageImpl(asyncCuratorFramework);
	offsetStorage = new ZkOffsetStorageImpl(logInfoStorage, asyncCuratorFramework);
}
 
Example 39
Project: fastmq   File: CuratorFrameworkTest.java   View source code 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
    curatorFramework.start();
    this.curatorFramework = AsyncCuratorFramework.wrap(curatorFramework);
}
 
Example 40
Project: fastmq   File: LogManagerImplTest.java   View source code 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    Configurator
        .initialize("FastMQ", Thread.currentThread().getContextClassLoader(), "log4j2.xml");
    CountDownLatch initLatch = new CountDownLatch(1);
    CuratorFramework curatorFramework = CuratorFrameworkFactory
        .newClient("127.0.0.1:2181", new ExponentialBackoffRetry(1000, 3));
    curatorFramework.start();
    AsyncCuratorFramework asyncCuratorFramework = AsyncCuratorFramework.wrap(curatorFramework);
    when(offsetStorage.queryOffset(any())).thenReturn(new Offset());

    ledgerManager = new LogManagerImpl("ledger-manager-read-test-name", new BookKeeperConfig(),
        new BookKeeper("127.0.0.1:2181"), asyncCuratorFramework,
        new LogInfoStorageImpl(asyncCuratorFramework), this.offsetStorage);
    ledgerManager.init(new CommonCallback<Void, LedgerStorageException>() {
        @Override
        public void onCompleted(Void data, Version version) {
            initLatch.countDown();
        }

        @Override
        public void onThrowable(LedgerStorageException throwable) {
            throwable.printStackTrace();
            initLatch.countDown();
        }
    });
    initLatch.await();
}