wirybeaver commented on code in PR #19469:
URL: https://github.com/apache/pinot/pull/19469#discussion_r4102819968


##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/operator/AggregationSpillManager.java:
##########
@@ -0,0 +1,428 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.query.runtime.operator;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.io.BufferedInputStream;
+import java.io.DataInputStream;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.StandardOpenOption;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+import org.apache.pinot.common.datablock.DataBlock;
+import org.apache.pinot.common.datablock.DataBlockUtils;
+import org.apache.pinot.common.utils.DataSchema;
+import org.apache.pinot.core.query.aggregation.function.AggregationFunction;
+import org.apache.pinot.query.runtime.blocks.MseBlock;
+import org.apache.pinot.query.runtime.blocks.RowHeapDataBlock;
+import org.apache.pinot.query.runtime.blocks.SerializedDataBlock;
+import org.apache.pinot.spi.query.QueryThreadContext;
+import org.apache.pinot.spi.utils.CommonConstants.Server;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/// Manages hash-partitioned aggregation spill files for one operator. The 
caller must finish reading before calling
+/// [#close()], which recursively removes the operator-scoped directory. This 
class is not thread-safe.
+@SuppressWarnings("rawtypes")
+class AggregationSpillManager implements AutoCloseable {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(AggregationSpillManager.class);
+  private static final String SPILL_FILE_PREFIX = "partition-";
+  private static final String SPILL_FILE_SUFFIX = ".spill";
+  private static final String SPILL_SCOPE = "AggregationSpillManager#spill";
+  private static final String RESTORE_SCOPE = 
"AggregationSpillManager#consumePartition";
+  private static final int MAX_BUFFERED_ROWS = 1024;
+  private static final int MAX_BUFFERED_PARTITIONS = 8;
+
+  private final int _numPartitions;
+  private final int _numGroupKeys;
+  private final DataSchema _spillSchema;
+  private final AggregationFunction[] _aggFunctions;
+  private final Path _spillDirectory;
+  private final Map<Integer, FileChannel> _spillWriters = new HashMap<>();
+  private final ByteBuffer _recordLengthBuffer = 
ByteBuffer.allocate(Integer.BYTES);
+
+  AggregationSpillManager(int numPartitions, int numGroupKeys, DataSchema 
spillSchema,
+      AggregationFunction[] aggFunctions) {
+    if (numPartitions <= 0 || numPartitions > 
Server.MAX_MSE_AGGREGATION_SPILL_PARTITIONS) {
+      throw new IllegalArgumentException(
+          "Number of spill partitions must be between 1 and " + 
Server.MAX_MSE_AGGREGATION_SPILL_PARTITIONS);
+    }
+    if (numGroupKeys < 0 || numGroupKeys > spillSchema.size()) {
+      throw new IllegalArgumentException("Invalid number of group keys: " + 
numGroupKeys);
+    }
+    _numPartitions = numPartitions;
+    _numGroupKeys = numGroupKeys;
+    _spillSchema = spillSchema;
+    _aggFunctions = aggFunctions;
+    try {
+      _spillDirectory = Files.createTempDirectory("pinot-aggregation-spill-");

Review Comment:
   Done in 2e40e9402d3069aa86e22cf7e6858b904795b57f. The spill root is 
server-configurable and defaults beneath the instance data directory; an 
instance-scoped startup sweep removes orphaned spill directories. Per-query (1 
GiB default) and process-wide (8 GiB default) byte reservations stop writes 
before either configured budget is exceeded. The root must be on a dedicated 
local filesystem, not tmpfs.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to