TanYuxin-tyx commented on code in PR #22501: URL: https://github.com/apache/flink/pull/22501#discussion_r1209974054
########## flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/hybrid/tiered/storage/HashBufferAccumulator.java: ########## @@ -0,0 +1,112 @@ +/* + * 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.io.network.partition.hybrid.tiered.storage; + +import org.apache.flink.runtime.io.network.buffer.Buffer; +import org.apache.flink.runtime.io.network.buffer.BufferBuilder; +import org.apache.flink.runtime.io.network.partition.hybrid.tiered.common.TieredStorageSubpartitionId; +import org.apache.flink.util.ExceptionUtils; + +import javax.annotation.Nullable; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.List; +import java.util.function.BiConsumer; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * The hash implementation of the {@link BufferAccumulator}. The {@link BufferAccumulator} receives + * the records from {@link TieredStorageProducerClient} and the records will accumulate and + * transform to finished buffers. The accumulated buffers will be transferred to the corresponding + * tier dynamically. + * + * <p>To avoid the buffer waiting deadlock between the subpartitions, the {@link + * HashBufferAccumulator} requires at least n buffers (n is the parallelism) to make sure that each + * subpartition has at least one buffer to accumulate the receiving data. Once an accumulated buffer + * is finished, the buffer will be flushed immediately. + * + * <p>Note that this class need not be thread-safe, because it should only be accessed from the main + * thread. + */ +public class HashBufferAccumulator + implements BufferAccumulator, HashSubpartitionBufferAccumulatorContext { + + private final int bufferSize; + + private final TieredStorageMemoryManager storageMemoryManager; + + /** + * The {@link HashBufferAccumulator}'s accumulated buffer flusher is not prepared during + * construction, requiring the field to be created during setup. Therefore, it is necessary to + * verify whether this field is null before using it. + */ + @Nullable private HashSubpartitionBufferAccumulator[] hashSubpartitionBufferAccumulators; + + public HashBufferAccumulator(int bufferSize, TieredStorageMemoryManager storageMemoryManager) { + this.bufferSize = bufferSize; + this.storageMemoryManager = storageMemoryManager; + } + + @Override + public void setup( + int numSubpartitions, + BiConsumer<TieredStorageSubpartitionId, List<Buffer>> accumulatedBufferFlusher) { + this.hashSubpartitionBufferAccumulators = + new HashSubpartitionBufferAccumulator[numSubpartitions]; + + for (int i = 0; i < numSubpartitions; i++) { + hashSubpartitionBufferAccumulators[i] = + new HashSubpartitionBufferAccumulator( + new TieredStorageSubpartitionId(i), + bufferSize, + this, + accumulatedBufferFlusher); + } + } + + @Override + public void receive( + ByteBuffer record, TieredStorageSubpartitionId subpartitionId, Buffer.DataType dataType) + throws IOException { + try { + getSubpartitionHashAccumulator(subpartitionId).append(record, dataType); + } catch (InterruptedException e) { + ExceptionUtils.rethrow(e); + } + } + + @Override + public void close() { + Arrays.stream(checkNotNull(hashSubpartitionBufferAccumulators)) + .forEach(HashSubpartitionBufferAccumulator::close); + } + + @Override + public BufferBuilder requestBufferBlocking() { + return storageMemoryManager.requestBufferBlocking(this); + } + + private HashSubpartitionBufferAccumulator getSubpartitionHashAccumulator( Review Comment: Fixed. -- 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]
