You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by da...@apache.org on 2016/02/17 16:43:03 UTC

svn commit: r1730864 - in /jackrabbit/oak/trunk: oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorUtils.java oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AtomicCounterClusterIT.java

Author: davide
Date: Wed Feb 17 15:43:03 2016
New Revision: 1730864

URL: http://svn.apache.org/viewvc?rev=1730864&view=rev
Log:
OAK-4023 - AtomicCounterClusterIT does not shut down scheduled executors

better handling of the executor shutdown

Added:
    jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorUtils.java
Modified:
    jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AtomicCounterClusterIT.java

Added: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorUtils.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorUtils.java?rev=1730864&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorUtils.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/concurrent/ExecutorUtils.java Wed Feb 17 15:43:03 2016
@@ -0,0 +1,70 @@
+/*
+ * 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.jackrabbit.oak.commons.concurrent;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import javax.annotation.Nonnull;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ExecutorUtils {
+    private final static Logger LOG = LoggerFactory.getLogger(ExecutorUtils.class);
+    
+    /**
+     * same as {@link #shutdown(ExecutorService, TimeUnit, long)} waiting for 10 seconds.
+     * 
+     * @param e
+     */
+    public static void shutdownIn10s(@Nonnull ExecutorService e) {
+        shutdown(e, TimeUnit.SECONDS, 10);
+    }
+    
+    /**
+     * Convenience method for gracefully shutdown an {@link ExecutorService}.
+     * 
+     * @param executor The executor to be shut down. Cannot be null.
+     * @param unit The time unit for the timeout. Cannot be null.
+     * @param timeout how long to wait for. Cannot be negative
+     */
+    public static void shutdown(@Nonnull ExecutorService executor, 
+                                @Nonnull TimeUnit unit, 
+                                long timeout) {
+        checkNotNull(executor);
+        checkNotNull(unit);
+        checkArgument(timeout >= 0, "timeout cannot be negative");
+        
+        try {
+            executor.shutdown();
+            executor.awaitTermination(timeout, unit);
+        } catch (InterruptedException e) {
+            LOG.error("Error while shutting down the ExecutorService", e);
+            Thread.currentThread().interrupt();
+        } finally {
+            if (!executor.isShutdown()) {
+                LOG.warn("ExecutorService `{}` didn't shutdown property. Will be forced now.",
+                    executor);
+            }
+            executor.shutdownNow();
+        }
+    }
+}

Modified: jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AtomicCounterClusterIT.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AtomicCounterClusterIT.java?rev=1730864&r1=1730863&r2=1730864&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AtomicCounterClusterIT.java (original)
+++ jackrabbit/oak/trunk/oak-jcr/src/test/java/org/apache/jackrabbit/oak/jcr/AtomicCounterClusterIT.java Wed Feb 17 15:43:03 2016
@@ -45,6 +45,7 @@ import javax.jcr.Session;
 
 import org.apache.jackrabbit.oak.commons.FixturesHelper;
 import org.apache.jackrabbit.oak.commons.FixturesHelper.Fixture;
+import org.apache.jackrabbit.oak.commons.concurrent.ExecutorUtils;
 import org.apache.jackrabbit.oak.plugins.atomic.AtomicCounterEditor;
 import org.apache.jackrabbit.oak.spi.state.NodeStore;
 import org.apache.jackrabbit.oak.util.PerfLogger;
@@ -80,8 +81,7 @@ public class AtomicCounterClusterIT  ext
     public void after() throws Exception {
         super.after();
         for (CustomScheduledExecutor exec : executors) {
-            exec.shutdown();
-            exec.awaitTermination(10, TimeUnit.SECONDS);
+            ExecutorUtils.shutdownIn10s(exec);
         }
     }