forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Prudhvi Godithi <[email protected]>
- Loading branch information
1 parent
9f790ee
commit 3e57800
Showing
13 changed files
with
489 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
115 changes: 115 additions & 0 deletions
115
.../opensearch/cluster/routing/allocation/decider/RemoveIndexingShardsAllocationDecider.java
This file contains 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 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.opensearch.cluster.routing.allocation.decider; | ||
|
||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.opensearch.cluster.metadata.IndexMetadata; | ||
import org.opensearch.cluster.routing.RoutingNode; | ||
import org.opensearch.cluster.routing.ShardRouting; | ||
import org.opensearch.cluster.routing.allocation.RoutingAllocation; | ||
|
||
public class RemoveIndexingShardsAllocationDecider extends AllocationDecider { | ||
private static final Logger logger = LogManager.getLogger(RemoveIndexingShardsAllocationDecider.class); | ||
|
||
public static final String NAME = "remove_indexing_shards"; | ||
|
||
@Override | ||
public Decision canAllocate(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { | ||
IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index()); | ||
boolean removeIndexingShardsEnabled = indexMetadata.getSettings().getAsBoolean("index.remove_indexing_shards.enabled", false); | ||
|
||
logger.debug("[canAllocate] Shard [{}] on node [{}], removeIndexingShards=[{}], searchOnly=[{}], settings={}", | ||
shardRouting.shardId(), | ||
node.nodeId(), | ||
removeIndexingShardsEnabled, | ||
shardRouting.isSearchOnly(), | ||
indexMetadata.getSettings().toString() | ||
); | ||
|
||
if (!removeIndexingShardsEnabled) { | ||
return allocation.decision(Decision.YES, NAME, "remove indexing shards is not enabled"); | ||
} | ||
|
||
// Only allow search replica allocation when remove_indexing_shards is enabled | ||
if (!shardRouting.primary() && shardRouting.isSearchOnly()) { | ||
return allocation.decision(Decision.YES, NAME, "search replicas are allowed"); | ||
} | ||
|
||
return allocation.decision(Decision.NO, NAME, "remove indexing shards enabled: only search replicas allowed"); | ||
} | ||
|
||
@Override | ||
public Decision canRemain(ShardRouting shardRouting, RoutingNode node, RoutingAllocation allocation) { | ||
IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index()); | ||
boolean removeIndexingShardsEnabled = indexMetadata.getSettings().getAsBoolean("index.remove_indexing_shards.enabled", false); | ||
|
||
logger.debug("[canRemain] Shard [{}] on node [{}], removeIndexingShards=[{}], searchOnly=[{}], settings={}", | ||
shardRouting.shardId(), | ||
node.nodeId(), | ||
removeIndexingShardsEnabled, | ||
shardRouting.isSearchOnly(), | ||
indexMetadata.getSettings().toString() | ||
); | ||
|
||
if (!removeIndexingShardsEnabled) { | ||
return allocation.decision(Decision.YES, NAME, "remove indexing shards is not enabled"); | ||
} | ||
|
||
// When remove_indexing_shards is enabled: To remove primary shards | ||
if (shardRouting.primary()) { | ||
if (hasHealthySearchReplicas(shardRouting, allocation)) { | ||
logger.info("Forcing removal of primary shard [{}] as search replicas are healthy", shardRouting.shardId()); | ||
return allocation.decision(Decision.NO, NAME, "removing primary with healthy search replicas"); | ||
} | ||
return allocation.decision(Decision.NO, NAME, "primary must be removed when remove_indexing_shards is enabled"); | ||
} | ||
|
||
//Allow only search replicas to remain | ||
if (shardRouting.isSearchOnly()) { | ||
return allocation.decision(Decision.YES, NAME, "search replicas are allowed"); | ||
} | ||
|
||
//Remove regular replicas | ||
logger.info("Forcing removal of regular replica shard [{}]", shardRouting.shardId()); | ||
return allocation.decision(Decision.NO, NAME, "regular replicas must be removed"); | ||
} | ||
|
||
@Override | ||
public Decision canRebalance(RoutingAllocation allocation) { | ||
boolean hasRemoveIndexingShardsIndex = allocation.metadata().indices().values().stream() | ||
.anyMatch(idx -> idx.getSettings().getAsBoolean("index.remove_indexing_shards.enabled", false)); | ||
|
||
if (hasRemoveIndexingShardsIndex) { | ||
logger.info("Allowing rebalancing to facilitate remove indexing shards"); | ||
return allocation.decision(Decision.YES, NAME, "allowing rebalance for remove indexing shards"); | ||
} | ||
|
||
return Decision.ALWAYS; | ||
} | ||
|
||
@Override | ||
public Decision canMoveAway(ShardRouting shardRouting, RoutingAllocation allocation) { | ||
IndexMetadata indexMetadata = allocation.metadata().getIndexSafe(shardRouting.index()); | ||
boolean removeIndexingShardsEnabled = indexMetadata.getSettings().getAsBoolean("index.remove_indexing_shards.enabled", false); | ||
|
||
if (removeIndexingShardsEnabled && !shardRouting.isSearchOnly()) { | ||
logger.info("Forcing non-search shard [{}] to move away", shardRouting.shardId()); | ||
return allocation.decision(Decision.YES, NAME, "must move away non-search shards"); | ||
} | ||
|
||
return Decision.ALWAYS; | ||
} | ||
|
||
private boolean hasHealthySearchReplicas(ShardRouting shardRouting, RoutingAllocation allocation) { | ||
boolean hasHealthy = allocation.routingTable() | ||
.shardRoutingTable(shardRouting.shardId()) | ||
.activeShards() | ||
.stream() | ||
.anyMatch(shard -> !shard.primary() && shard.isSearchOnly() && shard.active()); | ||
|
||
logger.debug("Search replica check for shard [{}]: hasHealthy=[{}]", shardRouting.shardId(), hasHealthy); | ||
return hasHealthy; | ||
} | ||
} |
This file contains 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 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.