You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by lq...@apache.org on 2015/10/23 17:59:33 UTC

svn commit: r1710254 - in /qpid/java/trunk/broker-core/src: main/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecoverer.java test/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecovererTest.java

Author: lquack
Date: Fri Oct 23 15:59:32 2015
New Revision: 1710254

URL: http://svn.apache.org/viewvc?rev=1710254&view=rev
Log:
QPID-6808: [Java Broker] Shut down the broker when an uncaught exception occurs during async recovery

Added:
    qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecovererTest.java
Modified:
    qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecoverer.java

Modified: qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecoverer.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecoverer.java?rev=1710254&r1=1710253&r2=1710254&view=diff
==============================================================================
--- qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecoverer.java (original)
+++ qpid/java/trunk/broker-core/src/main/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecoverer.java Fri Oct 23 15:59:32 2015
@@ -81,6 +81,8 @@ public class AsynchronousMessageStoreRec
 
     private static class AsynchronousRecoverer
     {
+        private static final Logger LOGGER = LoggerFactory.getLogger(AsynchronousRecoverer.class);
+
         public static final int THREAD_POOL_SHUTDOWN_TIMEOUT = 5000;
         private final VirtualHostImpl<?, ?, ?> _virtualHost;
         private final EventLogger _eventLogger;
@@ -420,11 +422,31 @@ public class AsynchronousMessageStoreRec
                 {
                     recoverQueue(_queue);
                 }
+                catch (Throwable e)
+                {
+                    handleUncaughtException(e);
+                }
                 finally
                 {
                     Thread.currentThread().setName(originalThreadName);
                 }
             }
+
+            private void handleUncaughtException(Throwable e)
+            {
+                LOGGER.error("Unexpected exception", e);
+                Thread.UncaughtExceptionHandler uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
+                if (uncaughtExceptionHandler != null)
+                {
+                    uncaughtExceptionHandler.uncaughtException(Thread.currentThread(), e);
+                }
+                else
+                {
+                    // it should never happen as we set default UncaughtExceptionHandler in main
+                    e.printStackTrace();
+                    Runtime.getRuntime().halt(1);
+                }
+            }
         }
 
         private class MessageInstanceVisitor implements MessageInstanceHandler

Added: qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecovererTest.java
URL: http://svn.apache.org/viewvc/qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecovererTest.java?rev=1710254&view=auto
==============================================================================
--- qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecovererTest.java (added)
+++ qpid/java/trunk/broker-core/src/test/java/org/apache/qpid/server/virtualhost/AsynchronousMessageStoreRecovererTest.java Fri Oct 23 15:59:32 2015
@@ -0,0 +1,80 @@
+/*
+ *
+ * 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.qpid.server.virtualhost;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.util.Collections;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.qpid.server.logging.EventLogger;
+import org.apache.qpid.server.queue.AMQQueue;
+import org.apache.qpid.server.store.MessageStore;
+import org.apache.qpid.server.store.TransactionLogResource;
+import org.apache.qpid.server.store.handler.MessageInstanceHandler;
+import org.apache.qpid.server.util.ServerScopedRuntimeException;
+import org.apache.qpid.test.utils.QpidTestCase;
+
+public class AsynchronousMessageStoreRecovererTest extends QpidTestCase
+{
+    private VirtualHostImpl _virtualHost;
+    private MessageStore _store;
+    private MessageStore.MessageStoreReader _storeReader;
+
+    @Override
+    protected void setUp() throws Exception
+    {
+        super.setUp();
+
+        _virtualHost = mock(VirtualHostImpl.class);
+        _store = mock(MessageStore.class);
+        _storeReader = mock(MessageStore.MessageStoreReader.class);
+
+        when(_virtualHost.getEventLogger()).thenReturn(new EventLogger());
+        when(_virtualHost.getMessageStore()).thenReturn(_store);
+        when(_store.newMessageStoreReader()).thenReturn(_storeReader);
+    }
+
+    public void testExceptionDuringRecoveryShutsDownBroker() throws Exception
+    {
+        final CountDownLatch uncaughtExceptionHandlerCalledLatch = new CountDownLatch(1);
+        Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()
+        {
+            @Override
+            public void uncaughtException(final Thread t, final Throwable e)
+            {
+                uncaughtExceptionHandlerCalledLatch.countDown();
+            }
+        });
+        doThrow(ServerScopedRuntimeException.class).when(_storeReader).visitMessageInstances(any(TransactionLogResource.class),
+                                                                                             any(MessageInstanceHandler.class));
+        AMQQueue queue = mock(AMQQueue.class);
+        when(_virtualHost.getQueues()).thenReturn(Collections.singleton(queue));
+
+        AsynchronousMessageStoreRecoverer recoverer = new AsynchronousMessageStoreRecoverer();
+        recoverer.recover(_virtualHost);
+        assertTrue("UncaughtExceptionHandler was not called", uncaughtExceptionHandlerCalledLatch.await(1000, TimeUnit.MILLISECONDS));
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org