Search in sources :

Example 16 with NetworkService

use of org.opensearch.common.network.NetworkService in project OpenSearch by opensearch-project.

the class Ec2NetworkTests method resolveEc2.

private InetAddress[] resolveEc2(String host, InetAddress... expected) throws IOException {
    Settings nodeSettings = Settings.builder().put("network.host", host).build();
    NetworkService networkService = new NetworkService(Collections.singletonList(new Ec2NameResolver()));
    InetAddress[] addresses = networkService.resolveBindHostAddresses(NetworkService.GLOBAL_NETWORK_BIND_HOST_SETTING.get(nodeSettings).toArray(Strings.EMPTY_ARRAY));
    if (expected == null) {
        fail("We should get an IOException, resolved addressed:" + Arrays.toString(addresses));
    }
    assertThat(addresses, arrayContaining(expected));
    return addresses;
}
Also used : NetworkService(org.opensearch.common.network.NetworkService) InetAddress(java.net.InetAddress) Settings(org.opensearch.common.settings.Settings)

Example 17 with NetworkService

use of org.opensearch.common.network.NetworkService in project OpenSearch by opensearch-project.

the class GceNetworkTests method resolveGce.

/**
 * Utility test method to test different settings
 * @param gceNetworkSetting tested network.host property
 * @param expected expected InetAddress, null if we expect an exception
 * @throws IOException Well... If something goes wrong :)
 */
private void resolveGce(String gceNetworkSetting, InetAddress[] expected) throws IOException {
    Settings nodeSettings = Settings.builder().put("network.host", gceNetworkSetting).build();
    GceMetadataServiceMock mock = new GceMetadataServiceMock(nodeSettings);
    NetworkService networkService = new NetworkService(Collections.singletonList(new GceNameResolver(mock)));
    try {
        InetAddress[] addresses = networkService.resolveBindHostAddresses(NetworkService.GLOBAL_NETWORK_BIND_HOST_SETTING.get(nodeSettings).toArray(Strings.EMPTY_ARRAY));
        if (expected == null) {
            fail("We should get a IllegalArgumentException when setting network.host: _gce:doesnotexist_");
        }
        assertThat(addresses, arrayContaining(expected));
    } catch (IllegalArgumentException e) {
        if (expected != null) {
            // We were expecting something and not an exception
            throw e;
        }
        // We check that we get the expected exception
        assertThat(e.getMessage(), containsString("is not one of the supported GCE network.host setting"));
    }
}
Also used : NetworkService(org.opensearch.common.network.NetworkService) GceNameResolver(org.opensearch.cloud.gce.network.GceNameResolver) InetAddress(java.net.InetAddress) Settings(org.opensearch.common.settings.Settings)

Example 18 with NetworkService

use of org.opensearch.common.network.NetworkService in project OpenSearch by opensearch-project.

the class NioHttpServerTransportTests method setup.

@Before
public void setup() throws Exception {
    networkService = new NetworkService(Collections.emptyList());
    threadPool = new TestThreadPool("test");
    pageRecycler = new MockPageCacheRecycler(Settings.EMPTY);
    bigArrays = new MockBigArrays(pageRecycler, new NoneCircuitBreakerService());
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) NetworkService(org.opensearch.common.network.NetworkService) MockBigArrays(org.opensearch.common.util.MockBigArrays) TestThreadPool(org.opensearch.threadpool.TestThreadPool) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) Before(org.junit.Before)

Example 19 with NetworkService

use of org.opensearch.common.network.NetworkService in project OpenSearch by opensearch-project.

the class TransportResyncReplicationActionTests method testResyncDoesNotBlockOnPrimaryAction.

public void testResyncDoesNotBlockOnPrimaryAction() throws Exception {
    try (ClusterService clusterService = createClusterService(threadPool)) {
        final String indexName = randomAlphaOfLength(5);
        setState(clusterService, state(indexName, true, ShardRoutingState.STARTED));
        setState(clusterService, ClusterState.builder(clusterService.state()).blocks(ClusterBlocks.builder().addGlobalBlock(NoMasterBlockService.NO_MASTER_BLOCK_ALL).addIndexBlock(indexName, IndexMetadata.INDEX_WRITE_BLOCK)));
        try (MockNioTransport transport = new MockNioTransport(Settings.EMPTY, Version.CURRENT, threadPool, new NetworkService(emptyList()), PageCacheRecycler.NON_RECYCLING_INSTANCE, new NamedWriteableRegistry(emptyList()), new NoneCircuitBreakerService())) {
            final MockTransportService transportService = new MockTransportService(Settings.EMPTY, transport, threadPool, NOOP_TRANSPORT_INTERCEPTOR, x -> clusterService.localNode(), null, Collections.emptySet());
            transportService.start();
            transportService.acceptIncomingRequests();
            final ShardStateAction shardStateAction = new ShardStateAction(clusterService, transportService, null, null, threadPool);
            final IndexMetadata indexMetadata = clusterService.state().metadata().index(indexName);
            final Index index = indexMetadata.getIndex();
            final ShardId shardId = new ShardId(index, 0);
            final IndexShardRoutingTable shardRoutingTable = clusterService.state().routingTable().shardRoutingTable(shardId);
            final ShardRouting primaryShardRouting = clusterService.state().routingTable().shardRoutingTable(shardId).primaryShard();
            final String allocationId = primaryShardRouting.allocationId().getId();
            final long primaryTerm = indexMetadata.primaryTerm(shardId.id());
            final AtomicInteger acquiredPermits = new AtomicInteger();
            final IndexShard indexShard = mock(IndexShard.class);
            when(indexShard.indexSettings()).thenReturn(new IndexSettings(indexMetadata, Settings.EMPTY));
            when(indexShard.shardId()).thenReturn(shardId);
            when(indexShard.routingEntry()).thenReturn(primaryShardRouting);
            when(indexShard.getPendingPrimaryTerm()).thenReturn(primaryTerm);
            when(indexShard.getOperationPrimaryTerm()).thenReturn(primaryTerm);
            when(indexShard.getActiveOperationsCount()).then(i -> acquiredPermits.get());
            doAnswer(invocation -> {
                ActionListener<Releasable> callback = (ActionListener<Releasable>) invocation.getArguments()[0];
                acquiredPermits.incrementAndGet();
                callback.onResponse(acquiredPermits::decrementAndGet);
                return null;
            }).when(indexShard).acquirePrimaryOperationPermit(any(ActionListener.class), anyString(), any(), eq(true));
            when(indexShard.getReplicationGroup()).thenReturn(new ReplicationGroup(shardRoutingTable, clusterService.state().metadata().index(index).inSyncAllocationIds(shardId.id()), shardRoutingTable.getAllAllocationIds(), 0));
            final IndexService indexService = mock(IndexService.class);
            when(indexService.getShard(eq(shardId.id()))).thenReturn(indexShard);
            final IndicesService indexServices = mock(IndicesService.class);
            when(indexServices.indexServiceSafe(eq(index))).thenReturn(indexService);
            final TransportResyncReplicationAction action = new TransportResyncReplicationAction(Settings.EMPTY, transportService, clusterService, indexServices, threadPool, shardStateAction, new ActionFilters(new HashSet<>()), new IndexingPressureService(Settings.EMPTY, clusterService), new SystemIndices(emptyMap()));
            assertThat(action.globalBlockLevel(), nullValue());
            assertThat(action.indexBlockLevel(), nullValue());
            final Task task = mock(Task.class);
            when(task.getId()).thenReturn(randomNonNegativeLong());
            final byte[] bytes = "{}".getBytes(Charset.forName("UTF-8"));
            final ResyncReplicationRequest request = new ResyncReplicationRequest(shardId, 42L, 100, new Translog.Operation[] { new Translog.Index("type", "id", 0, primaryTerm, 0L, bytes, null, -1) });
            final PlainActionFuture<ResyncReplicationResponse> listener = new PlainActionFuture<>();
            action.sync(request, task, allocationId, primaryTerm, listener);
            assertThat(listener.get().getShardInfo().getFailed(), equalTo(0));
            assertThat(listener.isDone(), is(true));
        }
    }
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) IndexShardRoutingTable(org.opensearch.cluster.routing.IndexShardRoutingTable) Task(org.opensearch.tasks.Task) MockTransportService(org.opensearch.test.transport.MockTransportService) IndexService(org.opensearch.index.IndexService) IndexSettings(org.opensearch.index.IndexSettings) Index(org.opensearch.index.Index) Mockito.anyString(org.mockito.Mockito.anyString) ShardStateAction(org.opensearch.cluster.action.shard.ShardStateAction) ReplicationGroup(org.opensearch.index.shard.ReplicationGroup) Translog(org.opensearch.index.translog.Translog) ShardId(org.opensearch.index.shard.ShardId) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) HashSet(java.util.HashSet) IndexingPressureService(org.opensearch.index.IndexingPressureService) IndexShard(org.opensearch.index.shard.IndexShard) IndicesService(org.opensearch.indices.IndicesService) ActionFilters(org.opensearch.action.support.ActionFilters) ClusterServiceUtils.createClusterService(org.opensearch.test.ClusterServiceUtils.createClusterService) ClusterService(org.opensearch.cluster.service.ClusterService) ActionListener(org.opensearch.action.ActionListener) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) MockNioTransport(org.opensearch.transport.nio.MockNioTransport) NetworkService(org.opensearch.common.network.NetworkService) Releasable(org.opensearch.common.lease.Releasable) ShardRouting(org.opensearch.cluster.routing.ShardRouting) SystemIndices(org.opensearch.indices.SystemIndices) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 20 with NetworkService

use of org.opensearch.common.network.NetworkService 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)

Aggregations

NetworkService (org.opensearch.common.network.NetworkService)25 NoneCircuitBreakerService (org.opensearch.indices.breaker.NoneCircuitBreakerService)21 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)15 MockPageCacheRecycler (org.opensearch.common.util.MockPageCacheRecycler)11 MockNioTransport (org.opensearch.transport.nio.MockNioTransport)11 Before (org.junit.Before)9 TestThreadPool (org.opensearch.threadpool.TestThreadPool)9 TransportAddress (org.opensearch.common.transport.TransportAddress)8 TransportService (org.opensearch.transport.TransportService)7 BoundTransportAddress (org.opensearch.common.transport.BoundTransportAddress)6 Transport (org.opensearch.transport.Transport)6 ActionListener (org.opensearch.action.ActionListener)5 PlainActionFuture (org.opensearch.action.support.PlainActionFuture)5 Settings (org.opensearch.common.settings.Settings)5 InetAddress (java.net.InetAddress)4 UnknownHostException (java.net.UnknownHostException)4 HashSet (java.util.HashSet)4 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)4 MockBigArrays (org.opensearch.common.util.MockBigArrays)4 PageCacheRecycler (org.opensearch.common.util.PageCacheRecycler)4