Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Randomized connection order of servers #391

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collections.unmodifiableSet(...) instead of new HashSet<>(asList(...))?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collections.unmodifiableSet(...) expects a Set, so that would change it to Collections.unmodifiableSet(new HashSet<>(asList(...).

new ServerAddress("server2"),
new ServerAddress("server3"))),
new HashSet<>(connectAttempts));

axonServerProxy.enable();

Expand Down
Loading