AHeise 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_r400944368
 
 

 ##########
 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];
+               return new ChannelStateByteBuffer() {
+                       @Override
+                       public boolean isWritable() {
+                               return !bufferBuilder.isFull();
+                       }
+
+                       @Override
+                       public int writableBytes() {
+                               return bufferBuilder.writableBytes();
+                       }
+
+                       @Override
+                       public int writeBytes(InputStream input, int upToBytes) 
throws IOException {
+                               int left = upToBytes;
+                               for (int toRead = getToRead(left); toRead > 0; 
toRead = getToRead(left)) {
+                                       int read = input.read(buf, 0, toRead);
+                                       int copied = 
bufferBuilder.append(java.nio.ByteBuffer.wrap(buf, 0, read));
+                                       Preconditions.checkState(copied == 
read);
+                                       left -= read;
+                               }
+                               bufferBuilder.commit();
+                               return upToBytes - left;
+                       }
+
+                       private int getToRead(int bytesToRead) {
+                               return min(bytesToRead, min(buf.length, 
writableBytes()));
+                       }
+               };
+       }
+
+       static ChannelStateByteBuffer wrap(byte[] bytes) {
+               int written = 0;
+               return new ChannelStateByteBuffer() {
+                       @Override
+                       public boolean isWritable() {
+                               return written < bytes.length;
+                       }
+
+                       @Override
+                       public int writableBytes() {
+                               return bytes.length - written;
+                       }
+
+                       @Override
+                       public int writeBytes(InputStream input, int 
bytesToRead) throws IOException {
+                               return input.read(bytes, written, 
writableBytes());
+                       }
+               };
+       }
+}
+
+class ChannelStateSerializerImpl implements ChannelStateSerializer, 
ChannelStateDeserializer {
+       private static final int LEN_SIZE = Integer.BYTES;
+       private static final int SERIALIZATION_VERSION = 0;
+
+       @Override
+       public void writeHeader(DataOutputStream dataStream) throws IOException 
{
+               dataStream.writeInt(SERIALIZATION_VERSION);
+       }
+
+       @Override
+       public void writeData(DataOutputStream stream, Buffer... flinkBuffers) 
throws IOException {
+               stream.writeInt(getSize(flinkBuffers));
+               for (Buffer buffer : flinkBuffers) {
+                       ByteBuf nettyByteBuf = buffer.asByteBuf();
+                       nettyByteBuf.getBytes(nettyByteBuf.readerIndex(), 
stream, nettyByteBuf.readableBytes()); // todo: review me
 
 Review comment:
   looks okay. anything particular to check?

----------------------------------------------------------------
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

Reply via email to