Skip to content

Commit 394bc1e

Browse files
authored
Merge pull request #777 from rabbitmq/connection-allocation-argument
Add --connection-allocation argument
2 parents cdc32d2 + 842f588 commit 394bc1e

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

src/main/java/com/rabbitmq/perf/MulticastSet.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
1616
package com.rabbitmq.perf;
1717

18+
import static com.rabbitmq.perf.PerfTest.CONNECTION_ALLOCATION.RANDOM;
19+
import static com.rabbitmq.perf.PerfTest.CONNECTION_ALLOCATION.ROUND_ROBIN;
1820
import static com.rabbitmq.perf.Utils.isRecoverable;
1921
import static java.lang.Math.min;
2022
import static java.lang.String.format;
@@ -48,8 +50,10 @@
4850
import java.util.concurrent.TimeUnit;
4951
import java.util.concurrent.TimeoutException;
5052
import java.util.concurrent.atomic.AtomicBoolean;
53+
import java.util.concurrent.atomic.AtomicInteger;
5154
import java.util.function.Function;
5255
import java.util.function.Supplier;
56+
import java.util.function.UnaryOperator;
5357
import java.util.stream.IntStream;
5458
import org.slf4j.Logger;
5559
import org.slf4j.LoggerFactory;
@@ -102,7 +106,8 @@ public MulticastSet(
102106
completionHandler,
103107
new ShutdownService(),
104108
new ExpectedMetrics(params, new SimpleMeterRegistry(), "perftest_", Collections.emptyMap()),
105-
InstanceSynchronization.NO_OP);
109+
InstanceSynchronization.NO_OP,
110+
RANDOM);
106111
}
107112

108113
public MulticastSet(
@@ -114,7 +119,8 @@ public MulticastSet(
114119
CompletionHandler completionHandler,
115120
ShutdownService shutdownService,
116121
ExpectedMetrics expectedMetrics,
117-
InstanceSynchronization instanceSynchronization) {
122+
InstanceSynchronization instanceSynchronization,
123+
PerfTest.CONNECTION_ALLOCATION connectionAllocation) {
118124
this.performanceMetrics = performanceMetrics;
119125
this.factory = factory;
120126
this.params = params;
@@ -158,7 +164,7 @@ public MulticastSet(
158164
input -> Long.valueOf(input));
159165
}
160166

161-
this.connectionCreator = new ConnectionCreator(this.factory, this.uris);
167+
this.connectionCreator = new ConnectionCreator(this.factory, this.uris, connectionAllocation);
162168
this.expectedMetrics = expectedMetrics;
163169
this.instanceSynchronization = instanceSynchronization;
164170
}
@@ -952,12 +958,17 @@ private static class ConnectionCreator {
952958

953959
private final ConnectionFactory cf;
954960
private final List<Address> addresses;
961+
private final UnaryOperator<List<Address>> connectionAllocation;
955962

956-
private ConnectionCreator(ConnectionFactory cf, List<String> uris) {
963+
private ConnectionCreator(
964+
ConnectionFactory cf,
965+
List<String> uris,
966+
PerfTest.CONNECTION_ALLOCATION connectionAllocation) {
957967
this.cf = cf;
958968
if (uris == null || uris.isEmpty()) {
959969
// URI already set on the connection factory, nothing special to do
960970
addresses = Collections.emptyList();
971+
this.connectionAllocation = UnaryOperator.identity();
961972
} else {
962973
List<Address> addresses = new ArrayList<>(uris.size());
963974
for (String uri : uris) {
@@ -968,6 +979,26 @@ private ConnectionCreator(ConnectionFactory cf, List<String> uris) {
968979
}
969980
}
970981
this.addresses = Collections.unmodifiableList(addresses);
982+
if (connectionAllocation == RANDOM) {
983+
this.connectionAllocation =
984+
l -> {
985+
List<Address> addrs = new ArrayList<>(l);
986+
if (addresses.size() > 1) {
987+
Collections.shuffle(addrs);
988+
}
989+
return addrs;
990+
};
991+
} else if (connectionAllocation == ROUND_ROBIN) {
992+
AtomicInteger allocationCount = new AtomicInteger(0);
993+
this.connectionAllocation =
994+
l -> {
995+
Address addr = l.get(allocationCount.getAndIncrement() % l.size());
996+
return Collections.singletonList(addr);
997+
};
998+
} else {
999+
throw new IllegalArgumentException(
1000+
"Unknown connection allocation type: " + connectionAllocation.name());
1001+
}
9711002
}
9721003
}
9731004

@@ -984,11 +1015,7 @@ Connection createConnection(String name) throws IOException, TimeoutException {
9841015
if (this.addresses.isEmpty()) {
9851016
connection = this.cf.newConnection(name);
9861017
} else {
987-
List<Address> addrs = new ArrayList<>(addresses);
988-
if (addresses.size() > 1) {
989-
Collections.shuffle(addrs);
990-
}
991-
connection = this.cf.newConnection(addrs, name);
1018+
connection = this.cf.newConnection(this.connectionAllocation.apply(this.addresses), name);
9921019
}
9931020
addBlockedListener(connection);
9941021
return connection;

src/main/java/com/rabbitmq/perf/PerfTest.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,23 @@ public static void main(String[] args, PerfTestOptions perfTestOptions) {
242242

243243
MulticastParams p = multicastParams(cmd, uris, perfTestOptions);
244244

245+
String connectionAllocationParam =
246+
strArg(cmd, "cal", CONNECTION_ALLOCATION.RANDOM.allocation());
247+
CONNECTION_ALLOCATION connectionAllocation =
248+
Arrays.stream(CONNECTION_ALLOCATION.values())
249+
.filter(a -> a.allocation().equals(connectionAllocationParam))
250+
.findAny()
251+
.orElse(null);
252+
253+
validate(
254+
() -> connectionAllocation != null,
255+
"--connection-allocation must one of "
256+
+ Arrays.stream(CONNECTION_ALLOCATION.values())
257+
.map(CONNECTION_ALLOCATION::allocation)
258+
.collect(Collectors.joining(", ")),
259+
systemExiter,
260+
consoleErr);
261+
245262
ConcurrentMap<String, Integer> completionReasons = new ConcurrentHashMap<>();
246263

247264
MulticastSet.CompletionHandler completionHandler = getCompletionHandler(p, completionReasons);
@@ -344,7 +361,8 @@ public static void main(String[] args, PerfTestOptions perfTestOptions) {
344361
completionHandler,
345362
shutdownService,
346363
expectedMetrics,
347-
instanceSynchronization);
364+
instanceSynchronization,
365+
connectionAllocation);
348366
set.run(true);
349367

350368
statsSummary.run();
@@ -1364,6 +1382,13 @@ static Options getOptions() {
13641382

13651383
options.addOption(new Option("tnd", "tcp-no-delay", true, "value for TCP NODELAY option"));
13661384

1385+
options.addOption(
1386+
new Option(
1387+
"cal",
1388+
"connection-allocation",
1389+
true,
1390+
"the way to allocate connection across nodes (random or round-robin), default is random."));
1391+
13671392
return options;
13681393
}
13691394

@@ -1614,6 +1639,21 @@ enum EXIT_WHEN {
16141639
IDLE
16151640
}
16161641

1642+
enum CONNECTION_ALLOCATION {
1643+
RANDOM("random"),
1644+
ROUND_ROBIN("round-robin");
1645+
1646+
private final String allocation;
1647+
1648+
CONNECTION_ALLOCATION(String allocation) {
1649+
this.allocation = allocation;
1650+
}
1651+
1652+
public String allocation() {
1653+
return allocation;
1654+
}
1655+
}
1656+
16171657
private static ByteCapacity validateByteCapacity(
16181658
String value, SystemExiter exiter, PrintStream output) {
16191659
try {

0 commit comments

Comments
 (0)