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 2021/02/23 08:31:59 UTC

[GitHub] [ignite] SammyVimes commented on a change in pull request #8820: IGNITE-2399: Implement acquireAndExecute Utility Method

SammyVimes commented on a change in pull request #8820:
URL: https://github.com/apache/ignite/pull/8820#discussion_r580404668



##########
File path: modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
##########
@@ -12051,4 +12055,25 @@ public static long uncompressedSize(File zip) throws IOException {
             return size;
         }
     }
+
+    /**
+     * Acquires the given semaphore, executes the given callable and schedules the release of permits asynchronously

Review comment:
       Missing parameters in javadocs

##########
File path: modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java
##########
@@ -1455,6 +1470,51 @@ public void testUncompressedSize() throws Exception {
         }
     }
 
+    /**
+     * Test to verify the {@link U#acquireAndExecute(IgniteSemaphore, IgniteCallable, int)}.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testAcquireAndExecute() throws Exception {
+        IgniteSemaphore semaphore = ignite(0).semaphore("testAcquireAndExecute", 1, true, true);
+        ExecutorService executorService = Executors.newSingleThreadExecutor();
+
+        IgniteCallable<IgniteFuture<Integer>> callable = new IgniteCallable<IgniteFuture<Integer>>() {
+            @Override public IgniteFuture<Integer> call() {

Review comment:
       inheritDoc missing here

##########
File path: modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
##########
@@ -12051,4 +12055,25 @@ public static long uncompressedSize(File zip) throws IOException {
             return size;
         }
     }
+
+    /**
+     * Acquires the given semaphore, executes the given callable and schedules the release of permits asynchronously
+     */
+    public static <T> IgniteFuture<T> acquireAndExecute(IgniteSemaphore semaphore,
+                                          IgniteCallable<IgniteFuture<T>> callable, int numPermits) throws Exception {
+
+        semaphore.acquire(numPermits);
+
+        IgniteFuture<T> future = callable.call();
+
+        future.listen(new IgniteInClosure<IgniteFuture<T>>() {
+            @Override public void apply(IgniteFuture<T> IgniteFuture) {

Review comment:
       Please add /** {@inheritDoc} */

##########
File path: modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
##########
@@ -12051,4 +12055,25 @@ public static long uncompressedSize(File zip) throws IOException {
             return size;
         }
     }
+
+    /**
+     * Acquires the given semaphore, executes the given callable and schedules the release of permits asynchronously
+     */
+    public static <T> IgniteFuture<T> acquireAndExecute(IgniteSemaphore semaphore,
+                                          IgniteCallable<IgniteFuture<T>> callable, int numPermits) throws Exception {
+
+        semaphore.acquire(numPermits);
+
+        IgniteFuture<T> future = callable.call();
+
+        future.listen(new IgniteInClosure<IgniteFuture<T>>() {
+            @Override public void apply(IgniteFuture<T> IgniteFuture) {

Review comment:
       Also, parameter name IgniteFuture doesn't follow Apache Ignite codestyle, should be "igniteFuture"

##########
File path: modules/core/src/main/java/org/apache/ignite/internal/util/IgniteUtils.java
##########
@@ -12051,4 +12055,25 @@ public static long uncompressedSize(File zip) throws IOException {
             return size;
         }
     }
+
+    /**
+     * Acquires the given semaphore, executes the given callable and schedules the release of permits asynchronously
+     */
+    public static <T> IgniteFuture<T> acquireAndExecute(IgniteSemaphore semaphore,
+                                          IgniteCallable<IgniteFuture<T>> callable, int numPermits) throws Exception {
+
+        semaphore.acquire(numPermits);
+
+        IgniteFuture<T> future = callable.call();
+
+        future.listen(new IgniteInClosure<IgniteFuture<T>>() {
+            @Override public void apply(IgniteFuture<T> IgniteFuture) {
+                if (IgniteFuture.isCancelled() || IgniteFuture.isDone()) {

Review comment:
       Not sure if these checks needed here: future's listen callback only executed when future is completed

##########
File path: modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java
##########
@@ -1455,6 +1470,51 @@ public void testUncompressedSize() throws Exception {
         }
     }
 
+    /**
+     * Test to verify the {@link U#acquireAndExecute(IgniteSemaphore, IgniteCallable, int)}.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testAcquireAndExecute() throws Exception {
+        IgniteSemaphore semaphore = ignite(0).semaphore("testAcquireAndExecute", 1, true, true);
+        ExecutorService executorService = Executors.newSingleThreadExecutor();
+
+        IgniteCallable<IgniteFuture<Integer>> callable = new IgniteCallable<IgniteFuture<Integer>>() {
+            @Override public IgniteFuture<Integer> call() {
+                IgniteFutureImpl<Integer> igniteFuture = new IgniteFutureImpl<>(new GridFutureAdapter<>());
+
+                assert (semaphore.availablePermits() == 0);
+
+                Runnable runnable = new Runnable() {
+                    @Override public void run() {

Review comment:
       igniteDoc missing here

##########
File path: modules/core/src/test/java/org/apache/ignite/internal/util/IgniteUtilsSelfTest.java
##########
@@ -1455,6 +1470,51 @@ public void testUncompressedSize() throws Exception {
         }
     }
 
+    /**
+     * Test to verify the {@link U#acquireAndExecute(IgniteSemaphore, IgniteCallable, int)}.
+     *
+     * @throws Exception If failed.
+     */
+    @Test
+    public void testAcquireAndExecute() throws Exception {

Review comment:
       You can also add a negative scenario, e.g. what will happen if there is an error during callable's execution




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