Search in sources :

Example 1 with ShardNotFoundException

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

the class TransportWriteActionTests method mockIndexService.

final IndexService mockIndexService(final IndexMetadata indexMetadata, ClusterService clusterService) {
    final IndexService indexService = mock(IndexService.class);
    when(indexService.getShard(anyInt())).then(invocation -> {
        int shard = (Integer) invocation.getArguments()[0];
        final ShardId shardId = new ShardId(indexMetadata.getIndex(), shard);
        if (shard > indexMetadata.getNumberOfShards()) {
            throw new ShardNotFoundException(shardId);
        }
        return mockIndexShard(shardId, clusterService);
    });
    return indexService;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ShardId(org.opensearch.index.shard.ShardId) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) IndexService(org.opensearch.index.IndexService)

Example 2 with ShardNotFoundException

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

the class TransportWriteActionTests method mockIndexShard.

private IndexShard mockIndexShard(ShardId shardId, ClusterService clusterService) {
    final IndexShard indexShard = mock(IndexShard.class);
    doAnswer(invocation -> {
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[0];
        count.incrementAndGet();
        callback.onResponse(count::decrementAndGet);
        return null;
    }).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), any());
    doAnswer(invocation -> {
        long term = (Long) invocation.getArguments()[0];
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[1];
        final long primaryTerm = indexShard.getPendingPrimaryTerm();
        if (term < primaryTerm) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "%s operation term [%d] is too old (current [%d])", shardId, term, primaryTerm));
        }
        count.incrementAndGet();
        callback.onResponse(count::decrementAndGet);
        return null;
    }).when(indexShard).acquireReplicaOperationPermit(anyLong(), anyLong(), anyLong(), any(ActionListener.class), anyString(), any());
    when(indexShard.routingEntry()).thenAnswer(invocationOnMock -> {
        final ClusterState state = clusterService.state();
        final RoutingNode node = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
        final ShardRouting routing = node.getByShardId(shardId);
        if (routing == null) {
            throw new ShardNotFoundException(shardId, "shard is no longer assigned to current node");
        }
        return routing;
    });
    when(indexShard.isRelocatedPrimary()).thenAnswer(invocationOnMock -> isRelocated.get());
    doThrow(new AssertionError("failed shard is not supported")).when(indexShard).failShard(anyString(), any(Exception.class));
    when(indexShard.getPendingPrimaryTerm()).thenAnswer(i -> clusterService.state().metadata().getIndexSafe(shardId.getIndex()).primaryTerm(shardId.id()));
    return indexShard;
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) IndexShard(org.opensearch.index.shard.IndexShard) OpenSearchException(org.opensearch.OpenSearchException) NoNodeAvailableException(org.opensearch.client.transport.NoNodeAvailableException) NodeClosedException(org.opensearch.node.NodeClosedException) TransportException(org.opensearch.transport.TransportException) IOException(java.io.IOException) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) ActionListener(org.opensearch.action.ActionListener) RoutingNode(org.opensearch.cluster.routing.RoutingNode) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) Mockito.anyLong(org.mockito.Mockito.anyLong) Releasable(org.opensearch.common.lease.Releasable) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting)

Example 3 with ShardNotFoundException

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

the class TransportReplicationActionTests method mockIndexService.

private IndexService mockIndexService(final IndexMetadata indexMetadata, ClusterService clusterService) {
    final IndexService indexService = mock(IndexService.class);
    when(indexService.getShard(anyInt())).then(invocation -> {
        int shard = (Integer) invocation.getArguments()[0];
        final ShardId shardId = new ShardId(indexMetadata.getIndex(), shard);
        if (shard > indexMetadata.getNumberOfShards()) {
            throw new ShardNotFoundException(shardId);
        }
        return mockIndexShard(shardId, clusterService);
    });
    return indexService;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ShardId(org.opensearch.index.shard.ShardId) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) IndexService(org.opensearch.index.IndexService)

Example 4 with ShardNotFoundException

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

the class TransportReplicationActionTests method mockIndexShard.

@SuppressWarnings("unchecked")
private IndexShard mockIndexShard(ShardId shardId, ClusterService clusterService) {
    final IndexShard indexShard = mock(IndexShard.class);
    when(indexShard.shardId()).thenReturn(shardId);
    when(indexShard.state()).thenReturn(IndexShardState.STARTED);
    doAnswer(invocation -> {
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[0];
        if (isPrimaryMode.get()) {
            count.incrementAndGet();
            callback.onResponse(count::decrementAndGet);
        } else {
            callback.onFailure(new ShardNotInPrimaryModeException(shardId, IndexShardState.STARTED));
        }
        return null;
    }).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), any(), eq(forceExecute));
    doAnswer(invocation -> {
        long term = (Long) invocation.getArguments()[0];
        ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[3];
        final long primaryTerm = indexShard.getPendingPrimaryTerm();
        if (term < primaryTerm) {
            throw new IllegalArgumentException(String.format(Locale.ROOT, "%s operation term [%d] is too old (current [%d])", shardId, term, primaryTerm));
        }
        count.incrementAndGet();
        callback.onResponse(count::decrementAndGet);
        return null;
    }).when(indexShard).acquireReplicaOperationPermit(anyLong(), anyLong(), anyLong(), any(ActionListener.class), anyString(), any());
    when(indexShard.getActiveOperationsCount()).thenAnswer(i -> count.get());
    when(indexShard.routingEntry()).thenAnswer(invocationOnMock -> {
        final ClusterState state = clusterService.state();
        final RoutingNode node = state.getRoutingNodes().node(state.nodes().getLocalNodeId());
        final ShardRouting routing = node.getByShardId(shardId);
        if (routing == null) {
            throw new ShardNotFoundException(shardId, "shard is no longer assigned to current node");
        }
        return routing;
    });
    when(indexShard.isRelocatedPrimary()).thenAnswer(invocationOnMock -> isRelocated.get());
    doThrow(new AssertionError("failed shard is not supported")).when(indexShard).failShard(anyString(), any(Exception.class));
    when(indexShard.getPendingPrimaryTerm()).thenAnswer(i -> clusterService.state().metadata().getIndexSafe(shardId.getIndex()).primaryTerm(shardId.id()));
    ReplicationGroup replicationGroup = mock(ReplicationGroup.class);
    when(indexShard.getReplicationGroup()).thenReturn(replicationGroup);
    return indexShard;
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ShardNotInPrimaryModeException(org.opensearch.index.shard.ShardNotInPrimaryModeException) IndexShard(org.opensearch.index.shard.IndexShard) ReplicationGroup(org.opensearch.index.shard.ReplicationGroup) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) NoNodeAvailableException(org.opensearch.client.transport.NoNodeAvailableException) TransportException(org.opensearch.transport.TransportException) IOException(java.io.IOException) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) ExecutionException(java.util.concurrent.ExecutionException) OpenSearchException(org.opensearch.OpenSearchException) UnavailableShardsException(org.opensearch.action.UnavailableShardsException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IndexShardClosedException(org.opensearch.index.shard.IndexShardClosedException) ShardNotInPrimaryModeException(org.opensearch.index.shard.ShardNotInPrimaryModeException) IndexClosedException(org.opensearch.indices.IndexClosedException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ActionListener(org.opensearch.action.ActionListener) RoutingNode(org.opensearch.cluster.routing.RoutingNode) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) Mockito.anyLong(org.mockito.Mockito.anyLong) Releasable(org.opensearch.common.lease.Releasable) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting)

Example 5 with ShardNotFoundException

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

the class TransportWriteActionForIndexingPressureTests method mockIndexService.

private IndexService mockIndexService(final IndexMetadata indexMetaData, ClusterService clusterService) {
    final IndexService indexService = mock(IndexService.class);
    when(indexService.getShard(anyInt())).then(invocation -> {
        int shard = (Integer) invocation.getArguments()[0];
        final ShardId shardId = new ShardId(indexMetaData.getIndex(), shard);
        if (shard > indexMetaData.getNumberOfShards()) {
            throw new ShardNotFoundException(shardId);
        }
        return mockIndexShard(shardId, clusterService);
    });
    return indexService;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ShardId(org.opensearch.index.shard.ShardId) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) IndexService(org.opensearch.index.IndexService)

Aggregations

ShardNotFoundException (org.opensearch.index.shard.ShardNotFoundException)12 RoutingNode (org.opensearch.cluster.routing.RoutingNode)6 ShardRouting (org.opensearch.cluster.routing.ShardRouting)6 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)5 ShardId (org.opensearch.index.shard.ShardId)5 IOException (java.io.IOException)4 ClusterState (org.opensearch.cluster.ClusterState)4 IndexService (org.opensearch.index.IndexService)4 IndexShard (org.opensearch.index.shard.IndexShard)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Mockito.anyLong (org.mockito.Mockito.anyLong)3 OpenSearchException (org.opensearch.OpenSearchException)3 ActionListener (org.opensearch.action.ActionListener)3 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)3 RoutingNodes (org.opensearch.cluster.routing.RoutingNodes)3 RerouteExplanation (org.opensearch.cluster.routing.allocation.RerouteExplanation)3 Releasable (org.opensearch.common.lease.Releasable)3 ExecutionException (java.util.concurrent.ExecutionException)2 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)2 NoNodeAvailableException (org.opensearch.client.transport.NoNodeAvailableException)2