You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@lucene.apache.org by GitBox <gi...@apache.org> on 2021/01/06 01:50:56 UTC

[GitHub] [lucene-solr] noblepaul commented on a change in pull request #2148: SOLR-15052: Per-replica states for reducing overseer bottlenecks

noblepaul commented on a change in pull request #2148:
URL: https://github.com/apache/lucene-solr/pull/2148#discussion_r552318967



##########
File path: solr/solrj/src/java/org/apache/solr/common/cloud/PerReplicaStates.java
##########
@@ -0,0 +1,600 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.solr.common.cloud;
+
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.function.BiConsumer;
+
+import org.apache.solr.cluster.api.SimpleMap;
+import org.apache.solr.common.MapWriter;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.annotation.JsonProperty;
+import org.apache.solr.common.util.ReflectMapWriter;
+import org.apache.solr.common.util.StrUtils;
+import org.apache.solr.common.util.WrappedSimpleMap;
+import org.apache.zookeeper.CreateMode;
+import org.apache.zookeeper.KeeperException;
+import org.apache.zookeeper.Op;
+import org.apache.zookeeper.data.ACL;
+import org.apache.zookeeper.data.Stat;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static java.util.Collections.singletonList;
+import static org.apache.solr.common.params.CommonParams.NAME;
+import static org.apache.solr.common.params.CommonParams.VERSION;
+
+/**
+ * This represents the individual replica states in a collection
+ * This is an immutable object. When states are modified, a new instance is constructed
+ */
+public class PerReplicaStates implements ReflectMapWriter {
+  private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  public static final char SEPARATOR = ':';
+
+
+  @JsonProperty
+  public final String path;
+
+  @JsonProperty
+  public final int cversion;
+
+  @JsonProperty
+  public final SimpleMap<State> states;
+
+  /**
+   * Construct with data read from ZK
+   * @param path path from where this is loaded
+   * @param cversion the current child version of the znode
+   * @param states the per-replica states (the list of all child nodes)
+   */
+  public PerReplicaStates(String path, int cversion, List<String> states) {
+    this.path = path;
+    this.cversion = cversion;
+    Map<String, State> tmp = new LinkedHashMap<>();
+
+    for (String state : states) {
+      State rs = State.parse(state);
+      if (rs == null) continue;
+      State existing = tmp.get(rs.replica);
+      if (existing == null) {
+        tmp.put(rs.replica, rs);
+      } else {
+        tmp.put(rs.replica, rs.insert(existing));
+      }
+    }
+    this.states = new WrappedSimpleMap<>(tmp);
+
+  }
+
+  /**Get the changed replicas
+   */
+  public static Set<String> findModifiedReplicas(PerReplicaStates old, PerReplicaStates fresh) {
+    Set<String> result = new HashSet<>();
+    if (fresh == null) {
+      old.states.forEachKey(result::add);
+      return result;
+    }
+    old.states.forEachEntry((s, state) -> {
+      // the state is modified or missing
+      if (!Objects.equals(fresh.get(s) , state)) result.add(s);
+    });
+    fresh.states.forEachEntry((s, state) -> { if (old.get(s) == null ) result.add(s);
+    });
+    return result;
+  }
+
+  /**
+   * This is a persist operation with retry if a write fails due to stale state
+   */
+  public static void persist(WriteOps ops, String znode, SolrZkClient zkClient) throws KeeperException, InterruptedException {
+    List<Operation> operations = ops.get();
+    for (int i = 0; i < 10; i++) {
+      try {
+        persist(operations, znode, zkClient);
+        return;
+      } catch (KeeperException.NodeExistsException | KeeperException.NoNodeException e) {
+        //state is stale
+        log.info("stale state for {}. retrying...", znode);
+        operations = ops.get(PerReplicaStates.fetch(znode, zkClient, null));
+      }
+    }
+  }
+
+  /**
+   * Persist a set of operations to Zookeeper
+   */
+  public static void persist(List<Operation> operations, String znode, SolrZkClient zkClient) throws KeeperException, InterruptedException {
+    if (operations == null || operations.isEmpty()) return;
+    log.debug("Per-replica state being persisted for :{}, ops: {}", znode, operations);
+
+    List<Op> ops = new ArrayList<>(operations.size());
+    for (Operation op : operations) {
+      //the state of the replica is being updated
+      String path = znode + "/" + op.state.asString;
+      List<ACL> acls = zkClient.getZkACLProvider().getACLsToAdd(path);
+      ops.add(op.typ == Operation.Type.ADD ?
+          Op.create(path, null, acls, CreateMode.PERSISTENT) :
+          Op.delete(path, -1));
+    }
+    try {
+      zkClient.multi(ops, true);
+      if (log.isDebugEnabled()) {
+        //nocommit
+        try {
+          Stat stat = zkClient.exists(znode, null, true);
+          log.debug("After update, cversion : {}", stat.getCversion());
+        } catch (Exception e) {
+        }
+
+      }
+    } catch (KeeperException e) {
+      log.error("multi op exception : " + e.getMessage() + zkClient.getChildren(znode, null, true));
+      throw e;

Review comment:
       does it add any value?




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@lucene.apache.org
For additional commands, e-mail: issues-help@lucene.apache.org