Skip to content

Commit

Permalink
Segment Replication - Update /_cat/segment_replication API with backp…
Browse files Browse the repository at this point in the history
…ressure metrics. (#6674) (#6741)

* Segment Replication - Update Segment Replication API with backpressure metrics.

This change updates the existing /_cat/segment_replication API to include backpressure metrics.
It does this by returning stats from primary shards for its tracked replication group and merging it with metrics returned from replicas.
Primary captured  metrics will now appear by default, with replica per sync events showing when detailed=true is set.



* PR Feedback.



* Fixed current_lag header alias.



---------

Signed-off-by: Marc Handalian <handalm@amazon.com>
Co-authored-by: Marc Handalian <handalm@amazon.com>
  • Loading branch information
Poojita-Raj and mch2 authored Mar 18, 2023
1 parent 70707fe commit f8f4cc6
Show file tree
Hide file tree
Showing 15 changed files with 582 additions and 195 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.opensearch.index.Index;
import org.opensearch.index.IndexModule;
import org.opensearch.index.IndexService;
import org.opensearch.index.SegmentReplicationPerGroupStats;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.index.store.Store;
import org.opensearch.index.store.StoreFileMetadata;
Expand Down Expand Up @@ -143,10 +144,14 @@ protected void waitForSegmentReplication(String node) throws Exception {
SegmentReplicationStatsResponse segmentReplicationStatsResponse = client(node).admin()
.indices()
.prepareSegmentReplicationStats(INDEX_NAME)
.setDetailed(true)
.execute()
.actionGet();
final SegmentReplicationPerGroupStats perGroupStats = segmentReplicationStatsResponse.getReplicationStats()
.get(INDEX_NAME)
.get(0);
assertEquals(
segmentReplicationStatsResponse.shardSegmentReplicationStates().get(INDEX_NAME).get(0).getStage(),
perGroupStats.getReplicaStats().stream().findFirst().get().getCurrentReplicationState().getStage(),
SegmentReplicationState.Stage.DONE
);
}, 1, TimeUnit.MINUTES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ public void testReplicaHasDiffFilesThanPrimary() throws Exception {
assertNotEquals(replicaAfterFailure.routingEntry().allocationId().getId(), replicaShard.routingEntry().allocationId().getId());
}

public void testSegmentReplicationStats() throws Exception {
public void testPressureServiceStats() throws Exception {
final String primaryNode = internalCluster().startNode();
createIndex(INDEX_NAME);
final String replicaNode = internalCluster().startNode();
Expand Down Expand Up @@ -784,7 +784,6 @@ public void testSegmentReplicationStats() throws Exception {
SegmentReplicationPerGroupStats groupStats = shardStats.get(primaryShard.shardId());
Set<SegmentReplicationShardStats> replicaStats = groupStats.getReplicaStats();
assertEquals(1, replicaStats.size());
assertEquals(replica.routingEntry().currentNodeId(), replicaStats.stream().findFirst().get().getNodeId());

// assert replica node returns nothing.
SegmentReplicationPressureService replicaNode_service = internalCluster().getInstance(
Expand Down Expand Up @@ -816,7 +815,6 @@ public void testSegmentReplicationStats() throws Exception {
assertEquals(1, replicaNode_service.nodeStats().getShardStats().size());
replicaStats = replicaNode_service.nodeStats().getShardStats().get(primaryShard.shardId()).getReplicaStats();
assertEquals(1, replicaStats.size());
assertEquals(replica.routingEntry().currentNodeId(), replicaStats.stream().findFirst().get().getNodeId());

// test a checkpoint without any new segments
flush(INDEX_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public void testFlushAfterRelocation() throws Exception {
.prepareSegmentReplicationStats(INDEX_NAME)
.execute()
.actionGet();
assertFalse(segmentReplicationStatsResponse.hasSegmentReplicationStats());
assertTrue(segmentReplicationStatsResponse.getReplicationStats().get(INDEX_NAME).get(0).getReplicaStats().isEmpty());

// Relocate primary to new primary. When new primary starts it does perform a flush.
logger.info("--> relocate the shard from primary to newPrimary");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
import org.opensearch.action.admin.indices.replication.SegmentReplicationStatsResponse;
import org.opensearch.cluster.metadata.IndexMetadata;
import org.opensearch.common.settings.Settings;
import org.opensearch.index.SegmentReplicationPerGroupStats;
import org.opensearch.index.SegmentReplicationShardStats;
import org.opensearch.index.shard.IndexShard;
import org.opensearch.indices.replication.common.ReplicationType;
import org.opensearch.test.OpenSearchIntegTestCase;
import org.opensearch.test.transport.MockTransportService;
import org.opensearch.transport.TransportService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -53,18 +60,20 @@ public void testSegmentReplicationStatsResponse() throws Exception {
SegmentReplicationStatsResponse segmentReplicationStatsResponse = dataNodeClient().admin()
.indices()
.prepareSegmentReplicationStats(INDEX_NAME)
.setDetailed(true)
.execute()
.actionGet();
assertEquals(segmentReplicationStatsResponse.shardSegmentReplicationStates().size(), 1);
SegmentReplicationPerGroupStats perGroupStats = segmentReplicationStatsResponse.getReplicationStats().get(INDEX_NAME).get(0);
final SegmentReplicationState currentReplicationState = perGroupStats.getReplicaStats()
.stream()
.findFirst()
.get()
.getCurrentReplicationState();
assertEquals(segmentReplicationStatsResponse.getReplicationStats().size(), 1);
assertEquals(segmentReplicationStatsResponse.getTotalShards(), numShards * 2);
assertEquals(segmentReplicationStatsResponse.getSuccessfulShards(), numShards * 2);
assertEquals(
segmentReplicationStatsResponse.shardSegmentReplicationStates().get(INDEX_NAME).get(0).getStage(),
SegmentReplicationState.Stage.DONE
);
assertTrue(
segmentReplicationStatsResponse.shardSegmentReplicationStates().get(INDEX_NAME).get(0).getIndex().recoveredFileCount() > 0
);
assertEquals(currentReplicationState.getStage(), SegmentReplicationState.Stage.DONE);
assertTrue(currentReplicationState.getIndex().recoveredFileCount() > 0);
}, 1, TimeUnit.MINUTES);
}

Expand Down Expand Up @@ -120,27 +129,250 @@ public void testSegmentReplicationStatsResponseForActiveAndCompletedOnly() throw
.indices()
.prepareSegmentReplicationStats(INDEX_NAME)
.setActiveOnly(true)
.setDetailed(true)
.execute()
.actionGet();
assertEquals(
activeOnlyResponse.shardSegmentReplicationStates().get(INDEX_NAME).get(0).getStage(),
SegmentReplicationState.Stage.GET_FILES
);
SegmentReplicationPerGroupStats perGroupStats = activeOnlyResponse.getReplicationStats().get(INDEX_NAME).get(0);
SegmentReplicationState.Stage stage = perGroupStats.getReplicaStats()
.stream()
.findFirst()
.get()
.getCurrentReplicationState()
.getStage();
assertEquals(SegmentReplicationState.Stage.GET_FILES, stage);

// verifying completed_only by checking if current stage is DONE
SegmentReplicationStatsResponse completedOnlyResponse = client().admin()
.indices()
.prepareSegmentReplicationStats(INDEX_NAME)
.setDetailed(true)
.setCompletedOnly(true)
.execute()
.actionGet();
assertEquals(completedOnlyResponse.shardSegmentReplicationStates().size(), SHARD_COUNT);
assertEquals(
completedOnlyResponse.shardSegmentReplicationStates().get(INDEX_NAME).get(0).getStage(),
SegmentReplicationState.Stage.DONE
);
assertTrue(completedOnlyResponse.shardSegmentReplicationStates().get(INDEX_NAME).get(0).getIndex().recoveredFileCount() > 0);
assertEquals(completedOnlyResponse.getReplicationStats().size(), SHARD_COUNT);
perGroupStats = completedOnlyResponse.getReplicationStats().get(INDEX_NAME).get(0);
final SegmentReplicationState currentReplicationState = perGroupStats.getReplicaStats()
.stream()
.findFirst()
.get()
.getCurrentReplicationState();

assertEquals(SegmentReplicationState.Stage.DONE, currentReplicationState.getStage());
assertTrue(currentReplicationState.getIndex().recoveredFileCount() > 0);
waitForAssertions.countDown();
}

public void testNonDetailedResponse() throws Exception {
internalCluster().startClusterManagerOnlyNode();
int numReplicas = 4;
List<String> nodes = new ArrayList<>();
final String primaryNode = internalCluster().startNode();
nodes.add(primaryNode);
createIndex(
INDEX_NAME,
Settings.builder()
.put(indexSettings())
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, numReplicas)
.build()
);
ensureYellow(INDEX_NAME);
for (int i = 0; i < numReplicas; i++) {
nodes.add(internalCluster().startNode());
}
ensureGreen(INDEX_NAME);

final long numDocs = scaledRandomIntBetween(50, 100);
for (int i = 0; i < numDocs; i++) {
index(INDEX_NAME, "doc", Integer.toString(i));
}
refresh(INDEX_NAME);
waitForSearchableDocs(numDocs, nodes);

final IndexShard indexShard = getIndexShard(primaryNode, INDEX_NAME);

assertBusy(() -> {
SegmentReplicationStatsResponse segmentReplicationStatsResponse = dataNodeClient().admin()
.indices()
.prepareSegmentReplicationStats(INDEX_NAME)
.execute()
.actionGet();

final Map<String, List<SegmentReplicationPerGroupStats>> replicationStats = segmentReplicationStatsResponse
.getReplicationStats();
assertEquals(1, replicationStats.size());
final List<SegmentReplicationPerGroupStats> replicationPerGroupStats = replicationStats.get(INDEX_NAME);
assertEquals(1, replicationPerGroupStats.size());
final SegmentReplicationPerGroupStats perGroupStats = replicationPerGroupStats.get(0);
assertEquals(perGroupStats.getShardId(), indexShard.shardId());
final Set<SegmentReplicationShardStats> replicaStats = perGroupStats.getReplicaStats();
assertEquals(4, replicaStats.size());
for (SegmentReplicationShardStats replica : replicaStats) {
assertNotNull(replica.getCurrentReplicationState());
}
});
}

public void testGetSpecificShard() throws Exception {
internalCluster().startClusterManagerOnlyNode();
List<String> nodes = new ArrayList<>();
final String primaryNode = internalCluster().startNode();
nodes.add(primaryNode);
createIndex(
INDEX_NAME,
Settings.builder()
.put(indexSettings())
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
.build()
);
ensureYellowAndNoInitializingShards(INDEX_NAME);
nodes.add(internalCluster().startNode());
ensureGreen(INDEX_NAME);

final long numDocs = scaledRandomIntBetween(50, 100);
for (int i = 0; i < numDocs; i++) {
index(INDEX_NAME, "doc", Integer.toString(i));
}
refresh(INDEX_NAME);
waitForSearchableDocs(numDocs, nodes);

final IndexShard indexShard = getIndexShard(primaryNode, INDEX_NAME);

// search for all
SegmentReplicationStatsResponse segmentReplicationStatsResponse = client().admin()
.indices()
.prepareSegmentReplicationStats(INDEX_NAME)
.setActiveOnly(true)
.execute()
.actionGet();

Map<String, List<SegmentReplicationPerGroupStats>> replicationStats = segmentReplicationStatsResponse.getReplicationStats();
assertEquals(1, replicationStats.size());
List<SegmentReplicationPerGroupStats> replicationPerGroupStats = replicationStats.get(INDEX_NAME);
assertEquals(2, replicationPerGroupStats.size());
for (SegmentReplicationPerGroupStats group : replicationPerGroupStats) {
assertEquals(1, group.getReplicaStats().size());
}

// now search for one shard.
final int id = indexShard.shardId().getId();
segmentReplicationStatsResponse = client().admin()
.indices()
.prepareSegmentReplicationStats(INDEX_NAME)
.setActiveOnly(true)
.shards(String.valueOf(id))
.execute()
.actionGet();

replicationStats = segmentReplicationStatsResponse.getReplicationStats();
assertEquals(1, replicationStats.size());
replicationPerGroupStats = replicationStats.get(INDEX_NAME);
assertEquals(1, replicationPerGroupStats.size());
for (SegmentReplicationPerGroupStats group : replicationPerGroupStats) {
assertEquals(group.getShardId(), indexShard.shardId());
assertEquals(1, group.getReplicaStats().size());
}

}

public void testMultipleIndices() throws Exception {
internalCluster().startClusterManagerOnlyNode();
final String index_2 = "tst-index-2";
List<String> nodes = new ArrayList<>();
final String primaryNode = internalCluster().startNode();
nodes.add(primaryNode);
createIndex(INDEX_NAME, index_2);

ensureYellowAndNoInitializingShards(INDEX_NAME, index_2);
nodes.add(internalCluster().startNode());
ensureGreen(INDEX_NAME, index_2);

final long numDocs = scaledRandomIntBetween(50, 100);
for (int i = 0; i < numDocs; i++) {
index(INDEX_NAME, "doc", Integer.toString(i));
index(index_2, "doc", Integer.toString(i));
}
refresh(INDEX_NAME, index_2);
waitForSearchableDocs(INDEX_NAME, numDocs, nodes);
waitForSearchableDocs(index_2, numDocs, nodes);

final IndexShard index_1_primary = getIndexShard(primaryNode, INDEX_NAME);
final IndexShard index_2_primary = getIndexShard(primaryNode, index_2);

assertTrue(index_1_primary.routingEntry().primary());
assertTrue(index_2_primary.routingEntry().primary());

// test both indices are returned in the response.
SegmentReplicationStatsResponse segmentReplicationStatsResponse = client().admin()
.indices()
.prepareSegmentReplicationStats()
.execute()
.actionGet();

Map<String, List<SegmentReplicationPerGroupStats>> replicationStats = segmentReplicationStatsResponse.getReplicationStats();
assertEquals(2, replicationStats.size());
List<SegmentReplicationPerGroupStats> replicationPerGroupStats = replicationStats.get(INDEX_NAME);
assertEquals(1, replicationPerGroupStats.size());
SegmentReplicationPerGroupStats perGroupStats = replicationPerGroupStats.get(0);
assertEquals(perGroupStats.getShardId(), index_1_primary.shardId());
Set<SegmentReplicationShardStats> replicaStats = perGroupStats.getReplicaStats();
assertEquals(1, replicaStats.size());
for (SegmentReplicationShardStats replica : replicaStats) {
assertNotNull(replica.getCurrentReplicationState());
}

replicationPerGroupStats = replicationStats.get(index_2);
assertEquals(1, replicationPerGroupStats.size());
perGroupStats = replicationPerGroupStats.get(0);
assertEquals(perGroupStats.getShardId(), index_2_primary.shardId());
replicaStats = perGroupStats.getReplicaStats();
assertEquals(1, replicaStats.size());
for (SegmentReplicationShardStats replica : replicaStats) {
assertNotNull(replica.getCurrentReplicationState());
}

// test only single index queried.
segmentReplicationStatsResponse = client().admin()
.indices()
.prepareSegmentReplicationStats()
.setIndices(index_2)
.execute()
.actionGet();
assertEquals(1, segmentReplicationStatsResponse.getReplicationStats().size());
assertTrue(segmentReplicationStatsResponse.getReplicationStats().containsKey(index_2));
}

public void testQueryAgainstDocRepIndex() {
internalCluster().startClusterManagerOnlyNode();
List<String> nodes = new ArrayList<>();
final String primaryNode = internalCluster().startNode();
nodes.add(primaryNode);
createIndex(
INDEX_NAME,
Settings.builder()
.put(indexSettings())
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
.put(IndexMetadata.SETTING_REPLICATION_TYPE, ReplicationType.DOCUMENT)
.build()
);
ensureYellowAndNoInitializingShards(INDEX_NAME);
nodes.add(internalCluster().startNode());
ensureGreen(INDEX_NAME);

final long numDocs = scaledRandomIntBetween(50, 100);
for (int i = 0; i < numDocs; i++) {
index(INDEX_NAME, "doc", Integer.toString(i));
}
refresh(INDEX_NAME);

// search for all
SegmentReplicationStatsResponse segmentReplicationStatsResponse = client().admin()
.indices()
.prepareSegmentReplicationStats(INDEX_NAME)
.execute()
.actionGet();
assertTrue(segmentReplicationStatsResponse.getReplicationStats().isEmpty());
}
}
Loading

0 comments on commit f8f4cc6

Please sign in to comment.