org.elasticsearch.common.breaker.CircuitBreakingException Java Examples

The following examples show how to use org.elasticsearch.common.breaker.CircuitBreakingException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CollectSetAggregation.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Map<Object, Object> iterate(RamAccounting ramAccounting,
                                   MemoryManager memoryManager,
                                   Map<Object, Object> state,
                                   Input... args) throws CircuitBreakingException {
    Object value = args[0].value();
    if (value == null) {
        return state;
    }
    if (state.put(value, PRESENT) == null) {
        ramAccounting.addBytes(
            // values size + 32 bytes for entry, 4 bytes for increased capacity
            RamUsageEstimator.alignObjectSize(innerTypeEstimator.estimateSize(value) + 36L)
        );
    }
    return state;
}
 
Example #2
Source File: SortingProjectorTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testUsedMemoryIsAccountedFor() throws Exception {
    MemoryCircuitBreaker circuitBreaker = new MemoryCircuitBreaker(new ByteSizeValue(30, ByteSizeUnit.BYTES),
                                                                   1,
                                                                   LogManager.getLogger(SortingProjectorTest.class)
    );
    RowCellsAccountingWithEstimators rowAccounting =
        new RowCellsAccountingWithEstimators(
            List.of(DataTypes.LONG, DataTypes.BOOLEAN),
            ConcurrentRamAccounting.forCircuitBreaker("testContext", circuitBreaker),
            0);

    Projector projector = createProjector(rowAccounting, 1, 0);
    consumer.accept(projector.apply(TestingBatchIterators.range(1, 11)), null);

    expectedException.expect(CircuitBreakingException.class);
    consumer.getResult();
}
 
Example #3
Source File: NodeFetchResponseTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testResponseCircuitBreaker() throws Exception {
    NodeFetchResponse orig = new NodeFetchResponse(fetched);
    BytesStreamOutput out = new BytesStreamOutput();
    orig.writeTo(out);
    StreamInput in = out.bytes().streamInput();

    expectedException.expect(CircuitBreakingException.class);
    new NodeFetchResponse(
        in,
        streamers,
        ConcurrentRamAccounting.forCircuitBreaker(
            "test",
            new MemoryCircuitBreaker(
                new ByteSizeValue(2, ByteSizeUnit.BYTES),
                1.0,
                LogManager.getLogger(NodeFetchResponseTest.class))));

}
 
Example #4
Source File: DistributingConsumerTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void test_exception_on_loadNextBatch_is_forwarded() throws Exception {
    Streamer<?>[] streamers = { DataTypes.INTEGER.streamer() };
    TestingRowConsumer collectingConsumer = new TestingRowConsumer();
    DistResultRXTask distResultRXTask = createPageDownstreamContext(streamers, collectingConsumer);
    TransportDistributedResultAction distributedResultAction = createFakeTransport(streamers, distResultRXTask);
    DistributingConsumer distributingConsumer = createDistributingConsumer(streamers, distributedResultAction);

    BatchSimulatingIterator<Row> batchSimulatingIterator =
        new BatchSimulatingIterator<>(TestingBatchIterators.range(0, 5),
                                      2,
                                      3,
                                      executorService) {

            @Override
            public CompletionStage<?> loadNextBatch() {
                throw new CircuitBreakingException("data too large");
            }
        };
    distributingConsumer.accept(batchSimulatingIterator, null);

    expectedException.expect(CircuitBreakingException.class);
    collectingConsumer.getResult();
}
 
Example #5
Source File: RamAccountingPageIteratorTest.java    From crate with Apache License 2.0 6 votes vote down vote up
@Test
public void testCircuitBreaking() throws Exception {
    PagingIterator<Integer, Row> pagingIterator = PagingIterator.create(
        2,
        true,
        null,
        () -> new RowAccountingWithEstimators(
            ImmutableList.of(DataTypes.STRING, DataTypes.STRING, DataTypes.STRING),
            ConcurrentRamAccounting.forCircuitBreaker(
                "test",
                new MemoryCircuitBreaker(
                    new ByteSizeValue(197, ByteSizeUnit.BYTES),
                    1,
                    LogManager.getLogger(RowAccountingWithEstimatorsTest.class)))));

    expectedException.expect(CircuitBreakingException.class);
    expectedException.expectMessage(
        "Data too large, data for field [test] would be [288/288b], which is larger than the limit of [197/197b]");
    pagingIterator.merge(Arrays.asList(
        new KeyIterable<>(0, Collections.singletonList(TEST_ROWS[0])),
        new KeyIterable<>(1, Collections.singletonList(TEST_ROWS[1]))));
}
 
Example #6
Source File: StringAgg.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public StringAggState iterate(RamAccounting ramAccounting,
                              MemoryManager memoryManager,
                              StringAggState state,
                              Input... args) throws CircuitBreakingException {
    String expression = (String) args[0].value();
    if (expression == null) {
        return state;
    }
    ramAccounting.addBytes(LIST_ENTRY_OVERHEAD + StringSizeEstimator.estimate(expression));
    String delimiter = (String) args[1].value();
    if (delimiter != null) {
        if (state.firstDelimiter == null && state.values.isEmpty()) {
            state.firstDelimiter = delimiter;
        } else {
            ramAccounting.addBytes(LIST_ENTRY_OVERHEAD + StringSizeEstimator.estimate(delimiter));
            state.values.add(delimiter);
        }
    }
    state.values.add(expression);
    return state;
}
 
Example #7
Source File: RamAccountingContext.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * Flush the {@code bytes} to the breaker, incrementing the total
 * bytes and adjusting the buffer.

 * @param bytes long value of bytes to be flushed to the breaker
 * @throws CircuitBreakingException
 */
private void flush(long bytes) throws CircuitBreakingException {
    if (bytes == 0) {
        return;
    }
    try {
        breaker.addEstimateBytesAndMaybeBreak(bytes, contextId);
    } catch (CircuitBreakingException e) {
        // since we've already created the data, we need to
        // add it so closing the context re-adjusts properly
        breaker.addWithoutBreaking(bytes);
        tripped = true;
        // re-throw the original exception
        throw e;
    } finally {
        totalBytes.addAndGet(bytes);
        flushBuffer.addAndGet(-bytes);
    }
}
 
Example #8
Source File: StandardDeviationAggregation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public StandardDeviation iterate(RamAccounting ramAccounting,
                                 MemoryManager memoryManager,
                                 StandardDeviation state,
                                 Input... args) throws CircuitBreakingException {
    if (state != null) {
        Number value = (Number) args[0].value();
        if (value != null) {
            state.increment(value.doubleValue());
        }
    }
    return state;
}
 
Example #9
Source File: CollectSetAggregation.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Set<Object> iterate(RamAccountingContext ramAccountingContext, Set<Object> state, Input... args) throws CircuitBreakingException {
    Object value = args[0].value();
    if (value == null) {
        return state;
    }
    if (state.add(value)) {
        ramAccountingContext.addBytes(innerTypeEstimator.estimateSize(value));
    }
    return state;
}
 
Example #10
Source File: VarianceAggregation.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public VarianceAggregation.VarianceState iterate(RamAccountingContext ramAccountingContext, VarianceAggregation.VarianceState state, Input... args) throws CircuitBreakingException {
    if (state != null) {
        Number value = (Number) args[0].value();
        if (value != null) {
            state.addValue(value.doubleValue());
        }
    }
    return state;
}
 
Example #11
Source File: StandardDeviationAggregation.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public StdDevState iterate(RamAccountingContext ramAccountingContext, StdDevState state, Input... args) throws CircuitBreakingException {
    if (state != null) {
        Number value = (Number) args[0].value();
        if (value != null) {
            state.addValue(value.doubleValue());
        }
    }
    return state;
}
 
Example #12
Source File: RowAccountingWithEstimatorsTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testCircuitBreakingWorksWithExtraSizePerRow() throws Exception {
    RowAccountingWithEstimators rowAccounting = new RowAccountingWithEstimators(
        Collections.singletonList(DataTypes.INTEGER),
        ConcurrentRamAccounting.forCircuitBreaker(
            "test",
            new MemoryCircuitBreaker(
                new ByteSizeValue(10, ByteSizeUnit.BYTES), 1.01, LogManager.getLogger(RowAccountingWithEstimatorsTest.class))
        ),
        2);

    expectedException.expect(CircuitBreakingException.class);
    RowGenerator.range(0, 2).forEach(rowAccounting::accountForAndMaybeBreak);
}
 
Example #13
Source File: RowAccountingWithEstimatorsTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testRowCellsAccountingCircuitBreakingWorks() throws Exception {
    RowCellsAccountingWithEstimators rowAccounting = new RowCellsAccountingWithEstimators(
        Collections.singletonList(DataTypes.INTEGER),
        ConcurrentRamAccounting.forCircuitBreaker(
            "test",
            new MemoryCircuitBreaker(
                new ByteSizeValue(10, ByteSizeUnit.BYTES), 1.01, LogManager.getLogger(RowAccountingWithEstimatorsTest.class))
        ), 0);

    expectedException.expect(CircuitBreakingException.class);
    IntStream.range(0, 3).forEach(i -> rowAccounting.accountForAndMaybeBreak(new Object[]{i}));
}
 
Example #14
Source File: RowAccountingWithEstimatorsTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testCircuitBreakingWorks() throws Exception {
    RowAccountingWithEstimators rowAccounting = new RowAccountingWithEstimators(
        Collections.singletonList(DataTypes.INTEGER),
        ConcurrentRamAccounting.forCircuitBreaker(
            "test",
            new MemoryCircuitBreaker(
                new ByteSizeValue(10, ByteSizeUnit.BYTES),
                1.01,
                LogManager.getLogger(RowAccountingWithEstimatorsTest.class))
        ));

    expectedException.expect(CircuitBreakingException.class);
    RowGenerator.range(0, 3).forEach(rowAccounting::accountForAndMaybeBreak);
}
 
Example #15
Source File: GeometricMeanAggregation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public GeometricMeanState iterate(RamAccounting ramAccounting,
                                  MemoryManager memoryManager,
                                  GeometricMeanState state,
                                  Input... args) throws CircuitBreakingException {
    if (state != null) {
        Number value = (Number) args[0].value();
        if (value != null) {
            state.addValue(value.doubleValue());
        }
    }
    return state;
}
 
Example #16
Source File: MaximumAggregation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Comparable iterate(RamAccounting ramAccounting,
                          MemoryManager memoryManager,
                          Comparable state,
                          Input... args) throws CircuitBreakingException {
    Object value = args[0].value();
    return reduce(ramAccounting, state, (Comparable) value);
}
 
Example #17
Source File: ArrayAgg.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public List<Object> iterate(RamAccounting ramAccounting,
                            MemoryManager memoryManager,
                            List<Object> state,
                            Input... args) throws CircuitBreakingException {
    var value = args[0].value();
    ramAccounting.addBytes(sizeEstimator.estimateSize(value));
    state.add(value);
    return state;
}
 
Example #18
Source File: RamAccountingContext.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Add bytes to the context and maybe break
 *
 * @param bytes bytes to be added to
 * @throws CircuitBreakingException
 */
public void addBytes(long bytes) throws CircuitBreakingException {
    if (closed) {
        return;
    }
    if (bytes == 0) {
        return;
    }
    long currentFlushBuffer = flushBuffer.addAndGet(bytes);
    if (currentFlushBuffer >= FLUSH_BUFFER_SIZE) {
        flush(currentFlushBuffer);
    }
}
 
Example #19
Source File: VarianceAggregation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Variance iterate(RamAccounting ramAccounting,
                        MemoryManager memoryManager,
                        Variance state,
                        Input... args) throws CircuitBreakingException {
    if (state != null) {
        Number value = (Number) args[0].value();
        if (value != null) {
            state.increment(value.doubleValue());
        }
    }
    return state;
}
 
Example #20
Source File: CollectSetAggregation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Map<Object, Long> iterate(RamAccounting ramAccounting,
                                 MemoryManager memoryManager, Map<Object, Long> state,
                                 Input... args) throws CircuitBreakingException {
    Object value = args[0].value();
    if (value == null) {
        return state;
    }
    upsertOccurrenceForValue(state, value, 1, ramAccounting, innerTypeEstimator);
    return state;
}
 
Example #21
Source File: BigArrays.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Adjust the circuit breaker with the given delta, if the delta is
 * negative, or checkBreaker is false, the breaker will be adjusted
 * without tripping.  If the data was already created before calling
 * this method, and the breaker trips, we add the delta without breaking
 * to account for the created data.  If the data has not been created yet,
 * we do not add the delta to the breaker if it trips.
 */
void adjustBreaker(final long delta, final boolean isDataAlreadyCreated) {
    if (this.breakerService != null) {
        CircuitBreaker breaker = this.breakerService.getBreaker(breakerName);
        if (this.checkBreaker) {
            // checking breaker means potentially tripping, but it doesn't
            // have to if the delta is negative
            if (delta > 0) {
                try {
                    breaker.addEstimateBytesAndMaybeBreak(delta, "<reused_arrays>");
                } catch (CircuitBreakingException e) {
                    if (isDataAlreadyCreated) {
                        // since we've already created the data, we need to
                        // add it so closing the stream re-adjusts properly
                        breaker.addWithoutBreaking(delta);
                    }
                    // re-throw the original exception
                    throw e;
                }
            } else {
                breaker.addWithoutBreaking(delta);
            }
        } else {
            // even if we are not checking the breaker, we need to adjust
            // its' totals, so add without breaking
            breaker.addWithoutBreaking(delta);
        }
    }
}
 
Example #22
Source File: HierarchyCircuitBreakerService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether the parent breaker has been tripped
 */
public void checkParentLimit(long newBytesReserved, String label) throws CircuitBreakingException {
    long totalUsed = parentUsed(newBytesReserved);
    long parentLimit = this.parentSettings.getLimit();
    if (totalUsed > parentLimit) {
        long breakersTotalUsed = breakers.values().stream()
            .mapToLong(x -> (long) (x.getUsed() * x.getOverhead()))
            .sum();
        // if the individual breakers hardly use any memory we assume that there is a lot of heap usage by objects which can be GCd.
        // We want to allow the query so that it triggers GCs
        if ((breakersTotalUsed + newBytesReserved) < (parentLimit * PARENT_BREAKER_ESCAPE_HATCH_PERCENTAGE)) {
            return;
        }
        this.parentTripCount.incrementAndGet();
        final StringBuilder message = new StringBuilder(
            "[parent] Data too large, data for [" + label + "]" +
            " would be [" + totalUsed + "/" + new ByteSizeValue(totalUsed) + "]" +
            ", which is larger than the limit of [" +
            parentLimit + "/" + new ByteSizeValue(parentLimit) + "]");
        message.append(", usages [");
        message.append(this.breakers.entrySet().stream().map(e -> {
            final CircuitBreaker breaker = e.getValue();
            final long breakerUsed = (long)(breaker.getUsed() * breaker.getOverhead());
            return e.getKey() + "=" + breakerUsed + "/" + new ByteSizeValue(breakerUsed);
        }).collect(Collectors.joining(", ")));
        message.append("]");
        throw new CircuitBreakingException(message.toString(), totalUsed, parentLimit);
    }
}
 
Example #23
Source File: TermsSetTest.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test(expected=CircuitBreakingException.class)
public void testCircuitBreakerOnNewIntTermsSet() {
  final int size = 42;
  HierarchyCircuitBreakerService hcbs = new HierarchyCircuitBreakerService(
          Settings.builder()
                  .put(HierarchyCircuitBreakerService.REQUEST_CIRCUIT_BREAKER_LIMIT_SETTING, size - 1, ByteSizeUnit.BYTES)
                  .build(),
          new NodeSettingsService(Settings.EMPTY));

  IntegerTermsSet termsSet = new IntegerTermsSet(size, hcbs.getBreaker(CircuitBreaker.REQUEST));
}
 
Example #24
Source File: TermsSetTest.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
@Test(expected=CircuitBreakingException.class)
public void testCircuitBreakerOnNewLongTermsSet() {
  final int size = 42;
  HierarchyCircuitBreakerService hcbs = new HierarchyCircuitBreakerService(
          Settings.builder()
                  .put(HierarchyCircuitBreakerService.REQUEST_CIRCUIT_BREAKER_LIMIT_SETTING, size - 1, ByteSizeUnit.BYTES)
                  .build(),
          new NodeSettingsService(Settings.EMPTY));

  LongTermsSet termsSet = new LongTermsSet(size, hcbs.getBreaker(CircuitBreaker.REQUEST));
}
 
Example #25
Source File: BigArrays.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Adjust the circuit breaker with the given delta, if the delta is
 * negative, or checkBreaker is false, the breaker will be adjusted
 * without tripping
 */
void adjustBreaker(long delta) {
    if (this.breakerService != null) {
        CircuitBreaker breaker = this.breakerService.getBreaker(CircuitBreaker.REQUEST);
        if (this.checkBreaker == true) {
            // checking breaker means potentially tripping, but it doesn't
            // have to if the delta is negative
            if (delta > 0) {
                try {
                    breaker.addEstimateBytesAndMaybeBreak(delta, "<reused_arrays>");
                } catch (CircuitBreakingException e) {
                    // since we've already created the data, we need to
                    // add it so closing the stream re-adjusts properly
                    breaker.addWithoutBreaking(delta);
                    // re-throw the original exception
                    throw e;
                }
            } else {
                breaker.addWithoutBreaking(delta);
            }
        } else {
            // even if we are not checking the breaker, we need to adjust
            // its' totals, so add without breaking
            breaker.addWithoutBreaking(delta);
        }
    }
}
 
Example #26
Source File: GeometricMeanAggregation.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public GeometricMeanState iterate(RamAccountingContext ramAccountingContext, GeometricMeanState state, Input... args) throws CircuitBreakingException {
    if (state != null) {
        Number value = (Number) args[0].value();
        if (value != null) {
            state.addValue(value.doubleValue());
        }
    }
    return state;
}
 
Example #27
Source File: SumAggregation.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public T iterate(RamAccounting ramAccounting, MemoryManager memoryManager, T state, Input[] args) throws CircuitBreakingException {
    return reduce(ramAccounting, state, returnType.value(args[0].value()));
}
 
Example #28
Source File: MaximumAggregation.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Comparable iterate(RamAccountingContext ramAccountingContext, Comparable state, Input... args) throws CircuitBreakingException {
    Object value = args[0].value();
    return reduce(ramAccountingContext, state, (Comparable) value);
}
 
Example #29
Source File: NoneCircuitBreakerService.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public void checkParentLimit(long newBytesReserved, String label) throws CircuitBreakingException {
    // ignore
}
 
Example #30
Source File: SumAggregation.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Double iterate(RamAccountingContext ramAccountingContext, Double state, Input... args) throws CircuitBreakingException {
    return reduce(ramAccountingContext, state, DataTypes.DOUBLE.value(args[0].value()));
}