Example usage for org.apache.zookeeper KeeperException code

List of usage examples for org.apache.zookeeper KeeperException code

Introduction

In this page you can find the example usage for org.apache.zookeeper KeeperException code.

Prototype

Code code

To view the source code for org.apache.zookeeper KeeperException code.

Click Source Link

Usage

From source file:com.navercorp.pinpoint.collector.cluster.zookeeper.ZookeeperClient.java

License:Apache License

public List<String> getChildrenNode(String path, boolean watch)
        throws PinpointZookeeperException, InterruptedException {
    checkState();//  w  w  w  .  j  a  va  2 s.  co m

    try {
        List<String> childNodeList = zookeeper.getChildren(path, watch, null);

        logger.info("ChildNode List = {}", childNodeList);
        return childNodeList;
    } catch (KeeperException exception) {
        if (exception.code() != Code.NONODE) {
            handleException(exception);
        }
    }

    return Collections.emptyList();
}

From source file:com.navercorp.pinpoint.common.server.cluster.zookeeper.ZookeeperExceptionResolver.java

License:Apache License

static PinpointZookeeperException resolve(Exception exception) {
    if (exception instanceof KeeperException) {
        KeeperException keeperException = (KeeperException) exception;
        switch (keeperException.code()) {
        case CONNECTIONLOSS:
        case SESSIONEXPIRED:
            return new ConnectionException(keeperException.getMessage(), keeperException);
        case AUTHFAILED:
        case INVALIDACL:
        case NOAUTH:
            return new AuthException(keeperException.getMessage(), keeperException);
        case BADARGUMENTS:
        case BADVERSION:
        case NOCHILDRENFOREPHEMERALS:
        case NOTEMPTY:
        case NODEEXISTS:
            return new BadOperationException(keeperException.getMessage(), keeperException);
        case NONODE:
            return new NoNodeException(keeperException.getMessage(), keeperException);
        case OPERATIONTIMEOUT:
            return new TimeoutException(keeperException.getMessage(), keeperException);
        default://from   ww  w  .java 2 s .c o m
            return new UnknownException(keeperException.getMessage(), keeperException);
        }
    } else {
        return new UnknownException(exception.getMessage(), exception);
    }
}

From source file:com.navercorp.pinpoint.web.cluster.ClusterTest.java

License:Apache License

@Test
public void clusterTest2() throws Exception {
    ZooKeeper zookeeper = new ZooKeeper(zookeeperAddress, 5000, null);
    awaitZookeeperConnected(zookeeper);/*w w  w.j  a  v  a  2 s  .  c  om*/

    ts.stop();

    awaitZookeeperDisconnected(zookeeper);
    try {
        zookeeper.getData(CLUSTER_NODE_PATH, null, null);
        Assert.fail();
    } catch (KeeperException e) {
        Assert.assertEquals(KeeperException.Code.CONNECTIONLOSS, e.code());
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    ts.restart();
    getNodeAndCompareContents(zookeeper);

    if (zookeeper != null) {
        zookeeper.close();
    }
}

From source file:com.nearinfinity.blur.utils.BlurUtil.java

License:Apache License

public static void createIfMissing(ZooKeeper zookeeper, String path)
        throws KeeperException, InterruptedException {
    if (zookeeper.exists(path, false) == null) {
        try {/*from  w w w.  j  ava  2  s.  c o m*/
            zookeeper.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException e) {
            if (e.code() == Code.NODEEXISTS) {
                return;
            }
            throw e;
        }
    }
}

From source file:com.nearinfinity.blur.zookeeper.ZkUtils.java

License:Apache License

public static void deleteAnyVersion(ZooKeeper zk, String path) {
    try {//from w w  w .j  a v  a2s .  c  om
        List<String> children = zk.getChildren(path, false);
        for (String c : children) {
            deleteAnyVersion(zk, path + "/" + c);
        }
        zk.delete(path, ANY_VERSION);
    } catch (KeeperException e) {
        if (e.code() == KeeperException.Code.NONODE) {
            return;
        }
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.nearinfinity.mele.util.ZkUtils.java

License:Apache License

public static void deleteAnyVersion(ZooKeeper zk, String path) {
    try {//  w w  w . j  a  va 2 s. c o  m
        List<String> children = zk.getChildren(path, false);
        for (String c : children) {
            deleteAnyVersion(zk, path + "/" + c);
        }
        zk.delete(path, -1);
    } catch (KeeperException e) {
        if (e.code() == KeeperException.Code.NONODE) {
            return;
        }
        throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.nesscomputing.service.discovery.client.internal.ServiceDiscoveryAnnouncer.java

License:Apache License

@Override
void visit(final List<String> childNodes, final ZooKeeper zookeeper, final long currentGeneration)
        throws InterruptedException, KeeperException {
    // Loop through everything that we *should* announce, add them with the current generation to the
    // generation map.
    for (final ServiceInformation si : localAnnouncements) {
        // This announcement is safe, so increment its generation.
        localAnnouncementGenerations.put(si.getAnnouncementName(), currentGeneration);

        // If announcement is not present, announce it.
        if (!childNodes.contains(si.getAnnouncementName())) {
            LOG.debug("Need to announce %s", si.getAnnouncementName());
            final String childPath = getNodePath(si.getAnnouncementName());

            try {
                final byte[] serialized = objectMapper.writeValueAsBytes(si);
                zookeeper.create(childPath, serialized, Ids.OPEN_ACL_UNSAFE,
                        si.isStaticAnnouncement() ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL);
                LOG.debug("Created %sannouncement for %s", si.isStaticAnnouncement() ? "static " : "",
                        si.getAnnouncementName());
            } catch (final IOException ioe) {
                LOG.warn(ioe, "While generating announcement:");
            }//from  w w w  . j  a  v a  2 s . co  m
        }
    }

    // Now loop through everything that is currently present in the generation map. Remove everything that
    // is not marked with the current generation.
    for (final Iterator<Map.Entry<String, Long>> it = localAnnouncementGenerations.entrySet().iterator(); it
            .hasNext();) {
        final Map.Entry<String, Long> entry = it.next();

        if (entry.getValue() == currentGeneration) {
            LOG.trace("Announcement %s survives in generation %d", entry.getKey(), entry.getValue());
            continue;
        } else {
            LOG.trace("Announcement %s no longer present in generation %d", entry.getKey(), entry.getValue());
            final String childPath = getNodePath(entry.getKey());

            try {
                zookeeper.delete(childPath, -1);
                LOG.debug("Removed announcement for %s", entry.getKey());
                it.remove();
            } catch (final KeeperException ke) {
                // The node disappeared under us. That should not happen, but
                // test for it anyway.
                if (ke.code() == Code.NONODE) {
                    LOG.trace("Node was already removed, ignoring");
                    it.remove();
                } else {
                    throw ke;
                }
            }
        }
    }

    // Now remove static announcements which have been unannounced
    for (final ServiceInformation si : staticAnnouncementsToRemove) {
        final String announcementName = si.getAnnouncementName();

        LOG.info("Removing static announcement %s", announcementName);
        final String childPath = getNodePath(announcementName);

        try {
            zookeeper.delete(childPath, -1);
            LOG.debug("Removed announcement for %s", announcementName);
            staticAnnouncementsToRemove.remove(si);
        } catch (final KeeperException ke) {
            // The node disappeared under us. That should not happen, but
            // test for it anyway.
            if (ke.code() == Code.NONODE) {
                LOG.trace("Node was already removed, ignoring");
                staticAnnouncementsToRemove.remove(si);
            } else {
                throw ke;
            }
        }
    }
}

From source file:com.nesscomputing.service.discovery.job.ZookeeperProcessingTask.java

License:Apache License

private void processKeeperException(final KeeperException ke) throws InterruptedException {
    switch (ke.code()) {
    case CONNECTIONLOSS:
        LOG.trace("Connection lost, waiting for reconnect");
        connected = false;/*from ww w.  j a v  a 2 s.c om*/
        break;

    case SESSIONEXPIRED:
        LOG.trace("Session expired, closing zookeeper.");
        connected = false;
        closeZookeeper();
        break;

    default:
        LOG.warn("Zookeeper Problem: %s", ke.code());
        break;
    }
}

From source file:com.nesscomputing.service.discovery.server.job.BuildPathJob.java

License:Apache License

@Override
protected boolean execute(final ZooKeeper zookeeper) throws KeeperException, IOException {
    if (elements.length > 0) {
        final StringBuilder sb = new StringBuilder("");

        for (String element : elements) {
            if (StringUtils.isBlank(element)) {
                continue;
            }/* ww  w.j av  a 2 s  .c o m*/

            sb.append('/').append(element);

            final String zPath = sb.toString();
            try {
                if (zookeeper.exists(zPath, false) == null) {
                    LOG.info("Node %s does not exist, creating", zPath);
                    zookeeper.create(zPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                return true;
            } catch (KeeperException ke) {
                if (ke.code() == Code.NODEEXISTS) {
                    LOG.trace("Node exists, ignoring");
                } else {
                    throw ke;
                }
            }
        }
    }
    return true;
}

From source file:com.netflix.curator.framework.imps.CuratorFrameworkImpl.java

License:Apache License

@SuppressWarnings({ "ThrowableResultOfMethodCallIgnored" })
<DATA_TYPE> void processBackgroundOperation(OperationAndData<DATA_TYPE> operationAndData, CuratorEvent event) {
    boolean isInitialExecution = (event == null);
    if (isInitialExecution) {
        performBackgroundOperation(operationAndData);
        return;/*w ww  .  java2 s.c om*/
    }

    boolean queueOperation = false;
    do {
        if (RetryLoop.shouldRetry(event.getResultCode())) {
            if (client.getRetryPolicy().allowRetry(operationAndData.getThenIncrementRetryCount(),
                    operationAndData.getElapsedTimeMs(), operationAndData)) {
                queueOperation = true;
            } else {
                if (operationAndData.getErrorCallback() != null) {
                    operationAndData.getErrorCallback().retriesExhausted(operationAndData);
                }

                KeeperException.Code code = KeeperException.Code.get(event.getResultCode());
                Exception e = null;
                try {
                    e = (code != null) ? KeeperException.create(code) : null;
                } catch (Throwable ignore) {
                }
                if (e == null) {
                    e = new Exception("Unknown result code: " + event.getResultCode());
                }
                logError("Background operation retry gave up", e);
            }
            break;
        }

        if (operationAndData.getCallback() != null) {
            sendToBackgroundCallback(operationAndData, event);
            break;
        }

        processEvent(event);
    } while (false);

    if (queueOperation) {
        backgroundOperations.offer(operationAndData);
    }
}