Search in sources :

Example 1 with UsageService

use of org.opensearch.usage.UsageService in project OpenSearch by opensearch-project.

the class ActionModuleTests method testPluginCanRegisterRestHandler.

public void testPluginCanRegisterRestHandler() {
    class FakeHandler implements RestHandler {

        @Override
        public List<Route> routes() {
            return singletonList(new Route(Method.GET, "/_dummy"));
        }

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
        }
    }
    ActionPlugin registersFakeHandler = new ActionPlugin() {

        @Override
        public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
            return singletonList(new FakeHandler());
        }
    };
    SettingsModule settings = new SettingsModule(Settings.EMPTY);
    ThreadPool threadPool = new TestThreadPool(getTestName());
    try {
        UsageService usageService = new UsageService();
        ActionModule actionModule = new ActionModule(settings.getSettings(), new IndexNameExpressionResolver(threadPool.getThreadContext()), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool, singletonList(registersFakeHandler), null, null, usageService, null);
        actionModule.initRestHandlers(null);
        // At this point the easiest way to confirm that a handler is loaded is to try to register another one on top of it and to fail
        Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.getRestController().registerHandler(new RestHandler() {

            @Override
            public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
            }

            @Override
            public List<Route> routes() {
                return singletonList(new Route(Method.GET, "/_dummy"));
            }
        }));
        assertThat(e.getMessage(), startsWith("Cannot replace existing handler for [/_dummy] for method: GET"));
    } finally {
        threadPool.shutdown();
    }
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) ClusterSettings(org.opensearch.common.settings.ClusterSettings) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) ActionPlugin(org.opensearch.plugins.ActionPlugin) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) RestChannel(org.opensearch.rest.RestChannel) RestController(org.opensearch.rest.RestController) TestThreadPool(org.opensearch.threadpool.TestThreadPool) IOException(java.io.IOException) SettingsFilter(org.opensearch.common.settings.SettingsFilter) RestRequest(org.opensearch.rest.RestRequest) RestHandler(org.opensearch.rest.RestHandler) UsageService(org.opensearch.usage.UsageService) SettingsModule(org.opensearch.common.settings.SettingsModule) Supplier(java.util.function.Supplier) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Example 2 with UsageService

use of org.opensearch.usage.UsageService in project OpenSearch by opensearch-project.

the class ActionModuleTests method testPluginCantOverwriteBuiltinRestHandler.

public void testPluginCantOverwriteBuiltinRestHandler() throws IOException {
    ActionPlugin dupsMainAction = new ActionPlugin() {

        @Override
        public List<RestHandler> getRestHandlers(Settings settings, RestController restController, ClusterSettings clusterSettings, IndexScopedSettings indexScopedSettings, SettingsFilter settingsFilter, IndexNameExpressionResolver indexNameExpressionResolver, Supplier<DiscoveryNodes> nodesInCluster) {
            return singletonList(new RestMainAction() {

                @Override
                public String getName() {
                    return "duplicated_" + super.getName();
                }
            });
        }
    };
    SettingsModule settings = new SettingsModule(Settings.EMPTY);
    ThreadPool threadPool = new TestThreadPool(getTestName());
    try {
        UsageService usageService = new UsageService();
        ActionModule actionModule = new ActionModule(settings.getSettings(), new IndexNameExpressionResolver(threadPool.getThreadContext()), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), threadPool, singletonList(dupsMainAction), null, null, usageService, null);
        Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.initRestHandlers(null));
        assertThat(e.getMessage(), startsWith("Cannot replace existing handler for [/] for method: GET"));
    } finally {
        threadPool.shutdown();
    }
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) ActionPlugin(org.opensearch.plugins.ActionPlugin) ThreadPool(org.opensearch.threadpool.ThreadPool) TestThreadPool(org.opensearch.threadpool.TestThreadPool) RestController(org.opensearch.rest.RestController) TestThreadPool(org.opensearch.threadpool.TestThreadPool) IOException(java.io.IOException) SettingsFilter(org.opensearch.common.settings.SettingsFilter) RestMainAction(org.opensearch.rest.action.RestMainAction) RestHandler(org.opensearch.rest.RestHandler) UsageService(org.opensearch.usage.UsageService) SettingsModule(org.opensearch.common.settings.SettingsModule) Supplier(java.util.function.Supplier) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) Settings(org.opensearch.common.settings.Settings)

Example 3 with UsageService

use of org.opensearch.usage.UsageService in project OpenSearch by opensearch-project.

the class ActionModuleTests method testSetupRestHandlerContainsKnownBuiltin.

public void testSetupRestHandlerContainsKnownBuiltin() {
    SettingsModule settings = new SettingsModule(Settings.EMPTY);
    UsageService usageService = new UsageService();
    ActionModule actionModule = new ActionModule(settings.getSettings(), new IndexNameExpressionResolver(new ThreadContext(Settings.EMPTY)), settings.getIndexScopedSettings(), settings.getClusterSettings(), settings.getSettingsFilter(), null, emptyList(), null, null, usageService, null);
    actionModule.initRestHandlers(null);
    // At this point the easiest way to confirm that a handler is loaded is to try to register another one on top of it and to fail
    Exception e = expectThrows(IllegalArgumentException.class, () -> actionModule.getRestController().registerHandler(new RestHandler() {

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
        }

        @Override
        public List<Route> routes() {
            return singletonList(new Route(Method.GET, "/"));
        }
    }));
    assertThat(e.getMessage(), startsWith("Cannot replace existing handler for [/] for method: GET"));
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) UsageService(org.opensearch.usage.UsageService) RestHandler(org.opensearch.rest.RestHandler) RestRequest(org.opensearch.rest.RestRequest) SettingsModule(org.opensearch.common.settings.SettingsModule) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) RestChannel(org.opensearch.rest.RestChannel) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) IOException(java.io.IOException)

Example 4 with UsageService

use of org.opensearch.usage.UsageService in project OpenSearch by opensearch-project.

the class RestActionTestCase method setUpController.

@Before
public void setUpController() {
    verifyingClient = new VerifyingClient(this.getTestName());
    controller = new RestController(Collections.emptySet(), null, verifyingClient, new NoneCircuitBreakerService(), new UsageService());
}
Also used : UsageService(org.opensearch.usage.UsageService) RestController(org.opensearch.rest.RestController) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService) Before(org.junit.Before)

Example 5 with UsageService

use of org.opensearch.usage.UsageService 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)

Aggregations

UsageService (org.opensearch.usage.UsageService)6 ClusterSettings (org.opensearch.common.settings.ClusterSettings)4 IOException (java.io.IOException)3 NodeClient (org.opensearch.client.node.NodeClient)3 IndexNameExpressionResolver (org.opensearch.cluster.metadata.IndexNameExpressionResolver)3 Settings (org.opensearch.common.settings.Settings)3 SettingsModule (org.opensearch.common.settings.SettingsModule)3 RestController (org.opensearch.rest.RestController)3 RestHandler (org.opensearch.rest.RestHandler)3 Supplier (java.util.function.Supplier)2 Before (org.junit.Before)2 IndexScopedSettings (org.opensearch.common.settings.IndexScopedSettings)2 SettingsFilter (org.opensearch.common.settings.SettingsFilter)2 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)2 HierarchyCircuitBreakerService (org.opensearch.indices.breaker.HierarchyCircuitBreakerService)2 ActionPlugin (org.opensearch.plugins.ActionPlugin)2 RestChannel (org.opensearch.rest.RestChannel)2 RestRequest (org.opensearch.rest.RestRequest)2 TestThreadPool (org.opensearch.threadpool.TestThreadPool)2 ThreadPool (org.opensearch.threadpool.ThreadPool)2