From 07286c09119b7a86d886218467f482acaeb75a2a Mon Sep 17 00:00:00 2001 From: Christine Poerschke Date: Fri, 24 Mar 2023 17:16:37 +0000 Subject: [PATCH] SOLR-15446: Fix three off-by-one ArrayList initialCapacity choices when calling ZooKeeper.multi API. (#156) Co-authored-by: Kevin Risden --- .../cloud/ShardLeaderElectionContextBase.java | 6 ++--- .../org/apache/solr/cloud/ZkController.java | 26 +++++++++---------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/cloud/ShardLeaderElectionContextBase.java b/solr/core/src/java/org/apache/solr/cloud/ShardLeaderElectionContextBase.java index c38521bf35c..b99764bdc42 100644 --- a/solr/core/src/java/org/apache/solr/cloud/ShardLeaderElectionContextBase.java +++ b/solr/core/src/java/org/apache/solr/cloud/ShardLeaderElectionContextBase.java @@ -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; @@ -110,10 +109,9 @@ public void cancelElection() throws InterruptedException, KeeperException { "Removing leader registration node on cancel: {} {}", leaderPath, leaderZkNodeParentVersion); - List ops = new ArrayList<>(2); String parent = ZkMaintenanceUtils.getZkParent(leaderPath); - ops.add(Op.check(parent, leaderZkNodeParentVersion)); - ops.add(Op.delete(leaderPath, -1)); + List ops = + List.of(Op.check(parent, leaderZkNodeParentVersion), Op.delete(leaderPath, -1)); zkClient.multi(ops, true); } catch (InterruptedException e) { throw e; diff --git a/solr/core/src/java/org/apache/solr/cloud/ZkController.java b/solr/core/src/java/org/apache/solr/cloud/ZkController.java index 246081bb7cc..a44cf3e8a6d 100644 --- a/solr/core/src/java/org/apache/solr/cloud/ZkController.java +++ b/solr/core/src/java/org/apache/solr/cloud/ZkController.java @@ -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 ops = new ArrayList<>(2); + Map roles = cc.nodeRoles.getRoles(); + List ops = new ArrayList<>(roles.size() + 1); ops.add( Op.create( nodePath, @@ -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); } @@ -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 ops = new ArrayList<>(2); - ops.add(Op.delete(nodePath, -1)); + List ops = List.of(Op.delete(nodePath, -1)); try { zkClient.multi(ops, true); @@ -2947,7 +2945,7 @@ public void publishNodeAsDown(String nodeName) { && processedCollections.add(collName) && (coll = zkStateReader.getCollection(collName)) != null && coll.isPerReplicaState()) { - final List replicasToDown = new ArrayList<>(); + final List replicasToDown = new ArrayList<>(coll.getSlicesMap().size()); coll.forEachReplica( (s, replica) -> { if (replica.getNodeName().equals(nodeName)) {