-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connect asynchronously in cluster topology refresh #424
Switch from blocking to asynchronous connection initialization when connecting Redis Cluster nodes for topology refresh. Asynchronous connection allows connecting concurrently.
- Loading branch information
Showing
7 changed files
with
226 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/com/lambdaworks/redis/cluster/topology/AsyncConnections.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.lambdaworks.redis.cluster.topology; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import com.lambdaworks.redis.RedisConnectionException; | ||
import com.lambdaworks.redis.RedisURI; | ||
import com.lambdaworks.redis.api.StatefulRedisConnection; | ||
|
||
/** | ||
* @author Mark Paluch | ||
*/ | ||
class AsyncConnections { | ||
|
||
private final Map<RedisURI, CompletableFuture<StatefulRedisConnection<String, String>>> futures = new TreeMap<>( | ||
TopologyComparators.RedisURIComparator.INSTANCE); | ||
|
||
public AsyncConnections() { | ||
} | ||
|
||
/** | ||
* Add a connection for a {@link RedisURI} | ||
* | ||
* @param redisURI | ||
* @param connection | ||
*/ | ||
public void addConnection(RedisURI redisURI, CompletableFuture<StatefulRedisConnection<String, String>> connection) { | ||
futures.put(redisURI, connection); | ||
} | ||
|
||
/** | ||
* | ||
* @return a set of {@link RedisURI} for which {@link Connections} has a connection. | ||
*/ | ||
public Set<RedisURI> connectedNodes() { | ||
return futures.keySet(); | ||
} | ||
|
||
/** | ||
* @return the {@link Connections}. | ||
* @throws RedisConnectionException if no connection could be established. | ||
*/ | ||
public Connections get(long timeout, TimeUnit timeUnit) throws InterruptedException { | ||
|
||
Connections connections = new Connections(); | ||
List<Future<?>> sync = new ArrayList<>(this.futures.size()); | ||
|
||
for (Map.Entry<RedisURI, CompletableFuture<StatefulRedisConnection<String, String>>> entry : this.futures.entrySet()) { | ||
|
||
CompletableFuture<StatefulRedisConnection<String, String>> future = entry.getValue(); | ||
|
||
sync.add(future.thenAccept((connection) -> { | ||
connections.addConnection(entry.getKey(), connection); | ||
})); | ||
} | ||
|
||
RefreshFutures.awaitAll(timeout, timeUnit, sync); | ||
|
||
return connections; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/com/lambdaworks/redis/cluster/topology/RefreshFutures.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.lambdaworks.redis.cluster.topology; | ||
|
||
import java.util.Collection; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @author Mark Paluch | ||
*/ | ||
class RefreshFutures { | ||
|
||
static long awaitAll(long timeout, TimeUnit timeUnit, Collection<? extends Future<?>> futures) throws InterruptedException { | ||
|
||
long waitTime = 0; | ||
|
||
for (Future<?> future : futures) { | ||
|
||
long timeoutLeft = timeUnit.toNanos(timeout) - waitTime; | ||
|
||
if (timeoutLeft <= 0) { | ||
break; | ||
} | ||
|
||
long startWait = System.nanoTime(); | ||
|
||
try { | ||
future.get(timeoutLeft, TimeUnit.NANOSECONDS); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
throw e; | ||
} catch (Exception e) { | ||
continue; | ||
} finally { | ||
waitTime += System.nanoTime() - startWait; | ||
} | ||
|
||
} | ||
return waitTime; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.