Search in sources :

Example 1 with ShardNotInPrimaryModeException

use of org.opensearch.index.shard.ShardNotInPrimaryModeException 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 2 with ShardNotInPrimaryModeException

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

the class TransportWriteActionForIndexingPressureTests 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());
    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) ShardNotInPrimaryModeException(org.opensearch.index.shard.ShardNotInPrimaryModeException) IOException(java.io.IOException) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) 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)

Aggregations

IOException (java.io.IOException)2 Mockito.anyLong (org.mockito.Mockito.anyLong)2 ActionListener (org.opensearch.action.ActionListener)2 ClusterState (org.opensearch.cluster.ClusterState)2 RoutingNode (org.opensearch.cluster.routing.RoutingNode)2 ShardRouting (org.opensearch.cluster.routing.ShardRouting)2 Releasable (org.opensearch.common.lease.Releasable)2 IndexShard (org.opensearch.index.shard.IndexShard)2 ReplicationGroup (org.opensearch.index.shard.ReplicationGroup)2 ShardNotFoundException (org.opensearch.index.shard.ShardNotFoundException)2 ShardNotInPrimaryModeException (org.opensearch.index.shard.ShardNotInPrimaryModeException)2 ExecutionException (java.util.concurrent.ExecutionException)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 OpenSearchException (org.opensearch.OpenSearchException)1 UnavailableShardsException (org.opensearch.action.UnavailableShardsException)1 NoNodeAvailableException (org.opensearch.client.transport.NoNodeAvailableException)1 ClusterBlockException (org.opensearch.cluster.block.ClusterBlockException)1 TestShardRouting (org.opensearch.cluster.routing.TestShardRouting)1 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)1 IndexShardClosedException (org.opensearch.index.shard.IndexShardClosedException)1