-
Notifications
You must be signed in to change notification settings - Fork 24.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Monitoring] Make unassigned replica shard documents unique #91153
Changes from all commits
af5b954
1254963
bbf99a8
325dd67
bae4a08
10064b0
3fc42fb
29ec5dd
59da890
933a9ed
3574b9b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
package org.elasticsearch.xpack.monitoring.collector.shards; | ||
|
||
import org.elasticsearch.cluster.node.DiscoveryNode; | ||
import org.elasticsearch.cluster.routing.IndexRoutingTable; | ||
import org.elasticsearch.cluster.routing.IndexShardRoutingTable; | ||
import org.elasticsearch.cluster.routing.RoutingTable; | ||
import org.elasticsearch.cluster.routing.ShardRouting; | ||
import org.elasticsearch.cluster.routing.ShardRoutingState; | ||
|
@@ -20,7 +22,10 @@ | |
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
|
||
import static org.elasticsearch.cluster.routing.ShardRoutingState.STARTED; | ||
|
@@ -30,6 +35,7 @@ | |
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.hamcrest.Matchers.instanceOf; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.matchesPattern; | ||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.hamcrest.Matchers.nullValue; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
|
@@ -77,7 +83,12 @@ public void testDoCollect() throws Exception { | |
final String stateUUID = UUID.randomUUID().toString(); | ||
when(clusterState.stateUUID()).thenReturn(stateUUID); | ||
|
||
final String[] indices = randomFrom(NONE, Strings.EMPTY_ARRAY, new String[] { "_all" }, new String[] { "_index*" }); | ||
final String[] indices = randomFrom( | ||
NONE, | ||
Strings.EMPTY_ARRAY, | ||
new String[] { "_all" }, | ||
new String[] { "_index*", "_does-not-exist" } | ||
); | ||
withCollectionIndices(indices); | ||
|
||
final RoutingTable routingTable = mockRoutingTable(); | ||
|
@@ -111,7 +122,11 @@ public void testDoCollect() throws Exception { | |
assertThat(document.getIntervalMillis(), equalTo(interval)); | ||
assertThat(document.getSystem(), is(MonitoredSystem.ES)); | ||
assertThat(document.getType(), equalTo(ShardMonitoringDoc.TYPE)); | ||
assertThat(document.getId(), equalTo(ShardMonitoringDoc.id(stateUUID, document.getShardRouting()))); | ||
if (document.getShardRouting().primary()) { | ||
assertThat(document.getId(), equalTo(ShardMonitoringDoc.id(stateUUID, document.getShardRouting(), 0))); | ||
} else { | ||
assertThat(document.getId(), matchesPattern("^[\\w-]+:(_current|_na)+:_index:s\\d:r[1-9]+[0-9]*")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At this point, I don't know which index is being used for which monitoring document, so I'm falling back to just checking the format and that at least there isn't any replica |
||
} | ||
assertThat(document.getClusterStateUUID(), equalTo(stateUUID)); | ||
|
||
if (document.getShardRouting().assignedToNode()) { | ||
|
@@ -130,15 +145,60 @@ public void testDoCollect() throws Exception { | |
private static RoutingTable mockRoutingTable() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test setup here is starting to feel a bit messy, not sure what can be done about it. |
||
final List<ShardRouting> allShards = new ArrayList<>(); | ||
|
||
final int nbShards = randomIntBetween(0, 10); | ||
for (int i = 0; i < nbShards; i++) { | ||
final int numberOfPrimaryShards = randomIntBetween(0, 10); | ||
for (int i = 0; i < numberOfPrimaryShards; i++) { | ||
ShardRoutingState state = randomFrom(STARTED, UNASSIGNED); | ||
ShardId shardId = new ShardId("_index", randomAlphaOfLength(12), i); | ||
allShards.add(TestShardRouting.newShardRouting(shardId, state == STARTED ? "_current" : null, true, state)); | ||
ShardRouting primary = TestShardRouting.newShardRouting(shardId, state == STARTED ? "_current" : null, true, state); | ||
allShards.add(primary); | ||
} | ||
|
||
final int numberOfReplicaShards = randomIntBetween(0, 3); | ||
for (int i = 0; i < numberOfPrimaryShards; i++) { | ||
for (int j = 0; j < numberOfReplicaShards; j++) { | ||
ShardRoutingState state = randomFrom(STARTED, UNASSIGNED); | ||
ShardId shardId = new ShardId("_index", randomAlphaOfLength(12), i); | ||
ShardRouting replica = TestShardRouting.newShardRouting(shardId, state == STARTED ? "_current" : null, false, state); | ||
allShards.add(replica); | ||
} | ||
} | ||
|
||
final RoutingTable routingTable = mock(RoutingTable.class); | ||
// _index* matches the test data above | ||
when(routingTable.allShards("_index*")).thenReturn(allShards); | ||
// _all is reserved to mean all indices in the routing table | ||
when(routingTable.allShards("_all")).thenReturn(allShards); | ||
// When collection indices is set to [], it's treated the same as "_all", so the key set of the routing table is used to grab the | ||
// index names | ||
when(routingTable.allShards("_index")).thenReturn(allShards); | ||
miltonhultgren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// This mock routing table only has the index named "_index", so if collection indices is set to ["_none"] no shards should be | ||
// found. | ||
when(routingTable.allShards("_none")).thenReturn(new ArrayList<>(0)); | ||
|
||
final IndexRoutingTable indexRoutingTable = mock(IndexRoutingTable.class); | ||
final Map<String, IndexRoutingTable> indicesRouting = Map.of("_index", indexRoutingTable); | ||
when(routingTable.indicesRouting()).thenReturn(indicesRouting); | ||
when(routingTable.index("_index")).thenReturn(indexRoutingTable); | ||
|
||
when(indexRoutingTable.size()).thenReturn(numberOfPrimaryShards); | ||
for (int i = 0; i < numberOfPrimaryShards; i++) { | ||
final IndexShardRoutingTable shardRoutingTable = mock(IndexShardRoutingTable.class); | ||
when(indexRoutingTable.shard(i)).thenReturn(shardRoutingTable); | ||
when(shardRoutingTable.primaryShard()).thenReturn(allShards.get(i)); | ||
List<ShardRouting> replicas = new ArrayList<>(); | ||
int replicaIndexStart = numberOfPrimaryShards + i * numberOfReplicaShards; | ||
int replicaIndexEnd = replicaIndexStart + numberOfReplicaShards; | ||
for (int j = replicaIndexStart; j < replicaIndexEnd; j++) { | ||
replicas.add(allShards.get(j)); | ||
} | ||
when(shardRoutingTable.replicaShards()).thenReturn(replicas); | ||
} | ||
|
||
// This is only used by the test to decide how many shards should be covered | ||
when(routingTable.allShards()).thenReturn(allShards); | ||
|
||
Collections.shuffle(allShards, new Random(numberOfPrimaryShards)); | ||
|
||
return routingTable; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file has a lot of these nested levels which we could get rid of by flipping the guard statements, but I'm not sure what the idiomatic style is here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is pretty deeply nested, but I don't think it's so bad that it needs a refactoring right now. A lot of this code is deprecated and on a course to being a distant memory when the internal monitoring feature is removed.