You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2023/01/13 06:19:04 UTC

[GitHub] [ignite-3] rpuch commented on a diff in pull request #1514: IGNITE-17833 Implement partition destruction for volatile PageMemory

rpuch commented on code in PR #1514:
URL: https://github.com/apache/ignite-3/pull/1514#discussion_r1068971058


##########
modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/util/GradualTaskExecutor.java:
##########
@@ -0,0 +1,105 @@
+/*
+ * 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.ignite.internal.pagememory.util;
+
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+import org.apache.ignite.internal.close.ManuallyCloseable;
+import org.apache.ignite.internal.future.InFlightFutures;
+import org.apache.ignite.internal.util.IgniteUtils;
+
+/**
+ * Executor for {@link GradualTask}s. See GradualTask documentation for details.
+ *
+ * <p>This executor owns an executor service that it is passed, so it shuts it down when being closed.
+ *
+ * @see GradualTask
+ */
+public class GradualTaskExecutor implements ManuallyCloseable {
+    private final ExecutorService executor;
+
+    private final InFlightFutures inFlightFutures = new InFlightFutures();
+
+    private volatile boolean cancelled = false;
+
+    public GradualTaskExecutor(ExecutorService executor) {
+        this.executor = executor;
+    }
+
+    /**
+     * Starts execution of a {@link GradualTask} and returns a future that completes when the task completes.
+     *
+     * @param task Task to execute.
+     * @return Future that completes when the task completes.
+     */
+    public CompletableFuture<Void> execute(GradualTask task) {
+        CompletableFuture<Void> future = new CompletableFuture<>();
+
+        inFlightFutures.registerFuture(future);
+
+        Runnable runnable = new Runnable() {
+            @Override
+            public void run() {
+                try {
+                    if (cancelled) {
+                        future.completeExceptionally(new CancellationException("The executor has been closed"));
+
+                        return;
+                    }
+
+                    if (task.isCompleted()) {
+                        future.complete(null);
+
+                        return;
+                    }
+
+                    task.runStep();
+
+                    if (task.isCompleted()) {
+                        future.complete(null);
+                    } else if (cancelled) {
+                        future.completeExceptionally(new CancellationException("The executor has been closed"));
+                    } else {
+                        executor.execute(this);
+                    }
+                } catch (Error e) {
+                    future.completeExceptionally(e);
+
+                    throw e;
+                } catch (Exception e) {
+                    future.completeExceptionally(e);
+                }
+            }
+        };
+
+        executor.execute(runnable);
+
+        return future.whenComplete((res, ex) -> task.cleanup());
+    }
+
+    @Override
+    public void close() {
+        cancelled = true;
+
+        IgniteUtils.shutdownAndAwaitTermination(executor, 10, TimeUnit.SECONDS);
+
+        inFlightFutures.cancelInFlightFutures();

Review Comment:
   If we do so, there will be a possibility that some futures will be added after we cancel them, but before the executor gets stopped, so they might remain uncancelled in the end. On the other hand, with the current approach, we'll cancel all not-yet-completed futures.



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

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org