ARTEMIS-4279 Shutdown critical analyzer thread if error on broker initialization

The broker process fails to exit if an error is encountered starting the NodeManager.  The issue is resolved by converting the critical analyzer thread to a daemon thread.  As added protection, the thread is manually stopped when this error is encountered.
This commit is contained in:
Stephen Higgs 2023-05-12 10:26:01 -04:00 committed by clebertsuconic
parent 1d4139f278
commit f1f017fb3b
4 changed files with 97 additions and 2 deletions

View File

@ -16,12 +16,15 @@
*/
package org.apache.activemq.artemis.utils.critical;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ConcurrentModificationException;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.activemq.artemis.core.server.ActiveMQScheduledComponent;
import org.apache.activemq.artemis.utils.ActiveMQThreadFactory;
import org.apache.activemq.artemis.utils.collections.ConcurrentHashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -48,11 +51,28 @@ public class CriticalAnalyzerImpl implements CriticalAnalyzer {
* issues and won't be able to shutdown the server or halt the VM
*/
this.scheduledComponent = new ActiveMQScheduledComponent(null, null, checkTimeNanoSeconds, TimeUnit.NANOSECONDS, false) {
@Override
public void run() {
logger.trace("Checking critical analyzer");
check();
}
@Override
protected ActiveMQThreadFactory getThreadFactory() {
return new ActiveMQThreadFactory("CriticalAnalyzer", "Critical-Analyzer-", true, getThisClassLoader());
}
private ClassLoader getThisClassLoader() {
return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
@Override
public ClassLoader run() {
return CriticalAnalyzerImpl.this.getClass().getClassLoader();
}
});
}
};
}

View File

@ -689,7 +689,13 @@ public class ActiveMQServerImpl implements ActiveMQServer {
nodeManager = createNodeManager(configuration.getNodeManagerLockLocation(), false);
nodeManager.start();
try {
nodeManager.start();
} catch (Exception e) {
//if there's an error here, ensure remaining threads shut down
stopTheServer(true);
throw e;
}
ActiveMQServerLogger.LOGGER.serverStarting((haPolicy.isBackup() ? "backup" : "live"), configuration);

View File

@ -1281,7 +1281,33 @@
</args>
</configuration>
</execution>
<execution>
<phase>test-compile</phase>
<id>create-jdbc-bad-driver</id>
<goals>
<goal>create</goal>
</goals>
<configuration>
<role>amq</role>
<user>admin</user>
<password>admin</password>
<allowAnonymous>false</allowAnonymous>
<noWeb>true</noWeb>
<instance>${basedir}/target/jdbc-bad-driver</instance>
<args>
<arg>--http-host</arg>
<arg>${sts-http-host}</arg>
<arg>--http-port</arg>
<arg>8161</arg>
<arg>--shared-store</arg>
<arg>--jdbc</arg>
<arg>--jdbc-connection-url</arg>
<arg>tcp://noexist</arg>
<arg>--jdbc-driver-class-name</arg>
<arg>badDriver</arg>
</args>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>

View File

@ -0,0 +1,43 @@
/*
* 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.activemq.artemis.tests.smoke.jdbc;
import org.apache.activemq.artemis.tests.smoke.common.SmokeTestBase;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
public class JdbcStartupTest extends SmokeTestBase {
protected static final String SERVER_NAME = "jdbc-bad-driver";
@Test
public void startupBadJdbcConnectionTest() throws Exception {
Integer exitVal = -1;
Process p = startServer(SERVER_NAME, 0, 0);
try {
p.waitFor(10, TimeUnit.SECONDS);
exitVal = p.exitValue();
} catch (Exception e) {
Assert.fail();
}
Assert.assertEquals(0, (long) exitVal);
}
}