Skip to content

Commit d0e192a

Browse files
committed
ARTEMIS-4545 Allow node ID to be configured
1 parent b05d45c commit d0e192a

14 files changed

Lines changed: 127 additions & 6 deletions

File tree

artemis-server/src/main/java/org/apache/activemq/artemis/core/config/Configuration.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ public interface Configuration {
7070
*/
7171
Configuration setName(String name);
7272

73+
/**
74+
* Returns the ID of the node.
75+
*/
76+
String getNodeID();
77+
78+
/**
79+
* Sets the ID of the node. If not set, a UUID generated on first startup will be used instead.
80+
*/
81+
Configuration setNodeID(String nodeID);
82+
7383
/**
7484
* We use Bean-utils to pass in System.properties that start with {@link #setSystemPropertyPrefix(String)}.
7585
* The default should be 'brokerconfig.' (Including the ".").

artemis-server/src/main/java/org/apache/activemq/artemis/core/config/impl/ConfigurationImpl.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ public class ConfigurationImpl implements Configuration, Serializable {
153153

154154
private String name = "localhost";
155155

156+
private String nodeID = null;
157+
156158
private boolean persistenceEnabled = ActiveMQDefaultConfiguration.isDefaultPersistenceEnabled();
157159

158160
private int maxRedeliveryRecords = ActiveMQDefaultConfiguration.getDefaultMaxRedeliveryRecords();
@@ -2559,6 +2561,17 @@ public ConfigurationImpl setName(String name) {
25592561
return this;
25602562
}
25612563

2564+
@Override
2565+
public String getNodeID() {
2566+
return nodeID;
2567+
}
2568+
2569+
@Override
2570+
public ConfigurationImpl setNodeID(String nodeID) {
2571+
this.nodeID = nodeID;
2572+
return this;
2573+
}
2574+
25622575
@Override
25632576
public ConfigurationImpl setResolveProtocols(boolean resolveProtocols) {
25642577
this.resolveProtocols = resolveProtocols;
@@ -2707,6 +2720,7 @@ public int hashCode() {
27072720
result = prime * result + (int) (messageExpiryScanPeriod ^ (messageExpiryScanPeriod >>> 32));
27082721
result = prime * result + messageExpiryThreadPriority;
27092722
result = prime * result + ((name == null) ? 0 : name.hashCode());
2723+
result = prime * result + ((nodeID == null) ? 0 : nodeID.hashCode());
27102724
result = prime * result + ((outgoingInterceptorClassNames == null) ? 0 : outgoingInterceptorClassNames.hashCode());
27112725
result = prime * result + ((pagingDirectory == null) ? 0 : pagingDirectory.hashCode());
27122726
result = prime * result + (persistDeliveryCountBeforeDelivery ? 1231 : 1237);
@@ -2910,6 +2924,11 @@ public boolean equals(Object obj) {
29102924
return false;
29112925
} else if (!name.equals(other.name))
29122926
return false;
2927+
if (nodeID == null) {
2928+
if (other.nodeID != null)
2929+
return false;
2930+
} else if (!nodeID.equals(other.nodeID))
2931+
return false;
29132932
if (outgoingInterceptorClassNames == null) {
29142933
if (other.outgoingInterceptorClassNames != null)
29152934
return false;

artemis-server/src/main/java/org/apache/activemq/artemis/core/deployers/impl/FileConfigurationParser.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,8 @@ public void parseMainConfig(final Element e, final Configuration config) throws
424424

425425
config.setName(getString(e, "name", config.getName(), NO_CHECK));
426426

427+
config.setNodeID(getString(e, "node-id", config.getNodeID(), NO_CHECK));
428+
427429
config.setSystemPropertyPrefix(getString(e, "system-property-prefix", config.getSystemPropertyPrefix(), NOT_NULL_OR_EMPTY));
428430

429431
NodeList haPolicyNodes = e.getElementsByTagName("ha-policy");

artemis-server/src/main/java/org/apache/activemq/artemis/core/server/NodeManager.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ public SimpleString getNodeId() {
8383
}
8484
}
8585

86+
/**
87+
* Returns the converted form of a given nodeID
88+
*
89+
* @param nodeID
90+
*/
91+
public String getConvertedNodeId(String nodeID) {
92+
return new UUID(UUID.TYPE_TIME_BASED, UUID.stringToBytes(nodeID)).toString();
93+
}
94+
8695
public long readNodeActivationSequence() throws NodeManagerException {
8796
// TODO make it abstract
8897
throw new UnsupportedOperationException("TODO");
@@ -117,16 +126,14 @@ public UUID getUUID() {
117126
}
118127

119128
/**
120-
* Sets the nodeID.
121-
* <p>
122-
* Only used by replicating backups.
129+
* Sets the nodeID
123130
*
124131
* @param nodeID
125132
*/
126133
public void setNodeID(String nodeID) {
127134
synchronized (nodeIDGuard) {
128-
this.nodeID = new SimpleString(nodeID);
129135
this.uuid = new UUID(UUID.TYPE_TIME_BASED, UUID.stringToBytes(nodeID));
136+
this.nodeID = new SimpleString(uuid.toString());
130137
}
131138
}
132139

artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ActiveMQServerImpl.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,35 @@ protected NodeManager createNodeManager(final File directory, boolean replicatin
617617
} else {
618618
manager = new FileLockNodeManager(directory, replicatingBackup, configuration.getJournalLockAcquisitionTimeout(), scheduledPool);
619619
}
620+
621+
if (!replicatingBackup && configuration.getNodeID() != null) {
622+
manager.setNodeID(toCompatibleNodeID(configuration.getNodeID()));
623+
}
624+
620625
return manager;
621626
}
622627

628+
private String toCompatibleNodeID(String nodeID) {
629+
if (nodeID == null) {
630+
return null;
631+
}
632+
633+
final int len = nodeID.length();
634+
635+
if (!(len > 0)) {
636+
return null;
637+
}
638+
639+
if (len >= 16) {
640+
nodeID = nodeID.substring(0, 16);
641+
} else if (len % 2 != 0) {
642+
// must be even for conversion to uuid, extend to next even
643+
nodeID = nodeID + "+";
644+
}
645+
646+
return nodeID.replace('-', '.');
647+
}
648+
623649
@Override
624650
public OperationContext newOperationContext() {
625651
return getStorageManager().newContext(getExecutorFactory().getExecutor());

artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/FileBasedNodeManager.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,9 @@ protected final synchronized void createNodeId() throws IOException {
208208
channel.write(id, 3);
209209
channel.force(true);
210210
} else if (read != 16) {
211-
setUUID(UUIDGenerator.getInstance().generateUUID());
211+
if (getUUID() == null) {
212+
setUUID(UUIDGenerator.getInstance().generateUUID());
213+
}
212214
id.put(getUUID().asBytes(), 0, 16);
213215
id.position(0);
214216
channel.write(id, 3);

artemis-server/src/main/java/org/apache/activemq/artemis/core/server/impl/ReplicationBackupActivation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public ReplicationBackupActivation(final ActiveMQServerImpl activeMQServer,
8080
// patch expectedNodeID
8181
final String coordinationId = policy.getPrimaryPolicy().getCoordinationId();
8282
if (coordinationId != null) {
83-
expectedNodeID = coordinationId;
83+
expectedNodeID = activeMQServer.getNodeManager().getConvertedNodeId(coordinationId);
8484
} else {
8585
final SimpleString serverNodeID = activeMQServer.getNodeID();
8686
if (serverNodeID == null || serverNodeID.isEmpty()) {

artemis-server/src/main/resources/schema/artemis-configuration.xsd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@
3636
</xsd:annotation>
3737
</xsd:element>
3838

39+
<xsd:element name="node-id" type="xsd:string" maxOccurs="1" minOccurs="0">
40+
<xsd:annotation>
41+
<xsd:documentation>
42+
Node ID. If set, it will be used as an identifier when running in a cluster.
43+
Has to be unique within the cluster
44+
</xsd:documentation>
45+
</xsd:annotation>
46+
</xsd:element>
47+
3948
<xsd:element name="system-property-prefix" type="xsd:string" maxOccurs="1" minOccurs="0">
4049
<xsd:annotation>
4150
<xsd:documentation>

artemis-server/src/test/resources/ConfigurationTest-full-config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
xsi:schemaLocation="urn:activemq ../../../../activemq-server/src/main/resources/schema/artemis-server.xsd">
2121
<core xmlns="urn:activemq:core">
2222
<name>SomeNameForUseOnTheApplicationServer</name>
23+
<node-id>AUniqueIDForThisBroker</node-id>
2324
<resolve-protocols>false</resolve-protocols>
2425
<persistence-enabled>false</persistence-enabled>
2526
<scheduled-thread-pool-max-size>12345</scheduled-thread-pool-max-size>

artemis-server/src/test/resources/ConfigurationTest-xinclude-config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
xsi:schemaLocation="urn:activemq ../../../../activemq-server/src/main/resources/schema/artemis-server.xsd">
2222
<core xmlns="urn:activemq:core">
2323
<name>SomeNameForUseOnTheApplicationServer</name>
24+
<node-id>AUniqueIDForThisBroker</node-id>
2425
<resolve-protocols>false</resolve-protocols>
2526
<persistence-enabled>false</persistence-enabled>
2627
<scheduled-thread-pool-max-size>12345</scheduled-thread-pool-max-size>

0 commit comments

Comments
 (0)