You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by mp...@apache.org on 2024/06/18 13:14:58 UTC

(ignite-3) branch main updated: IGNITE-22512 Make MapReduceTask methods asynchronous (#3938)

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

mpochatkin pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new 0069defae4 IGNITE-22512 Make MapReduceTask methods asynchronous (#3938)
0069defae4 is described below

commit 0069defae43414a72c8c24285195ddc5034771a8
Author: Vadim Pakhnushev <86...@users.noreply.github.com>
AuthorDate: Tue Jun 18 16:14:53 2024 +0300

    IGNITE-22512 Make MapReduceTask methods asynchronous (#3938)
---
 .../apache/ignite/compute/task/MapReduceTask.java  | 13 ++++----
 .../internal/compute/utils/InteractiveTasks.java   | 26 ++++++++-------
 .../apache/ignite/internal/compute/MapReduce.java  | 14 ++++----
 .../internal/compute/queue/QueueExecutionImpl.java |  2 +-
 .../compute/task/TaskExecutionInternal.java        | 11 ++++---
 .../runner/app/client/ItThinClientComputeTest.java | 38 +++++++++++-----------
 6 files changed, 55 insertions(+), 49 deletions(-)

diff --git a/modules/api/src/main/java/org/apache/ignite/compute/task/MapReduceTask.java b/modules/api/src/main/java/org/apache/ignite/compute/task/MapReduceTask.java
index c328e90c32..3c547ec2e2 100644
--- a/modules/api/src/main/java/org/apache/ignite/compute/task/MapReduceTask.java
+++ b/modules/api/src/main/java/org/apache/ignite/compute/task/MapReduceTask.java
@@ -20,6 +20,7 @@ package org.apache.ignite.compute.task;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
 
 /**
  * A map reduce task interface. Implement this interface and pass a name of the implemented class to the
@@ -34,18 +35,18 @@ public interface MapReduceTask<R> {
      *
      * @param taskContext Task execution context.
      * @param args Map reduce task arguments.
-     * @return A list of compute job execution parameters.
+     * @return A future with the list of compute job execution parameters.
      */
-    List<MapReduceJob> split(TaskExecutionContext taskContext, Object... args);
+    CompletableFuture<List<MapReduceJob>> splitAsync(TaskExecutionContext taskContext, Object... args);
 
     /**
      * This is a finishing step in the task execution. This method will be called with the map from identifiers of compute jobs submitted as
-     * a result of the {@link #split(TaskExecutionContext, Object...)} method call to the results of the execution of the corresponding job.
-     * The return value of this method will be returned as a result of this task.
+     * a result of the {@link #splitAsync(TaskExecutionContext, Object...)} method call to the results of the execution of the corresponding
+     * job. The return value of this method will be returned as a result of this task.
      *
      * @param taskContext Task execution context.
      * @param results Map from compute job ids to their results.
-     * @return Final task result.
+     * @return Final task result future.
      */
-    R reduce(TaskExecutionContext taskContext, Map<UUID, ?> results);
+    CompletableFuture<R> reduceAsync(TaskExecutionContext taskContext, Map<UUID, ?> results);
 }
diff --git a/modules/compute/src/integrationTest/java/org/apache/ignite/internal/compute/utils/InteractiveTasks.java b/modules/compute/src/integrationTest/java/org/apache/ignite/internal/compute/utils/InteractiveTasks.java
index cded885c9a..d9c65ed1d3 100644
--- a/modules/compute/src/integrationTest/java/org/apache/ignite/internal/compute/utils/InteractiveTasks.java
+++ b/modules/compute/src/integrationTest/java/org/apache/ignite/internal/compute/utils/InteractiveTasks.java
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.internal.compute.utils;
 
+import static java.util.concurrent.CompletableFuture.completedFuture;
 import static java.util.stream.Collectors.toList;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.equalTo;
@@ -27,6 +28,7 @@ import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -60,16 +62,16 @@ public final class InteractiveTasks {
     private static final BlockingQueue<Object> GLOBAL_CHANNEL = new LinkedBlockingQueue<>();
 
     /**
-     * This counter indicated how many {@link GlobalInteractiveMapReduceTask#split(TaskExecutionContext, Object...)} methods are running
-     * now. This counter increased each time the {@link GlobalInteractiveMapReduceTask#split(TaskExecutionContext, Object...)} is called and
-     * decreased when the method is finished (whatever the result is). Checked in {@link #clearState}.
+     * This counter indicated how many {@link GlobalInteractiveMapReduceTask#splitAsync(TaskExecutionContext, Object...)} methods are
+     * running now. This counter increased each time the {@link GlobalInteractiveMapReduceTask#splitAsync(TaskExecutionContext, Object...)}
+     * is called and decreased when the method is finished (whatever the result is). Checked in {@link #clearState}.
      */
     private static final AtomicInteger RUNNING_GLOBAL_SPLIT_CNT = new AtomicInteger(0);
 
     /**
-     * This counter indicates how many {@link GlobalInteractiveMapReduceTask#reduce(TaskExecutionContext, Map)} methods are running now.
-     * This counter is increased every time the {@link GlobalInteractiveMapReduceTask#reduce(TaskExecutionContext, Map)} is called and
-     * decreased when the method is finished (whatever the result is). Checked in {@link #clearState}.
+     * This counter indicates how many {@link GlobalInteractiveMapReduceTask#reduceAsync(TaskExecutionContext, Map)} methods are running
+     * now. This counter is increased every time the {@link GlobalInteractiveMapReduceTask#reduceAsync(TaskExecutionContext, Map)} is called
+     * and decreased when the method is finished (whatever the result is). Checked in {@link #clearState}.
      */
     private static final AtomicInteger RUNNING_GLOBAL_REDUCE_CNT = new AtomicInteger(0);
 
@@ -169,7 +171,7 @@ public final class InteractiveTasks {
         }
 
         @Override
-        public List<MapReduceJob> split(TaskExecutionContext context, Object... args) {
+        public CompletableFuture<List<MapReduceJob>> splitAsync(TaskExecutionContext context, Object... args) {
             RUNNING_GLOBAL_SPLIT_CNT.incrementAndGet();
 
             offerArgsAsSignals(args);
@@ -190,12 +192,12 @@ public final class InteractiveTasks {
                             GLOBAL_CHANNEL.offer(ACK);
                             break;
                         case SPLIT_RETURN_ALL_NODES:
-                            return context.ignite().clusterNodes().stream().map(node ->
+                            return completedFuture(context.ignite().clusterNodes().stream().map(node ->
                                     MapReduceJob.builder()
                                             .jobDescriptor(JobDescriptor.builder(InteractiveJobs.interactiveJobName()).build())
                                             .nodes(Set.of(node))
                                             .build()
-                            ).collect(toList());
+                            ).collect(toList()));
                         case CHECK_CANCEL:
                             if (context.isCancelled()) {
                                 throw new RuntimeException("Task is cancelled");
@@ -211,7 +213,7 @@ public final class InteractiveTasks {
         }
 
         @Override
-        public List<String> reduce(TaskExecutionContext context, Map<UUID, ?> results) {
+        public CompletableFuture<List<String>> reduceAsync(TaskExecutionContext context, Map<UUID, ?> results) {
             RUNNING_GLOBAL_REDUCE_CNT.incrementAndGet();
             try {
                 while (true) {
@@ -223,9 +225,9 @@ public final class InteractiveTasks {
                             GLOBAL_CHANNEL.offer(ACK);
                             break;
                         case REDUCE_RETURN:
-                            return results.values().stream()
+                            return completedFuture(results.values().stream()
                                     .map(String.class::cast)
-                                    .collect(toList());
+                                    .collect(toList()));
                         case CHECK_CANCEL:
                             if (context.isCancelled()) {
                                 throw new RuntimeException("Task is cancelled");
diff --git a/modules/compute/src/jobs/java/org/apache/ignite/internal/compute/MapReduce.java b/modules/compute/src/jobs/java/org/apache/ignite/internal/compute/MapReduce.java
index 78458216ef..e176d81b29 100644
--- a/modules/compute/src/jobs/java/org/apache/ignite/internal/compute/MapReduce.java
+++ b/modules/compute/src/jobs/java/org/apache/ignite/internal/compute/MapReduce.java
@@ -17,12 +17,14 @@
 
 package org.apache.ignite.internal.compute;
 
+import static java.util.concurrent.CompletableFuture.completedFuture;
 import static java.util.stream.Collectors.toList;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
 import org.apache.ignite.compute.DeploymentUnit;
 import org.apache.ignite.compute.JobDescriptor;
 import org.apache.ignite.compute.JobExecutionOptions;
@@ -33,10 +35,10 @@ import org.apache.ignite.compute.task.TaskExecutionContext;
 /** Map reduce task which runs a {@link GetNodeNameJob} on each node and computes a sum of length of all node names. */
 public class MapReduce implements MapReduceTask<Integer> {
     @Override
-    public List<MapReduceJob> split(TaskExecutionContext taskContext, Object... args) {
+    public CompletableFuture<List<MapReduceJob>> splitAsync(TaskExecutionContext taskContext, Object... args) {
         List<DeploymentUnit> deploymentUnits = (List<DeploymentUnit>) args[0];
 
-        return taskContext.ignite().clusterNodes().stream().map(node ->
+        return taskContext.ignite().clusterNodesAsync().thenApply(nodes -> nodes.stream().map(node ->
                 MapReduceJob.builder()
                         .jobDescriptor(JobDescriptor.builder(GetNodeNameJob.class)
                                 .units(deploymentUnits)
@@ -47,15 +49,15 @@ public class MapReduce implements MapReduceTask<Integer> {
                                 .build())
                         .nodes(Set.of(node))
                         .build()
-        ).collect(toList());
+        ).collect(toList()));
     }
 
     @Override
-    public Integer reduce(TaskExecutionContext taskContext, Map<UUID, ?> results) {
-        return results.values().stream()
+    public CompletableFuture<Integer> reduceAsync(TaskExecutionContext taskContext, Map<UUID, ?> results) {
+        return completedFuture(results.values().stream()
                 .map(String.class::cast)
                 .map(String::length)
                 .reduce(Integer::sum)
-                .orElseThrow();
+                .orElseThrow());
     }
 }
diff --git a/modules/compute/src/main/java/org/apache/ignite/internal/compute/queue/QueueExecutionImpl.java b/modules/compute/src/main/java/org/apache/ignite/internal/compute/queue/QueueExecutionImpl.java
index 16612b2da5..dcca3fb7e2 100644
--- a/modules/compute/src/main/java/org/apache/ignite/internal/compute/queue/QueueExecutionImpl.java
+++ b/modules/compute/src/main/java/org/apache/ignite/internal/compute/queue/QueueExecutionImpl.java
@@ -132,7 +132,7 @@ class QueueExecutionImpl<R> implements QueueExecution<R> {
                 run();
                 return true;
             }
-            LOG.info("Cannot change job priority, job already processing. [job id = {}]", job);
+            LOG.info("Cannot change job priority, job already processing. [job id = {}]", jobId);
         } finally {
             executionLock.unlock();
         }
diff --git a/modules/compute/src/main/java/org/apache/ignite/internal/compute/task/TaskExecutionInternal.java b/modules/compute/src/main/java/org/apache/ignite/internal/compute/task/TaskExecutionInternal.java
index 85a85a6785..4831a32966 100644
--- a/modules/compute/src/main/java/org/apache/ignite/internal/compute/task/TaskExecutionInternal.java
+++ b/modules/compute/src/main/java/org/apache/ignite/internal/compute/task/TaskExecutionInternal.java
@@ -56,9 +56,9 @@ import org.apache.ignite.internal.logger.Loggers;
 import org.jetbrains.annotations.Nullable;
 
 /**
- * Internal map reduce task execution object. Runs the {@link MapReduceTask#split(TaskExecutionContext, Object...)} method of the task as a
- * compute job, then submits the resulting list of jobs. Waits for completion of all compute jobs, then submits the
- * {@link MapReduceTask#reduce(TaskExecutionContext, Map)} method as a compute job. The result of the task is the result of the split
+ * Internal map reduce task execution object. Runs the {@link MapReduceTask#splitAsync(TaskExecutionContext, Object...)} method of the task
+ * as a compute job, then submits the resulting list of jobs. Waits for completion of all compute jobs, then submits the
+ * {@link MapReduceTask#reduceAsync(TaskExecutionContext, Map)} method as a compute job. The result of the task is the result of the split
  * method.
  *
  * @param <R> Task result type.
@@ -103,7 +103,8 @@ public class TaskExecutionInternal<R> implements JobExecution<R> {
                 () -> {
                     MapReduceTask<R> task = instantiateTask(taskClass);
 
-                    return completedFuture(new SplitResult<>(task, task.split(context, args)));
+                    return task.splitAsync(context, args)
+                            .thenApply(jobs -> new SplitResult<>(task, jobs));
                 },
                 Integer.MAX_VALUE,
                 0
@@ -124,7 +125,7 @@ public class TaskExecutionInternal<R> implements JobExecution<R> {
             MapReduceTask<R> task = splitExecution.resultAsync().thenApply(SplitResult::task).join();
 
             return executorService.submit(
-                    () -> completedFuture(task.reduce(context, results)),
+                    () -> task.reduceAsync(context, results),
                     Integer.MAX_VALUE,
                     0
             );
diff --git a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/runner/app/client/ItThinClientComputeTest.java b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/runner/app/client/ItThinClientComputeTest.java
index 9f47e4bda8..262b82bcca 100644
--- a/modules/runner/src/integrationTest/java/org/apache/ignite/internal/runner/app/client/ItThinClientComputeTest.java
+++ b/modules/runner/src/integrationTest/java/org/apache/ignite/internal/runner/app/client/ItThinClientComputeTest.java
@@ -834,71 +834,71 @@ public class ItThinClientComputeTest extends ItAbstractThinClientTest {
 
     private static class MapReduceNodeNameTask implements MapReduceTask<String> {
         @Override
-        public List<MapReduceJob> split(TaskExecutionContext context, Object... args) {
-            return context.ignite().clusterNodes().stream()
+        public CompletableFuture<List<MapReduceJob>> splitAsync(TaskExecutionContext context, Object... args) {
+            return completedFuture(context.ignite().clusterNodes().stream()
                     .map(node -> MapReduceJob.builder()
                             .jobDescriptor(JobDescriptor.builder(NodeNameJob.class).build())
                             .nodes(Set.of(node))
                             .args(args)
                             .build())
-                    .collect(Collectors.toList());
+                    .collect(Collectors.toList()));
         }
 
         @Override
-        public String reduce(TaskExecutionContext context, Map<UUID, ?> results) {
-            return results.values().stream()
+        public CompletableFuture<String> reduceAsync(TaskExecutionContext context, Map<UUID, ?> results) {
+            return completedFuture(results.values().stream()
                     .map(String.class::cast)
-                    .collect(Collectors.joining(","));
+                    .collect(Collectors.joining(",")));
         }
     }
 
     private static class MapReduceArgsTask implements MapReduceTask<String> {
         @Override
-        public List<MapReduceJob> split(TaskExecutionContext context, Object... args) {
-            return context.ignite().clusterNodes().stream()
+        public CompletableFuture<List<MapReduceJob>> splitAsync(TaskExecutionContext context, Object... args) {
+            return completedFuture(context.ignite().clusterNodes().stream()
                     .map(node -> MapReduceJob.builder()
                             .jobDescriptor(JobDescriptor.builder(ConcatJob.class).build())
                             .nodes(Set.of(node))
                             .args(args)
                             .build())
-                    .collect(Collectors.toList());
+                    .collect(Collectors.toList()));
         }
 
         @Override
-        public String reduce(TaskExecutionContext context, Map<UUID, ?> results) {
-            return results.values().stream()
+        public CompletableFuture<String> reduceAsync(TaskExecutionContext context, Map<UUID, ?> results) {
+            return completedFuture(results.values().stream()
                     .map(String.class::cast)
-                    .collect(Collectors.joining(","));
+                    .collect(Collectors.joining(",")));
         }
     }
 
     private static class MapReduceExceptionOnSplitTask implements MapReduceTask<String> {
         @Override
-        public List<MapReduceJob> split(TaskExecutionContext context, Object... args) {
+        public CompletableFuture<List<MapReduceJob>> splitAsync(TaskExecutionContext context, Object... args) {
             throw new CustomException(TRACE_ID, COLUMN_ALREADY_EXISTS_ERR, "Custom job error", null);
         }
 
         @Override
-        public String reduce(TaskExecutionContext context, Map<UUID, ?> results) {
-            return "expected split exception";
+        public CompletableFuture<String> reduceAsync(TaskExecutionContext context, Map<UUID, ?> results) {
+            return completedFuture("expected split exception");
         }
     }
 
     private static class MapReduceExceptionOnReduceTask implements MapReduceTask<String> {
 
         @Override
-        public List<MapReduceJob> split(TaskExecutionContext context, Object... args) {
-            return context.ignite().clusterNodes().stream()
+        public CompletableFuture<List<MapReduceJob>> splitAsync(TaskExecutionContext context, Object... args) {
+            return completedFuture(context.ignite().clusterNodes().stream()
                     .map(node -> MapReduceJob.builder()
                             .jobDescriptor(JobDescriptor.builder(NodeNameJob.class).build())
                             .nodes(Set.of(node))
                             .args(args)
                             .build())
-                    .collect(Collectors.toList());
+                    .collect(Collectors.toList()));
         }
 
         @Override
-        public String reduce(TaskExecutionContext context, Map<UUID, ?> results) {
+        public CompletableFuture<String> reduceAsync(TaskExecutionContext context, Map<UUID, ?> results) {
             throw new CustomException(TRACE_ID, COLUMN_ALREADY_EXISTS_ERR, "Custom job error", null);
         }
     }