Search in sources :

Example 1 with CircuitBreakerService

use of org.elasticsearch.indices.breaker.CircuitBreakerService in project crate by crate.

the class CrateCircuitBreakerServiceTest method testBreakerSettingsAssignment.

@Test
public void testBreakerSettingsAssignment() throws Exception {
    Settings settings = Settings.builder().put(CrateCircuitBreakerService.QUERY_CIRCUIT_BREAKER_LIMIT_SETTING, "10m").put(CrateCircuitBreakerService.QUERY_CIRCUIT_BREAKER_OVERHEAD_SETTING, 1.0).build();
    final NodeSettingsService.Listener[] listeners = new NodeSettingsService.Listener[1];
    NodeSettingsService settingsService = new NodeSettingsService(settings) {

        @Override
        public void addListener(Listener listener) {
            listeners[0] = listener;
        }
    };
    CircuitBreakerService esBreakerService = spy(new HierarchyCircuitBreakerService(Settings.EMPTY, settingsService));
    CrateCircuitBreakerService breakerService = new CrateCircuitBreakerService(settings, settingsService, esBreakerService);
    CircuitBreaker breaker = breakerService.getBreaker(CrateCircuitBreakerService.QUERY);
    assertThat(breaker.getLimit(), is(10_485_760L));
    assertThat(breaker.getOverhead(), is(1.0));
    Settings newSettings = Settings.settingsBuilder().put(CrateCircuitBreakerService.QUERY_CIRCUIT_BREAKER_LIMIT_SETTING, "100m").put(CrateCircuitBreakerService.QUERY_CIRCUIT_BREAKER_OVERHEAD_SETTING, 2.0).build();
    listeners[0].onRefreshSettings(newSettings);
    // expecting 4 times because registerBreaker() is also called from constructor of CrateCircuitBreakerService 3 times
    verify(esBreakerService, times(4)).registerBreaker(Matchers.any());
    breaker = breakerService.getBreaker(CrateCircuitBreakerService.QUERY);
    assertThat(breaker.getLimit(), is(104_857_600L));
    assertThat(breaker.getOverhead(), is(2.0));
    // updating with same settings should not register a new breaker
    listeners[0].onRefreshSettings(newSettings);
    verify(esBreakerService, times(4)).registerBreaker(Matchers.any());
}
Also used : CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) NodeSettingsService(org.elasticsearch.node.settings.NodeSettingsService) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 2 with CircuitBreakerService

use of org.elasticsearch.indices.breaker.CircuitBreakerService in project crate by crate.

the class CrateCircuitBreakerServiceTest method testQueryCircuitBreakerDynamicSettings.

@Test
public void testQueryCircuitBreakerDynamicSettings() throws Exception {
    final NodeSettingsService.Listener[] listeners = new NodeSettingsService.Listener[1];
    NodeSettingsService settingsService = new NodeSettingsService(Settings.EMPTY) {

        @Override
        public void addListener(Listener listener) {
            listeners[0] = listener;
        }
    };
    CircuitBreakerService esBreakerService = new HierarchyCircuitBreakerService(Settings.EMPTY, settingsService);
    CrateCircuitBreakerService breakerService = new CrateCircuitBreakerService(Settings.EMPTY, settingsService, esBreakerService);
    Settings newSettings = Settings.settingsBuilder().put(CrateCircuitBreakerService.QUERY_CIRCUIT_BREAKER_OVERHEAD_SETTING, 2.0).build();
    listeners[0].onRefreshSettings(newSettings);
    CircuitBreaker breaker = breakerService.getBreaker(CrateCircuitBreakerService.QUERY);
    assertThat(breaker, notNullValue());
    assertThat(breaker, instanceOf(CircuitBreaker.class));
    assertThat(breaker.getOverhead(), is(2.0));
}
Also used : CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) NodeSettingsService(org.elasticsearch.node.settings.NodeSettingsService) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) HierarchyCircuitBreakerService(org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService) Settings(org.elasticsearch.common.settings.Settings) Test(org.junit.Test) CrateUnitTest(io.crate.test.integration.CrateUnitTest)

Example 3 with CircuitBreakerService

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

the class AggregatorTestCase method createAggregator.

protected <A extends Aggregator, B extends AggregationBuilder> A createAggregator(B aggregationBuilder, IndexSearcher indexSearcher, MappedFieldType... fieldTypes) throws IOException {
    IndexSettings indexSettings = createIndexSettings();
    SearchContext searchContext = createSearchContext(indexSearcher, indexSettings);
    CircuitBreakerService circuitBreakerService = new NoneCircuitBreakerService();
    when(searchContext.bigArrays()).thenReturn(new MockBigArrays(Settings.EMPTY, circuitBreakerService));
    // TODO: now just needed for top_hits, this will need to be revised for other agg unit tests:
    MapperService mapperService = mapperServiceMock();
    when(mapperService.hasNested()).thenReturn(false);
    when(searchContext.mapperService()).thenReturn(mapperService);
    IndexFieldDataService ifds = new IndexFieldDataService(indexSettings, new IndicesFieldDataCache(Settings.EMPTY, new IndexFieldDataCache.Listener() {
    }), circuitBreakerService, mapperService);
    when(searchContext.fieldData()).thenReturn(ifds);
    SearchLookup searchLookup = new SearchLookup(mapperService, ifds, new String[] { "type" });
    when(searchContext.lookup()).thenReturn(searchLookup);
    QueryShardContext queryShardContext = queryShardContextMock(fieldTypes, indexSettings, circuitBreakerService);
    when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
    @SuppressWarnings("unchecked") A aggregator = (A) aggregationBuilder.build(searchContext, null).create(null, true);
    return aggregator;
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) SearchContext(org.elasticsearch.search.internal.SearchContext) MockBigArrays(org.elasticsearch.common.util.MockBigArrays) IndicesFieldDataCache(org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache) IndexFieldDataService(org.elasticsearch.index.fielddata.IndexFieldDataService) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) MapperService(org.elasticsearch.index.mapper.MapperService) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService) SearchLookup(org.elasticsearch.search.lookup.SearchLookup)

Example 4 with CircuitBreakerService

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

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

the class NetworkModuleTests method testOverrideDefault.

public void testOverrideDefault() {
    Settings settings = Settings.builder().put(NetworkModule.HTTP_TYPE_SETTING.getKey(), "custom").put(NetworkModule.HTTP_DEFAULT_TYPE_SETTING.getKey(), "default_custom").put(NetworkModule.TRANSPORT_DEFAULT_TYPE_SETTING.getKey(), "local").put(NetworkModule.TRANSPORT_TYPE_KEY, "default_custom").build();
    // content doesn't matter we check reference equality
    Supplier<Transport> customTransport = () -> null;
    Supplier<HttpServerTransport> custom = FakeHttpTransport::new;
    Supplier<HttpServerTransport> def = FakeHttpTransport::new;
    NetworkModule module = newNetworkModule(settings, false, new NetworkPlugin() {

        @Override
        public Map<String, Supplier<Transport>> getTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService) {
            return Collections.singletonMap("default_custom", customTransport);
        }

        @Override
        public Map<String, Supplier<HttpServerTransport>> getHttpTransports(Settings settings, ThreadPool threadPool, BigArrays bigArrays, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NamedXContentRegistry xContentRegistry, NetworkService networkService, HttpServerTransport.Dispatcher requestDispatcher) {
            Map<String, Supplier<HttpServerTransport>> supplierMap = new HashMap<>();
            supplierMap.put("custom", custom);
            supplierMap.put("default_custom", def);
            return supplierMap;
        }
    });
    assertSame(custom, module.getHttpServerTransportSupplier());
    assertSame(customTransport, module.getTransportSupplier());
}
Also used : NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) NetworkPlugin(org.elasticsearch.plugins.NetworkPlugin) ThreadPool(org.elasticsearch.threadpool.ThreadPool) TestThreadPool(org.elasticsearch.threadpool.TestThreadPool) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) BigArrays(org.elasticsearch.common.util.BigArrays) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) Transport(org.elasticsearch.transport.Transport) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) HashMap(java.util.HashMap) Map(java.util.Map) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

CircuitBreakerService (org.elasticsearch.indices.breaker.CircuitBreakerService)18 HierarchyCircuitBreakerService (org.elasticsearch.indices.breaker.HierarchyCircuitBreakerService)10 Settings (org.elasticsearch.common.settings.Settings)9 NamedWriteableRegistry (org.elasticsearch.common.io.stream.NamedWriteableRegistry)7 ThreadPool (org.elasticsearch.threadpool.ThreadPool)7 CircuitBreaker (org.elasticsearch.common.breaker.CircuitBreaker)6 BigArrays (org.elasticsearch.common.util.BigArrays)6 TestThreadPool (org.elasticsearch.threadpool.TestThreadPool)6 NetworkPlugin (org.elasticsearch.plugins.NetworkPlugin)5 CrateUnitTest (io.crate.test.integration.CrateUnitTest)4 HttpServerTransport (org.elasticsearch.http.HttpServerTransport)4 NodeSettingsService (org.elasticsearch.node.settings.NodeSettingsService)4 Transport (org.elasticsearch.transport.Transport)4 Test (org.junit.Test)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)3 BreakerSettings (org.elasticsearch.indices.breaker.BreakerSettings)3 Collections (java.util.Collections)2