Search in sources :

Example 11 with CircuitBreakerService

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

the class HyperLogLogPlusPlusSparseTests method testCircuitBreakerOnConstruction.

public void testCircuitBreakerOnConstruction() {
    int whenToBreak = randomInt(10);
    AtomicLong total = new AtomicLong();
    CircuitBreakerService breakerService = mock(CircuitBreakerService.class);
    when(breakerService.getBreaker(CircuitBreaker.REQUEST)).thenReturn(new NoopCircuitBreaker(CircuitBreaker.REQUEST) {

        private int countDown = whenToBreak;

        @Override
        public double addEstimateBytesAndMaybeBreak(long bytes, String label) throws CircuitBreakingException {
            if (countDown-- == 0) {
                throw new CircuitBreakingException("test error", bytes, Long.MAX_VALUE, Durability.TRANSIENT);
            }
            total.addAndGet(bytes);
            return total.get();
        }

        @Override
        public long addWithoutBreaking(long bytes) {
            total.addAndGet(bytes);
            return total.get();
        }
    });
    BigArrays bigArrays = new BigArrays(null, breakerService, CircuitBreaker.REQUEST).withCircuitBreaking();
    final int p = randomIntBetween(AbstractCardinalityAlgorithm.MIN_PRECISION, AbstractCardinalityAlgorithm.MAX_PRECISION);
    try {
        for (int i = 0; i < whenToBreak + 1; ++i) {
            final HyperLogLogPlusPlusSparse subject = new HyperLogLogPlusPlusSparse(p, bigArrays, 1, 1);
            subject.close();
        }
        fail("Must fail");
    } catch (CircuitBreakingException e) {
    // OK
    }
    assertThat(total.get(), CoreMatchers.equalTo(0L));
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) BigArrays(org.opensearch.common.util.BigArrays) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService)

Example 12 with CircuitBreakerService

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

the class ScriptedMetricAggregatorTests method mockBreaker.

@Before
public void mockBreaker() {
    circuitBreakerService = mock(CircuitBreakerService.class);
    when(circuitBreakerService.getBreaker(CircuitBreaker.REQUEST)).thenReturn(new NoopCircuitBreaker(CircuitBreaker.REQUEST) {

        private long total = 0;

        @Override
        public double addEstimateBytesAndMaybeBreak(long bytes, String label) throws CircuitBreakingException {
            logger.debug("Used {} grabbing {} for {}", total, bytes, label);
            total += bytes;
            return total;
        }

        @Override
        public long addWithoutBreaking(long bytes) {
            logger.debug("Used {} grabbing {}", total, bytes);
            total += bytes;
            return total;
        }

        @Override
        public long getUsed() {
            return total;
        }
    });
}
Also used : CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) NoopCircuitBreaker(org.opensearch.common.breaker.NoopCircuitBreaker) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) Before(org.junit.Before)

Example 13 with CircuitBreakerService

use of org.opensearch.indices.breaker.CircuitBreakerService 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 14 with CircuitBreakerService

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

the class InternalTestCluster method ensureEstimatedStats.

@Override
public void ensureEstimatedStats() {
    if (size() > 0) {
        // of the breakers
        for (NodeAndClient nodeAndClient : nodes.values()) {
            final IndicesFieldDataCache fdCache = getInstanceFromNode(IndicesService.class, nodeAndClient.node).getIndicesFieldDataCache();
            // Clean up the cache, ensuring that entries' listeners have been called
            fdCache.getCache().refresh();
            final String name = nodeAndClient.name;
            final CircuitBreakerService breakerService = getInstanceFromNode(CircuitBreakerService.class, nodeAndClient.node);
            CircuitBreaker fdBreaker = breakerService.getBreaker(CircuitBreaker.FIELDDATA);
            assertThat("Fielddata breaker not reset to 0 on node: " + name, fdBreaker.getUsed(), equalTo(0L));
            // fail if it never reached 0
            try {
                assertBusy(() -> {
                    CircuitBreaker reqBreaker = breakerService.getBreaker(CircuitBreaker.REQUEST);
                    assertThat("Request breaker not reset to 0 on node: " + name, reqBreaker.getUsed(), equalTo(0L));
                });
            } catch (Exception e) {
                throw new AssertionError("Exception during check for request breaker reset to 0", e);
            }
            NodeService nodeService = getInstanceFromNode(NodeService.class, nodeAndClient.node);
            CommonStatsFlags flags = new CommonStatsFlags(Flag.FieldData, Flag.QueryCache, Flag.Segments);
            NodeStats stats = nodeService.stats(flags, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false);
            assertThat("Fielddata size must be 0 on node: " + stats.getNode(), stats.getIndices().getFieldData().getMemorySizeInBytes(), equalTo(0L));
            assertThat("Query cache size must be 0 on node: " + stats.getNode(), stats.getIndices().getQueryCache().getMemorySizeInBytes(), equalTo(0L));
            assertThat("FixedBitSet cache size must be 0 on node: " + stats.getNode(), stats.getIndices().getSegments().getBitsetMemoryInBytes(), equalTo(0L));
        }
    }
}
Also used : IndicesFieldDataCache(org.opensearch.indices.fielddata.cache.IndicesFieldDataCache) NodeStats(org.opensearch.action.admin.cluster.node.stats.NodeStats) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) CommonStatsFlags(org.opensearch.action.admin.indices.stats.CommonStatsFlags) NodeService(org.opensearch.node.NodeService) IndicesService(org.opensearch.indices.IndicesService) HierarchyCircuitBreakerService(org.opensearch.indices.breaker.HierarchyCircuitBreakerService) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) NodeValidationException(org.opensearch.node.NodeValidationException) ExecutionException(java.util.concurrent.ExecutionException) ShardLockObtainFailedException(org.opensearch.env.ShardLockObtainFailedException)

Example 15 with CircuitBreakerService

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

the class NodeTests method testCreateWithCircuitBreakerPlugins.

public void testCreateWithCircuitBreakerPlugins() throws IOException {
    Settings.Builder settings = baseSettings().put("breaker.test_breaker.limit", "50b");
    List<Class<? extends Plugin>> plugins = basePlugins();
    plugins.add(MockCircuitBreakerPlugin.class);
    try (Node node = new MockNode(settings.build(), plugins)) {
        CircuitBreakerService service = node.injector().getInstance(CircuitBreakerService.class);
        assertThat(service.getBreaker("test_breaker"), is(not(nullValue())));
        assertThat(service.getBreaker("test_breaker").getLimit(), equalTo(50L));
        CircuitBreakerPlugin breakerPlugin = node.getPluginsService().filterPlugins(CircuitBreakerPlugin.class).get(0);
        assertTrue(breakerPlugin instanceof MockCircuitBreakerPlugin);
        assertSame("plugin circuit breaker instance is not the same as breaker service's instance", ((MockCircuitBreakerPlugin) breakerPlugin).myCircuitBreaker.get(), service.getBreaker("test_breaker"));
    }
}
Also used : CircuitBreakerPlugin(org.opensearch.plugins.CircuitBreakerPlugin) NodeRoles.dataNode(org.opensearch.test.NodeRoles.dataNode) CircuitBreakerService(org.opensearch.indices.breaker.CircuitBreakerService) Settings(org.opensearch.common.settings.Settings) BreakerSettings(org.opensearch.indices.breaker.BreakerSettings) CircuitBreakerPlugin(org.opensearch.plugins.CircuitBreakerPlugin) Plugin(org.opensearch.plugins.Plugin)

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