You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/10/13 12:02:07 UTC

[GitHub] [flink] curcur opened a new pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

curcur opened a new pull request #13614:
URL: https://github.com/apache/flink/pull/13614


   ## What is the purpose of the change
   
   Partial records happen if a record can not fit into one buffer, then the remaining part of the same record is put into the next buffer. Hence partial records only exist at the beginning of a buffer. Partial record clean-up is needed in the mode of approximate local recovery. If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
   
   `partialRecordLength` is the length of bytes to skip in order to start with a complete record, from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate local recovery to find the start position of a complete record on a BufferConsumer, so-called `partial record clean-up`.
   
   ## Brief change log
   - API change to add `partialRecordLength` when creating a BufferConsumer
   - Add `partialRecordLength` in `BufferWritingResultPartition` when emitRecord and `broadcastRecord`
   - Update the type of buffers in PipelinedSubpartition fr`PrioritizedDeque<BufferConsumer>` to `PrioritizedDeque<BufferConsumerWithPartialRecordLength>`
   - Implement partial record clean-up logic in BufferConsumerWithPartialRecordLength
   
   ## Verifying this change
   
   - add unit tests to test partial record length
   - pass existing tests to make sure the changes do not affect data write/read/transition.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (no)
     - The serializers: (no)
     - The runtime per-record code paths (performance sensitive): (yes)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn/Mesos, ZooKeeper: (no)
     - The S3 file system connector: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (no)
   
   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 47d3d60c3dc74ab402ce657c31c27568b190de40 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504675916



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       that would be perfect if we do not have `bytesToSkip == buffer.getMaxCapacity()`.
   
   In the current build(), currentReaderPosition can never be `buffer.getMaxCapacity()`, because the consumer has already been marked as finished `currentReaderPosition = cachedWriterPosition;` if the full buffer is read. Remember in case of a record spanning over buffers, we finish the builder first, and then request a new builder.
   
   But I can still handle this special case in build() if you are fine to change the build() method. I was trying to not touch the build() method which is used in the normal PipelinedSubPartition read




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r508268432



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);
 		}
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		checkInProduceState();
-		ensureUnicastMode();
+	private BufferBuilder appendDataToSubpartitionForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return buffer;
+	}
 
-		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
-		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
-		return bufferBuilder;
+	private BufferBuilder appendDataToBroadcastForNewRecord(ByteBuffer record) throws IOException {
+		BufferBuilder buffer = broadcastBufferBuilder;
+
+		if (buffer == null) {
+			buffer = requestNewBroadcastBufferBuilder();
+			createBroadcastBufferConsumers(buffer, 0);
+		}
+
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder appendDataToBroadcastForRecordContinuation(
+			final ByteBuffer remainingRecordBytes) throws IOException {
+		final BufferBuilder buffer = requestNewBroadcastBufferBuilder();
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		createBroadcastBufferConsumers(buffer, partialRecordBytes);
+
+		return buffer;
+	}
+
+	private void createBroadcastBufferConsumers(BufferBuilder buffer, int partialRecordBytes) throws IOException {
+		try (final BufferConsumer consumer = buffer.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), partialRecordBytes);
+			}
 		}
+	}
 
-		return getNewBroadcastBufferBuilder();
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+		checkInProduceState();
+		ensureUnicastMode();

Review comment:
       Sure, do you mind my changing the name within the same PR?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707692926


   Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
   to review your pull request. We will use this comment to track the progress of the review.
   
   
   ## Automated Checks
   Last check on commit a70ae897b37437579d8aab2ba3a744553a08feea (Fri Oct 16 10:57:49 UTC 2020)
   
   **Warnings:**
    * No documentation files were touched! Remember to keep the Flink docs up to date!
    * **This pull request references an unassigned [Jira ticket](https://issues.apache.org/jira/browse/FLINK-19547).** According to the [code contribution guide](https://flink.apache.org/contributing/contribute-code.html), tickets need to be assigned before starting with the implementation work.
   
   
   <sub>Mention the bot in a comment to re-run the automated checks.</sub>
   ## Review Progress
   
   * ❓ 1. The [description] looks good.
   * ❓ 2. There is [consensus] that the contribution should go into to Flink.
   * ❓ 3. Needs [attention] from.
   * ❓ 4. The change fits into the overall [architecture].
   * ❓ 5. Overall code [quality] is good.
   
   Please see the [Pull Request Review Guide](https://flink.apache.org/contributing/reviewing-prs.html) for a full explanation of the review process.<details>
    The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot approve description` to approve one or more aspects (aspects: `description`, `consensus`, `architecture` and `quality`)
    - `@flinkbot approve all` to approve all aspects
    - `@flinkbot approve-until architecture` to approve everything until `architecture`
    - `@flinkbot attention @username1 [@username2 ..]` to require somebody's attention
    - `@flinkbot disapprove architecture` to remove an approval you gave earlier
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r505277251



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -128,26 +128,40 @@ protected void flushAllSubpartitions(boolean finishProducers) {
 
 	@Override
 	public void emitRecord(ByteBuffer record, int targetSubpartition) throws IOException {
-		do {
-			final BufferBuilder bufferBuilder = getSubpartitionBufferBuilder(targetSubpartition);
-			bufferBuilder.appendAndCommit(record);
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
 
-			if (bufferBuilder.isFull()) {
-				finishSubpartitionBufferBuilder(targetSubpartition);
-			}
-		} while (record.hasRemaining());
+		if (buffer == null) {
+			buffer = getNewEmptySubpartitionBufferBuilderForNewRecord(targetSubpartition);
+		}
+		buffer.appendAndCommit(record);
+
+		while (record.hasRemaining()) {
+			finishSubpartitionBufferBuilder(targetSubpartition);
+			buffer = getNewEmptySubpartitionBufferBuilderForRecordContinuation(record, targetSubpartition);

Review comment:
       I've rewritten the first commit.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 47d3d60c3dc74ab402ce657c31c27568b190de40 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528) 
   * a001a7083dd0f78db05ef337692b5ddf08b354b0 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r505183102



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +225,62 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
-		}
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForNewRecord(int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), 0);
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		return bufferBuilder;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		final int partialRecordBytes = bufferBuilder.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return bufferBuilder;
+	}
+
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
 		checkInProduceState();
 		ensureUnicastMode();
-
 		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
 		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
+
 		return bufferBuilder;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder getNewEmptyBroadcastBufferBuilderForNewRecord() throws IOException {
+		final BufferBuilder bufferBuilder = requestNewBroadcastBufferBuilder();
+		try (final BufferConsumer consumer = bufferBuilder.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), 0);
+			}
 		}
 
-		return getNewBroadcastBufferBuilder();
+		return bufferBuilder;
 	}
 
-	private BufferBuilder getNewBroadcastBufferBuilder() throws IOException {
+	private BufferBuilder getNewEmptyBroadcastBufferBuilderForRecordContinuation(
+			final ByteBuffer remainingRecordBytes) throws IOException {
+			final BufferBuilder bufferBuilder = requestNewBroadcastBufferBuilder();
+		final int partialRecordBytes = bufferBuilder.appendAndCommit(remainingRecordBytes);
+		try (final BufferConsumer consumer = bufferBuilder.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), partialRecordBytes);

Review comment:
       I think that's roughly the same (from the complexity point of view).
   
   The only difference is easier to differentiate when remainingRecordLength == buffer.size(), but I think that's not that much big difference.
   
   I agree the original code is a bit duplicated. If that's the concern, I've rewritten the code (literally just rewrite), to see whether it looks much better now.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] pnowojski commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
pnowojski commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r505464538



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       > In the current build(), currentReaderPosition can never be buffer.getMaxCapacity()
   
   is that really the case? I think the `BufferConsumer` should be able to handle building empty buffers, after writer has appended and committed the data, but hasn't yet called `BufferBuilder#finish()`.  After all those are two separate calls happening in a different thread.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] pnowojski commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
pnowojski commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r508264197



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);

Review comment:
       > The answer is yes to create a new record, but I think the meaning is more clear to use createBufferConsumerFromBeginning. What do you think?
   
   👍 Ok, let's keep it as it is.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r505183877



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/PipelinedSubpartition.java
##########
@@ -305,6 +308,10 @@ BufferAndBacklog pollBuffer() {
 		}
 	}
 
+	Buffer buildSliceBuffer(BufferConsumerWithPartialRecordLength buffer) {
+		return buffer.build();
+	}

Review comment:
       This method is going to be @override by PipelinedApproximateSubPartition later.
   
   But I think it might be confused if changing it here. I will inline the change for now.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * a001a7083dd0f78db05ef337692b5ddf08b354b0 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652) 
   * 553ff42a3ded621e45167d5a9f426934bf460ea4 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746",
       "triggerID" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * a70ae897b37437579d8aab2ba3a744553a08feea Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507059066



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);

Review comment:
       Yes, it could be. In fact, in all existing cases, the consumer is created from the beginning. I was trying to remove `createBufferConsumer()` to use `createBufferConsumerFromBeginning()` in all places. 
   
   In fact, I am not happy with `createBufferConsumerFromBeginning()` at all. I think I probably have a way to get rid of createBufferConsumerFromBeginning(), with a bit more complicated clean-up logic. 
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] pnowojski commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
pnowojski commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r508262147



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);
 		}
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		checkInProduceState();
-		ensureUnicastMode();
+	private BufferBuilder appendDataToSubpartitionForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return buffer;
+	}
 
-		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
-		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
-		return bufferBuilder;
+	private BufferBuilder appendDataToBroadcastForNewRecord(ByteBuffer record) throws IOException {
+		BufferBuilder buffer = broadcastBufferBuilder;
+
+		if (buffer == null) {
+			buffer = requestNewBroadcastBufferBuilder();
+			createBroadcastBufferConsumers(buffer, 0);
+		}
+
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder appendDataToBroadcastForRecordContinuation(
+			final ByteBuffer remainingRecordBytes) throws IOException {
+		final BufferBuilder buffer = requestNewBroadcastBufferBuilder();
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		createBroadcastBufferConsumers(buffer, partialRecordBytes);
+
+		return buffer;
+	}
+
+	private void createBroadcastBufferConsumers(BufferBuilder buffer, int partialRecordBytes) throws IOException {
+		try (final BufferConsumer consumer = buffer.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), partialRecordBytes);
+			}
 		}
+	}
 
-		return getNewBroadcastBufferBuilder();
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+		checkInProduceState();
+		ensureUnicastMode();

Review comment:
       I think the `unicast` is more accurate and better prefix/infix compared to `subpartition`. `unicast` is after all exactly describing this:
   > unicast is a one-to-one transmission from one point in the network to another poin
   
   While `subpartition` is hmmm, what does it actually mean? Without a context you would have no idea what this name represents. And `unicast` is from the same domain as already used `broadcast` name, so I would vote to rename all `subpartition` usages in this context to `unicast`. 
   
   Maybe in your commit, when you are adding some methods use `unicast`, and add a follow up hotfix commit to rename the remaining things like the one you mentioned:
   ```
   private final BufferBuilder[] subpartitionBufferBuilders;
   ```
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504675916



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       > * `skipBuild(...)` would be replaced with `skip(...)`, that would just move the offset, without returning the buffer
   > * `.build()` call would be required afterwards, to get the remaining data
   
   That would be perfect if we do not have `bytesToSkip == buffer.getMaxCapacity()`.
   
   In the current build(), currentReaderPosition can never be `buffer.getMaxCapacity()`, because the consumer has already been marked as finished `currentReaderPosition = cachedWriterPosition` if the full buffer is read. Remember in case of a record spanning over buffers, we finish the builder first and then request a new builder.
   
   But I can still handle this special case in build() if you are fine to change the build() method. I was trying to not touch the build() method which is used in the normal PipelinedSubPartition read
   
   > Do you really need to return the buffer here? I have a feeling that the could would be simpler if:
   > 
   > * this method was returning just `true/false` if cleanup has finished or not.
   
   Yes, I need both `whether the clean-up is successful` + `returned buffer`, which would be called later in PipelinedApproximateSubpartition within `pollBuffer` method to
   1. decide whether the clean up is successful
   2. Handle sliced Buffer if successful or not.
   
   The reason I am returning an `empty` buffer instead of a `null` buffer in case of `bytesToSkip == buffer.getMaxCapacity()` is also to be easier to follow the current logic in pollBuffer what if we do not read data.
   
   Overall I want to wrap the clean-up logic within `BufferConsumerWithPartialRecordLength`, which I guess is one of the main reasons why we introduce this class?
   
   
   
    




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r505147881



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -128,26 +128,40 @@ protected void flushAllSubpartitions(boolean finishProducers) {
 
 	@Override
 	public void emitRecord(ByteBuffer record, int targetSubpartition) throws IOException {
-		do {
-			final BufferBuilder bufferBuilder = getSubpartitionBufferBuilder(targetSubpartition);
-			bufferBuilder.appendAndCommit(record);
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
 
-			if (bufferBuilder.isFull()) {
-				finishSubpartitionBufferBuilder(targetSubpartition);
-			}
-		} while (record.hasRemaining());
+		if (buffer == null) {
+			buffer = getNewEmptySubpartitionBufferBuilderForNewRecord(targetSubpartition);
+		}
+		buffer.appendAndCommit(record);

Review comment:
       That's a good idea!




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507153770



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);
 		}
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		checkInProduceState();
-		ensureUnicastMode();
+	private BufferBuilder appendDataToSubpartitionForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return buffer;
+	}
 
-		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
-		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
-		return bufferBuilder;
+	private BufferBuilder appendDataToBroadcastForNewRecord(ByteBuffer record) throws IOException {
+		BufferBuilder buffer = broadcastBufferBuilder;
+
+		if (buffer == null) {
+			buffer = requestNewBroadcastBufferBuilder();
+			createBroadcastBufferConsumers(buffer, 0);
+		}
+
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder appendDataToBroadcastForRecordContinuation(
+			final ByteBuffer remainingRecordBytes) throws IOException {
+		final BufferBuilder buffer = requestNewBroadcastBufferBuilder();
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		createBroadcastBufferConsumers(buffer, partialRecordBytes);
+
+		return buffer;
+	}
+
+	private void createBroadcastBufferConsumers(BufferBuilder buffer, int partialRecordBytes) throws IOException {
+		try (final BufferConsumer consumer = buffer.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), partialRecordBytes);
+			}
 		}
+	}
 
-		return getNewBroadcastBufferBuilder();
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+		checkInProduceState();
+		ensureUnicastMode();

Review comment:
       I am not sure, I think these are consistent with this
   ```
   /** For non-broadcast mode, each subpartition maintains a separate BufferBuilder which might be null. */
   	private final BufferBuilder[] subpartitionBufferBuilders;
   ```
   
   Do you want to change these to unicastBufferBuilders?
   It is a bit confusing as well?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504670777



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumer.java
##########
@@ -108,6 +109,30 @@ public Buffer build() {
 		return slice.retainBuffer();
 	}
 
+	/**
+	 * @param bytesToSkip number of bytes to skip from currentReaderPosition
+	 * @return sliced {@link Buffer} containing the not yet consumed data, Returned {@link Buffer} shares the reference
+	 * counter with the parent {@link BufferConsumer} - in order to recycle memory both of them must be recycled/closed.
+	 */
+	Buffer skipBuild(int bytesToSkip) {
+		writerPosition.update();
+		int cachedWriterPosition = writerPosition.getCached();
+		Buffer slice;
+
+		int bytesReadable = cachedWriterPosition - currentReaderPosition;
+		checkState(bytesToSkip <= bytesReadable, "bytes to skip beyond readable range");
+
+		if (bytesToSkip < buffer.getMaxCapacity()) {
+			slice = buffer.readOnlySlice(currentReaderPosition + bytesToSkip, bytesReadable - bytesToSkip);
+		} else {
+			// return an empty buffer if beyond buffer max capacity
+			slice = buffer.readOnlySlice(currentReaderPosition, 0);
+		}

Review comment:
       You are right, `bytesToSkip > buffer.getMaxCapacity()` is not possible. 
   But, the problem is if (bytesToSkip == buffer.getMaxCapacity()), which is the case when a long record occupy the entire buffer, `currentReaderPosition + bytesToSkip` euqals to `buffer.getMaxCapacity()`, which means 
   I would read from `buffer.getMaxCapacity()`. I guess I would get an out-of-range exception if I do that LOL




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 553ff42a3ded621e45167d5a9f426934bf460ea4 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504675916



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       that would be perfect if we do not have `bytesToSkip == buffer.getMaxCapacity()`.
   
   In the current build(), currentReaderPosition can never be `buffer.getMaxCapacity()`, because the consumer has already been marked as finished `currentReaderPosition = cachedWriterPosition;`
   
   But I can still handle this special case in build() if you are fine to change the build() method. I was trying to not touch the build() method which is used in the normal PipelinedSubPartition read




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507171053



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);

Review comment:
       I've actually tried a bit to remove `createBufferConsumerFromBeginning`, which can work, but the clean-up logic is not as beautiful as it is now: `skip() + build()`. Reason: skip can be unsuccessful because a partial record may not been written or visible yet before skip(), but can be visible after skip().
   
   So I will keep what we have right now.
   
   To your question:
   `could it be createBufferConsumer()?`
   
   The answer is yes to create a new record, but I think the meaning is more clear to use `createBufferConsumerFromBeginning`. What do you think?
   
   I can definitely change it to `createBufferConsumer()`
   
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507148578



##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/ResultPartitionTest.java
##########
@@ -526,6 +526,44 @@ public void testFlushBoundedBlockingResultPartition() throws IOException {
 		assertNull(readView2.getNextBuffer());
 	}
 
+	@Test
+	public void testEmitRecordWithRecordSpanningMultipleBuffers() throws Exception {
+		BufferWritingResultPartition bufferWritingResultPartition = createResultPartition(ResultPartitionType.PIPELINED);
+		PipelinedSubpartition pipelinedSubpartition = (PipelinedSubpartition) bufferWritingResultPartition.subpartitions[0];
+		int partialLength = bufferSize / 3;
+
+		try {
+			// emit the first record, record length = partialLength
+			bufferWritingResultPartition.emitRecord(ByteBuffer.allocate(partialLength), 0);
+			// emit the second record, record length = bufferSize
+			bufferWritingResultPartition.emitRecord(ByteBuffer.allocate(bufferSize), 0);
+		} finally {
+			assertEquals(2, pipelinedSubpartition.getCurrentNumberOfBuffers());
+			assertEquals(0, pipelinedSubpartition.getNextBuffer().getPartialRecordLength());

Review comment:
       the view can only read `BufferAndBacklog`, that's the sliced buffer + some other information.
   
   What I need is `PartialRecordLength` in the buffer queue cell in `PrioritizedDeque<BufferConsumerWithPartialRecordLength> buffers`.
   
   If you really do not like this, I can remove this assertion. This information is tested in later PRs in different ways as well.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-709707676


   Synced up offline with @pnowojski 
   
   1. change `skipBuild()` in `BufferConsumerWithPartialRecordLength` to `skip()` --- simply skip the read position, and uses the exisiting `build()` to do the building work
   2. Remove `PartialRecordCleanupResult`
   3. Rebase 


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * a001a7083dd0f78db05ef337692b5ddf08b354b0 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652) 
   * 553ff42a3ded621e45167d5a9f426934bf460ea4 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 553ff42a3ded621e45167d5a9f426934bf460ea4 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703) 
   * fd401b11ff98b70455ee395f8b4078851ab1ec9f UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504675916



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       that would be perfect if we do not have `bytesToSkip == buffer.getMaxCapacity()`.
   
   In the current build(), currentReaderPosition can never be `buffer.getMaxCapacity()`, because the consumer has already been marked as finished `currentReaderPosition = cachedWriterPosition` if the full buffer is read. Remember in case of a record spanning over buffers, we finish the builder first, and then request a new builder.
   
   But I can still handle this special case in build() if you are fine to change the build() method. I was trying to not touch the build() method which is used in the normal PipelinedSubPartition read




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746",
       "triggerID" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "triggerType" : "PUSH"
     }, {
       "hash" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7995",
       "triggerID" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "triggerType" : "PUSH"
     }, {
       "hash" : "886615ca80abb71f262e5c8e981ae7f0b0247729",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8004",
       "triggerID" : "886615ca80abb71f262e5c8e981ae7f0b0247729",
       "triggerType" : "PUSH"
     }, {
       "hash" : "534b8f7e737aef2ba2c1fa3198a2a53ed66ca080",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "534b8f7e737aef2ba2c1fa3198a2a53ed66ca080",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 886615ca80abb71f262e5c8e981ae7f0b0247729 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8004) 
   * 534b8f7e737aef2ba2c1fa3198a2a53ed66ca080 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] pnowojski merged pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
pnowojski merged pull request #13614:
URL: https://github.com/apache/flink/pull/13614


   


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746",
       "triggerID" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "triggerType" : "PUSH"
     }, {
       "hash" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * a70ae897b37437579d8aab2ba3a744553a08feea Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746) 
   * 8a7c7987492c18b09b9111b8a341792a7cd508c1 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * fd401b11ff98b70455ee395f8b4078851ab1ec9f Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717) 
   * a70ae897b37437579d8aab2ba3a744553a08feea UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 47d3d60c3dc74ab402ce657c31c27568b190de40 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507059066



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);

Review comment:
       Yes, it could be. In fact, in all existing cases, the consumer is created from the beginning. I was trying to remove `createBufferConsumer()` to use `createBufferConsumerFromBeginning()` in all places.
   
   But at a second thought, I am not happy with `createBufferConsumerFromBeginning()`, and that's why I am still keeping `createBufferConsumer()`.
   
   I think I probably have a way to get rid of `createBufferConsumerFromBeginning()`, with a bit more complicated clean-up logic. 
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504675916



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       > * `skipBuild(...)` would be replaced with `skip(...)`, that would just move the offset, without returning the buffer
   > * `.build()` call would be required afterwards, to get the remaining data
   
   I would also thinking of that, and it would be perfect if we do not have the case `bytesToSkip == buffer.getMaxCapacity()`.
   
   In the current build(), currentReaderPosition can never be `buffer.getMaxCapacity()`, because the consumer has already been marked as finished `currentReaderPosition = cachedWriterPosition` if the full buffer is read. Remember in case of a record spanning over buffers, we finish the builder first and then request a new builder.
   
   But I can still handle this special case in build() if you are fine to change the build() method. I was trying to not touch the build() method which is used in the normal PipelinedSubPartition read.
   
   I am fine with either way.
   
   > Do you really need to return the buffer here? I have a feeling that the could would be simpler if:
   > 
   > * this method was returning just `true/false` if cleanup has finished or not.
   
   Yes, I need both `whether the clean-up is successful` + `returned buffer`, which would be called later in PipelinedApproximateSubpartition within `pollBuffer` method to
   1. decide whether the clean up is successful
   2. Handle sliced Buffer if successful or not.
   
   The reason I am returning an `empty` buffer instead of a `null` buffer in case of `bytesToSkip == buffer.getMaxCapacity()` is also to be easier to follow the current logic in pollBuffer what if we do not read data.
   
   Overall I want to wrap the clean-up logic within `BufferConsumerWithPartialRecordLength`, which I guess is one of the main reasons why we introduce this class?
   
   
   
    




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504675916



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       > * `skipBuild(...)` would be replaced with `skip(...)`, that would just move the offset, without returning the buffer
   > * `.build()` call would be required afterwards, to get the remaining data
   
   I was also thinking of that, and it would be perfect if we do not have the case `bytesToSkip == buffer.getMaxCapacity()`.
   
   In the current build(), currentReaderPosition can never be `buffer.getMaxCapacity()`, because the consumer has already been marked as finished `currentReaderPosition = cachedWriterPosition` if the full buffer is read. Remember in case of a record spanning over buffers, we finish the builder first and then request a new builder.
   
   But I can still handle this special case in build() if you are fine to change the build() method. I was trying to not touch the build() method which is used in the normal PipelinedSubPartition read. It would be easier if later we really want to change to a different way to handle partial records.
   
   I am fine with either way.
   
   > Do you really need to return the buffer here? I have a feeling that the could be simpler if:
   > 
   > * this method was returning just `true/false` if cleanup has finished or not.
   
   Yes, I need both `whether the clean-up is successful` + `returned buffer`, which would be called later in PipelinedApproximateSubpartition within `pollBuffer` method to
   1. decide whether the clean up is successful
   2. Handle sliced Buffer if successful or not.
   
   The reason I am returning an `empty` buffer instead of a `null` buffer in case of `bytesToSkip == buffer.getMaxCapacity()` is also to be easier to follow the current logic in pollBuffer what if we do not read data.
   
   Overall I want to wrap the clean-up logic within `BufferConsumerWithPartialRecordLength`, which I guess is one of the main reasons why we introduce this class?
   
   
   
    




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 47d3d60c3dc74ab402ce657c31c27568b190de40 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504670777



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumer.java
##########
@@ -108,6 +109,30 @@ public Buffer build() {
 		return slice.retainBuffer();
 	}
 
+	/**
+	 * @param bytesToSkip number of bytes to skip from currentReaderPosition
+	 * @return sliced {@link Buffer} containing the not yet consumed data, Returned {@link Buffer} shares the reference
+	 * counter with the parent {@link BufferConsumer} - in order to recycle memory both of them must be recycled/closed.
+	 */
+	Buffer skipBuild(int bytesToSkip) {
+		writerPosition.update();
+		int cachedWriterPosition = writerPosition.getCached();
+		Buffer slice;
+
+		int bytesReadable = cachedWriterPosition - currentReaderPosition;
+		checkState(bytesToSkip <= bytesReadable, "bytes to skip beyond readable range");
+
+		if (bytesToSkip < buffer.getMaxCapacity()) {
+			slice = buffer.readOnlySlice(currentReaderPosition + bytesToSkip, bytesReadable - bytesToSkip);
+		} else {
+			// return an empty buffer if beyond buffer max capacity
+			slice = buffer.readOnlySlice(currentReaderPosition, 0);
+		}

Review comment:
       You are right, `bytesToSkip > buffer.getMaxCapacity()` is not possible. 
   **However**, the problem is if (bytesToSkip == buffer.getMaxCapacity()), which is the case when a long record occupy the entire buffer, `currentReaderPosition + bytesToSkip` euqals to `buffer.getMaxCapacity()`, which means I would read starting from `buffer.getMaxCapacity()`. I guess I would get an out-of-range exception if I do that LOL




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-717053226


   local azure finishes successfully
   
   https://dev.azure.com/mymeiyuan/Flink/_build/results?buildId=112&view=results


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504670777



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumer.java
##########
@@ -108,6 +109,30 @@ public Buffer build() {
 		return slice.retainBuffer();
 	}
 
+	/**
+	 * @param bytesToSkip number of bytes to skip from currentReaderPosition
+	 * @return sliced {@link Buffer} containing the not yet consumed data, Returned {@link Buffer} shares the reference
+	 * counter with the parent {@link BufferConsumer} - in order to recycle memory both of them must be recycled/closed.
+	 */
+	Buffer skipBuild(int bytesToSkip) {
+		writerPosition.update();
+		int cachedWriterPosition = writerPosition.getCached();
+		Buffer slice;
+
+		int bytesReadable = cachedWriterPosition - currentReaderPosition;
+		checkState(bytesToSkip <= bytesReadable, "bytes to skip beyond readable range");
+
+		if (bytesToSkip < buffer.getMaxCapacity()) {
+			slice = buffer.readOnlySlice(currentReaderPosition + bytesToSkip, bytesReadable - bytesToSkip);
+		} else {
+			// return an empty buffer if beyond buffer max capacity
+			slice = buffer.readOnlySlice(currentReaderPosition, 0);
+		}

Review comment:
       You are right, `bytesToSkip > buffer.getMaxCapacity()` is not possible. 
   **However**, the problem is if (bytesToSkip == buffer.getMaxCapacity()), which is the case when a long record occupy the entire buffer, `currentReaderPosition + bytesToSkip` euqals to `buffer.getMaxCapacity()`, which means 
   I would read from `buffer.getMaxCapacity()`. I guess I would get an out-of-range exception if I do that LOL




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707692926


   Thanks a lot for your contribution to the Apache Flink project. I'm the @flinkbot. I help the community
   to review your pull request. We will use this comment to track the progress of the review.
   
   
   ## Automated Checks
   Last check on commit 0f671afd0bdf2d4f6243b44346560446dc170385 (Tue Oct 13 12:04:12 UTC 2020)
   
   **Warnings:**
    * No documentation files were touched! Remember to keep the Flink docs up to date!
    * **This pull request references an unassigned [Jira ticket](https://issues.apache.org/jira/browse/FLINK-19547).** According to the [code contribution guide](https://flink.apache.org/contributing/contribute-code.html), tickets need to be assigned before starting with the implementation work.
   
   
   <sub>Mention the bot in a comment to re-run the automated checks.</sub>
   ## Review Progress
   
   * ❓ 1. The [description] looks good.
   * ❓ 2. There is [consensus] that the contribution should go into to Flink.
   * ❓ 3. Needs [attention] from.
   * ❓ 4. The change fits into the overall [architecture].
   * ❓ 5. Overall code [quality] is good.
   
   Please see the [Pull Request Review Guide](https://flink.apache.org/contributing/reviewing-prs.html) for a full explanation of the review process.<details>
    The Bot is tracking the review progress through labels. Labels are applied according to the order of the review items. For consensus, approval by a Flink committer of PMC member is required <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot approve description` to approve one or more aspects (aspects: `description`, `consensus`, `architecture` and `quality`)
    - `@flinkbot approve all` to approve all aspects
    - `@flinkbot approve-until architecture` to approve everything until `architecture`
    - `@flinkbot attention @username1 [@username2 ..]` to require somebody's attention
    - `@flinkbot disapprove architecture` to remove an approval you gave earlier
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] pnowojski commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
pnowojski commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r508270969



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);
 		}
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		checkInProduceState();
-		ensureUnicastMode();
+	private BufferBuilder appendDataToSubpartitionForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return buffer;
+	}
 
-		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
-		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
-		return bufferBuilder;
+	private BufferBuilder appendDataToBroadcastForNewRecord(ByteBuffer record) throws IOException {
+		BufferBuilder buffer = broadcastBufferBuilder;
+
+		if (buffer == null) {
+			buffer = requestNewBroadcastBufferBuilder();
+			createBroadcastBufferConsumers(buffer, 0);
+		}
+
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder appendDataToBroadcastForRecordContinuation(
+			final ByteBuffer remainingRecordBytes) throws IOException {
+		final BufferBuilder buffer = requestNewBroadcastBufferBuilder();
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		createBroadcastBufferConsumers(buffer, partialRecordBytes);
+
+		return buffer;
+	}
+
+	private void createBroadcastBufferConsumers(BufferBuilder buffer, int partialRecordBytes) throws IOException {
+		try (final BufferConsumer consumer = buffer.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), partialRecordBytes);
+			}
 		}
+	}
 
-		return getNewBroadcastBufferBuilder();
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+		checkInProduceState();
+		ensureUnicastMode();

Review comment:
       Please do so in the same PR :) as I described. New code that you are adding please add with already corrected name. Pre existing code that you are going to rename, please rename in a separate hotfix commit.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot commented on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot commented on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507059066



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);

Review comment:
       Yes, it could be. In fact, in all existing cases, the consumer is created from the beginning. I was trying to remove `createBufferConsumer()` to use `createBufferConsumerFromBeginning()` in all places.
   
   But at a second thought, I am not happy with `createBufferConsumerFromBeginning()` at all, and that's why I am still keeping `createBufferConsumer()`.
   
   I think I probably have a way to get rid of `createBufferConsumerFromBeginning()`, with a bit more complicated clean-up logic. 
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * a001a7083dd0f78db05ef337692b5ddf08b354b0 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504675916



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       that would be perfect if we do not have `bytesToSkip == buffer.getMaxCapacity()`.
   
   In the current build(), currentReaderPosition can never be `buffer.getMaxCapacity()`, because the consumer has already been marked as finished `currentReaderPosition = cachedWriterPosition;` if already read.
   
   But I can still handle this special case in build() if you are fine to change the build() method. I was trying to not touch the build() method which is used in the normal PipelinedSubPartition read




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] pnowojski commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
pnowojski commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504622425



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumer.java
##########
@@ -159,6 +184,14 @@ int getCurrentReaderPosition() {
 		return currentReaderPosition;
 	}
 
+	boolean startOfDataBuffer() {

Review comment:
       nit: `isStartOfDataBuffer()`

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -128,26 +128,40 @@ protected void flushAllSubpartitions(boolean finishProducers) {
 
 	@Override
 	public void emitRecord(ByteBuffer record, int targetSubpartition) throws IOException {
-		do {
-			final BufferBuilder bufferBuilder = getSubpartitionBufferBuilder(targetSubpartition);
-			bufferBuilder.appendAndCommit(record);
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
 
-			if (bufferBuilder.isFull()) {
-				finishSubpartitionBufferBuilder(targetSubpartition);
-			}
-		} while (record.hasRemaining());
+		if (buffer == null) {
+			buffer = getNewEmptySubpartitionBufferBuilderForNewRecord(targetSubpartition);
+		}
+		buffer.appendAndCommit(record);

Review comment:
       nit: those names are a bit lengthy, also it's a bit confusing that `getNewEmptySubpartitionBufferBuilderForNewRecord` is not appending the data, while `getNewEmptySubpartitionBufferBuilderForRecordContinuation` is.
   
   Maybe can you extract those lines above into a method:
   ```
   BufferBuilder buffer = appendDataForSubpartitionNewRecord(record, targetSubpartition);
   ```
   and for the broadcast version
   ```
   BufferBuilder buffer = appendDataForBroadcastNewRecord(record, targetSubpartition);
   ```
   ...

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/PipelinedSubpartition.java
##########
@@ -305,6 +308,10 @@ BufferAndBacklog pollBuffer() {
 		}
 	}
 
+	Buffer buildSliceBuffer(BufferConsumerWithPartialRecordLength buffer) {
+		return buffer.build();
+	}

Review comment:
       inline?

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       Do you really need to return the buffer here? I have a feeling that the could would be simpler if:
   
   - this method was returning just `true/false` if cleanup has finished or not. 
   - `skipBuild(...)` would be replaced with `skip(...)`, that would just move the offset, without returning the buffer
   - `.build()` call would be required afterwards, to get the remaining data

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +225,62 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
-		}
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForNewRecord(int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), 0);
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		return bufferBuilder;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		final int partialRecordBytes = bufferBuilder.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return bufferBuilder;
+	}
+
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
 		checkInProduceState();
 		ensureUnicastMode();
-
 		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
 		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
+
 		return bufferBuilder;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder getNewEmptyBroadcastBufferBuilderForNewRecord() throws IOException {
+		final BufferBuilder bufferBuilder = requestNewBroadcastBufferBuilder();
+		try (final BufferConsumer consumer = bufferBuilder.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), 0);
+			}
 		}
 
-		return getNewBroadcastBufferBuilder();
+		return bufferBuilder;

Review comment:
       if you moved appending data into this method as I suggested above, you could deduplicate this code with:
   ```
   return getNewEmptyBroadcastBufferBuilderForRecordContinuation(record, 0);
   ```

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumer.java
##########
@@ -108,6 +109,30 @@ public Buffer build() {
 		return slice.retainBuffer();
 	}
 
+	/**
+	 * @param bytesToSkip number of bytes to skip from currentReaderPosition
+	 * @return sliced {@link Buffer} containing the not yet consumed data, Returned {@link Buffer} shares the reference
+	 * counter with the parent {@link BufferConsumer} - in order to recycle memory both of them must be recycled/closed.
+	 */
+	Buffer skipBuild(int bytesToSkip) {
+		writerPosition.update();
+		int cachedWriterPosition = writerPosition.getCached();
+		Buffer slice;
+
+		int bytesReadable = cachedWriterPosition - currentReaderPosition;
+		checkState(bytesToSkip <= bytesReadable, "bytes to skip beyond readable range");
+
+		if (bytesToSkip < buffer.getMaxCapacity()) {
+			slice = buffer.readOnlySlice(currentReaderPosition + bytesToSkip, bytesReadable - bytesToSkip);
+		} else {
+			// return an empty buffer if beyond buffer max capacity
+			slice = buffer.readOnlySlice(currentReaderPosition, 0);
+		}

Review comment:
       How could it be that `bytesToSkip > buffer.getMaxCapacity()` if we already asserted `bytesToSkip <= bytesReadable`? If that's impossible, we could replace this if with just
   ```
   slice = buffer.readOnlySlice(currentReaderPosition + bytesToSkip, bytesReadable - bytesToSkip);
   ```
   as that would also return an empty buffer if `bytesToSkip == bytesReadable`?

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -128,26 +128,40 @@ protected void flushAllSubpartitions(boolean finishProducers) {
 
 	@Override
 	public void emitRecord(ByteBuffer record, int targetSubpartition) throws IOException {
-		do {
-			final BufferBuilder bufferBuilder = getSubpartitionBufferBuilder(targetSubpartition);
-			bufferBuilder.appendAndCommit(record);
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
 
-			if (bufferBuilder.isFull()) {
-				finishSubpartitionBufferBuilder(targetSubpartition);
-			}
-		} while (record.hasRemaining());
+		if (buffer == null) {
+			buffer = getNewEmptySubpartitionBufferBuilderForNewRecord(targetSubpartition);
+		}
+		buffer.appendAndCommit(record);
+
+		while (record.hasRemaining()) {
+			finishSubpartitionBufferBuilder(targetSubpartition);
+			buffer = getNewEmptySubpartitionBufferBuilderForRecordContinuation(record, targetSubpartition);

Review comment:
       ...
   ```
   buffer = appendDataForSubpartitionRecordContinuation(record, targetSubpartition);
   ```
   and for the broadcast version
   ```
   buffer = appendDataForBroadcastRecordContinuation(record, targetSubpartition);
   ```
   ?
   

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +225,62 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
-		}
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForNewRecord(int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), 0);
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		return bufferBuilder;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		final int partialRecordBytes = bufferBuilder.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return bufferBuilder;
+	}
+
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
 		checkInProduceState();
 		ensureUnicastMode();
-
 		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
 		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
+
 		return bufferBuilder;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder getNewEmptyBroadcastBufferBuilderForNewRecord() throws IOException {
+		final BufferBuilder bufferBuilder = requestNewBroadcastBufferBuilder();
+		try (final BufferConsumer consumer = bufferBuilder.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), 0);
+			}
 		}
 
-		return getNewBroadcastBufferBuilder();
+		return bufferBuilder;
 	}
 
-	private BufferBuilder getNewBroadcastBufferBuilder() throws IOException {
+	private BufferBuilder getNewEmptyBroadcastBufferBuilderForRecordContinuation(
+			final ByteBuffer remainingRecordBytes) throws IOException {
+			final BufferBuilder bufferBuilder = requestNewBroadcastBufferBuilder();
+		final int partialRecordBytes = bufferBuilder.appendAndCommit(remainingRecordBytes);
+		try (final BufferConsumer consumer = bufferBuilder.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), partialRecordBytes);

Review comment:
       hmmm, maybe it will be easier to deduplicate/generalise some of the code, if you replaced `partialRecordBytes` (`partialRecordLength`) with `remainingRecordBytes.remaining()` (`remainingRecordLength` - `remainingRecordLength` could be more than the size of one single `Buffer`, and would basically mean what's the remaining record length. if `remainingRecordLength > buffer.size()` it means we can not complete record clean up in one call)
   
   This would slightly change the implementation of `skipBuild(...)`, but maybe it would make the code overally a bit simpler?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r505277649



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +225,62 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
-		}
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForNewRecord(int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), 0);
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		return bufferBuilder;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		final int partialRecordBytes = bufferBuilder.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return bufferBuilder;
+	}
+
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
 		checkInProduceState();
 		ensureUnicastMode();
-
 		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
 		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
+
 		return bufferBuilder;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder getNewEmptyBroadcastBufferBuilderForNewRecord() throws IOException {
+		final BufferBuilder bufferBuilder = requestNewBroadcastBufferBuilder();
+		try (final BufferConsumer consumer = bufferBuilder.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), 0);
+			}
 		}
 
-		return getNewBroadcastBufferBuilder();
+		return bufferBuilder;

Review comment:
       I've rewritten the first commit.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * fd401b11ff98b70455ee395f8b4078851ab1ec9f Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r505183102



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +225,62 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
-		}
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForNewRecord(int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), 0);
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		return bufferBuilder;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		final int partialRecordBytes = bufferBuilder.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return bufferBuilder;
+	}
+
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
 		checkInProduceState();
 		ensureUnicastMode();
-
 		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
 		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
+
 		return bufferBuilder;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder getNewEmptyBroadcastBufferBuilderForNewRecord() throws IOException {
+		final BufferBuilder bufferBuilder = requestNewBroadcastBufferBuilder();
+		try (final BufferConsumer consumer = bufferBuilder.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), 0);
+			}
 		}
 
-		return getNewBroadcastBufferBuilder();
+		return bufferBuilder;
 	}
 
-	private BufferBuilder getNewBroadcastBufferBuilder() throws IOException {
+	private BufferBuilder getNewEmptyBroadcastBufferBuilderForRecordContinuation(
+			final ByteBuffer remainingRecordBytes) throws IOException {
+			final BufferBuilder bufferBuilder = requestNewBroadcastBufferBuilder();
+		final int partialRecordBytes = bufferBuilder.appendAndCommit(remainingRecordBytes);
+		try (final BufferConsumer consumer = bufferBuilder.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), partialRecordBytes);

Review comment:
       I think that's roughly the same (from the complexity point of view).
   
   The only difference is easier to differentiate when remainingRecordLength == buffer.size(), but I think that's not that much big difference.
   
   I agree the original code is a bit duplicated. If that's the motivation, I've rewritten the code (literally just rewrite), to see whether it looks much better now.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-713456983


   Have addressed all comments. @pnowojski 


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746",
       "triggerID" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "triggerType" : "PUSH"
     }, {
       "hash" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7995",
       "triggerID" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "triggerType" : "PUSH"
     }, {
       "hash" : "886615ca80abb71f262e5c8e981ae7f0b0247729",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "886615ca80abb71f262e5c8e981ae7f0b0247729",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * a70ae897b37437579d8aab2ba3a744553a08feea Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746) 
   * 8a7c7987492c18b09b9111b8a341792a7cd508c1 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7995) 
   * 886615ca80abb71f262e5c8e981ae7f0b0247729 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507171053



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);

Review comment:
       I've actually tried a bit to remove `createBufferConsumerFromBeginning`, which can work, but the clean-up logic is not as simple and beautful as it is now: `skip() + build()`. (Reason: skip can be unsuccessful because a partial record may not been written or visible yet before skip(), but can be visible after skip()).
   
   So I will keep what we have right now.
   
   To your question:
   `could it be createBufferConsumer()?`
   
   The answer is yes to create a new record, but I think the meaning is more clear to use `createBufferConsumerFromBeginning`. What do you think?
   
   I can definitely change it to `createBufferConsumer()`
   
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746",
       "triggerID" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "triggerType" : "PUSH"
     }, {
       "hash" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7995",
       "triggerID" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "triggerType" : "PUSH"
     }, {
       "hash" : "886615ca80abb71f262e5c8e981ae7f0b0247729",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8004",
       "triggerID" : "886615ca80abb71f262e5c8e981ae7f0b0247729",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 886615ca80abb71f262e5c8e981ae7f0b0247729 Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8004) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] pnowojski commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
pnowojski commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r506164368



##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/ResultPartitionTest.java
##########
@@ -526,6 +526,44 @@ public void testFlushBoundedBlockingResultPartition() throws IOException {
 		assertNull(readView2.getNextBuffer());
 	}
 
+	@Test
+	public void testEmitRecordWithRecordSpanningMultipleBuffers() throws Exception {
+		BufferWritingResultPartition bufferWritingResultPartition = createResultPartition(ResultPartitionType.PIPELINED);
+		PipelinedSubpartition pipelinedSubpartition = (PipelinedSubpartition) bufferWritingResultPartition.subpartitions[0];
+		int partialLength = bufferSize / 3;
+
+		try {
+			// emit the first record, record length = partialLength
+			bufferWritingResultPartition.emitRecord(ByteBuffer.allocate(partialLength), 0);
+			// emit the second record, record length = bufferSize
+			bufferWritingResultPartition.emitRecord(ByteBuffer.allocate(bufferSize), 0);
+		} finally {
+			assertEquals(2, pipelinedSubpartition.getCurrentNumberOfBuffers());
+			assertEquals(0, pipelinedSubpartition.getNextBuffer().getPartialRecordLength());

Review comment:
       Why do you need this special used only in the tests method? Can not you use the same thing as the other test are doing (for example above):
   ```
   ResultSubpartitionView readView = pipelinedSubpartition.createSubpartitionView(0, new NoOpBufferAvailablityListener());
   readView. getNextBuffer()
   ```
   ?

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);

Review comment:
       could it be `createBufferConsumer()`?

##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);
 		}
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		checkInProduceState();
-		ensureUnicastMode();
+	private BufferBuilder appendDataToSubpartitionForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return buffer;
+	}
 
-		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
-		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
-		return bufferBuilder;
+	private BufferBuilder appendDataToBroadcastForNewRecord(ByteBuffer record) throws IOException {
+		BufferBuilder buffer = broadcastBufferBuilder;
+
+		if (buffer == null) {
+			buffer = requestNewBroadcastBufferBuilder();
+			createBroadcastBufferConsumers(buffer, 0);
+		}
+
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder appendDataToBroadcastForRecordContinuation(
+			final ByteBuffer remainingRecordBytes) throws IOException {
+		final BufferBuilder buffer = requestNewBroadcastBufferBuilder();
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		createBroadcastBufferConsumers(buffer, partialRecordBytes);
+
+		return buffer;
+	}
+
+	private void createBroadcastBufferConsumers(BufferBuilder buffer, int partialRecordBytes) throws IOException {
+		try (final BufferConsumer consumer = buffer.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), partialRecordBytes);
+			}
 		}
+	}
 
-		return getNewBroadcastBufferBuilder();
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+		checkInProduceState();
+		ensureUnicastMode();

Review comment:
       `unicast`. That's I think better and more consistent naming scheme compared to `subpartition`.
   
   So maybe rename the  methods with `subpartition` in the name:
   `requestNewSubpartitionBufferBuilder ` -> `requetnNewUnicastBufferBuilder`
   `appendDataToSubpartitionForNewRecord` -> `appendUnicastDataForNewRecord`
   etc...
   ?




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746",
       "triggerID" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * fd401b11ff98b70455ee395f8b4078851ab1ec9f Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717) 
   * a70ae897b37437579d8aab2ba3a744553a08feea Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] pnowojski commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
pnowojski commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r508262147



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);
 		}
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		checkInProduceState();
-		ensureUnicastMode();
+	private BufferBuilder appendDataToSubpartitionForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return buffer;
+	}
 
-		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
-		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
-		return bufferBuilder;
+	private BufferBuilder appendDataToBroadcastForNewRecord(ByteBuffer record) throws IOException {
+		BufferBuilder buffer = broadcastBufferBuilder;
+
+		if (buffer == null) {
+			buffer = requestNewBroadcastBufferBuilder();
+			createBroadcastBufferConsumers(buffer, 0);
+		}
+
+		buffer.appendAndCommit(record);
+
+		return buffer;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder appendDataToBroadcastForRecordContinuation(
+			final ByteBuffer remainingRecordBytes) throws IOException {
+		final BufferBuilder buffer = requestNewBroadcastBufferBuilder();
+		// !! Be aware, in case of partialRecordBytes != 0, partial length and data has to `appendAndCommit` first
+		// before consumer is created. Otherwise it would be confused with the case the buffer starting
+		// with a complete record.
+		// !! The next two lines can not change order.
+		final int partialRecordBytes = buffer.appendAndCommit(remainingRecordBytes);
+		createBroadcastBufferConsumers(buffer, partialRecordBytes);
+
+		return buffer;
+	}
+
+	private void createBroadcastBufferConsumers(BufferBuilder buffer, int partialRecordBytes) throws IOException {
+		try (final BufferConsumer consumer = buffer.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), partialRecordBytes);
+			}
 		}
+	}
 
-		return getNewBroadcastBufferBuilder();
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+		checkInProduceState();
+		ensureUnicastMode();

Review comment:
       I think the `unicast` is more accurate and better prefix/infix compared to `subpartition`, so I would vote to rename all of them to the `unicast`. 




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504675916



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       > * `skipBuild(...)` would be replaced with `skip(...)`, that would just move the offset, without returning the buffer
   > * `.build()` call would be required afterwards, to get the remaining data
   
   I was also thinking of that, and it would be perfect if we do not have the case `bytesToSkip == buffer.getMaxCapacity()`.
   
   In the current build(), currentReaderPosition can never be `buffer.getMaxCapacity()`, because the consumer has already been marked as finished `currentReaderPosition = cachedWriterPosition` if the full buffer is read. Remember in case of a record spanning over buffers, we finish the builder first and then request a new builder.
   
   But I can still handle this special case in build() if you are fine to change the build() method. I was trying to not touch the build() method which is used in the normal PipelinedSubPartition read. It would be easier if later we really want to change to a different way to handle partial record handling.
   
   I am fine with either way.
   
   > Do you really need to return the buffer here? I have a feeling that the could be simpler if:
   > 
   > * this method was returning just `true/false` if cleanup has finished or not.
   
   Yes, I need both `whether the clean-up is successful` + `returned buffer`, which would be called later in PipelinedApproximateSubpartition within `pollBuffer` method to
   1. decide whether the clean up is successful
   2. Handle sliced Buffer if successful or not.
   
   The reason I am returning an `empty` buffer instead of a `null` buffer in case of `bytesToSkip == buffer.getMaxCapacity()` is also to be easier to follow the current logic in pollBuffer what if we do not read data.
   
   Overall I want to wrap the clean-up logic within `BufferConsumerWithPartialRecordLength`, which I guess is one of the main reasons why we introduce this class?
   
   
   
    




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504675916



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       > * `skipBuild(...)` would be replaced with `skip(...)`, that would just move the offset, without returning the buffer
   > * `.build()` call would be required afterwards, to get the remaining data
   
   I was also thinking of that, and it would be perfect if we do not have the case `bytesToSkip == buffer.getMaxCapacity()`.
   
   In the current build(), currentReaderPosition can never be `buffer.getMaxCapacity()`, because the consumer has already been marked as finished `currentReaderPosition = cachedWriterPosition` if the full buffer is read. Remember in case of a record spanning over buffers, we finish the builder first and then request a new builder.
   
   But I can still handle this special case in build() if you are fine to change the build() method. I was trying to not touch the build() method which is used in the normal PipelinedSubPartition read.
   
   I am fine with either way.
   
   > Do you really need to return the buffer here? I have a feeling that the could be simpler if:
   > 
   > * this method was returning just `true/false` if cleanup has finished or not.
   
   Yes, I need both `whether the clean-up is successful` + `returned buffer`, which would be called later in PipelinedApproximateSubpartition within `pollBuffer` method to
   1. decide whether the clean up is successful
   2. Handle sliced Buffer if successful or not.
   
   The reason I am returning an `empty` buffer instead of a `null` buffer in case of `bytesToSkip == buffer.getMaxCapacity()` is also to be easier to follow the current logic in pollBuffer what if we do not read data.
   
   Overall I want to wrap the clean-up logic within `BufferConsumerWithPartialRecordLength`, which I guess is one of the main reasons why we introduce this class?
   
   
   
    




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-713447681


   fix conflicts.


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504670777



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumer.java
##########
@@ -108,6 +109,30 @@ public Buffer build() {
 		return slice.retainBuffer();
 	}
 
+	/**
+	 * @param bytesToSkip number of bytes to skip from currentReaderPosition
+	 * @return sliced {@link Buffer} containing the not yet consumed data, Returned {@link Buffer} shares the reference
+	 * counter with the parent {@link BufferConsumer} - in order to recycle memory both of them must be recycled/closed.
+	 */
+	Buffer skipBuild(int bytesToSkip) {
+		writerPosition.update();
+		int cachedWriterPosition = writerPosition.getCached();
+		Buffer slice;
+
+		int bytesReadable = cachedWriterPosition - currentReaderPosition;
+		checkState(bytesToSkip <= bytesReadable, "bytes to skip beyond readable range");
+
+		if (bytesToSkip < buffer.getMaxCapacity()) {
+			slice = buffer.readOnlySlice(currentReaderPosition + bytesToSkip, bytesReadable - bytesToSkip);
+		} else {
+			// return an empty buffer if beyond buffer max capacity
+			slice = buffer.readOnlySlice(currentReaderPosition, 0);
+		}

Review comment:
       You are right, `bytesToSkip > buffer.getMaxCapacity()` is not possible. 
   But, the problem is if (bytesToSkip == buffer.getMaxCapacity()), which is the case when a long record occupy the entire buffer, `currentReaderPosition + bytesToSkip` will be equal to `buffer.getMaxCapacity()`, which means 
   I would read from `buffer.getMaxCapacity()`, which I guess I would get an out-of-range exception.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507171053



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);

Review comment:
       I've actually tried a bit to remove `createBufferConsumerFromBeginning`, which can work, but the clean-up logic is not as simple and beautful as it is now: `skip() + build()`. (Reason: skip can be unsuccessful because a partial record may not been written or visible yet before skip(), but can be visible after skip()).
   
   So I will keep the way we are going right now.
   
   To your question:
   `could it be createBufferConsumer()?`
   
   The answer is yes to create a new record, but I think the meaning is more clear to use `createBufferConsumerFromBeginning`. What do you think?
   
   I can definitely change it to `createBufferConsumer()`
   
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 553ff42a3ded621e45167d5a9f426934bf460ea4 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703) 
   * fd401b11ff98b70455ee395f8b4078851ab1ec9f Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504670777



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumer.java
##########
@@ -108,6 +109,30 @@ public Buffer build() {
 		return slice.retainBuffer();
 	}
 
+	/**
+	 * @param bytesToSkip number of bytes to skip from currentReaderPosition
+	 * @return sliced {@link Buffer} containing the not yet consumed data, Returned {@link Buffer} shares the reference
+	 * counter with the parent {@link BufferConsumer} - in order to recycle memory both of them must be recycled/closed.
+	 */
+	Buffer skipBuild(int bytesToSkip) {
+		writerPosition.update();
+		int cachedWriterPosition = writerPosition.getCached();
+		Buffer slice;
+
+		int bytesReadable = cachedWriterPosition - currentReaderPosition;
+		checkState(bytesToSkip <= bytesReadable, "bytes to skip beyond readable range");
+
+		if (bytesToSkip < buffer.getMaxCapacity()) {
+			slice = buffer.readOnlySlice(currentReaderPosition + bytesToSkip, bytesReadable - bytesToSkip);
+		} else {
+			// return an empty buffer if beyond buffer max capacity
+			slice = buffer.readOnlySlice(currentReaderPosition, 0);
+		}

Review comment:
       You are right, `bytesToSkip > buffer.getMaxCapacity()` is not possible. 
   **But**, the problem is if (bytesToSkip == buffer.getMaxCapacity()), which is the case when a long record occupy the entire buffer, `currentReaderPosition + bytesToSkip` euqals to `buffer.getMaxCapacity()`, which means 
   I would read from `buffer.getMaxCapacity()`. I guess I would get an out-of-range exception if I do that LOL




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "status" : "SUCCESS",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746",
       "triggerID" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "triggerType" : "PUSH"
     }, {
       "hash" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7995",
       "triggerID" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "triggerType" : "PUSH"
     }, {
       "hash" : "886615ca80abb71f262e5c8e981ae7f0b0247729",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8004",
       "triggerID" : "886615ca80abb71f262e5c8e981ae7f0b0247729",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * a70ae897b37437579d8aab2ba3a744553a08feea Azure: [SUCCESS](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746) 
   * 8a7c7987492c18b09b9111b8a341792a7cd508c1 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7995) 
   * 886615ca80abb71f262e5c8e981ae7f0b0247729 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8004) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504670777



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumer.java
##########
@@ -108,6 +109,30 @@ public Buffer build() {
 		return slice.retainBuffer();
 	}
 
+	/**
+	 * @param bytesToSkip number of bytes to skip from currentReaderPosition
+	 * @return sliced {@link Buffer} containing the not yet consumed data, Returned {@link Buffer} shares the reference
+	 * counter with the parent {@link BufferConsumer} - in order to recycle memory both of them must be recycled/closed.
+	 */
+	Buffer skipBuild(int bytesToSkip) {
+		writerPosition.update();
+		int cachedWriterPosition = writerPosition.getCached();
+		Buffer slice;
+
+		int bytesReadable = cachedWriterPosition - currentReaderPosition;
+		checkState(bytesToSkip <= bytesReadable, "bytes to skip beyond readable range");
+
+		if (bytesToSkip < buffer.getMaxCapacity()) {
+			slice = buffer.readOnlySlice(currentReaderPosition + bytesToSkip, bytesReadable - bytesToSkip);
+		} else {
+			// return an empty buffer if beyond buffer max capacity
+			slice = buffer.readOnlySlice(currentReaderPosition, 0);
+		}

Review comment:
       You are right, `bytesToSkip > buffer.getMaxCapacity()` is not possible. 
   But, the problem is if (bytesToSkip == buffer.getMaxCapacity()), which is the case when a long record occupy the entire buffer, `currentReaderPosition + bytesToSkip` euqals to `buffer.getMaxCapacity()`, which means 
   I would read from `buffer.getMaxCapacity()`, which I guess I would get an out-of-range exception.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] pnowojski commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
pnowojski commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r508271579



##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/ResultPartitionTest.java
##########
@@ -526,6 +526,44 @@ public void testFlushBoundedBlockingResultPartition() throws IOException {
 		assertNull(readView2.getNextBuffer());
 	}
 
+	@Test
+	public void testEmitRecordWithRecordSpanningMultipleBuffers() throws Exception {
+		BufferWritingResultPartition bufferWritingResultPartition = createResultPartition(ResultPartitionType.PIPELINED);
+		PipelinedSubpartition pipelinedSubpartition = (PipelinedSubpartition) bufferWritingResultPartition.subpartitions[0];
+		int partialLength = bufferSize / 3;
+
+		try {
+			// emit the first record, record length = partialLength
+			bufferWritingResultPartition.emitRecord(ByteBuffer.allocate(partialLength), 0);
+			// emit the second record, record length = bufferSize
+			bufferWritingResultPartition.emitRecord(ByteBuffer.allocate(bufferSize), 0);
+		} finally {
+			assertEquals(2, pipelinedSubpartition.getCurrentNumberOfBuffers());
+			assertEquals(0, pipelinedSubpartition.getNextBuffer().getPartialRecordLength());

Review comment:
       Ok I get it now, thanks for the explanation. I missed that `view.pollBuffer()` is doing much more. If you think it's valuable to have this unit tested in `ResultPartitionTest`, then such `@VisibleForTesting` method is ok 👍 




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r504675916



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferConsumerWithPartialRecordLength.java
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.buffer;
+
+import javax.annotation.concurrent.NotThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * BufferConsumer with partial record length if a record is spanning over buffers
+ *
+ * <p>`partialRecordLength` is the length of bytes to skip in order to start with a complete record,
+ * from position index 0 of the underlying MemorySegment. `partialRecordLength` is used in approximate
+ * local recovery to find the start position of a complete record on a BufferConsumer, so called
+ * `partial record clean-up`.
+ *
+ * <p>Partial records happen if a record can not fit into one buffer, then the remaining part of the same record
+ * is put into the next buffer. Hence partial records only exist at the beginning of a buffer.
+ * Partial record clean-up is needed in the mode of approximate local recovery.
+ * If a record is spanning over multiple buffers, and the first (several) buffers have got lost due to the failure
+ * of the receiver task, the remaining data belonging to the same record in transition should be cleaned up.
+ *
+ * <p> If partialRecordLength == 0, the buffer starts with a complete record</p>
+ * <p> If partialRecordLength > 0, the buffer starts with a partial record, its length = partialRecordLength</p>
+ * <p> If partialRecordLength < 0, partialRecordLength is undefined. It is currently used in
+ * 									{@cite ResultSubpartitionRecoveredStateHandler#recover}</p>
+ */
+@NotThreadSafe
+public class BufferConsumerWithPartialRecordLength {
+	private final BufferConsumer bufferConsumer;
+	private final int partialRecordLength;
+
+	public BufferConsumerWithPartialRecordLength(BufferConsumer bufferConsumer, int partialRecordLength) {
+		this.bufferConsumer = checkNotNull(bufferConsumer);
+		this.partialRecordLength = partialRecordLength;
+	}
+
+	public BufferConsumer getBufferConsumer() {
+		return bufferConsumer;
+	}
+
+	public int getPartialRecordLength() {
+		return partialRecordLength;
+	}
+
+	public Buffer build() {
+		return bufferConsumer.build();
+	}
+
+	public PartialRecordCleanupResult cleanupPartialRecord() {

Review comment:
       
   > * `skipBuild(...)` would be replaced with `skip(...)`, that would just move the offset, without returning the buffer
   > * `.build()` call would be required afterwards, to get the remaining data
   
   That would be perfect if we do not have `bytesToSkip == buffer.getMaxCapacity()`.
   
   In the current build(), currentReaderPosition can never be `buffer.getMaxCapacity()`, because the consumer has already been marked as finished `currentReaderPosition = cachedWriterPosition` if the full buffer is read. Remember in case of a record spanning over buffers, we finish the builder first and then request a new builder.
   
   But I can still handle this special case in build() if you are fine to change the build() method. I was trying to not touch the build() method which is used in the normal PipelinedSubPartition read
   
   > Do you really need to return the buffer here? I have a feeling that the could would be simpler if:
   > 
   > * this method was returning just `true/false` if cleanup has finished or not.
   
   Yes, I need both `whether the clean-up is successful` + `returned buffer`, which would be called later in PipelinedApproximateSubpartition within `pollBuffer` method to
   1. decide whether the clean up is successful
   2. Handle sliced Buffer if successful or not.
   
   The reason I am returning an `empty` buffer instead of a `null` buffer in case of `bytesToSkip == buffer.getMaxCapacity()` is also to be easier to follow the current logic in pollBuffer what if we do not read data.
   
   Overall I want to wrap the clean-up logic within `BufferConsumerWithPartialRecordLength`, which is the main reason why we introduce this class?
   
   
   
    




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "FAILURE",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 47d3d60c3dc74ab402ce657c31c27568b190de40 Azure: [FAILURE](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528) 
   * a001a7083dd0f78db05ef337692b5ddf08b354b0 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507148578



##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/ResultPartitionTest.java
##########
@@ -526,6 +526,44 @@ public void testFlushBoundedBlockingResultPartition() throws IOException {
 		assertNull(readView2.getNextBuffer());
 	}
 
+	@Test
+	public void testEmitRecordWithRecordSpanningMultipleBuffers() throws Exception {
+		BufferWritingResultPartition bufferWritingResultPartition = createResultPartition(ResultPartitionType.PIPELINED);
+		PipelinedSubpartition pipelinedSubpartition = (PipelinedSubpartition) bufferWritingResultPartition.subpartitions[0];
+		int partialLength = bufferSize / 3;
+
+		try {
+			// emit the first record, record length = partialLength
+			bufferWritingResultPartition.emitRecord(ByteBuffer.allocate(partialLength), 0);
+			// emit the second record, record length = bufferSize
+			bufferWritingResultPartition.emitRecord(ByteBuffer.allocate(bufferSize), 0);
+		} finally {
+			assertEquals(2, pipelinedSubpartition.getCurrentNumberOfBuffers());
+			assertEquals(0, pipelinedSubpartition.getNextBuffer().getPartialRecordLength());

Review comment:
       Hmm, it is a bit different, the view can only read `BufferAndBacklog`, that's the sliced buffer + some other information.
   
   What I need is `PartialRecordLength` in the buffer queue cell in `PrioritizedDeque<BufferConsumerWithPartialRecordLength> buffers`.
   
   Hmm, I am actually having a similar concern as you, so I can definitely remove this assertion. This information is tested in later PRs in different ways as well.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r505183102



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +225,62 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
-		}
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForNewRecord(int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), 0);
 
-		return getNewSubpartitionBufferBuilder(targetSubpartition);
+		return bufferBuilder;
 	}
 
-	private BufferBuilder getNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
+	private BufferBuilder getNewEmptySubpartitionBufferBuilderForRecordContinuation(
+			final ByteBuffer remainingRecordBytes,
+			final int targetSubpartition) throws IOException {
+		final BufferBuilder bufferBuilder = requestNewSubpartitionBufferBuilder(targetSubpartition);
+		final int partialRecordBytes = bufferBuilder.appendAndCommit(remainingRecordBytes);
+		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumerFromBeginning(), partialRecordBytes);
+
+		return bufferBuilder;
+	}
+
+	private BufferBuilder requestNewSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
 		checkInProduceState();
 		ensureUnicastMode();
-
 		final BufferBuilder bufferBuilder = requestNewBufferBuilderFromPool(targetSubpartition);
-		subpartitions[targetSubpartition].add(bufferBuilder.createBufferConsumer());
 		subpartitionBufferBuilders[targetSubpartition] = bufferBuilder;
+
 		return bufferBuilder;
 	}
 
-	private BufferBuilder getBroadcastBufferBuilder() throws IOException {
-		if (broadcastBufferBuilder != null) {
-			return broadcastBufferBuilder;
+	private BufferBuilder getNewEmptyBroadcastBufferBuilderForNewRecord() throws IOException {
+		final BufferBuilder bufferBuilder = requestNewBroadcastBufferBuilder();
+		try (final BufferConsumer consumer = bufferBuilder.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), 0);
+			}
 		}
 
-		return getNewBroadcastBufferBuilder();
+		return bufferBuilder;
 	}
 
-	private BufferBuilder getNewBroadcastBufferBuilder() throws IOException {
+	private BufferBuilder getNewEmptyBroadcastBufferBuilderForRecordContinuation(
+			final ByteBuffer remainingRecordBytes) throws IOException {
+			final BufferBuilder bufferBuilder = requestNewBroadcastBufferBuilder();
+		final int partialRecordBytes = bufferBuilder.appendAndCommit(remainingRecordBytes);
+		try (final BufferConsumer consumer = bufferBuilder.createBufferConsumerFromBeginning()) {
+			for (ResultSubpartition subpartition : subpartitions) {
+				subpartition.add(consumer.copy(), partialRecordBytes);

Review comment:
       I think that's roughly the same (from the complexity point of view).
   
   The only difference is easier to differentiate when remainingRecordLength == buffer.size()
   
   But I think that's not that much big difference.
   
   I agree the original code is a bit duplicated. I've rewritten it (literally just rewrite), to see whether it looks much better now.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] flinkbot edited a comment on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
flinkbot edited a comment on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-707710438


   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "f51a6098947a0967f0dadc32903bf1288fb48941",
       "triggerType" : "PUSH"
     }, {
       "hash" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7528",
       "triggerID" : "47d3d60c3dc74ab402ce657c31c27568b190de40",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7652",
       "triggerID" : "a001a7083dd0f78db05ef337692b5ddf08b354b0",
       "triggerType" : "PUSH"
     }, {
       "hash" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7703",
       "triggerID" : "553ff42a3ded621e45167d5a9f426934bf460ea4",
       "triggerType" : "PUSH"
     }, {
       "hash" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7717",
       "triggerID" : "fd401b11ff98b70455ee395f8b4078851ab1ec9f",
       "triggerType" : "PUSH"
     }, {
       "hash" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "status" : "DELETED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7746",
       "triggerID" : "a70ae897b37437579d8aab2ba3a744553a08feea",
       "triggerType" : "PUSH"
     }, {
       "hash" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "status" : "CANCELED",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7995",
       "triggerID" : "8a7c7987492c18b09b9111b8a341792a7cd508c1",
       "triggerType" : "PUSH"
     }, {
       "hash" : "886615ca80abb71f262e5c8e981ae7f0b0247729",
       "status" : "PENDING",
       "url" : "https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8004",
       "triggerID" : "886615ca80abb71f262e5c8e981ae7f0b0247729",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * f51a6098947a0967f0dadc32903bf1288fb48941 UNKNOWN
   * 8a7c7987492c18b09b9111b8a341792a7cd508c1 Azure: [CANCELED](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=7995) 
   * 886615ca80abb71f262e5c8e981ae7f0b0247729 Azure: [PENDING](https://dev.azure.com/apache-flink/98463496-1af2-4620-8eab-a2ecc1a2e6fe/_build/results?buildId=8004) 
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run travis` re-run the last Travis build
    - `@flinkbot run azure` re-run the last Azure build
   </details>


----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507148578



##########
File path: flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/ResultPartitionTest.java
##########
@@ -526,6 +526,44 @@ public void testFlushBoundedBlockingResultPartition() throws IOException {
 		assertNull(readView2.getNextBuffer());
 	}
 
+	@Test
+	public void testEmitRecordWithRecordSpanningMultipleBuffers() throws Exception {
+		BufferWritingResultPartition bufferWritingResultPartition = createResultPartition(ResultPartitionType.PIPELINED);
+		PipelinedSubpartition pipelinedSubpartition = (PipelinedSubpartition) bufferWritingResultPartition.subpartitions[0];
+		int partialLength = bufferSize / 3;
+
+		try {
+			// emit the first record, record length = partialLength
+			bufferWritingResultPartition.emitRecord(ByteBuffer.allocate(partialLength), 0);
+			// emit the second record, record length = bufferSize
+			bufferWritingResultPartition.emitRecord(ByteBuffer.allocate(bufferSize), 0);
+		} finally {
+			assertEquals(2, pipelinedSubpartition.getCurrentNumberOfBuffers());
+			assertEquals(0, pipelinedSubpartition.getNextBuffer().getPartialRecordLength());

Review comment:
       the view can only read `BufferAndBacklog`, that's the sliced buffer + some other information.
   
   What I need is `PartialRecordLength` in the buffer queue cell `PrioritizedDeque<BufferConsumerWithPartialRecordLength> buffers`.
   
   If you really do not like this, I can remove this assertion. This information is tested in later PRs in different ways as well.




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on a change in pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on a change in pull request #13614:
URL: https://github.com/apache/flink/pull/13614#discussion_r507171053



##########
File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/BufferWritingResultPartition.java
##########
@@ -211,46 +223,83 @@ protected void releaseInternal() {
 		}
 	}
 
-	private BufferBuilder getSubpartitionBufferBuilder(int targetSubpartition) throws IOException {
-		final BufferBuilder bufferBuilder = subpartitionBufferBuilders[targetSubpartition];
-		if (bufferBuilder != null) {
-			return bufferBuilder;
+	private BufferBuilder appendDataToSubpartitionForNewRecord(
+			ByteBuffer record, int targetSubpartition) throws IOException {
+		BufferBuilder buffer = subpartitionBufferBuilders[targetSubpartition];
+
+		if (buffer == null) {
+			buffer = requestNewSubpartitionBufferBuilder(targetSubpartition);
+			subpartitions[targetSubpartition].add(buffer.createBufferConsumerFromBeginning(), 0);

Review comment:
       I've actually tried a bit to remove `createBufferConsumerFromBeginning`, which can work, but the clean-up logic is not as simple and beautful as it is now: `skip() + build()`. Reason: skip can be unsuccessful because a partial record may not been written or visible yet before skip(), but can be visible after skip().
   
   So I will keep what we have right now.
   
   To your question:
   `could it be createBufferConsumer()?`
   
   The answer is yes to create a new record, but I think the meaning is more clear to use `createBufferConsumerFromBeginning`. What do you think?
   
   I can definitely change it to `createBufferConsumer()`
   
   




----------------------------------------------------------------
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:
users@infra.apache.org



[GitHub] [flink] curcur commented on pull request #13614: [FLINK-19547][runtime] Clean up partial record when reconnecting for Approximate Local Recovery

Posted by GitBox <gi...@apache.org>.
curcur commented on pull request #13614:
URL: https://github.com/apache/flink/pull/13614#issuecomment-708138894


   Local azure results:
   
   https://dev.azure.com/mymeiyuan/Flink/_build/results?buildId=92&view=results
   
   green light except `JMXReporterFactoryTest` due to FLINK-19539


----------------------------------------------------------------
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:
users@infra.apache.org