CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-curator--curator-client

Low-level API for Apache ZooKeeper client library providing connection management, retry policies, and basic ZooKeeper operations

Pending
Overview
Eval results
Files

ensemble-providers.mddocs/

Ensemble Providers

Connection string providers for static and dynamic ZooKeeper cluster configuration. Ensemble providers abstract the source of ZooKeeper connection strings, enabling both fixed configurations and dynamic service discovery patterns.

Capabilities

EnsembleProvider Interface

Core interface for providing ZooKeeper connection strings, supporting both static and dynamic ensemble configurations.

/**
 * Interface for providing ZooKeeper connection strings
 */
public interface EnsembleProvider extends Closeable {
    /**
     * Start the ensemble provider
     * @throws Exception if provider cannot be started
     */
    void start() throws Exception;
    
    /**
     * Get current ZooKeeper connection string
     * @return Connection string in format "host1:port1,host2:port2,..."
     */
    String getConnectionString();
    
    /**
     * Close the ensemble provider and release resources
     * @throws IOException if close operation fails
     */
    void close() throws IOException;
    
    /**
     * Set a new connection string (for providers that support updates)
     * @param connectionString New connection string to use
     */
    void setConnectionString(String connectionString);
    
    /**
     * Check if this provider supports server list updates
     * @return true if provider can update server list dynamically
     */
    boolean updateServerListEnabled();
}

Fixed Ensemble Provider

Static ensemble provider that uses a fixed connection string, ideal for stable cluster configurations.

/**
 * Ensemble provider with fixed connection string
 */
public class FixedEnsembleProvider implements EnsembleProvider {
    /**
     * Create fixed ensemble provider with connection string
     * @param connectionString ZooKeeper connection string
     */
    public FixedEnsembleProvider(String connectionString);
    
    /**
     * Create fixed ensemble provider with connection string and update flag
     * @param connectionString ZooKeeper connection string
     * @param updateServerListEnabled Whether server list updates are enabled
     */
    public FixedEnsembleProvider(String connectionString, boolean updateServerListEnabled);
}

Usage Examples:

import org.apache.curator.ensemble.fixed.FixedEnsembleProvider;
import org.apache.curator.CuratorZookeeperClient;
import org.apache.curator.retry.ExponentialBackoffRetry;

// Basic fixed ensemble
EnsembleProvider ensemble = new FixedEnsembleProvider("zk1:2181,zk2:2181,zk3:2181");

// Create client with ensemble provider
CuratorZookeeperClient client = new CuratorZookeeperClient(
    ensemble,
    5000,  // session timeout
    5000,  // connection timeout
    null,  // watcher
    new ExponentialBackoffRetry(1000, 3)
);

try {
    ensemble.start();
    client.start();
    // ... use client ...
} finally {
    client.close();
    ensemble.close();
}

Exhibitor Ensemble Provider

Dynamic ensemble provider that integrates with Netflix Exhibitor for automatic ZooKeeper cluster discovery and monitoring.

/**
 * Ensemble provider that uses Netflix Exhibitor for dynamic ensemble discovery
 */
public class ExhibitorEnsembleProvider implements EnsembleProvider {
    /**
     * Create Exhibitor-based ensemble provider
     * @param exhibitors Collection of Exhibitor instances
     * @param restClient REST client for Exhibitor communication
     * @param pollingMs Polling interval in milliseconds for ensemble updates
     */
    public ExhibitorEnsembleProvider(Exhibitors exhibitors, ExhibitorRestClient restClient, int pollingMs);
    
    /**
     * Create Exhibitor ensemble provider with backup connection string
     * @param exhibitors Collection of Exhibitor instances  
     * @param restClient REST client for Exhibitor communication
     * @param backup Backup connection string provider
     * @param pollingMs Polling interval for ensemble updates
     */
    public ExhibitorEnsembleProvider(Exhibitors exhibitors, ExhibitorRestClient restClient, 
                                   BackupConnectionStringProvider backup, int pollingMs);
}

Exhibitor Configuration Classes

Supporting classes for configuring and managing Exhibitor-based ensemble discovery.

/**
 * Collection of Exhibitor instances for ensemble discovery
 */
public class Exhibitors {
    /**
     * Create Exhibitors collection
     * @param hostnames Collection of Exhibitor hostnames
     * @param restPort REST port for Exhibitor communication
     * @param backupConnectionStringProvider Backup connection string provider
     */
    public Exhibitors(Collection<String> hostnames, int restPort, 
                     BackupConnectionStringProvider backupConnectionStringProvider);
    
    /**
     * Get backup connection string provider
     * @return Backup connection string provider
     */
    public BackupConnectionStringProvider getBackupConnectionStringProvider();
    
    /**
     * Get Exhibitor hostnames
     * @return Collection of hostnames
     */
    public Collection<String> getHostnames();
    
    /**
     * Get REST port for Exhibitor communication
     * @return REST port number
     */
    public int getRestPort();
}

/**
 * Interface for REST client communication with Exhibitor
 */
public interface ExhibitorRestClient {
    /**
     * Make raw REST request to Exhibitor
     * @param hostname Exhibitor hostname
     * @param port Exhibitor port
     * @param uriPath URI path for the request
     * @param mimeType Expected MIME type of response
     * @return Raw response string
     * @throws Exception if request fails
     */
    String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception;
}

/**
 * Default implementation of ExhibitorRestClient
 */
public class DefaultExhibitorRestClient implements ExhibitorRestClient {
    /**
     * Create default REST client with connection timeout
     * @param connectionTimeoutMs Connection timeout in milliseconds
     */
    public DefaultExhibitorRestClient(int connectionTimeoutMs);
    
    /**
     * Create default REST client with custom socket timeout
     * @param connectionTimeoutMs Connection timeout in milliseconds
     * @param socketTimeoutMs Socket timeout in milliseconds
     */
    public DefaultExhibitorRestClient(int connectionTimeoutMs, int socketTimeoutMs);
}

Usage Examples:

import org.apache.curator.ensemble.exhibitor.*;
import java.util.Arrays;

// Configure Exhibitor instances
Exhibitors exhibitors = new Exhibitors(
    Arrays.asList("exhibitor1.example.com", "exhibitor2.example.com"), 
    8080, 
    () -> "fallback1:2181,fallback2:2181"  // Backup connection string
);

// Create REST client
ExhibitorRestClient restClient = new DefaultExhibitorRestClient(5000);

// Create dynamic ensemble provider
ExhibitorEnsembleProvider ensembleProvider = new ExhibitorEnsembleProvider(
    exhibitors, 
    restClient, 
    10000  // Poll every 10 seconds
);

// Use with Curator client
CuratorZookeeperClient client = new CuratorZookeeperClient(
    ensembleProvider,
    5000, 5000, null, 
    new ExponentialBackoffRetry(1000, 3)
);

try {
    ensembleProvider.start();
    client.start();
    
    // Ensemble provider will automatically update connection string
    // as ZooKeeper cluster membership changes
    
} finally {
    client.close();
    ensembleProvider.close();
}

Backup Connection String Provider

Functional interface for providing fallback connection strings when dynamic discovery fails.

/**
 * Functional interface for backup connection string provision
 */
@FunctionalInterface
public interface BackupConnectionStringProvider {
    /**
     * Get backup connection string when primary discovery fails
     * @return Backup ZooKeeper connection string
     * @throws Exception if backup cannot be provided
     */
    String getBackupConnectionString() throws Exception;
}

Usage Examples:

// Lambda expression for backup connection
BackupConnectionStringProvider backup = () -> "backup1:2181,backup2:2181,backup3:2181";

// Method reference
BackupConnectionStringProvider configBackup = this::getConfiguredBackupString;

// Anonymous class implementation
BackupConnectionStringProvider dynamicBackup = new BackupConnectionStringProvider() {
    @Override
    public String getBackupConnectionString() throws Exception {
        // Query configuration service or database for backup servers
        return configService.getZookeeperBackupServers();
    }
};

Ensemble Provider Selection Guide

FixedEnsembleProvider

Best for:

  • Stable ZooKeeper clusters with known, unchanging membership
  • Development and testing environments
  • Simple production deployments without dynamic scaling

Use when:

  • ZooKeeper cluster membership is static
  • You want minimal complexity and dependencies
  • Service discovery infrastructure is not available

ExhibitorEnsembleProvider

Best for:

  • Dynamic ZooKeeper clusters managed by Netflix Exhibitor
  • Cloud deployments where cluster membership changes
  • Large-scale production environments with automated cluster management

Use when:

  • Using Netflix Exhibitor for ZooKeeper cluster management
  • ZooKeeper cluster membership changes dynamically
  • You need automatic failover to backup connection strings
  • High availability requirements with multiple ZooKeeper clusters

Integration Patterns

Client Configuration with Ensemble Providers

// Pattern 1: Fixed ensemble for stable clusters
EnsembleProvider fixedEnsemble = new FixedEnsembleProvider("zk1:2181,zk2:2181,zk3:2181");

// Pattern 2: Dynamic ensemble with Exhibitor
Exhibitors exhibitors = new Exhibitors(
    Arrays.asList("exhibitor1.com", "exhibitor2.com"), 8080, 
    () -> "backup1:2181,backup2:2181"
);
EnsembleProvider dynamicEnsemble = new ExhibitorEnsembleProvider(
    exhibitors, new DefaultExhibitorRestClient(5000), 30000
);

// Both patterns work identically with CuratorZookeeperClient
CuratorZookeeperClient client = new CuratorZookeeperClient(
    ensembleProvider, 5000, 5000, null, retryPolicy
);

Resource Management

// Always ensure proper resource cleanup
try (EnsembleProvider ensemble = new FixedEnsembleProvider("localhost:2181");
     CuratorZookeeperClient client = new CuratorZookeeperClient(ensemble, 5000, 5000, null, retryPolicy)) {
    
    ensemble.start();
    client.start();
    
    // Use client...
    
} // Resources automatically closed

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-curator--curator-client

docs

client-connection.md

connection-handling.md

ensemble-providers.md

index.md

path-utilities.md

retry-policies.md

tracing-metrics.md

tile.json