Search in sources :

Example 26 with NamedWriteableRegistry

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

the class CoordinationMetadataTests method testVotingTombstoneSerializationEqualsHashCode.

public void testVotingTombstoneSerializationEqualsHashCode() {
    VotingConfigExclusion tombstone = new VotingConfigExclusion(randomAlphaOfLength(10), randomAlphaOfLength(10));
    // Note: the explicit cast of the CopyFunction is needed for some IDE (specifically Eclipse 4.8.0) to infer the right type
    EqualsHashCodeTestUtils.checkEqualsAndHashCode(tombstone, (CopyFunction<VotingConfigExclusion>) orig -> OpenSearchTestCase.copyWriteable(orig, new NamedWriteableRegistry(Collections.emptyList()), VotingConfigExclusion::new), orig -> randomlyChangeVotingTombstone(orig));
}
Also used : VotingConfigExclusion(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) VotingConfigExclusion(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) VotingConfiguration(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) BytesReference(org.opensearch.common.bytes.BytesReference) ToXContent(org.opensearch.common.xcontent.ToXContent) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) IOException(java.io.IOException) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) HashSet(java.util.HashSet) Sets(org.opensearch.common.util.set.Sets) Matchers.equalTo(org.hamcrest.Matchers.equalTo) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) EqualsHashCodeTestUtils(org.opensearch.test.EqualsHashCodeTestUtils) CopyFunction(org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction) Collections(java.util.Collections) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry)

Example 27 with NamedWriteableRegistry

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

the class CoordinationMetadataTests method testCoordinationMetadataSerializationEqualsHashCode.

public void testCoordinationMetadataSerializationEqualsHashCode() {
    CoordinationMetadata initialMetadata = new CoordinationMetadata(randomNonNegativeLong(), randomVotingConfig(), randomVotingConfig(), randomVotingTombstones());
    // Note: the explicit cast of the CopyFunction is needed for some IDE (specifically Eclipse 4.8.0) to infer the right type
    EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialMetadata, (CopyFunction<CoordinationMetadata>) orig -> OpenSearchTestCase.copyWriteable(orig, new NamedWriteableRegistry(Collections.emptyList()), CoordinationMetadata::new), meta -> {
        CoordinationMetadata.Builder builder = CoordinationMetadata.builder(meta);
        switch(randomInt(3)) {
            case 0:
                builder.term(randomValueOtherThan(meta.term(), OpenSearchTestCase::randomNonNegativeLong));
                break;
            case 1:
                builder.lastCommittedConfiguration(randomlyChangeVotingConfiguration(meta.getLastCommittedConfiguration()));
                break;
            case 2:
                builder.lastAcceptedConfiguration(randomlyChangeVotingConfiguration(meta.getLastAcceptedConfiguration()));
                break;
            case 3:
                if (meta.getVotingConfigExclusions().isEmpty() == false && randomBoolean()) {
                    builder.clearVotingConfigExclusions();
                } else {
                    randomVotingTombstones().forEach(dn -> builder.addVotingConfigExclusion(dn));
                }
                break;
        }
        return builder.build();
    });
}
Also used : VotingConfigExclusion(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfigExclusion) VotingConfiguration(org.opensearch.cluster.coordination.CoordinationMetadata.VotingConfiguration) BytesReference(org.opensearch.common.bytes.BytesReference) ToXContent(org.opensearch.common.xcontent.ToXContent) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) IOException(java.io.IOException) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) HashSet(java.util.HashSet) Sets(org.opensearch.common.util.set.Sets) Matchers.equalTo(org.hamcrest.Matchers.equalTo) JsonXContent(org.opensearch.common.xcontent.json.JsonXContent) EqualsHashCodeTestUtils(org.opensearch.test.EqualsHashCodeTestUtils) CopyFunction(org.opensearch.test.EqualsHashCodeTestUtils.CopyFunction) Collections(java.util.Collections) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry)

Example 28 with NamedWriteableRegistry

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

the class AllocationCommandsTests method testSerialization.

public void testSerialization() throws Exception {
    AllocationCommands commands = new AllocationCommands(new AllocateEmptyPrimaryAllocationCommand("test", 1, "node1", true), new AllocateStalePrimaryAllocationCommand("test", 2, "node1", true), new AllocateReplicaAllocationCommand("test", 2, "node1"), new MoveAllocationCommand("test", 3, "node2", "node3"), new CancelAllocationCommand("test", 4, "node5", true));
    BytesStreamOutput bytes = new BytesStreamOutput();
    AllocationCommands.writeTo(commands, bytes);
    StreamInput in = bytes.bytes().streamInput();
    // Since the commands are named writeable we need to register them and wrap the input stream
    NamedWriteableRegistry namedWriteableRegistry = new NamedWriteableRegistry(NetworkModule.getNamedWriteables());
    in = new NamedWriteableAwareStreamInput(in, namedWriteableRegistry);
    // Now we can read them!
    AllocationCommands sCommands = AllocationCommands.readFrom(in);
    assertThat(sCommands.commands().size(), equalTo(5));
    assertThat(((AllocateEmptyPrimaryAllocationCommand) (sCommands.commands().get(0))).shardId(), equalTo(1));
    assertThat(((AllocateEmptyPrimaryAllocationCommand) (sCommands.commands().get(0))).index(), equalTo("test"));
    assertThat(((AllocateEmptyPrimaryAllocationCommand) (sCommands.commands().get(0))).node(), equalTo("node1"));
    assertThat(((AllocateEmptyPrimaryAllocationCommand) (sCommands.commands().get(0))).acceptDataLoss(), equalTo(true));
    assertThat(((AllocateStalePrimaryAllocationCommand) (sCommands.commands().get(1))).shardId(), equalTo(2));
    assertThat(((AllocateStalePrimaryAllocationCommand) (sCommands.commands().get(1))).index(), equalTo("test"));
    assertThat(((AllocateStalePrimaryAllocationCommand) (sCommands.commands().get(1))).node(), equalTo("node1"));
    assertThat(((AllocateStalePrimaryAllocationCommand) (sCommands.commands().get(1))).acceptDataLoss(), equalTo(true));
    assertThat(((AllocateReplicaAllocationCommand) (sCommands.commands().get(2))).shardId(), equalTo(2));
    assertThat(((AllocateReplicaAllocationCommand) (sCommands.commands().get(2))).index(), equalTo("test"));
    assertThat(((AllocateReplicaAllocationCommand) (sCommands.commands().get(2))).node(), equalTo("node1"));
    assertThat(((MoveAllocationCommand) (sCommands.commands().get(3))).shardId(), equalTo(3));
    assertThat(((MoveAllocationCommand) (sCommands.commands().get(3))).index(), equalTo("test"));
    assertThat(((MoveAllocationCommand) (sCommands.commands().get(3))).fromNode(), equalTo("node2"));
    assertThat(((MoveAllocationCommand) (sCommands.commands().get(3))).toNode(), equalTo("node3"));
    assertThat(((CancelAllocationCommand) (sCommands.commands().get(4))).shardId(), equalTo(4));
    assertThat(((CancelAllocationCommand) (sCommands.commands().get(4))).index(), equalTo("test"));
    assertThat(((CancelAllocationCommand) (sCommands.commands().get(4))).node(), equalTo("node5"));
    assertThat(((CancelAllocationCommand) (sCommands.commands().get(4))).allowPrimary(), equalTo(true));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) AllocateStalePrimaryAllocationCommand(org.opensearch.cluster.routing.allocation.command.AllocateStalePrimaryAllocationCommand) CancelAllocationCommand(org.opensearch.cluster.routing.allocation.command.CancelAllocationCommand) AllocateEmptyPrimaryAllocationCommand(org.opensearch.cluster.routing.allocation.command.AllocateEmptyPrimaryAllocationCommand) MoveAllocationCommand(org.opensearch.cluster.routing.allocation.command.MoveAllocationCommand) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.opensearch.common.io.stream.StreamInput) AllocateReplicaAllocationCommand(org.opensearch.cluster.routing.allocation.command.AllocateReplicaAllocationCommand) NamedWriteableAwareStreamInput(org.opensearch.common.io.stream.NamedWriteableAwareStreamInput) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) AllocationCommands(org.opensearch.cluster.routing.allocation.command.AllocationCommands)

Example 29 with NamedWriteableRegistry

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

the class SeedHostsResolverTests method testUnknownHost.

public void testUnknownHost() {
    final Logger logger = mock(Logger.class);
    final NetworkService networkService = new NetworkService(Collections.emptyList());
    final String hostname = randomAlphaOfLength(8);
    final UnknownHostException unknownHostException = new UnknownHostException(hostname);
    final Transport transport = new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, networkService, PageCacheRecycler.NON_RECYCLING_INSTANCE, new NamedWriteableRegistry(Collections.emptyList()), new NoneCircuitBreakerService()) {

        @Override
        public BoundTransportAddress boundAddress() {
            return new BoundTransportAddress(new TransportAddress[] { new TransportAddress(InetAddress.getLoopbackAddress(), 9300) }, new TransportAddress(InetAddress.getLoopbackAddress(), 9300));
        }

        @Override
        public TransportAddress[] addressesFromString(String address) throws UnknownHostException {
            throw unknownHostException;
        }
    };
    closeables.push(transport);
    final TransportService transportService = new TransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    closeables.push(transportService);
    final List<TransportAddress> transportAddresses = SeedHostsResolver.resolveHostsLists(new CancellableThreads(), executorService, logger, Arrays.asList(hostname), transportService, TimeValue.timeValueSeconds(30));
    assertThat(transportAddresses, empty());
    verify(logger).warn("failed to resolve host [" + hostname + "]", unknownHostException);
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) CancellableThreads(org.opensearch.common.util.CancellableThreads) UnknownHostException(java.net.UnknownHostException) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) Logger(org.apache.logging.log4j.Logger) TransportService(org.opensearch.transport.TransportService) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) NetworkService(org.opensearch.common.network.NetworkService) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) Transport(org.opensearch.transport.Transport) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 30 with NamedWriteableRegistry

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

the class SeedHostsResolverTests method testInvalidHosts.

public void testInvalidHosts() throws IllegalAccessException {
    final Logger logger = mock(Logger.class);
    final Transport transport = new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, new NetworkService(Collections.emptyList()), PageCacheRecycler.NON_RECYCLING_INSTANCE, new NamedWriteableRegistry(Collections.emptyList()), new NoneCircuitBreakerService()) {

        @Override
        public BoundTransportAddress boundAddress() {
            return new BoundTransportAddress(new TransportAddress[] { new TransportAddress(InetAddress.getLoopbackAddress(), 9300) }, new TransportAddress(InetAddress.getLoopbackAddress(), 9300));
        }
    };
    closeables.push(transport);
    final TransportService transportService = new TransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    closeables.push(transportService);
    final List<TransportAddress> transportAddresses = SeedHostsResolver.resolveHostsLists(new CancellableThreads(), executorService, logger, Arrays.asList("127.0.0.1:9300:9300", "127.0.0.1:9301"), transportService, TimeValue.timeValueSeconds(30));
    // only one of the two is valid and will be used
    assertThat(transportAddresses, hasSize(1));
    assertThat(transportAddresses.get(0).getAddress(), equalTo("127.0.0.1"));
    assertThat(transportAddresses.get(0).getPort(), equalTo(9301));
    verify(logger).warn(eq("failed to resolve host [127.0.0.1:9300:9300]"), Mockito.any(IllegalArgumentException.class));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) CancellableThreads(org.opensearch.common.util.CancellableThreads) TransportService(org.opensearch.transport.TransportService) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) NetworkService(org.opensearch.common.network.NetworkService) Logger(org.apache.logging.log4j.Logger) Transport(org.opensearch.transport.Transport) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

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