Java Code Examples for org.apache.curator.framework.api.CuratorEvent#getResultCode()

The following examples show how to use org.apache.curator.framework.api.CuratorEvent#getResultCode() . 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: PersistentNode.java    From xian with Apache License 2.0 6 votes vote down vote up
private void processBackgroundCallbackClosedState(CuratorEvent event)
{
    String path = null;
    if ( event.getResultCode() == KeeperException.Code.NODEEXISTS.intValue() )
    {
        path = event.getPath();
    }
    else if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
    {
        path = event.getName();
    }

    if ( path != null )
    {
        try
        {
            client.delete().guaranteed().inBackground().forPath(path);
        }
        catch ( Exception e )
        {
            log.error("Could not delete node after close", e);
        }
    }
}
 
Example 2
Source File: PathChildrenCache.java    From xian with Apache License 2.0 6 votes vote down vote up
void refresh(final RefreshMode mode) throws Exception
{
    ensurePath();

    final BackgroundCallback callback = new BackgroundCallback()
    {
        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception
        {
            if (PathChildrenCache.this.state.get().equals(State.CLOSED)) {
                // This ship is closed, don't handle the callback
                return;
            }
            if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                processChildren(event.getChildren(), mode);
            }
        }
    };

    client.getChildren().usingWatcher(childrenWatcher).inBackground(callback).forPath(path);
}
 
Example 3
Source File: ZKHostIndex.java    From pravega with Apache License 2.0 6 votes vote down vote up
private StoreException translateErrorCode(String path, CuratorEvent event) {
    StoreException ex;
    if (event.getResultCode() == KeeperException.Code.CONNECTIONLOSS.intValue() ||
            event.getResultCode() == KeeperException.Code.SESSIONEXPIRED.intValue() ||
            event.getResultCode() == KeeperException.Code.SESSIONMOVED.intValue() ||
            event.getResultCode() == KeeperException.Code.OPERATIONTIMEOUT.intValue()) {
        ex = StoreException.create(StoreException.Type.CONNECTION_ERROR, path);
    } else if (event.getResultCode() == KeeperException.Code.NODEEXISTS.intValue()) {
        ex = StoreException.create(StoreException.Type.DATA_EXISTS, path);
    } else if (event.getResultCode() == KeeperException.Code.BADVERSION.intValue()) {
        ex = StoreException.create(StoreException.Type.WRITE_CONFLICT, path);
    } else if (event.getResultCode() == KeeperException.Code.NONODE.intValue()) {
        ex = StoreException.create(StoreException.Type.DATA_NOT_FOUND, path);
    } else if (event.getResultCode() == KeeperException.Code.NOTEMPTY.intValue()) {
        ex = StoreException.create(StoreException.Type.DATA_CONTAINS_ELEMENTS, path);
    } else {
        ex = StoreException.create(StoreException.Type.UNKNOWN,
                KeeperException.create(KeeperException.Code.get(event.getResultCode()), path));
    }
    return ex;
}
 
Example 4
Source File: PersistentNode.java    From curator with Apache License 2.0 6 votes vote down vote up
private void processBackgroundCallbackClosedState(CuratorEvent event)
{
    String path = null;
    if ( event.getResultCode() == KeeperException.Code.NODEEXISTS.intValue() )
    {
        path = event.getPath();
    }
    else if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
    {
        path = event.getName();
    }

    if ( path != null )
    {
        try
        {
            client.delete().guaranteed().inBackground().forPath(path);
        }
        catch ( Exception e )
        {
            log.error("Could not delete node after close", e);
        }
    }
}
 
Example 5
Source File: LeaderLatch.java    From curator with Apache License 2.0 5 votes vote down vote up
private void getChildren() throws Exception
{
    BackgroundCallback callback = new BackgroundCallback()
    {
        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception
        {
            if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                checkLeadership(event.getChildren());
            }
        }
    };
    client.getChildren().inBackground(callback).forPath(ZKPaths.makePath(latchPath, null));
}
 
Example 6
Source File: LeaderLatch.java    From curator with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void reset() throws Exception
{
    setLeadership(false);
    setNode(null);

    BackgroundCallback callback = new BackgroundCallback()
    {
        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception
        {
            if ( debugResetWaitLatch != null )
            {
                debugResetWaitLatch.await();
                debugResetWaitLatch = null;
            }

            if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                setNode(event.getName());
                if ( state.get() == State.CLOSED )
                {
                    setNode(null);
                }
                else
                {
                    getChildren();
                }
            }
            else
            {
                log.error("getChildren() failed. rc = " + event.getResultCode());
            }
        }
    };
    client.create().creatingParentContainersIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).inBackground(callback).forPath(ZKPaths.makePath(latchPath, LOCK_NAME), LeaderSelector.getIdBytes(id));
}
 
Example 7
Source File: DistributedQueue.java    From curator with Apache License 2.0 5 votes vote down vote up
private void doPutInBackground(final T item, String path, final MultiItem<T> givenMultiItem, byte[] bytes) throws Exception
{
    BackgroundCallback callback = new BackgroundCallback()
    {
        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception
        {
            if ( event.getResultCode() != KeeperException.Code.OK.intValue() )
            {
                return;
            }

            if ( event.getType() == CuratorEventType.CREATE )
            {
                synchronized(putCount)
                {
                    putCount.decrementAndGet();
                    putCount.notifyAll();
                }
            }

            putListenerContainer.forEach(listener -> {
                if ( item != null )
                {
                    listener.putCompleted(item);
                }
                else
                {
                    listener.putMultiCompleted(givenMultiItem);
                }
            });
        }
    };
    internalCreateNode(path, bytes, callback);
}
 
Example 8
Source File: ChildrenCache.java    From curator with Apache License 2.0 5 votes vote down vote up
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception
{
    if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
    {
        setNewChildren(event.getChildren());
    }
}
 
Example 9
Source File: NodeCache.java    From curator with Apache License 2.0 5 votes vote down vote up
private void processBackgroundResult(CuratorEvent event) throws Exception
{
    switch ( event.getType() )
    {
        case GET_DATA:
        {
            if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                ChildData childData = new ChildData(path, event.getStat(), event.getData());
                setNewData(childData);
            }
            break;
        }

        case EXISTS:
        {
            if ( event.getResultCode() == KeeperException.Code.NONODE.intValue() )
            {
                setNewData(null);
            }
            else if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                if ( dataIsCompressed )
                {
                    client.getData().decompressed().usingWatcher(watcher).inBackground(backgroundCallback).forPath(path);
                }
                else
                {
                    client.getData().usingWatcher(watcher).inBackground(backgroundCallback).forPath(path);
                }
            }
            break;
        }
    }
}
 
Example 10
Source File: PathChildrenCache.java    From curator with Apache License 2.0 5 votes vote down vote up
void refresh(final RefreshMode mode) throws Exception
{
    ensurePath();

    final BackgroundCallback callback = new BackgroundCallback()
    {
        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception
        {
            if ( reRemoveWatchersOnBackgroundClosed() )
            {
                return;
            }
            if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                processChildren(event.getChildren(), mode);
            }
            else if ( event.getResultCode() == KeeperException.Code.NONODE.intValue() )
            {
                if ( mode == RefreshMode.NO_NODE_EXCEPTION )
                {
                    log.debug("KeeperException.NoNodeException received for getChildren() and refresh has failed. Resetting ensureContainers but not refreshing. Path: [{}]", path);
                    ensureContainers.reset();
                }
                else
                {
                    log.debug("KeeperException.NoNodeException received for getChildren(). Resetting ensureContainers. Path: [{}]", path);
                    ensureContainers.reset();
                    offerOperation(new RefreshOperation(PathChildrenCache.this, RefreshMode.NO_NODE_EXCEPTION));
                }
            }
        }
    };

    client.getChildren().usingWatcher(childrenWatcher).inBackground(callback).forPath(path);
}
 
Example 11
Source File: PersistentNode.java    From curator with Apache License 2.0 5 votes vote down vote up
private void processBackgroundCallback(CuratorEvent event) throws Exception
{
    String path = null;
    boolean nodeExists = false;
    if ( event.getResultCode() == KeeperException.Code.NODEEXISTS.intValue() )
    {
        path = event.getPath();
        nodeExists = true;
    }
    else if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
    {
        path = event.getName();
    }
    else if ( event.getResultCode() == KeeperException.Code.NOAUTH.intValue() )
    {
        log.warn("Client does not have authorisation to create node at path {}", event.getPath());
        authFailure.set(true);
        return;
    }
    if ( path != null )
    {
        authFailure.set(false);
        nodePath.set(path);
        watchNode();

        if ( nodeExists )
        {
            client.setData().inBackground(setDataCallback).forPath(getActualPath(), getData());
        }
        else
        {
            initialisationComplete();
            notifyListeners();
        }
    }
    else
    {
        createNode();
    }
}
 
Example 12
Source File: LeaderLatch.java    From xian with Apache License 2.0 5 votes vote down vote up
private void getChildren() throws Exception
{
    BackgroundCallback callback = new BackgroundCallback()
    {
        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception
        {
            if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                checkLeadership(event.getChildren());
            }
        }
    };
    client.getChildren().inBackground(callback).forPath(ZKPaths.makePath(latchPath, null));
}
 
Example 13
Source File: LeaderLatch.java    From xian with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
void reset() throws Exception
{
    setLeadership(false);
    setNode(null);

    BackgroundCallback callback = new BackgroundCallback()
    {
        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception
        {
            if ( debugResetWaitLatch != null )
            {
                debugResetWaitLatch.await();
                debugResetWaitLatch = null;
            }

            if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                setNode(event.getName());
                if ( state.get() == State.CLOSED )
                {
                    setNode(null);
                }
                else
                {
                    getChildren();
                }
            }
            else
            {
                log.error("getChildren() failed. rc = " + event.getResultCode());
            }
        }
    };
    client.create().creatingParentContainersIfNeeded().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).inBackground(callback).forPath(ZKPaths.makePath(latchPath, LOCK_NAME), LeaderSelector.getIdBytes(id));
}
 
Example 14
Source File: ChildrenCache.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception
{
    if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
    {
        setNewChildren(event.getChildren());
    }
}
 
Example 15
Source File: NodeCache.java    From xian with Apache License 2.0 5 votes vote down vote up
private void processBackgroundResult(CuratorEvent event) throws Exception
{
    switch ( event.getType() )
    {
        case GET_DATA:
        {
            if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                ChildData childData = new ChildData(path, event.getStat(), event.getData());
                setNewData(childData);
            }
            break;
        }

        case EXISTS:
        {
            if ( event.getResultCode() == KeeperException.Code.NONODE.intValue() )
            {
                setNewData(null);
            }
            else if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
            {
                if ( dataIsCompressed )
                {
                    client.getData().decompressed().usingWatcher(watcher).inBackground(backgroundCallback).forPath(path);
                }
                else
                {
                    client.getData().usingWatcher(watcher).inBackground(backgroundCallback).forPath(path);
                }
            }
            break;
        }
    }
}
 
Example 16
Source File: PersistentNode.java    From xian with Apache License 2.0 5 votes vote down vote up
private void processBackgroundCallback(CuratorEvent event) throws Exception
{
    String path = null;
    boolean nodeExists = false;
    if ( event.getResultCode() == KeeperException.Code.NODEEXISTS.intValue() )
    {
        path = event.getPath();
        nodeExists = true;
    }
    else if ( event.getResultCode() == KeeperException.Code.OK.intValue() )
    {
        path = event.getName();
    }
    else if ( event.getResultCode() == KeeperException.Code.NOAUTH.intValue() )
    {
        log.warn("Client does not have authorisation to write node at path {}", event.getPath());
        authFailure.set(true);
        return;
    }
    if ( path != null )
    {
        authFailure.set(false);
        nodePath.set(path);
        watchNode();

        if ( nodeExists )
        {
            client.setData().inBackground(setDataCallback).forPath(getActualPath(), getData());
        }
        else
        {
            initialisationComplete();
        }
    }
    else
    {
        createNode();
    }
}
 
Example 17
Source File: DistributedQueue.java    From xian with Apache License 2.0 4 votes vote down vote up
private void doPutInBackground(final T item, String path, final MultiItem<T> givenMultiItem, byte[] bytes) throws Exception
{
    BackgroundCallback callback = new BackgroundCallback()
    {
        @Override
        public void processResult(CuratorFramework client, CuratorEvent event) throws Exception
        {
            if ( event.getResultCode() != KeeperException.Code.OK.intValue() )
            {
                return;
            }

            if ( event.getType() == CuratorEventType.CREATE )
            {
                synchronized(putCount)
                {
                    putCount.decrementAndGet();
                    putCount.notifyAll();
                }
            }

            putListenerContainer.forEach
            (
                new Function<QueuePutListener<T>, Void>()
                {
                    @Override
                    public Void apply(QueuePutListener<T> listener)
                    {
                        if ( item != null )
                        {
                            listener.putCompleted(item);
                        }
                        else
                        {
                            listener.putMultiCompleted(givenMultiItem);
                        }
                        return null;
                    }
                }
            );
        }
    };
    internalCreateNode(path, bytes, callback);
}