Search in sources :

Example 76 with NamedWriteableRegistry

use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.

the class TransportReplicationActionTests method testRetryOnReplicaWithRealTransport.

public void testRetryOnReplicaWithRealTransport() throws Exception {
    final ShardId shardId = new ShardId("test", "_na_", 0);
    final ClusterState initialState = state(shardId.getIndexName(), true, ShardRoutingState.STARTED, ShardRoutingState.STARTED);
    final ShardRouting replica = initialState.getRoutingTable().shardRoutingTable(shardId).replicaShards().get(0);
    final long primaryTerm = initialState.metadata().index(shardId.getIndexName()).primaryTerm(shardId.id());
    // simulate execution of the node holding the replica
    final ClusterState stateWithNodes = ClusterState.builder(initialState).nodes(DiscoveryNodes.builder(initialState.nodes()).localNodeId(replica.currentNodeId())).build();
    setState(clusterService, stateWithNodes);
    AtomicBoolean throwException = new AtomicBoolean(true);
    final ReplicationTask task = maybeTask();
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(Collections.emptyList());
    final Transport transport = new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, new NetworkService(Collections.emptyList()), PageCacheRecycler.NON_RECYCLING_INSTANCE, namedWriteableRegistry, new NoneCircuitBreakerService());
    transportService = new MockTransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null, Collections.emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    AtomicBoolean calledSuccessfully = new AtomicBoolean(false);
    TestAction action = new TestAction(Settings.EMPTY, "internal:testActionWithExceptions", transportService, clusterService, shardStateAction, threadPool) {

        @Override
        protected void shardOperationOnReplica(Request shardRequest, IndexShard replica, ActionListener<ReplicaResult> listener) {
            ActionListener.completeWith(listener, () -> {
                assertPhase(task, "replica");
                if (throwException.get()) {
                    throw new RetryOnReplicaException(shardId, "simulation");
                }
                calledSuccessfully.set(true);
                return new ReplicaResult();
            });
        }
    };
    final PlainActionFuture<TransportResponse> listener = new PlainActionFuture<>();
    final Request request = new Request(shardId);
    final long checkpoint = randomNonNegativeLong();
    final long maxSeqNoOfUpdates = randomNonNegativeLong();
    action.handleReplicaRequest(new TransportReplicationAction.ConcreteReplicaRequest<>(request, replica.allocationId().getId(), primaryTerm, checkpoint, maxSeqNoOfUpdates), createTransportChannel(listener), task);
    if (listener.isDone()) {
        // fail with the exception if there
        listener.get();
        fail("listener shouldn't be done");
    }
    // release the waiting
    throwException.set(false);
    // publish a new state (same as the old state with the version incremented)
    setState(clusterService, stateWithNodes);
    // Assert that the request was retried, this time successful
    assertTrue("action should have been successfully called on retry but was not", calledSuccessfully.get());
    transportService.stop();
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Metadata(org.opensearch.cluster.metadata.Metadata) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService) TestThreadPool(org.opensearch.threadpool.TestThreadPool) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) ClusterStateChanges(org.opensearch.indices.cluster.ClusterStateChanges) Version(org.opensearch.Version) ClusterServiceUtils.setState(org.opensearch.test.ClusterServiceUtils.setState) NoNodeAvailableException(org.opensearch.client.transport.NoNodeAvailableException) Transport(org.opensearch.transport.Transport) Mockito.doThrow(org.mockito.Mockito.doThrow) ClusterBlock(org.opensearch.cluster.block.ClusterBlock) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) OpenSearchAllocationTestCase(org.opensearch.cluster.OpenSearchAllocationTestCase) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Is.is(org.hamcrest.core.Is.is) ActionListener(org.opensearch.action.ActionListener) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) ShardStateAction(org.opensearch.cluster.action.shard.ShardStateAction) EnumSet(java.util.EnumSet) AfterClass(org.junit.AfterClass) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) Index(org.opensearch.index.Index) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) TransportService(org.opensearch.transport.TransportService) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ActionFilters(org.opensearch.action.support.ActionFilters) ActionTestUtils(org.opensearch.action.support.ActionTestUtils) RoutingNode(org.opensearch.cluster.routing.RoutingNode) TransportException(org.opensearch.transport.TransportException) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) Matchers.arrayWithSize(org.hamcrest.Matchers.arrayWithSize) IndexShardState(org.opensearch.index.shard.IndexShardState) ThreadPool(org.opensearch.threadpool.ThreadPool) Releasable(org.opensearch.common.lease.Releasable) MockTransportService(org.opensearch.test.transport.MockTransportService) ClusterState(org.opensearch.cluster.ClusterState) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) Mockito.anyLong(org.mockito.Mockito.anyLong) ClusterBlocks(org.opensearch.cluster.block.ClusterBlocks) Mockito.anyString(org.mockito.Mockito.anyString) Before(org.junit.Before) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) IOException(java.io.IOException) ShardNotFoundException(org.opensearch.index.shard.ShardNotFoundException) IndexService(org.opensearch.index.IndexService) SETTING_WAIT_FOR_ACTIVE_SHARDS(org.opensearch.cluster.metadata.IndexMetadata.SETTING_WAIT_FOR_ACTIVE_SHARDS) ExecutionException(java.util.concurrent.ExecutionException) Matcher(org.hamcrest.Matcher) ClusterService(org.opensearch.cluster.service.ClusterService) NetworkService(org.opensearch.common.network.NetworkService) Mockito.anyInt(org.mockito.Mockito.anyInt) ReplicationGroup(org.opensearch.index.shard.ReplicationGroup) Assert(org.junit.Assert) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) OpenSearchException(org.opensearch.OpenSearchException) TestTransportChannel(org.opensearch.transport.TestTransportChannel) ReplicaResponse(org.opensearch.action.support.replication.ReplicationOperation.ReplicaResponse) Collections.singleton(java.util.Collections.singleton) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) After(org.junit.After) UnavailableShardsException(org.opensearch.action.UnavailableShardsException) CloseIndexRequest(org.opensearch.action.admin.indices.close.CloseIndexRequest) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) TransportChannel(org.opensearch.transport.TransportChannel) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IndicesService(org.opensearch.indices.IndicesService) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IndexShardClosedException(org.opensearch.index.shard.IndexShardClosedException) TransportResponse(org.opensearch.transport.TransportResponse) RestStatus(org.opensearch.rest.RestStatus) ClusterStateCreationUtils.state(org.opensearch.action.support.replication.ClusterStateCreationUtils.state) Collectors(java.util.stream.Collectors) Nullable(org.opensearch.common.Nullable) List(java.util.List) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Mockito.any(org.mockito.Mockito.any) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) BeforeClass(org.junit.BeforeClass) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) ShardNotInPrimaryModeException(org.opensearch.index.shard.ShardNotInPrimaryModeException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StreamOutput(org.opensearch.common.io.stream.StreamOutput) AtomicReference(java.util.concurrent.atomic.AtomicReference) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) HashSet(java.util.HashSet) IndexShard(org.opensearch.index.shard.IndexShard) IndexClosedException(org.opensearch.indices.IndexClosedException) StreamInput(org.opensearch.common.io.stream.StreamInput) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) TransportRequest(org.opensearch.transport.TransportRequest) AllocationId(org.opensearch.cluster.routing.AllocationId) ClusterBlockLevel(org.opensearch.cluster.block.ClusterBlockLevel) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) ClusterStateCreationUtils.stateWithActivePrimary(org.opensearch.action.support.replication.ClusterStateCreationUtils.stateWithActivePrimary) Mockito.when(org.mockito.Mockito.when) ActiveShardCount(org.opensearch.action.support.ActiveShardCount) Mockito.verify(org.mockito.Mockito.verify) ShardRouting(org.opensearch.cluster.routing.ShardRouting) ShardId(org.opensearch.index.shard.ShardId) TimeUnit(java.util.concurrent.TimeUnit) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) CapturingTransport(org.opensearch.test.transport.CapturingTransport) Collections(java.util.Collections) ClusterState(org.opensearch.cluster.ClusterState) MockTransportService(org.opensearch.test.transport.MockTransportService) IndexShard(org.opensearch.index.shard.IndexShard) CreateIndexRequest(org.opensearch.action.admin.indices.create.CreateIndexRequest) CloseIndexRequest(org.opensearch.action.admin.indices.close.CloseIndexRequest) TransportRequest(org.opensearch.transport.TransportRequest) TransportResponse(org.opensearch.transport.TransportResponse) ShardId(org.opensearch.index.shard.ShardId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ActionListener(org.opensearch.action.ActionListener) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) NetworkService(org.opensearch.common.network.NetworkService) ShardRouting(org.opensearch.cluster.routing.ShardRouting) TestShardRouting(org.opensearch.cluster.routing.TestShardRouting) Transport(org.opensearch.transport.Transport) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) CapturingTransport(org.opensearch.test.transport.CapturingTransport) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 77 with NamedWriteableRegistry

use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.

the class NodeClientHeadersTests method buildClient.

@Override
protected Client buildClient(Settings headersSettings, ActionType[] testedActions) {
    Settings settings = HEADER_SETTINGS;
    Actions actions = new Actions(settings, threadPool, testedActions);
    NodeClient client = new NodeClient(settings, threadPool);
    client.initialize(actions, () -> "test", null, new NamedWriteableRegistry(Collections.emptyList()));
    return client;
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Settings(org.opensearch.common.settings.Settings)

Example 78 with NamedWriteableRegistry

use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.

the class MetadataTests method testSerialization.

public void testSerialization() throws IOException {
    final Metadata orig = randomMetadata();
    final BytesStreamOutput out = new BytesStreamOutput();
    orig.writeTo(out);
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(ClusterModule.getNamedWriteables());
    final Metadata fromStreamMeta = Metadata.readFrom(new NamedWriteableAwareStreamInput(out.bytes().streamInput(), namedWriteableRegistry));
    assertTrue(Metadata.isGlobalStateEquals(orig, fromStreamMeta));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) CoordinationMetadata(org.opensearch.cluster.coordination.CoordinationMetadata) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 79 with NamedWriteableRegistry

use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.

the class ClusterSerializationTests method testObjectReuseWhenApplyingClusterStateDiff.

public void testObjectReuseWhenApplyingClusterStateDiff() throws Exception {
    IndexMetadata indexMetadata = IndexMetadata.builder("test").settings(settings(Version.CURRENT)).numberOfShards(10).numberOfReplicas(1).build();
    IndexTemplateMetadata indexTemplateMetadata = IndexTemplateMetadata.builder("test-template").patterns(Arrays.asList(generateRandomStringArray(10, 100, false, false))).build();
    Metadata metadata = Metadata.builder().put(indexMetadata, true).put(indexTemplateMetadata).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metadata.index("test")).build();
    ClusterState clusterState1 = ClusterState.builder(new ClusterName("clusterName1")).metadata(metadata).routingTable(routingTable).build();
    BytesStreamOutput outStream = new BytesStreamOutput();
    outStream.setVersion(Version.CURRENT);
    clusterState1.writeTo(outStream);
    StreamInput inStream = new NamedWriteableAwareStreamInput(outStream.bytes().streamInput(), new NamedWriteableRegistry(ClusterModule.getNamedWriteables()));
    ClusterState serializedClusterState1 = ClusterState.readFrom(inStream, newNode("node4"));
    // Create a new, albeit equal, IndexMetadata object
    ClusterState clusterState2 = ClusterState.builder(clusterState1).incrementVersion().metadata(Metadata.builder().put(IndexMetadata.builder(indexMetadata).numberOfReplicas(1).build(), true)).build();
    assertNotSame("Should have created a new, equivalent, IndexMetadata object in clusterState2", clusterState1.metadata().index("test"), clusterState2.metadata().index("test"));
    ClusterState serializedClusterState2 = updateUsingSerialisedDiff(serializedClusterState1, clusterState2.diff(clusterState1));
    assertSame("Unchanged metadata should not create new IndexMetadata objects", serializedClusterState1.metadata().index("test"), serializedClusterState2.metadata().index("test"));
    assertSame("Unchanged routing table should not create new IndexRoutingTable objects", serializedClusterState1.routingTable().index("test"), serializedClusterState2.routingTable().index("test"));
    // Create a new and different IndexMetadata object
    ClusterState clusterState3 = ClusterState.builder(clusterState1).incrementVersion().metadata(Metadata.builder().put(IndexMetadata.builder(indexMetadata).numberOfReplicas(2).build(), true)).build();
    ClusterState serializedClusterState3 = updateUsingSerialisedDiff(serializedClusterState2, clusterState3.diff(clusterState2));
    assertNotEquals("Should have a new IndexMetadata object", serializedClusterState2.metadata().index("test"), serializedClusterState3.metadata().index("test"));
    assertSame("Unchanged routing table should not create new IndexRoutingTable objects", serializedClusterState2.routingTable().index("test"), serializedClusterState3.routingTable().index("test"));
    assertSame("nodes", serializedClusterState2.nodes(), serializedClusterState3.nodes());
    assertSame("blocks", serializedClusterState2.blocks(), serializedClusterState3.blocks());
    assertSame("template", serializedClusterState2.metadata().templates().get("test-template"), serializedClusterState3.metadata().templates().get("test-template"));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ClusterState(org.opensearch.cluster.ClusterState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) ClusterName(org.opensearch.cluster.ClusterName) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput)

Example 80 with NamedWriteableRegistry

use of org.opensearch.common.io.stream.NamedWriteableRegistry in project OpenSearch by opensearch-project.

the class ClusterSerializationTests method testClusterStateSerialization.

public void testClusterStateSerialization() throws Exception {
    Metadata metadata = Metadata.builder().put(IndexMetadata.builder("test").settings(settings(Version.CURRENT)).numberOfShards(10).numberOfReplicas(1)).build();
    RoutingTable routingTable = RoutingTable.builder().addAsNew(metadata.index("test")).build();
    DiscoveryNodes nodes = DiscoveryNodes.builder().add(newNode("node1")).add(newNode("node2")).add(newNode("node3")).localNodeId("node1").masterNodeId("node2").build();
    ClusterState clusterState = ClusterState.builder(new ClusterName("clusterName1")).nodes(nodes).metadata(metadata).routingTable(routingTable).build();
    AllocationService strategy = createAllocationService();
    clusterState = ClusterState.builder(clusterState).routingTable(strategy.reroute(clusterState, "reroute").routingTable()).build();
    ClusterState serializedClusterState = ClusterState.Builder.fromBytes(ClusterState.Builder.toBytes(clusterState), newNode("node1"), new NamedWriteableRegistry(ClusterModule.getNamedWriteables()));
    assertThat(serializedClusterState.getClusterName().value(), equalTo(clusterState.getClusterName().value()));
    assertThat(serializedClusterState.routingTable().toString(), equalTo(clusterState.routingTable().toString()));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ClusterState(org.opensearch.cluster.ClusterState) RoutingTable(org.opensearch.cluster.routing.RoutingTable) Metadata(org.opensearch.cluster.metadata.Metadata) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata) ClusterName(org.opensearch.cluster.ClusterName) DiscoveryNodes(org.opensearch.cluster.node.DiscoveryNodes) AllocationService(org.opensearch.cluster.routing.allocation.AllocationService)

Aggregations

NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)117 ThreadPool (org.opensearch.threadpool.ThreadPool)41 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)37 ClusterService (org.opensearch.cluster.service.ClusterService)37 TestThreadPool (org.opensearch.threadpool.TestThreadPool)37 AsynchronousSearchActiveStore (org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveStore)33 InternalAsynchronousSearchStats (org.opensearch.search.asynchronous.stats.InternalAsynchronousSearchStats)32 SubmitAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest)30 TimeValue (org.opensearch.common.unit.TimeValue)29 SearchRequest (org.opensearch.action.search.SearchRequest)28 CountDownLatch (java.util.concurrent.CountDownLatch)26 User (org.opensearch.commons.authuser.User)24 AsynchronousSearchActiveContext (org.opensearch.search.asynchronous.context.active.AsynchronousSearchActiveContext)23 AsynchronousSearchTask (org.opensearch.search.asynchronous.task.AsynchronousSearchTask)22 NamedWriteableAwareStreamInput (org.opensearch.common.io.stream.NamedWriteableAwareStreamInput)21 SearchModule (org.opensearch.search.SearchModule)20 StreamInput (org.opensearch.common.io.stream.StreamInput)19 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)18 ArrayList (java.util.ArrayList)16 Version (org.opensearch.Version)16