Skip to content

Commit 975ab78

Browse files
committed
- introduce forceActiveCluster by duration
- fix formatting
1 parent c39fda1 commit 975ab78

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/main/java/redis/clients/jedis/MultiClusterClientConfig.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ public MultiClusterClientConfig(ClusterConfig[] clusterConfigs) {
165165
if (clusterConfigs == null || clusterConfigs.length < 1) throw new JedisValidationException(
166166
"ClusterClientConfigs are required for MultiClusterPooledConnectionProvider");
167167
for (ClusterConfig clusterConfig : clusterConfigs) {
168-
if (clusterConfig == null) throw new IllegalArgumentException(
169-
"ClusterClientConfigs must not contain null elements");
168+
if (clusterConfig == null)
169+
throw new IllegalArgumentException("ClusterClientConfigs must not contain null elements");
170170
}
171171
this.clusterConfigs = clusterConfigs;
172172
}

src/main/java/redis/clients/jedis/providers/MultiClusterPooledConnectionProvider.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ private Cluster waitForInitialHealthyCluster() {
378378

379379
log.info("Selecting initial cluster from {} configured clusters", sortedClusters.size());
380380

381-
// Select clusters in weight order
381+
// Select cluster in weight order
382382
for (Map.Entry<Endpoint, Cluster> entry : sortedClusters) {
383383
Endpoint endpoint = entry.getKey();
384384
Cluster cluster = entry.getValue();
@@ -519,6 +519,21 @@ public void setActiveCluster(Endpoint endpoint) {
519519
setActiveCluster(cluster, true);
520520
}
521521

522+
public void forceActiveCluster(Endpoint endpoint, long forcedActiveDuration) {
523+
Cluster cluster = multiClusterMap.get(endpoint);
524+
cluster.clearGracePeriod();
525+
if (!cluster.isHealthy()) {
526+
throw new JedisValidationException("Provided endpoint: " + endpoint
527+
+ " is not healthy. Please consider a healthy endpoint from the configuration");
528+
}
529+
multiClusterMap.entrySet().stream().forEach(entry -> {
530+
if (entry.getKey() != endpoint) {
531+
entry.getValue().setGracePeriod(forcedActiveDuration);
532+
}
533+
});
534+
setActiveCluster(endpoint);
535+
}
536+
522537
private boolean setActiveCluster(Cluster cluster, boolean validateConnection) {
523538
// Cluster cluster = clusterEntry.getValue();
524539
// Field-level synchronization is used to avoid the edge case in which
@@ -710,7 +725,17 @@ public boolean isInGracePeriod() {
710725
* Sets the grace period for this cluster
711726
*/
712727
public void setGracePeriod() {
713-
gracePeriodEndsAt = System.currentTimeMillis() + multiClusterClientConfig.getGracePeriod();
728+
setGracePeriod(multiClusterClientConfig.getGracePeriod());
729+
}
730+
731+
public void setGracePeriod(long gracePeriod) {
732+
long endTime = System.currentTimeMillis() + gracePeriod;
733+
if (endTime < gracePeriodEndsAt) return;
734+
gracePeriodEndsAt = endTime;
735+
}
736+
737+
public void clearGracePeriod() {
738+
gracePeriodEndsAt = 0;
714739
}
715740

716741
/**

0 commit comments

Comments
 (0)