https://issues.apache.org/jira/browse/AMQ-6799 - IOExceptionHandler during the startup

This commit is contained in:
Dejan Bosanac 2017-08-24 14:04:28 +02:00
parent 4c986d102c
commit c7291f1ecf
5 changed files with 55 additions and 3 deletions

View File

@ -51,7 +51,7 @@ import org.slf4j.LoggerFactory;
@Override
public void handle(IOException exception) {
if (ignoreAllErrors) {
if (!broker.isStarted() || ignoreAllErrors) {
allowIOResumption();
LOG.info("Ignoring IO exception, " + exception, exception);
return;
@ -167,7 +167,9 @@ import org.slf4j.LoggerFactory;
protected void allowIOResumption() {
try {
broker.getPersistenceAdapter().allowIOResumption();
if (broker.getPersistenceAdapter() != null) {
broker.getPersistenceAdapter().allowIOResumption();
}
} catch (IOException e) {
LOG.warn("Failed to allow IO resumption", e);
}

View File

@ -48,6 +48,11 @@ public class DefaultIOExceptionHandlerTest {
underTest.setSystemExitOnShutdown(exitPlease);
underTest.setBrokerService(new BrokerService() {
@Override
public boolean isStarted() {
return true;
}
@Override
public void stop() throws Exception {
shutdownOnExitSet.set(isSystemExitOnShutdown());

View File

@ -91,7 +91,7 @@ public class LeaseDatabaseLocker extends AbstractJDBCLocker {
+ e, e);
}
if (handleStartException) {
lockable.getBrokerService().handleIOException(IOExceptionSupport.create(e));
throw e;
}
} finally {
close(statement);

View File

@ -65,6 +65,8 @@ public class JDBCIOExceptionHandlerMockeryTest {
// simulate jdbc up between hasLock and checkpoint, so hasLock fails to verify
context.checking(new Expectations() {{
allowing(brokerService).isStarted();
will(returnValue(true));
allowing(brokerService).isRestartAllowed();
will(returnValue(false));
allowing(brokerService).setSystemExitOnShutdown(with(false));

View File

@ -28,6 +28,7 @@ import javax.jms.Connection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;
import org.apache.activemq.broker.ft.SyncCreateDataSource;
import org.apache.activemq.bugs.embedded.ThreadExplorer;
import org.apache.activemq.util.IOHelper;
import org.apache.activemq.util.LeaseLockerIOExceptionHandler;
import org.apache.activemq.util.Wait;
@ -101,6 +102,47 @@ public class JDBCIOExceptionHandlerTest {
return broker;
}
@Test
public void testStartWithDatabaseDown() throws Exception {
BrokerService broker = new BrokerService();
JDBCPersistenceAdapter jdbc = new JDBCPersistenceAdapter();
EmbeddedDataSource embeddedDataSource = (EmbeddedDataSource) jdbc.getDataSource();
// create a wrapper to EmbeddedDataSource to allow the connection be
// reestablished to derby db
dataSource = new ReconnectingEmbeddedDataSource(new SyncCreateDataSource(embeddedDataSource));
dataSource.stopDB();
jdbc.setDataSource(dataSource);
jdbc.setLockKeepAlivePeriod(1000l);
LeaseDatabaseLocker leaseDatabaseLocker = new LeaseDatabaseLocker();
leaseDatabaseLocker.setHandleStartException(true);
leaseDatabaseLocker.setLockAcquireSleepInterval(2000l);
jdbc.setLocker(leaseDatabaseLocker);
broker.setPersistenceAdapter(jdbc);
LeaseLockerIOExceptionHandler ioExceptionHandler = new LeaseLockerIOExceptionHandler();
ioExceptionHandler.setResumeCheckSleepPeriod(1000l);
ioExceptionHandler.setStopStartConnectors(true);
broker.setIoExceptionHandler(ioExceptionHandler);
try {
broker.start();
fail("Broker should have been stopped!");
} catch (Exception e) {
Thread.sleep(5000);
assertTrue("Broker should have been stopped!", broker.isStopped());
Thread[] threads = ThreadExplorer.listThreads();
for (int i = 0; i < threads.length; i++) {
if (threads[i].getName().startsWith("IOExceptionHandler")) {
fail("IOExceptionHanlder still active");
}
}
} finally {
dataSource = null;
broker = null;
}
}
/*
* run test without JMX enabled
*/
@ -223,6 +265,7 @@ public class JDBCIOExceptionHandlerTest {
} finally {
LOG.debug("*** broker is stopping...");
broker.stop();
broker.waitUntilStopped();
}
}