Skip to content

Commit

Permalink
builders and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHegarty committed Oct 2, 2023
1 parent 77c6e59 commit 7192bc0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ void adjustBreaker(final long delta, final boolean isDataAlreadyCreated) {
try {
breaker.addEstimateBytesAndMaybeBreak(delta, "<esql_block_factory>");
} 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);
}
// if (isDataAlreadyCreated) { // TODO: remove 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,39 @@ public static List<Object[]> params() {

private final ElementType elementType;

BlockFactory blockFactory = BlockFactoryTests.blockFactory(ByteSizeValue.ofGb(1));

public BlockBuilderTests(ElementType elementType) {
this.elementType = elementType;
}

public void testAllNulls() {
for (int numEntries : List.of(1, randomIntBetween(1, 100))) {
testAllNullsImpl(elementType.newBlockBuilder(0), numEntries);
testAllNullsImpl(elementType.newBlockBuilder(100), numEntries);
testAllNullsImpl(elementType.newBlockBuilder(1000), numEntries);
testAllNullsImpl(elementType.newBlockBuilder(randomIntBetween(0, 100)), numEntries);
testAllNullsImpl(elementType.newBlockBuilder(0, blockFactory), numEntries);
testAllNullsImpl(elementType.newBlockBuilder(100, blockFactory), numEntries);
testAllNullsImpl(elementType.newBlockBuilder(1000, blockFactory), numEntries);
testAllNullsImpl(elementType.newBlockBuilder(randomIntBetween(0, 100), blockFactory), numEntries);
}
}

private void testAllNullsImpl(Block.Builder builder, int numEntries) {
for (int i = 0; i < numEntries; i++) {
builder.appendNull();
}
Block block = builder.build();
assertThat(block.getPositionCount(), is(numEntries));
assertThat(block.isNull(0), is(true));
assertThat(block.isNull(numEntries - 1), is(true));
assertThat(block.isNull(randomPosition(numEntries)), is(true));
try (Block block = builder.build()) {
assertThat(block.getPositionCount(), is(numEntries));
assertThat(block.isNull(0), is(true));
assertThat(block.isNull(numEntries - 1), is(true));
assertThat(block.isNull(randomPosition(numEntries)), is(true));
}
assertThat(blockFactory.breaker().getUsed(), equalTo(0L));
}

static int randomPosition(int positionCount) {
return positionCount == 1 ? 0 : randomIntBetween(0, positionCount - 1);
}

public void testCloseWithoutBuilding() {
BlockFactory blockFactory = BlockFactoryTests.blockFactory(ByteSizeValue.ofGb(1));
elementType.newBlockBuilder(10, blockFactory).close();
assertThat(blockFactory.breaker().getUsed(), equalTo(0L));
}
Expand Down Expand Up @@ -110,7 +113,6 @@ public void testBuildSingle() {
}

private void testBuild(int size, boolean nullable, int maxValueCount) {
BlockFactory blockFactory = BlockFactoryTests.blockFactory(ByteSizeValue.ofGb(1));
try (Block.Builder builder = elementType.newBlockBuilder(randomBoolean() ? size : 1, blockFactory)) {
BasicBlockTests.RandomBlock random = BasicBlockTests.randomBlock(elementType, size, nullable, 1, maxValueCount, 0, 0);
builder.copyFrom(random.block(), 0, random.block().getPositionCount());
Expand All @@ -124,7 +126,6 @@ private void testBuild(int size, boolean nullable, int maxValueCount) {
}

public void testDoubleBuild() {
BlockFactory blockFactory = BlockFactoryTests.blockFactory(ByteSizeValue.ofGb(1));
try (Block.Builder builder = elementType.newBlockBuilder(10, blockFactory)) {
BasicBlockTests.RandomBlock random = BasicBlockTests.randomBlock(elementType, 10, false, 1, 1, 0, 0);
builder.copyFrom(random.block(), 0, random.block().getPositionCount());
Expand All @@ -142,19 +143,43 @@ public void testDoubleBuild() {
public void testCranky() {
BigArrays bigArrays = new MockBigArrays(PageCacheRecycler.NON_RECYCLING_INSTANCE, new CrankyCircuitBreakerService());
BlockFactory blockFactory = new BlockFactory(bigArrays.breakerService().getBreaker(CircuitBreaker.REQUEST), bigArrays);
try {
try (Block.Builder builder = elementType.newBlockBuilder(10, blockFactory)) {
BasicBlockTests.RandomBlock random = BasicBlockTests.randomBlock(elementType, 10, false, 1, 1, 0, 0);
builder.copyFrom(random.block(), 0, random.block().getPositionCount());
try (Block built = builder.build()) {
assertThat(built, equalTo(random.block()));
for (int i = 0; i < 100; i++) {
try {
try (Block.Builder builder = elementType.newBlockBuilder(10, blockFactory)) {
BasicBlockTests.RandomBlock random = BasicBlockTests.randomBlock(elementType, 10, false, 1, 1, 0, 0);
builder.copyFrom(random.block(), 0, random.block().getPositionCount());
try (Block built = builder.build()) {
assertThat(built, equalTo(random.block()));
}
}
// If we made it this far cranky didn't fail us!
} catch (CircuitBreakingException e) {
logger.info("cranky", e);
assertThat(e.getMessage(), equalTo(CrankyCircuitBreakerService.ERROR_MESSAGE));
}
// If we made it this far cranky didn't fail us!
} catch (CircuitBreakingException e) {
logger.info("cranky", e);
assertThat(e.getMessage(), equalTo(CrankyCircuitBreakerService.ERROR_MESSAGE));
assertThat(blockFactory.breaker().getUsed(), equalTo(0L));
}
}

public void testCrankyConstantBlock() {
BigArrays bigArrays = new MockBigArrays(PageCacheRecycler.NON_RECYCLING_INSTANCE, new CrankyCircuitBreakerService());
BlockFactory blockFactory = new BlockFactory(bigArrays.breakerService().getBreaker(CircuitBreaker.REQUEST), bigArrays);
for (int i = 0; i < 100; i++) {
try {
try (Block.Builder builder = elementType.newBlockBuilder(randomInt(10), blockFactory)) {
BasicBlockTests.RandomBlock random = BasicBlockTests.randomBlock(elementType, 1, false, 1, 1, 0, 0);
builder.copyFrom(random.block(), 0, random.block().getPositionCount());
try (Block built = builder.build()) {
assertThat(built.asVector().isConstant(), is(true));
assertThat(built, equalTo(random.block()));
}
}
// If we made it this far cranky didn't fail us!
} catch (CircuitBreakingException e) {
logger.info("cranky", e);
assertThat(e.getMessage(), equalTo(CrankyCircuitBreakerService.ERROR_MESSAGE));
}
assertThat(blockFactory.breaker().getUsed(), equalTo(0L));
}
assertThat(blockFactory.breaker().getUsed(), equalTo(0L));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,20 +93,22 @@ public void testDoubleBuild() {
public void testCranky() {
BigArrays bigArrays = new MockBigArrays(PageCacheRecycler.NON_RECYCLING_INSTANCE, new CrankyCircuitBreakerService());
BlockFactory blockFactory = new BlockFactory(bigArrays.breakerService().getBreaker(CircuitBreaker.REQUEST), bigArrays);
try {
try (Vector.Builder builder = vectorBuilder(10, blockFactory)) {
BasicBlockTests.RandomBlock random = BasicBlockTests.randomBlock(elementType, 10, false, 1, 1, 0, 0);
fill(builder, random.block().asVector());
try (Vector built = builder.build()) {
assertThat(built, equalTo(random.block().asVector()));
for (int i = 0; i < 100; i++) {
try {
try (Vector.Builder builder = vectorBuilder(10, blockFactory)) {
BasicBlockTests.RandomBlock random = BasicBlockTests.randomBlock(elementType, 10, false, 1, 1, 0, 0);
fill(builder, random.block().asVector());
try (Vector built = builder.build()) {
assertThat(built, equalTo(random.block().asVector()));
}
}
// If we made it this far cranky didn't fail us!
} catch (CircuitBreakingException e) {
logger.info("cranky", e);
assertThat(e.getMessage(), equalTo(CrankyCircuitBreakerService.ERROR_MESSAGE));
}
// If we made it this far cranky didn't fail us!
} catch (CircuitBreakingException e) {
logger.info("cranky", e);
assertThat(e.getMessage(), equalTo(CrankyCircuitBreakerService.ERROR_MESSAGE));
assertThat(blockFactory.breaker().getUsed(), equalTo(0L));
}
assertThat(blockFactory.breaker().getUsed(), equalTo(0L));
}

private Vector.Builder vectorBuilder(int estimatedSize, BlockFactory blockFactory) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,21 @@ public void testDoubleBuild() {
public void testCranky() {
BigArrays bigArrays = new MockBigArrays(PageCacheRecycler.NON_RECYCLING_INSTANCE, new CrankyCircuitBreakerService());
BlockFactory blockFactory = new BlockFactory(bigArrays.breakerService().getBreaker(CircuitBreaker.REQUEST), bigArrays);
try {
Vector.Builder builder = vectorBuilder(10, blockFactory);
BasicBlockTests.RandomBlock random = BasicBlockTests.randomBlock(elementType, 10, false, 1, 1, 0, 0);
fill(builder, random.block().asVector());
try (Vector built = builder.build()) {
assertThat(built, equalTo(random.block().asVector()));
for (int i = 0; i < 100; i++) {
try {
Vector.Builder builder = vectorBuilder(10, blockFactory);
BasicBlockTests.RandomBlock random = BasicBlockTests.randomBlock(elementType, 10, false, 1, 1, 0, 0);
fill(builder, random.block().asVector());
try (Vector built = builder.build()) {
assertThat(built, equalTo(random.block().asVector()));
}
// If we made it this far cranky didn't fail us!
} catch (CircuitBreakingException e) {
logger.info("cranky", e);
assertThat(e.getMessage(), equalTo(CrankyCircuitBreakerService.ERROR_MESSAGE));
}
// If we made it this far cranky didn't fail us!
} catch (CircuitBreakingException e) {
logger.info("cranky", e);
assertThat(e.getMessage(), equalTo(CrankyCircuitBreakerService.ERROR_MESSAGE));
assertThat(blockFactory.breaker().getUsed(), equalTo(0L));
}
assertThat(blockFactory.breaker().getUsed(), equalTo(0L));
}

private Vector.Builder vectorBuilder(int size, BlockFactory blockFactory) {
Expand Down

0 comments on commit 7192bc0

Please sign in to comment.