Fixed - retryAttempt setting isn't applied during Redisson startup. #3256

pull/5186/head
Nikita Koksharov 2 years ago
parent 623225cd61
commit 8d6d7d32c4

@ -80,7 +80,7 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager {
}
@Override
public void connect() {
public void doConnect() {
if (cfg.getNodeAddresses().isEmpty()) {
throw new IllegalArgumentException("At least one cluster node should be defined!");
}

@ -216,7 +216,11 @@ public class ConfigSupport {
throw new IllegalArgumentException("server(s) address(es) not defined!");
}
if (!configCopy.isLazyInitialization()) {
cm.connect();
try {
cm.connect();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
return cm;
}

@ -31,7 +31,7 @@ import java.util.concurrent.TimeUnit;
*/
public interface ConnectionManager {
void connect();
void connect() throws InterruptedException;
PublishSubscribeService getSubscribeService();

@ -62,6 +62,8 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
protected final AtomicReference<CompletableFuture<Void>> lazyConnectLatch = new AtomicReference<>();
private boolean lastAttempt;
public MasterSlaveConnectionManager(BaseMasterSlaveServersConfig<?> cfg, ServiceManager serviceManager) {
this.serviceManager = serviceManager;
@ -170,6 +172,8 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
try {
connect();
f.complete(null);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
f.completeExceptionally(e);
lazyConnectLatch.set(null);
@ -177,7 +181,31 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
}
}
public void connect() {
@Override
public final void connect() throws InterruptedException {
for (int i = 0; i < config.getRetryAttempts(); i++) {
try {
if (i == config.getRetryAttempts() - 1) {
lastAttempt = true;
}
doConnect();
return;
} catch (Exception e) {
try {
Thread.sleep(config.getRetryInterval());
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
return;
}
if (i == config.getRetryAttempts() - 1) {
lastAttempt = false;
throw e;
}
}
}
}
protected void doConnect() {
try {
if (config.isSlaveNotUsed()) {
masterSlaveEntry = new SingleEntry(this, serviceManager.getConnectionWatcher(), config);
@ -420,7 +448,7 @@ public class MasterSlaveConnectionManager implements ConnectionManager {
}
protected void internalShutdown() {
if (lazyConnectLatch.get() == null) {
if (lazyConnectLatch.get() == null && lastAttempt) {
shutdown();
}
}

@ -72,7 +72,7 @@ public class ReplicatedConnectionManager extends MasterSlaveConnectionManager {
}
@Override
public void connect() {
public void doConnect() {
for (String address : cfg.getNodeAddresses()) {
RedisURI addr = new RedisURI(address);
CompletionStage<RedisConnection> connectionFuture = connectToNode(cfg, addr, addr.getHost());
@ -105,7 +105,7 @@ public class ReplicatedConnectionManager extends MasterSlaveConnectionManager {
log.warn("ReadMode = {}, but slave nodes are not found! Please specify all nodes in replicated mode.", this.config.getReadMode());
}
super.connect();
super.doConnect();
scheduleMasterChangeCheck(cfg);
}

@ -84,7 +84,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
}
@Override
public void connect() {
public void doConnect() {
checkAuth(cfg);
if ("redis".equals(scheme)) {
@ -202,7 +202,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
log.warn("ReadMode = {}, but slave nodes are not found!", this.config.getReadMode());
}
super.connect();
super.doConnect();
scheduleChangeCheck(cfg, null);
}

@ -4,6 +4,7 @@ import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.util.CharsetUtil;
import net.bytebuddy.utility.RandomString;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
@ -31,6 +32,7 @@ import org.redisson.connection.balancer.RandomLoadBalancer;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
@ -1237,12 +1239,12 @@ public class RedissonTest extends BaseTest {
@Test
public void testClusterConnectionFail() {
Assertions.assertThrows(RedisConnectionException.class, () -> {
Config config = new Config();
config.useClusterServers().addNodeAddress("redis://127.99.0.1:1111");
Redisson.create(config);
Thread.sleep(1500);
Awaitility.await().atLeast(Duration.ofSeconds(3)).atMost(Duration.ofSeconds(5)).untilAsserted(() -> {
Assertions.assertThrows(RedisConnectionException.class, () -> {
Config config = new Config();
config.useClusterServers().addNodeAddress("redis://127.99.0.1:1111");
Redisson.create(config);
});
});
}

Loading…
Cancel
Save