-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Add helper to pick node for reindex relocation #139081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
PeteGillinElastic
merged 8 commits into
elastic:main
from
PeteGillinElastic:reindex-relocation-node-picker
Dec 18, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f86685f
Add helper to pick node for reindex relocation
PeteGillinElastic 7ba85b5
Merge branch 'main' into reindex-relocation-node-picker
PeteGillinElastic dc669d7
Stop using `DiscoveryNodes.getCoordinatingOnlyNodes()` due to https:/…
PeteGillinElastic f849562
Merge remote-tracking branch 'upstream/main' into reindex-relocation-…
PeteGillinElastic 14c7a9c
self-nit: omit trailing periods in log messages
PeteGillinElastic 52683fd
Merge remote-tracking branch 'upstream/main' into reindex-relocation-…
PeteGillinElastic 2fe7fa8
Merge branch 'main' into reindex-relocation-node-picker
PeteGillinElastic 799e245
Merge branch 'main' into reindex-relocation-node-picker
PeteGillinElastic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...s/reindex/src/main/java/org/elasticsearch/reindex/DefaultReindexRelocationNodePicker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.reindex; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
| import org.elasticsearch.common.Randomness; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
||
| class DefaultReindexRelocationNodePicker implements ReindexRelocationNodePicker { | ||
|
|
||
| private static final Logger logger = LogManager.getLogger(DefaultReindexRelocationNodePicker.class); | ||
|
|
||
| private final Random random; | ||
|
|
||
| DefaultReindexRelocationNodePicker() { | ||
| this.random = Randomness.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public String pickNode(DiscoveryNodes nodes) { | ||
| String currentNodeId = nodes.getLocalNodeId(); | ||
| if (currentNodeId == null) { | ||
| logger.debug( | ||
| "Trying to pick a node to relocate a reindex task to, but the current node ID is unexpectedly unknown:" | ||
| + " the relocation attempt will be aborted" | ||
| ); | ||
| return null; | ||
| } | ||
| List<String> eligibleDedicatedCoordinatingNodes = nodes.getNodes() | ||
| .values() | ||
| .stream() | ||
| .filter(node -> node.getRoles().isEmpty()) | ||
| .map(DiscoveryNode::getId) | ||
| .filter(id -> id.equals(currentNodeId) == false) | ||
| .toList(); | ||
| if (eligibleDedicatedCoordinatingNodes.isEmpty() == false) { | ||
| String newNodeId = randomNodeId(eligibleDedicatedCoordinatingNodes); | ||
| logger.debug("Chose dedicated coordinating node ID {} for relocating a reindex task from node {}", newNodeId, currentNodeId); | ||
| return newNodeId; | ||
| } | ||
| List<String> eligibleDataNodes = nodes.getDataNodes().keySet().stream().filter(id -> id.equals(currentNodeId) == false).toList(); | ||
| if (eligibleDataNodes.isEmpty() == false) { | ||
| String newNodeId = randomNodeId(eligibleDataNodes); | ||
| logger.debug( | ||
| "Chose data node ID {} for relocating a reindex task from node {}" | ||
| + " (there are no dedicated coordinating nodes, perhaps excluding the current node)", | ||
| newNodeId, | ||
| currentNodeId | ||
| ); | ||
| return newNodeId; | ||
| } | ||
| logger.debug( | ||
| "Trying to pick a node to relocate a reindex task to, but there are no dedicated coordinating or data nodes" | ||
| + " (perhaps excluding the current node): the relocation attempt will be aborted" | ||
| ); | ||
| return null; | ||
| } | ||
|
|
||
| private String randomNodeId(List<String> nodeIds) { | ||
| return nodeIds.get(random.nextInt(nodeIds.size())); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
modules/reindex/src/main/java/org/elasticsearch/reindex/ReindexRelocationNodePicker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.reindex; | ||
|
|
||
| import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
| import org.elasticsearch.core.Nullable; | ||
|
|
||
| /** | ||
| * Interface for helper to pick a node to relocate a running reindex task to when the current node is about to shut down. | ||
| */ | ||
| public interface ReindexRelocationNodePicker { | ||
|
|
||
| /** | ||
| * Picks a suitable node. | ||
| * | ||
| * @param nodes The set of nodes to pick from | ||
| * @return The ID of the selected node, or null if there are no suitable nodes | ||
| */ | ||
| @Nullable | ||
| String pickNode(DiscoveryNodes nodes); | ||
| } |
115 changes: 115 additions & 0 deletions
115
...ndex/src/test/java/org/elasticsearch/reindex/DefaultReindexRelocationNodePickerTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.reindex; | ||
|
|
||
| import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| import org.elasticsearch.cluster.node.DiscoveryNodeRole; | ||
| import org.elasticsearch.cluster.node.DiscoveryNodeUtils; | ||
| import org.elasticsearch.cluster.node.DiscoveryNodes; | ||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import static org.hamcrest.Matchers.nullValue; | ||
| import static org.hamcrest.Matchers.oneOf; | ||
|
|
||
| public class DefaultReindexRelocationNodePickerTests extends ESTestCase { | ||
|
|
||
| private final DefaultReindexRelocationNodePicker picker = new DefaultReindexRelocationNodePicker(); | ||
|
|
||
| public void testPickNode_prefersDedicatedCoordinatingNode() { | ||
| DiscoveryNodes nodes = DiscoveryNodes.builder() | ||
| .add(createNode("dataMaster1", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("dataMaster2", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("dataMaster3", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("data4", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("data5", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("data6", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("ingest1", DiscoveryNodeRole.INGEST_ROLE)) | ||
| .add(createNode("coordinating1")) | ||
| .add(createNode("coordinating2Local")) | ||
| .add(createNode("coordinating3")) | ||
| .masterNodeId("dataMaster" + randomIntBetween(1, 3)) | ||
| .localNodeId("coordinating2Local") | ||
| .build(); | ||
| String id = picker.pickNode(nodes); | ||
| assertThat(id, oneOf("coordinating1", "coordinating3")); | ||
| } | ||
|
|
||
| public void testPickNode_noDedicatedCoordinatingNodes_fallsBackToDataNode() { | ||
| DiscoveryNodes nodes = DiscoveryNodes.builder() | ||
| .add(createNode("dataMaster1", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("dataMaster2", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("dataMaster3", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("data4", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("data5Local", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("data6", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("ingest1", DiscoveryNodeRole.INGEST_ROLE)) | ||
| .add(createNode("ml1", DiscoveryNodeRole.ML_ROLE)) | ||
| .add(createNode("voting1", DiscoveryNodeRole.VOTING_ONLY_NODE_ROLE)) | ||
| .masterNodeId("dataMaster" + randomIntBetween(1, 3)) | ||
| .localNodeId("data5Local") | ||
| .build(); | ||
| String id = picker.pickNode(nodes); | ||
| assertThat(id, oneOf("dataMaster1", "dataMaster2", "dataMaster3", "data4", "data6")); | ||
| } | ||
|
|
||
| public void testPickNode_onlyDedicatedCoordinatingNodeIsLocal_fallsBackToDataNode() { | ||
| DiscoveryNodes nodes = DiscoveryNodes.builder() | ||
| .add(createNode("dataMaster1", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("dataMaster2", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("dataMaster3", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("data4", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("data5", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("data6", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("ingest1", DiscoveryNodeRole.INGEST_ROLE)) | ||
| .add(createNode("coordinating1Local")) | ||
| .masterNodeId("dataMaster" + randomIntBetween(1, 3)) | ||
| .localNodeId("coordinating1Local") | ||
| .build(); | ||
| String id = picker.pickNode(nodes); | ||
| assertThat(id, oneOf("dataMaster1", "dataMaster2", "dataMaster3", "data4", "data5", "data6")); | ||
| } | ||
|
|
||
| public void testPickNode_noDedicatedCoordinatingNodes_onlyDataNodeIsLocal_returnsNull() { | ||
| DiscoveryNodes nodes = DiscoveryNodes.builder() | ||
| .add(createNode("dataMaster1Local", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("ingestMaster1", DiscoveryNodeRole.INGEST_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .masterNodeId(randomFrom("dataMaster1Local", "ingestMaster1")) | ||
| .localNodeId("dataMaster1Local") | ||
| .build(); | ||
| String id = picker.pickNode(nodes); | ||
| assertThat(id, nullValue()); | ||
| } | ||
|
|
||
| public void testPickNode_localNodeUnknown_returnsNull() { | ||
| DiscoveryNodes nodes = DiscoveryNodes.builder() | ||
| .add(createNode("dataMaster1", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("dataMaster2", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("dataMaster3", DiscoveryNodeRole.DATA_ROLE, DiscoveryNodeRole.MASTER_ROLE)) | ||
| .add(createNode("data4", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("data5", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("data6", DiscoveryNodeRole.DATA_ROLE)) | ||
| .add(createNode("ingest1", DiscoveryNodeRole.INGEST_ROLE)) | ||
| .add(createNode("coordinating1")) | ||
| .add(createNode("coordinating2Local")) | ||
| .add(createNode("coordinating3")) | ||
| .masterNodeId("dataMaster" + randomIntBetween(1, 3)) | ||
| // We never set the local node | ||
| .build(); | ||
| String id = picker.pickNode(nodes); | ||
| assertThat(id, nullValue()); | ||
| } | ||
|
|
||
| private static DiscoveryNode createNode(String id, DiscoveryNodeRole... roles) { | ||
| return DiscoveryNodeUtils.create(id, buildNewFakeTransportAddress(), Map.of(), Set.of(roles)); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question for @Tim-Brooks: The doc says "a valid node for index coordinating (data node, no dedicated master nodes)". Is this the correct interpretation of that? (I wasn't quite sure what "no dedicated master nodes" meant, as my understanding was that would mean nodes with only the
masterrole, which therefore could never be a data node.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. This looks fine to me (can contain data excluded dedicated master nodes).