Skip to content
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

Multi channel group by hash first batch of optimizations #12336

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,10 @@ public static GroupByHashYieldResult finishOperatorWithYieldingGroupByHash(List<
// free the pool for the next iteration
memoryPool.free(anotherTaskId, "test", reservedMemoryInBytes);
// this required in case input is blocked
operator.getOutput();
output = operator.getOutput();
if (output != null) {
result.add(output);
}
continue;
}

Expand All @@ -140,7 +143,7 @@ public static GroupByHashYieldResult finishOperatorWithYieldingGroupByHash(List<
assertTrue(operator.getOperatorContext().isWaitingForMemory().isDone());

// assert the hash capacity is not changed; otherwise, we should have yielded
assertTrue(oldCapacity == getHashCapacity.apply(operator));
assertEquals((int) getHashCapacity.apply(operator), oldCapacity);

// We are not going to rehash; therefore, assert the memory increase only comes from the aggregator
assertLessThan(actualIncreasedMemory, additionalMemoryInBytes);
Expand All @@ -164,8 +167,8 @@ public static GroupByHashYieldResult finishOperatorWithYieldingGroupByHash(List<
expectedReservedExtraBytes = oldCapacity * (long) (Long.BYTES * 1.75 + Integer.BYTES) + page.getRetainedSizeInBytes();
}
else {
// groupAddressByHash, groupIdsByHash, and rawHashByHashPosition double by hashCapacity; while groupAddressByGroupId double by maxFill = hashCapacity / 0.75
expectedReservedExtraBytes = oldCapacity * (long) (Long.BYTES * 1.75 + Integer.BYTES + Byte.BYTES) + page.getRetainedSizeInBytes();
// groupIdsByHash, and rawHashByHashPosition double by hashCapacity
expectedReservedExtraBytes = oldCapacity * (long) (Integer.BYTES + Byte.BYTES);
}
assertBetweenInclusive(actualIncreasedMemory, expectedReservedExtraBytes, expectedReservedExtraBytes + additionalMemoryInBytes);

Expand All @@ -187,10 +190,24 @@ public static GroupByHashYieldResult finishOperatorWithYieldingGroupByHash(List<

// Assert the estimated reserved memory before rehash is very close to the one after rehash
long rehashedMemoryUsage = operator.getOperatorContext().getDriverContext().getMemoryUsage();
assertBetweenInclusive(rehashedMemoryUsage * 1.0 / newMemoryUsage, 0.99, 1.01);
double memoryUsageErrorUpperBound = 1.01;
double memoryUsageError = rehashedMemoryUsage * 1.0 / newMemoryUsage;
if (memoryUsageError > memoryUsageErrorUpperBound) {
// Usually the error is < 1%, but since MultiChannelGroupByHash.getEstimatedSize
// accounts for changes in completedPagesMemorySize, which is increased if new page is
// added by addNewGroup (an even that cannot be predicted as it depends on the number of unique groups
// in the current page being processed), the difference includes size of the added new page.
// Lower bound is 1% lower than normal because additionalMemoryInBytes includes also aggregator state.
assertBetweenInclusive(rehashedMemoryUsage * 1.0 / (newMemoryUsage + additionalMemoryInBytes), 0.98, memoryUsageErrorUpperBound,
sopel39 marked this conversation as resolved.
Show resolved Hide resolved
"rehashedMemoryUsage " + rehashedMemoryUsage + ", newMemoryUsage: " + newMemoryUsage);
}
else {
assertBetweenInclusive(memoryUsageError, 0.99, memoryUsageErrorUpperBound);
}

// unblocked
assertTrue(operator.needsInput());
assertTrue(operator.getOperatorContext().isWaitingForMemory().isDone());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import java.util.concurrent.ScheduledExecutorService;

import static io.airlift.concurrent.Threads.daemonThreadsNamed;
import static io.airlift.testing.Assertions.assertGreaterThan;
import static io.airlift.testing.Assertions.assertGreaterThanOrEqual;
import static io.trino.RowPagesBuilder.rowPagesBuilder;
import static io.trino.SessionTestUtils.TEST_SESSION;
import static io.trino.operator.GroupByHashYieldAssertion.createPagesWithDistinctHashKeys;
Expand Down Expand Up @@ -167,9 +167,9 @@ public void testMemoryReservationYield(Type type)
joinCompiler,
blockTypeOperators);

GroupByHashYieldAssertion.GroupByHashYieldResult result = finishOperatorWithYieldingGroupByHash(input, type, operatorFactory, operator -> ((DistinctLimitOperator) operator).getCapacity(), 1_400_000);
assertGreaterThan(result.getYieldCount(), 5);
assertGreaterThan(result.getMaxReservedBytes(), 20L << 20);
GroupByHashYieldAssertion.GroupByHashYieldResult result = finishOperatorWithYieldingGroupByHash(input, type, operatorFactory, operator -> ((DistinctLimitOperator) operator).getCapacity(), 450_000);
assertGreaterThanOrEqual(result.getYieldCount(), 5);
assertGreaterThanOrEqual(result.getMaxReservedBytes(), 20L << 20);
sopel39 marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(result.getOutput().stream().mapToInt(Page::getPositionCount).sum(), 6_000 * 600);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import static io.airlift.slice.SizeOf.SIZE_OF_LONG;
import static io.airlift.testing.Assertions.assertEqualsIgnoreOrder;
import static io.airlift.testing.Assertions.assertGreaterThan;
import static io.airlift.testing.Assertions.assertGreaterThanOrEqual;
import static io.airlift.units.DataSize.Unit.KILOBYTE;
import static io.airlift.units.DataSize.Unit.MEGABYTE;
import static io.airlift.units.DataSize.succinctBytes;
Expand Down Expand Up @@ -410,9 +411,9 @@ public void testMemoryReservationYield(Type type)

// get result with yield; pick a relatively small buffer for aggregator's memory usage
GroupByHashYieldResult result;
result = finishOperatorWithYieldingGroupByHash(input, type, operatorFactory, this::getHashCapacity, 1_400_000);
assertGreaterThan(result.getYieldCount(), 5);
assertGreaterThan(result.getMaxReservedBytes(), 20L << 20);
result = finishOperatorWithYieldingGroupByHash(input, type, operatorFactory, this::getHashCapacity, 450_000);
assertGreaterThanOrEqual(result.getYieldCount(), 5);
assertGreaterThanOrEqual(result.getMaxReservedBytes(), 20L << 20);

int count = 0;
for (Page page : result.getOutput()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

import static com.google.common.collect.Iterables.concat;
import static io.airlift.concurrent.Threads.daemonThreadsNamed;
import static io.airlift.testing.Assertions.assertGreaterThan;
import static io.airlift.testing.Assertions.assertGreaterThanOrEqual;
import static io.trino.RowPagesBuilder.rowPagesBuilder;
import static io.trino.SessionTestUtils.TEST_SESSION;
Expand Down Expand Up @@ -244,10 +243,10 @@ public void testSemiJoinMemoryReservationYield(Type type)
type,
setBuilderOperatorFactory,
operator -> ((SetBuilderOperator) operator).getCapacity(),
1_400_000);
450_000);

assertGreaterThanOrEqual(result.getYieldCount(), 5);
assertGreaterThan(result.getMaxReservedBytes(), 20L << 20);
assertGreaterThanOrEqual(result.getYieldCount(), 4);
assertGreaterThanOrEqual(result.getMaxReservedBytes(), 20L << 19);
sopel39 marked this conversation as resolved.
Show resolved Hide resolved
assertEquals(result.getOutput().stream().mapToInt(Page::getPositionCount).sum(), 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

import static com.google.common.base.Throwables.throwIfUnchecked;
import static io.airlift.concurrent.Threads.daemonThreadsNamed;
import static io.airlift.testing.Assertions.assertGreaterThan;
import static io.airlift.testing.Assertions.assertGreaterThanOrEqual;
import static io.airlift.testing.Assertions.assertInstanceOf;
import static io.trino.RowPagesBuilder.rowPagesBuilder;
import static io.trino.SessionTestUtils.TEST_SESSION;
Expand Down Expand Up @@ -176,9 +176,9 @@ public void testMemoryReservationYield(Type type)
OperatorFactory operatorFactory = new MarkDistinctOperatorFactory(0, new PlanNodeId("test"), ImmutableList.of(type), ImmutableList.of(0), Optional.of(1), joinCompiler, blockTypeOperators);

// get result with yield; pick a relatively small buffer for partitionRowCount's memory usage
GroupByHashYieldAssertion.GroupByHashYieldResult result = finishOperatorWithYieldingGroupByHash(input, type, operatorFactory, operator -> ((MarkDistinctOperator) operator).getCapacity(), 1_400_000);
assertGreaterThan(result.getYieldCount(), 5);
assertGreaterThan(result.getMaxReservedBytes(), 20L << 20);
GroupByHashYieldAssertion.GroupByHashYieldResult result = finishOperatorWithYieldingGroupByHash(input, type, operatorFactory, operator -> ((MarkDistinctOperator) operator).getCapacity(), 450_000);
assertGreaterThanOrEqual(result.getYieldCount(), 5);
assertGreaterThanOrEqual(result.getMaxReservedBytes(), 20L << 20);

int count = 0;
for (Page page : result.getOutput()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static io.airlift.concurrent.Threads.daemonThreadsNamed;
import static io.airlift.testing.Assertions.assertEqualsIgnoreOrder;
import static io.airlift.testing.Assertions.assertGreaterThan;
import static io.airlift.testing.Assertions.assertGreaterThanOrEqual;
import static io.trino.RowPagesBuilder.rowPagesBuilder;
import static io.trino.SessionTestUtils.TEST_SESSION;
import static io.trino.operator.GroupByHashYieldAssertion.createPagesWithDistinctHashKeys;
Expand Down Expand Up @@ -171,9 +171,9 @@ public void testMemoryReservationYield(Type type)
blockTypeOperators);

// get result with yield; pick a relatively small buffer for partitionRowCount's memory usage
GroupByHashYieldAssertion.GroupByHashYieldResult result = finishOperatorWithYieldingGroupByHash(input, type, operatorFactory, operator -> ((RowNumberOperator) operator).getCapacity(), 1_400_000);
assertGreaterThan(result.getYieldCount(), 5);
assertGreaterThan(result.getMaxReservedBytes(), 20L << 20);
GroupByHashYieldAssertion.GroupByHashYieldResult result = finishOperatorWithYieldingGroupByHash(input, type, operatorFactory, operator -> ((RowNumberOperator) operator).getCapacity(), 280_000);
assertGreaterThanOrEqual(result.getYieldCount(), 5);
assertGreaterThanOrEqual(result.getMaxReservedBytes(), 20L << 20);

int count = 0;
for (Page page : result.getOutput()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public void testMemoryReservationYield()
type,
operatorFactory,
operator -> ((TopNRankingOperator) operator).getGroupedTopNBuilder() == null ? 0 : ((GroupedTopNRowNumberBuilder) ((TopNRankingOperator) operator).getGroupedTopNBuilder()).getGroupByHash().getCapacity(),
1_000_000);
lukasz-stec marked this conversation as resolved.
Show resolved Hide resolved
450_000);
assertGreaterThan(result.getYieldCount(), 3);
assertGreaterThan(result.getMaxReservedBytes(), 5L << 20);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ public void testClusterPools()

List<Future<?>> queryFutures = new ArrayList<>();
for (int i = 0; i < 2; i++) {
queryFutures.add(executor.submit(() -> queryRunner.execute("SELECT COUNT(*), clerk FROM orders GROUP BY clerk")));
// for this test to work, the query has to have enough groups for HashAggregationOperator to go over QueryContext.GUARANTEED_MEMORY
queryFutures.add(executor.submit(() -> queryRunner.execute("SELECT COUNT(*), cast(orderkey as varchar), partkey FROM lineitem GROUP BY cast(orderkey as varchar), partkey")));
}

ClusterMemoryManager memoryManager = queryRunner.getCoordinator().getClusterMemoryManager();
Expand Down