Skip to content

Commit 7b3d1d4

Browse files
committed
fix making cluster
1 parent 9ad5d92 commit 7b3d1d4

File tree

5 files changed

+200
-147
lines changed

5 files changed

+200
-147
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2025 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package io.nats;
15+
16+
public class ClusterDefaults {
17+
private String host;
18+
private int portStart;
19+
private int listenStart;
20+
private boolean hasMonitor;
21+
private int monitorStart;
22+
23+
private int count;
24+
private String clusterName;
25+
private String serverNamePrefix;
26+
27+
@Override
28+
public String toString() {
29+
return "ClusterDefaults{" +
30+
"h='" + host + '\'' +
31+
", p=" + portStart +
32+
", l=" + listenStart +
33+
", mB=" + hasMonitor +
34+
", mS=" + monitorStart +
35+
", c=" + count +
36+
", n='" + clusterName + '\'' +
37+
", s='" + serverNamePrefix + '\'' +
38+
'}';
39+
}
40+
41+
public ClusterDefaults() {
42+
host = NatsRunnerUtils.getDefaultLocalhostHost().host;
43+
portStart = 4220;
44+
listenStart = 4230;
45+
hasMonitor = false;
46+
monitorStart = 4280;
47+
48+
count = 3;
49+
clusterName = "cluster";
50+
serverNamePrefix = "server";
51+
}
52+
53+
public ClusterDefaults host(String host) {
54+
this.host = host;
55+
return this;
56+
}
57+
58+
public ClusterDefaults portStart(int portStart) {
59+
this.portStart = portStart;
60+
return this;
61+
}
62+
63+
public ClusterDefaults listenStart(int listenStart) {
64+
this.listenStart = listenStart;
65+
return this;
66+
}
67+
68+
public ClusterDefaults monitor(boolean monitor) {
69+
this.hasMonitor = monitor;
70+
return this;
71+
}
72+
73+
public ClusterDefaults monitorStart(int monitorStart) {
74+
if (monitorStart < 1) {
75+
hasMonitor = false;
76+
}
77+
else {
78+
this.monitorStart = monitorStart;
79+
hasMonitor = true;
80+
}
81+
return this;
82+
}
83+
84+
public ClusterDefaults count(int count) {
85+
this.count = count;
86+
return this;
87+
}
88+
89+
public ClusterDefaults clusterName(String clusterName) {
90+
this.clusterName = clusterName;
91+
return this;
92+
}
93+
94+
public ClusterDefaults serverNamePrefix(String serverNamePrefix) {
95+
this.serverNamePrefix = serverNamePrefix;
96+
return this;
97+
}
98+
99+
public String getHost() {
100+
return host;
101+
}
102+
103+
public int getPortStart() {
104+
return portStart;
105+
}
106+
107+
public int getListenStart() {
108+
return listenStart;
109+
}
110+
111+
public boolean hasMonitor() {
112+
return hasMonitor;
113+
}
114+
115+
public int getMonitorStart() {
116+
return monitorStart;
117+
}
118+
119+
public int getCount() {
120+
return count;
121+
}
122+
123+
public String getClusterName() {
124+
return clusterName;
125+
}
126+
127+
public String getServerNamePrefix() {
128+
return serverNamePrefix;
129+
}
130+
}

src/main/java/io/nats/ClusterUtils.java

Lines changed: 22 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -22,120 +22,48 @@
2222
import java.util.List;
2323

2424
public abstract class ClusterUtils {
25-
private static String DEFAULT_CLUSTER_HOST = "127.0.0.1";
26-
private static int DEFAULT_CLUSTER_PORT_START = 4220;
27-
private static int DEFAULT_CLUSTER_LISTEN_START = 4230;
28-
private static int DEFAULT_CLUSTER_MONITOR_START = 4280;
25+
public static final ClusterDefaults DEFAULT_CLUSTER_DEFAULTS = new ClusterDefaults();
2926

30-
private static int DEFAULT_CLUSTER_COUNT = 3;
31-
private static String DEFAULT_CLUSTER_NAME = "cluster";
32-
private static String DEFAULT_SERVER_NAME_PREFIX = "server";
33-
34-
public static void setDefaultClusterHost(String defaultHost) {
35-
DEFAULT_CLUSTER_HOST = defaultHost;
36-
}
37-
38-
public static void setDefaultClusterPortStart(int defaultPortStart) {
39-
DEFAULT_CLUSTER_PORT_START = defaultPortStart;
40-
}
41-
42-
public static void setDefaultClusterListenStart(int defaultListenStart) {
43-
DEFAULT_CLUSTER_LISTEN_START = defaultListenStart;
44-
}
45-
46-
public static void setDefaultClusterMonitorStart(int defaultMonitorStart) {
47-
DEFAULT_CLUSTER_MONITOR_START = defaultMonitorStart;
48-
}
49-
50-
public static void setDefaultClusterCount(int defaultClusterCount) {
51-
DEFAULT_CLUSTER_COUNT = defaultClusterCount;
52-
}
53-
54-
public static void setDefaultClusterName(String defaultClusterName) {
55-
DEFAULT_CLUSTER_NAME = defaultClusterName;
56-
}
57-
58-
public static void setDefaultServerNamePrefix(String defaultServerNamePrefix) {
59-
DEFAULT_SERVER_NAME_PREFIX = defaultServerNamePrefix;
60-
}
61-
62-
public static String getDefaultClusterHost() {
63-
return DEFAULT_CLUSTER_HOST;
64-
}
65-
66-
public static int getDefaultClusterPortStart() {
67-
return DEFAULT_CLUSTER_PORT_START;
68-
}
69-
70-
public static int getDefaultClusterListenStart() {
71-
return DEFAULT_CLUSTER_LISTEN_START;
72-
}
73-
74-
public static int getDefaultClusterMonitorStart() {
75-
return DEFAULT_CLUSTER_MONITOR_START;
76-
}
77-
78-
public static int getDefaultClusterCount() {
79-
return DEFAULT_CLUSTER_COUNT;
80-
}
81-
82-
public static String getDefaultClusterName() {
83-
return DEFAULT_CLUSTER_NAME;
84-
}
85-
86-
public static String getDefaultServerNamePrefix() {
87-
return DEFAULT_SERVER_NAME_PREFIX;
27+
private ClusterUtils() {
8828
}
8929

90-
private ClusterUtils() {}
91-
9230
public static List<ClusterInsert> createClusterInserts() {
93-
return createClusterInserts(DEFAULT_CLUSTER_COUNT, DEFAULT_CLUSTER_NAME, DEFAULT_SERVER_NAME_PREFIX, false, null);
31+
return createClusterInserts(createNodes());
9432
}
9533

9634
public static List<ClusterInsert> createClusterInserts(Path jsStoreDirBase) {
97-
return createClusterInserts(DEFAULT_CLUSTER_COUNT, DEFAULT_CLUSTER_NAME, DEFAULT_SERVER_NAME_PREFIX, false, jsStoreDirBase);
98-
}
99-
100-
public static List<ClusterInsert> createClusterInserts(int count) {
101-
return createClusterInserts(count, DEFAULT_CLUSTER_NAME, DEFAULT_SERVER_NAME_PREFIX, false, null);
102-
}
103-
104-
public static List<ClusterInsert> createClusterInserts(int count, Path jsStoreDirBase) {
105-
return createClusterInserts(count, DEFAULT_CLUSTER_NAME, DEFAULT_SERVER_NAME_PREFIX, false, jsStoreDirBase);
35+
return createClusterInserts(createNodes(jsStoreDirBase));
10636
}
10737

108-
public static List<ClusterInsert> createClusterInserts(int count, String clusterName, String serverNamePrefix) {
109-
return createClusterInserts(createNodes(count, clusterName, serverNamePrefix, false, null));
38+
public static List<ClusterInsert> createClusterInserts(ClusterDefaults cd) {
39+
return createClusterInserts(createNodes(cd));
11040
}
11141

112-
public static List<ClusterInsert> createClusterInserts(int count, String clusterName, String serverNamePrefix, Path jsStoreDirBase) {
113-
return createClusterInserts(createNodes(count, clusterName, serverNamePrefix, false, jsStoreDirBase));
42+
public static List<ClusterInsert> createClusterInserts(ClusterDefaults cd, Path jsStoreDirBase) {
43+
return createClusterInserts(createNodes(cd, jsStoreDirBase));
11444
}
11545

116-
public static List<ClusterInsert> createClusterInserts(int count, String clusterName, String serverNamePrefix, boolean monitor) {
117-
return createClusterInserts(createNodes(count, clusterName, serverNamePrefix, monitor, null));
46+
public static List<ClusterNode> createNodes() {
47+
return createNodes(DEFAULT_CLUSTER_DEFAULTS, null);
11848
}
11949

120-
public static List<ClusterInsert> createClusterInserts(int count, String clusterName, String serverNamePrefix, boolean monitor, Path jsStoreDirBase) {
121-
return createClusterInserts(createNodes(count, clusterName, serverNamePrefix, monitor, jsStoreDirBase));
50+
public static List<ClusterNode> createNodes(Path jsStoreDirBase) {
51+
return createNodes(DEFAULT_CLUSTER_DEFAULTS, jsStoreDirBase);
12252
}
12353

124-
public static List<ClusterNode> createNodes(int count, String clusterName, String serverNamePrefix, boolean monitor, Path jsStoreDirBase) {
125-
return createNodes(count, clusterName, serverNamePrefix, jsStoreDirBase,
126-
DEFAULT_CLUSTER_HOST, DEFAULT_CLUSTER_PORT_START, DEFAULT_CLUSTER_LISTEN_START,
127-
monitor ? DEFAULT_CLUSTER_MONITOR_START : null);
54+
public static List<ClusterNode> createNodes(ClusterDefaults cd) {
55+
return createNodes(cd, null);
12856
}
12957

130-
public static List<ClusterNode> createNodes(int count, String clusterName, String serverNamePrefix, Path jsStoreDirBase,
131-
String host, int portStart, int listenStart, Integer monitorStart) {
58+
public static List<ClusterNode> createNodes(ClusterDefaults cd, Path jsStoreDirBase) {
13259
List<ClusterNode> nodes = new ArrayList<>();
133-
for (int x = 0; x < count; x++) {
134-
int port = portStart + x;
135-
int listen = listenStart + x;
136-
Integer monitor = monitorStart == null ? null : monitorStart + x;
60+
for (int x = 0; x < cd.getCount(); x++) {
61+
int port = cd.getPortStart() + x;
62+
int listen = cd.getListenStart() + x;
63+
String server = cd.getServerNamePrefix() + x;
64+
Integer monitor = cd.hasMonitor() ? cd.getMonitorStart() + x : null;
13765
Path jsStoreDir = jsStoreDirBase == null ? null : Paths.get(jsStoreDirBase.toString(), "" + port);
138-
nodes.add( new ClusterNode(clusterName, serverNamePrefix + x, host, port, listen, monitor, jsStoreDir));
66+
nodes.add( new ClusterNode(cd.getClusterName(), server, cd.getHost(), port, listen, monitor, jsStoreDir));
13967
}
14068
return nodes;
14169
}
@@ -167,7 +95,7 @@ public static List<ClusterInsert> createClusterInserts(List<ClusterNode> nodes)
16795
lines.add("server_name=" + node.serverName);
16896
lines.add("cluster {");
16997
lines.add(" name: " + node.clusterName);
170-
String host = node.host == null ? DEFAULT_CLUSTER_HOST : node.host;
98+
String host = node.host == null ? DEFAULT_CLUSTER_DEFAULTS.getHost() : node.host;
17199
lines.add(" listen: " + host + ":" + node.listen);
172100
lines.add(" routes: [");
173101
for (ClusterNode routeNode : nodes) {

src/main/java/io/nats/NatsServerRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ public String getConfigFile() {
668668
* @return the uri string
669669
*/
670670
public String getNatsLocalhostUri() {
671-
return NatsRunnerUtils.getNatsLocalhostUri(getNatsPort());
671+
return NatsRunnerUtils.getNatsLocalhostUri(getPort());
672672
}
673673

674674
/**

src/test/java/io/nats/ClusterTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.junit.jupiter.api.Test;
1717
import org.junit.jupiter.api.parallel.Isolated;
1818

19+
import java.nio.file.Path;
1920
import java.nio.file.Paths;
2021
import java.util.Collections;
2122
import java.util.List;
@@ -29,18 +30,18 @@ public class ClusterTest extends TestBase {
2930
@Test
3031
public void testCreateCluster() throws Exception {
3132
_testCreateCluster(createClusterInserts(), false);
32-
_testCreateCluster(createClusterInserts(3), false);
33+
_testCreateCluster(createClusterInserts(DEFAULT_CLUSTER_DEFAULTS), false);
3334
_testCreateCluster(createClusterInserts(createTemporaryJetStreamStoreDirBase()), true);
34-
_testCreateCluster(createClusterInserts(3, createTemporaryJetStreamStoreDirBase()), true);
35+
_testCreateCluster(createClusterInserts(DEFAULT_CLUSTER_DEFAULTS, createTemporaryJetStreamStoreDirBase()), true);
3536
}
3637

3738
private void _testCreateCluster(List<ClusterInsert> clusterInserts, boolean js) throws Exception {
3839
for (ClusterInsert ci : clusterInserts) {
3940
String s = ci.toString();
4041
assertTrue(s.contains("port: " + ci.node.port));
41-
assertTrue(s.contains("server_name=" + getDefaultServerNamePrefix()));
42-
assertTrue(s.contains("listen: " + getDefaultClusterHost() + ":" + ci.node.listen));
43-
assertTrue(s.contains("name: " + getDefaultClusterName()));
42+
assertTrue(s.contains("server_name=" + DEFAULT_CLUSTER_DEFAULTS.getServerNamePrefix()));
43+
assertTrue(s.contains("listen: " + DEFAULT_CLUSTER_DEFAULTS.getHost() + ":" + ci.node.listen));
44+
assertTrue(s.contains("name: " + DEFAULT_CLUSTER_DEFAULTS.getClusterName()));
4445
if (js) {
4546
assertTrue(s.contains("jetstream"));
4647
assertTrue(s.contains("store_dir="));
@@ -97,7 +98,8 @@ public void testClusterNodeConstruction() {
9798
assertEquals(9999, cn.monitor);
9899
assertNull(cn.jsStoreDir);
99100

100-
cn = new ClusterNode("name", "server", 1234, 5678, Paths.get("path"));
101+
Path path = Paths.get("path");
102+
cn = new ClusterNode("name", "server", 1234, 5678, path);
101103
assertEquals("name", cn.clusterName);
102104
assertEquals("server", cn.serverName);
103105
assertNull(cn.host);
@@ -107,7 +109,7 @@ public void testClusterNodeConstruction() {
107109
assertNotNull(cn.jsStoreDir);
108110
assertEquals("path", cn.jsStoreDir.toString());
109111

110-
cn = new ClusterNode("name", "server", "host", 1234, 5678, 9999, Paths.get("path"));
112+
cn = new ClusterNode("name", "server", "host", 1234, 5678, 9999, path);
111113
assertEquals("name", cn.clusterName);
112114
assertEquals("server", cn.serverName);
113115
assertEquals("host", cn.host);
@@ -125,7 +127,7 @@ public void testClusterNodeConstruction() {
125127
.port(1234)
126128
.listen(5678)
127129
.monitor(9999)
128-
.jsStoreDir(Paths.get("path"))
130+
.jsStoreDir(path)
129131
.build()
130132
;
131133
assertEquals("name", cn.clusterName);

0 commit comments

Comments
 (0)