dawidwys commented on a change in pull request #13357: URL: https://github.com/apache/flink/pull/13357#discussion_r493319802
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/operators/sort/SpillingThread.java ########## @@ -0,0 +1,575 @@ +/* + * 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.flink.runtime.operators.sort; + +import org.apache.flink.api.common.typeutils.TypeComparator; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.core.memory.MemorySegment; +import org.apache.flink.runtime.io.disk.ChannelReaderInputViewIterator; +import org.apache.flink.runtime.io.disk.iomanager.BlockChannelReader; +import org.apache.flink.runtime.io.disk.iomanager.BlockChannelWriter; +import org.apache.flink.runtime.io.disk.iomanager.ChannelReaderInputView; +import org.apache.flink.runtime.io.disk.iomanager.ChannelWriterOutputView; +import org.apache.flink.runtime.io.disk.iomanager.FileIOChannel; +import org.apache.flink.runtime.io.disk.iomanager.IOManager; +import org.apache.flink.runtime.memory.MemoryManager; +import org.apache.flink.runtime.util.EmptyMutableObjectIterator; +import org.apache.flink.util.MutableObjectIterator; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; +import java.util.Queue; + +import static org.apache.flink.runtime.operators.sort.CircularElement.EOF_MARKER; +import static org.apache.flink.runtime.operators.sort.CircularElement.SPILLING_MARKER; +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * The thread that handles the spilling of intermediate results and sets up the merging. It also merges the + * channels until sufficiently few channels remain to perform the final streamed merge. + */ +final class SpillingThread<E> extends ThreadBase<E> { + /** + * An interface for injecting custom behaviour for spilling and merging phases. + */ + interface SpillingBehaviour<E> { + default void open() {} + + default void close() {} + + /** + * A method that allows adjusting the spilling phase. We can inject e.g. combining the elements while spilling. + */ + void spillBuffer( + CircularElement<E> element, + ChannelWriterOutputView output, + LargeRecordHandler<E> largeRecordHandler) throws IOException; + + /** + * A method that allows adjusting the merging phase. We can inject e.g. combining the spilled elements. + */ + void mergeRecords(MergeIterator<E> mergeIterator, ChannelWriterOutputView output) throws IOException; + } + + /** Logging. */ + private static final Logger LOG = LoggerFactory.getLogger(SpillingThread.class); + + private final MemoryManager memManager; // memory manager to release memory + + private final IOManager ioManager; // I/O manager to create channels + + private final TypeSerializer<E> serializer; // The serializer for the data type + + private final TypeComparator<E> comparator; // The comparator that establishes the order relation. + + private final List<MemorySegment> writeMemory; // memory segments for writing + + private final List<MemorySegment> mergeReadMemory; // memory segments for sorting/reading + + private final int maxFanIn; + + private final SpillChannelManager spillChannelManager; + + private final LargeRecordHandler<E> largeRecordHandler; + + private final SpillingBehaviour<E> spillingBehaviour; + + private boolean spillingBehaviourOpened = false; Review comment: Good call! I missed that. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org