Search in sources :

Example 1 with MockNioTransport

use of org.opensearch.transport.nio.MockNioTransport 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 2 with MockNioTransport

use of org.opensearch.transport.nio.MockNioTransport 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)

Example 3 with MockNioTransport

use of org.opensearch.transport.nio.MockNioTransport in project OpenSearch by opensearch-project.

the class SeedHostsResolverTests method testCancellationOnClose.

public void testCancellationOnClose() throws InterruptedException {
    final NetworkService networkService = new NetworkService(Collections.emptyList());
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch conditionLatch = new CountDownLatch(1);
    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(), 9500) }, new TransportAddress(InetAddress.getLoopbackAddress(), 9500));
        }

        @Override
        public TransportAddress[] addressesFromString(String address) throws UnknownHostException {
            if ("hostname1".equals(address)) {
                return new TransportAddress[] { new TransportAddress(TransportAddress.META_ADDRESS, 9300) };
            } else if ("hostname2".equals(address)) {
                try {
                    conditionLatch.countDown();
                    latch.await();
                    throw new AssertionError("should never be called");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            } else {
                throw new UnknownHostException(address);
            }
        }
    };
    closeables.push(transport);
    final TransportService transportService = new TransportService(Settings.EMPTY, transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> null, null, Collections.emptySet());
    closeables.push(transportService);
    recreateSeedHostsResolver(transportService, Settings.builder().put(SeedHostsResolver.DISCOVERY_SEED_RESOLVER_TIMEOUT_SETTING.getKey(), "10m").build());
    final PlainActionFuture<List<TransportAddress>> fut = new PlainActionFuture<>();
    threadPool.generic().execute((() -> fut.onResponse(seedHostsResolver.resolveHosts(Arrays.asList("hostname1", "hostname2")))));
    conditionLatch.await();
    seedHostsResolver.stop();
    assertThat(FutureUtils.get(fut, 10, TimeUnit.SECONDS), hasSize(0));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) UnknownHostException(java.net.UnknownHostException) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) CountDownLatch(java.util.concurrent.CountDownLatch) TransportService(org.opensearch.transport.TransportService) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) NetworkService(org.opensearch.common.network.NetworkService) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) List(java.util.List) ArrayList(java.util.ArrayList) Transport(org.opensearch.transport.Transport) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 4 with MockNioTransport

use of org.opensearch.transport.nio.MockNioTransport in project OpenSearch by opensearch-project.

the class SeedHostsResolverTests method testRemovingLocalAddresses.

public void testRemovingLocalAddresses() {
    final NetworkService networkService = new NetworkService(Collections.emptyList());
    final InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
    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(loopbackAddress, 9300), new TransportAddress(loopbackAddress, 9301) }, new TransportAddress(loopbackAddress, 9302));
        }
    };
    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, IntStream.range(9300, 9310).mapToObj(port -> NetworkAddress.format(loopbackAddress) + ":" + port).collect(Collectors.toList()), transportService, TimeValue.timeValueSeconds(30));
    assertThat(transportAddresses, hasSize(7));
    final Set<Integer> ports = new HashSet<>();
    for (final TransportAddress address : transportAddresses) {
        assertTrue(address.address().getAddress().isLoopbackAddress());
        ports.add(address.getPort());
    }
    assertThat(ports, equalTo(IntStream.range(9303, 9310).mapToObj(m -> m).collect(Collectors.toSet())));
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Arrays(java.util.Arrays) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Version(org.opensearch.Version) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) FutureUtils(org.opensearch.common.util.concurrent.FutureUtils) Transport(org.opensearch.transport.Transport) InetAddress(java.net.InetAddress) IsNull.nullValue(org.hamcrest.core.IsNull.nullValue) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) After(org.junit.After) ThreadFactory(java.util.concurrent.ThreadFactory) TimeValue(org.opensearch.common.unit.TimeValue) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) TransportService(org.opensearch.transport.TransportService) Collectors(java.util.stream.Collectors) TransportAddress(org.opensearch.common.transport.TransportAddress) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Mockito.eq(org.mockito.Mockito.eq) Mockito.mock(org.mockito.Mockito.mock) CancellableThreads(org.opensearch.common.util.CancellableThreads) IntStream(java.util.stream.IntStream) ThreadPool(org.opensearch.threadpool.ThreadPool) OpenSearchExecutors(org.opensearch.common.util.concurrent.OpenSearchExecutors) AtomicReference(java.util.concurrent.atomic.AtomicReference) Stack(java.util.Stack) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) NetworkAddress(org.opensearch.common.network.NetworkAddress) Matchers.empty(org.hamcrest.Matchers.empty) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) UnknownHostException(java.net.UnknownHostException) Mockito.verify(org.mockito.Mockito.verify) IOUtils(org.opensearch.core.internal.io.IOUtils) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Closeable(java.io.Closeable) NetworkService(org.opensearch.common.network.NetworkService) Collections(java.util.Collections) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) CancellableThreads(org.opensearch.common.util.CancellableThreads) BoundTransportAddress(org.opensearch.common.transport.BoundTransportAddress) TransportAddress(org.opensearch.common.transport.TransportAddress) 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) InetAddress(java.net.InetAddress) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) HashSet(java.util.HashSet)

Example 5 with MockNioTransport

use of org.opensearch.transport.nio.MockNioTransport in project OpenSearch by opensearch-project.

the class BroadcastReplicationTests method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    MockNioTransport transport = new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, new NetworkService(Collections.emptyList()), PageCacheRecycler.NON_RECYCLING_INSTANCE, new NamedWriteableRegistry(Collections.emptyList()), circuitBreakerService);
    clusterService = createClusterService(threadPool);
    transportService = new TransportService(clusterService.getSettings(), transport, threadPool, TransportService.NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null, Collections.emptySet());
    transportService.start();
    transportService.acceptIncomingRequests();
    broadcastReplicationAction = new TestBroadcastReplicationAction(clusterService, transportService, new ActionFilters(new HashSet<>()), new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)), null);
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) Date(java.util.Date) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Version(org.opensearch.Version) ClusterServiceUtils.setState(org.opensearch.test.ClusterServiceUtils.setState) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) ConcurrentCollections(org.opensearch.common.util.concurrent.ConcurrentCollections) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) After(org.junit.After) ActionListener(org.opensearch.action.ActionListener) UnavailableShardsException(org.opensearch.action.UnavailableShardsException) AfterClass(org.junit.AfterClass) Matchers.lessThanOrEqualTo(org.hamcrest.Matchers.lessThanOrEqualTo) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) Set(java.util.Set) Settings(org.opensearch.common.settings.Settings) Task(org.opensearch.tasks.Task) RestStatus(org.opensearch.rest.RestStatus) ClusterStateCreationUtils.state(org.opensearch.action.support.replication.ClusterStateCreationUtils.state) TransportService(org.opensearch.transport.TransportService) Tuple(org.opensearch.common.collect.Tuple) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) ActionFilters(org.opensearch.action.support.ActionFilters) List(java.util.List) ActionTestUtils(org.opensearch.action.support.ActionTestUtils) Matchers.equalTo(org.hamcrest.Matchers.equalTo) BroadcastResponse(org.opensearch.action.support.broadcast.BroadcastResponse) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) FlushResponse(org.opensearch.action.admin.indices.flush.FlushResponse) BroadcastRequest(org.opensearch.action.support.broadcast.BroadcastRequest) BeforeClass(org.junit.BeforeClass) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) ThreadPool(org.opensearch.threadpool.ThreadPool) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) HashSet(java.util.HashSet) ClusterState(org.opensearch.cluster.ClusterState) ShardRoutingState(org.opensearch.cluster.routing.ShardRoutingState) Before(org.junit.Before) StreamInput(org.opensearch.common.io.stream.StreamInput) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) ClusterStateCreationUtils.stateWithAssignedPrimariesAndOneReplica(org.opensearch.action.support.replication.ClusterStateCreationUtils.stateWithAssignedPrimariesAndOneReplica) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) TransportFlushAction(org.opensearch.action.admin.indices.flush.TransportFlushAction) IOException(java.io.IOException) IOUtils(org.opensearch.core.internal.io.IOUtils) ShardId(org.opensearch.index.shard.ShardId) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ClusterStateCreationUtils.stateWithNoShard(org.opensearch.action.support.replication.ClusterStateCreationUtils.stateWithNoShard) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) ClusterService(org.opensearch.cluster.service.ClusterService) NetworkService(org.opensearch.common.network.NetworkService) NoShardAvailableActionException(org.opensearch.action.NoShardAvailableActionException) Collections(java.util.Collections) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) TransportService(org.opensearch.transport.TransportService) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) NetworkService(org.opensearch.common.network.NetworkService) ActionFilters(org.opensearch.action.support.ActionFilters) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) Before(org.junit.Before)

Aggregations

NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)11 NetworkService (org.opensearch.common.network.NetworkService)11 NoneCircuitBreakerService (org.opensearch.indices.breaker.NoneCircuitBreakerService)11 MockNioTransport (org.opensearch.transport.nio.MockNioTransport)11 TransportService (org.opensearch.transport.TransportService)7 BoundTransportAddress (org.opensearch.common.transport.BoundTransportAddress)6 TransportAddress (org.opensearch.common.transport.TransportAddress)6 Transport (org.opensearch.transport.Transport)6 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)5 UnknownHostException (java.net.UnknownHostException)4 HashSet (java.util.HashSet)4 List (java.util.List)4 MockTransportService (org.opensearch.test.transport.MockTransportService)4 IOException (java.io.IOException)3 Collections (java.util.Collections)3 Set (java.util.Set)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 TimeUnit (java.util.concurrent.TimeUnit)3 Logger (org.apache.logging.log4j.Logger)3 CancellableThreads (org.opensearch.common.util.CancellableThreads)3