forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce IndexRoutingTable#readyForSearch (elastic#99890)
This commit introduces IndexRoutingTable#readyForSearch to determine if a given index is available to for search.
- Loading branch information
1 parent
8aa6964
commit cbe3083
Showing
3 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
server/src/test/java/org/elasticsearch/cluster/routing/IndexRoutingTableTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.cluster.routing; | ||
|
||
import org.elasticsearch.cluster.ClusterState; | ||
import org.elasticsearch.common.UUIDs; | ||
import org.elasticsearch.common.settings.Settings; | ||
import org.elasticsearch.index.Index; | ||
import org.elasticsearch.index.shard.ShardId; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.List; | ||
|
||
import static org.elasticsearch.index.IndexSettings.INDEX_FAST_REFRESH_SETTING; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class IndexRoutingTableTests extends ESTestCase { | ||
|
||
public void testReadyForSearch() { | ||
innerReadyForSearch(false); | ||
innerReadyForSearch(true); | ||
} | ||
|
||
private void innerReadyForSearch(boolean fastRefresh) { | ||
Index index = new Index(randomIdentifier(), UUIDs.randomBase64UUID()); | ||
ClusterState clusterState = mock(ClusterState.class, Mockito.RETURNS_DEEP_STUBS); | ||
when(clusterState.metadata().index(any(Index.class)).getSettings()).thenReturn( | ||
Settings.builder().put(INDEX_FAST_REFRESH_SETTING.getKey(), fastRefresh).build() | ||
); | ||
// 2 primaries that are search and index | ||
ShardId p1 = new ShardId(index, 0); | ||
IndexShardRoutingTable shardTable1 = new IndexShardRoutingTable( | ||
p1, | ||
List.of(getShard(p1, true, ShardRoutingState.STARTED, ShardRouting.Role.DEFAULT)) | ||
); | ||
ShardId p2 = new ShardId(index, 1); | ||
IndexShardRoutingTable shardTable2 = new IndexShardRoutingTable( | ||
p2, | ||
List.of(getShard(p2, true, ShardRoutingState.STARTED, ShardRouting.Role.DEFAULT)) | ||
); | ||
IndexRoutingTable indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); | ||
assertTrue(indexRoutingTable.readyForSearch(clusterState)); | ||
|
||
// 2 primaries that are index only | ||
shardTable1 = new IndexShardRoutingTable(p1, List.of(getShard(p1, true, ShardRoutingState.STARTED, ShardRouting.Role.INDEX_ONLY))); | ||
shardTable2 = new IndexShardRoutingTable(p2, List.of(getShard(p2, true, ShardRoutingState.STARTED, ShardRouting.Role.INDEX_ONLY))); | ||
indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); | ||
if (fastRefresh) { | ||
assertTrue(indexRoutingTable.readyForSearch(clusterState)); | ||
} else { | ||
assertFalse(indexRoutingTable.readyForSearch(clusterState)); | ||
} | ||
|
||
// 2 unassigned primaries that are index only | ||
shardTable1 = new IndexShardRoutingTable( | ||
p1, | ||
List.of(getShard(p1, true, ShardRoutingState.UNASSIGNED, ShardRouting.Role.INDEX_ONLY)) | ||
); | ||
shardTable2 = new IndexShardRoutingTable( | ||
p2, | ||
List.of(getShard(p2, true, ShardRoutingState.UNASSIGNED, ShardRouting.Role.INDEX_ONLY)) | ||
); | ||
indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); | ||
assertFalse(indexRoutingTable.readyForSearch(clusterState)); | ||
|
||
// 2 primaries that are index only with replicas that are not all available | ||
shardTable1 = new IndexShardRoutingTable( | ||
p1, | ||
List.of( | ||
getShard(p1, true, ShardRoutingState.STARTED, ShardRouting.Role.INDEX_ONLY), | ||
getShard(p1, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY), | ||
getShard(p1, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY) | ||
) | ||
); | ||
shardTable2 = new IndexShardRoutingTable( | ||
p2, | ||
List.of( | ||
getShard(p2, true, ShardRoutingState.STARTED, ShardRouting.Role.INDEX_ONLY), | ||
getShard(p2, false, ShardRoutingState.UNASSIGNED, ShardRouting.Role.SEARCH_ONLY), | ||
getShard(p2, false, ShardRoutingState.UNASSIGNED, ShardRouting.Role.SEARCH_ONLY) | ||
) | ||
); | ||
indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); | ||
if (fastRefresh) { | ||
assertTrue(indexRoutingTable.readyForSearch(clusterState)); | ||
} else { | ||
assertFalse(indexRoutingTable.readyForSearch(clusterState)); | ||
} | ||
|
||
// 2 primaries that are index only with some replicas that are all available | ||
shardTable1 = new IndexShardRoutingTable( | ||
p1, | ||
List.of( | ||
getShard(p1, true, ShardRoutingState.STARTED, ShardRouting.Role.INDEX_ONLY), | ||
getShard(p1, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY), | ||
getShard(p1, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY) | ||
) | ||
); | ||
shardTable2 = new IndexShardRoutingTable( | ||
p2, | ||
List.of( | ||
getShard(p2, true, ShardRoutingState.STARTED, ShardRouting.Role.INDEX_ONLY), | ||
getShard(p2, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY), | ||
getShard(p2, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY) | ||
) | ||
); | ||
indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); | ||
assertTrue(indexRoutingTable.readyForSearch(clusterState)); | ||
|
||
// 2 unassigned primaries that are index only with some replicas that are all available | ||
// Fast refresh indices do not support replicas so this can not practically happen. If we add support we will want to ensure | ||
// that readyForSearch allows for searching replicas when the index shard is not available. | ||
shardTable1 = new IndexShardRoutingTable( | ||
p1, | ||
List.of( | ||
getShard(p1, true, ShardRoutingState.UNASSIGNED, ShardRouting.Role.INDEX_ONLY), | ||
getShard(p1, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY), | ||
getShard(p1, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY) | ||
) | ||
); | ||
shardTable2 = new IndexShardRoutingTable( | ||
p2, | ||
List.of( | ||
getShard(p2, true, ShardRoutingState.UNASSIGNED, ShardRouting.Role.INDEX_ONLY), | ||
getShard(p2, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY), | ||
getShard(p2, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY) | ||
) | ||
); | ||
indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); | ||
if (fastRefresh) { | ||
assertFalse(indexRoutingTable.readyForSearch(clusterState)); // if we support replicas for fast refreshes this needs to change | ||
} else { | ||
assertTrue(indexRoutingTable.readyForSearch(clusterState)); | ||
} | ||
|
||
// 2 primaries that are index only with at least 1 replica per primary that is available | ||
shardTable1 = new IndexShardRoutingTable( | ||
p1, | ||
List.of( | ||
getShard(p1, true, ShardRoutingState.STARTED, ShardRouting.Role.INDEX_ONLY), | ||
getShard(p1, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY), | ||
getShard(p1, false, ShardRoutingState.UNASSIGNED, ShardRouting.Role.SEARCH_ONLY) | ||
) | ||
); | ||
shardTable2 = new IndexShardRoutingTable( | ||
p2, | ||
List.of( | ||
getShard(p2, true, ShardRoutingState.STARTED, ShardRouting.Role.INDEX_ONLY), | ||
getShard(p2, false, ShardRoutingState.UNASSIGNED, ShardRouting.Role.SEARCH_ONLY), | ||
getShard(p2, false, ShardRoutingState.STARTED, ShardRouting.Role.SEARCH_ONLY) | ||
) | ||
); | ||
indexRoutingTable = new IndexRoutingTable(index, new IndexShardRoutingTable[] { shardTable1, shardTable2 }); | ||
assertTrue(indexRoutingTable.readyForSearch(clusterState)); | ||
} | ||
|
||
private ShardRouting getShard(ShardId shardId, boolean isPrimary, ShardRoutingState state, ShardRouting.Role role) { | ||
return new ShardRouting( | ||
shardId, | ||
state == ShardRoutingState.UNASSIGNED ? null : randomIdentifier(), | ||
state == ShardRoutingState.UNASSIGNED || state == ShardRoutingState.STARTED ? null : randomIdentifier(), | ||
isPrimary, | ||
state, | ||
TestShardRouting.buildRecoveryTarget(isPrimary, state), | ||
TestShardRouting.buildUnassignedInfo(state), | ||
TestShardRouting.buildRelocationFailureInfo(state), | ||
TestShardRouting.buildAllocationId(state), | ||
randomLongBetween(-1, 1024), | ||
role | ||
); | ||
} | ||
|
||
} |