private Set<String> getStateFormat2CollectionNames() throws KeeperException, InterruptedException { List<String> children = null; try { children = zkClient.getChildren(COLLECTIONS_ZKNODE, null, true); } catch (KeeperException.NoNodeException e) { log.warn("Error fetching collection names"); return new HashSet<>(); } if (children == null || children.isEmpty()) return new HashSet<>(); HashSet<String> result = new HashSet<>(children.size(), 1.0f); for (String c : children) { try { // this exists call is necessary because we only want to return // those collections which have their own state.json. // The getCollectionPath() calls returns the complete path to the // collection's state.json if (zkClient.exists(getCollectionPath(c), true)) { result.add(c); } } catch (Exception e) { log.warn("Error reading collections nodes", e); } } return result; }
@Override public synchronized DocCollection get(boolean allowCached) { gets.incrementAndGet(); if (!allowCached || lastUpdateTime < 0 || System.nanoTime() - lastUpdateTime > LAZY_CACHE_TIME) { boolean shouldFetch = true; if (cachedDocCollection != null) { Stat exists = null; try { exists = zkClient.exists(getCollectionPath(collName), null, true); } catch (Exception e) {} if (exists != null && exists.getVersion() == cachedDocCollection.getZNodeVersion()) { shouldFetch = false; } } if (shouldFetch) { cachedDocCollection = getCollectionLive(ZkStateReader.this, collName); lastUpdateTime = System.nanoTime(); } } return cachedDocCollection; }
public static DocCollection getCollectionLive(ZkStateReader zkStateReader, String coll) { String collectionPath = getCollectionPath(coll); try { Stat stat = new Stat(); byte[] data = zkStateReader.getZkClient().getData(collectionPath, null, 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) { log.warn("No node available : " + collectionPath, e); return null; } catch (KeeperException e) { throw new SolrException(ErrorCode.BAD_REQUEST, "Could not load collection from ZK:" + coll, e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new SolrException(ErrorCode.BAD_REQUEST, "Could not load collection from ZK:" + coll, e); } }
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) { // Rare race condition, we tried to fetch the data and couldn't find it, then we found it exists. // Loop and try again. continue; } } return null; } } }
private void addZkWatch(final String coll) throws KeeperException, InterruptedException { log.info("addZkWatch {}", coll); final String fullpath = getCollectionPath(coll); synchronized (getUpdateLock()) {