From: Michal Rehak Date: Mon, 5 May 2014 15:05:20 +0000 (+0200) Subject: fix for BUG-947 - ErrorHandler classLoader issue X-Git-Tag: release/helium~191 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=c78886b152178e17e225b0ab61f9913b4180f540;p=openflowplugin.git fix for BUG-947 - ErrorHandler classLoader issue - exception must be logged in the same thread as there is thread classLoader context, which might differ from the previously dedicated thread for exception logging Change-Id: Ia2f911fdd5845280575fcb6a57c16aafdc0c2b53 Signed-off-by: Michal Rehak --- diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ErrorHandler.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ErrorHandler.java index bce5f2e826..d31372a9ef 100644 --- a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ErrorHandler.java +++ b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ErrorHandler.java @@ -13,7 +13,7 @@ import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext; * @author mirehak * */ -public interface ErrorHandler extends Runnable, AutoCloseable { +public interface ErrorHandler { /** * @param e cause diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ErrorHandlerQueueImpl.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ErrorHandlerQueueImpl.java deleted file mode 100644 index 42a8c67b50..0000000000 --- a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ErrorHandlerQueueImpl.java +++ /dev/null @@ -1,80 +0,0 @@ -/** - * Copyright (c) 2013 Cisco Systems, 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.openflowplugin.openflow.md.core; - -import java.util.Arrays; -import java.util.concurrent.LinkedBlockingQueue; - -import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * dumping all exceptions to log - * @author mirehak - */ -public class ErrorHandlerQueueImpl implements ErrorHandler { - - private static final Logger LOG = LoggerFactory - .getLogger(ErrorHandlerQueueImpl.class); - - private LinkedBlockingQueue errorQueue; - - /** - * default ctor - */ - public ErrorHandlerQueueImpl() { - this.errorQueue = new LinkedBlockingQueue<>(); - } - - @Override - public void run() { - while (true) { - Exception error; - try { - error = errorQueue.take(); - if (error instanceof QueueShutdownItem) { - break; - } - Throwable cause = error.getCause(); - LOG.error(error.getMessage()+" -> "+cause.getMessage(), cause); - } catch (InterruptedException e) { - LOG.warn(e.getMessage()); - } - } - } - - @Override - public void handleException(Throwable e, SessionContext sessionContext) { - String sessionKeyId = null; - if (sessionContext != null) { - sessionKeyId = Arrays.toString(sessionContext.getSessionKey().getId()); - } - - Exception causeAndThread = new Exception( - "IN THREAD: "+Thread.currentThread().getName() + - "; session:"+sessionKeyId, e); - try { - errorQueue.put(causeAndThread); - } catch (InterruptedException e1) { - LOG.error(e1.getMessage(), e1); - } - } - - @Override - public void close() { - // add special exception to queue and recognize it in run method - errorQueue.add(new QueueShutdownItem()); - } - - static class QueueShutdownItem extends Exception { - private static final long serialVersionUID = 1L; - // nothing - } -} diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ErrorHandlerSimpleImpl.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ErrorHandlerSimpleImpl.java new file mode 100644 index 0000000000..f9156eb4ac --- /dev/null +++ b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/ErrorHandlerSimpleImpl.java @@ -0,0 +1,35 @@ +/** + * Copyright (c) 2013 Cisco Systems, 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.openflowplugin.openflow.md.core; + +import java.util.Arrays; + +import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * dumping all exceptions to log + * @author mirehak + */ +public class ErrorHandlerSimpleImpl implements ErrorHandler { + + private static final Logger LOG = LoggerFactory + .getLogger(ErrorHandlerSimpleImpl.class); + + @Override + public void handleException(Throwable e, SessionContext sessionContext) { + String sessionKeyId = null; + if (sessionContext != null) { + sessionKeyId = Arrays.toString(sessionContext.getSessionKey().getId()); + } + + LOG.error("exception -> {}, session -> {}", e.getMessage(), sessionKeyId, e); + } +} diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/MDController.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/MDController.java index acd3814e7f..5e0a6a923b 100644 --- a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/MDController.java +++ b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/MDController.java @@ -107,7 +107,7 @@ public class MDController implements IMDController, AutoCloseable { final private int OF10 = OFConstants.OFP_VERSION_1_0; final private int OF13 = OFConstants.OFP_VERSION_1_3; - private ErrorHandlerQueueImpl errorHandler; + private ErrorHandlerSimpleImpl errorHandler; private ExecutorService rpcPool; @@ -226,8 +226,7 @@ public class MDController implements IMDController, AutoCloseable { SwitchConnectionHandlerImpl switchConnectionHandler = new SwitchConnectionHandlerImpl(); switchConnectionHandler.setMessageSpy(messageSpyCounter); - errorHandler = new ErrorHandlerQueueImpl(); - new Thread(errorHandler).start(); + errorHandler = new ErrorHandlerSimpleImpl(); switchConnectionHandler.setErrorHandler(errorHandler); switchConnectionHandler.init(); @@ -351,6 +350,6 @@ public class MDController implements IMDController, AutoCloseable { } switchConnectionProviders = null; OFSessionUtil.releaseSessionManager(); - errorHandler.close(); + errorHandler = null; } } diff --git a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/ConnectionConductorImplTest.java b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/ConnectionConductorImplTest.java index d06cbc0b59..73d5740a26 100644 --- a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/ConnectionConductorImplTest.java +++ b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/ConnectionConductorImplTest.java @@ -50,13 +50,11 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessageBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessageBuilder; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessageBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.group.features._case.MultipartReplyGroupFeaturesBuilder; import org.opendaylight.yangtools.yang.binding.DataObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -92,7 +90,7 @@ public class ConnectionConductorImplTest { private int errorMessageCounter; @Mock - private ErrorHandlerQueueImpl errorHandler; + private ErrorHandlerSimpleImpl errorHandler; private int expectedErrors = 0; @@ -139,7 +137,6 @@ public class ConnectionConductorImplTest { connectionConductor = new ConnectionConductorImpl(adapter); connectionConductor.setQueueKeeper(queueKeeper); connectionConductor.init(); - pool.execute(errorHandler); connectionConductor.setErrorHandler(errorHandler); controller = new MDController(); controller.init();