Search in sources :

Example 6 with HttpServerTransport

use of org.opensearch.http.HttpServerTransport 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 7 with HttpServerTransport

use of org.opensearch.http.HttpServerTransport in project OpenSearch by opensearch-project.

the class RestControllerTests method setup.

@Before
public void setup() {
    circuitBreakerService = new HierarchyCircuitBreakerService(Settings.builder().put(HierarchyCircuitBreakerService.IN_FLIGHT_REQUESTS_CIRCUIT_BREAKER_LIMIT_SETTING.getKey(), BREAKER_LIMIT).put(HierarchyCircuitBreakerService.USE_REAL_MEMORY_USAGE_SETTING.getKey(), false).build(), Collections.emptyList(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
    usageService = new UsageService();
    // we can do this here only because we know that we don't adjust breaker settings dynamically in the test
    inFlightRequestsBreaker = circuitBreakerService.getBreaker(CircuitBreaker.IN_FLIGHT_REQUESTS);
    HttpServerTransport httpServerTransport = new TestHttpServerTransport();
    client = new NoOpNodeClient(this.getTestName());
    restController = new RestController(Collections.emptySet(), null, client, circuitBreakerService, usageService);
    restController.registerHandler(RestRequest.Method.GET, "/", (request, channel, client) -> channel.sendResponse(new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY)));
    restController.registerHandler(RestRequest.Method.GET, "/error", (request, channel, client) -> {
        throw new IllegalArgumentException("test error");
    });
    httpServerTransport.start();
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) UsageService(org.opensearch.usage.UsageService) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService) HttpServerTransport(org.opensearch.http.HttpServerTransport) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) Before(org.junit.Before)

Example 8 with HttpServerTransport

use of org.opensearch.http.HttpServerTransport in project OpenSearch by opensearch-project.

the class RestControllerTests method testRestHandlerWrapper.

public void testRestHandlerWrapper() throws Exception {
    AtomicBoolean handlerCalled = new AtomicBoolean(false);
    AtomicBoolean wrapperCalled = new AtomicBoolean(false);
    final RestHandler handler = (RestRequest request, RestChannel channel, NodeClient client) -> handlerCalled.set(true);
    final HttpServerTransport httpServerTransport = new TestHttpServerTransport();
    final RestController restController = new RestController(Collections.emptySet(), h -> {
        assertSame(handler, h);
        return (RestRequest request, RestChannel channel, NodeClient client) -> wrapperCalled.set(true);
    }, client, circuitBreakerService, usageService);
    restController.registerHandler(RestRequest.Method.GET, "/wrapped", handler);
    RestRequest request = testRestRequest("/wrapped", "{}", XContentType.JSON);
    AssertingChannel channel = new AssertingChannel(request, true, RestStatus.BAD_REQUEST);
    restController.dispatchRequest(request, channel, client.threadPool().getThreadContext());
    httpServerTransport.start();
    assertTrue(wrapperCalled.get());
    assertFalse(handlerCalled.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NodeClient(org.opensearch.client.node.NodeClient) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) HttpServerTransport(org.opensearch.http.HttpServerTransport)

Example 9 with HttpServerTransport

use of org.opensearch.http.HttpServerTransport 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 10 with HttpServerTransport

use of org.opensearch.http.HttpServerTransport in project OpenSearch by opensearch-project.

the class Netty4HttpServerPipeliningTests method testThatHttpPipeliningWorks.

public void testThatHttpPipeliningWorks() throws Exception {
    final Settings settings = Settings.builder().put("http.port", "0").build();
    try (HttpServerTransport httpServerTransport = new CustomNettyHttpServerTransport(settings)) {
        httpServerTransport.start();
        final TransportAddress transportAddress = randomFrom(httpServerTransport.boundAddress().boundAddresses());
        final int numberOfRequests = randomIntBetween(4, 16);
        final List<String> requests = new ArrayList<>(numberOfRequests);
        for (int i = 0; i < numberOfRequests; i++) {
            if (rarely()) {
                requests.add("/slow/" + i);
            } else {
                requests.add("/" + i);
            }
        }
        try (Netty4HttpClient nettyHttpClient = new Netty4HttpClient()) {
            Collection<FullHttpResponse> responses = nettyHttpClient.get(transportAddress.address(), requests.toArray(new String[] {}));
            try {
                Collection<String> responseBodies = Netty4HttpClient.returnHttpResponseBodies(responses);
                assertThat(responseBodies, contains(requests.toArray()));
            } finally {
                responses.forEach(ReferenceCounted::release);
            }
        }
    }
}
Also used : TransportAddress(org.opensearch.common.transport.TransportAddress) ArrayList(java.util.ArrayList) HttpServerTransport(org.opensearch.http.HttpServerTransport) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings) ReferenceCounted(io.netty.util.ReferenceCounted)

Aggregations

HttpServerTransport (org.opensearch.http.HttpServerTransport)12 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)6 ClusterSettings (org.opensearch.common.settings.ClusterSettings)6 TransportAddress (org.opensearch.common.transport.TransportAddress)6 ReferenceCounted (io.netty.util.ReferenceCounted)5 Settings (org.opensearch.common.settings.Settings)5 ThreadPool (org.opensearch.threadpool.ThreadPool)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 BigArrays (org.opensearch.common.util.BigArrays)3 PageCacheRecycler (org.opensearch.common.util.PageCacheRecycler)3 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)3 CircuitBreakerService (org.opensearch.indices.breaker.CircuitBreakerService)3 NetworkPlugin (org.opensearch.plugins.NetworkPlugin)3 TestThreadPool (org.opensearch.threadpool.TestThreadPool)3 IOException (java.io.IOException)2 Tuple (org.opensearch.common.collect.Tuple)2 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)2 NoOpNodeClient (org.opensearch.test.client.NoOpNodeClient)2