org.apache.curator.ensemble.EnsembleProvider Java Examples

The following examples show how to use org.apache.curator.ensemble.EnsembleProvider. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CuratorZookeeperClient.java    From curator with Apache License 2.0 6 votes vote down vote up
/**
 * @param zookeeperFactory factory for creating {@link ZooKeeper} instances
 * @param ensembleProvider the ensemble provider
 * @param sessionTimeoutMs session timeout
 * @param connectionTimeoutMs connection timeout
 * @param waitForShutdownTimeoutMs default timeout fo close operation
 * @param watcher default watcher or null
 * @param retryPolicy the retry policy to use
 * @param canBeReadOnly if true, allow ZooKeeper client to enter
 *                      read only mode in case of a network partition. See
 *                      {@link ZooKeeper#ZooKeeper(String, int, Watcher, long, byte[], boolean)}
 *                      for details
 * @since 4.0.2
 */
public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider,
        int sessionTimeoutMs, int connectionTimeoutMs, int waitForShutdownTimeoutMs, Watcher watcher,
        RetryPolicy retryPolicy, boolean canBeReadOnly)
{
    if ( sessionTimeoutMs < connectionTimeoutMs )
    {
        log.warn(String.format("session timeout [%d] is less than connection timeout [%d]", sessionTimeoutMs, connectionTimeoutMs));
    }

    retryPolicy = Preconditions.checkNotNull(retryPolicy, "retryPolicy cannot be null");
    ensembleProvider = Preconditions.checkNotNull(ensembleProvider, "ensembleProvider cannot be null");

    this.connectionTimeoutMs = connectionTimeoutMs;
    this.waitForShutdownTimeoutMs = waitForShutdownTimeoutMs;
    state = new ConnectionState(zookeeperFactory, ensembleProvider, sessionTimeoutMs, watcher, tracer, canBeReadOnly);
    setRetryPolicy(retryPolicy);
}
 
Example #2
Source File: CuratorZookeeperClient.java    From xian with Apache License 2.0 5 votes vote down vote up
/**
 * @param zookeeperFactory factory for creating {@link ZooKeeper} instances
 * @param ensembleProvider the ensemble provider
 * @param sessionTimeoutMs session timeout
 * @param connectionTimeoutMs connection timeout
 * @param watcher default watcher or null
 * @param retryPolicy the retry policy to use
 * @param canBeReadOnly if true, allow ZooKeeper client to enter
 *                      read only mode in case of a network partition. See
 *                      {@link ZooKeeper#ZooKeeper(String, int, Watcher, long, byte[], boolean)}
 *                      for details
 */
public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly)
{
    if ( sessionTimeoutMs < connectionTimeoutMs )
    {
        log.warn(String.format("session timeout [%d] is less than connection timeout [%d]", sessionTimeoutMs, connectionTimeoutMs));
    }

    retryPolicy = Preconditions.checkNotNull(retryPolicy, "retryPolicy cannot be null");
    ensembleProvider = Preconditions.checkNotNull(ensembleProvider, "ensembleProvider cannot be null");

    this.connectionTimeoutMs = connectionTimeoutMs;
    state = new ConnectionState(zookeeperFactory, ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, watcher, tracer, canBeReadOnly);
    setRetryPolicy(retryPolicy);
}
 
Example #3
Source File: ResolvingEnsembleProvider.java    From curator-extensions with Apache License 2.0 5 votes vote down vote up
private static EnsembleProvider defaultDelegate(String connectString) {
    // ZooKeeper 3.4.13 and 3.5.5 are have the fixed HostProvider
    if (Info.MAJOR < 3
            || (Info.MAJOR == 3
                && (Info.MINOR < 4
                    || (Info.MINOR == 4 && Info.MICRO < 13)
                    || (Info.MINOR == 5 && Info.MICRO < 5)
                )
            )
    ) {
        return new FixedEnsembleProvider(connectString);
    } else {
        return new ResolvingEnsembleProviderDelegate(connectString);
    }
}
 
Example #4
Source File: HandleHolder.java    From curator with Apache License 2.0 5 votes vote down vote up
HandleHolder(ZookeeperFactory zookeeperFactory, Watcher watcher, EnsembleProvider ensembleProvider, int sessionTimeout, boolean canBeReadOnly)
{
    this.zookeeperFactory = zookeeperFactory;
    this.watcher = watcher;
    this.ensembleProvider = ensembleProvider;
    this.sessionTimeout = sessionTimeout;
    this.canBeReadOnly = canBeReadOnly;
}
 
Example #5
Source File: ConnectionState.java    From curator with Apache License 2.0 5 votes vote down vote up
ConnectionState(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, Watcher parentWatcher, AtomicReference<TracerDriver> tracer, boolean canBeReadOnly)
{
    this.ensembleProvider = ensembleProvider;
    this.tracer = tracer;
    if ( parentWatcher != null )
    {
        parentWatchers.offer(parentWatcher);
    }

    handleHolder = new HandleHolder(zookeeperFactory, this, ensembleProvider, sessionTimeoutMs, canBeReadOnly);
}
 
Example #6
Source File: AwsDeployment.java    From xio with Apache License 2.0 5 votes vote down vote up
public AwsDeployment(AwsDeploymentConfig config, int port) throws Exception {
  this.config = config;
  objectMapper = new ObjectMapper();
  EnsembleProvider ensembleProvider = buildEnsembleProvider(config.getExhibitorConfig());
  this.curatorClient = buildCuratorClient(ensembleProvider, config.getZookeeperConfig());
  AwsIdentity identity = AwsIdentity.getIdentity(config);
  DeploymentPayload payload =
      new DeploymentPayload(identity.privateIp, port, identity.availabilityZone);
  this.groupMember =
      buildGroupMember(curatorClient, config.getZookeeperConfig(), identity.instanceId, payload);
}
 
Example #7
Source File: AwsDeployment.java    From xio with Apache License 2.0 5 votes vote down vote up
private CuratorFramework buildCuratorClient(
    EnsembleProvider ensembleProvider, ZookeeperConfig config) throws Exception {
  RetryPolicy retryPolicy = buildRetryPolicy(config.getRetryConfig());
  CuratorFramework curatorClient =
      CuratorFrameworkFactory.builder()
          .ensembleProvider(ensembleProvider)
          .retryPolicy(retryPolicy)
          .build();
  return curatorClient;
}
 
Example #8
Source File: AwsDeployment.java    From xio with Apache License 2.0 5 votes vote down vote up
private EnsembleProvider buildEnsembleProvider(ExhibitorConfig config) throws Exception {
  Exhibitors exhibitors = new Exhibitors(config.getHostnames(), config.getRestPort(), () -> "");
  RetryPolicy retryPolicy = buildRetryPolicy(config.getRetryConfig());
  ExhibitorEnsembleProvider ensembleProvider =
      new ExhibitorEnsembleProvider(
          exhibitors,
          new DefaultExhibitorRestClient(),
          config.getRestUriPath(),
          config.getPollingMs(),
          retryPolicy);
  ensembleProvider.pollForInitialEnsemble();
  return ensembleProvider;
}
 
Example #9
Source File: ZookeeperAutoConfiguration.java    From spring-cloud-zookeeper with Apache License 2.0 5 votes vote down vote up
@Bean(destroyMethod = "close")
@ConditionalOnMissingBean
public CuratorFramework curatorFramework(RetryPolicy retryPolicy,
		ZookeeperProperties properties,
		ObjectProvider<CuratorFrameworkCustomizer> optionalCuratorFrameworkCustomizerProvider,
		ObjectProvider<EnsembleProvider> optionalEnsembleProvider,
		ObjectProvider<TracerDriver> optionalTracerDriverProvider) throws Exception {
	CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();

	EnsembleProvider ensembleProvider = optionalEnsembleProvider.getIfAvailable();
	if (ensembleProvider != null) {
		builder.ensembleProvider(ensembleProvider);
	}
	else {
		builder.connectString(properties.getConnectString());
	}
	builder.sessionTimeoutMs((int) properties.getSessionTimeout().toMillis())
			.connectionTimeoutMs((int) properties.getConnectionTimeout().toMillis())
			.retryPolicy(retryPolicy);

	optionalCuratorFrameworkCustomizerProvider.orderedStream()
			.forEach(curatorFrameworkCustomizer -> curatorFrameworkCustomizer
					.customize(builder));

	CuratorFramework curator = builder.build();
	optionalTracerDriverProvider.ifAvailable(tracerDriver -> {
		if (curator.getZookeeperClient() != null) {
			curator.getZookeeperClient().setTracerDriver(tracerDriver);
		}
	});

	curator.start();
	log.trace("blocking until connected to zookeeper for "
			+ properties.getBlockUntilConnectedWait()
			+ properties.getBlockUntilConnectedUnit());
	curator.blockUntilConnected(properties.getBlockUntilConnectedWait(),
			properties.getBlockUntilConnectedUnit());
	log.trace("connected to zookeeper");
	return curator;
}
 
Example #10
Source File: ZooKeeperHolder.java    From nakadi with MIT License 5 votes vote down vote up
private EnsembleProvider createEnsembleProvider() throws Exception {
    switch (conn.getType()) {
        case EXHIBITOR:
            final Exhibitors exhibitors = new Exhibitors(
                    conn.getAddresses().stream().map(AddressPort::getAddress).collect(Collectors.toList()),
                    conn.getAddresses().get(0).getPort(),
                    () -> {
                        throw new RuntimeException("There is no backup connection string (or it is wrong)");
                    });
            final ExhibitorRestClient exhibitorRestClient = new DefaultExhibitorRestClient();
            final ExhibitorEnsembleProvider result = new ExhibitorEnsembleProvider(
                    exhibitors,
                    exhibitorRestClient,
                    "/exhibitor/v1/cluster/list",
                    EXHIBITOR_POLLING_MS,
                    new ExponentialBackoffRetry(EXHIBITOR_RETRY_TIME, EXHIBITOR_RETRY_MAX)) {
                @Override
                public String getConnectionString() {
                    return super.getConnectionString() + conn.getPathPrepared();
                }
            };
            result.pollForInitialEnsemble();
            return result;
        case ZOOKEEPER:
            final String addressesJoined = conn.getAddresses().stream()
                    .map(AddressPort::asAddressPort)
                    .collect(Collectors.joining(","));
            return new ChrootedFixedEnsembleProvider(addressesJoined, conn.getPathPrepared());
        default:
            throw new RuntimeException("Connection type " + conn.getType() + " is not supported");
    }
}
 
Example #11
Source File: ZKClient.java    From hermes with Apache License 2.0 5 votes vote down vote up
private void startCuratorFramework(ZookeeperEnsemble primaryEnsemble) throws InitializationException {
	Builder builder = CuratorFrameworkFactory.builder();

	builder.connectionTimeoutMs(m_config.getZkConnectionTimeoutMillis());
	builder.maxCloseWaitMs(m_config.getZkCloseWaitMillis());
	builder.namespace(m_config.getZkNamespace());
	builder.retryPolicy(new RetryNTimes(m_config.getZkRetries(), m_config.getSleepMsBetweenRetries()));
	builder.sessionTimeoutMs(m_config.getZkSessionTimeoutMillis());
	builder.threadFactory(HermesThreadFactory.create("Broker-Zk", true));
	builder.ensembleProvider(new EnsembleProvider() {

		@Override
		public void start() throws Exception {
		}

		@Override
		public String getConnectionString() {
			return m_primaryZookeeperEnsemble.get().getConnectionString();
		}

		@Override
		public void close() throws IOException {
		}
	});

	m_client = builder.build();
	m_client.start();
	try {
		m_client.blockUntilConnected();
		log.info("Conneted to zookeeper({}).", JSON.toJSONString(primaryEnsemble));
	} catch (InterruptedException e) {
		throw new InitializationException(e.getMessage(), e);
	}
}
 
Example #12
Source File: ZKClient.java    From hermes with Apache License 2.0 5 votes vote down vote up
private void startCuratorFramework(ZookeeperEnsemble primaryEnsemble) throws InitializationException {
	Builder builder = CuratorFrameworkFactory.builder();

	builder.connectionTimeoutMs(m_config.getZkConnectionTimeoutMillis());
	builder.maxCloseWaitMs(m_config.getZkCloseWaitMillis());
	builder.namespace(m_config.getZkNamespace());
	builder.retryPolicy(new RetryNTimes(m_config.getZkRetries(), m_config.getSleepMsBetweenRetries()));
	builder.sessionTimeoutMs(m_config.getZkSessionTimeoutMillis());
	builder.threadFactory(HermesThreadFactory.create("MetaService-Zk", true));
	builder.ensembleProvider(new EnsembleProvider() {

		@Override
		public void start() throws Exception {
		}

		@Override
		public String getConnectionString() {
			return m_primaryZookeeperEnsemble.get().getConnectionString();
		}

		@Override
		public void close() throws IOException {
		}
	});

	m_client = builder.build();
	m_client.start();
	try {
		m_client.blockUntilConnected();
		log.info("Conneted to zookeeper({}).", JSON.toJSONString(primaryEnsemble));
	} catch (InterruptedException e) {
		throw new InitializationException(e.getMessage(), e);
	}
}
 
Example #13
Source File: HandleHolder.java    From xian with Apache License 2.0 5 votes vote down vote up
HandleHolder(ZookeeperFactory zookeeperFactory, Watcher watcher, EnsembleProvider ensembleProvider, int sessionTimeout, boolean canBeReadOnly)
{
    this.zookeeperFactory = zookeeperFactory;
    this.watcher = watcher;
    this.ensembleProvider = ensembleProvider;
    this.sessionTimeout = sessionTimeout;
    this.canBeReadOnly = canBeReadOnly;
}
 
Example #14
Source File: ConnectionState.java    From xian with Apache License 2.0 5 votes vote down vote up
ConnectionState(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher parentWatcher, AtomicReference<TracerDriver> tracer, boolean canBeReadOnly)
{
    this.ensembleProvider = ensembleProvider;
    this.sessionTimeoutMs = sessionTimeoutMs;
    this.connectionTimeoutMs = connectionTimeoutMs;
    this.tracer = tracer;
    if ( parentWatcher != null )
    {
        parentWatchers.offer(parentWatcher);
    }

    zooKeeper = new HandleHolder(zookeeperFactory, this, ensembleProvider, sessionTimeoutMs, canBeReadOnly);
}
 
Example #15
Source File: ZookeeperAutoConfigurationTests.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
@Bean
EnsembleProvider ensembleProvider(TestingServer testingServer) {
	return new FixedEnsembleProvider(testingServer.getConnectString());
}
 
Example #16
Source File: CuratorFrameworkFactory.java    From xian with Apache License 2.0 4 votes vote down vote up
public EnsembleProvider getEnsembleProvider()
{
    return ensembleProvider;
}
 
Example #17
Source File: CuratorFrameworkFactory.java    From curator with Apache License 2.0 4 votes vote down vote up
public EnsembleProvider getEnsembleProvider()
{
    return ensembleProvider;
}
 
Example #18
Source File: EnsembleTracker.java    From curator with Apache License 2.0 4 votes vote down vote up
EnsembleTracker(CuratorFramework client, EnsembleProvider ensembleProvider)
{
    this.client = client.newWatcherRemoveCuratorFramework();
    this.ensembleProvider = ensembleProvider;
}
 
Example #19
Source File: ConnectionState.java    From xian with Apache License 2.0 4 votes vote down vote up
EnsembleProvider getEnsembleProvider()
{
    return ensembleProvider;
}
 
Example #20
Source File: ConnectionState.java    From curator with Apache License 2.0 4 votes vote down vote up
EnsembleProvider getEnsembleProvider()
{
    return ensembleProvider;
}
 
Example #21
Source File: ResolvingEnsembleProvider.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
ResolvingEnsembleProvider(EnsembleProvider delegate) {
    _delegate = delegate;
}
 
Example #22
Source File: CuratorZookeeperClient.java    From curator with Apache License 2.0 2 votes vote down vote up
/**
 * @param ensembleProvider the ensemble provider
 * @param sessionTimeoutMs session timeout
 * @param connectionTimeoutMs connection timeout
 * @param watcher default watcher or null
 * @param retryPolicy the retry policy to use
 */
public CuratorZookeeperClient(EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy)
{
    this(new DefaultZookeeperFactory(), ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, watcher, retryPolicy, false);
}
 
Example #23
Source File: CuratorZookeeperClient.java    From curator with Apache License 2.0 2 votes vote down vote up
/**
 * @param zookeeperFactory factory for creating {@link ZooKeeper} instances
 * @param ensembleProvider the ensemble provider
 * @param sessionTimeoutMs session timeout
 * @param connectionTimeoutMs connection timeout
 * @param watcher default watcher or null
 * @param retryPolicy the retry policy to use
 * @param canBeReadOnly if true, allow ZooKeeper client to enter
 *                      read only mode in case of a network partition. See
 *                      {@link ZooKeeper#ZooKeeper(String, int, Watcher, long, byte[], boolean)}
 *                      for details
 */
public CuratorZookeeperClient(ZookeeperFactory zookeeperFactory, EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy, boolean canBeReadOnly)
{
    this(zookeeperFactory, ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, 0, watcher, retryPolicy, canBeReadOnly);
}
 
Example #24
Source File: CuratorZookeeperClient.java    From xian with Apache License 2.0 2 votes vote down vote up
/**
 * @param ensembleProvider the ensemble provider
 * @param sessionTimeoutMs session timeout
 * @param connectionTimeoutMs connection timeout
 * @param watcher default watcher or null
 * @param retryPolicy the retry policy to use
 */
public CuratorZookeeperClient(EnsembleProvider ensembleProvider, int sessionTimeoutMs, int connectionTimeoutMs, Watcher watcher, RetryPolicy retryPolicy)
{
    this(new DefaultZookeeperFactory(), ensembleProvider, sessionTimeoutMs, connectionTimeoutMs, watcher, retryPolicy, false);
}