15151616package 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 ;
1820import static com .rabbitmq .perf .Utils .isRecoverable ;
1921import static java .lang .Math .min ;
2022import static java .lang .String .format ;
4850import java .util .concurrent .TimeUnit ;
4951import java .util .concurrent .TimeoutException ;
5052import java .util .concurrent .atomic .AtomicBoolean ;
53+ import java .util .concurrent .atomic .AtomicInteger ;
5154import java .util .function .Function ;
5255import java .util .function .Supplier ;
56+ import java .util .function .UnaryOperator ;
5357import java .util .stream .IntStream ;
5458import org .slf4j .Logger ;
5559import 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 ;
0 commit comments