hiteshk25 commented on code in PR #1585: URL: https://github.com/apache/solr/pull/1585#discussion_r1187636106
########## solr/solrj-zookeeper/src/java/org/apache/solr/common/cloud/ZkStateReader.java: ########## @@ -368,10 +368,152 @@ private StatefulCollectionWatch compute( BiFunction<String, StatefulCollectionWatch, StatefulCollectionWatch> remappingFunction) { return statefulWatchesByCollectionName.compute(collectionName, remappingFunction); } + + public boolean isWatched(String coll) { + return statefulWatchesByCollectionName.containsKey(coll); + } } - private static class StatefulCollectionWatch extends CollectionWatch<DocCollectionWatcher> { + private class StatefulCollectionWatch extends CollectionWatch<DocCollectionWatcher> { private DocCollection currentState; + + Watcher persistentWatcher; + + final String coll; + private final String collectionPath; + + private StatefulCollectionWatch(String coll) { + this.coll = coll; + this.collectionPath = DocCollection.getCollectionPath(coll); + } + + public void handleWatch(WatchedEvent event) { + if (!collectionWatches.isWatched(coll)) { + return; + } + if (log.isTraceEnabled()) { + log.trace("an event happened for {}, event: {}", coll, event.toString()); + } + DocCollection collectionState = getCollection(coll); + if (collectionPath.equals(event.getPath())) { + DocCollection newState = null; + try { + newState = fetchCollectionState(coll, null); + } catch (KeeperException.SessionExpiredException + | KeeperException.ConnectionLossException e) { + log.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK: ", e); + } catch (KeeperException e) { + log.error("exception for collection: [{}]", coll, e); + throw new ZooKeeperException(ErrorCode.SERVER_ERROR, "A ZK error has occurred", e); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + log.error("exception for collection: [{}]", coll, e); + } + collectionWatches.updateDocCollection(coll, newState); + synchronized (getUpdateLock()) { + constructState(Collections.singleton(coll)); + } + } + if (collectionState != null && collectionState.isPerReplicaState()) { + String path = event.getPath(); + if ((event.getType() == EventType.NodeCreated || event.getType() == EventType.NodeDeleted) + && path.length() > collectionState.getZNode().length() + && path.startsWith(collectionPath)) { + if (log.isDebugEnabled()) { + log.debug("PRS node event : {}", event.getType()); + } + + PerReplicaStates prs = collectionState.getPerReplicaStates(); + if (prs != null) { + String stateStr = path.substring(collectionPath.length() + 1); + PerReplicaStates.State newState = PerReplicaStates.State.parse(stateStr); + PerReplicaStates.State oldState = prs.states.get(newState.replica); + if (event.getType() == EventType.NodeCreated) { + if (oldState != null && newState.version < oldState.version) { + // we got a notification out of order? . Shouldn't happen + if (log.isTraceEnabled()) { + log.trace("newState {} < oldState {}", newState, oldState); + } + return; + } + if (oldState == null) { + // the state does not exist now. fetch everything + + if (log.isTraceEnabled()) { + log.trace("fresh replica, force fetch all {}", collectionPath); + } + + prs = PerReplicaStatesFetcher.fetch(collectionPath, zkClient, null); + } else { + // the PRS entry is already available, need to do an update + Stat stat = null; + try { + // get the stat of this child node + stat = zkClient.exists(path, null, true); + } catch (Exception e) { + throw new RuntimeException(e); + } + if (stat == null) { + // this got deleted so soon. nothing to do + return; + } + + if (log.isTraceEnabled()) { + log.trace("PRS insert {}, v:{}", newState, stat.getCzxid()); + } + prs = prs.insert(newState, stat.getCzxid()); + if (prs == null) { + // something went wrong + prs = PerReplicaStatesFetcher.fetch(collectionPath, zkClient, null); + } + } + } else if (event.getType() == EventType.NodeDeleted) { + if (oldState == null) { + // This is already removed (unlikely, but) + return; + } + if (newState.version < oldState.version) { + if (log.isTraceEnabled()) { + log.trace("PRS in-place remove {}", path); + } + // removed the duplicate without modifying the PRS Object + oldState.removeDuplicate(newState); + return; + } else { + if (log.isTraceEnabled()) { + log.trace("Replica: {} delete, force fetch", path); + } + // a replica got removed. we can't get the pzxid reliably. fetch everything + prs = PerReplicaStatesFetcher.fetch(collectionPath, zkClient, null); Review Comment: similar, can we get stat of `state.json` and use cversion of it? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org