Search in sources :

Example 16 with CircuitBreakerService

use of org.opensearch.indices.breaker.CircuitBreakerService in project OpenSearch by opensearch-project.

the class AggregatorBaseTests method mockSearchContext.

private SearchContext mockSearchContext(Query query) {
    SearchContext searchContext = mock(SearchContext.class);
    when(searchContext.query()).thenReturn(query);
    BigArrays mockBigArrays = mock(BigArrays.class);
    CircuitBreakerService mockCBS = mock(CircuitBreakerService.class);
    when(mockCBS.getBreaker(CircuitBreaker.REQUEST)).thenReturn(mock(CircuitBreaker.class));
    when(mockBigArrays.breakerService()).thenReturn(mockCBS);
    when(searchContext.bigArrays()).thenReturn(mockBigArrays);
    return searchContext;
}
Also used : BigArrays(org.opensearch.common.util.BigArrays) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) SearchContext(org.opensearch.search.internal.SearchContext) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService)

Example 17 with CircuitBreakerService

use of org.opensearch.indices.breaker.CircuitBreakerService in project OpenSearch by opensearch-project.

the class RestHttpResponseHeadersTests method testUnsupportedMethodResponseHttpHeader.

/**
 * For requests to a valid REST endpoint using an unsupported HTTP method,
 * verify that a 405 HTTP response code is returned, and that the response
 * 'Allow' header includes a list of valid HTTP methods for the endpoint
 * (see
 * <a href="https://tools.ietf.org/html/rfc2616#section-10.4.6">HTTP/1.1 -
 * 10.4.6 - 405 Method Not Allowed</a>).
 */
public void testUnsupportedMethodResponseHttpHeader() throws Exception {
    /*
         * Generate a random set of candidate valid HTTP methods to register
         * with the test RestController endpoint. Enums are returned in the
         * order they are declared, so the first step is to shuffle the HTTP
         * method list, passing in the RandomizedContext's Random instance,
         * before picking out a candidate sublist.
         */
    List<RestRequest.Method> validHttpMethodArray = new ArrayList<RestRequest.Method>(Arrays.asList(RestRequest.Method.values()));
    validHttpMethodArray.remove(RestRequest.Method.OPTIONS);
    Collections.shuffle(validHttpMethodArray, random());
    /*
         * The upper bound of the potential sublist is one less than the size of
         * the array, so we are guaranteed at least one invalid method to test.
         */
    validHttpMethodArray = validHttpMethodArray.subList(0, randomIntBetween(1, validHttpMethodArray.size() - 1));
    assert (validHttpMethodArray.size() > 0);
    assert (validHttpMethodArray.size() < RestRequest.Method.values().length);
    /*
         * Generate an inverse list of one or more candidate invalid HTTP
         * methods, so we have a candidate method to fire at the test endpoint.
         */
    List<RestRequest.Method> invalidHttpMethodArray = new ArrayList<RestRequest.Method>(Arrays.asList(RestRequest.Method.values()));
    invalidHttpMethodArray.removeAll(validHttpMethodArray);
    // Remove OPTIONS, or else we'll get a 200 instead of 405
    invalidHttpMethodArray.remove(RestRequest.Method.OPTIONS);
    assert (invalidHttpMethodArray.size() > 0);
    // Initialize test candidate RestController
    CircuitBreakerService circuitBreakerService = new HierarchyCircuitBreakerService(Settings.EMPTY, Collections.emptyList(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
    final Settings settings = Settings.EMPTY;
    UsageService usageService = new UsageService();
    RestController restController = new RestController(Collections.emptySet(), null, null, circuitBreakerService, usageService);
    // A basic RestHandler handles requests to the endpoint
    RestHandler restHandler = new RestHandler() {

        @Override
        public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws Exception {
            channel.sendResponse(new TestResponse());
        }
    };
    // Register valid test handlers with test RestController
    for (RestRequest.Method method : validHttpMethodArray) {
        restController.registerHandler(method, "/", restHandler);
    }
    // Generate a test request with an invalid HTTP method
    FakeRestRequest.Builder fakeRestRequestBuilder = new FakeRestRequest.Builder(xContentRegistry());
    fakeRestRequestBuilder.withMethod(invalidHttpMethodArray.get(0));
    RestRequest restRequest = fakeRestRequestBuilder.build();
    // Send the request and verify the response status code
    FakeRestChannel restChannel = new FakeRestChannel(restRequest, false, 1);
    restController.dispatchRequest(restRequest, restChannel, new ThreadContext(Settings.EMPTY));
    assertThat(restChannel.capturedResponse().status().getStatus(), is(405));
    /*
         * Verify the response allow header contains the valid methods for the
         * test endpoint
         */
    assertThat(restChannel.capturedResponse().getHeaders().get("Allow"), notNullValue());
    String responseAllowHeader = restChannel.capturedResponse().getHeaders().get("Allow").get(0);
    List<String> responseAllowHeaderArray = Arrays.asList(responseAllowHeader.split(","));
    assertThat(responseAllowHeaderArray.size(), is(validHttpMethodArray.size()));
    assertThat(responseAllowHeaderArray, containsInAnyOrder(getMethodNameStringArray(validHttpMethodArray).toArray()));
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) ClusterSettings(org.opensearch.common.settings.ClusterSettings) ArrayList(java.util.ArrayList) ThreadContext(org.opensearch.common.util.concurrent.ThreadContext) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) FakeRestRequest(org.opensearch.test.rest.FakeRestRequest) UsageService(org.opensearch.usage.UsageService) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) FakeRestChannel(org.opensearch.test.rest.FakeRestChannel) Settings(org.opensearch.common.settings.Settings) ClusterSettings(org.opensearch.common.settings.ClusterSettings)

Aggregations

CircuitBreakerService (org.opensearch.indices.breaker.CircuitBreakerService)17 Settings (org.opensearch.common.settings.Settings)10 BigArrays (org.opensearch.common.util.BigArrays)8 ThreadPool (org.opensearch.threadpool.ThreadPool)7 NamedWriteableRegistry (org.opensearch.common.io.stream.NamedWriteableRegistry)6 ClusterSettings (org.opensearch.common.settings.ClusterSettings)6 IOException (java.io.IOException)5 Map (java.util.Map)5 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)5 Collections (java.util.Collections)4 HashMap (java.util.HashMap)4 List (java.util.List)4 Supplier (java.util.function.Supplier)4 PageCacheRecycler (org.opensearch.common.util.PageCacheRecycler)4 MapperService (org.opensearch.index.mapper.MapperService)4 TestThreadPool (org.opensearch.threadpool.TestThreadPool)4 ArrayList (java.util.ArrayList)3 Objects (java.util.Objects)3 Set (java.util.Set)3 TimeUnit (java.util.concurrent.TimeUnit)3