Skip to content

Commit 940e9c4

Browse files
ARTEMIS-5852 Leader/Follower pattern on acceptors
I'm adding a LeaderManager to the broker, that will use DistributedLock to determine the node that will be the lead. You can associate the leaderManager with acceptors and an acceptor serving only clients would then be activated in only one of the brokers.
1 parent e83b0fe commit 940e9c4

63 files changed

Lines changed: 4087 additions & 110 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

artemis-bom/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@
191191
<artifactId>artemis-lockmanager-ri</artifactId>
192192
<version>${project.version}</version>
193193
</dependency>
194+
<dependency>
195+
<groupId>org.apache.artemis</groupId>
196+
<artifactId>artemis-lockmanager-etcd</artifactId>
197+
<version>${project.version}</version>
198+
</dependency>
194199
<dependency>
195200
<groupId>org.apache.artemis</groupId>
196201
<artifactId>artemis-ra</artifactId>
@@ -393,6 +398,11 @@
393398
<artifactId>artemis-lockmanager-ri</artifactId>
394399
<version>${project.version}</version>
395400
</dependency>
401+
<dependency>
402+
<groupId>org.apache.activemq</groupId>
403+
<artifactId>artemis-lockmanager-etcd</artifactId>
404+
<version>${project.version}</version>
405+
</dependency>
396406
<dependency>
397407
<groupId>org.apache.activemq</groupId>
398408
<artifactId>artemis-ra</artifactId>

artemis-core-client/src/main/java/org/apache/activemq/artemis/api/core/TransportConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class TransportConfiguration implements Serializable {
5151

5252
private String name;
5353

54+
private String leaderManager;
55+
5456
private String factoryClassName = "null";
5557

5658
private Map<String, Object> params;
@@ -413,6 +415,15 @@ public void decode(final ActiveMQBuffer buffer) {
413415
}
414416
}
415417

418+
public String getLeaderManager() {
419+
return leaderManager;
420+
}
421+
422+
public TransportConfiguration setLeaderManager(String leaderManager) {
423+
this.leaderManager = leaderManager;
424+
return this;
425+
}
426+
416427
private static String replaceWildcardChars(final String str) {
417428
return str.replace('.', '-');
418429
}

artemis-distribution/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@
126126
<groupId>org.apache.artemis</groupId>
127127
<artifactId>artemis-lockmanager-ri</artifactId>
128128
</dependency>
129+
<dependency>
130+
<groupId>org.apache.artemis</groupId>
131+
<artifactId>artemis-lockmanager-etcd</artifactId>
132+
</dependency>
129133

130134
<!--TODO: no other modules seem to use this, is it equivalent to something else they do use ? -->
131135
<dependency>

artemis-lockmanager/artemis-lockmanager-api/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@
3636
<groupId>org.apache.artemis</groupId>
3737
<artifactId>artemis-commons</artifactId>
3838
</dependency>
39+
40+
<!-- tests -->
41+
<dependency>
42+
<groupId>org.junit.jupiter</groupId>
43+
<artifactId>junit-jupiter-api</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.junit.jupiter</groupId>
48+
<artifactId>junit-jupiter-engine</artifactId>
49+
<scope>test</scope>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.apache.artemis</groupId>
53+
<artifactId>artemis-unit-test-support</artifactId>
54+
<version>${project.version}</version>
55+
<scope>test</scope>
56+
</dependency>
57+
3958
</dependencies>
4059

4160
</project>

artemis-lockmanager/artemis-lockmanager-api/src/main/java/org/apache/activemq/artemis/lockmanager/DistributedLock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface DistributedLock extends AutoCloseable {
2424

2525
String getLockId();
2626

27-
boolean isHeldByCaller() throws UnavailableStateException;
27+
boolean isLockValid() throws UnavailableStateException;
2828

2929
boolean tryLock() throws UnavailableStateException, InterruptedException;
3030

artemis-lockmanager/artemis-lockmanager-api/src/main/java/org/apache/activemq/artemis/lockmanager/DistributedLockManager.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@
2121
import java.util.concurrent.TimeUnit;
2222
import java.util.concurrent.TimeoutException;
2323

24-
import org.apache.activemq.artemis.utils.ClassloadingUtil;
25-
2624
public interface DistributedLockManager extends AutoCloseable {
2725

2826
static DistributedLockManager newInstanceOf(String className, Map<String, String> properties) throws Exception {
29-
return (DistributedLockManager) ClassloadingUtil.getInstanceForParamsWithTypeCheck(className,
30-
DistributedLockManager.class,
31-
DistributedLockManager.class.getClassLoader(),
32-
new Class[]{Map.class},
33-
properties);
27+
DistributedLockManagerFactory factory = Registry.getInstance().getFactoryWithClassName(className);
28+
if (factory == null) {
29+
throw new IllegalArgumentException(className + " not found");
30+
}
31+
return factory.build(properties);
3432
}
3533

3634
@FunctionalInterface
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.activemq.artemis.lockmanager;
19+
20+
import java.util.Map;
21+
import java.util.Set;
22+
23+
public interface DistributedLockManagerFactory {
24+
DistributedLockManager build(Map<String, String> properties);
25+
26+
String getName();
27+
28+
String getImplName();
29+
30+
default Map<String, String> validateParameters(Map<String, String> config) {
31+
config.forEach((parameterName, ignore) -> validateParameter(parameterName));
32+
return config;
33+
}
34+
35+
Set<String> getValidParametersList();
36+
37+
default void validateParameter(String parameterName) {
38+
Set<String> validList = getValidParametersList();
39+
if (!validList.contains(parameterName)) {
40+
throw new IllegalArgumentException("Invalid parameter '" + parameterName + "'. Accepted parameters: " + String.join(", ", validList));
41+
}
42+
}
43+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.activemq.artemis.lockmanager;
19+
20+
import java.util.HashMap;
21+
import java.util.ServiceLoader;
22+
23+
public class Registry {
24+
25+
private final HashMap<String, DistributedLockManagerFactory> factories = new HashMap<>();
26+
private final HashMap<String, DistributedLockManagerFactory> factoriesWithImpl = new HashMap<>();
27+
28+
private volatile boolean serviceLoaded = false;
29+
30+
private static final Registry INSTANCE = new Registry();
31+
32+
private Registry() {
33+
}
34+
35+
public static Registry getInstance() {
36+
return INSTANCE;
37+
}
38+
39+
public synchronized void register(DistributedLockManagerFactory factory) {
40+
factories.put(factory.getName(), factory);
41+
factoriesWithImpl.put(factory.getImplName(), factory);
42+
}
43+
44+
public synchronized void unregisterWithType(String type) {
45+
DistributedLockManagerFactory factory = factories.get(type);
46+
unregister(factory);
47+
}
48+
49+
public synchronized void unregisterWithClassName(String name) {
50+
DistributedLockManagerFactory factory = factoriesWithImpl.get(name);
51+
unregister(factory);
52+
}
53+
54+
private void unregister(DistributedLockManagerFactory factory) {
55+
factories.remove(factory.getName());
56+
factoriesWithImpl.remove(factory.getImplName());
57+
}
58+
59+
public synchronized DistributedLockManagerFactory getFactoryWithClassName(String className) {
60+
checkService();
61+
DistributedLockManagerFactory factory = factoriesWithImpl.get(className);
62+
if (factory == null) {
63+
throw new IllegalArgumentException("factory " + className + " not found");
64+
}
65+
return factory;
66+
}
67+
68+
public synchronized DistributedLockManagerFactory getFactory(String name) {
69+
checkService();
70+
return factories.get(name);
71+
}
72+
73+
public synchronized void checkService() {
74+
if (serviceLoaded) {
75+
return;
76+
}
77+
ServiceLoader<DistributedLockManagerFactory> services = ServiceLoader.load(DistributedLockManagerFactory.class);
78+
services.forEach(this::register);
79+
serviceLoaded = true;
80+
}
81+
82+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.activemq.artemis.lockmanager;
19+
20+
import java.util.Map;
21+
import java.util.Set;
22+
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
26+
import static org.junit.jupiter.api.Assertions.assertNull;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
28+
29+
public class RegistryTest {
30+
31+
32+
@Test
33+
public void testRegistryUnregister() {
34+
Registry.getInstance().register(new FakeDistributedLockManagerFactory());
35+
assertInstanceOf(FakeDistributedLockManagerFactory.class, Registry.getInstance().getFactory("fake"));
36+
assertInstanceOf(FakeDistributedLockManagerFactory.class, Registry.getInstance().getFactoryWithClassName("Fake"));
37+
Registry.getInstance().unregisterWithType("fake");
38+
assertNull(Registry.getInstance().getFactory("fake"));
39+
assertThrows(IllegalArgumentException.class, () -> Registry.getInstance().getFactoryWithClassName("Fake"));
40+
Registry.getInstance().register(new FakeDistributedLockManagerFactory());
41+
assertInstanceOf(FakeDistributedLockManagerFactory.class, Registry.getInstance().getFactory("fake"));
42+
assertInstanceOf(FakeDistributedLockManagerFactory.class, Registry.getInstance().getFactoryWithClassName("Fake"));
43+
Registry.getInstance().unregisterWithClassName("Fake");
44+
assertNull(Registry.getInstance().getFactory("fake"));
45+
assertNull(Registry.getInstance().getFactory("Fake"));
46+
assertThrows(IllegalArgumentException.class, () -> Registry.getInstance().getFactoryWithClassName("Fake"));
47+
48+
}
49+
50+
public static class FakeDistributedLockManagerFactory implements DistributedLockManagerFactory {
51+
52+
@Override
53+
public DistributedLockManager build(Map<String, String> properties) {
54+
return null;
55+
}
56+
57+
@Override
58+
public String getName() {
59+
return "fake";
60+
}
61+
62+
@Override
63+
public String getImplName() {
64+
return "Fake";
65+
}
66+
67+
@Override
68+
public Set<String> getValidParametersList() {
69+
return Set.of();
70+
}
71+
}
72+
73+
}

0 commit comments

Comments
 (0)