org.apache.curator.utils.ZookeeperFactory Java Examples

The following examples show how to use org.apache.curator.utils.ZookeeperFactory. 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: CuratorFrameworkImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
private ZookeeperFactory makeZookeeperFactory(final ZookeeperFactory actualZookeeperFactory)
{
    return new ZookeeperFactory()
    {
        @Override
        public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws Exception
        {
            ZooKeeper zooKeeper = actualZookeeperFactory.newZooKeeper(connectString, sessionTimeout, watcher, canBeReadOnly);
            for ( AuthInfo auth : authInfos )
            {
                zooKeeper.addAuthInfo(auth.getScheme(), auth.getAuth());
            }

            return zooKeeper;
        }
    };
}
 
Example #2
Source File: BasicTests.java    From curator with Apache License 2.0 6 votes vote down vote up
@Test
public void     testFactory() throws Exception
{
    final ZooKeeper         mockZookeeper = Mockito.mock(ZooKeeper.class);
    ZookeeperFactory        zookeeperFactory = new ZookeeperFactory()
    {
        @Override
        public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws Exception
        {
            return mockZookeeper;
        }
    };
    CuratorZookeeperClient  client = new CuratorZookeeperClient(zookeeperFactory, new FixedEnsembleProvider(server.getConnectString()), 10000, 10000, null, new RetryOneTime(1), false);
    client.start();
    Assert.assertEquals(client.getZooKeeper(), mockZookeeper);
}
 
Example #3
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 #4
Source File: CuratorFrameworkImpl.java    From xian with Apache License 2.0 6 votes vote down vote up
private ZookeeperFactory makeZookeeperFactory(final ZookeeperFactory actualZookeeperFactory)
{
    return new ZookeeperFactory()
    {
        @Override
        public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws Exception
        {
            ZooKeeper zooKeeper = actualZookeeperFactory.newZooKeeper(connectString, sessionTimeout, watcher, canBeReadOnly);
            for ( AuthInfo auth : authInfos )
            {
                zooKeeper.addAuthInfo(auth.getScheme(), auth.getAuth());
            }

            return zooKeeper;
        }
    };
}
 
Example #5
Source File: BasicTests.java    From xian with Apache License 2.0 6 votes vote down vote up
@Test
public void     testFactory() throws Exception
{
    final ZooKeeper         mockZookeeper = Mockito.mock(ZooKeeper.class);
    ZookeeperFactory        zookeeperFactory = new ZookeeperFactory()
    {
        @Override
        public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws Exception
        {
            return mockZookeeper;
        }
    };
    CuratorZookeeperClient  client = new CuratorZookeeperClient(zookeeperFactory, new FixedEnsembleProvider(server.getConnectString()), 10000, 10000, null, new RetryOneTime(1), false);
    client.start();
    Assert.assertEquals(client.getZooKeeper(), mockZookeeper);
}
 
Example #6
Source File: CuratorFrameworkImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
public CuratorFrameworkImpl(CuratorFrameworkFactory.Builder builder)
{
    ZookeeperFactory localZookeeperFactory = makeZookeeperFactory(builder.getZookeeperFactory());
    this.client = new CuratorZookeeperClient(localZookeeperFactory, builder.getEnsembleProvider(), builder.getSessionTimeoutMs(), builder.getConnectionTimeoutMs(), new Watcher()
    {
        @Override
        public void process(WatchedEvent watchedEvent)
        {
            CuratorEvent event = new CuratorEventImpl(CuratorFrameworkImpl.this, CuratorEventType.WATCHED, watchedEvent.getState().getIntValue(), unfixForNamespace(watchedEvent.getPath()), null, null, null, null, null, watchedEvent, null);
            processEvent(event);
        }
    }, builder.getRetryPolicy(), builder.canBeReadOnly());

    listeners = new ListenerContainer<CuratorListener>();
    unhandledErrorListeners = new ListenerContainer<UnhandledErrorListener>();
    backgroundOperations = new DelayQueue<OperationAndData<?>>();
    namespace = new NamespaceImpl(this, builder.getNamespace());
    threadFactory = getThreadFactory(builder);
    maxCloseWaitMs = builder.getMaxCloseWaitMs();
    connectionStateManager = new ConnectionStateManager(this, builder.getThreadFactory());
    compressionProvider = builder.getCompressionProvider();
    aclProvider = builder.getAclProvider();
    state = new AtomicReference<CuratorFrameworkState>(CuratorFrameworkState.LATENT);
    useContainerParentsIfAvailable = builder.useContainerParentsIfAvailable();

    byte[] builderDefaultData = builder.getDefaultData();
    defaultData = (builderDefaultData != null) ? Arrays.copyOf(builderDefaultData, builderDefaultData.length) : new byte[0];
    authInfos = buildAuths(builder);

    failedDeleteManager = new FailedDeleteManager(this);
    namespaceFacadeCache = new NamespaceFacadeCache(this);
}
 
Example #7
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 #8
Source File: TestWatchesBuilder.java    From curator with Apache License 2.0 5 votes vote down vote up
@Test(groups = CuratorTestBase.zk36Group)
public void testPersistentRecursiveDefaultWatch() throws Exception
{
    CountDownLatch latch = new CountDownLatch(6);   // 5 creates plus the initial sync
    ZookeeperFactory zookeeperFactory = (connectString, sessionTimeout, watcher, canBeReadOnly) -> {
        Watcher actualWatcher = event -> {
            watcher.process(event);
            latch.countDown();
        };
        return new ZooKeeper(connectString, sessionTimeout, actualWatcher);
    };
    try (CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).zookeeperFactory(zookeeperFactory).build() )
    {
        client.start();
        client.blockUntilConnected();

        client.watchers().add().withMode(AddWatchMode.PERSISTENT_RECURSIVE).forPath("/test");

        client.create().forPath("/test");
        client.create().forPath("/test/a");
        client.create().forPath("/test/a/b");
        client.create().forPath("/test/a/b/c");
        client.create().forPath("/test/a/b/c/d");

        Assert.assertTrue(timing.awaitLatch(latch));
    }
}
 
Example #9
Source File: TestFramework.java    From curator with Apache License 2.0 5 votes vote down vote up
public void testWaitForShutdownTimeoutMs() throws Exception
{
    final BlockingQueue<Integer> timeoutQueue = new ArrayBlockingQueue<>(1);
    ZookeeperFactory zookeeperFactory = new ZookeeperFactory()
    {
        @Override
        public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws IOException
        {
            return new ZooKeeper(connectString, sessionTimeout, watcher, canBeReadOnly)
            {
                @Override
                public boolean close(int waitForShutdownTimeoutMs) throws InterruptedException
                {
                    timeoutQueue.add(waitForShutdownTimeoutMs);
                    return super.close(waitForShutdownTimeoutMs);
                }
            };
        }
    };

    CuratorFramework client = CuratorFrameworkFactory.builder()
        .connectString(server.getConnectString())
        .retryPolicy(new RetryOneTime(1))
        .zookeeperFactory(zookeeperFactory)
        .waitForShutdownTimeoutMs(10064)
        .build();
    try
    {
        client.start();
        client.checkExists().forPath("/foo");
    }
    finally
    {
        CloseableUtils.closeQuietly(client);
    }

    Integer polledValue = timeoutQueue.poll(new Timing().milliseconds(), TimeUnit.MILLISECONDS);
    Assert.assertNotNull(polledValue);
    Assert.assertEquals(10064, polledValue.intValue());
}
 
Example #10
Source File: TestAddWatch.java    From curator with Apache License 2.0 5 votes vote down vote up
@Test
public void testPersistentRecursiveDefaultWatch() throws Exception
{
    CountDownLatch latch = new CountDownLatch(6);   // 5 creates plus the initial sync
    ZookeeperFactory zookeeperFactory = (connectString, sessionTimeout, watcher, canBeReadOnly) -> {
        Watcher actualWatcher = event -> {
            watcher.process(event);
            latch.countDown();
        };
        return new ZooKeeper(connectString, sessionTimeout, actualWatcher);
    };
    try (CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).zookeeperFactory(zookeeperFactory).build() )
    {
        client.start();
        client.blockUntilConnected();

        AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
        async.addWatch().withMode(AddWatchMode.PERSISTENT_RECURSIVE).forPath("/test");

        client.create().forPath("/test");
        client.create().forPath("/test/a");
        client.create().forPath("/test/a/b");
        client.create().forPath("/test/a/b/c");
        client.create().forPath("/test/a/b/c/d");

        Assert.assertTrue(timing.awaitLatch(latch));
    }
}
 
Example #11
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 #12
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 #13
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 #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: CuratorFrameworkFactory.java    From xian with Apache License 2.0 4 votes vote down vote up
public ZookeeperFactory getZookeeperFactory()
{
    return zookeeperFactory;
}
 
Example #16
Source File: CuratorFrameworkFactory.java    From curator with Apache License 2.0 4 votes vote down vote up
public ZookeeperFactory getZookeeperFactory()
{
    return zookeeperFactory;
}
 
Example #17
Source File: CuratorFrameworkImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
public CuratorFrameworkImpl(CuratorFrameworkFactory.Builder builder)
{
    ZookeeperFactory localZookeeperFactory = makeZookeeperFactory(builder.getZookeeperFactory());
    this.client = new CuratorZookeeperClient
        (
            localZookeeperFactory,
            builder.getEnsembleProvider(),
            builder.getSessionTimeoutMs(),
            builder.getConnectionTimeoutMs(),
            builder.getWaitForShutdownTimeoutMs(),
            new Watcher()
            {
                @Override
                public void process(WatchedEvent watchedEvent)
                {
                    CuratorEvent event = new CuratorEventImpl(CuratorFrameworkImpl.this, CuratorEventType.WATCHED, watchedEvent.getState().getIntValue(), unfixForNamespace(watchedEvent.getPath()), null, null, null, null, null, watchedEvent, null, null);
                    processEvent(event);
                }
            },
            builder.getRetryPolicy(),
            builder.canBeReadOnly()
        );

    internalConnectionHandler = new StandardInternalConnectionHandler();
    listeners = StandardListenerManager.standard();
    unhandledErrorListeners = StandardListenerManager.standard();
    backgroundOperations = new DelayQueue<OperationAndData<?>>();
    forcedSleepOperations = new LinkedBlockingQueue<>();
    namespace = new NamespaceImpl(this, builder.getNamespace());
    threadFactory = getThreadFactory(builder);
    maxCloseWaitMs = builder.getMaxCloseWaitMs();
    connectionStateManager = new ConnectionStateManager(this, builder.getThreadFactory(), builder.getSessionTimeoutMs(), builder.getSimulatedSessionExpirationPercent(), builder.getConnectionStateListenerManagerFactory());
    compressionProvider = builder.getCompressionProvider();
    aclProvider = builder.getAclProvider();
    state = new AtomicReference<CuratorFrameworkState>(CuratorFrameworkState.LATENT);
    useContainerParentsIfAvailable = builder.useContainerParentsIfAvailable();
    connectionStateErrorPolicy = Preconditions.checkNotNull(builder.getConnectionStateErrorPolicy(), "errorPolicy cannot be null");
    schemaSet = Preconditions.checkNotNull(builder.getSchemaSet(), "schemaSet cannot be null");

    byte[] builderDefaultData = builder.getDefaultData();
    defaultData = (builderDefaultData != null) ? Arrays.copyOf(builderDefaultData, builderDefaultData.length) : new byte[0];
    authInfos = buildAuths(builder);

    failedDeleteManager = new FailedDeleteManager(this);
    failedRemoveWatcherManager = new FailedRemoveWatchManager(this);
    namespaceFacadeCache = new NamespaceFacadeCache(this);

    ensembleTracker = builder.withEnsembleTracker() ? new EnsembleTracker(this, builder.getEnsembleProvider()) : null;

    runSafeService = makeRunSafeService(builder);
}
 
Example #18
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);
}