|
22 | 22 | import java.util.List; |
23 | 23 |
|
24 | 24 | 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(); |
29 | 26 |
|
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() { |
88 | 28 | } |
89 | 29 |
|
90 | | - private ClusterUtils() {} |
91 | | - |
92 | 30 | public static List<ClusterInsert> createClusterInserts() { |
93 | | - return createClusterInserts(DEFAULT_CLUSTER_COUNT, DEFAULT_CLUSTER_NAME, DEFAULT_SERVER_NAME_PREFIX, false, null); |
| 31 | + return createClusterInserts(createNodes()); |
94 | 32 | } |
95 | 33 |
|
96 | 34 | 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)); |
106 | 36 | } |
107 | 37 |
|
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)); |
110 | 40 | } |
111 | 41 |
|
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)); |
114 | 44 | } |
115 | 45 |
|
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); |
118 | 48 | } |
119 | 49 |
|
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); |
122 | 52 | } |
123 | 53 |
|
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); |
128 | 56 | } |
129 | 57 |
|
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) { |
132 | 59 | 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; |
137 | 65 | 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)); |
139 | 67 | } |
140 | 68 | return nodes; |
141 | 69 | } |
@@ -167,7 +95,7 @@ public static List<ClusterInsert> createClusterInserts(List<ClusterNode> nodes) |
167 | 95 | lines.add("server_name=" + node.serverName); |
168 | 96 | lines.add("cluster {"); |
169 | 97 | 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; |
171 | 99 | lines.add(" listen: " + host + ":" + node.listen); |
172 | 100 | lines.add(" routes: ["); |
173 | 101 | for (ClusterNode routeNode : nodes) { |
|
0 commit comments