Skip to content

Commit 65bca15

Browse files
authored
Introduce ShardRouting.Role (#92668)
Introduces the concept of a `ShardRouting.Role` which allows plugins to assign different roles to each shard copy in a replication group. Unlike the primary/replica flag, a shard copy role sticks to the shard copy throughout its life and is preserved across relocations and reassignments.
1 parent 8df0008 commit 65bca15

File tree

162 files changed

+1917
-502
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

162 files changed

+1917
-502
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/routing/allocation/AllocationBenchmark.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void setUp() throws Exception {
131131
);
132132
}
133133
Metadata metadata = mb.build();
134-
RoutingTable.Builder rb = RoutingTable.builder();
134+
RoutingTable.Builder rb = RoutingTable.builder(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY);
135135
for (int i = 1; i <= numIndices; i++) {
136136
rb.addAsNew(metadata.index("test_" + i));
137137
}

benchmarks/src/main/java/org/elasticsearch/benchmark/routing/allocation/Allocators.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public static AllocationService createAllocationService(Settings settings, Clust
8181
NoopGatewayAllocator.INSTANCE,
8282
new BalancedShardsAllocator(settings),
8383
EmptyClusterInfoService.INSTANCE,
84-
EmptySnapshotsInfoService.INSTANCE
84+
EmptySnapshotsInfoService.INSTANCE,
85+
TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY
8586
);
8687
}
8788

benchmarks/src/main/java/org/elasticsearch/benchmark/routing/allocation/ShardsAvailabilityHealthIndicatorBenchmark.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,16 @@ public void setUp() throws Exception {
129129
.numberOfReplicas(numReplicas)
130130
.build();
131131

132-
final IndexRoutingTable.Builder indexRountingTableBuilder = new IndexRoutingTable.Builder(indexMetadata.getIndex());
132+
final IndexRoutingTable.Builder indexRountingTableBuilder = IndexRoutingTable.builder(indexMetadata.getIndex());
133133
for (int shardIdNumber = 0; shardIdNumber < numShards; shardIdNumber++) {
134134
ShardId shardId = new ShardId(indexMetadata.getIndex(), shardIdNumber);
135135
final IndexShardRoutingTable.Builder shardBuilder = new IndexShardRoutingTable.Builder(shardId);
136136
ShardRouting shardRouting = ShardRouting.newUnassigned(
137137
shardId,
138138
true,
139139
RecoverySource.ExistingStoreRecoverySource.INSTANCE,
140-
decidersNoUnassignedInfo
140+
decidersNoUnassignedInfo,
141+
ShardRouting.Role.DEFAULT
141142
);
142143
shardBuilder.addShard(shardRouting);
143144
if (shardIdNumber < numReplicas) {
@@ -146,7 +147,8 @@ public void setUp() throws Exception {
146147
shardId,
147148
false,
148149
RecoverySource.EmptyStoreRecoverySource.INSTANCE,
149-
decidersNoUnassignedInfo
150+
decidersNoUnassignedInfo,
151+
ShardRouting.Role.DEFAULT
150152
)
151153
);
152154
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
package org.elasticsearch.benchmark.routing.allocation;
10+
11+
import org.elasticsearch.cluster.routing.ShardRouting;
12+
import org.elasticsearch.cluster.routing.ShardRoutingRoleStrategy;
13+
14+
public class TestShardRoutingRoleStrategies {
15+
16+
/**
17+
* A strategy which only returns the default role in all situations. This is deliberately not available to production code to avoid any
18+
* possibility of using it instead of the strategy provided by the plugin (if so configured).
19+
*/
20+
public static final ShardRoutingRoleStrategy DEFAULT_ROLE_ONLY = new ShardRoutingRoleStrategy() {
21+
@Override
22+
public ShardRouting.Role newReplicaRole() {
23+
return ShardRouting.Role.DEFAULT;
24+
}
25+
26+
@Override
27+
public ShardRouting.Role newEmptyRole(int copyIndex) {
28+
return ShardRouting.Role.DEFAULT;
29+
}
30+
};
31+
32+
private TestShardRoutingRoleStrategies() {
33+
// no instances
34+
}
35+
}

docs/changelog/92668.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 92668
2+
summary: "Introduce ShardRouting.Role"
3+
area: Allocation
4+
type: enhancement
5+
issues: []

modules/data-streams/src/test/java/org/elasticsearch/datastreams/DataStreamGetWriteIndexTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.action.admin.indices.rollover.MetadataRolloverService;
1818
import org.elasticsearch.action.index.IndexRequest;
1919
import org.elasticsearch.cluster.ClusterState;
20+
import org.elasticsearch.cluster.TestShardRoutingRoleStrategies;
2021
import org.elasticsearch.cluster.metadata.ComposableIndexTemplate;
2122
import org.elasticsearch.cluster.metadata.DataStream;
2223
import org.elasticsearch.cluster.metadata.DataStreamTestHelper;
@@ -248,6 +249,7 @@ public void setup() throws Exception {
248249
when(env.sharedDataFile()).thenReturn(null);
249250
AllocationService allocationService = mock(AllocationService.class);
250251
when(allocationService.reroute(any(ClusterState.class), any(String.class), any())).then(i -> i.getArguments()[0]);
252+
when(allocationService.getShardRoutingRoleStrategy()).thenReturn(TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY);
251253
ShardLimitValidator shardLimitValidator = new ShardLimitValidator(Settings.EMPTY, clusterService);
252254
createIndexService = new MetadataCreateIndexService(
253255
Settings.EMPTY,

modules/ingest-geoip/src/test/java/org/elasticsearch/ingest/geoip/DatabaseNodeServiceTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ static ClusterState createClusterState(PersistentTasksCustomMetadata tasksCustom
383383
new ShardId(index, 0),
384384
true,
385385
RecoverySource.ExistingStoreRecoverySource.INSTANCE,
386-
new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "")
386+
new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, ""),
387+
ShardRouting.Role.DEFAULT
387388
);
388389
String nodeId = ESTestCase.randomAlphaOfLength(8);
389390
shardRouting = shardRouting.initialize(nodeId, null, shardRouting.getExpectedShardSize());

server/src/internalClusterTest/java/org/elasticsearch/cluster/coordination/RareClusterStateIT.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.elasticsearch.action.support.master.AcknowledgedResponse;
2121
import org.elasticsearch.cluster.ClusterState;
2222
import org.elasticsearch.cluster.ClusterStateUpdateTask;
23+
import org.elasticsearch.cluster.TestShardRoutingRoleStrategies;
2324
import org.elasticsearch.cluster.block.ClusterBlocks;
2425
import org.elasticsearch.cluster.metadata.IndexMetadata;
2526
import org.elasticsearch.cluster.metadata.MappingMetadata;
@@ -102,7 +103,10 @@ public ClusterState execute(ClusterState currentState) {
102103
builder.blocks(ClusterBlocks.builder().blocks(currentState.blocks()).removeIndexBlocks(index));
103104
ClusterState updatedState = builder.build();
104105

105-
RoutingTable.Builder routingTable = RoutingTable.builder(updatedState.routingTable());
106+
RoutingTable.Builder routingTable = RoutingTable.builder(
107+
TestShardRoutingRoleStrategies.DEFAULT_ROLE_ONLY,
108+
updatedState.routingTable()
109+
);
106110
routingTable.addAsRecovery(updatedState.metadata().index(index));
107111
updatedState = ClusterState.builder(updatedState).routingTable(routingTable.build()).build();
108112

0 commit comments

Comments
 (0)