Skip to content

Commit

Permalink
Segment Replication - Update Segment Replication API with backpressur…
Browse files Browse the repository at this point in the history
…e 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.

Signed-off-by: Marc Handalian <handalm@amazon.com>
  • Loading branch information
mch2 committed Mar 15, 2023
1 parent 1e5d913 commit a9575f8
Show file tree
Hide file tree
Showing 14 changed files with 560 additions and 193 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 @@ -749,7 +749,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 @@ -783,7 +783,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 @@ -815,7 +814,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 @@ -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,228 @@ 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);
nodes.add(internalCluster().startNode());
createIndex(
INDEX_NAME,
Settings.builder()
.put(indexSettings())
.put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 2)
.put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1)
.build()
);
ensureYellow(INDEX_NAME);
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(internalCluster().startNode());

nodes.add(primaryNode);
createIndex(INDEX_NAME);
createIndex(index_2);
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 indexShard = getIndexShard(primaryNode, INDEX_NAME);

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

final Map<String, List<SegmentReplicationPerGroupStats>> replicationStats = segmentReplicationStatsResponse
.getReplicationStats();
assertEquals(2, 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(1, replicaStats.size());
for (SegmentReplicationShardStats replica : replicaStats) {
assertNotNull(replica.getCurrentReplicationState());
}
});
}

public void testQueryAgainstDocRepIndex() {
internalCluster().startClusterManagerOnlyNode();
List<String> nodes = new ArrayList<>();
final String primaryNode = internalCluster().startNode();
nodes.add(primaryNode);
nodes.add(internalCluster().startNode());
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()
);
ensureYellow(INDEX_NAME);
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 a9575f8

Please sign in to comment.