flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] azagrebin commented on a change in pull request #6556: FLINK-10042][state] Extract snapshot algorithms from inner classes of RocksDBKeyedStateBackend into full classes
Date Mon, 20 Aug 2018 17:52:14 GMT
azagrebin commented on a change in pull request #6556: FLINK-10042][state] Extract snapshot
algorithms from inner classes of RocksDBKeyedStateBackend into full classes
URL: https://github.com/apache/flink/pull/6556#discussion_r211193248
 
 

 ##########
 File path: flink-runtime/src/main/java/org/apache/flink/runtime/state/AsyncSnapshotCallable.java
 ##########
 @@ -0,0 +1,190 @@
+/*
+ * 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.state;
+
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.core.fs.CloseableRegistry;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.concurrent.Callable;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.FutureTask;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+/**
+ * Base class that outlines the strategy for asynchronous snapshots. Implementations of this
class are typically
+ * instantiated with resources that have been created in the synchronous part of a snapshot.
Then, the implementation
+ * of {@link #callInternal()} is invoked in the asynchronous part. All resources created
by this methods should
+ * be released by the end of the method. If the created resources are {@link Closeable} objects
and can block in calls
+ * (e.g. in/output streams), they should be registered with the snapshot's {@link CloseableRegistry}
so that the can
+ * be closed and unblocked on cancellation. After {@link #callInternal()} ended, {@link #logAsyncSnapshotComplete(long)}
+ * is called. In that method, implementations can emit log statements about the duration.
At the very end, this class
+ * calls {@link #cleanupProvidedResources()}. The implementation of this method should release
all provided resources
+ * that have been passed into the snapshot from the synchronous part of the snapshot.
+ *
+ * @param <T> type of the result.
+ */
+public abstract class AsyncSnapshotCallable<T> implements Callable<T> {
+
+	/** Message for the {@link CancellationException}. */
+	private static final String CANCELLATION_EXCEPTION_MSG = "Async snapshot was cancelled.";
+
+	private static final Logger LOG = LoggerFactory.getLogger(AsyncSnapshotCallable.class);
+
+	/** This is used to atomically claim ownership for the resource cleanup. */
+	@Nonnull
+	private final AtomicBoolean resourceCleanupOwnershipTaken;
+
+	/** Registers streams that can block in I/O during snapshot. Forwards close from taskCancelCloseableRegistry.
*/
+	@Nonnull
+	private final CloseableRegistry snapshotCloseableRegistry;
+
+	protected AsyncSnapshotCallable() {
+		this.snapshotCloseableRegistry = new CloseableRegistry();
+		this.resourceCleanupOwnershipTaken = new AtomicBoolean(false);
+	}
+
+	@Override
+	public T call() throws Exception {
+		final long startTime = System.currentTimeMillis();
+
+		if (resourceCleanupOwnershipTaken.compareAndSet(false, true)) {
+			try {
+				T result = callInternal();
+				logAsyncSnapshotComplete(startTime);
+				return result;
+			} catch (Exception ex) {
+				if (!snapshotCloseableRegistry.isClosed()) {
+					throw ex;
+				}
+			} finally {
+				closeSnapshotIO();
+				cleanup();
+			}
+		}
+
+		throw new CancellationException(CANCELLATION_EXCEPTION_MSG);
+	}
+
+	@VisibleForTesting
+	protected void cancel() {
+		closeSnapshotIO();
+		if (resourceCleanupOwnershipTaken.compareAndSet(false, true)) {
+			cleanup();
+		}
+	}
+
+	/**
+	 * Creates a future task from this and registers it with the given {@link CloseableRegistry}.
The task is
+	 * unregistered again in {@link FutureTask#done()}.
+	 */
+	public AsyncSnapshotTask toAsyncSnapshotFutureTask(@Nonnull CloseableRegistry taskRegistry)
throws IOException {
+		return new AsyncSnapshotTask(taskRegistry);
+	}
+
+	/**
+	 * {@link FutureTask} that wraps a {@link AsyncSnapshotCallable} and connects it with cancellation
and closing.
+	 */
+	public class AsyncSnapshotTask extends FutureTask<T> {
 
 Review comment:
   `AsyncSnapshotCallable` is always converted to `AsyncSnapshotTask` after its creation.
I wonder if they could be one task class in the first place, unless there is a good reason
and they have really different concerns.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message