Java Code Examples for org.apache.curator.RetryLoop#callWithRetry()

The following examples show how to use org.apache.curator.RetryLoop#callWithRetry() . 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: TestThreadLocalRetryLoop.java    From curator with Apache License 2.0 6 votes vote down vote up
private Void doOperation(CuratorFramework client) throws Exception
{
    try
    {
        RetryLoop.callWithRetry(client.getZookeeperClient(), () -> {
            client.checkExists().forPath("/hey");
            return null;
        });
        Assert.fail("Should have thrown an exception");
    }
    catch ( KeeperException dummy )
    {
        // correct
    }
    return null;
}
 
Example 2
Source File: AddWatchBuilderImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
private void pathInForeground(final String path) throws Exception
{
    final String fixedPath = client.fixForNamespace(path);
    OperationTrace trace = client.getZookeeperClient().startAdvancedTracer("AddWatchBuilderImpl-Foreground");
    RetryLoop.callWithRetry
    (
        client.getZookeeperClient(), () -> {
            if ( watching.isWatched() )
            {
                client.getZooKeeper().addWatch(fixedPath, mode);
            }
            else
            {
                client.getZooKeeper().addWatch(fixedPath, watching.getWatcher(path), mode);
            }
            return null;
        });
    trace.setPath(fixedPath).setWithWatcher(true).commit();
}
 
Example 3
Source File: SetACLBuilderImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
private Stat pathInForeground(final String path, final List<ACL> aclList) throws Exception
{
    OperationTrace   trace = client.getZookeeperClient().startAdvancedTracer("SetACLBuilderImpl-Foreground");
    Stat        resultStat = RetryLoop.callWithRetry
    (
        client.getZookeeperClient(),
        new Callable<Stat>()
        {
            @Override
            public Stat call() throws Exception
            {
                return client.getZooKeeper().setACL(path, aclList, version);
            }
        }
    );
    trace.setPath(path).setStat(resultStat).commit();
    return resultStat;
}
 
Example 4
Source File: CuratorTransactionImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
@Override
public Collection<CuratorTransactionResult> commit() throws Exception
{
    Preconditions.checkState(!isCommitted, "transaction already committed");
    isCommitted = true;

    List<OpResult> resultList = RetryLoop.callWithRetry
        (
            client.getZookeeperClient(),
            new Callable<List<OpResult>>()
            {
                @Override
                public List<OpResult> call() throws Exception
                {
                    return doOperation();
                }
            }
        );

    if ( resultList.size() != transaction.metadataSize() )
    {
        throw new IllegalStateException(String.format("Result size (%d) doesn't match input size (%d)", resultList.size(), transaction.metadataSize()));
    }

    return wrapResults(client, resultList, transaction);
}
 
Example 5
Source File: ReconfigBuilderImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
private byte[] ensembleInForeground() throws Exception
{
    TimeTrace trace = client.getZookeeperClient().startTracer("ReconfigBuilderImpl-Foreground");
    byte[] responseData = RetryLoop.callWithRetry
        (
            client.getZookeeperClient(),
            new Callable<byte[]>()
            {
                @Override
                public byte[] call() throws Exception
                {
                    return ((ZooKeeperAdmin)client.getZooKeeper()).reconfigure(joining, leaving, newMembers, fromConfig, responseStat);
                }
            }
        );
    trace.commit();
    return responseData;
}
 
Example 6
Source File: SetDataBuilderImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
private Stat pathInForeground(final String path, final byte[] data) throws Exception
{
    OperationTrace   trace = client.getZookeeperClient().startAdvancedTracer("SetDataBuilderImpl-Foreground");
    Stat        resultStat = RetryLoop.callWithRetry
    (
        client.getZookeeperClient(),
        new Callable<Stat>()
        {
            @Override
            public Stat call() throws Exception
            {
                return client.getZooKeeper().setData(path, data, version);
            }
        }
    );
    trace.setRequestBytesLength(data).setPath(path).setStat(resultStat).commit();
    return resultStat;
}
 
Example 7
Source File: TempGetDataBuilderImpl.java    From curator with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] forPath(String path) throws Exception
{
    final String    localPath = client.fixForNamespace(path);

    OperationTrace       trace = client.getZookeeperClient().startAdvancedTracer("GetDataBuilderImpl-Foreground");
    byte[]          responseData = RetryLoop.callWithRetry
    (
        client.getZookeeperClient(),
        new Callable<byte[]>()
        {
            @Override
            public byte[] call() throws Exception
            {
                return client.getZooKeeper().getData(localPath, false, responseStat);
            }
        }
    );
    trace.setResponseBytesLength(responseData).setPath(path).setStat(responseStat).commit();

    return decompress ? client.getCompressionProvider().decompress(path, responseData) : responseData;
}
 
Example 8
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 9
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 10
Source File: GetACLBuilderImpl.java    From xian with Apache License 2.0 6 votes vote down vote up
private List<ACL> pathInForeground(final String path) throws Exception
{
    OperationTrace    trace = client.getZookeeperClient().startAdvancedTracer("GetACLBuilderImpl-Foreground");
    List<ACL>    result = RetryLoop.callWithRetry
    (
        client.getZookeeperClient(),
        new Callable<List<ACL>>()
        {
            @Override
            public List<ACL> call() throws Exception
            {
                return client.getZooKeeper().getACL(path, responseStat);
            }
        }
    );
    trace.setPath(path).setStat(responseStat).commit();
    return result;
}
 
Example 11
Source File: SetACLBuilderImpl.java    From xian with Apache License 2.0 6 votes vote down vote up
private Stat pathInForeground(final String path) throws Exception
{
    OperationTrace   trace = client.getZookeeperClient().startAdvancedTracer("SetACLBuilderImpl-Foreground");
    Stat        resultStat = RetryLoop.callWithRetry
    (
        client.getZookeeperClient(),
        new Callable<Stat>()
        {
            @Override
            public Stat call() throws Exception
            {
                return client.getZooKeeper().setACL(path, acling.getAclList(path), version);
            }
        }
    );
    trace.setPath(path).setStat(resultStat).commit();
    return resultStat;
}
 
Example 12
Source File: SetDataBuilderImpl.java    From xian with Apache License 2.0 6 votes vote down vote up
private Stat pathInForeground(final String path, final byte[] data) throws Exception
{
    OperationTrace   trace = client.getZookeeperClient().startAdvancedTracer("SetDataBuilderImpl-Foreground");
    Stat        resultStat = RetryLoop.callWithRetry
    (
        client.getZookeeperClient(),
        new Callable<Stat>()
        {
            @Override
            public Stat call() throws Exception
            {
                return client.getZooKeeper().setData(path, data, version);
            }
        }
    );
    trace.setRequestBytesLength(data).setPath(path).setStat(resultStat).commit();
    return resultStat;
}
 
Example 13
Source File: TempGetDataBuilderImpl.java    From xian with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] forPath(String path) throws Exception
{
    final String    localPath = client.fixForNamespace(path);

    OperationTrace       trace = client.getZookeeperClient().startAdvancedTracer("GetDataBuilderImpl-Foreground");
    byte[]          responseData = RetryLoop.callWithRetry
    (
        client.getZookeeperClient(),
        new Callable<byte[]>()
        {
            @Override
            public byte[] call() throws Exception
            {
                return client.getZooKeeper().getData(localPath, false, responseStat);
            }
        }
    );
    trace.setResponseBytesLength(responseData).setPath(path).setStat(responseStat).commit();

    return decompress ? client.getCompressionProvider().decompress(path, responseData) : responseData;
}
 
Example 14
Source File: CuratorTransactionImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<CuratorTransactionResult> commit() throws Exception
{
    Preconditions.checkState(!isCommitted, "transaction already committed");
    isCommitted = true;

    final AtomicBoolean firstTime = new AtomicBoolean(true);
    List<OpResult>      resultList = RetryLoop.callWithRetry
    (
        client.getZookeeperClient(),
        new Callable<List<OpResult>>()
        {
            @Override
            public List<OpResult> call() throws Exception
            {
                return doOperation(firstTime);
            }
        }
    );
    
    if ( resultList.size() != transaction.metadataSize() )
    {
        throw new IllegalStateException(String.format("Result size (%d) doesn't match input size (%d)", resultList.size(), transaction.metadataSize()));
    }

    ImmutableList.Builder<CuratorTransactionResult>     builder = ImmutableList.builder();
    for ( int i = 0; i < resultList.size(); ++i )
    {
        OpResult                                    opResult = resultList.get(i);
        CuratorMultiTransactionRecord.TypeAndPath   metadata = transaction.getMetadata(i);
        CuratorTransactionResult                    curatorResult = makeCuratorResult(opResult, metadata);
        builder.add(curatorResult);
    }

    return builder.build();
}
 
Example 15
Source File: GetConfigBuilderImpl.java    From curator with Apache License 2.0 5 votes vote down vote up
private byte[] configInForeground() throws Exception
{
    TimeTrace trace = client.getZookeeperClient().startTracer("GetConfigBuilderImpl-Foreground");
    try
    {
        return RetryLoop.callWithRetry
        (
            client.getZookeeperClient(),
            new Callable<byte[]>()
            {
                @Override
                public byte[] call() throws Exception
                {
                    if ( watching.isWatched() )
                    {
                        return client.getZooKeeper().getConfig(true, stat);
                    }
                    byte[] config = client.getZooKeeper().getConfig(watching.getWatcher(ZooDefs.CONFIG_NODE), stat);
                    watching.commitWatcher(KeeperException.NoNodeException.Code.OK.intValue(), false);
                    return config;
                }
            }
        );
    }
    finally
    {
        trace.commit();
    }
}
 
Example 16
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 17
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 18
Source File: CreateBuilderImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
private String findProtectedNodeInForeground(final String path) throws Exception
{
    OperationTrace trace = client.getZookeeperClient().startAdvancedTracer("CreateBuilderImpl-findProtectedNodeInForeground");

    String returnPath = RetryLoop.callWithRetry
        (
            client.getZookeeperClient(),
            new Callable<String>()
            {
                @Override
                public String call() throws Exception
                {
                    String foundNode = null;
                    try
                    {
                        final ZKPaths.PathAndNode pathAndNode = ZKPaths.getPathAndNode(path);
                        List<String> children = client.getZooKeeper().getChildren(pathAndNode.getPath(), false);

                        foundNode = findNode(children, pathAndNode.getPath(), protectedId);
                    }
                    catch ( KeeperException.NoNodeException ignore )
                    {
                        // ignore
                    }
                    return foundNode;
                }
            }
        );

    trace.setPath(path).commit();
    return returnPath;
}
 
Example 19
Source File: CreateBuilderImpl.java    From xian with Apache License 2.0 4 votes vote down vote up
private String pathInForeground(final String path, final byte[] data) throws Exception
{
    OperationTrace trace = client.getZookeeperClient().startAdvancedTracer("CreateBuilderImpl-Foreground");

    final AtomicBoolean firstTime = new AtomicBoolean(true);
    String returnPath = RetryLoop.callWithRetry
        (
            client.getZookeeperClient(),
            new Callable<String>()
            {
                @Override
                public String call() throws Exception
                {
                    boolean localFirstTime = firstTime.getAndSet(false) && !debugForceFindProtectedNode;

                    String createdPath = null;
                    if ( !localFirstTime && doProtected )
                    {
                        debugForceFindProtectedNode = false;
                        createdPath = findProtectedNodeInForeground(path);
                    }

                    if ( createdPath == null )
                    {
                        try
                        {
                            createdPath = client.getZooKeeper().create(path, data, acling.getAclList(path), createMode);
                        }
                        catch ( KeeperException.NoNodeException e )
                        {
                            if ( createParentsIfNeeded )
                            {
                                ZKPaths.mkdirs(client.getZooKeeper(), path, false, client.getAclProvider(), createParentsAsContainers);
                                createdPath = client.getZooKeeper().create(path, data, acling.getAclList(path), createMode);
                            }
                            else
                            {
                                throw e;
                            }
                        }
                    }

                    if ( failNextCreateForTesting )
                    {
                        failNextCreateForTesting = false;
                        throw new KeeperException.ConnectionLossException();
                    }
                    return createdPath;
                }
            }
        );

    trace.setRequestBytesLength(data).setPath(path).commit();
    return returnPath;
}
 
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);
    }
}