Search in sources :

Example 61 with KeeperException

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException in project lucene-solr by apache.

the class ZkStateReader method addSecuritynodeWatcher.

private void addSecuritynodeWatcher(final Callable<Pair<byte[], Stat>> callback) throws KeeperException, InterruptedException {
    zkClient.exists(SOLR_SECURITY_CONF_PATH, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            // session events are not change events, and do not remove the watcher
            if (EventType.None.equals(event.getType())) {
                return;
            }
            try {
                synchronized (ZkStateReader.this.getUpdateLock()) {
                    LOG.debug("Updating [{}] ... ", SOLR_SECURITY_CONF_PATH);
                    // remake watch
                    final Watcher thisWatch = this;
                    final Stat stat = new Stat();
                    final byte[] data = getZkClient().getData(SOLR_SECURITY_CONF_PATH, thisWatch, stat, true);
                    try {
                        callback.call(new Pair<>(data, stat));
                    } catch (Exception e) {
                        LOG.error("Error running collections node listener", e);
                    }
                }
            } catch (KeeperException.ConnectionLossException | KeeperException.SessionExpiredException e) {
                LOG.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK: [{}]", e.getMessage());
            } catch (KeeperException e) {
                LOG.error("A ZK error has occurred", e);
                throw new ZooKeeperException(ErrorCode.SERVER_ERROR, "", e);
            } catch (InterruptedException e) {
                // Restore the interrupted status
                Thread.currentThread().interrupt();
                LOG.warn("Interrupted", e);
            }
        }
    }, true);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) Watcher(org.apache.zookeeper.Watcher) TimeoutException(java.util.concurrent.TimeoutException) SolrException(org.apache.solr.common.SolrException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) KeeperException(org.apache.zookeeper.KeeperException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) KeeperException(org.apache.zookeeper.KeeperException) Pair(org.apache.solr.common.util.Pair)

Example 62 with KeeperException

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException in project lucene-solr by apache.

the class SolrZkClient method makePath.

/**
   * Creates the path in ZooKeeper, creating each node as necessary.
   *
   * e.g. If <code>path=/solr/group/node</code> and none of the nodes, solr,
   * group, node exist, each will be created.
   * 
   * skipPathParts will force the call to fail if the first skipPathParts do not exist already.
   *
   * Note: retryOnConnLoss is only respected for the final node - nodes
   * before that are always retried on connection loss.
   */
public void makePath(String path, byte[] data, CreateMode createMode, Watcher watcher, boolean failOnExists, boolean retryOnConnLoss, int skipPathParts) throws KeeperException, InterruptedException {
    log.debug("makePath: {}", path);
    boolean retry = true;
    if (path.startsWith("/")) {
        path = path.substring(1, path.length());
    }
    String[] paths = path.split("/");
    StringBuilder sbPath = new StringBuilder();
    for (int i = 0; i < paths.length; i++) {
        String pathPiece = paths[i];
        sbPath.append("/" + pathPiece);
        if (i < skipPathParts) {
            continue;
        }
        byte[] bytes = null;
        final String currentPath = sbPath.toString();
        Object exists = exists(currentPath, watcher, retryOnConnLoss);
        if (exists == null || ((i == paths.length - 1) && failOnExists)) {
            CreateMode mode = CreateMode.PERSISTENT;
            if (i == paths.length - 1) {
                mode = createMode;
                bytes = data;
                if (!retryOnConnLoss)
                    retry = false;
            }
            try {
                if (retry) {
                    final CreateMode finalMode = mode;
                    final byte[] finalBytes = bytes;
                    zkCmdExecutor.retryOperation(new ZkOperation() {

                        @Override
                        public Object execute() throws KeeperException, InterruptedException {
                            keeper.create(currentPath, finalBytes, zkACLProvider.getACLsToAdd(currentPath), finalMode);
                            return null;
                        }
                    });
                } else {
                    keeper.create(currentPath, bytes, zkACLProvider.getACLsToAdd(currentPath), mode);
                }
            } catch (NodeExistsException e) {
                if (!failOnExists) {
                    // TODO: version ? for now, don't worry about race
                    setData(currentPath, data, -1, retryOnConnLoss);
                    // set new watch
                    exists(currentPath, watcher, retryOnConnLoss);
                    return;
                }
                // ignore unless it's the last node in the path
                if (i == paths.length - 1) {
                    throw e;
                }
            }
            if (i == paths.length - 1) {
                // set new watch
                exists(currentPath, watcher, retryOnConnLoss);
            }
        } else if (i == paths.length - 1) {
            // TODO: version ? for now, don't worry about race
            setData(currentPath, data, -1, retryOnConnLoss);
            // set new watch
            exists(currentPath, watcher, retryOnConnLoss);
        }
    }
}
Also used : NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) CreateMode(org.apache.zookeeper.CreateMode) KeeperException(org.apache.zookeeper.KeeperException)

Example 63 with KeeperException

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException in project lucene-solr by apache.

the class ClusterProperties method setClusterProperty.

/**
   * This method sets a cluster property.
   *
   * @param propertyName  The property name to be set.
   * @param propertyValue The value of the property.
   * @throws IOException if there is an error writing data to the cluster
   */
@SuppressWarnings("unchecked")
public void setClusterProperty(String propertyName, String propertyValue) throws IOException {
    if (!ZkStateReader.KNOWN_CLUSTER_PROPS.contains(propertyName)) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Not a known cluster property " + propertyName);
    }
    for (; ; ) {
        Stat s = new Stat();
        try {
            if (client.exists(ZkStateReader.CLUSTER_PROPS, true)) {
                Map properties = (Map) Utils.fromJSON(client.getData(ZkStateReader.CLUSTER_PROPS, null, s, true));
                if (propertyValue == null) {
                    //Don't update ZK unless absolutely necessary.
                    if (properties.get(propertyName) != null) {
                        properties.remove(propertyName);
                        client.setData(ZkStateReader.CLUSTER_PROPS, Utils.toJSON(properties), s.getVersion(), true);
                    }
                } else {
                    //Don't update ZK unless absolutely necessary.
                    if (!propertyValue.equals(properties.get(propertyName))) {
                        properties.put(propertyName, propertyValue);
                        client.setData(ZkStateReader.CLUSTER_PROPS, Utils.toJSON(properties), s.getVersion(), true);
                    }
                }
            } else {
                Map properties = new LinkedHashMap();
                properties.put(propertyName, propertyValue);
                client.create(ZkStateReader.CLUSTER_PROPS, Utils.toJSON(properties), CreateMode.PERSISTENT, true);
            }
        } catch (KeeperException.BadVersionException | KeeperException.NodeExistsException e) {
            //race condition
            continue;
        } catch (InterruptedException | KeeperException e) {
            throw new IOException("Error setting cluster property", SolrZkClient.checkInterrupted(e));
        }
        break;
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException) KeeperException(org.apache.zookeeper.KeeperException) LinkedHashMap(java.util.LinkedHashMap)

Example 64 with KeeperException

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException in project lucene-solr by apache.

the class ZkStateReader method fetchCollectionState.

private DocCollection fetchCollectionState(String coll, Watcher watcher) throws KeeperException, InterruptedException {
    String collectionPath = getCollectionPath(coll);
    while (true) {
        try {
            Stat stat = new Stat();
            byte[] data = zkClient.getData(collectionPath, watcher, stat, true);
            ClusterState state = ClusterState.load(stat.getVersion(), data, Collections.<String>emptySet(), collectionPath);
            ClusterState.CollectionRef collectionRef = state.getCollectionStates().get(coll);
            return collectionRef == null ? null : collectionRef.get();
        } catch (KeeperException.NoNodeException e) {
            if (watcher != null) {
                // Leave an exists watch in place in case a state.json is created later.
                Stat exists = zkClient.exists(collectionPath, watcher, true);
                if (exists != null) {
                    // Loop and try again.
                    continue;
                }
            }
            return null;
        }
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) KeeperException(org.apache.zookeeper.KeeperException)

Example 65 with KeeperException

use of org.apache.flink.shaded.zookeeper3.org.apache.zookeeper.KeeperException in project lucene-solr by apache.

the class MiniSolrCloudCluster method waitForAllNodes.

private void waitForAllNodes(int numServers, int timeout) throws IOException, InterruptedException {
    try (SolrZkClient zkClient = new SolrZkClient(zkServer.getZkHost(), AbstractZkTestCase.TIMEOUT)) {
        int numliveNodes = 0;
        int retries = timeout;
        String liveNodesPath = "/solr/live_nodes";
        // Wait up to {timeout} seconds for number of live_nodes to match up number of servers
        do {
            if (zkClient.exists(liveNodesPath, true)) {
                numliveNodes = zkClient.getChildren(liveNodesPath, null, true).size();
                if (numliveNodes == numServers) {
                    break;
                }
            }
            retries--;
            if (retries == 0) {
                throw new IllegalStateException("Solr servers failed to register with ZK." + " Current count: " + numliveNodes + "; Expected count: " + numServers);
            }
            Thread.sleep(1000);
        } while (numliveNodes != numServers);
    } catch (KeeperException e) {
        throw new IOException("Error communicating with zookeeper", e);
    }
}
Also used : IOException(java.io.IOException) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

KeeperException (org.apache.zookeeper.KeeperException)566 IOException (java.io.IOException)188 Stat (org.apache.zookeeper.data.Stat)127 ZooKeeper (org.apache.zookeeper.ZooKeeper)87 ArrayList (java.util.ArrayList)51 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)45 Watcher (org.apache.zookeeper.Watcher)39 WatchedEvent (org.apache.zookeeper.WatchedEvent)38 Test (org.junit.jupiter.api.Test)38 CountDownLatch (java.util.concurrent.CountDownLatch)30 SolrException (org.apache.solr.common.SolrException)30 HashMap (java.util.HashMap)29 List (java.util.List)28 ACL (org.apache.zookeeper.data.ACL)27 Test (org.junit.Test)27 HeliosRuntimeException (com.spotify.helios.common.HeliosRuntimeException)25 ServerName (org.apache.hadoop.hbase.ServerName)24 Map (java.util.Map)23 IZooReaderWriter (org.apache.accumulo.fate.zookeeper.IZooReaderWriter)23 InterruptedIOException (java.io.InterruptedIOException)20