You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ja...@apache.org on 2019/07/22 22:29:57 UTC

[geode] branch develop updated: GEODE-3718: shutdownNow should also interrupt non executing runnables (#3821)

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

jasonhuynh pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 336c1a2  GEODE-3718: shutdownNow should also interrupt non executing runnables (#3821)
336c1a2 is described below

commit 336c1a288641a0587fbbfa38a5818ca9382e25e4
Author: Jason Huynh <hu...@gmail.com>
AuthorDate: Mon Jul 22 15:29:43 2019 -0700

    GEODE-3718: shutdownNow should also interrupt non executing runnables (#3821)
---
 .../cache/control/InternalResourceManager.java     |  9 +++++++-
 .../cache/control/InternalResourceManagerTest.java | 24 ++++++++++++++++++++++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/control/InternalResourceManager.java b/geode-core/src/main/java/org/apache/geode/internal/cache/control/InternalResourceManager.java
index 2ac55cb..15bca3c 100755
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/control/InternalResourceManager.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/control/InternalResourceManager.java
@@ -17,10 +17,12 @@ package org.apache.geode.internal.cache.control;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Future;
 import java.util.concurrent.RejectedExecutionException;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
@@ -348,7 +350,12 @@ public class InternalResourceManager implements ResourceManager {
     if (!executor.isTerminated()) {
       logger.warn("Failed to stop resource manager threads in {} seconds, forcing shutdown",
           secToWait);
-      executor.shutdownNow();
+      List<Runnable> remainingTasks = executor.shutdownNow();
+      remainingTasks.forEach(runnable -> {
+        if (runnable instanceof Future) {
+          ((Future) runnable).cancel(true);
+        }
+      });
     }
   }
 
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/control/InternalResourceManagerTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/control/InternalResourceManagerTest.java
index eb7522f..1b8b5a5 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/cache/control/InternalResourceManagerTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/control/InternalResourceManagerTest.java
@@ -22,13 +22,18 @@ import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
+import java.util.concurrent.CancellationException;
 import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 import org.junit.After;
 import org.junit.Before;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.ExpectedException;
 
 import org.apache.geode.Statistics;
 import org.apache.geode.distributed.DistributedSystem;
@@ -40,6 +45,9 @@ public class InternalResourceManagerTest {
 
   private final CountDownLatch hangLatch = new CountDownLatch(1);
 
+  @Rule
+  public ExpectedException thrown = ExpectedException.none();
+
   @Before
   public void setUp() {
     InternalCache cache = mock(InternalCache.class);
@@ -69,4 +77,20 @@ public class InternalResourceManagerTest {
     await("Submitted task is done")
         .until(() -> submittedTask.isDone());
   }
+
+  @Test
+  public void nonExecutedRunnablesShouldBeInterruptedSoFutureGetDoesNotHang()
+      throws InterruptedException, ExecutionException {
+    ScheduledExecutorService executor = resourceManager.getExecutor();
+
+    Future<Boolean> submittedTask =
+        executor.schedule(() -> {
+          return true;
+        }, 1, TimeUnit.DAYS);
+
+    resourceManager.close();
+
+    thrown.expect(CancellationException.class);
+    submittedTask.get();
+  }
 }