Skip to content

Commit

Permalink
Randomized connection order of servers
Browse files Browse the repository at this point in the history
Randomizing the order in which servers are used for connections prevents a misbehaving (e.g. out of sync) first candidate to prevent the application from connecting via other instances in the cluster.

(cherry picked from commit 5e1d0d7)
  • Loading branch information
abuijze authored and smcvb committed Nov 6, 2024
1 parent 7fe8aeb commit 6f7864f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -102,7 +104,12 @@ public AxonServerManagedChannel(List<ServerAddress> routingServers,

private ManagedChannel connectChannel() {
ManagedChannel connection = null;
for (ServerAddress nodeInfo : routingServers) {

// reorder the addresses to avoid a misbehaving platform server to prevent connections
List<ServerAddress> routingCandidates = new ArrayList<>(routingServers);
Collections.shuffle(routingCandidates, ThreadLocalRandom.current());

for (ServerAddress nodeInfo : routingCandidates) {
ManagedChannel candidate = null;
try {
candidate = connectionFactory.apply(nodeInfo, context);
Expand Down Expand Up @@ -214,7 +221,8 @@ public <REQ, RESP> ClientCall<REQ, RESP> newCall(MethodDescriptor<REQ, RESP> met

@Override
public String authority() {
return routingServers.get(0).toString();
ManagedChannel activeChannel = this.activeChannel.get();
return activeChannel != null ? activeChannel.authority() : routingServers.get(0).toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -203,10 +204,11 @@ void connectionRecoveredOnDisconnection() throws Exception {

nextConnectTask.run();

assertEquals(asList(new ServerAddress("server1"),
new ServerAddress("server2"),
new ServerAddress("server3")),
connectAttempts);
// we don't care in which order the connects happened, as long as all hosts have been tried
assertEquals(new HashSet<>(asList(new ServerAddress("server1"),
new ServerAddress("server2"),
new ServerAddress("server3"))),
new HashSet<>(connectAttempts));

axonServerProxy.enable();

Expand Down

0 comments on commit 6f7864f

Please sign in to comment.