Search in sources :

Example 1 with IllegalIndexShardStateException

use of org.opensearch.index.shard.IllegalIndexShardStateException in project OpenSearch by opensearch-project.

the class IndicesServiceTests method testStatsByShardDoesNotDieFromExpectedExceptions.

public void testStatsByShardDoesNotDieFromExpectedExceptions() {
    final int shardCount = randomIntBetween(2, 5);
    final int failedShardId = randomIntBetween(0, shardCount - 1);
    final Index index = new Index("test-index", "abc123");
    // the shard that is going to fail
    final ShardId shardId = new ShardId(index, failedShardId);
    final List<IndexShard> shards = new ArrayList<>(shardCount);
    final List<IndexShardStats> shardStats = new ArrayList<>(shardCount - 1);
    final IndexShardState state = randomFrom(IndexShardState.values());
    final String message = "TEST - expected";
    final RuntimeException expectedException = randomFrom(new IllegalIndexShardStateException(shardId, state, message), new AlreadyClosedException(message));
    // this allows us to control the indices that exist
    final IndicesService mockIndicesService = mock(IndicesService.class);
    final IndexService indexService = mock(IndexService.class);
    // generate fake shards and their responses
    for (int i = 0; i < shardCount; ++i) {
        final IndexShard shard = mock(IndexShard.class);
        shards.add(shard);
        if (failedShardId != i) {
            final IndexShardStats successfulShardStats = mock(IndexShardStats.class);
            shardStats.add(successfulShardStats);
            when(mockIndicesService.indexShardStats(mockIndicesService, shard, CommonStatsFlags.ALL)).thenReturn(successfulShardStats);
        } else {
            when(mockIndicesService.indexShardStats(mockIndicesService, shard, CommonStatsFlags.ALL)).thenThrow(expectedException);
        }
    }
    when(mockIndicesService.iterator()).thenReturn(Collections.singleton(indexService).iterator());
    when(indexService.iterator()).thenReturn(shards.iterator());
    when(indexService.index()).thenReturn(index);
    // real one, which has a logger defined
    final IndicesService indicesService = getIndicesService();
    final Map<Index, List<IndexShardStats>> indexStats = indicesService.statsByShard(mockIndicesService, CommonStatsFlags.ALL);
    assertThat(indexStats.isEmpty(), equalTo(false));
    assertThat("index not defined", indexStats.containsKey(index), equalTo(true));
    assertThat("unexpected shard stats", indexStats.get(index), equalTo(shardStats));
}
Also used : IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) ArrayList(java.util.ArrayList) Index(org.opensearch.index.Index) IndexShardStats(org.opensearch.action.admin.indices.stats.IndexShardStats) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IllegalIndexShardStateException(org.opensearch.index.shard.IllegalIndexShardStateException) ShardId(org.opensearch.index.shard.ShardId) List(java.util.List) ArrayList(java.util.ArrayList) IndexShardState(org.opensearch.index.shard.IndexShardState)

Example 2 with IllegalIndexShardStateException

use of org.opensearch.index.shard.IllegalIndexShardStateException in project OpenSearch by opensearch-project.

the class ExceptionSerializationTests method testIllegalIndexShardStateException.

public void testIllegalIndexShardStateException() throws IOException {
    ShardId id = new ShardId("foo", "_na_", 1);
    IndexShardState state = randomFrom(IndexShardState.values());
    IllegalIndexShardStateException ex = serialize(new IllegalIndexShardStateException(id, state, "come back later buddy"));
    assertEquals(id, ex.getShardId());
    assertEquals("CurrentState[" + state.name() + "] come back later buddy", ex.getMessage());
    assertEquals(state, ex.currentState());
}
Also used : ShardId(org.opensearch.index.shard.ShardId) IllegalIndexShardStateException(org.opensearch.index.shard.IllegalIndexShardStateException) IndexShardState(org.opensearch.index.shard.IndexShardState)

Aggregations

IllegalIndexShardStateException (org.opensearch.index.shard.IllegalIndexShardStateException)2 IndexShardState (org.opensearch.index.shard.IndexShardState)2 ShardId (org.opensearch.index.shard.ShardId)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Matchers.hasToString (org.hamcrest.Matchers.hasToString)1 IndexShardStats (org.opensearch.action.admin.indices.stats.IndexShardStats)1 Index (org.opensearch.index.Index)1 IndexService (org.opensearch.index.IndexService)1 IndexShard (org.opensearch.index.shard.IndexShard)1