Skip to content

Commit

Permalink
SOLR-15446: Fix three off-by-one ArrayList initialCapacity choices wh…
Browse files Browse the repository at this point in the history
…en calling ZooKeeper.multi API. (#156)

Co-authored-by: Kevin Risden <[email protected]>
  • Loading branch information
cpoerschke and risdenk authored Mar 24, 2023
1 parent d8f57f0 commit 07286c0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.solr.cloud;

import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.List;
import org.apache.solr.cloud.overseer.OverseerAction;
import org.apache.solr.common.SolrException;
Expand Down Expand Up @@ -110,10 +109,9 @@ public void cancelElection() throws InterruptedException, KeeperException {
"Removing leader registration node on cancel: {} {}",
leaderPath,
leaderZkNodeParentVersion);
List<Op> ops = new ArrayList<>(2);
String parent = ZkMaintenanceUtils.getZkParent(leaderPath);
ops.add(Op.check(parent, leaderZkNodeParentVersion));
ops.add(Op.delete(leaderPath, -1));
List<Op> ops =
List.of(Op.check(parent, leaderZkNodeParentVersion), Op.delete(leaderPath, -1));
zkClient.multi(ops, true);
} catch (InterruptedException e) {
throw e;
Expand Down
26 changes: 12 additions & 14 deletions solr/core/src/java/org/apache/solr/cloud/ZkController.java
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,8 @@ private void createEphemeralLiveNode() throws KeeperException, InterruptedExcept
String nodeName = getNodeName();
String nodePath = ZkStateReader.LIVE_NODES_ZKNODE + "/" + nodeName;
log.info("Register node as live in ZooKeeper:{}", nodePath);
List<Op> ops = new ArrayList<>(2);
Map<NodeRoles.Role, String> roles = cc.nodeRoles.getRoles();
List<Op> ops = new ArrayList<>(roles.size() + 1);
ops.add(
Op.create(
nodePath,
Expand All @@ -1226,16 +1227,14 @@ private void createEphemeralLiveNode() throws KeeperException, InterruptedExcept
CreateMode.EPHEMERAL));

// Create the roles node as well
cc.nodeRoles
.getRoles()
.forEach(
(role, mode) ->
ops.add(
Op.create(
NodeRoles.getZNodeForRoleMode(role, mode) + "/" + nodeName,
null,
zkClient.getZkACLProvider().getACLsToAdd(nodePath),
CreateMode.EPHEMERAL)));
roles.forEach(
(role, mode) ->
ops.add(
Op.create(
NodeRoles.getZNodeForRoleMode(role, mode) + "/" + nodeName,
null,
zkClient.getZkACLProvider().getACLsToAdd(nodePath),
CreateMode.EPHEMERAL)));

zkClient.multi(ops, true);
}
Expand All @@ -1247,8 +1246,7 @@ public void removeEphemeralLiveNode() throws KeeperException, InterruptedExcepti
String nodeName = getNodeName();
String nodePath = ZkStateReader.LIVE_NODES_ZKNODE + "/" + nodeName;
log.info("Remove node as live in ZooKeeper:{}", nodePath);
List<Op> ops = new ArrayList<>(2);
ops.add(Op.delete(nodePath, -1));
List<Op> ops = List.of(Op.delete(nodePath, -1));

try {
zkClient.multi(ops, true);
Expand Down Expand Up @@ -2947,7 +2945,7 @@ public void publishNodeAsDown(String nodeName) {
&& processedCollections.add(collName)
&& (coll = zkStateReader.getCollection(collName)) != null
&& coll.isPerReplicaState()) {
final List<String> replicasToDown = new ArrayList<>();
final List<String> replicasToDown = new ArrayList<>(coll.getSlicesMap().size());
coll.forEachReplica(
(s, replica) -> {
if (replica.getNodeName().equals(nodeName)) {
Expand Down

0 comments on commit 07286c0

Please sign in to comment.