Skip to content

Commit 61d4d43

Browse files
authored
[Segment Replication] Add ClusterState utility to identify SEGMENT replication (#9593)
* [Segment Replication] Add ClusterState utility to identify SEGMENT replication Signed-off-by: Suraj Singh <surajrider@gmail.com> * Address review comment Signed-off-by: Suraj Singh <surajrider@gmail.com> * Address review comments Signed-off-by: Suraj Singh <surajrider@gmail.com> --------- Signed-off-by: Suraj Singh <surajrider@gmail.com>
1 parent 012c4fa commit 61d4d43

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

server/src/main/java/org/opensearch/action/get/TransportGetAction.java

+1-13
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.opensearch.action.support.ActionFilters;
3737
import org.opensearch.action.support.single.shard.TransportSingleShardAction;
3838
import org.opensearch.cluster.ClusterState;
39-
import org.opensearch.cluster.metadata.IndexMetadata;
4039
import org.opensearch.cluster.metadata.IndexNameExpressionResolver;
4140
import org.opensearch.cluster.routing.Preference;
4241
import org.opensearch.cluster.routing.ShardIterator;
@@ -49,12 +48,10 @@
4948
import org.opensearch.index.get.GetResult;
5049
import org.opensearch.index.shard.IndexShard;
5150
import org.opensearch.indices.IndicesService;
52-
import org.opensearch.indices.replication.common.ReplicationType;
5351
import org.opensearch.threadpool.ThreadPool;
5452
import org.opensearch.transport.TransportService;
5553

5654
import java.io.IOException;
57-
import java.util.Optional;
5855

5956
/**
6057
* Performs the get operation.
@@ -92,20 +89,11 @@ protected boolean resolveIndex(GetRequest request) {
9289
return true;
9390
}
9491

95-
static boolean isSegmentReplicationEnabled(ClusterState state, String indexName) {
96-
return Optional.ofNullable(state.getMetadata().index(indexName))
97-
.map(
98-
indexMetadata -> ReplicationType.parseString(indexMetadata.getSettings().get(IndexMetadata.SETTING_REPLICATION_TYPE))
99-
.equals(ReplicationType.SEGMENT)
100-
)
101-
.orElse(false);
102-
}
103-
10492
/**
10593
* Returns true if GET request should be routed to primary shards, else false.
10694
*/
10795
protected static boolean shouldForcePrimaryRouting(ClusterState state, boolean realtime, String preference, String indexName) {
108-
return isSegmentReplicationEnabled(state, indexName) && realtime && preference == null;
96+
return state.isSegmentReplicationEnabled(indexName) && realtime && preference == null;
10997
}
11098

11199
@Override

server/src/main/java/org/opensearch/cluster/ClusterState.java

+16
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.opensearch.core.xcontent.ToXContentFragment;
6262
import org.opensearch.core.xcontent.XContentBuilder;
6363
import org.opensearch.discovery.Discovery;
64+
import org.opensearch.indices.replication.common.ReplicationType;
6465

6566
import java.io.IOException;
6667
import java.util.Collections;
@@ -409,6 +410,21 @@ public boolean supersedes(ClusterState other) {
409410

410411
}
411412

413+
/**
414+
* Utility to identify whether input index belongs to SEGMENT replication in established cluster state.
415+
*
416+
* @param indexName Index name
417+
* @return true if index belong SEGMENT replication, false otherwise
418+
*/
419+
public boolean isSegmentReplicationEnabled(String indexName) {
420+
return Optional.ofNullable(this.getMetadata().index(indexName))
421+
.map(
422+
indexMetadata -> ReplicationType.parseString(indexMetadata.getSettings().get(IndexMetadata.SETTING_REPLICATION_TYPE))
423+
.equals(ReplicationType.SEGMENT)
424+
)
425+
.orElse(false);
426+
}
427+
412428
/**
413429
* Metrics for cluster state.
414430
*

server/src/test/java/org/opensearch/cluster/ClusterStateTests.java

+36-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.opensearch.core.rest.RestStatus;
5858
import org.opensearch.core.xcontent.ToXContent;
5959
import org.opensearch.core.xcontent.XContentBuilder;
60+
import org.opensearch.indices.replication.common.ReplicationType;
6061
import org.opensearch.test.OpenSearchTestCase;
6162
import org.opensearch.test.TestCustomMetadata;
6263

@@ -73,6 +74,7 @@
7374
import static java.util.Collections.emptyMap;
7475
import static java.util.Collections.emptySet;
7576
import static java.util.Collections.singletonMap;
77+
import static org.opensearch.cluster.ClusterName.CLUSTER_NAME_SETTING;
7678
import static org.opensearch.cluster.metadata.IndexMetadata.SETTING_VERSION_CREATED;
7779
import static org.hamcrest.Matchers.containsString;
7880
import static org.hamcrest.Matchers.equalTo;
@@ -84,7 +86,7 @@ public void testSupersedes() {
8486
final DiscoveryNode node1 = new DiscoveryNode("node1", buildNewFakeTransportAddress(), emptyMap(), emptySet(), version);
8587
final DiscoveryNode node2 = new DiscoveryNode("node2", buildNewFakeTransportAddress(), emptyMap(), emptySet(), version);
8688
final DiscoveryNodes nodes = DiscoveryNodes.builder().add(node1).add(node2).build();
87-
ClusterName name = ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY);
89+
ClusterName name = CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY);
8890
ClusterState noClusterManager1 = ClusterState.builder(name).version(randomInt(5)).nodes(nodes).build();
8991
ClusterState noClusterManager2 = ClusterState.builder(name).version(randomInt(5)).nodes(nodes).build();
9092
ClusterState withClusterManager1a = ClusterState.builder(name)
@@ -115,6 +117,39 @@ public void testSupersedes() {
115117
);
116118
}
117119

120+
public void testIsSegmentReplicationEnabled() {
121+
final String indexName = "test";
122+
ClusterState clusterState = ClusterState.builder(CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).build();
123+
Settings.Builder builder = settings(Version.CURRENT).put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.SEGMENT);
124+
IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName)
125+
.settings(builder)
126+
.numberOfShards(1)
127+
.numberOfReplicas(1);
128+
Metadata.Builder metadataBuilder = Metadata.builder().put(indexMetadataBuilder);
129+
RoutingTable.Builder routingTableBuilder = RoutingTable.builder().addAsNew(indexMetadataBuilder.build());
130+
clusterState = ClusterState.builder(clusterState)
131+
.metadata(metadataBuilder.build())
132+
.routingTable(routingTableBuilder.build())
133+
.build();
134+
assertTrue(clusterState.isSegmentReplicationEnabled(indexName));
135+
}
136+
137+
public void testIsSegmentReplicationDisabled() {
138+
final String indexName = "test";
139+
ClusterState clusterState = ClusterState.builder(CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).build();
140+
IndexMetadata.Builder indexMetadataBuilder = IndexMetadata.builder(indexName)
141+
.settings(settings(Version.CURRENT))
142+
.numberOfShards(1)
143+
.numberOfReplicas(1);
144+
Metadata.Builder metadataBuilder = Metadata.builder().put(indexMetadataBuilder);
145+
RoutingTable.Builder routingTableBuilder = RoutingTable.builder().addAsNew(indexMetadataBuilder.build());
146+
clusterState = ClusterState.builder(clusterState)
147+
.metadata(metadataBuilder.build())
148+
.routingTable(routingTableBuilder.build())
149+
.build();
150+
assertFalse(clusterState.isSegmentReplicationEnabled(indexName));
151+
}
152+
118153
public void testBuilderRejectsNullCustom() {
119154
final ClusterState.Builder builder = ClusterState.builder(ClusterName.DEFAULT);
120155
final String key = randomAlphaOfLength(10);

0 commit comments

Comments
 (0)