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

Allow setHostWeight() to work before cluster is started. #27

Merged
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 @@ -45,6 +45,8 @@ public class AuroraClusterMonitor {
protected static final int MAXIMUM_THREAD_POOL_SIZE = 64;
protected static final SchedulerService MONITOR_SCHEDULER;
protected static final ConcurrentMap<AuroraServersKey, AuroraClusterMonitor> MONITORS;
private static final ConcurrentMap<AuroraServer, Integer> preConfiguredWeights =
new ConcurrentHashMap<>();
private static volatile long checkFrequencyMillis = 500;
private static volatile BiConsumer<AuroraServer, Throwable> errorExceptionHandler;

Expand Down Expand Up @@ -101,7 +103,10 @@ public static void setExceptionHandler(BiConsumer<AuroraServer, Throwable> handl
* When a weight higher than the default of {@code 1} is used, it is in effect as if that server
* was in the cluster multiple times. For example a cluster with a server X of weight 1, and
* server Y of weight 2, would send 2/3's of the requests to server Y.
*
*
* <p>This call immediately applies to all initialised clusters, and will also apply to clusters
* initialised after.
*
* @param host The host to update against
* @param port The port for the replica server
* @param weight The weight to apply to this replica
Expand All @@ -111,17 +116,12 @@ public static void setHostWeight(String host, int port, int weight) {
throw new IllegalArgumentException("Negative server weights not allowed");
} else if (weight > 100) {
throw new IllegalArgumentException("Maximum allowed weight is 100");
} else if (MONITORS.isEmpty()) {
throw new IllegalStateException("No aurora clusters monitored, make sure database is setup");
}

boolean updated = false;

for (AuroraClusterMonitor acm : MONITORS.values()) {
updated |= acm.clusterChecker.queueReplicaWeightUpdate(host, port, weight);
}
if (! updated) {
throw new IllegalStateException("Could not find server: " + host + ":" + port);
acm.clusterChecker.queueReplicaWeightUpdate(host, port, weight);
}
preConfiguredWeights.put(new AuroraServer(host, port, null), weight);
}

/**
Expand All @@ -134,18 +134,18 @@ public static void setHostWeight(String host, int port, int weight) {
*/
protected static AuroraClusterMonitor getMonitor(DelegateAuroraDriver driver, AuroraServer[] servers) {
// the implementation of `AbstractSet`'s equals will verify the size, followed by `containsAll`.
// Since order and other variations should not impact the lookup we just store the provided set
// Since order and other variations should not impact the lookup we just store the provided set
// into the map directly for efficiency when the same set is provided multiple times

AuroraServersKey mapKey = new AuroraServersKey(servers);
AuroraClusterMonitor result = MONITORS.get(mapKey);
if (result != null) {
return result;
}

return MONITORS.computeIfAbsent(mapKey,
(s) -> new AuroraClusterMonitor(MONITOR_SCHEDULER,
checkFrequencyMillis,
checkFrequencyMillis,
driver, mapKey.clusterServers));
}

Expand Down Expand Up @@ -364,6 +364,11 @@ protected ClusterChecker(SchedulerService scheduler, long checkIntervalMillis,
pendingServerWeightUpdates = new ConcurrentHashMap<>();

for (AuroraServer server : clusterServers) {
Integer preConfiguredWeight = preConfiguredWeights.get(server);
if (null != preConfiguredWeight) {
server.setWeight(preConfiguredWeight);
}

ServerMonitor monitor = new ServerMonitor(scheduler, driver, server, this);
allServers.put(server, monitor);

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = org.threadly
version = 0.14
version = 0.15-SNAPSHOT
org.gradle.parallel = false

threadlyVersion = 6.2
Expand Down