Search in sources :

Example 1 with SessionConnectionStateErrorPolicy

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.state.SessionConnectionStateErrorPolicy in project flink by apache.

the class ZooKeeperUtils method startCuratorFramework.

/**
 * Starts a {@link CuratorFramework} instance and connects it to the given ZooKeeper quorum.
 *
 * @param configuration {@link Configuration} object containing the configuration values
 * @param fatalErrorHandler {@link FatalErrorHandler} fatalErrorHandler to handle unexpected
 *     errors of {@link CuratorFramework}
 * @return {@link CuratorFrameworkWithUnhandledErrorListener} instance
 */
public static CuratorFrameworkWithUnhandledErrorListener startCuratorFramework(Configuration configuration, FatalErrorHandler fatalErrorHandler) {
    checkNotNull(configuration, "configuration");
    String zkQuorum = configuration.getValue(HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM);
    if (zkQuorum == null || StringUtils.isBlank(zkQuorum)) {
        throw new RuntimeException("No valid ZooKeeper quorum has been specified. " + "You can specify the quorum via the configuration key '" + HighAvailabilityOptions.HA_ZOOKEEPER_QUORUM.key() + "'.");
    }
    int sessionTimeout = configuration.getInteger(HighAvailabilityOptions.ZOOKEEPER_SESSION_TIMEOUT);
    int connectionTimeout = configuration.getInteger(HighAvailabilityOptions.ZOOKEEPER_CONNECTION_TIMEOUT);
    int retryWait = configuration.getInteger(HighAvailabilityOptions.ZOOKEEPER_RETRY_WAIT);
    int maxRetryAttempts = configuration.getInteger(HighAvailabilityOptions.ZOOKEEPER_MAX_RETRY_ATTEMPTS);
    String root = configuration.getValue(HighAvailabilityOptions.HA_ZOOKEEPER_ROOT);
    String namespace = configuration.getValue(HighAvailabilityOptions.HA_CLUSTER_ID);
    boolean disableSaslClient = configuration.getBoolean(SecurityOptions.ZOOKEEPER_SASL_DISABLE);
    ACLProvider aclProvider;
    ZkClientACLMode aclMode = ZkClientACLMode.fromConfig(configuration);
    if (disableSaslClient && aclMode == ZkClientACLMode.CREATOR) {
        String errorMessage = "Cannot set ACL role to " + ZkClientACLMode.CREATOR + "  since SASL authentication is " + "disabled through the " + SecurityOptions.ZOOKEEPER_SASL_DISABLE.key() + " property";
        LOG.warn(errorMessage);
        throw new IllegalConfigurationException(errorMessage);
    }
    if (aclMode == ZkClientACLMode.CREATOR) {
        LOG.info("Enforcing creator for ZK connections");
        aclProvider = new SecureAclProvider();
    } else {
        LOG.info("Enforcing default ACL for ZK connections");
        aclProvider = new DefaultACLProvider();
    }
    String rootWithNamespace = generateZookeeperPath(root, namespace);
    LOG.info("Using '{}' as Zookeeper namespace.", rootWithNamespace);
    final CuratorFrameworkFactory.Builder curatorFrameworkBuilder = CuratorFrameworkFactory.builder().connectString(zkQuorum).sessionTimeoutMs(sessionTimeout).connectionTimeoutMs(connectionTimeout).retryPolicy(new ExponentialBackoffRetry(retryWait, maxRetryAttempts)).namespace(trimStartingSlash(rootWithNamespace)).aclProvider(aclProvider);
    if (configuration.get(HighAvailabilityOptions.ZOOKEEPER_TOLERATE_SUSPENDED_CONNECTIONS)) {
        curatorFrameworkBuilder.connectionStateErrorPolicy(new SessionConnectionStateErrorPolicy());
    }
    return startCuratorFramework(curatorFrameworkBuilder, fatalErrorHandler);
}
Also used : ACLProvider(org.apache.flink.shaded.curator5.org.apache.curator.framework.api.ACLProvider) DefaultACLProvider(org.apache.flink.shaded.curator5.org.apache.curator.framework.imps.DefaultACLProvider) CuratorFrameworkFactory(org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFrameworkFactory) ExponentialBackoffRetry(org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry) IllegalConfigurationException(org.apache.flink.configuration.IllegalConfigurationException) CompletedCheckpoint(org.apache.flink.runtime.checkpoint.CompletedCheckpoint) SessionConnectionStateErrorPolicy(org.apache.flink.shaded.curator5.org.apache.curator.framework.state.SessionConnectionStateErrorPolicy) DefaultACLProvider(org.apache.flink.shaded.curator5.org.apache.curator.framework.imps.DefaultACLProvider)

Example 2 with SessionConnectionStateErrorPolicy

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.state.SessionConnectionStateErrorPolicy in project alluxio by Alluxio.

the class PrimarySelectorClient method getNewCuratorClient.

/**
 * Returns a new client for the zookeeper connection. The client is already started before
 * returning.
 *
 * @return a new {@link CuratorFramework} client to use for leader selection
 */
private CuratorFramework getNewCuratorClient() {
    LOG.info("Creating new zookeeper client for primary selector {}", mZookeeperAddress);
    CuratorFrameworkFactory.Builder curatorBuilder = CuratorFrameworkFactory.builder();
    curatorBuilder.connectString(mZookeeperAddress);
    curatorBuilder.retryPolicy(new ExponentialBackoffRetry(Constants.SECOND_MS, 3));
    curatorBuilder.sessionTimeoutMs((int) ServerConfiguration.getMs(PropertyKey.ZOOKEEPER_SESSION_TIMEOUT));
    curatorBuilder.connectionTimeoutMs((int) ServerConfiguration.getMs(PropertyKey.ZOOKEEPER_CONNECTION_TIMEOUT));
    // Force compatibility mode to support writing to 3.4.x servers.
    curatorBuilder.zk34CompatibilityMode(true);
    // Prevent using container parents as it breaks compatibility with 3.4.x servers.
    // This is only required if the client is used to write data to zookeeper.
    curatorBuilder.dontUseContainerParents();
    // Use SESSION policy for leader connection errors, when configured.
    if (mConnectionErrorPolicy == ZookeeperConnectionErrorPolicy.SESSION) {
        curatorBuilder.connectionStateErrorPolicy(new SessionConnectionStateErrorPolicy());
    }
    CuratorFramework client = curatorBuilder.build();
    client.start();
    // Sometimes, if the master crashes and restarts too quickly (faster than the zookeeper
    // timeout), zookeeper thinks the new client is still an old one. In order to ensure a clean
    // state, explicitly close the "old" client and recreate a new one.
    client.close();
    client = curatorBuilder.build();
    client.start();
    return client;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) SessionConnectionStateErrorPolicy(org.apache.curator.framework.state.SessionConnectionStateErrorPolicy)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)1 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)1 SessionConnectionStateErrorPolicy (org.apache.curator.framework.state.SessionConnectionStateErrorPolicy)1 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)1 IllegalConfigurationException (org.apache.flink.configuration.IllegalConfigurationException)1 CompletedCheckpoint (org.apache.flink.runtime.checkpoint.CompletedCheckpoint)1 CuratorFrameworkFactory (org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFrameworkFactory)1 ACLProvider (org.apache.flink.shaded.curator5.org.apache.curator.framework.api.ACLProvider)1 DefaultACLProvider (org.apache.flink.shaded.curator5.org.apache.curator.framework.imps.DefaultACLProvider)1 SessionConnectionStateErrorPolicy (org.apache.flink.shaded.curator5.org.apache.curator.framework.state.SessionConnectionStateErrorPolicy)1 ExponentialBackoffRetry (org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry)1