-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Add replica-only node type for read scalability #20295
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
Open
synhershko
wants to merge
1
commit into
opensearch-project:main
Choose a base branch
from
BigDataBoutique:feature/replica-only-node-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
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
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
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
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
140 changes: 140 additions & 0 deletions
140
.../java/org/opensearch/cluster/routing/allocation/decider/ReplicaOnlyAllocationDecider.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,140 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * | ||
| * The OpenSearch Contributors require contributions made to | ||
| * this file be licensed under the Apache-2.0 license or a | ||
| * compatible open source license. | ||
| */ | ||
|
|
||
| package org.opensearch.cluster.routing.allocation.decider; | ||
|
|
||
| import org.opensearch.cluster.metadata.AutoExpandReplicas; | ||
| import org.opensearch.cluster.metadata.IndexMetadata; | ||
| import org.opensearch.cluster.node.DiscoveryNode; | ||
| import org.opensearch.cluster.routing.RoutingNode; | ||
| import org.opensearch.cluster.routing.ShardRouting; | ||
| import org.opensearch.cluster.routing.allocation.RoutingAllocation; | ||
|
|
||
| /** | ||
| * This allocation decider ensures that replica-only nodes follow strict allocation rules: | ||
| * <ul> | ||
| * <li>Replica-only nodes never host primary shards</li> | ||
| * <li>Replica-only nodes only host replica shards from indices with auto_expand_replicas: 0-all</li> | ||
| * <li>Primary shards are never promoted on replica-only nodes</li> | ||
| * <li>Regular data nodes are not affected by replica-only node presence</li> | ||
| * </ul> | ||
| * | ||
| * @opensearch.internal | ||
| */ | ||
| public class ReplicaOnlyAllocationDecider extends AllocationDecider { | ||
|
|
||
| public static final String NAME = "replica_only"; | ||
|
|
||
| @Override | ||
| public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { | ||
| return canAllocate(shardRouting, node.node(), allocation); | ||
| } | ||
|
|
||
| @Override | ||
| public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { | ||
| return canAllocate(shardRouting, node.node(), allocation); | ||
| } | ||
|
|
||
| @Override | ||
| public Decision canForceAllocatePrimary(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { | ||
| // CRITICAL: Never allow primary allocation to replica-only nodes, even with force | ||
| if (node.node().isReplicaOnlyNode()) { | ||
| return allocation.decision( | ||
| Decision.NO, | ||
| NAME, | ||
| "primary shard [%s] cannot be force allocated to replica-only node [%s]", | ||
| shardRouting.shardId(), | ||
| node.nodeId() | ||
| ); | ||
| } | ||
| return allocation.decision(Decision.YES, NAME, "node is not a replica-only node"); | ||
| } | ||
|
|
||
| @Override | ||
| public Decision shouldAutoExpandToNode(IndexMetadata indexMetadata, DiscoveryNode node, RoutingAllocation allocation) { | ||
| if (!node.isReplicaOnlyNode()) { | ||
| // Regular data nodes participate in auto-expand for all indices | ||
| return allocation.decision(Decision.YES, NAME, "node [%s] is a data node, eligible for auto-expand", node.getId()); | ||
| } | ||
|
|
||
| // Replica-only nodes only participate in 0-all auto-expand | ||
| AutoExpandReplicas autoExpandReplicas = AutoExpandReplicas.SETTING.get(indexMetadata.getSettings()); | ||
| boolean isAutoExpandAll = autoExpandReplicas.isEnabled() | ||
| && autoExpandReplicas.getMaxReplicas() == Integer.MAX_VALUE | ||
| && autoExpandReplicas.toString().startsWith("0-"); | ||
|
|
||
| if (isAutoExpandAll) { | ||
| return allocation.decision( | ||
| Decision.YES, | ||
| NAME, | ||
| "replica-only node [%s] is eligible for auto-expand replicas from index [%s] with auto_expand_replicas: 0-all", | ||
| node.getId(), | ||
| indexMetadata.getIndex().getName() | ||
| ); | ||
| } else { | ||
| return allocation.decision( | ||
| Decision.NO, | ||
| NAME, | ||
| "replica-only node [%s] is not eligible for index [%s] without auto_expand_replicas: 0-all", | ||
| node.getId(), | ||
| indexMetadata.getIndex().getName() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private Decision canAllocate(ShardRouting shardRouting, DiscoveryNode node, RoutingAllocation allocation) { | ||
| boolean isReplicaOnlyNode = node.isReplicaOnlyNode(); | ||
|
|
||
| // Case 1: Primary shard allocation | ||
| if (shardRouting.primary()) { | ||
| if (isReplicaOnlyNode) { | ||
| return allocation.decision( | ||
| Decision.NO, | ||
| NAME, | ||
| "primary shard [%s] cannot be allocated to replica-only node [%s]", | ||
| shardRouting.shardId(), | ||
| node.getId() | ||
| ); | ||
| } | ||
| // Allow primaries on regular data nodes | ||
| return allocation.decision(Decision.YES, NAME, "node [%s] is a data node, can host primary shard", node.getId()); | ||
| } | ||
|
|
||
| // Case 2: Replica shard allocation | ||
| IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index()); | ||
| AutoExpandReplicas autoExpandReplicas = AutoExpandReplicas.SETTING.get(indexMetadata.getSettings()); | ||
| boolean isAutoExpandAll = autoExpandReplicas.isEnabled() | ||
| && autoExpandReplicas.getMaxReplicas() == Integer.MAX_VALUE | ||
| && autoExpandReplicas.toString().startsWith("0-"); | ||
|
|
||
| if (isReplicaOnlyNode) { | ||
| if (isAutoExpandAll) { | ||
| return allocation.decision( | ||
| Decision.YES, | ||
| NAME, | ||
| "replica shard [%s] from auto-expand (0-all) index can be allocated to replica-only node [%s]", | ||
| shardRouting.shardId(), | ||
| node.getId() | ||
| ); | ||
| } else { | ||
| return allocation.decision( | ||
| Decision.NO, | ||
| NAME, | ||
| "replica shard [%s] cannot be allocated to replica-only node [%s] " | ||
| + "because index [%s] does not have auto_expand_replicas: 0-all", | ||
| shardRouting.shardId(), | ||
| node.getId(), | ||
| indexMetadata.getIndex().getName() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Regular data nodes can host any replica | ||
| return allocation.decision(Decision.YES, NAME, "node [%s] is a data node, can host replica shard", node.getId()); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
Critical: Early return leaves cluster without a primary shard.
The guard returns early without promoting the replica, but at this point
movePrimaryToUnassignedAndDemoteToReplica()has already been called inunassignPrimaryAndPromoteActiveReplicaIfExists(). This leaves the shard group with no primary:The replica-only check should occur before the primary is demoted, ideally in
unassignPrimaryAndPromoteActiveReplicaIfExists()when selecting the active replica. The replica selection logic should skip replicas on replica-only nodes.🔎 Suggested approach
Modify the replica selection in
unassignPrimaryAndPromoteActiveReplicaIfExists()to exclude replicas on replica-only nodes:private void unassignPrimaryAndPromoteActiveReplicaIfExists( ShardRouting failedShard, UnassignedInfo unassignedInfo, RoutingChangesObserver routingChangesObserver ) { assert failedShard.primary(); ShardRouting activeReplica = null; if (isMigratingToRemoteStore(metadata)) { activeReplica = activeReplicaOnRemoteNode(failedShard.shardId()); + // Skip if on replica-only node + if (activeReplica != null && isReplicaOnReplicaOnlyNode(activeReplica)) { + activeReplica = null; + } } if (activeReplica == null) { if (metadata.isSegmentReplicationEnabled(failedShard.getIndexName())) { activeReplica = activeReplicaWithOldestVersion(failedShard.shardId()); } else { activeReplica = activeReplicaWithHighestVersion(failedShard.shardId()); } + // Skip if on replica-only node + if (activeReplica != null && isReplicaOnReplicaOnlyNode(activeReplica)) { + activeReplica = null; + } } // ... rest of method } + +private boolean isReplicaOnReplicaOnlyNode(ShardRouting replica) { + RoutingNode rn = node(replica.currentNodeId()); + return rn != null && rn.node().isReplicaOnlyNode(); +}Alternatively, filter replica-only nodes in
activeReplicaWithHighestVersion()andactiveReplicaWithOldestVersion().