Search in sources :

Example 1 with BreakerSettings

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

the class CircuitBreakerUnitTests method testRegisterCustomBreaker.

public void testRegisterCustomBreaker() throws Exception {
    CircuitBreakerService service = new HierarchyCircuitBreakerService(Settings.EMPTY, new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS));
    String customName = "custom";
    BreakerSettings settings = new BreakerSettings(customName, 20, 1.0);
    service.registerBreaker(settings);
    CircuitBreaker breaker = service.getBreaker(customName);
    assertThat(breaker, notNullValue());
    assertThat(breaker, instanceOf(CircuitBreaker.class));
    assertThat(breaker.getName(), is(customName));
}
Also used : BreakerSettings(org.elasticsearch.indices.breaker.BreakerSettings) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService)

Example 2 with BreakerSettings

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

the class CircuitBreakerUnitTests method testBreakerSettingsValidationNegativeOverhead.

public void testBreakerSettingsValidationNegativeOverhead() {
    // parent: {:limit 70}, fd: {:limit 50}, request: {:limit 20}
    BreakerSettings fd = new BreakerSettings(CircuitBreaker.FIELDDATA, pctBytes("50%"), -0.1);
    BreakerSettings request = new BreakerSettings(CircuitBreaker.REQUEST, pctBytes("20%"), 1.0);
    try {
        HierarchyCircuitBreakerService.validateSettings(new BreakerSettings[] { fd, request });
        fail("settings are invalid but validate settings did not throw an exception");
    } catch (Exception e) {
        assertThat("Incorrect message: " + e.getMessage(), e.getMessage().contains("must be non-negative"), equalTo(true));
    }
}
Also used : BreakerSettings(org.elasticsearch.indices.breaker.BreakerSettings)

Example 3 with BreakerSettings

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

the class CircuitBreakerServiceIT method testCustomCircuitBreakerRegistration.

public void testCustomCircuitBreakerRegistration() throws Exception {
    Iterable<CircuitBreakerService> serviceIter = internalCluster().getInstances(CircuitBreakerService.class);
    final String breakerName = "customBreaker";
    BreakerSettings breakerSettings = new BreakerSettings(breakerName, 8, 1.03);
    CircuitBreaker breaker = null;
    for (CircuitBreakerService s : serviceIter) {
        s.registerBreaker(breakerSettings);
        breaker = s.getBreaker(breakerSettings.getName());
    }
    if (breaker != null) {
        try {
            breaker.addEstimateBytesAndMaybeBreak(16, "test");
        } catch (CircuitBreakingException e) {
        // ignore, we forced a circuit break
        }
    }
    NodesStatsResponse stats = client().admin().cluster().prepareNodesStats().clear().setBreaker(true).get();
    int breaks = 0;
    for (NodeStats stat : stats.getNodes()) {
        CircuitBreakerStats breakerStats = stat.getBreaker().getStats(breakerName);
        breaks += breakerStats.getTrippedCount();
    }
    assertThat(breaks, greaterThanOrEqualTo(1));
}
Also used : NodesStatsResponse(org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse) NodeStats(org.elasticsearch.action.admin.cluster.node.stats.NodeStats) BreakerSettings(org.elasticsearch.indices.breaker.BreakerSettings) NoopCircuitBreaker(org.elasticsearch.common.breaker.NoopCircuitBreaker) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) CircuitBreakerStats(org.elasticsearch.indices.breaker.CircuitBreakerStats) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService)

Example 4 with BreakerSettings

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

the class MemoryCircuitBreakerTests method testThreadedUpdatesToChildBreaker.

public void testThreadedUpdatesToChildBreaker() throws Exception {
    final int NUM_THREADS = scaledRandomIntBetween(3, 15);
    final int BYTES_PER_THREAD = scaledRandomIntBetween(500, 4500);
    final Thread[] threads = new Thread[NUM_THREADS];
    final AtomicBoolean tripped = new AtomicBoolean(false);
    final AtomicReference<Throwable> lastException = new AtomicReference<>(null);
    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 {
        // never trip
        }
    };
    final BreakerSettings settings = new BreakerSettings(CircuitBreaker.REQUEST, (BYTES_PER_THREAD * NUM_THREADS) - 1, 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) {
                        if (tripped.get()) {
                            assertThat("tripped too many times", true, equalTo(false));
                        } else {
                            assertThat(tripped.compareAndSet(false, true), equalTo(true));
                        }
                    } catch (Exception e) {
                        lastException.set(e);
                    }
                }
            }
        });
        threads[i].start();
    }
    for (Thread t : threads) {
        t.join();
    }
    assertThat("no other exceptions were thrown", lastException.get(), equalTo(null));
    assertThat("breaker was tripped", tripped.get(), equalTo(true));
    assertThat("breaker was tripped at least once", breaker.getTrippedCount(), greaterThanOrEqualTo(1L));
}
Also used : ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BreakerSettings(org.elasticsearch.indices.breaker.BreakerSettings) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService)

Example 5 with BreakerSettings

use of org.elasticsearch.indices.breaker.BreakerSettings 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

BreakerSettings (org.elasticsearch.indices.breaker.BreakerSettings)6 CircuitBreakerService (org.elasticsearch.indices.breaker.CircuitBreakerService)4 HierarchyCircuitBreakerService (org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService)4 ClusterSettings (org.elasticsearch.common.settings.ClusterSettings)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 CircuitBreaker (org.elasticsearch.common.breaker.CircuitBreaker)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 NodeStats (org.elasticsearch.action.admin.cluster.node.stats.NodeStats)1 NodesStatsResponse (org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse)1 CircuitBreakingException (org.elasticsearch.common.breaker.CircuitBreakingException)1 NoopCircuitBreaker (org.elasticsearch.common.breaker.NoopCircuitBreaker)1 CircuitBreakerStats (org.elasticsearch.indices.breaker.CircuitBreakerStats)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1