NicoK commented on a change in pull request #6417: [FLINK-9913][runtime] 
Improve output serialization only once in RecordWriter
URL: https://github.com/apache/flink/pull/6417#discussion_r217406052
 
 

 ##########
 File path: 
flink-runtime/src/main/java/org/apache/flink/runtime/io/network/api/writer/RecordWriter.java
 ##########
 @@ -87,62 +86,71 @@ public RecordWriter(ResultPartitionWriter writer, 
ChannelSelector<T> channelSele
 
                this.numChannels = writer.getNumberOfSubpartitions();
 
-               /*
-                * The runtime exposes a channel abstraction for the produced 
results
-                * (see {@link ChannelSelector}). Every channel has an 
independent
-                * serializer.
-                */
-               this.serializers = new SpanningRecordSerializer[numChannels];
+               this.serializer = new SpanningRecordSerializer<T>();
                this.bufferBuilders = new Optional[numChannels];
                for (int i = 0; i < numChannels; i++) {
-                       serializers[i] = new SpanningRecordSerializer<T>();
                        bufferBuilders[i] = Optional.empty();
                }
        }
 
        public void emit(T record) throws IOException, InterruptedException {
+               serializer.serializeRecord(record);
+
                for (int targetChannel : channelSelector.selectChannels(record, 
numChannels)) {
-                       sendToTarget(record, targetChannel);
+                       copyToTarget(targetChannel);
                }
+
+               // Make sure we don't hold onto the large intermediate 
serialization buffer for too long
+               serializer.prune();
        }
 
        /**
         * This is used to broadcast Streaming Watermarks in-band with records. 
This ignores
         * the {@link ChannelSelector}.
         */
        public void broadcastEmit(T record) throws IOException, 
InterruptedException {
+               serializer.serializeRecord(record);
+
                for (int targetChannel = 0; targetChannel < numChannels; 
targetChannel++) {
-                       sendToTarget(record, targetChannel);
+                       copyToTarget(targetChannel);
                }
+
+               serializer.prune();
        }
 
        /**
         * This is used to send LatencyMarks to a random target channel.
         */
        public void randomEmit(T record) throws IOException, 
InterruptedException {
-               sendToTarget(record, rng.nextInt(numChannels));
-       }
+               serializer.serializeRecord(record);
 
-       private void sendToTarget(T record, int targetChannel) throws 
IOException, InterruptedException {
-               RecordSerializer<T> serializer = serializers[targetChannel];
+               copyToTarget(rng.nextInt(numChannels));
 
-               SerializationResult result = serializer.addRecord(record);
+               serializer.prune();
+       }
 
+       private void copyToTarget(int targetChannel) throws IOException, 
InterruptedException {
+               // We should reset the initial position of the intermediate 
serialization buffer before
+               // copying, so the serialization results can be copied to 
multiple target buffers.
+               serializer.reset();
+
+               BufferBuilder bufferBuilder = getBufferBuilder(targetChannel);
+               SerializationResult result = 
serializer.copyToBufferBuilder(bufferBuilder);
                while (result.isFullBuffer()) {
-                       if (tryFinishCurrentBufferBuilder(targetChannel, 
serializer)) {
-                               // If this was a full record, we are done. Not 
breaking
-                               // out of the loop at this point will lead to 
another
-                               // buffer request before breaking out (that 
would not be
-                               // a problem per se, but it can lead to stalls 
in the
-                               // pipeline).
-                               if (result.isFullRecord()) {
-                                       break;
-                               }
+                       tryFinishCurrentBufferBuilder(targetChannel);
 
 Review comment:
   I guess, I was worried about the same thing as @pnowojski ... the expanded 
method here will actually look like this:
   ```
                boolean pruneTriggered = false;
                BufferBuilder bufferBuilder = getBufferBuilder(targetChannel);
                SerializationResult result = 
serializer.copyToBufferBuilder(bufferBuilder);
                while (result.isFullBuffer()) {
                        if (bufferBuilders[targetChannel].isPresent()) {
                                bufferBuilder = 
bufferBuilders[targetChannel].get();
                                bufferBuilders[targetChannel] = 
Optional.empty();
                                numBytesOut.inc(bufferBuilder.finish());
                                numBuffersOut.inc();
                        }
                        tryFinishCurrentBufferBuilder(targetChannel);
   
                        // If this was a full record, we are done. Not breaking
                        // out of the loop at this point will lead to another
                        // buffer request before breaking out (that would not be
                        // a problem per se, but it can lead to stalls in the 
pipeline).
                        if (result.isFullRecord()) {
                                pruneTriggered = true;
                                break;
                        }
   
                        checkState(!bufferBuilders[targetChannel].isPresent());
                        bufferBuilder = 
targetPartition.getBufferProvider().requestBufferBuilderBlocking();
                        bufferBuilders[targetChannel] = 
Optional.of(bufferBuilder);
                        
targetPartition.addBufferConsumer(bufferBuilder.createBufferConsumer(), 
targetChannel);
                        result = serializer.copyToBufferBuilder(bufferBuilder);
                }
   ```
   
   while it could be the following in the extreme
   
   ```
                boolean pruneTriggered = false;
                BufferBuilder bufferBuilder = getBufferBuilder(targetChannel);
                SerializationResult result = 
serializer.copyToBufferBuilder(bufferBuilder);
                while (result.isFullBuffer()) {
                        numBytesOut.inc(bufferBuilder.finish());
                        numBuffersOut.inc();
   
                        // If this was a full record, we are done. Not breaking
                        // out of the loop at this point will lead to another
                        // buffer request before breaking out (that would not be
                        // a problem per se, but it can lead to stalls in the 
pipeline).
                        if (result.isFullRecord()) {
                                bufferBuilder = null;
                                pruneTriggered = true;
                                break;
                        }
   
                        bufferBuilder = 
targetPartition.getBufferProvider().requestBufferBuilderBlocking();
                        
targetPartition.addBufferConsumer(bufferBuilder.createBufferConsumer(), 
targetChannel);
                        result = serializer.copyToBufferBuilder(bufferBuilder);
                }
                bufferBuilders[targetChannel] = 
Optional.ofNullable(bufferBuilder);
   ```
   
   I'll leave it up to you whether this is actually worth it (benchmarks?) and 
feasible to abstract away a bit more nicely without adding too much complexity.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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