From b0497ed724eb8a513bc619485c324ebab1a9405c Mon Sep 17 00:00:00 2001 From: Clebert Suconic Date: Thu, 10 Oct 2019 11:55:12 -0400 Subject: [PATCH] ARTEMIS-2517 JMX will be shutdown after failback --- .../activemq/artemis/cli/commands/Run.java | 12 +- .../activemq/artemis/util/ServerUtil.java | 10 +- .../server/management/ManagementContext.java | 11 +- .../artemis/component/WebServerComponent.java | 1 - tests/smoke-tests/pom.xml | 45 +++- .../servers/jmx-failback1/broker.xml | 252 ++++++++++++++++++ .../servers/jmx-failback1/management.xml | 20 ++ .../servers/jmx-failback2/broker.xml | 251 +++++++++++++++++ .../servers/jmx-failback2/management.xml | 20 ++ .../main/resources/servers/jmx/management.xml | 1 - .../smoke/jmxfailback/JmxFailbackTest.java | 109 ++++++++ 11 files changed, 710 insertions(+), 22 deletions(-) create mode 100644 tests/smoke-tests/src/main/resources/servers/jmx-failback1/broker.xml create mode 100644 tests/smoke-tests/src/main/resources/servers/jmx-failback1/management.xml create mode 100644 tests/smoke-tests/src/main/resources/servers/jmx-failback2/broker.xml create mode 100644 tests/smoke-tests/src/main/resources/servers/jmx-failback2/management.xml create mode 100644 tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jmxfailback/JmxFailbackTest.java diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Run.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Run.java index d7a26cb337..5e0209a883 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Run.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/cli/commands/Run.java @@ -29,7 +29,6 @@ import org.apache.activemq.artemis.cli.factory.BrokerFactory; import org.apache.activemq.artemis.cli.factory.jmx.ManagementFactory; import org.apache.activemq.artemis.cli.factory.security.SecurityManagerFactory; import org.apache.activemq.artemis.components.ExternalComponent; -import org.apache.activemq.artemis.core.server.impl.CleaningActivateCallback; import org.apache.activemq.artemis.core.server.management.ManagementContext; import org.apache.activemq.artemis.dto.BrokerDTO; import org.apache.activemq.artemis.dto.ComponentDTO; @@ -83,16 +82,7 @@ public class Run extends LockAbstract { managementContext.start(); server.start(); - server.getServer().registerActivateCallback(new CleaningActivateCallback() { - @Override - public void deActivate() { - try { - managementContext.stop(); - } catch (Exception e) { - e.printStackTrace(); - } - } - }); + server.getServer().addExternalComponent(managementContext); if (broker.web != null) { broker.components.add(broker.web); diff --git a/artemis-cli/src/main/java/org/apache/activemq/artemis/util/ServerUtil.java b/artemis-cli/src/main/java/org/apache/activemq/artemis/util/ServerUtil.java index 50d85244fa..a80dc38b3f 100644 --- a/artemis-cli/src/main/java/org/apache/activemq/artemis/util/ServerUtil.java +++ b/artemis-cli/src/main/java/org/apache/activemq/artemis/util/ServerUtil.java @@ -83,11 +83,11 @@ public class ServerUtil { return process; } - public static void waitForServerToStart(int id, int timeout) throws InterruptedException { - waitForServerToStart("tcp://localhost:" + (61616 + id), timeout); + public static boolean waitForServerToStart(int id, int timeout) throws InterruptedException { + return waitForServerToStart("tcp://localhost:" + (61616 + id), timeout); } - public static void waitForServerToStart(String uri, long timeout) throws InterruptedException { + public static boolean waitForServerToStart(String uri, long timeout) throws InterruptedException { long realTimeout = System.currentTimeMillis() + timeout; while (System.currentTimeMillis() < realTimeout) { try (ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactory(uri, null)) { @@ -98,8 +98,10 @@ public class ServerUtil { Thread.sleep(500); continue; } - break; + return true; } + + return false; } public static void killServer(final Process server) throws Exception { diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/ManagementContext.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/ManagementContext.java index 175007c4a1..8d987e7033 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/ManagementContext.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/management/ManagementContext.java @@ -20,9 +20,9 @@ package org.apache.activemq.artemis.core.server.management; import java.util.concurrent.atomic.AtomicBoolean; import org.apache.activemq.artemis.core.config.JMXConnectorConfiguration; -import org.apache.activemq.artemis.core.server.ActiveMQComponent; +import org.apache.activemq.artemis.core.server.ServiceComponent; -public class ManagementContext implements ActiveMQComponent { +public class ManagementContext implements ServiceComponent { private AtomicBoolean isStarted = new AtomicBoolean(false); private JMXAccessControlList accessControlList; private JMXConnectorConfiguration jmxConnectorConfiguration; @@ -54,6 +54,13 @@ public class ManagementContext implements ActiveMQComponent { } } + @Override + public void stop(boolean shutdown) throws Exception { + if (shutdown) { + stop(); + } + } + @Override public boolean isStarted() { return isStarted.get(); diff --git a/artemis-web/src/main/java/org/apache/activemq/artemis/component/WebServerComponent.java b/artemis-web/src/main/java/org/apache/activemq/artemis/component/WebServerComponent.java index 2b0ff02294..a6136cbeec 100644 --- a/artemis-web/src/main/java/org/apache/activemq/artemis/component/WebServerComponent.java +++ b/artemis-web/src/main/java/org/apache/activemq/artemis/component/WebServerComponent.java @@ -236,7 +236,6 @@ public class WebServerComponent implements ExternalComponent { } public void internalStop() throws Exception { - System.out.println("Stopping"); server.stop(); if (webContexts != null) { cleanupWebTemporaryFiles(webContexts); diff --git a/tests/smoke-tests/pom.xml b/tests/smoke-tests/pom.xml index 36d7ea1381..e466917665 100644 --- a/tests/smoke-tests/pom.xml +++ b/tests/smoke-tests/pom.xml @@ -247,9 +247,48 @@ - - - + + test-compile + create-jmx-failback + + create + + + + ${basedir}/target/classes/servers/jmx-failback1 + true + admin + admin + ${basedir}/target/jmx-failback1 + + + --java-options + -Djava.rmi.server.hostname=localhost + + + + + test-compile + create-jmx-failback2 + + create + + + + ${basedir}/target/classes/servers/jmx-failback2 + true + admin + admin + ${basedir}/target/jmx-failback2 + + + --java-options + -Djava.rmi.server.hostname=localhost + + + + + org.apache.activemq.tests smoke-tests diff --git a/tests/smoke-tests/src/main/resources/servers/jmx-failback1/broker.xml b/tests/smoke-tests/src/main/resources/servers/jmx-failback1/broker.xml new file mode 100644 index 0000000000..a50a204f22 --- /dev/null +++ b/tests/smoke-tests/src/main/resources/servers/jmx-failback1/broker.xml @@ -0,0 +1,252 @@ + + + + + + + + live + + + true + + + NIO + + ../shared-jmxfailback/paging + + ../shared-jmxfailback/bindings + + ../shared-jmxfailback/journal + + ../shared-jmxfailback/large-messages + + true + + 2 + + 10 + + 4096 + + 10M + + + + + + + + + + + + + + + + + + + + tcp://localhost:61616 + tcp://localhost:61617 + + + + + 5000 + + + 90 + + + true + + 120000 + + 60000 + + HALT + + + + + + + + + + + + + tcp://localhost:61616?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=CORE,AMQP,STOMP,HORNETQ,MQTT,OPENWIRE;useEpoll=true;amqpCredits=1000;amqpLowCredits=300 + + + tcp://localhost:5672?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=AMQP;useEpoll=true;amqpCredits=1000;amqpLowCredits=300 + + + tcp://localhost:61613?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=STOMP;useEpoll=true + + + tcp://localhost:5445?anycastPrefix=jms.queue.;multicastPrefix=jms.topic.;protocols=HORNETQ,STOMP;useEpoll=true + + + tcp://localhost:1883?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=MQTT;useEpoll=true + + + + + cluster-admin + + password-admin + + + artemis + ON_DEMAND + 0 + + node0 + + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + DLQ + ExpiryQueue + 0 + + -1 + 10 + PAGE + true + true + true + true + + + + DLQ + ExpiryQueue + 0 + + -1 + 10 + PAGE + true + true + true + true + + + + +
+ + + +
+
+ + + +
+
+ + + +
+ +
+ + + + +
+
diff --git a/tests/smoke-tests/src/main/resources/servers/jmx-failback1/management.xml b/tests/smoke-tests/src/main/resources/servers/jmx-failback1/management.xml new file mode 100644 index 0000000000..576f1e5995 --- /dev/null +++ b/tests/smoke-tests/src/main/resources/servers/jmx-failback1/management.xml @@ -0,0 +1,20 @@ + + + + + \ No newline at end of file diff --git a/tests/smoke-tests/src/main/resources/servers/jmx-failback2/broker.xml b/tests/smoke-tests/src/main/resources/servers/jmx-failback2/broker.xml new file mode 100644 index 0000000000..b53074494c --- /dev/null +++ b/tests/smoke-tests/src/main/resources/servers/jmx-failback2/broker.xml @@ -0,0 +1,251 @@ + + + + + + + + backup + + + true + + + NIO + + ../shared-jmxfailback/paging + + ../shared-jmxfailback/bindings + + ../shared-jmxfailback/journal + + ../shared-jmxfailback/large-messages + + true + + 2 + + 10 + + 4096 + + 10M + + + + + + + + + + + + + + + + + + + + tcp://localhost:61617 + tcp://localhost:61616 + + + + + 5000 + + + 90 + + + true + + 120000 + + 60000 + + HALT + + + + + + + + + + + + + tcp://localhost:61617?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=CORE,AMQP,STOMP,HORNETQ,MQTT,OPENWIRE;useEpoll=true;amqpCredits=1000;amqpLowCredits=300 + + + tcp://localhost:5673?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=AMQP;useEpoll=true;amqpCredits=1000;amqpLowCredits=300 + + + tcp://localhost:61614?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=STOMP;useEpoll=true + + + tcp://localhost:5446?anycastPrefix=jms.queue.;multicastPrefix=jms.topic.;protocols=HORNETQ,STOMP;useEpoll=true + + + tcp://localhost:1884?tcpSendBufferSize=1048576;tcpReceiveBufferSize=1048576;protocols=MQTT;useEpoll=true + + + + + cluster-admin + + password-admin + + + artemis + ON_DEMAND + 0 + + node0 + + + + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + DLQ + ExpiryQueue + 0 + + -1 + 10 + PAGE + true + true + true + true + + + + DLQ + ExpiryQueue + 0 + + -1 + 10 + PAGE + true + true + true + true + + + + +
+ + + +
+
+ + + +
+
+ + + +
+ +
+ + + + +
+
diff --git a/tests/smoke-tests/src/main/resources/servers/jmx-failback2/management.xml b/tests/smoke-tests/src/main/resources/servers/jmx-failback2/management.xml new file mode 100644 index 0000000000..14bbaf2218 --- /dev/null +++ b/tests/smoke-tests/src/main/resources/servers/jmx-failback2/management.xml @@ -0,0 +1,20 @@ + + + + + \ No newline at end of file diff --git a/tests/smoke-tests/src/main/resources/servers/jmx/management.xml b/tests/smoke-tests/src/main/resources/servers/jmx/management.xml index f62611e0b1..87d1e02af3 100644 --- a/tests/smoke-tests/src/main/resources/servers/jmx/management.xml +++ b/tests/smoke-tests/src/main/resources/servers/jmx/management.xml @@ -16,7 +16,6 @@ ~ limitations under the License. --> - diff --git a/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jmxfailback/JmxFailbackTest.java b/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jmxfailback/JmxFailbackTest.java new file mode 100644 index 0000000000..2398c0fd60 --- /dev/null +++ b/tests/smoke-tests/src/test/java/org/apache/activemq/artemis/tests/smoke/jmxfailback/JmxFailbackTest.java @@ -0,0 +1,109 @@ +/** + * 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.jmxfailback; + +import javax.management.MBeanServerInvocationHandler; +import javax.management.remote.JMXConnector; +import javax.management.remote.JMXConnectorFactory; +import javax.management.remote.JMXServiceURL; +import java.io.File; + +import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration; +import org.apache.activemq.artemis.api.core.management.ActiveMQServerControl; +import org.apache.activemq.artemis.api.core.management.ObjectNameBuilder; +import org.apache.activemq.artemis.tests.smoke.common.SmokeTestBase; +import org.apache.activemq.artemis.utils.Wait; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class JmxFailbackTest extends SmokeTestBase { + + // This test will use a smoke created by the pom on this project (smoke-tsts) + private static final String JMX_SERVER_HOSTNAME = "localhost"; + private static final int JMX_SERVER_PORT_0 = 10099; + private static final int JMX_SERVER_PORT_1 = 10199; + + public static final String SERVER_NAME_0 = "jmx-failback1"; + public static final String SERVER_NAME_1 = "jmx-failback2"; + + String urlString_1 = "service:jmx:rmi:///jndi/rmi://" + JMX_SERVER_HOSTNAME + ":" + JMX_SERVER_PORT_0 + "/jmxrmi"; + String urlString_2 = "service:jmx:rmi:///jndi/rmi://" + JMX_SERVER_HOSTNAME + ":" + JMX_SERVER_PORT_1 + "/jmxrmi"; + + ObjectNameBuilder objectNameBuilder1 = ObjectNameBuilder.create(ActiveMQDefaultConfiguration.getDefaultJmxDomain(), "live", true); + ObjectNameBuilder objectNameBuilder2 = ObjectNameBuilder.create(ActiveMQDefaultConfiguration.getDefaultJmxDomain(), "backup", true); + + JMXServiceURL url1 = null; + JMXServiceURL url2 = null; + + Process server1; + Process server2; + + @Before + public void before() throws Exception { + url1 = new JMXServiceURL(urlString_1); + url2 = new JMXServiceURL(urlString_2); + deleteDirectory(new File("shared-jmxfailback")); + disableCheckThread(); + server1 = startServer(SERVER_NAME_0, 0, 30000); + Wait.assertTrue(() -> testConnection(url1, objectNameBuilder1)); + server2 = startServer(SERVER_NAME_1, 0, 0); + Wait.assertTrue(() -> testConnection(url2, objectNameBuilder2)); + } + + boolean isBackup(JMXServiceURL serviceURI, ObjectNameBuilder builder) throws Exception { + JMXConnector jmx = null; + try { + jmx = JMXConnectorFactory.connect(serviceURI); + builder.getActiveMQServerObjectName(); + + ActiveMQServerControl control = MBeanServerInvocationHandler.newProxyInstance(jmx.getMBeanServerConnection(), builder.getActiveMQServerObjectName(), ActiveMQServerControl.class, false); + return control.isBackup(); // performing any operation to make sure JMX is bound already + } finally { + try { + jmx.close(); + } catch (Exception e) { + } + } + } + + boolean testConnection(JMXServiceURL serviceURI, ObjectNameBuilder builder) { + try { + isBackup(serviceURI, builder); + return true; + } catch (Exception e) { + return false; + } + } + + @Test + public void testFailbackOnJMX() throws Exception { + Assert.assertFalse(isBackup(url1, objectNameBuilder1)); + Assert.assertTrue(isBackup(url2, objectNameBuilder2)); + + server1.destroyForcibly(); + Wait.assertFalse(() -> isBackup(url2, objectNameBuilder2)); + + server1 = startServer(SERVER_NAME_0, 0, 30000); + Wait.assertTrue(() -> testConnection(url1, objectNameBuilder1), 5_000, 100); + Wait.assertTrue(() -> testConnection(url2, objectNameBuilder2), 5_000, 100); + + Wait.assertFalse(() -> isBackup(url1, objectNameBuilder1), 5_000, 100); + Wait.assertTrue(() -> isBackup(url2, objectNameBuilder2), 5_000, 100); + } +}