Search in sources :

Example 1 with BaseRestHandler

use of org.opensearch.rest.BaseRestHandler in project OpenSearch by opensearch-project.

the class UsageService method addRestHandler.

/**
 * Add a REST handler to this service.
 *
 * @param handler the {@link BaseRestHandler} to add to the usage service.
 */
public void addRestHandler(BaseRestHandler handler) {
    Objects.requireNonNull(handler);
    if (handler.getName() == null) {
        throw new IllegalArgumentException("handler of type [" + handler.getClass().getName() + "] does not have a name");
    }
    final BaseRestHandler maybeHandler = handlers.put(handler.getName(), handler);
    /*
         * Handlers will be registered multiple times, once for each route that the handler handles. This means that we will see handlers
         * multiple times, so we do not have a conflict if we are seeing the same instance multiple times. So, we only reject if a handler
         * with the same name was registered before, and it is not the same instance as before.
         */
    if (maybeHandler != null && maybeHandler != handler) {
        final String message = String.format(Locale.ROOT, "handler of type [%s] conflicts with handler of type [%s] as they both have the same name [%s]", handler.getClass().getName(), maybeHandler.getClass().getName(), handler.getName());
        throw new IllegalArgumentException(message);
    }
}
Also used : BaseRestHandler(org.opensearch.rest.BaseRestHandler)

Example 2 with BaseRestHandler

use of org.opensearch.rest.BaseRestHandler in project OpenSearch by opensearch-project.

the class UsageServiceTests method testHandlerWithConflictingNamesButSameInstance.

/**
 * Test that we can add the same instance of a {@link org.opensearch.rest.RestHandler} to the {@link UsageService} multiple times.
 */
public void testHandlerWithConflictingNamesButSameInstance() {
    final UsageService service = new UsageService();
    final String name = randomAlphaOfLength(8);
    final BaseRestHandler first = new MockRestHandler(name);
    service.addRestHandler(first);
    // nothing bad ever happens to me
    service.addRestHandler(first);
}
Also used : AggregationUsageService(org.opensearch.search.aggregations.support.AggregationUsageService) BaseRestHandler(org.opensearch.rest.BaseRestHandler)

Example 3 with BaseRestHandler

use of org.opensearch.rest.BaseRestHandler in project OpenSearch by opensearch-project.

the class UsageServiceTests method testAHandlerWithNoName.

/**
 * Test that we can not add an instance of a {@link org.opensearch.rest.RestHandler} with no name to the {@link UsageService}.
 */
public void testAHandlerWithNoName() {
    final UsageService service = new UsageService();
    final BaseRestHandler horse = new MockRestHandler(null);
    final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> service.addRestHandler(horse));
    assertThat(e.getMessage(), equalTo("handler of type [org.opensearch.usage.UsageServiceTests$MockRestHandler] does not have a name"));
}
Also used : AggregationUsageService(org.opensearch.search.aggregations.support.AggregationUsageService) BaseRestHandler(org.opensearch.rest.BaseRestHandler)

Example 4 with BaseRestHandler

use of org.opensearch.rest.BaseRestHandler in project OpenSearch by opensearch-project.

the class UsageServiceTests method testHandlersWithConflictingNamesButDifferentInstances.

/**
 * Test that we can not add different instances of {@link org.opensearch.rest.RestHandler} with the same name to the
 * {@link UsageService}.
 */
public void testHandlersWithConflictingNamesButDifferentInstances() {
    final UsageService service = new UsageService();
    final String name = randomAlphaOfLength(8);
    final BaseRestHandler first = new MockRestHandler(name);
    final BaseRestHandler second = new MockRestHandler(name);
    service.addRestHandler(first);
    final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> service.addRestHandler(second));
    final String expected = String.format(Locale.ROOT, "handler of type [%s] conflicts with handler of type [%1$s] as they both have the same name [%s]", "org.opensearch.usage.UsageServiceTests$MockRestHandler", name);
    assertThat(e.getMessage(), equalTo(expected));
}
Also used : AggregationUsageService(org.opensearch.search.aggregations.support.AggregationUsageService) BaseRestHandler(org.opensearch.rest.BaseRestHandler)

Example 5 with BaseRestHandler

use of org.opensearch.rest.BaseRestHandler in project OpenSearch by opensearch-project.

the class UsageServiceTests method testRestUsage.

public void testRestUsage() throws Exception {
    RestRequest restRequest = new FakeRestRequest();
    BaseRestHandler handlerA = new MockRestHandler("a");
    BaseRestHandler handlerB = new MockRestHandler("b");
    BaseRestHandler handlerC = new MockRestHandler("c");
    BaseRestHandler handlerD = new MockRestHandler("d");
    BaseRestHandler handlerE = new MockRestHandler("e");
    BaseRestHandler handlerF = new MockRestHandler("f");
    UsageService usageService = new UsageService();
    usageService.addRestHandler(handlerA);
    usageService.addRestHandler(handlerB);
    usageService.addRestHandler(handlerC);
    usageService.addRestHandler(handlerD);
    usageService.addRestHandler(handlerE);
    usageService.addRestHandler(handlerF);
    try (NodeClient client = new NoOpNodeClient(this.getClass().getSimpleName() + "TestClient")) {
        handlerA.handleRequest(restRequest, null, client);
        handlerB.handleRequest(restRequest, null, client);
        handlerA.handleRequest(restRequest, null, client);
        handlerA.handleRequest(restRequest, null, client);
        handlerB.handleRequest(restRequest, null, client);
        handlerC.handleRequest(restRequest, null, client);
        handlerC.handleRequest(restRequest, null, client);
        handlerD.handleRequest(restRequest, null, client);
        handlerA.handleRequest(restRequest, null, client);
        handlerB.handleRequest(restRequest, null, client);
        handlerE.handleRequest(restRequest, null, client);
        handlerF.handleRequest(restRequest, null, client);
        handlerC.handleRequest(restRequest, null, client);
        handlerD.handleRequest(restRequest, null, client);
    }
    Map<String, Long> restUsage = usageService.getRestUsageStats();
    assertThat(restUsage, notNullValue());
    assertThat(restUsage.size(), equalTo(6));
    assertThat(restUsage.get("a"), equalTo(4L));
    assertThat(restUsage.get("b"), equalTo(3L));
    assertThat(restUsage.get("c"), equalTo(3L));
    assertThat(restUsage.get("d"), equalTo(2L));
    assertThat(restUsage.get("e"), equalTo(1L));
    assertThat(restUsage.get("f"), equalTo(1L));
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) RestRequest(org.opensearch.rest.RestRequest) AggregationUsageService(org.opensearch.search.aggregations.support.AggregationUsageService) BaseRestHandler(org.opensearch.rest.BaseRestHandler) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) NoOpNodeClient(org.opensearch.test.client.NoOpNodeClient)

Aggregations

BaseRestHandler (org.opensearch.rest.BaseRestHandler)5 AggregationUsageService (org.opensearch.search.aggregations.support.AggregationUsageService)4 NodeClient (org.opensearch.client.node.NodeClient)1 RestRequest (org.opensearch.rest.RestRequest)1 NoOpNodeClient (org.opensearch.test.client.NoOpNodeClient)1 FakeRestRequest (org.opensearch.test.rest.FakeRestRequest)1