Search in sources :

Example 1 with HierarchyCircuitBreakerService

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

the class BigArraysTests method testMaxSizeExceededOnResize.

public void testMaxSizeExceededOnResize() throws Exception {
    for (String type : Arrays.asList("Byte", "Int", "Long", "Float", "Double", "Object")) {
        final int maxSize = randomIntBetween(1 << 8, 1 << 14);
        HierarchyCircuitBreakerService hcbs = new HierarchyCircuitBreakerService(Settings.builder().put(REQUEST_CIRCUIT_BREAKER_LIMIT_SETTING.getKey(), maxSize, ByteSizeUnit.BYTES).put(HierarchyCircuitBreakerService.USE_REAL_MEMORY_USAGE_SETTING.getKey(), false).build(), Collections.emptyList(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
        BigArrays bigArrays = new BigArrays(null, hcbs, CircuitBreaker.REQUEST).withCircuitBreaking();
        Method create = BigArrays.class.getMethod("new" + type + "Array", long.class);
        final int size = scaledRandomIntBetween(10, maxSize / 16);
        BigArray array = (BigArray) create.invoke(bigArrays, size);
        Method resize = BigArrays.class.getMethod("resize", array.getClass().getInterfaces()[0], long.class);
        while (true) {
            long newSize = array.size() * 2;
            try {
                array = (BigArray) resize.invoke(bigArrays, array, newSize);
            } catch (InvocationTargetException e) {
                assertTrue(e.getCause() instanceof CircuitBreakingException);
                break;
            }
        }
        assertEquals(array.ramBytesUsed(), hcbs.getBreaker(CircuitBreaker.REQUEST).getUsed());
        array.close();
        assertEquals(0, hcbs.getBreaker(CircuitBreaker.REQUEST).getUsed());
    }
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with HierarchyCircuitBreakerService

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

the class BigArraysTests method newBigArraysInstance.

private BigArrays newBigArraysInstance(final long maxSize, final boolean withBreaking) {
    HierarchyCircuitBreakerService hcbs = new HierarchyCircuitBreakerService(Settings.builder().put(REQUEST_CIRCUIT_BREAKER_LIMIT_SETTING.getKey(), maxSize, ByteSizeUnit.BYTES).put(HierarchyCircuitBreakerService.USE_REAL_MEMORY_USAGE_SETTING.getKey(), false).build(), Collections.emptyList(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
    BigArrays bigArrays = new BigArrays(null, hcbs, CircuitBreaker.REQUEST);
    return (withBreaking ? bigArrays.withCircuitBreaking() : bigArrays);
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService)

Example 3 with HierarchyCircuitBreakerService

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

the class IndexShardTestCase method newShard.

/**
 * creates a new initializing shard.
 * @param routing                       shard routing to use
 * @param shardPath                     path to use for shard data
 * @param indexMetadata                 indexMetadata for the shard, including any mapping
 * @param storeProvider                 an optional custom store provider to use. If null a default file based store will be created
 * @param indexReaderWrapper            an optional wrapper to be used during search
 * @param globalCheckpointSyncer        callback for syncing global checkpoints
 * @param indexEventListener            index event listener
 * @param listeners                     an optional set of listeners to add to the shard
 */
protected IndexShard newShard(ShardRouting routing, ShardPath shardPath, IndexMetadata indexMetadata, @Nullable CheckedFunction<IndexSettings, Store, IOException> storeProvider, @Nullable CheckedFunction<DirectoryReader, DirectoryReader, IOException> indexReaderWrapper, @Nullable EngineFactory engineFactory, @Nullable EngineConfigFactory engineConfigFactory, Runnable globalCheckpointSyncer, RetentionLeaseSyncer retentionLeaseSyncer, IndexEventListener indexEventListener, IndexingOperationListener... listeners) throws IOException {
    final Settings nodeSettings = Settings.builder().put("node.name", routing.currentNodeId()).build();
    final IndexSettings indexSettings = new IndexSettings(indexMetadata, nodeSettings);
    final IndexShard indexShard;
    if (storeProvider == null) {
        storeProvider = is -> createStore(is, shardPath);
    }
    final Store store = storeProvider.apply(indexSettings);
    boolean success = false;
    try {
        IndexCache indexCache = new IndexCache(indexSettings, new DisabledQueryCache(indexSettings), null);
        MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), indexSettings.getSettings(), "index");
        mapperService.merge(indexMetadata, MapperService.MergeReason.MAPPING_RECOVERY);
        SimilarityService similarityService = new SimilarityService(indexSettings, null, Collections.emptyMap());
        final Engine.Warmer warmer = createTestWarmer(indexSettings);
        ClusterSettings clusterSettings = new ClusterSettings(nodeSettings, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
        CircuitBreakerService breakerService = new HierarchyCircuitBreakerService(nodeSettings, Collections.emptyList(), clusterSettings);
        indexShard = new IndexShard(routing, indexSettings, shardPath, store, () -> null, indexCache, mapperService, similarityService, engineFactory, engineConfigFactory, indexEventListener, indexReaderWrapper, threadPool, BigArrays.NON_RECYCLING_INSTANCE, warmer, Collections.emptyList(), Arrays.asList(listeners), globalCheckpointSyncer, retentionLeaseSyncer, breakerService);
        indexShard.addShardFailureCallback(DEFAULT_SHARD_FAILURE_HANDLER);
        success = true;
    } finally {
        if (success == false) {
            IOUtils.close(store);
        }
    }
    return indexShard;
}
Also used : ClusterSettings(org.opensearch.common.settings.ClusterSettings) IndexSettings(org.opensearch.index.IndexSettings) Store(org.opensearch.index.store.Store) IndexCache(org.opensearch.index.cache.IndexCache) SimilarityService(org.opensearch.index.similarity.SimilarityService) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) RecoverySettings(org.opensearch.indices.recovery.RecoverySettings) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) ClusterSettings(org.opensearch.common.settings.ClusterSettings) DisabledQueryCache(org.opensearch.index.cache.query.DisabledQueryCache) MapperService(org.opensearch.index.mapper.MapperService) Engine(org.opensearch.index.engine.Engine)

Example 4 with HierarchyCircuitBreakerService

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

the class RestControllerTests method setup.

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

Example 5 with HierarchyCircuitBreakerService

use of org.opensearch.indices.breaker.HierarchyCircuitBreakerService 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

ClusterSettings (org.opensearch.common.settings.ClusterSettings)5 HierarchyCircuitBreakerService (org.opensearch.indices.breaker.HierarchyCircuitBreakerService)5 Settings (org.opensearch.common.settings.Settings)2 CircuitBreakerService (org.opensearch.indices.breaker.CircuitBreakerService)2 UsageService (org.opensearch.usage.UsageService)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 NodeClient (org.opensearch.client.node.NodeClient)1 CircuitBreakingException (org.opensearch.common.breaker.CircuitBreakingException)1 ThreadContext (org.opensearch.common.util.concurrent.ThreadContext)1 HttpServerTransport (org.opensearch.http.HttpServerTransport)1 IndexSettings (org.opensearch.index.IndexSettings)1 IndexCache (org.opensearch.index.cache.IndexCache)1 DisabledQueryCache (org.opensearch.index.cache.query.DisabledQueryCache)1 Engine (org.opensearch.index.engine.Engine)1 MapperService (org.opensearch.index.mapper.MapperService)1 SimilarityService (org.opensearch.index.similarity.SimilarityService)1 Store (org.opensearch.index.store.Store)1