-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[automatic-failover] Implement weighted endpoint selection #3519
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
base: feature/automatic-failover-1
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package io.lettuce.core.failover; | ||
|
|
||
| public enum HealthStatus { | ||
| UNKNOWN, HEALTHY, UNHEALTHY | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,7 +70,7 @@ public StatefulRedisMultiDbConnectionImpl(Map<RedisURI, RedisDatabase<C>> connec | |
| this.codec = codec; | ||
| this.parser = parser; | ||
| this.connectionFactory = connectionFactory; | ||
| this.current = connections.values().stream().max(Comparator.comparingDouble(RedisDatabase::getWeight)).get(); | ||
| this.current = getNextHealthyDatabase(null); | ||
atakavci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| this.async = newRedisAsyncCommandsImpl(); | ||
| this.sync = newRedisSyncCommandsImpl(); | ||
|
|
@@ -86,7 +86,7 @@ private void onCircuitBreakerStateChange(CircuitBreakerStateChangeEvent event) { | |
| } | ||
|
|
||
| private void failoverFrom(RedisDatabase<C> fromDb) { | ||
| RedisDatabase<C> healthyDatabase = getHealthyDatabase(fromDb); | ||
| RedisDatabase<C> healthyDatabase = getNextHealthyDatabase(fromDb); | ||
| if (healthyDatabase != null) { | ||
| switchToDatabase(healthyDatabase.getRedisURI()); | ||
| } else { | ||
|
|
@@ -95,10 +95,9 @@ private void failoverFrom(RedisDatabase<C> fromDb) { | |
| } | ||
| } | ||
|
|
||
| private RedisDatabase<C> getHealthyDatabase(RedisDatabase<C> current) { | ||
| return databases.values().stream().filter(db -> db != current) | ||
| .filter(db -> db.getCircuitBreaker().getCurrentState() == CircuitBreaker.State.CLOSED) | ||
| .max(Comparator.comparingDouble(RedisDatabase::getWeight)).get(); | ||
| private RedisDatabase<C> getNextHealthyDatabase(RedisDatabase<C> current) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to confirm my understanding getHealthStatus() reflects the state of database only based on HealthChecks that will be introduced(not considering the state of CB for given DB)? In that case where do we ensure that failoverFrom() is not failing over toward DB with CB already in OPEN_STATE? |
||
| return databases.values().stream().filter(db -> db.getHealthStatus() == HealthStatus.HEALTHY) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Improve readability,I suggest something like : private RedisDatabase<C> getNextHealthyDatabase(RedisDatabase<C> current) {
return databases.values().stream()
.filter(isHealthy)
.filter(isNotCurrent(current))
.max(DatabaseComparators.byWeight).orElse(null);
}
static class DatabaseComparators {
public static final Comparator<RedisDatabase<?>> byWeight = Comparator.comparingDouble(RedisDatabase::getWeight);
}
static class DatabasePredicates {
public static final Predicate<RedisDatabase<?>> isHealthy = db -> db.getHealthStatus() == HealthStatus.HEALTHY;
public static Predicate<RedisDatabase<?>> isNotCurrent( RedisDatabase<?> current) {
return db -> !db.equals(current);
}
} |
||
| .filter(db -> !db.equals(current)).max(Comparator.comparingDouble(RedisDatabase::getWeight)).orElse(null); | ||
atakavci marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Override | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume UNKNOWN status is not used as of now, but is intended to be used once we have actual health checks as the initial state, till we get a result from the first health check?