-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESQL: Handle allocation errors inside topn #99931
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
import org.elasticsearch.indices.breaker.CircuitBreakerStats; | ||
import org.elasticsearch.test.ESTestCase; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* {@link CircuitBreakerService} that fails one twentieth of the time when you | ||
* add bytes. This is useful to make sure code responds sensibly to circuit | ||
|
@@ -27,31 +29,32 @@ public class CrankyCircuitBreakerService extends CircuitBreakerService { | |
public static final String ERROR_MESSAGE = "cranky breaker"; | ||
|
||
private final CircuitBreaker breaker = new CircuitBreaker() { | ||
@Override | ||
public void circuitBreak(String fieldName, long bytesNeeded) { | ||
private final AtomicLong used = new AtomicLong(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm modifying this so I can assert that we release all memory after we break. |
||
|
||
} | ||
@Override | ||
public void circuitBreak(String fieldName, long bytesNeeded) {} | ||
|
||
@Override | ||
public void addEstimateBytesAndMaybeBreak(long bytes, String label) throws CircuitBreakingException { | ||
if (ESTestCase.random().nextInt(20) == 0) { | ||
throw new CircuitBreakingException(ERROR_MESSAGE, Durability.PERMANENT); | ||
} | ||
used.addAndGet(bytes); | ||
} | ||
|
||
@Override | ||
public void addWithoutBreaking(long bytes) { | ||
|
||
used.addAndGet(bytes); | ||
} | ||
|
||
@Override | ||
public long getUsed() { | ||
return 0; | ||
return used.get(); | ||
} | ||
|
||
@Override | ||
public long getLimit() { | ||
return 0; | ||
return Long.MAX_VALUE; | ||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,7 @@ public BooleanBlock expand() { | |
public static long ramBytesEstimated(boolean[] values, int[] firstValueIndexes, BitSet nullsMask) { | ||
return BASE_RAM_BYTES_USED + RamUsageEstimator.sizeOf(values) + BlockRamUsageEstimator.sizeOf(firstValueIndexes) | ||
+ BlockRamUsageEstimator.sizeOfBitSet(nullsMask) + RamUsageEstimator.shallowSizeOfInstance(MvOrdering.class); | ||
// TODO mvordering is shared | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yes, of course. 👍 |
||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
|
||
package org.elasticsearch.compute.data; | ||
|
||
import org.apache.lucene.util.RamUsageEstimator; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
|
@@ -20,7 +22,7 @@ final class BooleanBlockBuilder extends AbstractBlockBuilder implements BooleanB | |
BooleanBlockBuilder(int estimatedSize, BlockFactory blockFactory) { | ||
super(blockFactory); | ||
int initialSize = Math.max(estimatedSize, 2); | ||
adjustBreaker(initialSize); | ||
adjustBreaker(RamUsageEstimator.NUM_BYTES_ARRAY_HEADER + initialSize * elementSize()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These were pretty far off so I took the liberty of making them a bit more accurate. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Thanks. |
||
values = new boolean[initialSize]; | ||
} | ||
|
||
|
@@ -192,8 +194,16 @@ public BooleanBlock build() { | |
block = new BooleanArrayBlock(values, positionCount, firstValueIndexes, nullsMask, mvOrdering, blockFactory); | ||
} | ||
} | ||
// update the breaker with the actual bytes used. | ||
blockFactory.adjustBreaker(block.ramBytesUsed() - estimatedBytes, true); | ||
/* | ||
* Update the breaker with the actual bytes used. | ||
* We pass false below even though we've used the bytes. That's weird, | ||
* but if we break here we will throw away the used memory, letting | ||
* it be deallocated. The exception will bubble up and the builder will | ||
* still technically be open, meaning the calling code should close it | ||
* which will return all used memory to the breaker. | ||
*/ | ||
blockFactory.adjustBreaker(block.ramBytesUsed() - estimatedBytes, false); | ||
built(); | ||
return block; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++