Skip to content

Commit

Permalink
ESQL: Test evaluators with cranky breaker
Browse files Browse the repository at this point in the history
This tests our `ExpressionEvaluator`s with a `CrankyCircuitBreaker`
which fails 5% of the time it is called, simulating a cluster that is
close to running out of memory and servicing many requests. It found
some errors! And this fixes them.
  • Loading branch information
nik9000 committed Oct 4, 2023
1 parent 60daef3 commit 1d38b7a
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public Block.Ref dedupeToBlockAdaptive(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
BytesRefBlock.Builder builder = BytesRefBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; BytesRefBlock.Builder builder = BytesRefBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand Down Expand Up @@ -100,8 +99,7 @@ public Block.Ref dedupeToBlockUsingCopyAndSort(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
BytesRefBlock.Builder builder = BytesRefBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; BytesRefBlock.Builder builder = BytesRefBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand Down Expand Up @@ -130,8 +128,7 @@ public Block.Ref dedupeToBlockUsingCopyMissing(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
BytesRefBlock.Builder builder = BytesRefBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; BytesRefBlock.Builder builder = BytesRefBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand All @@ -153,32 +150,33 @@ public Block.Ref dedupeToBlockUsingCopyMissing(BlockFactory blockFactory) {
* as the grouping block to a {@link GroupingAggregatorFunction}.
*/
public MultivalueDedupe.HashResult hash(BytesRefHash hash) {
IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount());
boolean sawNull = false;
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
switch (count) {
case 0 -> {
sawNull = true;
builder.appendInt(0);
}
case 1 -> {
BytesRef v = block.getBytesRef(first, work[0]);
hash(builder, hash, v);
}
default -> {
if (count < ALWAYS_COPY_MISSING) {
copyMissing(first, count);
hashUniquedWork(hash, builder);
} else {
copyAndSort(first, count);
hashSortedWork(hash, builder);
try (IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount())) {
boolean sawNull = false;
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
switch (count) {
case 0 -> {
sawNull = true;
builder.appendInt(0);
}
case 1 -> {
BytesRef v = block.getBytesRef(first, work[0]);
hash(builder, hash, v);
}
default -> {
if (count < ALWAYS_COPY_MISSING) {
copyMissing(first, count);
hashUniquedWork(hash, builder);
} else {
copyAndSort(first, count);
hashSortedWork(hash, builder);
}
}
}
}
return new MultivalueDedupe.HashResult(builder.build(), sawNull);
}
return new MultivalueDedupe.HashResult(builder.build(), sawNull);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public Block.Ref dedupeToBlockAdaptive(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
DoubleBlock.Builder builder = DoubleBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; DoubleBlock.Builder builder = DoubleBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand Down Expand Up @@ -97,8 +96,7 @@ public Block.Ref dedupeToBlockUsingCopyAndSort(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
DoubleBlock.Builder builder = DoubleBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; DoubleBlock.Builder builder = DoubleBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand Down Expand Up @@ -127,8 +125,7 @@ public Block.Ref dedupeToBlockUsingCopyMissing(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
DoubleBlock.Builder builder = DoubleBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; DoubleBlock.Builder builder = DoubleBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand All @@ -150,32 +147,33 @@ public Block.Ref dedupeToBlockUsingCopyMissing(BlockFactory blockFactory) {
* as the grouping block to a {@link GroupingAggregatorFunction}.
*/
public MultivalueDedupe.HashResult hash(LongHash hash) {
IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount());
boolean sawNull = false;
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
switch (count) {
case 0 -> {
sawNull = true;
builder.appendInt(0);
}
case 1 -> {
double v = block.getDouble(first);
hash(builder, hash, v);
}
default -> {
if (count < ALWAYS_COPY_MISSING) {
copyMissing(first, count);
hashUniquedWork(hash, builder);
} else {
copyAndSort(first, count);
hashSortedWork(hash, builder);
try (IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount())) {
boolean sawNull = false;
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
switch (count) {
case 0 -> {
sawNull = true;
builder.appendInt(0);
}
case 1 -> {
double v = block.getDouble(first);
hash(builder, hash, v);
}
default -> {
if (count < ALWAYS_COPY_MISSING) {
copyMissing(first, count);
hashUniquedWork(hash, builder);
} else {
copyAndSort(first, count);
hashSortedWork(hash, builder);
}
}
}
}
return new MultivalueDedupe.HashResult(builder.build(), sawNull);
}
return new MultivalueDedupe.HashResult(builder.build(), sawNull);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ public Block.Ref dedupeToBlockAdaptive(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand Down Expand Up @@ -96,8 +95,7 @@ public Block.Ref dedupeToBlockUsingCopyAndSort(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand Down Expand Up @@ -126,8 +124,7 @@ public Block.Ref dedupeToBlockUsingCopyMissing(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand All @@ -149,32 +146,33 @@ public Block.Ref dedupeToBlockUsingCopyMissing(BlockFactory blockFactory) {
* as the grouping block to a {@link GroupingAggregatorFunction}.
*/
public MultivalueDedupe.HashResult hash(LongHash hash) {
IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount());
boolean sawNull = false;
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
switch (count) {
case 0 -> {
sawNull = true;
builder.appendInt(0);
}
case 1 -> {
int v = block.getInt(first);
hash(builder, hash, v);
}
default -> {
if (count < ALWAYS_COPY_MISSING) {
copyMissing(first, count);
hashUniquedWork(hash, builder);
} else {
copyAndSort(first, count);
hashSortedWork(hash, builder);
try (IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount())) {
boolean sawNull = false;
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
switch (count) {
case 0 -> {
sawNull = true;
builder.appendInt(0);
}
case 1 -> {
int v = block.getInt(first);
hash(builder, hash, v);
}
default -> {
if (count < ALWAYS_COPY_MISSING) {
copyMissing(first, count);
hashUniquedWork(hash, builder);
} else {
copyAndSort(first, count);
hashSortedWork(hash, builder);
}
}
}
}
return new MultivalueDedupe.HashResult(builder.build(), sawNull);
}
return new MultivalueDedupe.HashResult(builder.build(), sawNull);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public Block.Ref dedupeToBlockAdaptive(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
LongBlock.Builder builder = LongBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; LongBlock.Builder builder = LongBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand Down Expand Up @@ -98,8 +97,7 @@ public Block.Ref dedupeToBlockUsingCopyAndSort(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
LongBlock.Builder builder = LongBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; LongBlock.Builder builder = LongBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand Down Expand Up @@ -128,8 +126,7 @@ public Block.Ref dedupeToBlockUsingCopyMissing(BlockFactory blockFactory) {
if (block.mvDeduplicated()) {
return ref;
}
try (ref) {
LongBlock.Builder builder = LongBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; LongBlock.Builder builder = LongBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand All @@ -151,32 +148,33 @@ public Block.Ref dedupeToBlockUsingCopyMissing(BlockFactory blockFactory) {
* as the grouping block to a {@link GroupingAggregatorFunction}.
*/
public MultivalueDedupe.HashResult hash(LongHash hash) {
IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount());
boolean sawNull = false;
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
switch (count) {
case 0 -> {
sawNull = true;
builder.appendInt(0);
}
case 1 -> {
long v = block.getLong(first);
hash(builder, hash, v);
}
default -> {
if (count < ALWAYS_COPY_MISSING) {
copyMissing(first, count);
hashUniquedWork(hash, builder);
} else {
copyAndSort(first, count);
hashSortedWork(hash, builder);
try (IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount())) {
boolean sawNull = false;
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
switch (count) {
case 0 -> {
sawNull = true;
builder.appendInt(0);
}
case 1 -> {
long v = block.getLong(first);
hash(builder, hash, v);
}
default -> {
if (count < ALWAYS_COPY_MISSING) {
copyMissing(first, count);
hashUniquedWork(hash, builder);
} else {
copyAndSort(first, count);
hashSortedWork(hash, builder);
}
}
}
}
return new MultivalueDedupe.HashResult(builder.build(), sawNull);
}
return new MultivalueDedupe.HashResult(builder.build(), sawNull);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public Block.Ref dedupeToBlock(BlockFactory blockFactory) {
if (false == block.mayHaveMultivaluedFields()) {
return ref;
}
try (ref) {
BooleanBlock.Builder builder = BooleanBlock.newBlockBuilder(block.getPositionCount(), blockFactory);
try (ref; BooleanBlock.Builder builder = BooleanBlock.newBlockBuilder(block.getPositionCount(), blockFactory)) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
Expand All @@ -72,23 +71,24 @@ public Block.Ref dedupeToBlock(BlockFactory blockFactory) {
* @param everSeen array tracking if the values {@code false} and {@code true} are ever seen
*/
public IntBlock hash(boolean[] everSeen) {
IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount());
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
switch (count) {
case 0 -> {
everSeen[NULL_ORD] = true;
builder.appendInt(NULL_ORD);
}
case 1 -> builder.appendInt(hashOrd(everSeen, block.getBoolean(first)));
default -> {
readValues(first, count);
hashValues(everSeen, builder);
try (IntBlock.Builder builder = IntBlock.newBlockBuilder(block.getPositionCount())) {
for (int p = 0; p < block.getPositionCount(); p++) {
int count = block.getValueCount(p);
int first = block.getFirstValueIndex(p);
switch (count) {
case 0 -> {
everSeen[NULL_ORD] = true;
builder.appendInt(NULL_ORD);
}
case 1 -> builder.appendInt(hashOrd(everSeen, block.getBoolean(first)));
default -> {
readValues(first, count);
hashValues(everSeen, builder);
}
}
}
return builder.build();
}
return builder.build();
}

/**
Expand Down
Loading

0 comments on commit 1d38b7a

Please sign in to comment.