Search in sources :

Example 11 with HierarchyCircuitBreakerService

use of org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService in project elasticsearch by elastic.

the class BigArraysTests method testMaxSizeExceededOnResize.

public void testMaxSizeExceededOnResize() throws Exception {
    for (String type : Arrays.asList("Byte", "Int", "Long", "Float", "Double", "Object")) {
        final long maxSize = randomIntBetween(1 << 10, 1 << 22);
        HierarchyCircuitBreakerService hcbs = new HierarchyCircuitBreakerService(Settings.builder().put(HierarchyCircuitBreakerService.REQUEST_CIRCUIT_BREAKER_LIMIT_SETTING.getKey(), maxSize, ByteSizeUnit.BYTES).build(), new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
        BigArrays bigArrays = new BigArrays(null, hcbs, false).withCircuitBreaking();
        Method create = BigArrays.class.getMethod("new" + type + "Array", long.class);
        final int size = scaledRandomIntBetween(1, 20);
        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.elasticsearch.common.settings.ClusterSettings) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 12 with HierarchyCircuitBreakerService

use of org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService in project elasticsearch by elastic.

the class MemoryCircuitBreakerTests method testThreadedUpdatesToChildBreakerWithParentLimit.

public void testThreadedUpdatesToChildBreakerWithParentLimit() throws Exception {
    final int NUM_THREADS = scaledRandomIntBetween(3, 15);
    final int BYTES_PER_THREAD = scaledRandomIntBetween(500, 4500);
    final int parentLimit = (BYTES_PER_THREAD * NUM_THREADS) - 2;
    final int childLimit = parentLimit + 10;
    final Thread[] threads = new Thread[NUM_THREADS];
    final AtomicInteger tripped = new AtomicInteger(0);
    final AtomicReference<Throwable> lastException = new AtomicReference<>(null);
    final AtomicInteger parentTripped = new AtomicInteger(0);
    final AtomicReference<ChildMemoryCircuitBreaker> breakerRef = new AtomicReference<>(null);
    final CircuitBreakerService service = new HierarchyCircuitBreakerService(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS)) {

        @Override
        public CircuitBreaker getBreaker(String name) {
            return breakerRef.get();
        }

        @Override
        public void checkParentLimit(String label) throws CircuitBreakingException {
            // Parent will trip right before regular breaker would trip
            if (getBreaker(CircuitBreaker.REQUEST).getUsed() > parentLimit) {
                parentTripped.incrementAndGet();
                logger.info("--> parent tripped");
                throw new CircuitBreakingException("parent tripped");
            }
        }
    };
    final BreakerSettings settings = new BreakerSettings(CircuitBreaker.REQUEST, childLimit, 1.0);
    final ChildMemoryCircuitBreaker breaker = new ChildMemoryCircuitBreaker(settings, logger, (HierarchyCircuitBreakerService) service, CircuitBreaker.REQUEST);
    breakerRef.set(breaker);
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(new Runnable() {

            @Override
            public void run() {
                for (int j = 0; j < BYTES_PER_THREAD; j++) {
                    try {
                        breaker.addEstimateBytesAndMaybeBreak(1L, "test");
                    } catch (CircuitBreakingException e) {
                        tripped.incrementAndGet();
                    } catch (Exception e) {
                        lastException.set(e);
                    }
                }
            }
        });
    }
    logger.info("--> NUM_THREADS: [{}], BYTES_PER_THREAD: [{}], TOTAL_BYTES: [{}], PARENT_LIMIT: [{}], CHILD_LIMIT: [{}]", NUM_THREADS, BYTES_PER_THREAD, (BYTES_PER_THREAD * NUM_THREADS), parentLimit, childLimit);
    logger.info("--> starting threads...");
    for (Thread t : threads) {
        t.start();
    }
    for (Thread t : threads) {
        t.join();
    }
    logger.info("--> child breaker: used: {}, limit: {}", breaker.getUsed(), breaker.getLimit());
    logger.info("--> parent tripped: {}, total trip count: {} (expecting 1-2 for each)", parentTripped.get(), tripped.get());
    assertThat("no other exceptions were thrown", lastException.get(), equalTo(null));
    assertThat("breaker should be reset back to the parent limit after parent breaker trips", breaker.getUsed(), greaterThanOrEqualTo((long) parentLimit - NUM_THREADS));
    assertThat("parent breaker was tripped at least once", parentTripped.get(), greaterThanOrEqualTo(1));
    assertThat("total breaker was tripped at least once", tripped.get(), greaterThanOrEqualTo(1));
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) AtomicReference(java.util.concurrent.atomic.AtomicReference) BreakerSettings(org.elasticsearch.indices.breaker.BreakerSettings) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService)

Aggregations

HierarchyCircuitBreakerService (org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService)12 CircuitBreakerService (org.elasticsearch.indices.breaker.CircuitBreakerService)8 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)7 CrateUnitTest (io.crate.test.integration.CrateUnitTest)4 CircuitBreaker (org.elasticsearch.common.breaker.CircuitBreaker)4 Settings (org.elasticsearch.common.settings.Settings)4 NodeSettingsService (org.elasticsearch.node.settings.NodeSettingsService)4 Test (org.junit.Test)4 BreakerSettings (org.elasticsearch.indices.breaker.BreakerSettings)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 CircuitBreakingException (org.elasticsearch.common.breaker.CircuitBreakingException)2 Before (org.junit.Before)2 CrateCircuitBreakerService (io.crate.breaker.CrateCircuitBreakerService)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)1 ByteSizeValue (org.elasticsearch.common.unit.ByteSizeValue)1