Search in sources :

Example 1 with NetworkPlugin

use of org.opensearch.plugins.NetworkPlugin in project OpenSearch by opensearch-project.

the class NetworkModuleTests method testRegisterTransport.

public void testRegisterTransport() {
    Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "custom").build();
    // content doesn't matter we check reference equality
    Supplier<Transport> custom = () -> null;
    NetworkPlugin plugin = new NetworkPlugin() {

        @Override
        public Map<String, Supplier<Transport>> getTransports(Settings settings, ThreadPool threadPool, PageCacheRecycler pageCacheRecycler, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService) {
            return Collections.singletonMap("custom", custom);
        }
    };
    NetworkModule module = newNetworkModule(settings, plugin);
    assertSame(custom, module.getTransportSupplier());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) NetworkPlugin(org.opensearch.plugins.NetworkPlugin) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) Supplier(java.util.function.Supplier) Transport(org.opensearch.transport.Transport) HttpServerTransport(org.opensearch.http.HttpServerTransport) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Example 2 with NetworkPlugin

use of org.opensearch.plugins.NetworkPlugin in project OpenSearch by opensearch-project.

the class NetworkModuleTests method testOverrideDefault.

public void testOverrideDefault() {
    Settings settings = Settings.builder().put(NetworkModule.HTTP_TYPE_SETTING.getKey(), "custom").put(NetworkModule.HTTP_DEFAULT_TYPE_SETTING.getKey(), "default_custom").put(NetworkModule.TRANSPORT_DEFAULT_TYPE_SETTING.getKey(), "local").put(NetworkModule.TRANSPORT_TYPE_KEY, "default_custom").build();
    // content doesn't matter we check reference equality
    Supplier<Transport> customTransport = () -> null;
    Supplier<HttpServerTransport> custom = FakeHttpTransport::new;
    Supplier<HttpServerTransport> def = FakeHttpTransport::new;
    NetworkModule module = newNetworkModule(settings, new NetworkPlugin() {

        @Override
        public Map<String, Supplier<Transport>> getTransports(Settings settings, ThreadPool threadPool, PageCacheRecycler pageCacheRecycler, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService) {
            return Collections.singletonMap("default_custom", customTransport);
        }

        @Override
        public Map<String, Supplier<HttpServerTransport>> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, PageCacheRecycler pageCacheRecycler, CircuitBreakerService circuitBreakerService, NamedXContentRegistry xContentRegistry, NetworkService networkService, HttpServerTransport.Dispatcher requestDispatcher, ClusterSettings clusterSettings) {
            Map<String, Supplier<HttpServerTransport>> supplierMap = new HashMap<>();
            supplierMap.put("custom", custom);
            supplierMap.put("default_custom", def);
            return supplierMap;
        }
    });
    assertSame(custom, module.getHttpServerTransportSupplier());
    assertSame(customTransport, module.getTransportSupplier());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) NetworkPlugin(org.opensearch.plugins.NetworkPlugin) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) HttpServerTransport(org.opensearch.http.HttpServerTransport) BigArrays(org.opensearch.common.util.BigArrays) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) Transport(org.opensearch.transport.Transport) HttpServerTransport(org.opensearch.http.HttpServerTransport) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) HashMap(java.util.HashMap) Map(java.util.Map) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Example 3 with NetworkPlugin

use of org.opensearch.plugins.NetworkPlugin in project OpenSearch by opensearch-project.

the class NetworkModuleTests method testRegisterHttpTransport.

public void testRegisterHttpTransport() {
    Settings settings = Settings.builder().put(NetworkModule.HTTP_TYPE_SETTING.getKey(), "custom").put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build();
    Supplier<HttpServerTransport> custom = FakeHttpTransport::new;
    NetworkModule module = newNetworkModule(settings, new NetworkPlugin() {

        @Override
        public Map<String, Supplier<HttpServerTransport>> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, PageCacheRecycler pageCacheRecycler, CircuitBreakerService circuitBreakerService, NamedXContentRegistry xContentRegistry, NetworkService networkService, HttpServerTransport.Dispatcher requestDispatcher, ClusterSettings clusterSettings) {
            return Collections.singletonMap("custom", custom);
        }
    });
    assertSame(custom, module.getHttpServerTransportSupplier());
    settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build();
    NetworkModule newModule = newNetworkModule(settings);
    expectThrows(IllegalStateException.class, () -> newModule.getHttpServerTransportSupplier());
}
Also used : NetworkPlugin(org.opensearch.plugins.NetworkPlugin) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) HttpServerTransport(org.opensearch.http.HttpServerTransport) BigArrays(org.opensearch.common.util.BigArrays) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) HashMap(java.util.HashMap) Map(java.util.Map) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Example 4 with NetworkPlugin

use of org.opensearch.plugins.NetworkPlugin in project OpenSearch by opensearch-project.

the class NetworkModuleTests method testDefaultKeys.

public void testDefaultKeys() {
    Settings settings = Settings.builder().put(NetworkModule.HTTP_DEFAULT_TYPE_SETTING.getKey(), "default_custom").put(NetworkModule.TRANSPORT_DEFAULT_TYPE_SETTING.getKey(), "default_custom").build();
    Supplier<HttpServerTransport> custom = FakeHttpTransport::new;
    Supplier<HttpServerTransport> def = FakeHttpTransport::new;
    Supplier<Transport> customTransport = () -> null;
    NetworkModule module = newNetworkModule(settings, new NetworkPlugin() {

        @Override
        public Map<String, Supplier<Transport>> getTransports(Settings settings, ThreadPool threadPool, PageCacheRecycler pageCacheRecycler, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService) {
            return Collections.singletonMap("default_custom", customTransport);
        }

        @Override
        public Map<String, Supplier<HttpServerTransport>> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, PageCacheRecycler pageCacheRecycler, CircuitBreakerService circuitBreakerService, NamedXContentRegistry xContentRegistry, NetworkService networkService, HttpServerTransport.Dispatcher requestDispatcher, ClusterSettings clusterSettings) {
            Map<String, Supplier<HttpServerTransport>> supplierMap = new HashMap<>();
            supplierMap.put("custom", custom);
            supplierMap.put("default_custom", def);
            return supplierMap;
        }
    });
    assertSame(def, module.getHttpServerTransportSupplier());
    assertSame(customTransport, module.getTransportSupplier());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) NetworkPlugin(org.opensearch.plugins.NetworkPlugin) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) HttpServerTransport(org.opensearch.http.HttpServerTransport) BigArrays(org.opensearch.common.util.BigArrays) PageCacheRecycler(org.opensearch.common.util.PageCacheRecycler) Transport(org.opensearch.transport.Transport) HttpServerTransport(org.opensearch.http.HttpServerTransport) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) HashMap(java.util.HashMap) Map(java.util.Map) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Example 5 with NetworkPlugin

use of org.opensearch.plugins.NetworkPlugin in project OpenSearch by opensearch-project.

the class NetworkModuleTests method testRegisterInterceptor.

public void testRegisterInterceptor() {
    Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build();
    AtomicInteger called = new AtomicInteger(0);
    TransportInterceptor interceptor = new TransportInterceptor() {

        @Override
        public <T extends TransportRequest> TransportRequestHandler<T> interceptHandler(String action, String executor, boolean forceExecution, TransportRequestHandler<T> actualHandler) {
            called.incrementAndGet();
            if ("foo/bar/boom".equals(action)) {
                assertTrue(forceExecution);
            } else {
                assertFalse(forceExecution);
            }
            return actualHandler;
        }
    };
    NetworkModule module = newNetworkModule(settings, new NetworkPlugin() {

        @Override
        public List<TransportInterceptor> getTransportInterceptors(NamedWriteableRegistry namedWriteableRegistry, ThreadContext threadContext) {
            assertNotNull(threadContext);
            return Collections.singletonList(interceptor);
        }
    });
    TransportInterceptor transportInterceptor = module.getTransportInterceptor();
    assertEquals(0, called.get());
    transportInterceptor.interceptHandler("foo/bar/boom", null, true, null);
    assertEquals(1, called.get());
    transportInterceptor.interceptHandler("foo/baz/boom", null, false, null);
    assertEquals(2, called.get());
    assertTrue(transportInterceptor instanceof NetworkModule.CompositeTransportInterceptor);
    assertEquals(((NetworkModule.CompositeTransportInterceptor) transportInterceptor).transportInterceptors.size(), 1);
    assertSame(((NetworkModule.CompositeTransportInterceptor) transportInterceptor).transportInterceptors.get(0), interceptor);
    NullPointerException nullPointerException = expectThrows(NullPointerException.class, () -> {
        newNetworkModule(settings, new NetworkPlugin() {

            @Override
            public List<TransportInterceptor> getTransportInterceptors(NamedWriteableRegistry namedWriteableRegistry, ThreadContext threadContext) {
                assertNotNull(threadContext);
                return Collections.singletonList(null);
            }
        });
    });
    assertEquals("interceptor must not be null", nullPointerException.getMessage());
}
Also used : NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) NetworkPlugin(org.opensearch.plugins.NetworkPlugin) TransportInterceptor(org.opensearch.transport.TransportInterceptor) TransportRequest(org.opensearch.transport.TransportRequest) TransportRequestHandler(org.opensearch.transport.TransportRequestHandler) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Aggregations

ClusterSettings (org.opensearch.common.settings.ClusterSettings)5 Settings (org.opensearch.common.settings.Settings)5 NetworkPlugin (org.opensearch.plugins.NetworkPlugin)5 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)4 PageCacheRecycler (org.opensearch.common.util.PageCacheRecycler)4 HttpServerTransport (org.opensearch.http.HttpServerTransport)4 CircuitBreakerService (org.opensearch.indices.breaker.CircuitBreakerService)4 TestThreadPool (org.opensearch.threadpool.TestThreadPool)4 ThreadPool (org.opensearch.threadpool.ThreadPool)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BigArrays (org.opensearch.common.util.BigArrays)3 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)3 Transport (org.opensearch.transport.Transport)3 List (java.util.List)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Supplier (java.util.function.Supplier)1 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)1 TransportInterceptor (org.opensearch.transport.TransportInterceptor)1 TransportRequest (org.opensearch.transport.TransportRequest)1