Use Executors.newSingleThreadExecutor in AsyncDataTreeChangeListenerBase 87/63787/4
authorMichael Vorburger <vorburger@redhat.com>
Thu, 28 Sep 2017 16:25:48 +0000 (18:25 +0200)
committerDavid Suarez <david.suarez.fuentes@gmail.com>
Fri, 6 Oct 2017 05:54:41 +0000 (05:54 +0000)
as well as the same in AsyncClusteredDataTreeChangeListenerBase

instead of their current use of ThreadPoolExecutor, which [AFAIK...] is
pointless here now, since we switched it (a long time ago) to always be
of fixed size 1 only, for some reason?

This is JUST a clean-up, to make this class more easy to understand.
This should (AFAIK) NOT change anything in its behaviour, so safe.

This is also to align it with e.g.
https://git.opendaylight.org/gerrit/#/c/63505/ (and perhaps more changed
like that to come) - seeing the exact same ExecutorService used in both
IMHO makes it clearer how similar this old and that new async DTCL are.

NB: This is Executors from infrautils not java.util.concurrent, but
that's just a wrapper that deals with the setUncaughtExceptionHandler
etc.

Change-Id: If424fd7ec7cd41fe2d35a68ec1eee064e7742214
Signed-off-by: Michael Vorburger <vorburger@redhat.com>
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/AsyncClusteredDataTreeChangeListenerBase.java
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/datastoreutils/AsyncDataTreeChangeListenerBase.java
mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/infra/LoggingRejectedExecutionHandler.java [deleted file]

index 67df09b6455aa68517f3a444de8e0d8739359df1..059e26c5c52081cd30e53a3831626f687887ede8 100644 (file)
@@ -10,9 +10,7 @@ package org.opendaylight.genius.datastoreutils;
 
 import com.google.common.base.Preconditions;
 import java.util.Collection;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.ExecutorService;
 import javax.annotation.PreDestroy;
 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -21,9 +19,8 @@ import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.infra.LoggingRejectedExecutionHandler;
 import org.opendaylight.genius.utils.SuperTypeUtil;
-import org.opendaylight.infrautils.utils.concurrent.ThreadFactoryProvider;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -37,24 +34,11 @@ public abstract class AsyncClusteredDataTreeChangeListenerBase
 
     private static final Logger LOG = LoggerFactory.getLogger(AsyncClusteredDataTreeChangeListenerBase.class);
 
-    private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_CORE_SIZE = 1;
-    private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_MAX_SIZE = 1;
-    private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_KEEP_ALIVE_TIME_SECS = 300;
-
     private ListenerRegistration<K> listenerRegistration;
     private final ChainableDataTreeChangeListenerImpl<T> chainingDelegate = new ChainableDataTreeChangeListenerImpl<>();
 
-    private final ThreadPoolExecutor dataTreeChangeHandlerExecutor = new ThreadPoolExecutor(
-            DATATREE_CHANGE_HANDLER_THREAD_POOL_CORE_SIZE,
-            DATATREE_CHANGE_HANDLER_THREAD_POOL_MAX_SIZE,
-            DATATREE_CHANGE_HANDLER_THREAD_POOL_KEEP_ALIVE_TIME_SECS,
-            TimeUnit.SECONDS,
-            new LinkedBlockingQueue<>(),
-            ThreadFactoryProvider.builder()
-                .namePrefix("AsyncClusteredDataTreeChangeListenerBase-DataTreeChangeHandler")
-                .logger(LOG)
-                .build().get(),
-            new LoggingRejectedExecutionHandler());
+    private final ExecutorService dataTreeChangeHandlerExecutor =
+            Executors.newSingleThreadExecutor("AsyncClusteredDataTreeChangeListenerBase-DataTreeChangeHandler", LOG);
 
     protected final Class<T> clazz;
 
index 7c011af0507e98b8b72147dad8964609fb79134b..c9181c55791240b94d22bb829a60da8b5506494e 100644 (file)
@@ -10,9 +10,7 @@ package org.opendaylight.genius.datastoreutils;
 
 import com.google.common.base.Preconditions;
 import java.util.Collection;
-import java.util.concurrent.LinkedBlockingQueue;
-import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.TimeUnit;
+import java.util.concurrent.ExecutorService;
 import javax.annotation.PostConstruct;
 import javax.annotation.PreDestroy;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
@@ -21,9 +19,8 @@ import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.genius.infra.LoggingRejectedExecutionHandler;
 import org.opendaylight.genius.utils.SuperTypeUtil;
-import org.opendaylight.infrautils.utils.concurrent.ThreadFactoryProvider;
+import org.opendaylight.infrautils.utils.concurrent.Executors;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
@@ -36,25 +33,11 @@ public abstract class AsyncDataTreeChangeListenerBase<T extends DataObject, K ex
 
     private static final Logger LOG = LoggerFactory.getLogger(AsyncDataTreeChangeListenerBase.class);
 
-    private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_CORE_SIZE = 1;
-    private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_MAX_SIZE = 1;
-    private static final int DATATREE_CHANGE_HANDLER_THREAD_POOL_KEEP_ALIVE_TIME_SECS = 300;
-
     private ListenerRegistration<K> listenerRegistration;
     private final ChainableDataTreeChangeListenerImpl<T> chainingDelegate = new ChainableDataTreeChangeListenerImpl<>();
 
-    private final ThreadPoolExecutor dataTreeChangeHandlerExecutor = new ThreadPoolExecutor(
-            DATATREE_CHANGE_HANDLER_THREAD_POOL_CORE_SIZE,
-            DATATREE_CHANGE_HANDLER_THREAD_POOL_MAX_SIZE,
-            DATATREE_CHANGE_HANDLER_THREAD_POOL_KEEP_ALIVE_TIME_SECS,
-            TimeUnit.SECONDS,
-            new LinkedBlockingQueue<>(),
-            ThreadFactoryProvider.builder()
-                .namePrefix("AsyncDataTreeChangeListenerBase-DataTreeChangeHandler")
-                .logger(LOG)
-                .build().get(),
-            new LoggingRejectedExecutionHandler());
-
+    private final ExecutorService dataTreeChangeHandlerExecutor =
+            Executors.newSingleThreadExecutor("AsyncDataTreeChangeListenerBase-DataTreeChangeHandler", LOG);
     protected final Class<T> clazz;
 
     protected AsyncDataTreeChangeListenerBase() {
diff --git a/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/infra/LoggingRejectedExecutionHandler.java b/mdsalutil/mdsalutil-api/src/main/java/org/opendaylight/genius/infra/LoggingRejectedExecutionHandler.java
deleted file mode 100644 (file)
index 9589ace..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.genius.infra;
-
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.RejectedExecutionHandler;
-import java.util.concurrent.ThreadPoolExecutor;
-import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
-import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * A {@link RejectedExecutionHandler} which logs
- * instead of throwing an exception (like e.g. {@link ThreadPoolExecutor}'s default
- * {@link AbortPolicy} does) or just completely silently ignores the execute
- * (like e.g. {@link DiscardPolicy} does).
- *
- * <p>This logs an ERROR level message (because typically that's a real problem),
- * unless the {@link ExecutorService#isShutdown()} - then it logs only an INFO level message
- * (because typically that's "just" a shutdown ordering issue, and not a real problem).
- *
- * @author Michael Vorburger.ch
- */
-public class LoggingRejectedExecutionHandler implements RejectedExecutionHandler {
-
-    // TODO This utility could eventually be moved to org.opendaylight.infrautils.utils.concurrent
-
-    private static final Logger LOG = LoggerFactory.getLogger(LoggingRejectedExecutionHandler.class);
-
-    @Override
-    public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
-        if (executor.isShutdown() || executor.isTerminating() || executor.isTerminated()) {
-            LOG.info("rejectedExecution, but OK as ExecutorService is terminating or shutdown; "
-                    + "executor: {}, runnable: {}", executor, runnable);
-        } else {
-            LOG.error("rejectedExecution (BUT ExecutorService is NOT terminating or shutdown, so that's a PROBLEM); "
-                    + "executor: {}, runnable: {}", executor, runnable);
-        }
-    }
-
-}