Search in sources :

Example 1 with CapturingTransport

use of org.opensearch.test.transport.CapturingTransport in project OpenSearch by opensearch-project.

the class TransportVerifyShardBeforeCloseActionTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    indexShard = mock(IndexShard.class);
    when(indexShard.getActiveOperationsCount()).thenReturn(IndexShard.OPERATIONS_BLOCKED);
    final ShardId shardId = new ShardId("index", "_na_", randomIntBetween(0, 3));
    when(indexShard.shardId()).thenReturn(shardId);
    clusterService = createClusterService(threadPool);
    clusterBlock = MetadataIndexStateService.createIndexClosingBlock();
    setState(clusterService, new ClusterState.Builder(clusterService.state()).blocks(ClusterBlocks.builder().blocks(clusterService.state().blocks()).addIndexBlock("index", clusterBlock).build()).build());
    transport = new CapturingTransport();
    TransportService transportService = transport.createTransportService(Settings.EMPTY, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null, Collections.emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    ShardStateAction shardStateAction = new ShardStateAction(clusterService, transportService, null, null, threadPool);
    action = new TransportVerifyShardBeforeCloseAction(Settings.EMPTY, transportService, clusterService, mock(IndicesService.class), mock(ThreadPool.class), shardStateAction, mock(ActionFilters.class));
}
Also used : ShardId(org.opensearch.index.shard.ShardId) ClusterState(org.opensearch.cluster.ClusterState) TransportService(org.opensearch.transport.TransportService) IndexShard(org.opensearch.index.shard.IndexShard) CapturingTransport(org.opensearch.test.transport.CapturingTransport) ShardStateAction(org.opensearch.cluster.action.shard.ShardStateAction) Before(org.junit.Before)

Example 2 with CapturingTransport

use of org.opensearch.test.transport.CapturingTransport in project OpenSearch by opensearch-project.

the class GetIndexActionTests method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    settingsFilter = new SettingsModule(Settings.EMPTY, emptyList(), emptyList(), emptySet()).getSettingsFilter();
    threadPool = new TestThreadPool("GetIndexActionTests");
    clusterService = getInstanceFromNode(ClusterService.class);
    indicesService = getInstanceFromNode(IndicesService.class);
    CapturingTransport capturingTransport = new CapturingTransport();
    transportService = capturingTransport.createTransportService(clusterService.getSettings(), threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundAddress -> clusterService.localNode(), null, emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    getIndexAction = new GetIndexActionTests.TestTransportGetIndexAction();
}
Also used : SettingsFilter(org.opensearch.common.settings.SettingsFilter) OpenSearchSingleNodeTestCase(org.opensearch.test.OpenSearchSingleNodeTestCase) Collections.emptySet(java.util.Collections.emptySet) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) Index(org.opensearch.index.Index) Collections.emptyList(java.util.Collections.emptyList) ThreadPool(org.opensearch.threadpool.ThreadPool) IndicesService(org.opensearch.indices.IndicesService) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Settings(org.opensearch.common.settings.Settings) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) TransportService(org.opensearch.transport.TransportService) ClusterStateCreationUtils(org.opensearch.action.support.replication.ClusterStateCreationUtils) TimeUnit(java.util.concurrent.TimeUnit) IndicesRequest(org.opensearch.action.IndicesRequest) ActionFilters(org.opensearch.action.support.ActionFilters) SettingsModule(org.opensearch.common.settings.SettingsModule) ClusterState(org.opensearch.cluster.ClusterState) After(org.junit.After) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) CapturingTransport(org.opensearch.test.transport.CapturingTransport) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Before(org.junit.Before) ClusterService(org.opensearch.cluster.service.ClusterService) CapturingTransport(org.opensearch.test.transport.CapturingTransport) SettingsModule(org.opensearch.common.settings.SettingsModule) IndicesService(org.opensearch.indices.IndicesService) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Before(org.junit.Before)

Example 3 with CapturingTransport

use of org.opensearch.test.transport.CapturingTransport in project OpenSearch by opensearch-project.

the class GetSettingsActionTests method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    settingsFilter = new SettingsModule(Settings.EMPTY, emptyList(), emptyList(), emptySet()).getSettingsFilter();
    threadPool = new TestThreadPool("GetSettingsActionTests");
    clusterService = createClusterService(threadPool);
    CapturingTransport capturingTransport = new CapturingTransport();
    transportService = capturingTransport.createTransportService(clusterService.getSettings(), threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, boundAddress -> clusterService.localNode(), null, Collections.emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    getSettingsAction = new GetSettingsActionTests.TestTransportGetSettingsAction();
}
Also used : IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ClusterStateCreationUtils(org.opensearch.action.support.replication.ClusterStateCreationUtils) IndicesRequest(org.opensearch.action.IndicesRequest) ClusterState(org.opensearch.cluster.ClusterState) After(org.junit.After) ActionListener(org.opensearch.action.ActionListener) Before(org.junit.Before) SettingsFilter(org.opensearch.common.settings.SettingsFilter) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) Collections.emptySet(java.util.Collections.emptySet) Index(org.opensearch.index.Index) Collections.emptyList(java.util.Collections.emptyList) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Settings(org.opensearch.common.settings.Settings) TransportService(org.opensearch.transport.TransportService) TimeUnit(java.util.concurrent.TimeUnit) ActionFilters(org.opensearch.action.support.ActionFilters) SettingsModule(org.opensearch.common.settings.SettingsModule) ClusterService(org.opensearch.cluster.service.ClusterService) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Collections(java.util.Collections) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) CapturingTransport(org.opensearch.test.transport.CapturingTransport) SettingsModule(org.opensearch.common.settings.SettingsModule) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Before(org.junit.Before)

Example 4 with CapturingTransport

use of org.opensearch.test.transport.CapturingTransport in project OpenSearch by opensearch-project.

the class TransportWriteActionTests method testReplicaProxy.

public void testReplicaProxy() throws InterruptedException, ExecutionException {
    CapturingTransport transport = new CapturingTransport();
    TransportService transportService = transport.createTransportService(clusterService.getSettings(), threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null, Collections.emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    ShardStateAction shardStateAction = new ShardStateAction(clusterService, transportService, null, null, threadPool);
    TestAction action = new TestAction(Settings.EMPTY, "internal:testAction", transportService, clusterService, shardStateAction, threadPool);
    final String index = "test";
    final ShardId shardId = new ShardId(index, "_na_", 0);
    ClusterState state = ClusterStateCreationUtils.stateWithActivePrimary(index, true, 1 + randomInt(3), randomInt(2));
    logger.info("using state: {}", state);
    ClusterServiceUtils.setState(clusterService, state);
    final long primaryTerm = state.metadata().index(index).primaryTerm(0);
    ReplicationOperation.Replicas<TestRequest> proxy = action.newReplicasProxy();
    // check that at unknown node fails
    PlainActionFuture<ReplicaResponse> listener = new PlainActionFuture<>();
    ShardRoutingState routingState = randomFrom(ShardRoutingState.INITIALIZING, ShardRoutingState.STARTED, ShardRoutingState.RELOCATING);
    proxy.performOn(TestShardRouting.newShardRouting(shardId, "NOT THERE", routingState == ShardRoutingState.RELOCATING ? state.nodes().iterator().next().getId() : null, false, routingState), new TestRequest(), primaryTerm, randomNonNegativeLong(), randomNonNegativeLong(), listener);
    assertTrue(listener.isDone());
    assertListenerThrows("non existent node should throw a NoNodeAvailableException", listener, NoNodeAvailableException.class);
    final IndexShardRoutingTable shardRoutings = state.routingTable().shardRoutingTable(shardId);
    final ShardRouting replica = randomFrom(shardRoutings.replicaShards().stream().filter(ShardRouting::assignedToNode).collect(Collectors.toList()));
    listener = new PlainActionFuture<>();
    proxy.performOn(replica, new TestRequest(), primaryTerm, randomNonNegativeLong(), randomNonNegativeLong(), listener);
    assertFalse(listener.isDone());
    CapturingTransport.CapturedRequest[] captures = transport.getCapturedRequestsAndClear();
    assertThat(captures, arrayWithSize(1));
    if (randomBoolean()) {
        final TransportReplicationAction.ReplicaResponse response = new TransportReplicationAction.ReplicaResponse(randomLong(), randomLong());
        transport.handleResponse(captures[0].requestId, response);
        assertTrue(listener.isDone());
        assertThat(listener.get(), equalTo(response));
    } else if (randomBoolean()) {
        transport.handleRemoteError(captures[0].requestId, new OpenSearchException("simulated"));
        assertTrue(listener.isDone());
        assertListenerThrows("listener should reflect remote error", listener, OpenSearchException.class);
    } else {
        transport.handleError(captures[0].requestId, new TransportException("simulated"));
        assertTrue(listener.isDone());
        assertListenerThrows("listener should reflect remote error", listener, TransportException.class);
    }
    AtomicReference<Object> failure = new AtomicReference<>();
    AtomicBoolean success = new AtomicBoolean();
    proxy.failShardIfNeeded(replica, primaryTerm, "test", new OpenSearchException("simulated"), ActionListener.wrap(r -> success.set(true), failure::set));
    CapturingTransport.CapturedRequest[] shardFailedRequests = transport.getCapturedRequestsAndClear();
    // A write replication action proxy should fail the shard
    assertEquals(1, shardFailedRequests.length);
    CapturingTransport.CapturedRequest shardFailedRequest = shardFailedRequests[0];
    ShardStateAction.FailedShardEntry shardEntry = (ShardStateAction.FailedShardEntry) shardFailedRequest.request;
    // the shard the request was sent to and the shard to be failed should be the same
    assertEquals(shardEntry.getShardId(), replica.shardId());
    assertEquals(shardEntry.getAllocationId(), replica.allocationId().getId());
    if (randomBoolean()) {
        // simulate success
        transport.handleResponse(shardFailedRequest.requestId, TransportResponse.Empty.INSTANCE);
        assertTrue(success.get());
        assertNull(failure.get());
    } else if (randomBoolean()) {
        // simulate the primary has been demoted
        transport.handleRemoteError(shardFailedRequest.requestId, new ShardStateAction.NoLongerPrimaryShardException(replica.shardId(), "shard-failed-test"));
        assertFalse(success.get());
        assertNotNull(failure.get());
    } else {
        // simulated a node closing exception
        transport.handleRemoteError(shardFailedRequest.requestId, new NodeClosedException(state.nodes().getLocalNode()));
        assertFalse(success.get());
        assertNotNull(failure.get());
    }
}
Also used : TestThreadPool(org.opensearch.threadpool.TestThreadPool) OpenSearchException(org.opensearch.OpenSearchException) NoNodeAvailableException(org.opensearch.client.transport.NoNodeAvailableException) Transport(org.opensearch.transport.Transport) ReplicaResponse(org.opensearch.action.support.replication.ReplicationOperation.ReplicaResponse) Mockito.doThrow(org.mockito.Mockito.doThrow) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) WriteResponse(org.opensearch.action.support.WriteResponse) After(org.junit.After) Mockito.doAnswer(org.mockito.Mockito.doAnswer) NodeClosedException(org.opensearch.node.NodeClosedException) ActionListener(org.opensearch.action.ActionListener) ShardStateAction(org.opensearch.cluster.action.shard.ShardStateAction) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) AfterClass(org.junit.AfterClass) Index(org.opensearch.index.Index) IndexingPressureService(org.opensearch.index.IndexingPressureService) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IndicesService(org.opensearch.indices.IndicesService) Settings(org.opensearch.common.settings.Settings) TransportResponse(org.opensearch.transport.TransportResponse) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ActionFilters(org.opensearch.action.support.ActionFilters) ActionTestUtils(org.opensearch.action.support.ActionTestUtils) Matchers.equalTo(org.hamcrest.Matchers.equalTo) RoutingNode(org.opensearch.cluster.routing.RoutingNode) Mockito.any(org.mockito.Mockito.any) ClusterServiceUtils(org.opensearch.test.ClusterServiceUtils) TransportException(org.opensearch.transport.TransportException) RefreshPolicy(org.opensearch.action.support.WriteRequest.RefreshPolicy) Mockito.mock(org.mockito.Mockito.mock) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) BeforeClass(org.junit.BeforeClass) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ThreadPool(org.opensearch.threadpool.ThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Releasable(org.opensearch.common.lease.Releasable) AtomicReference(java.util.concurrent.atomic.AtomicReference) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) IndexShard(org.opensearch.index.shard.IndexShard) ArgumentCaptor(org.mockito.ArgumentCaptor) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) Translog(org.opensearch.index.translog.Translog) Mockito.anyLong(org.mockito.Mockito.anyLong) Mockito.anyString(org.mockito.Mockito.anyString) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) Collections.emptyMap(java.util.Collections.emptyMap) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) IOException(java.io.IOException) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) Mockito.when(org.mockito.Mockito.when) IndexService(org.opensearch.index.IndexService) Mockito.verify(org.mockito.Mockito.verify) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) SystemIndices(org.opensearch.indices.SystemIndices) Mockito.never(org.mockito.Mockito.never) ClusterService(org.opensearch.cluster.service.ClusterService) Mockito.anyInt(org.mockito.Mockito.anyInt) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Collections(java.util.Collections) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) ShardStateAction(org.opensearch.cluster.action.shard.ShardStateAction) Mockito.anyString(org.mockito.Mockito.anyString) ShardId(org.opensearch.index.shard.ShardId) NodeClosedException(org.opensearch.node.NodeClosedException) ClusterState(org.opensearch.cluster.ClusterState) CapturingTransport(org.opensearch.test.transport.CapturingTransport) AtomicReference(java.util.concurrent.atomic.AtomicReference) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) TransportException(org.opensearch.transport.TransportException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransportService(org.opensearch.transport.TransportService) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ReplicaResponse(org.opensearch.action.support.replication.ReplicationOperation.ReplicaResponse) OpenSearchException(org.opensearch.OpenSearchException) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting)

Example 5 with CapturingTransport

use of org.opensearch.test.transport.CapturingTransport in project OpenSearch by opensearch-project.

the class TransportNodesActionTests method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    transport = new CapturingTransport();
    clusterService = createClusterService(THREAD_POOL);
    transportService = transport.createTransportService(clusterService.getSettings(), THREAD_POOL, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null, Collections.emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    int numNodes = randomIntBetween(3, 10);
    DiscoveryNodes.Builder discoBuilder = DiscoveryNodes.builder();
    List<DiscoveryNode> discoveryNodes = new ArrayList<>();
    for (int i = 0; i < numNodes; i++) {
        Map<String, String> attributes = new HashMap<>();
        Set<DiscoveryNodeRole> roles = new HashSet<>(randomSubsetOf(DiscoveryNodeRole.BUILT_IN_ROLES));
        if (frequently()) {
            attributes.put("custom", randomBoolean() ? "match" : randomAlphaOfLengthBetween(3, 5));
        }
        final DiscoveryNode node = newNode(i, attributes, roles);
        discoBuilder = discoBuilder.add(node);
        discoveryNodes.add(node);
    }
    discoBuilder.localNodeId(randomFrom(discoveryNodes).getId());
    discoBuilder.masterNodeId(randomFrom(discoveryNodes).getId());
    ClusterState.Builder stateBuilder = ClusterState.builder(clusterService.getClusterName());
    stateBuilder.nodes(discoBuilder);
    ClusterState clusterState = stateBuilder.build();
    setState(clusterService, clusterState);
}
Also used : DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) BeforeClass(org.junit.BeforeClass) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Version(org.opensearch.Version) StreamOutput(org.opensearch.common.io.stream.StreamOutput) HashMap(java.util.HashMap) ClusterServiceUtils.setState(org.opensearch.test.ClusterServiceUtils.setState) Writeable(org.opensearch.common.io.stream.Writeable) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) After(org.junit.After) Map(java.util.Map) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) TransportBroadcastByNodeActionTests(org.opensearch.action.support.broadcast.node.TransportBroadcastByNodeActionTests) AfterClass(org.junit.AfterClass) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) FailedNodeException(org.opensearch.action.FailedNodeException) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) IOException(java.io.IOException) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) TransportService(org.opensearch.transport.TransportService) TimeUnit(java.util.concurrent.TimeUnit) ActionFilters(org.opensearch.action.support.ActionFilters) List(java.util.List) ClusterService(org.opensearch.cluster.service.ClusterService) ClusterName(org.opensearch.cluster.ClusterName) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) ClusterState(org.opensearch.cluster.ClusterState) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) DiscoveryNodeRole(org.opensearch.cluster.node.DiscoveryNodeRole) HashMap(java.util.HashMap) CapturingTransport(org.opensearch.test.transport.CapturingTransport) ArrayList(java.util.ArrayList) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

CapturingTransport (org.opensearch.test.transport.CapturingTransport)24 TransportService (org.opensearch.transport.TransportService)24 Collections (java.util.Collections)16 Settings (org.opensearch.common.settings.Settings)16 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)16 ActionFilters (org.opensearch.action.support.ActionFilters)15 TestThreadPool (org.opensearch.threadpool.TestThreadPool)15 ThreadPool (org.opensearch.threadpool.ThreadPool)15 Before (org.junit.Before)14 ActionListener (org.opensearch.action.ActionListener)14 ClusterState (org.opensearch.cluster.ClusterState)14 ClusterService (org.opensearch.cluster.service.ClusterService)14 After (org.junit.After)13 ClusterServiceUtils.createClusterService (org.opensearch.test.ClusterServiceUtils.createClusterService)13 TimeUnit (java.util.concurrent.TimeUnit)12 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)11 IOException (java.io.IOException)10 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)10 StreamInput (org.opensearch.common.io.stream.StreamInput)10 HashSet (java.util.HashSet)9