zhijiangW commented on a change in pull request #11515: [FLINK-16744][task] implement channel state persistence for unaligned checkpoints URL: https://github.com/apache/flink/pull/11515#discussion_r402410595
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/checkpoint/channel/ChannelStateSerializer.java ########## @@ -0,0 +1,199 @@ +/* + * 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.checkpoint.channel; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.runtime.io.network.buffer.Buffer; +import org.apache.flink.runtime.io.network.buffer.BufferBuilder; +import org.apache.flink.util.Preconditions; + +import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf; + +import javax.annotation.concurrent.NotThreadSafe; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import static java.lang.Math.addExact; +import static java.lang.Math.min; + +interface ChannelStateSerializer { + + void writeHeader(DataOutputStream dataStream) throws IOException; + + void writeData(DataOutputStream stream, Buffer... flinkBuffers) throws IOException; +} + +interface ChannelStateDeserializer { + + void readHeader(InputStream stream) throws IOException; + + /** + * Reads the length of state. + * + * @return size of state in bytes and offset of the current position resulted from reading. + */ + Tuple2<Integer, Integer> readLength(InputStream stream) throws IOException; + + int readData(InputStream stream, ChannelStateByteBuffer buffer, int bytes) throws IOException; +} + +/** + * Wrapper around various buffers to receive channel state data. + */ +@Internal +@NotThreadSafe +interface ChannelStateByteBuffer { + + boolean isWritable(); + + int writableBytes(); + + int writeBytes(InputStream input, int bytesToRead) throws IOException; + + static ChannelStateByteBuffer wrap(Buffer buffer) { + return new ChannelStateByteBuffer() { + + private ByteBuf byteBuf = buffer.asByteBuf(); + + @Override + public boolean isWritable() { + return byteBuf.isWritable(); + } + + @Override + public int writableBytes() { + return byteBuf.writableBytes(); + } + + @Override + public int writeBytes(InputStream input, int bytesToRead) throws IOException { + return byteBuf.writeBytes(input, Math.min(bytesToRead, byteBuf.writableBytes())); + } + }; + } + + static ChannelStateByteBuffer wrap(BufferBuilder bufferBuilder) { + final byte[] buf = new byte[1024]; Review comment: TBH I am concerning of creating the temporary byte array for every buffer level, it might be not friendly for GC. And it also brings additional copy while reading. But i have not thought of a better option now. Maybe at-least to reuse the same `buf` for every wrap? ---------------------------------------------------------------- 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