zhijiangW commented on a change in pull request #7368: [FLINK-10742][network] Let Netty use Flink's buffers directly in credit-based mode URL: https://github.com/apache/flink/pull/7368#discussion_r379996093
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/ByteBufCumulator.java ########## @@ -0,0 +1,74 @@ +/* + * 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.netty; + +import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf; +import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufAllocator; + +/** + * Utilities to cumulates data until the specified size is reached. + */ +public class ByteBufCumulator { + + /** The buffer used to cumulate data. */ + private final ByteBuf cumulationBuf; + + /** + * The flag indicating whether we are in the middle of cumulating. If + * it is true, we could continue copying the data, otherwise we should + * first reset the state of the cumulation buffer and then start copying + * data. + */ + private boolean inCumulating; + + public ByteBufCumulator(ByteBufAllocator alloc, int initBufferSize) { + this.cumulationBuf = alloc.buffer(initBufferSize); + this.inCumulating = false; + } + + public ByteBuf cumulate(ByteBuf src, int expectedSize) { Review comment: This method is more like a utility to be used in other three classes. We can also pass the `cumulationBuf` as an argument which can be allocated and maintained by upper component, and the state `inCumulating` is actually can be got via judging the passed `cumulationBuf` position. Then it is feasible to refactor it as a utility. ---------------------------------------------------------------- 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 With regards, Apache Git Services