From 69bea6756c220fe9f97f109cb0395c1c2fa0ee78 Mon Sep 17 00:00:00 2001 From: Andy Taylor Date: Tue, 23 Mar 2021 12:07:42 +0000 Subject: [PATCH] ARTEMIS-3201 - configured diverts arent persisted https://issues.apache.org/jira/browse/ARTEMIS-3201 --- .../impl/ActiveMQServerControlImpl.java | 2 - .../core/server/impl/ActiveMQServerImpl.java | 28 ++++-- .../tests/integration/divert/DivertTest.java | 95 +++++++++++++++++++ .../management/ActiveMQServerControlTest.java | 4 + 4 files changed, 118 insertions(+), 11 deletions(-) diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java index 8c4f906d03..1ed32661ce 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/management/impl/ActiveMQServerControlImpl.java @@ -82,7 +82,6 @@ import org.apache.activemq.artemis.core.messagecounter.MessageCounterManager; import org.apache.activemq.artemis.core.messagecounter.impl.MessageCounterManagerImpl; import org.apache.activemq.artemis.core.persistence.StorageManager; import org.apache.activemq.artemis.core.persistence.config.PersistedAddressSetting; -import org.apache.activemq.artemis.core.persistence.config.PersistedDivertConfiguration; import org.apache.activemq.artemis.core.persistence.config.PersistedSecuritySetting; import org.apache.activemq.artemis.core.postoffice.Binding; import org.apache.activemq.artemis.core.postoffice.Bindings; @@ -3590,7 +3589,6 @@ public class ActiveMQServerControlImpl extends AbstractControl implements Active TransformerConfiguration transformerConfiguration = transformerClassName == null || transformerClassName.isEmpty() ? null : new TransformerConfiguration(transformerClassName).setProperties(transformerProperties); DivertConfiguration config = new DivertConfiguration().setName(name).setRoutingName(routingName).setAddress(address).setForwardingAddress(forwardingAddress).setExclusive(exclusive).setFilterString(filterString).setTransformerConfiguration(transformerConfiguration).setRoutingType(ComponentConfigurationRoutingType.valueOf(routingType)); server.deployDivert(config); - storageManager.storeDivertConfiguration(new PersistedDivertConfiguration(config)); } finally { blockOnIO(); } diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java index 5f831229d6..45221bf49e 100644 --- a/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java +++ b/artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java @@ -2752,7 +2752,6 @@ public class ActiveMQServerImpl implements ActiveMQServer { return federationManager; } - @Override public Divert deployDivert(DivertConfiguration config) throws Exception { if (config.getName() == null) { @@ -2791,6 +2790,8 @@ public class ActiveMQServerImpl implements ActiveMQServer { Binding binding = new DivertBinding(storageManager.generateID(), sAddress, divert); + storageManager.storeDivertConfiguration(new PersistedDivertConfiguration(config)); + postOffice.addBinding(binding); managementService.registerDivert(divert); @@ -2831,6 +2832,8 @@ public class ActiveMQServerImpl implements ActiveMQServer { divert.setRoutingType(config.getRoutingType()); } + storageManager.storeDivertConfiguration(new PersistedDivertConfiguration(config)); + return divert; } @@ -3594,14 +3597,6 @@ public class ActiveMQServerImpl implements ActiveMQServer { securityRepository.addMatch(roleItem.getAddressMatch().toString(), setRoles); } - - List persistedDivertConfigurations = storageManager.recoverDivertConfigurations(); - - if (persistedDivertConfigurations != null) { - for (PersistedDivertConfiguration persistedDivertConfiguration : persistedDivertConfigurations) { - configuration.getDivertConfigurations().add(persistedDivertConfiguration.getDivertConfiguration()); - } - } } @Override @@ -4060,6 +4055,21 @@ public class ActiveMQServerImpl implements ActiveMQServer { } private void deployDiverts() throws Exception { + if (storageManager.recoverDivertConfigurations() != null) { + for (PersistedDivertConfiguration persistedDivertConfiguration : storageManager.recoverDivertConfigurations()) { + boolean deleted = true; + for (DivertConfiguration config : configuration.getDivertConfigurations()) { + if (persistedDivertConfiguration.getName().equals(config.getName())) { + deleted = false; + } + } + + if (deleted) { + //todo add a flag to specify whether to delete or not + deployDivert(persistedDivertConfiguration.getDivertConfiguration()); + } + } + } for (DivertConfiguration config : configuration.getDivertConfigurations()) { deployDivert(config); } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/divert/DivertTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/divert/DivertTest.java index 29b94a1008..cea40dd2e1 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/divert/DivertTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/divert/DivertTest.java @@ -741,6 +741,101 @@ public class DivertTest extends ActiveMQTestBase { Assert.assertNull(consumer4.receiveImmediate()); } + @Test + public void testSinglePersistedDivert() throws Exception { + final String testAddress = "testAddress"; + + final String forwardAddress = "forwardAddress"; + + DivertConfiguration divertConf = new DivertConfiguration().setName("divert1").setRoutingName("divert1").setAddress(testAddress).setForwardingAddress(forwardAddress).setExclusive(true); + + + QueueConfiguration q1 = new QueueConfiguration("forwardAddress1").setDurable(true).setRoutingType(RoutingType.ANYCAST); + + Configuration config = createDefaultInVMConfig().addDivertConfiguration(divertConf).addQueueConfiguration(q1); + + ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, true)); + + server.start(); + + server.stop(); + + divertConf.setRoutingName("divert2"); + + server.start(); + + Binding divert1 = server.getPostOffice().getBinding(new SimpleString("divert1")); + + Assert.assertNotNull(divert1); + + Assert.assertEquals(divert1.getRoutingName(), new SimpleString("divert2")); + } + + @Test + public void testSinglePersistedNewDivert() throws Exception { + final String testAddress = "testAddress"; + + final String forwardAddress = "forwardAddress"; + + DivertConfiguration divertConf = new DivertConfiguration().setName("divert1").setRoutingName("divert1").setAddress(testAddress).setForwardingAddress(forwardAddress).setExclusive(true); + + + QueueConfiguration q1 = new QueueConfiguration("forwardAddress1").setDurable(true).setRoutingType(RoutingType.ANYCAST); + + Configuration config = createDefaultInVMConfig().addDivertConfiguration(divertConf).addQueueConfiguration(q1); + + ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, true)); + + server.start(); + + server.stop(); + + divertConf = new DivertConfiguration().setName("divert1").setRoutingName("divert2").setAddress(testAddress).setForwardingAddress(forwardAddress).setExclusive(true); + + config.getDivertConfigurations().clear(); + + config.getDivertConfigurations().add(divertConf); + + server.start(); + + Binding divert1 = server.getPostOffice().getBinding(new SimpleString("divert1")); + + Assert.assertNotNull(divert1); + + Assert.assertEquals(divert1.getRoutingName(), new SimpleString("divert2")); + } + + @Test + public void testSinglePersistedNoDeleteDivert() throws Exception { + final String testAddress = "testAddress"; + + final String forwardAddress = "forwardAddress"; + + DivertConfiguration divertConf = new DivertConfiguration().setName("divert1").setRoutingName("divert1").setAddress(testAddress).setForwardingAddress(forwardAddress).setExclusive(true); + + + QueueConfiguration q1 = new QueueConfiguration("forwardAddress1").setDurable(true).setRoutingType(RoutingType.ANYCAST); + + Configuration config = createDefaultInVMConfig().addDivertConfiguration(divertConf).addQueueConfiguration(q1); + + ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, true)); + + server.start(); + + server.stop(); + + config.getDivertConfigurations().clear(); + + server.start(); + + Binding divert1 = server.getPostOffice().getBinding(new SimpleString("divert1")); + + Assert.assertNotNull(divert1); + + Assert.assertEquals(divert1.getRoutingName(), new SimpleString("divert1")); + } + + @Test public void testMultipleNonExclusiveDivert() throws Exception { final String testAddress = "testAddress"; diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java index de80874428..10d88ce130 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/artemis/tests/integration/management/ActiveMQServerControlTest.java @@ -66,6 +66,7 @@ import org.apache.activemq.artemis.core.config.ClusterConnectionConfiguration; import org.apache.activemq.artemis.core.config.Configuration; import org.apache.activemq.artemis.core.config.impl.SecurityConfiguration; import org.apache.activemq.artemis.core.messagecounter.impl.MessageCounterManagerImpl; +import org.apache.activemq.artemis.core.persistence.config.PersistedDivertConfiguration; import org.apache.activemq.artemis.core.remoting.impl.invm.InVMAcceptorFactory; import org.apache.activemq.artemis.core.remoting.impl.invm.TransportConstants; import org.apache.activemq.artemis.core.security.Role; @@ -1770,6 +1771,9 @@ public class ActiveMQServerControlTest extends ManagementTestBase { divertNames = serverControl.getDivertNames(); assertEquals(1, divertNames.length); assertEquals(name, divertNames[0]); + //now check its been persisted + PersistedDivertConfiguration pdc = server.getStorageManager().recoverDivertConfigurations().get(0); + assertEquals(pdc.getDivertConfiguration().getForwardingAddress(), updatedForwardingAddress); // check that a message is no longer exclusively diverted message = session.createMessage(false);