From 2989e9d577e82fc2be974b6981f3643678d243a3 Mon Sep 17 00:00:00 2001 From: Michael Barker Date: Mon, 9 Aug 2021 12:56:59 +1200 Subject: [PATCH] [Java] Add formatting of client ingress endpoints to ClusterConfig. --- .../aeron/samples/cluster/ClusterConfig.java | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/aeron-samples/src/main/java/io/aeron/samples/cluster/ClusterConfig.java b/aeron-samples/src/main/java/io/aeron/samples/cluster/ClusterConfig.java index 5312052a61..540a8ce385 100644 --- a/aeron-samples/src/main/java/io/aeron/samples/cluster/ClusterConfig.java +++ b/aeron-samples/src/main/java/io/aeron/samples/cluster/ClusterConfig.java @@ -38,7 +38,6 @@ */ public final class ClusterConfig { - public static final int PORT_BASE = 9000; public static final int PORTS_PER_NODE = 100; public static final int ARCHIVE_CONTROL_PORT_OFFSET = 1; public static final int CLIENT_FACING_PORT_OFFSET = 2; @@ -91,7 +90,7 @@ public static ClusterConfig create( "nodeId=" + nodeId + " >= ingressHostnames.size()=" + ingressHostnames.size()); } - final String clusterMembers = clusterMembers(ingressHostnames, clusterHostnames, PORT_BASE); + final String clusterMembers = clusterMembers(ingressHostnames, clusterHostnames, portBase); final String aeronDirName = CommonContext.getAeronDirectoryName() + "-" + nodeId + "-driver"; final File baseDir = new File(System.getProperty("user.dir"), "aeron-cluster-" + nodeId); @@ -128,6 +127,7 @@ public static ClusterConfig create( .clusterMembers(clusterMembers) .clusterDir(new File(baseDir, "consensus-module")) .archiveContext(aeronArchiveContext.clone()) + .ingressChannel("aeron:udp?term-length=64k") .replicationChannel("aeron:udp?endpoint=" + hostname + ":0"); final ClusteredServiceContainer.Context clusteredServiceContext = new ClusteredServiceContainer.Context() @@ -273,6 +273,33 @@ public static String clusterMembers( return sb.toString(); } + /** + * Ingress endpoints generated from a list of hostnames. + * + * @param hostnames for the cluster members. + * @param portBase Base port for the cluster + * @param clientFacingPortOffset Offset for the client facing port + * @return a formatted string of ingress endpoints for connecting to a cluster. + */ + public static String ingressEndpoints( + final List hostnames, + final int portBase, + final int clientFacingPortOffset) + { + final StringBuilder sb = new StringBuilder(); + for (int i = 0; i < hostnames.size(); i++) + { + sb.append(i).append('='); + sb.append(hostnames.get(i)).append(':').append( + calculatePort(i, portBase, clientFacingPortOffset)); + sb.append(','); + } + + sb.setLength(sb.length() - 1); + + return sb.toString(); + } + static int calculatePort(final int nodeId, final int portBase, final int offset) { return portBase + (nodeId * PORTS_PER_NODE) + offset;