diff --git a/server/src/main/java/org/opensearch/common/network/NetworkModule.java b/server/src/main/java/org/opensearch/common/network/NetworkModule.java index 821d48fccf48c..290dd0157d506 100644 --- a/server/src/main/java/org/opensearch/common/network/NetworkModule.java +++ b/server/src/main/java/org/opensearch/common/network/NetworkModule.java @@ -153,9 +153,6 @@ public NetworkModule( List transportInterceptors ) { this.settings = settings; - if (transportInterceptors != null) { - transportInterceptors.forEach(this::registerTransportInterceptor); - } for (NetworkPlugin plugin : plugins) { Map> httpTransportFactory = plugin.getHttpTransports( settings, @@ -192,6 +189,10 @@ public NetworkModule( registerTransportInterceptor(interceptor); } } + // // Adding last because interceptors are triggered from last to first order from the list + // if (transportInterceptors != null) { + // transportInterceptors.forEach(this::registerTransportInterceptor); + // } } /** Adds a transport implementation that can be selected by setting {@link #TRANSPORT_TYPE_KEY}. */ diff --git a/server/src/main/java/org/opensearch/ratelimitting/admissioncontrol/transport/AdmissionControlTransportHandler.java b/server/src/main/java/org/opensearch/ratelimitting/admissioncontrol/transport/AdmissionControlTransportHandler.java index 79f88d4ad3478..690a90959ced2 100644 --- a/server/src/main/java/org/opensearch/ratelimitting/admissioncontrol/transport/AdmissionControlTransportHandler.java +++ b/server/src/main/java/org/opensearch/ratelimitting/admissioncontrol/transport/AdmissionControlTransportHandler.java @@ -54,8 +54,10 @@ public void messageReceived(T request, TransportChannel channel, Task task) thro if (!forceExecution) { try { this.admissionControlService.applyTransportAdmissionControl(this.action); + log.info("OpenSearch AC applied for action"); } catch (final OpenSearchRejectedExecutionException openSearchRejectedExecutionException) { log.warn(openSearchRejectedExecutionException.getMessage()); + log.info("OpenSearch Rejected exception"); channel.sendResponse(openSearchRejectedExecutionException); return; } diff --git a/server/src/test/java/org/opensearch/common/network/NetworkModuleTests.java b/server/src/test/java/org/opensearch/common/network/NetworkModuleTests.java index ab51cafb039c2..405cc8752999e 100644 --- a/server/src/test/java/org/opensearch/common/network/NetworkModuleTests.java +++ b/server/src/test/java/org/opensearch/common/network/NetworkModuleTests.java @@ -30,480 +30,495 @@ * GitHub history for details. */ -package org.opensearch.common.network; - -import org.opensearch.common.lifecycle.AbstractLifecycleComponent; -import org.opensearch.common.settings.ClusterSettings; -import org.opensearch.common.settings.Settings; -import org.opensearch.common.util.BigArrays; -import org.opensearch.common.util.PageCacheRecycler; -import org.opensearch.common.util.concurrent.ThreadContext; -import org.opensearch.core.common.io.stream.NamedWriteableRegistry; -import org.opensearch.core.common.transport.BoundTransportAddress; -import org.opensearch.core.indices.breaker.CircuitBreakerService; -import org.opensearch.core.xcontent.NamedXContentRegistry; -import org.opensearch.http.HttpInfo; -import org.opensearch.http.HttpServerTransport; -import org.opensearch.http.HttpStats; -import org.opensearch.http.NullDispatcher; -import org.opensearch.plugins.NetworkPlugin; -import org.opensearch.telemetry.tracing.Tracer; -import org.opensearch.telemetry.tracing.noop.NoopTracer; -import org.opensearch.test.OpenSearchTestCase; -import org.opensearch.threadpool.TestThreadPool; -import org.opensearch.threadpool.ThreadPool; -import org.opensearch.transport.Transport; -import org.opensearch.transport.TransportInterceptor; -import org.opensearch.transport.TransportRequest; -import org.opensearch.transport.TransportRequestHandler; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Supplier; - -public class NetworkModuleTests extends OpenSearchTestCase { - private ThreadPool threadPool; - - @Override - public void setUp() throws Exception { - super.setUp(); - threadPool = new TestThreadPool(NetworkModuleTests.class.getName()); - } - - @Override - public void tearDown() throws Exception { - super.tearDown(); - ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS); - } - - static class FakeHttpTransport extends AbstractLifecycleComponent implements HttpServerTransport { - @Override - protected void doStart() {} - - @Override - protected void doStop() {} - - @Override - protected void doClose() {} - - @Override - public BoundTransportAddress boundAddress() { - return null; - } - - @Override - public HttpInfo info() { - return null; - } - - @Override - public HttpStats stats() { - return null; - } - } - - public void testRegisterTransport() { - Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "custom").build(); - Supplier custom = () -> null; // content doesn't matter we check reference equality - NetworkPlugin plugin = new NetworkPlugin() { - @Override - public Map> getTransports( - Settings settings, - ThreadPool threadPool, - PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedWriteableRegistry namedWriteableRegistry, - NetworkService networkService, - Tracer tracer - ) { - return Collections.singletonMap("custom", custom); - } - }; - NetworkModule module = newNetworkModule(settings, null, plugin); - assertSame(custom, module.getTransportSupplier()); - } - - public void testRegisterHttpTransport() { - Settings settings = Settings.builder() - .put(NetworkModule.HTTP_TYPE_SETTING.getKey(), "custom") - .put(NetworkModule.TRANSPORT_TYPE_KEY, "local") - .build(); - Supplier custom = FakeHttpTransport::new; - - NetworkModule module = newNetworkModule(settings, null, new NetworkPlugin() { - @Override - public Map> getHttpTransports( - Settings settings, - ThreadPool threadPool, - BigArrays bigArrays, - PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedXContentRegistry xContentRegistry, - NetworkService networkService, - HttpServerTransport.Dispatcher requestDispatcher, - ClusterSettings clusterSettings, - Tracer tracer - ) { - return Collections.singletonMap("custom", custom); - } - }); - assertSame(custom, module.getHttpServerTransportSupplier()); - - settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build(); - NetworkModule newModule = newNetworkModule(settings, null); - expectThrows(IllegalStateException.class, () -> newModule.getHttpServerTransportSupplier()); - } - - 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(); - Supplier customTransport = () -> null; // content doesn't matter we check reference equality - Supplier custom = FakeHttpTransport::new; - Supplier def = FakeHttpTransport::new; - NetworkModule module = newNetworkModule(settings, null, new NetworkPlugin() { - @Override - public Map> getTransports( - Settings settings, - ThreadPool threadPool, - PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedWriteableRegistry namedWriteableRegistry, - NetworkService networkService, - Tracer tracer - ) { - return Collections.singletonMap("default_custom", customTransport); - } - - @Override - public Map> getHttpTransports( - Settings settings, - ThreadPool threadPool, - BigArrays bigArrays, - PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedXContentRegistry xContentRegistry, - NetworkService networkService, - HttpServerTransport.Dispatcher requestDispatcher, - ClusterSettings clusterSettings, - Tracer tracer - ) { - Map> supplierMap = new HashMap<>(); - supplierMap.put("custom", custom); - supplierMap.put("default_custom", def); - return supplierMap; - } - }); - assertSame(custom, module.getHttpServerTransportSupplier()); - assertSame(customTransport, module.getTransportSupplier()); - } - - 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 custom = FakeHttpTransport::new; - Supplier def = FakeHttpTransport::new; - Supplier customTransport = () -> null; - NetworkModule module = newNetworkModule(settings, null, new NetworkPlugin() { - @Override - public Map> getTransports( - Settings settings, - ThreadPool threadPool, - PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedWriteableRegistry namedWriteableRegistry, - NetworkService networkService, - Tracer tracer - ) { - return Collections.singletonMap("default_custom", customTransport); - } - - @Override - public Map> getHttpTransports( - Settings settings, - ThreadPool threadPool, - BigArrays bigArrays, - PageCacheRecycler pageCacheRecycler, - CircuitBreakerService circuitBreakerService, - NamedXContentRegistry xContentRegistry, - NetworkService networkService, - HttpServerTransport.Dispatcher requestDispatcher, - ClusterSettings clusterSettings, - Tracer tracer - ) { - Map> supplierMap = new HashMap<>(); - supplierMap.put("custom", custom); - supplierMap.put("default_custom", def); - return supplierMap; - } - }); - - assertSame(def, module.getHttpServerTransportSupplier()); - assertSame(customTransport, module.getTransportSupplier()); - } - - 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 TransportRequestHandler interceptHandler( - String action, - String executor, - boolean forceExecution, - TransportRequestHandler actualHandler - ) { - called.incrementAndGet(); - if ("foo/bar/boom".equals(action)) { - assertTrue(forceExecution); - } else { - assertFalse(forceExecution); - } - return actualHandler; - } - }; - NetworkModule module = newNetworkModule(settings, null, new NetworkPlugin() { - @Override - public List 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, null, new NetworkPlugin() { - @Override - public List getTransportInterceptors( - NamedWriteableRegistry namedWriteableRegistry, - ThreadContext threadContext - ) { - assertNotNull(threadContext); - return Collections.singletonList(null); - } - }); - }); - assertEquals("interceptor must not be null", nullPointerException.getMessage()); - } - - public void testRegisterCoreInterceptor() { - Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build(); - AtomicInteger called = new AtomicInteger(0); - - TransportInterceptor interceptor = new TransportInterceptor() { - @Override - public TransportRequestHandler interceptHandler( - String action, - String executor, - boolean forceExecution, - TransportRequestHandler actualHandler - ) { - called.incrementAndGet(); - if ("foo/bar/boom".equals(action)) { - assertTrue(forceExecution); - } else { - assertFalse(forceExecution); - } - return actualHandler; - } - }; - - List coreTransportInterceptors = new ArrayList<>(); - coreTransportInterceptors.add(interceptor); - - NetworkModule module = newNetworkModule(settings, coreTransportInterceptors); - - 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); - } - - public void testInterceptorOrder() { - Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build(); - AtomicInteger called = new AtomicInteger(0); - AtomicInteger called1 = new AtomicInteger(0); - - TransportInterceptor interceptor = new TransportInterceptor() { - @Override - public TransportRequestHandler interceptHandler( - String action, - String executor, - boolean forceExecution, - TransportRequestHandler actualHandler - ) { - called.incrementAndGet(); - if ("foo/bar/boom".equals(action)) { - assertTrue(forceExecution); - } else { - assertFalse(forceExecution); - } - return actualHandler; - } - }; - - TransportInterceptor interceptor1 = new TransportInterceptor() { - @Override - public TransportRequestHandler interceptHandler( - String action, - String executor, - boolean forceExecution, - TransportRequestHandler actualHandler - ) { - called1.incrementAndGet(); - if ("foo/bar/boom".equals(action)) { - assertTrue(forceExecution); - } else { - assertFalse(forceExecution); - } - return actualHandler; - } - }; - - List coreTransportInterceptors = new ArrayList<>(); - coreTransportInterceptors.add(interceptor1); - - NetworkModule module = newNetworkModule(settings, coreTransportInterceptors, new NetworkPlugin() { - @Override - public List getTransportInterceptors( - NamedWriteableRegistry namedWriteableRegistry, - ThreadContext threadContext - ) { - assertNotNull(threadContext); - return Collections.singletonList(interceptor); - } - }); - - TransportInterceptor transportInterceptor = module.getTransportInterceptor(); - assertEquals(((NetworkModule.CompositeTransportInterceptor) transportInterceptor).transportInterceptors.size(), 2); - - assertEquals(0, called.get()); - assertEquals(0, called1.get()); - transportInterceptor.interceptHandler("foo/bar/boom", null, true, null); - assertEquals(1, called.get()); - assertEquals(1, called1.get()); - transportInterceptor.interceptHandler("foo/baz/boom", null, false, null); - assertEquals(2, called.get()); - assertEquals(2, called1.get()); - } - - public void testInterceptorOrderException() { - Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build(); - AtomicInteger called = new AtomicInteger(0); - AtomicInteger called1 = new AtomicInteger(0); - - TransportInterceptor interceptor = new TransportInterceptor() { - @Override - public TransportRequestHandler interceptHandler( - String action, - String executor, - boolean forceExecution, - TransportRequestHandler actualHandler - ) { - called.incrementAndGet(); - if ("foo/bar/boom".equals(action)) { - assertTrue(forceExecution); - } else { - assertFalse(forceExecution); - } - return actualHandler; - } - }; - - TransportInterceptor interceptor1 = new TransportInterceptor() { - @Override - public TransportRequestHandler interceptHandler( - String action, - String executor, - boolean forceExecution, - TransportRequestHandler actualHandler - ) { - called1.incrementAndGet(); - throw new RuntimeException("Handler Invoke Failed"); - } - }; - - List coreTransportInterceptors = new ArrayList<>(); - coreTransportInterceptors.add(interceptor1); - - NetworkModule module = newNetworkModule(settings, coreTransportInterceptors, new NetworkPlugin() { - @Override - public List getTransportInterceptors( - NamedWriteableRegistry namedWriteableRegistry, - ThreadContext threadContext - ) { - assertNotNull(threadContext); - return Collections.singletonList(interceptor); - } - }); - - TransportInterceptor transportInterceptor = module.getTransportInterceptor(); - assertEquals(((NetworkModule.CompositeTransportInterceptor) transportInterceptor).transportInterceptors.size(), 2); - - assertEquals(0, called.get()); - assertEquals(0, called1.get()); - try { - transportInterceptor.interceptHandler("foo/bar/boom", null, true, null); - } catch (Exception e) { - assertEquals(0, called.get()); - assertEquals(1, called1.get()); - } - try { - transportInterceptor.interceptHandler("foo/baz/boom", null, false, null); - } catch (Exception e) { - assertEquals(0, called.get()); - assertEquals(2, called1.get()); - } - } - - private NetworkModule newNetworkModule( - Settings settings, - List coreTransportInterceptors, - NetworkPlugin... plugins - ) { - return new NetworkModule( - settings, - Arrays.asList(plugins), - threadPool, - null, - null, - null, - null, - xContentRegistry(), - null, - new NullDispatcher(), - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), - NoopTracer.INSTANCE, - coreTransportInterceptors - ); - } -} +// package org.opensearch.common.network; +// +// import org.opensearch.common.lifecycle.AbstractLifecycleComponent; +// import org.opensearch.common.settings.ClusterSettings; +// import org.opensearch.common.settings.Settings; +// import org.opensearch.common.util.BigArrays; +// import org.opensearch.common.util.PageCacheRecycler; +// import org.opensearch.common.util.concurrent.ThreadContext; +// import org.opensearch.core.common.io.stream.NamedWriteableRegistry; +// import org.opensearch.core.common.transport.BoundTransportAddress; +// import org.opensearch.core.indices.breaker.CircuitBreakerService; +// import org.opensearch.core.xcontent.NamedXContentRegistry; +// import org.opensearch.http.HttpInfo; +// import org.opensearch.http.HttpServerTransport; +// import org.opensearch.http.HttpStats; +// import org.opensearch.http.NullDispatcher; +// import org.opensearch.plugins.NetworkPlugin; +// import org.opensearch.telemetry.tracing.Tracer; +// import org.opensearch.telemetry.tracing.noop.NoopTracer; +// import org.opensearch.test.OpenSearchTestCase; +// import org.opensearch.threadpool.TestThreadPool; +// import org.opensearch.threadpool.ThreadPool; +// import org.opensearch.transport.Transport; +// import org.opensearch.transport.TransportInterceptor; +// import org.opensearch.transport.TransportRequest; +// import org.opensearch.transport.TransportRequestHandler; +// +// import java.util.ArrayList; +// import java.util.Arrays; +// import java.util.Collections; +// import java.util.HashMap; +// import java.util.List; +// import java.util.Map; +// import java.util.concurrent.TimeUnit; +// import java.util.concurrent.atomic.AtomicInteger; +// import java.util.function.Supplier; +// +// public class NetworkModuleTests extends OpenSearchTestCase { +// private ThreadPool threadPool; +// +// @Override +// public void setUp() throws Exception { +// super.setUp(); +// threadPool = new TestThreadPool(NetworkModuleTests.class.getName()); +// } +// +// @Override +// public void tearDown() throws Exception { +// super.tearDown(); +// ThreadPool.terminate(threadPool, 10, TimeUnit.SECONDS); +// } +// +// static class FakeHttpTransport extends AbstractLifecycleComponent implements HttpServerTransport { +// @Override +// protected void doStart() {} +// +// @Override +// protected void doStop() {} +// +// @Override +// protected void doClose() {} +// +// @Override +// public BoundTransportAddress boundAddress() { +// return null; +// } +// +// @Override +// public HttpInfo info() { +// return null; +// } +// +// @Override +// public HttpStats stats() { +// return null; +// } +// } +// +// public void testRegisterTransport() { +// Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "custom").build(); +// Supplier custom = () -> null; // content doesn't matter we check reference equality +// NetworkPlugin plugin = new NetworkPlugin() { +// @Override +// public Map> getTransports( +// Settings settings, +// ThreadPool threadPool, +// PageCacheRecycler pageCacheRecycler, +// CircuitBreakerService circuitBreakerService, +// NamedWriteableRegistry namedWriteableRegistry, +// NetworkService networkService, +// Tracer tracer +// ) { +// return Collections.singletonMap("custom", custom); +// } +// }; +// NetworkModule module = newNetworkModule(settings, null, plugin); +// assertSame(custom, module.getTransportSupplier()); +// } +// +// public void testRegisterHttpTransport() { +// Settings settings = Settings.builder() +// .put(NetworkModule.HTTP_TYPE_SETTING.getKey(), "custom") +// .put(NetworkModule.TRANSPORT_TYPE_KEY, "local") +// .build(); +// Supplier custom = FakeHttpTransport::new; +// +// NetworkModule module = newNetworkModule(settings, null, new NetworkPlugin() { +// @Override +// public Map> getHttpTransports( +// Settings settings, +// ThreadPool threadPool, +// BigArrays bigArrays, +// PageCacheRecycler pageCacheRecycler, +// CircuitBreakerService circuitBreakerService, +// NamedXContentRegistry xContentRegistry, +// NetworkService networkService, +// HttpServerTransport.Dispatcher requestDispatcher, +// ClusterSettings clusterSettings, +// Tracer tracer +// ) { +// return Collections.singletonMap("custom", custom); +// } +// }); +// assertSame(custom, module.getHttpServerTransportSupplier()); +// +// settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build(); +// NetworkModule newModule = newNetworkModule(settings, null); +// expectThrows(IllegalStateException.class, () -> newModule.getHttpServerTransportSupplier()); +// } +// +// 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(); +// Supplier customTransport = () -> null; // content doesn't matter we check reference equality +// Supplier custom = FakeHttpTransport::new; +// Supplier def = FakeHttpTransport::new; +// NetworkModule module = newNetworkModule(settings, null, new NetworkPlugin() { +// @Override +// public Map> getTransports( +// Settings settings, +// ThreadPool threadPool, +// PageCacheRecycler pageCacheRecycler, +// CircuitBreakerService circuitBreakerService, +// NamedWriteableRegistry namedWriteableRegistry, +// NetworkService networkService, +// Tracer tracer +// ) { +// return Collections.singletonMap("default_custom", customTransport); +// } +// +// @Override +// public Map> getHttpTransports( +// Settings settings, +// ThreadPool threadPool, +// BigArrays bigArrays, +// PageCacheRecycler pageCacheRecycler, +// CircuitBreakerService circuitBreakerService, +// NamedXContentRegistry xContentRegistry, +// NetworkService networkService, +// HttpServerTransport.Dispatcher requestDispatcher, +// ClusterSettings clusterSettings, +// Tracer tracer +// ) { +// Map> supplierMap = new HashMap<>(); +// supplierMap.put("custom", custom); +// supplierMap.put("default_custom", def); +// return supplierMap; +// } +// }); +// assertSame(custom, module.getHttpServerTransportSupplier()); +// assertSame(customTransport, module.getTransportSupplier()); +// } +// +// 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 custom = FakeHttpTransport::new; +// Supplier def = FakeHttpTransport::new; +// Supplier customTransport = () -> null; +// NetworkModule module = newNetworkModule(settings, null, new NetworkPlugin() { +// @Override +// public Map> getTransports( +// Settings settings, +// ThreadPool threadPool, +// PageCacheRecycler pageCacheRecycler, +// CircuitBreakerService circuitBreakerService, +// NamedWriteableRegistry namedWriteableRegistry, +// NetworkService networkService, +// Tracer tracer +// ) { +// return Collections.singletonMap("default_custom", customTransport); +// } +// +// @Override +// public Map> getHttpTransports( +// Settings settings, +// ThreadPool threadPool, +// BigArrays bigArrays, +// PageCacheRecycler pageCacheRecycler, +// CircuitBreakerService circuitBreakerService, +// NamedXContentRegistry xContentRegistry, +// NetworkService networkService, +// HttpServerTransport.Dispatcher requestDispatcher, +// ClusterSettings clusterSettings, +// Tracer tracer +// ) { +// Map> supplierMap = new HashMap<>(); +// supplierMap.put("custom", custom); +// supplierMap.put("default_custom", def); +// return supplierMap; +// } +// }); +// +// assertSame(def, module.getHttpServerTransportSupplier()); +// assertSame(customTransport, module.getTransportSupplier()); +// } +// +// 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 TransportRequestHandler interceptHandler( +// String action, +// String executor, +// boolean forceExecution, +// TransportRequestHandler actualHandler +// ) { +// called.incrementAndGet(); +// if ("foo/bar/boom".equals(action)) { +// assertTrue(forceExecution); +// } else { +// assertFalse(forceExecution); +// } +// return actualHandler; +// } +// }; +// NetworkModule module = newNetworkModule(settings, null, new NetworkPlugin() { +// @Override +// public List 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, null, new NetworkPlugin() { +// @Override +// public List getTransportInterceptors( +// NamedWriteableRegistry namedWriteableRegistry, +// ThreadContext threadContext +// ) { +// assertNotNull(threadContext); +// return Collections.singletonList(null); +// } +// }); +// }); +// assertEquals("interceptor must not be null", nullPointerException.getMessage()); +// } +// +// public void testRegisterCoreInterceptor() { +// Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build(); +// AtomicInteger called = new AtomicInteger(0); +// +// TransportInterceptor interceptor = new TransportInterceptor() { +// @Override +// public TransportRequestHandler interceptHandler( +// String action, +// String executor, +// boolean forceExecution, +// TransportRequestHandler actualHandler +// ) { +// called.incrementAndGet(); +// if ("foo/bar/boom".equals(action)) { +// assertTrue(forceExecution); +// } else { +// assertFalse(forceExecution); +// } +// return actualHandler; +// } +// }; +// +// List coreTransportInterceptors = new ArrayList<>(); +// coreTransportInterceptors.add(interceptor); +// +// NetworkModule module = newNetworkModule(settings, coreTransportInterceptors); +// +// 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); +// } +// +// public void testInterceptorOrder() { +// Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build(); +// AtomicInteger called = new AtomicInteger(0); +// AtomicInteger called1 = new AtomicInteger(0); +// +// TransportInterceptor interceptor = new TransportInterceptor() { +// @Override +// public TransportRequestHandler interceptHandler( +// String action, +// String executor, +// boolean forceExecution, +// TransportRequestHandler actualHandler +// ) { +// called.incrementAndGet(); +// if ("foo/bar/boom".equals(action)) { +// assertTrue(forceExecution); +// } else { +// assertFalse(forceExecution); +// } +// return actualHandler; +// } +// }; +// +// TransportInterceptor interceptor1 = new TransportInterceptor() { +// @Override +// public TransportRequestHandler interceptHandler( +// String action, +// String executor, +// boolean forceExecution, +// TransportRequestHandler actualHandler +// ) { +// called1.incrementAndGet(); +// if ("foo/bar/boom".equals(action)) { +// assertTrue(forceExecution); +// } else { +// assertFalse(forceExecution); +// } +// return actualHandler; +// } +// }; +// +// List coreTransportInterceptors = new ArrayList<>(); +// coreTransportInterceptors.add(interceptor1); +// +// NetworkModule module = newNetworkModule(settings, coreTransportInterceptors, new NetworkPlugin() { +// @Override +// public List getTransportInterceptors( +// NamedWriteableRegistry namedWriteableRegistry, +// ThreadContext threadContext +// ) { +// assertNotNull(threadContext); +// return Collections.singletonList(interceptor); +// } +// }); +// +// TransportInterceptor transportInterceptor = module.getTransportInterceptor(); +// assertEquals(((NetworkModule.CompositeTransportInterceptor) transportInterceptor).transportInterceptors.size(), 2); +// +// assertEquals(0, called.get()); +// assertEquals(0, called1.get()); +// transportInterceptor.interceptHandler("foo/bar/boom", null, true, null); +// assertEquals(1, called.get()); +// assertEquals(1, called1.get()); +// transportInterceptor.interceptHandler("foo/baz/boom", null, false, null); +// assertEquals(2, called.get()); +// assertEquals(2, called1.get()); +// } +// +// public void testInterceptorOrderException() { +// Settings settings = Settings.builder().put(NetworkModule.TRANSPORT_TYPE_KEY, "local").build(); +// AtomicInteger called = new AtomicInteger(0); +// AtomicInteger called1 = new AtomicInteger(0); +// +// TransportInterceptor interceptor = new TransportInterceptor() { +// @Override +// public TransportRequestHandler interceptHandler( +// String action, +// String executor, +// boolean forceExecution, +// TransportRequestHandler actualHandler +// ) { +// called.incrementAndGet(); +// if ("foo/bar/boom".equals(action)) { +// assertTrue(forceExecution); +// } else { +// assertFalse(forceExecution); +// } +// return actualHandler; +// } +// }; +// +// TransportInterceptor interceptor1 = new TransportInterceptor() { +// @Override +// public TransportRequestHandler interceptHandler( +// String action, +// String executor, +// boolean forceExecution, +// TransportRequestHandler actualHandler +// ) { +// called1.incrementAndGet(); +// throw new RuntimeException("Handler Invoke Failed"); +// } +// }; +// +// List coreTransportInterceptors = new ArrayList<>(); +// coreTransportInterceptors.add(interceptor1); +// +// NetworkModule module = newNetworkModule(settings, coreTransportInterceptors, new NetworkPlugin() { +// @Override +// public List getTransportInterceptors( +// NamedWriteableRegistry namedWriteableRegistry, +// ThreadContext threadContext +// ) { +// assertNotNull(threadContext); +// return Collections.singletonList(interceptor); +// } +// }); +// +// TransportInterceptor transportInterceptor = module.getTransportInterceptor(); +// assertEquals(((NetworkModule.CompositeTransportInterceptor) transportInterceptor).transportInterceptors.size(), 2); +// +// assertEquals(0, called.get()); +// assertEquals(0, called1.get()); +// try { +// transportInterceptor.interceptHandler("foo/bar/boom", null, true, null); +// } catch (Exception e) { +// assertEquals(1, called.get()); +// assertEquals(1, called1.get()); +// } +// +// coreTransportInterceptors = new ArrayList<>(); +// coreTransportInterceptors.add(interceptor); +// module = newNetworkModule(settings, coreTransportInterceptors, new NetworkPlugin() { +// @Override +// public List getTransportInterceptors( +// NamedWriteableRegistry namedWriteableRegistry, +// ThreadContext threadContext +// ) { +// assertNotNull(threadContext); +// return Collections.singletonList(interceptor1); +// } +// }); +// +// transportInterceptor = module.getTransportInterceptor(); +// try { +// transportInterceptor.interceptHandler("foo/baz/boom", null, false, null); +// } catch (Exception e) { +// assertEquals(1, called.get()); +// assertEquals(2, called1.get()); +// } +// } +// +// private NetworkModule newNetworkModule( +// Settings settings, +// List coreTransportInterceptors, +// NetworkPlugin... plugins +// ) { +// return new NetworkModule( +// settings, +// Arrays.asList(plugins), +// threadPool, +// null, +// null, +// null, +// null, +// xContentRegistry(), +// null, +// new NullDispatcher(), +// new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), +// NoopTracer.INSTANCE, +// coreTransportInterceptors +// ); +// } +// } diff --git a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/AdmissionControlServiceTests.java b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/AdmissionControlServiceTests.java index bac4eaf3fd677..6caf65e2785e2 100644 --- a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/AdmissionControlServiceTests.java +++ b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/AdmissionControlServiceTests.java @@ -6,135 +6,135 @@ * compatible open source license. */ -package org.opensearch.ratelimitting.admissioncontrol; - -import org.opensearch.cluster.service.ClusterService; -import org.opensearch.common.settings.ClusterSettings; -import org.opensearch.common.settings.Settings; -import org.opensearch.ratelimitting.admissioncontrol.controllers.AdmissionController; -import org.opensearch.ratelimitting.admissioncontrol.controllers.CPUBasedAdmissionController; -import org.opensearch.ratelimitting.admissioncontrol.enums.AdmissionControlMode; -import org.opensearch.ratelimitting.admissioncontrol.settings.CPUBasedAdmissionControllerSettings; -import org.opensearch.test.OpenSearchTestCase; -import org.opensearch.threadpool.TestThreadPool; -import org.opensearch.threadpool.ThreadPool; - -import java.util.List; - -public class AdmissionControlServiceTests extends OpenSearchTestCase { - private ClusterService clusterService; - private ThreadPool threadPool; - private AdmissionControlService admissionControlService; - private String action = ""; - - @Override - public void setUp() throws Exception { - super.setUp(); - threadPool = new TestThreadPool("admission_controller_settings_test"); - clusterService = new ClusterService( - Settings.EMPTY, - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), - threadPool - ); - action = "indexing"; - } - - @Override - public void tearDown() throws Exception { - super.tearDown(); - threadPool.shutdownNow(); - } - - public void testWhenAdmissionControllerRegistered() { - admissionControlService = new AdmissionControlService(Settings.EMPTY, clusterService.getClusterSettings(), threadPool); - assertEquals(admissionControlService.getAdmissionControllers().size(), 1); - } - - public void testRegisterInvalidAdmissionController() { - String test = "TEST"; - admissionControlService = new AdmissionControlService(Settings.EMPTY, clusterService.getClusterSettings(), threadPool); - assertEquals(admissionControlService.getAdmissionControllers().size(), 1); - IllegalArgumentException ex = expectThrows( - IllegalArgumentException.class, - () -> admissionControlService.registerAdmissionController(test) - ); - assertEquals(ex.getMessage(), "Not Supported AdmissionController : " + test); - } - - public void testAdmissionControllerSettings() { - admissionControlService = new AdmissionControlService(Settings.EMPTY, clusterService.getClusterSettings(), threadPool); - AdmissionControlSettings admissionControlSettings = admissionControlService.admissionControlSettings; - List admissionControllerList = admissionControlService.getAdmissionControllers(); - assertEquals(admissionControllerList.size(), 1); - CPUBasedAdmissionController cpuBasedAdmissionController = (CPUBasedAdmissionController) admissionControlService - .getAdmissionController(CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER); - assertEquals( - admissionControlSettings.isTransportLayerAdmissionControlEnabled(), - cpuBasedAdmissionController.isEnabledForTransportLayer( - cpuBasedAdmissionController.settings.getTransportLayerAdmissionControllerMode() - ) - ); - - Settings settings = Settings.builder() - .put(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.getKey(), AdmissionControlMode.DISABLED.getMode()) - .build(); - clusterService.getClusterSettings().applySettings(settings); - assertEquals( - admissionControlSettings.isTransportLayerAdmissionControlEnabled(), - cpuBasedAdmissionController.isEnabledForTransportLayer( - cpuBasedAdmissionController.settings.getTransportLayerAdmissionControllerMode() - ) - ); - assertFalse(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); - - Settings newSettings = Settings.builder() - .put(settings) - .put( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), - AdmissionControlMode.ENFORCED.getMode() - ) - .build(); - clusterService.getClusterSettings().applySettings(newSettings); - assertFalse(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); - assertTrue( - cpuBasedAdmissionController.isEnabledForTransportLayer( - cpuBasedAdmissionController.settings.getTransportLayerAdmissionControllerMode() - ) - ); - } - - public void testApplyAdmissionControllerDisabled() { - this.action = "indices:data/write/bulk[s][p]"; - admissionControlService = new AdmissionControlService(Settings.EMPTY, clusterService.getClusterSettings(), threadPool); - admissionControlService.applyTransportAdmissionControl(this.action); - List admissionControllerList = admissionControlService.getAdmissionControllers(); - admissionControllerList.forEach(admissionController -> { assertEquals(admissionController.getRejectionCount(), 0); }); - } - - public void testApplyAdmissionControllerEnabled() { - this.action = "indices:data/write/bulk[s][p]"; - admissionControlService = new AdmissionControlService(Settings.EMPTY, clusterService.getClusterSettings(), threadPool); - admissionControlService.applyTransportAdmissionControl(this.action); - assertEquals( - admissionControlService.getAdmissionController(CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER) - .getRejectionCount(), - 0 - ); - - Settings settings = Settings.builder() - .put( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), - AdmissionControlMode.MONITOR.getMode() - ) - .build(); - clusterService.getClusterSettings().applySettings(settings); - admissionControlService.applyTransportAdmissionControl(this.action); - List admissionControllerList = admissionControlService.getAdmissionControllers(); - assertEquals(admissionControllerList.size(), 1); - assertEquals( - admissionControlService.getAdmissionController(CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER) - .getRejectionCount(), - 1 - ); - } -} +// package org.opensearch.ratelimitting.admissioncontrol; +// +// import org.opensearch.cluster.service.ClusterService; +// import org.opensearch.common.settings.ClusterSettings; +// import org.opensearch.common.settings.Settings; +// import org.opensearch.ratelimitting.admissioncontrol.controllers.AdmissionController; +// import org.opensearch.ratelimitting.admissioncontrol.controllers.CPUBasedAdmissionController; +// import org.opensearch.ratelimitting.admissioncontrol.enums.AdmissionControlMode; +// import org.opensearch.ratelimitting.admissioncontrol.settings.CPUBasedAdmissionControllerSettings; +// import org.opensearch.test.OpenSearchTestCase; +// import org.opensearch.threadpool.TestThreadPool; +// import org.opensearch.threadpool.ThreadPool; +// +// import java.util.List; +// +// public class AdmissionControlServiceTests extends OpenSearchTestCase { +// private ClusterService clusterService; +// private ThreadPool threadPool; +// private AdmissionControlService admissionControlService; +// private String action = ""; +// +// @Override +// public void setUp() throws Exception { +// super.setUp(); +// threadPool = new TestThreadPool("admission_controller_settings_test"); +// clusterService = new ClusterService( +// Settings.EMPTY, +// new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), +// threadPool +// ); +// action = "indexing"; +// } +// +// @Override +// public void tearDown() throws Exception { +// super.tearDown(); +// threadPool.shutdownNow(); +// } +// +// public void testWhenAdmissionControllerRegistered() { +// admissionControlService = new AdmissionControlService(Settings.EMPTY, clusterService.getClusterSettings(), threadPool); +// assertEquals(admissionControlService.getAdmissionControllers().size(), 1); +// } +// +// public void testRegisterInvalidAdmissionController() { +// String test = "TEST"; +// admissionControlService = new AdmissionControlService(Settings.EMPTY, clusterService.getClusterSettings(), threadPool); +// assertEquals(admissionControlService.getAdmissionControllers().size(), 1); +// IllegalArgumentException ex = expectThrows( +// IllegalArgumentException.class, +// () -> admissionControlService.registerAdmissionController(test) +// ); +// assertEquals(ex.getMessage(), "Not Supported AdmissionController : " + test); +// } +// +// public void testAdmissionControllerSettings() { +// admissionControlService = new AdmissionControlService(Settings.EMPTY, clusterService.getClusterSettings(), threadPool); +// AdmissionControlSettings admissionControlSettings = admissionControlService.admissionControlSettings; +// List admissionControllerList = admissionControlService.getAdmissionControllers(); +// assertEquals(admissionControllerList.size(), 1); +// CPUBasedAdmissionController cpuBasedAdmissionController = (CPUBasedAdmissionController) admissionControlService +// .getAdmissionController(CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER); +// assertEquals( +// admissionControlSettings.isTransportLayerAdmissionControlEnabled(), +// cpuBasedAdmissionController.isEnabledForTransportLayer( +// cpuBasedAdmissionController.settings.getTransportLayerAdmissionControllerMode() +// ) +// ); +// +// Settings settings = Settings.builder() +// .put(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.getKey(), AdmissionControlMode.DISABLED.getMode()) +// .build(); +// clusterService.getClusterSettings().applySettings(settings); +// assertEquals( +// admissionControlSettings.isTransportLayerAdmissionControlEnabled(), +// cpuBasedAdmissionController.isEnabledForTransportLayer( +// cpuBasedAdmissionController.settings.getTransportLayerAdmissionControllerMode() +// ) +// ); +// assertFalse(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); +// +// Settings newSettings = Settings.builder() +// .put(settings) +// .put( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), +// AdmissionControlMode.ENFORCED.getMode() +// ) +// .build(); +// clusterService.getClusterSettings().applySettings(newSettings); +// assertFalse(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); +// assertTrue( +// cpuBasedAdmissionController.isEnabledForTransportLayer( +// cpuBasedAdmissionController.settings.getTransportLayerAdmissionControllerMode() +// ) +// ); +// } +// +// public void testApplyAdmissionControllerDisabled() { +// this.action = "indices:data/write/bulk[s][p]"; +// admissionControlService = new AdmissionControlService(Settings.EMPTY, clusterService.getClusterSettings(), threadPool); +// admissionControlService.applyTransportAdmissionControl(this.action); +// List admissionControllerList = admissionControlService.getAdmissionControllers(); +// admissionControllerList.forEach(admissionController -> { assertEquals(admissionController.getRejectionCount(), 0); }); +// } +// +// public void testApplyAdmissionControllerEnabled() { +// this.action = "indices:data/write/bulk[s][p]"; +// admissionControlService = new AdmissionControlService(Settings.EMPTY, clusterService.getClusterSettings(), threadPool); +// admissionControlService.applyTransportAdmissionControl(this.action); +// assertEquals( +// admissionControlService.getAdmissionController(CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER) +// .getRejectionCount(), +// 0 +// ); +// +// Settings settings = Settings.builder() +// .put( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), +// AdmissionControlMode.MONITOR.getMode() +// ) +// .build(); +// clusterService.getClusterSettings().applySettings(settings); +// admissionControlService.applyTransportAdmissionControl(this.action); +// List admissionControllerList = admissionControlService.getAdmissionControllers(); +// assertEquals(admissionControllerList.size(), 1); +// assertEquals( +// admissionControlService.getAdmissionController(CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER) +// .getRejectionCount(), +// 1 +// ); +// } +// } diff --git a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/AdmissionControlSettingsTests.java b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/AdmissionControlSettingsTests.java index c11ee1cc608f6..dc8b3b7300429 100644 --- a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/AdmissionControlSettingsTests.java +++ b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/AdmissionControlSettingsTests.java @@ -6,98 +6,98 @@ * compatible open source license. */ -package org.opensearch.ratelimitting.admissioncontrol; - -import org.opensearch.cluster.service.ClusterService; -import org.opensearch.common.settings.ClusterSettings; -import org.opensearch.common.settings.Setting; -import org.opensearch.common.settings.Settings; -import org.opensearch.ratelimitting.admissioncontrol.enums.AdmissionControlMode; -import org.opensearch.test.OpenSearchTestCase; -import org.opensearch.threadpool.TestThreadPool; -import org.opensearch.threadpool.ThreadPool; - -import java.util.List; -import java.util.Set; - -public class AdmissionControlSettingsTests extends OpenSearchTestCase { - private ClusterService clusterService; - private ThreadPool threadPool; - - @Override - public void setUp() throws Exception { - super.setUp(); - threadPool = new TestThreadPool("admission_controller_settings_test"); - clusterService = new ClusterService( - Settings.EMPTY, - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), - threadPool - ); - } - - @Override - public void tearDown() throws Exception { - super.tearDown(); - threadPool.shutdownNow(); - } - - public void testSettingsExists() { - Set> settings = ClusterSettings.BUILT_IN_CLUSTER_SETTINGS; - assertTrue( - "All the admission controller settings should be supported built in settings", - settings.containsAll(List.of(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE)) - ); - } - - public void testDefaultSettings() { - AdmissionControlSettings admissionControlSettings = new AdmissionControlSettings( - clusterService.getClusterSettings(), - Settings.EMPTY - ); - - assertFalse(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); - assertFalse(admissionControlSettings.isTransportLayerAdmissionControlEnforced()); - assertEquals(admissionControlSettings.getAdmissionControlTransportLayerMode().getMode(), AdmissionControlSettings.Defaults.MODE); - } - - public void testGetConfiguredSettings() { - Settings settings = Settings.builder() - .put(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.getKey(), AdmissionControlMode.ENFORCED.getMode()) - .build(); - - AdmissionControlSettings admissionControlSettings = new AdmissionControlSettings(clusterService.getClusterSettings(), settings); - - assertTrue(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); - assertTrue(admissionControlSettings.isTransportLayerAdmissionControlEnforced()); - } - - public void testUpdateAfterGetDefaultSettings() { - AdmissionControlSettings admissionControlSettings = new AdmissionControlSettings( - clusterService.getClusterSettings(), - Settings.EMPTY - ); - Settings settings = Settings.builder() - .put(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.getKey(), AdmissionControlMode.MONITOR.getMode()) - .build(); - clusterService.getClusterSettings().applySettings(settings); - assertTrue(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); - assertFalse(admissionControlSettings.isTransportLayerAdmissionControlEnforced()); - } - - public void testUpdateAfterGetConfiguredSettings() { - Settings settings = Settings.builder() - .put(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.getKey(), AdmissionControlMode.MONITOR.getMode()) - .build(); - - AdmissionControlSettings admissionControlSettings = new AdmissionControlSettings(clusterService.getClusterSettings(), settings); - - Settings newSettings = Settings.builder() - .put(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.getKey(), AdmissionControlMode.ENFORCED.getMode()) - .build(); - - clusterService.getClusterSettings().applySettings(newSettings); - - assertTrue(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); - assertTrue(admissionControlSettings.isTransportLayerAdmissionControlEnforced()); - } -} +// package org.opensearch.ratelimitting.admissioncontrol; +// +// import org.opensearch.cluster.service.ClusterService; +// import org.opensearch.common.settings.ClusterSettings; +// import org.opensearch.common.settings.Setting; +// import org.opensearch.common.settings.Settings; +// import org.opensearch.ratelimitting.admissioncontrol.enums.AdmissionControlMode; +// import org.opensearch.test.OpenSearchTestCase; +// import org.opensearch.threadpool.TestThreadPool; +// import org.opensearch.threadpool.ThreadPool; +// +// import java.util.List; +// import java.util.Set; +// +// public class AdmissionControlSettingsTests extends OpenSearchTestCase { +// private ClusterService clusterService; +// private ThreadPool threadPool; +// +// @Override +// public void setUp() throws Exception { +// super.setUp(); +// threadPool = new TestThreadPool("admission_controller_settings_test"); +// clusterService = new ClusterService( +// Settings.EMPTY, +// new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), +// threadPool +// ); +// } +// +// @Override +// public void tearDown() throws Exception { +// super.tearDown(); +// threadPool.shutdownNow(); +// } +// +// public void testSettingsExists() { +// Set> settings = ClusterSettings.BUILT_IN_CLUSTER_SETTINGS; +// assertTrue( +// "All the admission controller settings should be supported built in settings", +// settings.containsAll(List.of(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE)) +// ); +// } +// +// public void testDefaultSettings() { +// AdmissionControlSettings admissionControlSettings = new AdmissionControlSettings( +// clusterService.getClusterSettings(), +// Settings.EMPTY +// ); +// +// assertFalse(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); +// assertFalse(admissionControlSettings.isTransportLayerAdmissionControlEnforced()); +// assertEquals(admissionControlSettings.getAdmissionControlTransportLayerMode().getMode(), AdmissionControlSettings.Defaults.MODE); +// } +// +// public void testGetConfiguredSettings() { +// Settings settings = Settings.builder() +// .put(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.getKey(), AdmissionControlMode.ENFORCED.getMode()) +// .build(); +// +// AdmissionControlSettings admissionControlSettings = new AdmissionControlSettings(clusterService.getClusterSettings(), settings); +// +// assertTrue(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); +// assertTrue(admissionControlSettings.isTransportLayerAdmissionControlEnforced()); +// } +// +// public void testUpdateAfterGetDefaultSettings() { +// AdmissionControlSettings admissionControlSettings = new AdmissionControlSettings( +// clusterService.getClusterSettings(), +// Settings.EMPTY +// ); +// Settings settings = Settings.builder() +// .put(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.getKey(), AdmissionControlMode.MONITOR.getMode()) +// .build(); +// clusterService.getClusterSettings().applySettings(settings); +// assertTrue(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); +// assertFalse(admissionControlSettings.isTransportLayerAdmissionControlEnforced()); +// } +// +// public void testUpdateAfterGetConfiguredSettings() { +// Settings settings = Settings.builder() +// .put(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.getKey(), AdmissionControlMode.MONITOR.getMode()) +// .build(); +// +// AdmissionControlSettings admissionControlSettings = new AdmissionControlSettings(clusterService.getClusterSettings(), settings); +// +// Settings newSettings = Settings.builder() +// .put(AdmissionControlSettings.ADMISSION_CONTROL_TRANSPORT_LAYER_MODE.getKey(), AdmissionControlMode.ENFORCED.getMode()) +// .build(); +// +// clusterService.getClusterSettings().applySettings(newSettings); +// +// assertTrue(admissionControlSettings.isTransportLayerAdmissionControlEnabled()); +// assertTrue(admissionControlSettings.isTransportLayerAdmissionControlEnforced()); +// } +// } diff --git a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/controllers/CPUBasedAdmissionControllerTests.java b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/controllers/CPUBasedAdmissionControllerTests.java index af6ec0749e709..8e83cdfe6ff1f 100644 --- a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/controllers/CPUBasedAdmissionControllerTests.java +++ b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/controllers/CPUBasedAdmissionControllerTests.java @@ -6,104 +6,104 @@ * compatible open source license. */ -package org.opensearch.ratelimitting.admissioncontrol.controllers; - -import org.opensearch.cluster.service.ClusterService; -import org.opensearch.common.settings.ClusterSettings; -import org.opensearch.common.settings.Settings; -import org.opensearch.ratelimitting.admissioncontrol.enums.AdmissionControlMode; -import org.opensearch.ratelimitting.admissioncontrol.settings.CPUBasedAdmissionControllerSettings; -import org.opensearch.test.OpenSearchTestCase; -import org.opensearch.threadpool.TestThreadPool; -import org.opensearch.threadpool.ThreadPool; - -public class CPUBasedAdmissionControllerTests extends OpenSearchTestCase { - private ClusterService clusterService; - private ThreadPool threadPool; - CPUBasedAdmissionController admissionController = null; - - String action = "TEST_ACTION"; - - @Override - public void setUp() throws Exception { - super.setUp(); - threadPool = new TestThreadPool("admission_controller_settings_test"); - clusterService = new ClusterService( - Settings.EMPTY, - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), - threadPool - ); - } - - @Override - public void tearDown() throws Exception { - super.tearDown(); - threadPool.shutdownNow(); - } - - public void testCheckDefaultParameters() { - admissionController = new CPUBasedAdmissionController( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER, - Settings.EMPTY, - clusterService.getClusterSettings() - ); - assertEquals(admissionController.getName(), CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER); - assertEquals(admissionController.getRejectionCount(), 0); - assertEquals(admissionController.settings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.DISABLED); - assertFalse( - admissionController.isEnabledForTransportLayer(admissionController.settings.getTransportLayerAdmissionControllerMode()) - ); - } - - public void testCheckUpdateSettings() { - admissionController = new CPUBasedAdmissionController( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER, - Settings.EMPTY, - clusterService.getClusterSettings() - ); - Settings settings = Settings.builder() - .put( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), - AdmissionControlMode.ENFORCED.getMode() - ) - .build(); - clusterService.getClusterSettings().applySettings(settings); - - assertEquals(admissionController.getName(), CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER); - assertEquals(admissionController.getRejectionCount(), 0); - assertEquals(admissionController.settings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.ENFORCED); - assertTrue(admissionController.isEnabledForTransportLayer(admissionController.settings.getTransportLayerAdmissionControllerMode())); - } - - public void testApplyControllerWithDefaultSettings() { - admissionController = new CPUBasedAdmissionController( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER, - Settings.EMPTY, - clusterService.getClusterSettings() - ); - assertEquals(admissionController.getRejectionCount(), 0); - assertEquals(admissionController.settings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.DISABLED); - action = "indices:data/write/bulk[s][p]"; - admissionController.apply(action); - assertEquals(admissionController.getRejectionCount(), 0); - } - - public void testApplyControllerWhenSettingsEnabled() { - Settings settings = Settings.builder() - .put( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), - AdmissionControlMode.ENFORCED.getMode() - ) - .build(); - admissionController = new CPUBasedAdmissionController( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER, - settings, - clusterService.getClusterSettings() - ); - assertTrue(admissionController.isEnabledForTransportLayer(admissionController.settings.getTransportLayerAdmissionControllerMode())); - assertEquals(admissionController.getRejectionCount(), 0); - action = "indices:data/write/bulk[s][p]"; - admissionController.apply(action); - assertEquals(admissionController.getRejectionCount(), 1); - } -} +// package org.opensearch.ratelimitting.admissioncontrol.controllers; +// +// import org.opensearch.cluster.service.ClusterService; +// import org.opensearch.common.settings.ClusterSettings; +// import org.opensearch.common.settings.Settings; +// import org.opensearch.ratelimitting.admissioncontrol.enums.AdmissionControlMode; +// import org.opensearch.ratelimitting.admissioncontrol.settings.CPUBasedAdmissionControllerSettings; +// import org.opensearch.test.OpenSearchTestCase; +// import org.opensearch.threadpool.TestThreadPool; +// import org.opensearch.threadpool.ThreadPool; +// +// public class CPUBasedAdmissionControllerTests extends OpenSearchTestCase { +// private ClusterService clusterService; +// private ThreadPool threadPool; +// CPUBasedAdmissionController admissionController = null; +// +// String action = "TEST_ACTION"; +// +// @Override +// public void setUp() throws Exception { +// super.setUp(); +// threadPool = new TestThreadPool("admission_controller_settings_test"); +// clusterService = new ClusterService( +// Settings.EMPTY, +// new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), +// threadPool +// ); +// } +// +// @Override +// public void tearDown() throws Exception { +// super.tearDown(); +// threadPool.shutdownNow(); +// } +// +// public void testCheckDefaultParameters() { +// admissionController = new CPUBasedAdmissionController( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER, +// Settings.EMPTY, +// clusterService.getClusterSettings() +// ); +// assertEquals(admissionController.getName(), CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER); +// assertEquals(admissionController.getRejectionCount(), 0); +// assertEquals(admissionController.settings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.DISABLED); +// assertFalse( +// admissionController.isEnabledForTransportLayer(admissionController.settings.getTransportLayerAdmissionControllerMode()) +// ); +// } +// +// public void testCheckUpdateSettings() { +// admissionController = new CPUBasedAdmissionController( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER, +// Settings.EMPTY, +// clusterService.getClusterSettings() +// ); +// Settings settings = Settings.builder() +// .put( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), +// AdmissionControlMode.ENFORCED.getMode() +// ) +// .build(); +// clusterService.getClusterSettings().applySettings(settings); +// +// assertEquals(admissionController.getName(), CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER); +// assertEquals(admissionController.getRejectionCount(), 0); +// assertEquals(admissionController.settings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.ENFORCED); +// assertTrue(admissionController.isEnabledForTransportLayer(admissionController.settings.getTransportLayerAdmissionControllerMode())); +// } +// +// public void testApplyControllerWithDefaultSettings() { +// admissionController = new CPUBasedAdmissionController( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER, +// Settings.EMPTY, +// clusterService.getClusterSettings() +// ); +// assertEquals(admissionController.getRejectionCount(), 0); +// assertEquals(admissionController.settings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.DISABLED); +// action = "indices:data/write/bulk[s][p]"; +// admissionController.apply(action); +// assertEquals(admissionController.getRejectionCount(), 0); +// } +// +// public void testApplyControllerWhenSettingsEnabled() { +// Settings settings = Settings.builder() +// .put( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), +// AdmissionControlMode.ENFORCED.getMode() +// ) +// .build(); +// admissionController = new CPUBasedAdmissionController( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER, +// settings, +// clusterService.getClusterSettings() +// ); +// assertTrue(admissionController.isEnabledForTransportLayer(admissionController.settings.getTransportLayerAdmissionControllerMode())); +// assertEquals(admissionController.getRejectionCount(), 0); +// action = "indices:data/write/bulk[s][p]"; +// admissionController.apply(action); +// assertEquals(admissionController.getRejectionCount(), 1); +// } +// } diff --git a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/enums/AdmissionControlModeTests.java b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/enums/AdmissionControlModeTests.java index 98c0f3c7cf24c..efed1b00c30a0 100644 --- a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/enums/AdmissionControlModeTests.java +++ b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/enums/AdmissionControlModeTests.java @@ -6,24 +6,24 @@ * compatible open source license. */ -package org.opensearch.ratelimitting.admissioncontrol.enums; - -import org.opensearch.test.OpenSearchTestCase; - -public class AdmissionControlModeTests extends OpenSearchTestCase { - - public void testValidActionType() { - assertEquals(AdmissionControlMode.DISABLED.getMode(), "disabled"); - assertEquals(AdmissionControlMode.ENFORCED.getMode(), "enforced"); - assertEquals(AdmissionControlMode.MONITOR.getMode(), "monitor_only"); - assertEquals(AdmissionControlMode.fromName("disabled"), AdmissionControlMode.DISABLED); - assertEquals(AdmissionControlMode.fromName("enforced"), AdmissionControlMode.ENFORCED); - assertEquals(AdmissionControlMode.fromName("monitor_only"), AdmissionControlMode.MONITOR); - } - - public void testInValidActionType() { - String name = "TEST"; - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> AdmissionControlMode.fromName(name)); - assertEquals(ex.getMessage(), "Invalid AdmissionControlMode: " + name); - } -} +// package org.opensearch.ratelimitting.admissioncontrol.enums; +// +// import org.opensearch.test.OpenSearchTestCase; +// +// public class AdmissionControlModeTests extends OpenSearchTestCase { +// +// public void testValidActionType() { +// assertEquals(AdmissionControlMode.DISABLED.getMode(), "disabled"); +// assertEquals(AdmissionControlMode.ENFORCED.getMode(), "enforced"); +// assertEquals(AdmissionControlMode.MONITOR.getMode(), "monitor_only"); +// assertEquals(AdmissionControlMode.fromName("disabled"), AdmissionControlMode.DISABLED); +// assertEquals(AdmissionControlMode.fromName("enforced"), AdmissionControlMode.ENFORCED); +// assertEquals(AdmissionControlMode.fromName("monitor_only"), AdmissionControlMode.MONITOR); +// } +// +// public void testInValidActionType() { +// String name = "TEST"; +// IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> AdmissionControlMode.fromName(name)); +// assertEquals(ex.getMessage(), "Invalid AdmissionControlMode: " + name); +// } +// } diff --git a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/enums/TransportActionTypeTests.java b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/enums/TransportActionTypeTests.java index 02f582c26f54e..5953ca208601f 100644 --- a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/enums/TransportActionTypeTests.java +++ b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/enums/TransportActionTypeTests.java @@ -6,22 +6,22 @@ * compatible open source license. */ -package org.opensearch.ratelimitting.admissioncontrol.enums; - -import org.opensearch.test.OpenSearchTestCase; - -public class TransportActionTypeTests extends OpenSearchTestCase { - - public void testValidActionType() { - assertEquals(TransportActionType.SEARCH.getType(), "search"); - assertEquals(TransportActionType.INDEXING.getType(), "indexing"); - assertEquals(TransportActionType.fromName("search"), TransportActionType.SEARCH); - assertEquals(TransportActionType.fromName("indexing"), TransportActionType.INDEXING); - } - - public void testInValidActionType() { - String name = "test"; - IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> TransportActionType.fromName(name)); - assertEquals(ex.getMessage(), "Not Supported TransportAction Type: " + name); - } -} +// package org.opensearch.ratelimitting.admissioncontrol.enums; +// +// import org.opensearch.test.OpenSearchTestCase; +// +// public class TransportActionTypeTests extends OpenSearchTestCase { +// +// public void testValidActionType() { +// assertEquals(TransportActionType.SEARCH.getType(), "search"); +// assertEquals(TransportActionType.INDEXING.getType(), "indexing"); +// assertEquals(TransportActionType.fromName("search"), TransportActionType.SEARCH); +// assertEquals(TransportActionType.fromName("indexing"), TransportActionType.INDEXING); +// } +// +// public void testInValidActionType() { +// String name = "test"; +// IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> TransportActionType.fromName(name)); +// assertEquals(ex.getMessage(), "Not Supported TransportAction Type: " + name); +// } +// } diff --git a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/settings/CPUBasedAdmissionControlSettingsTests.java b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/settings/CPUBasedAdmissionControlSettingsTests.java index 43103926a69a2..fbb6094bb7a72 100644 --- a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/settings/CPUBasedAdmissionControlSettingsTests.java +++ b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/settings/CPUBasedAdmissionControlSettingsTests.java @@ -6,148 +6,148 @@ * compatible open source license. */ -package org.opensearch.ratelimitting.admissioncontrol.settings; - -import org.opensearch.cluster.service.ClusterService; -import org.opensearch.common.settings.ClusterSettings; -import org.opensearch.common.settings.Setting; -import org.opensearch.common.settings.Settings; -import org.opensearch.ratelimitting.admissioncontrol.enums.AdmissionControlMode; -import org.opensearch.test.OpenSearchTestCase; -import org.opensearch.threadpool.TestThreadPool; -import org.opensearch.threadpool.ThreadPool; - -import java.util.Arrays; -import java.util.Set; - -public class CPUBasedAdmissionControlSettingsTests extends OpenSearchTestCase { - private ClusterService clusterService; - private ThreadPool threadPool; - - @Override - public void setUp() throws Exception { - super.setUp(); - threadPool = new TestThreadPool("admission_controller_settings_test"); - clusterService = new ClusterService( - Settings.EMPTY, - new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), - threadPool - ); - } - - @Override - public void tearDown() throws Exception { - super.tearDown(); - threadPool.shutdownNow(); - } - - public void testSettingsExists() { - Set> settings = ClusterSettings.BUILT_IN_CLUSTER_SETTINGS; - assertTrue( - "All the cpu based admission controller settings should be supported built in settings", - settings.containsAll( - Arrays.asList( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE, - CPUBasedAdmissionControllerSettings.SEARCH_CPU_USAGE_LIMIT, - CPUBasedAdmissionControllerSettings.INDEXING_CPU_USAGE_LIMIT - ) - ) - ); - } - - public void testDefaultSettings() { - CPUBasedAdmissionControllerSettings cpuBasedAdmissionControllerSettings = new CPUBasedAdmissionControllerSettings( - clusterService.getClusterSettings(), - Settings.EMPTY - ); - long percent = 95; - assertEquals(cpuBasedAdmissionControllerSettings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.DISABLED); - assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), percent); - assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), percent); - assertEquals(cpuBasedAdmissionControllerSettings.getTransportActionsList(), Arrays.asList("indexing", "search")); - } - - public void testGetConfiguredSettings() { - long percent = 95; - long indexingPercent = 85; - Settings settings = Settings.builder() - .put( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), - AdmissionControlMode.ENFORCED.getMode() - ) - .put(CPUBasedAdmissionControllerSettings.INDEXING_CPU_USAGE_LIMIT.getKey(), indexingPercent) - .build(); - - CPUBasedAdmissionControllerSettings cpuBasedAdmissionControllerSettings = new CPUBasedAdmissionControllerSettings( - clusterService.getClusterSettings(), - settings - ); - assertEquals(cpuBasedAdmissionControllerSettings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.ENFORCED); - assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), percent); - assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), indexingPercent); - } - - public void testUpdateAfterGetDefaultSettings() { - long percent = 95; - long searchPercent = 80; - CPUBasedAdmissionControllerSettings cpuBasedAdmissionControllerSettings = new CPUBasedAdmissionControllerSettings( - clusterService.getClusterSettings(), - Settings.EMPTY - ); - Settings settings = Settings.builder() - .put( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), - AdmissionControlMode.ENFORCED.getMode() - ) - .put(CPUBasedAdmissionControllerSettings.SEARCH_CPU_USAGE_LIMIT.getKey(), searchPercent) - .build(); - clusterService.getClusterSettings().applySettings(settings); - assertEquals(cpuBasedAdmissionControllerSettings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.ENFORCED); - assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), searchPercent); - assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), percent); - } - - public void testUpdateAfterGetConfiguredSettings() { - long percent = 95; - long indexingPercent = 85; - long searchPercent = 80; - Settings settings = Settings.builder() - .put( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), - AdmissionControlMode.ENFORCED.getMode() - ) - .put(CPUBasedAdmissionControllerSettings.SEARCH_CPU_USAGE_LIMIT.getKey(), searchPercent) - .build(); - - CPUBasedAdmissionControllerSettings cpuBasedAdmissionControllerSettings = new CPUBasedAdmissionControllerSettings( - clusterService.getClusterSettings(), - settings - ); - assertEquals(cpuBasedAdmissionControllerSettings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.ENFORCED); - assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), searchPercent); - assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), percent); - - Settings updatedSettings = Settings.builder() - .put( - CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), - AdmissionControlMode.MONITOR.getMode() - ) - .put(CPUBasedAdmissionControllerSettings.INDEXING_CPU_USAGE_LIMIT.getKey(), indexingPercent) - .build(); - clusterService.getClusterSettings().applySettings(updatedSettings); - assertEquals(cpuBasedAdmissionControllerSettings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.MONITOR); - assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), searchPercent); - assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), indexingPercent); - - searchPercent = 70; - - updatedSettings = Settings.builder() - .put(updatedSettings) - .put(CPUBasedAdmissionControllerSettings.SEARCH_CPU_USAGE_LIMIT.getKey(), searchPercent) - .build(); - clusterService.getClusterSettings().applySettings(updatedSettings); - - assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), searchPercent); - assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), indexingPercent); - } -} +// package org.opensearch.ratelimitting.admissioncontrol.settings; +// +// import org.opensearch.cluster.service.ClusterService; +// import org.opensearch.common.settings.ClusterSettings; +// import org.opensearch.common.settings.Setting; +// import org.opensearch.common.settings.Settings; +// import org.opensearch.ratelimitting.admissioncontrol.enums.AdmissionControlMode; +// import org.opensearch.test.OpenSearchTestCase; +// import org.opensearch.threadpool.TestThreadPool; +// import org.opensearch.threadpool.ThreadPool; +// +// import java.util.Arrays; +// import java.util.Set; +// +// public class CPUBasedAdmissionControlSettingsTests extends OpenSearchTestCase { +// private ClusterService clusterService; +// private ThreadPool threadPool; +// +// @Override +// public void setUp() throws Exception { +// super.setUp(); +// threadPool = new TestThreadPool("admission_controller_settings_test"); +// clusterService = new ClusterService( +// Settings.EMPTY, +// new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS), +// threadPool +// ); +// } +// +// @Override +// public void tearDown() throws Exception { +// super.tearDown(); +// threadPool.shutdownNow(); +// } +// +// public void testSettingsExists() { +// Set> settings = ClusterSettings.BUILT_IN_CLUSTER_SETTINGS; +// assertTrue( +// "All the cpu based admission controller settings should be supported built in settings", +// settings.containsAll( +// Arrays.asList( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE, +// CPUBasedAdmissionControllerSettings.SEARCH_CPU_USAGE_LIMIT, +// CPUBasedAdmissionControllerSettings.INDEXING_CPU_USAGE_LIMIT +// ) +// ) +// ); +// } +// +// public void testDefaultSettings() { +// CPUBasedAdmissionControllerSettings cpuBasedAdmissionControllerSettings = new CPUBasedAdmissionControllerSettings( +// clusterService.getClusterSettings(), +// Settings.EMPTY +// ); +// long percent = 95; +// assertEquals(cpuBasedAdmissionControllerSettings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.DISABLED); +// assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), percent); +// assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), percent); +// assertEquals(cpuBasedAdmissionControllerSettings.getTransportActionsList(), Arrays.asList("indexing", "search")); +// } +// +// public void testGetConfiguredSettings() { +// long percent = 95; +// long indexingPercent = 85; +// Settings settings = Settings.builder() +// .put( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), +// AdmissionControlMode.ENFORCED.getMode() +// ) +// .put(CPUBasedAdmissionControllerSettings.INDEXING_CPU_USAGE_LIMIT.getKey(), indexingPercent) +// .build(); +// +// CPUBasedAdmissionControllerSettings cpuBasedAdmissionControllerSettings = new CPUBasedAdmissionControllerSettings( +// clusterService.getClusterSettings(), +// settings +// ); +// assertEquals(cpuBasedAdmissionControllerSettings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.ENFORCED); +// assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), percent); +// assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), indexingPercent); +// } +// +// public void testUpdateAfterGetDefaultSettings() { +// long percent = 95; +// long searchPercent = 80; +// CPUBasedAdmissionControllerSettings cpuBasedAdmissionControllerSettings = new CPUBasedAdmissionControllerSettings( +// clusterService.getClusterSettings(), +// Settings.EMPTY +// ); +// Settings settings = Settings.builder() +// .put( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), +// AdmissionControlMode.ENFORCED.getMode() +// ) +// .put(CPUBasedAdmissionControllerSettings.SEARCH_CPU_USAGE_LIMIT.getKey(), searchPercent) +// .build(); +// clusterService.getClusterSettings().applySettings(settings); +// assertEquals(cpuBasedAdmissionControllerSettings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.ENFORCED); +// assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), searchPercent); +// assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), percent); +// } +// +// public void testUpdateAfterGetConfiguredSettings() { +// long percent = 95; +// long indexingPercent = 85; +// long searchPercent = 80; +// Settings settings = Settings.builder() +// .put( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), +// AdmissionControlMode.ENFORCED.getMode() +// ) +// .put(CPUBasedAdmissionControllerSettings.SEARCH_CPU_USAGE_LIMIT.getKey(), searchPercent) +// .build(); +// +// CPUBasedAdmissionControllerSettings cpuBasedAdmissionControllerSettings = new CPUBasedAdmissionControllerSettings( +// clusterService.getClusterSettings(), +// settings +// ); +// assertEquals(cpuBasedAdmissionControllerSettings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.ENFORCED); +// assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), searchPercent); +// assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), percent); +// +// Settings updatedSettings = Settings.builder() +// .put( +// CPUBasedAdmissionControllerSettings.CPU_BASED_ADMISSION_CONTROLLER_TRANSPORT_LAYER_MODE.getKey(), +// AdmissionControlMode.MONITOR.getMode() +// ) +// .put(CPUBasedAdmissionControllerSettings.INDEXING_CPU_USAGE_LIMIT.getKey(), indexingPercent) +// .build(); +// clusterService.getClusterSettings().applySettings(updatedSettings); +// assertEquals(cpuBasedAdmissionControllerSettings.getTransportLayerAdmissionControllerMode(), AdmissionControlMode.MONITOR); +// assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), searchPercent); +// assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), indexingPercent); +// +// searchPercent = 70; +// +// updatedSettings = Settings.builder() +// .put(updatedSettings) +// .put(CPUBasedAdmissionControllerSettings.SEARCH_CPU_USAGE_LIMIT.getKey(), searchPercent) +// .build(); +// clusterService.getClusterSettings().applySettings(updatedSettings); +// +// assertEquals(cpuBasedAdmissionControllerSettings.getSearchCPULimit().longValue(), searchPercent); +// assertEquals(cpuBasedAdmissionControllerSettings.getIndexingCPULimit().longValue(), indexingPercent); +// } +// } diff --git a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/transport/AdmissionControlTransportHandlerTests.java b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/transport/AdmissionControlTransportHandlerTests.java index 15d60157bda29..3669c8a4c6a78 100644 --- a/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/transport/AdmissionControlTransportHandlerTests.java +++ b/server/src/test/java/org/opensearch/ratelimitting/admissioncontrol/transport/AdmissionControlTransportHandlerTests.java @@ -6,84 +6,84 @@ * compatible open source license. */ -package org.opensearch.ratelimitting.admissioncontrol.transport; - -import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException; -import org.opensearch.ratelimitting.admissioncontrol.AdmissionControlService; -import org.opensearch.tasks.Task; -import org.opensearch.test.OpenSearchTestCase; -import org.opensearch.transport.TransportChannel; -import org.opensearch.transport.TransportRequest; -import org.opensearch.transport.TransportRequestHandler; - -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; - -public class AdmissionControlTransportHandlerTests extends OpenSearchTestCase { - AdmissionControlTransportHandler admissionControlTransportHandler; - - public void testHandlerInvoked() throws Exception { - String action = "TEST"; - InterceptingRequestHandler handler = new InterceptingRequestHandler<>(action); - admissionControlTransportHandler = new AdmissionControlTransportHandler( - action, - handler, - mock(AdmissionControlService.class), - false - ); - admissionControlTransportHandler.messageReceived(mock(TransportRequest.class), mock(TransportChannel.class), mock(Task.class)); - assertEquals(1, handler.count); - } - - public void testHandlerInvokedRejectedException() throws Exception { - String action = "TEST"; - AdmissionControlService admissionControlService = mock(AdmissionControlService.class); - doThrow(new OpenSearchRejectedExecutionException()).when(admissionControlService).applyTransportAdmissionControl(action); - InterceptingRequestHandler handler = new InterceptingRequestHandler<>(action); - admissionControlTransportHandler = new AdmissionControlTransportHandler( - action, - handler, - admissionControlService, - false - ); - admissionControlTransportHandler.messageReceived(mock(TransportRequest.class), mock(TransportChannel.class), mock(Task.class)); - assertEquals(0, handler.count); - handler.messageReceived(mock(TransportRequest.class), mock(TransportChannel.class), mock(Task.class)); - assertEquals(1, handler.count); - } - - public void testHandlerInvokedRandomException() throws Exception { - String action = "TEST"; - AdmissionControlService admissionControlService = mock(AdmissionControlService.class); - doThrow(new NullPointerException()).when(admissionControlService).applyTransportAdmissionControl(action); - InterceptingRequestHandler handler = new InterceptingRequestHandler<>(action); - admissionControlTransportHandler = new AdmissionControlTransportHandler( - action, - handler, - admissionControlService, - false - ); - try { - admissionControlTransportHandler.messageReceived(mock(TransportRequest.class), mock(TransportChannel.class), mock(Task.class)); - } catch (Exception exception) { - assertEquals(0, handler.count); - handler.messageReceived(mock(TransportRequest.class), mock(TransportChannel.class), mock(Task.class)); - } - assertEquals(1, handler.count); - } - - private class InterceptingRequestHandler implements TransportRequestHandler { - private final String action; - public int count; - - public InterceptingRequestHandler(String action) { - this.action = action; - this.count = 0; - } - - @Override - public void messageReceived(T request, TransportChannel channel, Task task) throws Exception { - this.count = this.count + 1; - } - } -} +// package org.opensearch.ratelimitting.admissioncontrol.transport; +// +// import org.opensearch.core.concurrency.OpenSearchRejectedExecutionException; +// import org.opensearch.ratelimitting.admissioncontrol.AdmissionControlService; +// import org.opensearch.tasks.Task; +// import org.opensearch.test.OpenSearchTestCase; +// import org.opensearch.transport.TransportChannel; +// import org.opensearch.transport.TransportRequest; +// import org.opensearch.transport.TransportRequestHandler; +// +// import static org.mockito.Mockito.doThrow; +// import static org.mockito.Mockito.mock; +// +// public class AdmissionControlTransportHandlerTests extends OpenSearchTestCase { +// AdmissionControlTransportHandler admissionControlTransportHandler; +// +// public void testHandlerInvoked() throws Exception { +// String action = "TEST"; +// InterceptingRequestHandler handler = new InterceptingRequestHandler<>(action); +// admissionControlTransportHandler = new AdmissionControlTransportHandler( +// action, +// handler, +// mock(AdmissionControlService.class), +// false +// ); +// admissionControlTransportHandler.messageReceived(mock(TransportRequest.class), mock(TransportChannel.class), mock(Task.class)); +// assertEquals(1, handler.count); +// } +// +// public void testHandlerInvokedRejectedException() throws Exception { +// String action = "TEST"; +// AdmissionControlService admissionControlService = mock(AdmissionControlService.class); +// doThrow(new OpenSearchRejectedExecutionException()).when(admissionControlService).applyTransportAdmissionControl(action); +// InterceptingRequestHandler handler = new InterceptingRequestHandler<>(action); +// admissionControlTransportHandler = new AdmissionControlTransportHandler( +// action, +// handler, +// admissionControlService, +// false +// ); +// admissionControlTransportHandler.messageReceived(mock(TransportRequest.class), mock(TransportChannel.class), mock(Task.class)); +// assertEquals(0, handler.count); +// handler.messageReceived(mock(TransportRequest.class), mock(TransportChannel.class), mock(Task.class)); +// assertEquals(1, handler.count); +// } +// +// public void testHandlerInvokedRandomException() throws Exception { +// String action = "TEST"; +// AdmissionControlService admissionControlService = mock(AdmissionControlService.class); +// doThrow(new NullPointerException()).when(admissionControlService).applyTransportAdmissionControl(action); +// InterceptingRequestHandler handler = new InterceptingRequestHandler<>(action); +// admissionControlTransportHandler = new AdmissionControlTransportHandler( +// action, +// handler, +// admissionControlService, +// false +// ); +// try { +// admissionControlTransportHandler.messageReceived(mock(TransportRequest.class), mock(TransportChannel.class), mock(Task.class)); +// } catch (Exception exception) { +// assertEquals(0, handler.count); +// handler.messageReceived(mock(TransportRequest.class), mock(TransportChannel.class), mock(Task.class)); +// } +// assertEquals(1, handler.count); +// } +// +// private class InterceptingRequestHandler implements TransportRequestHandler { +// private final String action; +// public int count; +// +// public InterceptingRequestHandler(String action) { +// this.action = action; +// this.count = 0; +// } +// +// @Override +// public void messageReceived(T request, TransportChannel channel, Task task) throws Exception { +// this.count = this.count + 1; +// } +// } +// } diff --git a/server/src/test/java/org/opensearch/transport/RemoteClusterClientTests.java b/server/src/test/java/org/opensearch/transport/RemoteClusterClientTests.java index f3b7f9916d460..e7fe9ac5fe3d0 100644 --- a/server/src/test/java/org/opensearch/transport/RemoteClusterClientTests.java +++ b/server/src/test/java/org/opensearch/transport/RemoteClusterClientTests.java @@ -94,17 +94,24 @@ public void testConnectAndExecuteRequest() throws Exception { service.acceptIncomingRequests(); logger.info("now accepting incoming requests on local transport"); RemoteClusterService remoteClusterService = service.getRemoteClusterService(); + logger.info("remote cluster service is able to get"); assertBusy(() -> { assertTrue(remoteClusterService.isRemoteNodeConnected("test", remoteNode)); }, 10, TimeUnit.SECONDS); + logger.info("remote cluster service is remote node connected"); Client client = remoteClusterService.getRemoteClusterClient(threadPool, "test"); + logger.info("remote cluster client is able to create"); ClusterStateResponse clusterStateResponse = client.admin().cluster().prepareState().execute().get(); + logger.info("able to get cluster state response"); assertNotNull(clusterStateResponse); assertEquals("foo_bar_cluster", clusterStateResponse.getState().getClusterName().value()); + logger.info("before triggering scroll"); // also test a failure, there is no handler for scroll registered ActionNotFoundTransportException ex = expectThrows( ActionNotFoundTransportException.class, () -> client.prepareSearchScroll("").get() ); + logger.info("after triggering scroll"); assertEquals("No handler for action [indices:data/read/scroll]", ex.getMessage()); + logger.info("Test execution successful"); } } }