org.apache.curator.CuratorZookeeperClient Java Examples

The following examples show how to use org.apache.curator.CuratorZookeeperClient. 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: EnsurePath.java    From curator with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void ensure(final CuratorZookeeperClient client, final String path, final boolean makeLastNode) throws Exception
{
    if ( !isSet )
    {
        RetryLoop.callWithRetry
            (
                client,
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        ZKPaths.mkdirs(client.getZooKeeper(), path, makeLastNode, aclProvider, asContainers());
                        helper.set(doNothingHelper);
                        isSet = true;
                        return null;
                    }
                }
            );
    }
}
 
Example #2
Source File: EnsurePath.java    From xian with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void ensure(final CuratorZookeeperClient client, final String path, final boolean makeLastNode) throws Exception
{
    if ( !isSet )
    {
        RetryLoop.callWithRetry
            (
                client,
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        ZKPaths.mkdirs(client.getZooKeeper(), path, makeLastNode, aclProvider, asContainers());
                        helper.set(doNothingHelper);
                        isSet = true;
                        return null;
                    }
                }
            );
    }
}
 
Example #3
Source File: MasterRespondsWithNoZkTest.java    From helios with Apache License 2.0 5 votes vote down vote up
@Override
public CuratorFramework newClient(final String connectString, final int sessionTimeoutMs,
                                  final int connectionTimeoutMs, final RetryPolicy retryPolicy,
                                  final ACLProvider aclProvider,
                                  final List<AuthInfo> authorization) {
  final CuratorFramework curator = mock(CuratorFramework.class);

  final RetryLoop retryLoop = mock(RetryLoop.class);
  when(retryLoop.shouldContinue()).thenReturn(false);

  final CuratorZookeeperClient czkClient = mock(CuratorZookeeperClient.class);
  when(czkClient.newRetryLoop()).thenReturn(retryLoop);

  when(curator.getZookeeperClient()).thenReturn(czkClient);

  @SuppressWarnings("unchecked") final Listenable<ConnectionStateListener> mockListener =
      (Listenable<ConnectionStateListener>) mock(Listenable.class);

  when(curator.getConnectionStateListenable()).thenReturn(mockListener);

  final GetChildrenBuilder builder = mock(GetChildrenBuilder.class);
  when(curator.getChildren()).thenReturn(builder);

  try {
    when(builder.forPath(anyString())).thenThrow(
        new KeeperException.ConnectionLossException());
  } catch (Exception ignored) {
    // never throws
  }
  when(curator.newNamespaceAwareEnsurePath(anyString())).thenReturn(mock(EnsurePath.class));

  return curator;
}
 
Example #4
Source File: TestExhibitorEnsembleProvider.java    From xian with Apache License 2.0 5 votes vote down vote up
@Test
public void     testSimple() throws Exception
{
    Exhibitors                  exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, dummyConnectionStringProvider);
    ExhibitorRestClient         mockRestClient = new ExhibitorRestClient()
    {
        @Override
        public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception
        {
            return "count=1&port=" + server.getPort() + "&server0=localhost";
        }
    };
    ExhibitorEnsembleProvider   provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1));
    provider.pollForInitialEnsemble();

    Timing                      timing = new Timing();
    CuratorZookeeperClient      client = new CuratorZookeeperClient(provider, timing.session(), timing.connection(), null, new ExponentialBackoffRetry(timing.milliseconds(), 3));
    client.start();
    try
    {
        client.blockUntilConnectedOrTimedOut();
        client.getZooKeeper().exists("/", false);
    }
    catch ( Exception e )
    {
        Assert.fail("provider.getConnectionString(): " + provider.getConnectionString() + " server.getPort(): " + server.getPort(), e);
    }
    finally
    {
        client.close();
    }
}
 
Example #5
Source File: NamespaceImpl.java    From curator with Apache License 2.0 5 votes vote down vote up
String    fixForNamespace(String path, boolean isSequential)
{
    if ( ensurePathNeeded.get() )
    {
        try
        {
            final CuratorZookeeperClient zookeeperClient = client.getZookeeperClient();
            RetryLoop.callWithRetry
            (
                zookeeperClient,
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        ZKPaths.mkdirs(zookeeperClient.getZooKeeper(), ZKPaths.makePath("/", namespace), true, client.getAclProvider(), true);
                        return null;
                    }
                }
            );
            ensurePathNeeded.set(false);
        }
        catch ( Exception e )
        {
            ThreadUtils.checkInterrupted(e);
            client.logError("Ensure path threw exception", e);
        }
    }

    return ZKPaths.fixForNamespace(namespace, path, isSequential);
}
 
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: NamespaceImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
String    fixForNamespace(String path, boolean isSequential)
{
    if ( ensurePathNeeded.get() )
    {
        try
        {
            final CuratorZookeeperClient zookeeperClient = client.getZookeeperClient();
            RetryLoop.callWithRetry
            (
                zookeeperClient,
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        ZKPaths.mkdirs(zookeeperClient.getZooKeeper(), ZKPaths.makePath("/", namespace), true, client.getAclProvider(), true);
                        return null;
                    }
                }
            );
            ensurePathNeeded.set(false);
        }
        catch ( Exception e )
        {
            ThreadUtils.checkInterrupted(e);
            client.logError("Ensure path threw exception", e);
        }
    }

    return ZKPaths.fixForNamespace(namespace, path, isSequential);
}
 
Example #8
Source File: ZooKeeperServerResource.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
  testingServer = new TestingServer(true);
  zkClient = new CuratorZookeeperClient(testingServer.getConnectString(), 5000, 5000, null, new RetryOneTime(1000));
  zkClient.start();
  zkClient.blockUntilConnectedOrTimedOut();
}
 
Example #9
Source File: MetricCollectorHATest.java    From ambari-metrics with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmbeddedModeCollectorZK() throws Exception {


  BoundedExponentialBackoffRetry retryPolicyMock = PowerMock.createMock(BoundedExponentialBackoffRetry.class);
  expectNew(BoundedExponentialBackoffRetry.class, 1000, 10000, 1).andReturn(retryPolicyMock);

  CuratorZookeeperClient clientMock = PowerMock.createMock(CuratorZookeeperClient.class);
  expectNew(CuratorZookeeperClient.class, "zkQ", 10000, 2000, null, retryPolicyMock)
    .andReturn(clientMock);

  clientMock.start();
  expectLastCall().once();

  clientMock.close();
  expectLastCall().once();

  ZooKeeper zkMock = PowerMock.createMock(ZooKeeper.class);
  expect(clientMock.getZooKeeper()).andReturn(zkMock).once();

  expect(zkMock.exists("/ambari-metrics-cluster", false)).andReturn(null).once();

  replayAll();
  MetricCollectorHAHelper metricCollectorHAHelper = new MetricCollectorHAHelper("zkQ", 1, 1000);
  Collection<String> liveInstances = metricCollectorHAHelper.findLiveCollectorHostsFromZNode();
  verifyAll();
  Assert.assertTrue(liveInstances.isEmpty());
}
 
Example #10
Source File: CuratorConnector.java    From jumbune with GNU Lesser General Public License v3.0 5 votes vote down vote up
public CuratorZookeeperClient getZookeeperClient() throws Exception {
        CuratorFramework client = getOrCreateClient();
        client.start();
        CuratorZookeeperClient zkClient = client.getZookeeperClient();
        client.close();
        return zkClient;
	
}
 
Example #11
Source File: MockCurator.java    From vespa with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorZookeeperClient getZookeeperClient() {
    throw new UnsupportedOperationException("Not implemented in MockCurator");
}
 
Example #12
Source File: ZooKeeperTest.java    From curator-extensions with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("UnusedDeclaration")
public void killSession(CuratorFramework curator) throws Exception {
    CuratorZookeeperClient client = curator.getZookeeperClient();
    KillSession.kill(client.getZooKeeper(), client.getCurrentConnectionString());
}
 
Example #13
Source File: EnsurePath.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public void ensure(CuratorZookeeperClient client, String path, final boolean makeLastNode) throws Exception
{
    // NOP
}
 
Example #14
Source File: WatcherRemovalFacade.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorZookeeperClient getZookeeperClient()
{
    return client.getZookeeperClient();
}
 
Example #15
Source File: CuratorFrameworkImpl.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorZookeeperClient getZookeeperClient()
{
    return client;
}
 
Example #16
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 #17
Source File: NamespaceFacade.java    From curator with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorZookeeperClient getZookeeperClient()
{
    return client.getZookeeperClient();
}
 
Example #18
Source File: CuratorFrameworkImpl.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorZookeeperClient getZookeeperClient()
{
    return client;
}
 
Example #19
Source File: NamespaceFacade.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public CuratorZookeeperClient getZookeeperClient()
{
    return client.getZookeeperClient();
}
 
Example #20
Source File: TestExhibitorEnsembleProvider.java    From xian with Apache License 2.0 4 votes vote down vote up
@Test
public void     testChanging() throws Exception
{
    TestingServer               secondServer = new TestingServer();
    try
    {
        String                          mainConnectionString = "count=1&port=" + server.getPort() + "&server0=localhost";
        String                          secondConnectionString = "count=1&port=" + secondServer.getPort() + "&server0=localhost";

        final Semaphore                 semaphore = new Semaphore(0);
        final AtomicReference<String>   connectionString = new AtomicReference<String>(mainConnectionString);
        Exhibitors                      exhibitors = new Exhibitors(Lists.newArrayList("foo", "bar"), 1000, dummyConnectionStringProvider);
        ExhibitorRestClient             mockRestClient = new ExhibitorRestClient()
        {
            @Override
            public String getRaw(String hostname, int port, String uriPath, String mimeType) throws Exception
            {
                semaphore.release();
                return connectionString.get();
            }
        };
        ExhibitorEnsembleProvider   provider = new ExhibitorEnsembleProvider(exhibitors, mockRestClient, "/foo", 10, new RetryOneTime(1));
        provider.pollForInitialEnsemble();

        Timing                          timing = new Timing().multiple(4);
        final CuratorZookeeperClient    client = new CuratorZookeeperClient(provider, timing.session(), timing.connection(), null, new RetryOneTime(2));
        client.start();
        try
        {
            RetryLoop.callWithRetry
            (
                client,
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        client.getZooKeeper().create("/test", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                        return null;
                    }
                }
            );

            connectionString.set(secondConnectionString);
            semaphore.drainPermits();
            semaphore.acquire();

            server.stop();  // create situation where the current zookeeper gets a sys-disconnected

            Stat        stat = RetryLoop.callWithRetry
            (
                client,
                new Callable<Stat>()
                {
                    @Override
                    public Stat call() throws Exception
                    {
                        return client.getZooKeeper().exists("/test", false);
                    }
                }
            );
            Assert.assertNull(stat);    // it's a different server so should be null
        }
        finally
        {
            client.close();
        }
    }
    finally
    {
        CloseableUtils.closeQuietly(secondServer);
    }
}
 
Example #21
Source File: EnsurePath.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public void ensure(CuratorZookeeperClient client, String path, final boolean makeLastNode) throws Exception
{
    // NOP
}
 
Example #22
Source File: CuratorFramework.java    From xian with Apache License 2.0 2 votes vote down vote up
/**
 * Return the managed zookeeper client
 *
 * @return client
 */
public CuratorZookeeperClient getZookeeperClient();
 
Example #23
Source File: CuratorFramework.java    From curator with Apache License 2.0 2 votes vote down vote up
/**
 * Return the managed zookeeper client
 *
 * @return client
 */
public CuratorZookeeperClient getZookeeperClient();
 
Example #24
Source File: EnsurePath.java    From curator with Apache License 2.0 2 votes vote down vote up
/**
 * First time, synchronizes and makes sure all nodes in the path are created. Subsequent calls
 * with this instance are NOPs.
 *
 * @param client ZK client
 * @throws Exception ZK errors
 */
public void ensure(CuratorZookeeperClient client) throws Exception
{
    Helper localHelper = helper.get();
    localHelper.ensure(client, path, makeLastNode);
}
 
Example #25
Source File: EnsurePath.java    From xian with Apache License 2.0 2 votes vote down vote up
/**
 * First time, synchronizes and makes sure all nodes in the path are created. Subsequent calls
 * with this instance are NOPs.
 *
 * @param client ZK client
 * @throws Exception ZK errors
 */
public void ensure(CuratorZookeeperClient client) throws Exception
{
    Helper localHelper = helper.get();
    localHelper.ensure(client, path, makeLastNode);
}
 
Example #26
Source File: EnsurePath.java    From curator with Apache License 2.0 votes vote down vote up
public void ensure(CuratorZookeeperClient client, String path, final boolean makeLastNode) throws Exception; 
Example #27
Source File: EnsurePath.java    From xian with Apache License 2.0 votes vote down vote up
public void ensure(CuratorZookeeperClient client, String path, final boolean makeLastNode) throws Exception;