Skip to content

Commit

Permalink
Polishing #721
Browse files Browse the repository at this point in the history
Adapt to async connection handling. Add author tags. Add test.

Original pull request: #722
  • Loading branch information
mp911de committed Mar 6, 2018
1 parent 49c83a5 commit 3b72614
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 34 deletions.
37 changes: 20 additions & 17 deletions src/main/java/io/lettuce/core/cluster/topology/Connections.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@
*/
package io.lettuce.core.cluster.topology;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.*;
import java.util.concurrent.CompletableFuture;

import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
Expand All @@ -31,6 +29,7 @@

/**
* @author Mark Paluch
* @author Christian Weitendorf
*/
class Connections {

Expand All @@ -53,28 +52,28 @@ private Connections(Map<RedisURI, StatefulRedisConnection<String, String>> conne
*/
public void addConnection(RedisURI redisURI, StatefulRedisConnection<String, String> connection) {

if (closed) { // fastpath
connection.close();
if (this.closed) { // fastpath
connection.closeAsync();
return;
}

synchronized (connections) {
synchronized (this.connections) {

if (closed) {
connection.close();
if (this.closed) {
connection.closeAsync();
return;
}

connections.put(redisURI, connection);
this.connections.put(redisURI, connection);
}
}

/**
* @return {@literal true} if no connections present.
*/
public boolean isEmpty() {
synchronized (connections) {
return connections.isEmpty();
synchronized (this.connections) {
return this.connections.isEmpty();
}
}

Expand All @@ -87,7 +86,7 @@ public Requests requestTopology() {

Requests requests = new Requests();

for (Map.Entry<RedisURI, StatefulRedisConnection<String, String>> entry : connections.entrySet()) {
for (Map.Entry<RedisURI, StatefulRedisConnection<String, String>> entry : this.connections.entrySet()) {

CommandArgs<String, String> args = new CommandArgs<>(StringCodec.UTF8).add(CommandKeyword.NODES);
Command<String, String, String> command = new Command<>(CommandType.CLUSTER, new StatusOutput<>(StringCodec.UTF8),
Expand All @@ -110,7 +109,7 @@ public Requests requestClients() {

Requests requests = new Requests();

for (Map.Entry<RedisURI, StatefulRedisConnection<String, String>> entry : connections.entrySet()) {
for (Map.Entry<RedisURI, StatefulRedisConnection<String, String>> entry : this.connections.entrySet()) {

CommandArgs<String, String> args = new CommandArgs<>(StringCodec.UTF8).add(CommandKeyword.LIST);
Command<String, String, String> command = new Command<>(CommandType.CLIENT, new StatusOutput<>(StringCodec.UTF8),
Expand All @@ -131,23 +130,27 @@ public void close() {

this.closed = true;

List<CompletableFuture<?>> closeFutures = new ArrayList<>();
while (hasConnections()) {

for (StatefulRedisConnection<String, String> connection : drainConnections()) {
connection.closeAsync();
closeFutures.add(connection.closeAsync());
}
}

CompletableFuture.allOf(closeFutures.toArray(new CompletableFuture[0])).join();
}

private boolean hasConnections() {
synchronized (connections) {

synchronized (this.connections) {
return !this.connections.isEmpty();
}
}

private Collection<StatefulRedisConnection<String, String>> drainConnections() {

Map<RedisURI, StatefulRedisConnection<String, String>> drainedConnections;

synchronized (this.connections) {

drainedConnections = new HashMap<>(this.connections);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@

/**
* @author Mark Paluch
* @author Christian Weitendorf
*/
@SuppressWarnings("unchecked")
@RunWith(MockitoJUnitRunner.class)
public class ClusterTopologyRefreshTest {

Expand Down Expand Up @@ -96,6 +98,8 @@ public void before() {
when(clientResources.dnsResolver()).thenReturn(DnsResolvers.JVM_DEFAULT);
when(connection1.async()).thenReturn(asyncCommands1);
when(connection2.async()).thenReturn(asyncCommands2);
when(connection1.closeAsync()).thenReturn(CompletableFuture.completedFuture(null));
when(connection2.closeAsync()).thenReturn(CompletableFuture.completedFuture(null));

when(connection1.dispatch(any(RedisCommand.class))).thenAnswer(invocation -> {

Expand Down Expand Up @@ -277,7 +281,7 @@ public void shouldFailIfNoNodeConnects() {
@Test
public void shouldShouldDiscoverNodes() {

List<RedisURI> seed = Arrays.asList(RedisURI.create("127.0.0.1", 7380));
List<RedisURI> seed = Collections.singletonList(RedisURI.create("127.0.0.1", 7380));

when(nodeConnectionFactory.connectToNodeAsync(any(RedisCodec.class), eq(new InetSocketAddress("127.0.0.1", 7380))))
.thenReturn(completedFuture((StatefulRedisConnection) connection1));
Expand All @@ -293,7 +297,7 @@ public void shouldShouldDiscoverNodes() {
@Test
public void shouldShouldNotDiscoverNodes() {

List<RedisURI> seed = Arrays.asList(RedisURI.create("127.0.0.1", 7380));
List<RedisURI> seed = Collections.singletonList(RedisURI.create("127.0.0.1", 7380));

when(nodeConnectionFactory.connectToNodeAsync(any(RedisCodec.class), eq(new InetSocketAddress("127.0.0.1", 7380))))
.thenReturn(completedFuture((StatefulRedisConnection) connection1));
Expand Down Expand Up @@ -334,14 +338,14 @@ public void shouldCloseConnections() {

sut.loadViews(seed, true);

verify(connection1).close();
verify(connection2).close();
verify(connection1).closeAsync();
verify(connection2).closeAsync();
}

@Test
public void undiscoveredAdditionalNodesShouldBeLastUsingClientCount() {

List<RedisURI> seed = Arrays.asList(RedisURI.create("127.0.0.1", 7380));
List<RedisURI> seed = Collections.singletonList(RedisURI.create("127.0.0.1", 7380));

when(nodeConnectionFactory.connectToNodeAsync(any(RedisCodec.class), eq(new InetSocketAddress("127.0.0.1", 7380))))
.thenReturn(completedFuture((StatefulRedisConnection) connection1));
Expand All @@ -359,7 +363,7 @@ public void undiscoveredAdditionalNodesShouldBeLastUsingClientCount() {
@Test
public void discoveredAdditionalNodesShouldBeOrderedUsingClientCount() {

List<RedisURI> seed = Arrays.asList(RedisURI.create("127.0.0.1", 7380));
List<RedisURI> seed = Collections.singletonList(RedisURI.create("127.0.0.1", 7380));

when(nodeConnectionFactory.connectToNodeAsync(any(RedisCodec.class), eq(new InetSocketAddress("127.0.0.1", 7380))))
.thenReturn(completedFuture((StatefulRedisConnection) connection1));
Expand All @@ -379,7 +383,7 @@ public void discoveredAdditionalNodesShouldBeOrderedUsingClientCount() {
@Test
public void undiscoveredAdditionalNodesShouldBeLastUsingLatency() {

List<RedisURI> seed = Arrays.asList(RedisURI.create("127.0.0.1", 7380));
List<RedisURI> seed = Collections.singletonList(RedisURI.create("127.0.0.1", 7380));

when(nodeConnectionFactory.connectToNodeAsync(any(RedisCodec.class), eq(new InetSocketAddress("127.0.0.1", 7380))))
.thenReturn(completedFuture((StatefulRedisConnection) connection1));
Expand All @@ -397,7 +401,7 @@ public void undiscoveredAdditionalNodesShouldBeLastUsingLatency() {
@Test
public void discoveredAdditionalNodesShouldBeOrderedUsingLatency() {

List<RedisURI> seed = Arrays.asList(RedisURI.create("127.0.0.1", 7380));
List<RedisURI> seed = Collections.singletonList(RedisURI.create("127.0.0.1", 7380));

when(nodeConnectionFactory.connectToNodeAsync(any(RedisCodec.class), eq(new InetSocketAddress("127.0.0.1", 7380))))
.thenReturn(completedFuture((StatefulRedisConnection) connection1));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
/*
* Copyright 2018 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 io.lettuce.core.cluster.topology;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import java.util.concurrent.CompletableFuture;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;

/**
* @author Christian Weitendorf
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class ConnectionsTest {

Expand All @@ -18,15 +43,34 @@ public class ConnectionsTest {
@Mock
private StatefulRedisConnection<String, String> connection2;

@Before
public void before() throws Exception {

when(connection1.closeAsync()).thenReturn(CompletableFuture.completedFuture(null));
when(connection2.closeAsync()).thenReturn(CompletableFuture.completedFuture(null));
}

@Test
public void shouldCloseAllConnections() {
final Connections iut = new Connections();
iut.addConnection(RedisURI.create("127.0.0.1", 7380), connection1);
iut.addConnection(RedisURI.create("127.0.0.1", 7381), connection2);

iut.close();
Connections sut = new Connections();
sut.addConnection(RedisURI.create("127.0.0.1", 7380), connection1);
sut.addConnection(RedisURI.create("127.0.0.1", 7381), connection2);

sut.close();

verify(connection1).closeAsync();
verify(connection2).closeAsync();
}

@Test
public void shouldCloseAllConnectionsAfterCloseSignal() {

Connections sut = new Connections();
sut.close();
verifyZeroInteractions(connection1);

verify(connection1).close();
verify(connection2).close();
sut.addConnection(RedisURI.create("127.0.0.1", 7381), connection1);
verify(connection1).closeAsync();
}
}
}

0 comments on commit 3b72614

Please sign in to comment.