You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by pn...@apache.org on 2018/11/27 14:27:19 UTC

[flink] branch master updated: [FLINK-10367][network] Introduce NotificationResult for BufferListener to solve recursive stack overflow (#6829)

This is an automated email from the ASF dual-hosted git repository.

pnowojski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new b379316  [FLINK-10367][network] Introduce NotificationResult for BufferListener to solve recursive stack overflow (#6829)
b379316 is described below

commit b379316dc835c4fbec574452690ecca770243aef
Author: zhijiang <wa...@aliyun.com>
AuthorDate: Tue Nov 27 22:27:12 2018 +0800

    [FLINK-10367][network] Introduce NotificationResult for BufferListener to solve recursive stack overflow (#6829)
    
    Before this fix, during LocalBufferPool#recycle, the recycled buffer would be notified to a BufferListener. But this BufferListener may not need floating buffers any more currently, so this buffer is recycled again to the LocalBufferPool, then another BufferListener is selected to be notified of this available buffer.
    
    The above process may be repeatedly triggered in recursive way that will cause stack overflow error in extreme case. We ever encountered this error triggered by release all resources during task failover in large scale job, especially it will also result in buffer leak after stack overflow.
    
    This fix removes recursive calls and replace them with a loop and adds a `NotificationResult` as a result from BufferListener#notifyBufferAvailable to inform the caller whether the buffer was used or not.
---
 .../runtime/io/network/buffer/BufferListener.java  | 39 ++++++++++++++++++++--
 .../runtime/io/network/buffer/LocalBufferPool.java | 36 ++++++++++----------
 .../netty/PartitionRequestClientHandler.java       | 32 +++++-------------
 .../partition/consumer/RemoteInputChannel.java     | 27 ++++++---------
 .../io/network/buffer/LocalBufferPoolTest.java     |  8 +++--
 .../partition/consumer/RemoteInputChannelTest.java |  6 +++-
 6 files changed, 86 insertions(+), 62 deletions(-)

diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferListener.java b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferListener.java
index 4cc32c0..e6b5416 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferListener.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/BufferListener.java
@@ -25,6 +25,41 @@ package org.apache.flink.runtime.io.network.buffer;
 public interface BufferListener {
 
 	/**
+	 * Status of the notification result from the buffer listener.
+	 */
+	enum NotificationResult {
+		BUFFER_NOT_USED(false, false),
+		BUFFER_USED_NO_NEED_MORE(true, false),
+		BUFFER_USED_NEED_MORE(true, true);
+
+		private final boolean isBufferUsed;
+		private final boolean needsMoreBuffers;
+
+		NotificationResult(boolean isBufferUsed, boolean needsMoreBuffers) {
+			this.isBufferUsed = isBufferUsed;
+			this.needsMoreBuffers = needsMoreBuffers;
+		}
+
+		/**
+		 * Whether the notified buffer is accepted to use by the listener.
+		 *
+		 * @return <tt>true</tt> if the notified buffer is accepted.
+		 */
+		boolean isBufferUsed() {
+			return isBufferUsed;
+		}
+
+		/**
+		 * Whether the listener still needs more buffers to be notified.
+		 *
+		 * @return <tt>true</tt> if the listener is still waiting for more buffers.
+		 */
+		boolean needsMoreBuffers() {
+			return needsMoreBuffers;
+		}
+	}
+
+	/**
 	 * Notification callback if a buffer is recycled and becomes available in buffer pool.
 	 *
 	 * <p>Note: responsibility on recycling the given buffer is transferred to this implementation,
@@ -37,9 +72,9 @@ public interface BufferListener {
 	 * stack!
 	 *
 	 * @param buffer buffer that becomes available in buffer pool.
-	 * @return true if the listener wants to be notified next time.
+	 * @return NotificationResult if the listener wants to be notified next time.
 	 */
-	boolean notifyBufferAvailable(Buffer buffer);
+	NotificationResult notifyBufferAvailable(Buffer buffer);
 
 	/**
 	 * Notification callback if the buffer provider is destroyed.
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/LocalBufferPool.java b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/LocalBufferPool.java
index d7bfb60..dfad5b9 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/LocalBufferPool.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/buffer/LocalBufferPool.java
@@ -19,6 +19,7 @@
 package org.apache.flink.runtime.io.network.buffer;
 
 import org.apache.flink.core.memory.MemorySegment;
+import org.apache.flink.runtime.io.network.buffer.BufferListener.NotificationResult;
 import org.apache.flink.util.ExceptionUtils;
 
 import org.slf4j.Logger;
@@ -271,30 +272,31 @@ class LocalBufferPool implements BufferPool {
 	@Override
 	public void recycle(MemorySegment segment) {
 		BufferListener listener;
-		synchronized (availableMemorySegments) {
-			if (isDestroyed || numberOfRequestedMemorySegments > currentPoolSize) {
-				returnMemorySegment(segment);
-				return;
-			} else {
-				listener = registeredListeners.poll();
-
-				if (listener == null) {
-					availableMemorySegments.add(segment);
-					availableMemorySegments.notify();
+		NotificationResult notificationResult = NotificationResult.BUFFER_NOT_USED;
+		while (!notificationResult.isBufferUsed()) {
+			synchronized (availableMemorySegments) {
+				if (isDestroyed || numberOfRequestedMemorySegments > currentPoolSize) {
+					returnMemorySegment(segment);
 					return;
+				} else {
+					listener = registeredListeners.poll();
+					if (listener == null) {
+						availableMemorySegments.add(segment);
+						availableMemorySegments.notify();
+						return;
+					}
 				}
 			}
+			notificationResult = fireBufferAvailableNotification(listener, segment);
 		}
+	}
 
+	private NotificationResult fireBufferAvailableNotification(BufferListener listener, MemorySegment segment) {
 		// We do not know which locks have been acquired before the recycle() or are needed in the
 		// notification and which other threads also access them.
 		// -> call notifyBufferAvailable() outside of the synchronized block to avoid a deadlock (FLINK-9676)
-		// Note that in case of any exceptions notifyBufferAvailable() should recycle the buffer
-		// (either directly or later during error handling) and therefore eventually end up in this
-		// method again.
-		boolean needMoreBuffers = listener.notifyBufferAvailable(new NetworkBuffer(segment, this));
-
-		if (needMoreBuffers) {
+		NotificationResult notificationResult = listener.notifyBufferAvailable(new NetworkBuffer(segment, this));
+		if (notificationResult.needsMoreBuffers()) {
 			synchronized (availableMemorySegments) {
 				if (isDestroyed) {
 					// cleanup tasks how they would have been done if we only had one synchronized block
@@ -304,6 +306,7 @@ class LocalBufferPool implements BufferPool {
 				}
 			}
 		}
+		return notificationResult;
 	}
 
 	/**
@@ -404,5 +407,4 @@ class LocalBufferPool implements BufferPool {
 			returnMemorySegment(segment);
 		}
 	}
-
 }
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/PartitionRequestClientHandler.java b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/PartitionRequestClientHandler.java
index c5ba7a4..34f65c0 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/PartitionRequestClientHandler.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/PartitionRequestClientHandler.java
@@ -417,32 +417,18 @@ class PartitionRequestClientHandler extends ChannelInboundHandlerAdapter impleme
 
 		// Called by the recycling thread (not network I/O thread)
 		@Override
-		public boolean notifyBufferAvailable(Buffer buffer) {
-			boolean success = false;
-
-			try {
-				if (availableBuffer.compareAndSet(null, buffer)) {
-					ctx.channel().eventLoop().execute(this);
+		public NotificationResult notifyBufferAvailable(Buffer buffer) {
+			if (availableBuffer.compareAndSet(null, buffer)) {
+				ctx.channel().eventLoop().execute(this);
 
-					success = true;
-				}
-				else {
-					throw new IllegalStateException("Received a buffer notification, " +
-							" but the previous one has not been handled yet.");
-				}
-			}
-			catch (Throwable t) {
-				ctx.channel().eventLoop().execute(new AsyncErrorNotificationTask(t));
+				return NotificationResult.BUFFER_USED_NO_NEED_MORE;
 			}
-			finally {
-				if (!success) {
-					if (buffer != null) {
-						buffer.recycleBuffer();
-					}
-				}
+			else {
+				ctx.channel().eventLoop().execute(new AsyncErrorNotificationTask(
+					new IllegalStateException("Received a buffer notification, " +
+						" but the previous one has not been handled yet.")));
+				return NotificationResult.BUFFER_NOT_USED;
 			}
-
-			return false;
 		}
 
 		/**
diff --git a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/consumer/RemoteInputChannel.java b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/consumer/RemoteInputChannel.java
index 6738abd..1414949 100644
--- a/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/consumer/RemoteInputChannel.java
+++ b/flink-runtime/src/main/java/org/apache/flink/runtime/io/network/partition/consumer/RemoteInputChannel.java
@@ -342,18 +342,18 @@ public class RemoteInputChannel extends InputChannel implements BufferRecycler,
 
 	/**
 	 * The Buffer pool notifies this channel of an available floating buffer. If the channel is released or
-	 * currently does not need extra buffers, the buffer should be recycled to the buffer pool. Otherwise,
+	 * currently does not need extra buffers, the buffer should be returned to the buffer pool. Otherwise,
 	 * the buffer will be added into the <tt>bufferQueue</tt> and the unannounced credit is increased
 	 * by one.
 	 *
 	 * @param buffer Buffer that becomes available in buffer pool.
-	 * @return True when this channel is waiting for more floating buffers, otherwise false.
+	 * @return NotificationResult indicates whether this channel accepts the buffer and is waiting for
+	 *  	more floating buffers.
 	 */
 	@Override
-	public boolean notifyBufferAvailable(Buffer buffer) {
-		boolean recycleBuffer = true;
+	public NotificationResult notifyBufferAvailable(Buffer buffer) {
+		NotificationResult notificationResult = NotificationResult.BUFFER_NOT_USED;
 		try {
-			boolean needMoreBuffers = false;
 			synchronized (bufferQueue) {
 				checkState(isWaitingForFloatingBuffers,
 					"This channel should be waiting for floating buffers.");
@@ -364,36 +364,29 @@ public class RemoteInputChannel extends InputChannel implements BufferRecycler,
 				// -> then isReleased is set correctly
 				// 2) releaseAllResources() did not yet release buffers from bufferQueue
 				// -> we may or may not have set isReleased yet but will always wait for the
-				//    lock on bufferQueue to release buffers
+				// lock on bufferQueue to release buffers
 				if (isReleased.get() || bufferQueue.getAvailableBufferSize() >= numRequiredBuffers) {
 					isWaitingForFloatingBuffers = false;
-					recycleBuffer = false; // just in case
-					buffer.recycleBuffer();
-					return false;
+					return notificationResult;
 				}
 
-				recycleBuffer = false;
 				bufferQueue.addFloatingBuffer(buffer);
 
 				if (bufferQueue.getAvailableBufferSize() == numRequiredBuffers) {
 					isWaitingForFloatingBuffers = false;
+					notificationResult = NotificationResult.BUFFER_USED_NO_NEED_MORE;
 				} else {
-					needMoreBuffers = true;
+					notificationResult = NotificationResult.BUFFER_USED_NEED_MORE;
 				}
 			}
 
 			if (unannouncedCredit.getAndAdd(1) == 0) {
 				notifyCreditAvailable();
 			}
-
-			return needMoreBuffers;
 		} catch (Throwable t) {
-			if (recycleBuffer) {
-				buffer.recycleBuffer();
-			}
 			setError(t);
-			return false;
 		}
+		return notificationResult;
 	}
 
 	@Override
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/io/network/buffer/LocalBufferPoolTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/io/network/buffer/LocalBufferPoolTest.java
index 537d167..a0e10d7 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/io/network/buffer/LocalBufferPoolTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/io/network/buffer/LocalBufferPoolTest.java
@@ -414,10 +414,14 @@ public class LocalBufferPoolTest extends TestLogger {
 			AtomicInteger times = new AtomicInteger(0);
 
 			@Override
-			public boolean notifyBufferAvailable(Buffer buffer) {
+			public NotificationResult notifyBufferAvailable(Buffer buffer) {
 				int newCount = times.incrementAndGet();
 				buffer.recycleBuffer();
-				return newCount < notificationTimes;
+				if (newCount < notificationTimes) {
+					return NotificationResult.BUFFER_USED_NEED_MORE;
+				} else {
+					return NotificationResult.BUFFER_USED_NO_NEED_MORE;
+				}
 			}
 
 			@Override
diff --git a/flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/consumer/RemoteInputChannelTest.java b/flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/consumer/RemoteInputChannelTest.java
index ec80459..7747421 100644
--- a/flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/consumer/RemoteInputChannelTest.java
+++ b/flink-runtime/src/test/java/org/apache/flink/runtime/io/network/partition/consumer/RemoteInputChannelTest.java
@@ -23,6 +23,7 @@ import org.apache.flink.runtime.execution.CancelTaskException;
 import org.apache.flink.runtime.io.network.ConnectionID;
 import org.apache.flink.runtime.io.network.ConnectionManager;
 import org.apache.flink.runtime.io.network.buffer.Buffer;
+import org.apache.flink.runtime.io.network.buffer.BufferListener.NotificationResult;
 import org.apache.flink.runtime.io.network.buffer.BufferPool;
 import org.apache.flink.runtime.io.network.buffer.NetworkBufferPool;
 import org.apache.flink.runtime.io.network.netty.PartitionRequestClient;
@@ -150,7 +151,10 @@ public class RemoteInputChannelTest {
 						for (int j = 0; j < 128; j++) {
 							// this is the same buffer over and over again which will be
 							// recycled by the RemoteInputChannel
-							function.apply(inputChannel, buffer.retainBuffer(), j);
+							Object obj = function.apply(inputChannel, buffer.retainBuffer(), j);
+							if (obj instanceof NotificationResult && obj == NotificationResult.BUFFER_NOT_USED) {
+								buffer.recycleBuffer();
+							}
 						}
 
 						if (inputChannel.isReleased()) {