From 9d71b491bb9ad8b14f6a3bea7269f879812373dd Mon Sep 17 00:00:00 2001 From: Asad Ahmed Date: Wed, 5 Feb 2014 15:44:31 -0800 Subject: [PATCH] The Switch Event thread is the thread that processes events coming from all switches. Right now its very fragile as an unhandled exception can cause the thread to die causing the controller to be unusable. Making the thread more tolerant of exceptions and making the shutdown of the thread more deterministic. Change-Id: I31c31f012229de8e124c0c13fa651513daaf6776 Signed-off-by: Asad Ahmed --- .../openflow/core/internal/Controller.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/Controller.java b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/Controller.java index 72ee7a679b..63dd0bc29a 100644 --- a/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/Controller.java +++ b/opendaylight/protocol_plugins/openflow/src/main/java/org/opendaylight/controller/protocol_plugin/openflow/core/internal/Controller.java @@ -48,6 +48,7 @@ public class Controller implements IController, CommandProvider, IPluginInConnec .getLogger(Controller.class); private ControllerIO controllerIO; private Thread switchEventThread; + private volatile boolean shutdownSwitchEventThread;// default to false private ConcurrentHashMap switches; private PriorityBlockingQueue switchEvents; // only 1 message listener per OFType @@ -69,6 +70,12 @@ public class Controller implements IController, CommandProvider, IPluginInConnec while (true) { try { + if(shutdownSwitchEventThread) { + // break out of the infinite loop + // if you are shutting down + logger.info("Switch Event Thread is shutting down"); + break; + } SwitchEvent ev = switchEvents.take(); SwitchEvent.SwitchEventType eType = ev.getEventType(); ISwitch sw = ev.getSwitch(); @@ -104,12 +111,14 @@ public class Controller implements IController, CommandProvider, IPluginInConnec logger.error("Unknown switch event {}", eType.ordinal()); } } catch (InterruptedException e) { - switchEvents.clear(); - return; + // nothing to do except retry + } catch (Exception e) { + // log the exception and retry + logger.warn("Exception in Switch Event Thread is {}" ,e); } } + switchEvents.clear(); } - } /** @@ -167,6 +176,7 @@ public class Controller implements IController, CommandProvider, IPluginInConnec ((SwitchHandler) entry.getValue()).stop(); it.remove(); } + shutdownSwitchEventThread = true; switchEventThread.interrupt(); try { controllerIO.shutDown(); -- 2.36.6