diff --git a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java index e4b761f25..db4a8dacf 100644 --- a/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java +++ b/redisson/src/main/java/org/redisson/client/protocol/RedisCommands.java @@ -26,7 +26,6 @@ import org.redisson.client.protocol.decoder.*; import org.redisson.client.protocol.pubsub.PubSubStatusDecoder; import org.redisson.cluster.ClusterNodeInfo; import org.redisson.codec.CompositeCodec; -import org.redisson.misc.RedisURI; import java.time.Duration; import java.time.Instant; @@ -762,10 +761,6 @@ public interface RedisCommands { RedisStrictCommand SENTINEL_REMOVE = new RedisStrictCommand("SENTINEL", "REMOVE", new VoidReplayConvertor()); RedisStrictCommand SENTINEL_MONITOR = new RedisStrictCommand("SENTINEL", "MONITOR", new VoidReplayConvertor()); - RedisStrictCommand SENTINEL_GET_MASTER_ADDR_BY_NAME = new RedisStrictCommand<>("SENTINEL", "GET-MASTER-ADDR-BY-NAME", - new RedisURIDecoder(false)); - RedisStrictCommand SENTINEL_GET_MASTER_ADDR_BY_NAME_SSL = new RedisStrictCommand<>("SENTINEL", "GET-MASTER-ADDR-BY-NAME", - new RedisURIDecoder(true)); RedisCommand>> SENTINEL_MASTERS = new RedisCommand>>("SENTINEL", "MASTERS", new ListMultiDecoder2(new ListResultReplayDecoder(), new ObjectMapReplayDecoder())); RedisCommand> SENTINEL_MASTER = new RedisCommand("SENTINEL", "MASTER", new ObjectMapReplayDecoder()); diff --git a/redisson/src/main/java/org/redisson/client/protocol/decoder/RedisURIDecoder.java b/redisson/src/main/java/org/redisson/client/protocol/decoder/RedisURIDecoder.java index b39978588..ac1be32fa 100644 --- a/redisson/src/main/java/org/redisson/client/protocol/decoder/RedisURIDecoder.java +++ b/redisson/src/main/java/org/redisson/client/protocol/decoder/RedisURIDecoder.java @@ -32,13 +32,8 @@ public class RedisURIDecoder implements MultiDecoder { private final String scheme; - public RedisURIDecoder(boolean ssl) { - super(); - if (ssl) { - scheme = "rediss"; - } else { - scheme = "redis"; - } + public RedisURIDecoder(String scheme) { + this.scheme = scheme; } @Override diff --git a/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java b/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java index 54a1fe4da..a8fbc9238 100755 --- a/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java +++ b/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java @@ -24,6 +24,7 @@ import org.redisson.client.*; import org.redisson.client.codec.StringCodec; import org.redisson.client.protocol.RedisCommands; import org.redisson.client.protocol.RedisStrictCommand; +import org.redisson.client.protocol.decoder.RedisURIDecoder; import org.redisson.config.*; import org.redisson.misc.RedisURI; import org.slf4j.Logger; @@ -72,18 +73,15 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager { sentinelHosts.add(addr); } } + + masterHostCommand = new RedisStrictCommand<>("SENTINEL", "GET-MASTER-ADDR-BY-NAME", + new RedisURIDecoder(scheme)); } @Override public void doConnect(Function hostnameMapper) { checkAuth(cfg); - if ("redis".equals(scheme)) { - masterHostCommand = RedisCommands.SENTINEL_GET_MASTER_ADDR_BY_NAME; - } else { - masterHostCommand = RedisCommands.SENTINEL_GET_MASTER_ADDR_BY_NAME_SSL; - } - Map uri2hostname = new HashMap<>(); Throwable lastException = null; for (String address : cfg.getSentinelAddresses()) { diff --git a/redisson/src/main/java/org/redisson/redisnode/SentinelRedisNode.java b/redisson/src/main/java/org/redisson/redisnode/SentinelRedisNode.java index 47a2aa503..3eba300c0 100644 --- a/redisson/src/main/java/org/redisson/redisnode/SentinelRedisNode.java +++ b/redisson/src/main/java/org/redisson/redisnode/SentinelRedisNode.java @@ -25,7 +25,9 @@ import org.redisson.client.codec.LongCodec; import org.redisson.client.codec.StringCodec; import org.redisson.client.protocol.RedisCommand; import org.redisson.client.protocol.RedisCommands; +import org.redisson.client.protocol.RedisStrictCommand; import org.redisson.client.protocol.Time; +import org.redisson.client.protocol.decoder.RedisURIDecoder; import org.redisson.command.CommandAsyncExecutor; import org.redisson.misc.CompletableFutureWrapper; import org.redisson.misc.RedisURI; @@ -223,7 +225,10 @@ public class SentinelRedisNode implements RedisSentinel, RedisSentinelAsync { @Override public RFuture getMasterAddrAsync(String masterName) { - return executeAsync(null, StringCodec.INSTANCE, -1, RedisCommands.SENTINEL_GET_MASTER_ADDR_BY_NAME, masterName); + RedisStrictCommand masterHostCommand = new RedisStrictCommand<>("SENTINEL", "GET-MASTER-ADDR-BY-NAME", + new RedisURIDecoder(client.getConfig().getAddress().getScheme())); + + return executeAsync(null, StringCodec.INSTANCE, -1, masterHostCommand, masterName); } @Override diff --git a/redisson/src/test/java/org/redisson/RedisDockerTest.java b/redisson/src/test/java/org/redisson/RedisDockerTest.java index a5f7b56a0..525db392e 100644 --- a/redisson/src/test/java/org/redisson/RedisDockerTest.java +++ b/redisson/src/test/java/org/redisson/RedisDockerTest.java @@ -5,11 +5,17 @@ import com.github.dockerjava.api.model.ContainerNetwork; import com.github.dockerjava.api.model.ExposedPort; import com.github.dockerjava.api.model.PortBinding; import com.github.dockerjava.api.model.Ports; +import io.netty.channel.socket.DatagramChannel; +import io.netty.channel.socket.SocketChannel; +import io.netty.resolver.AddressResolverGroup; +import io.netty.resolver.dns.DnsServerAddressStreamProvider; +import io.netty.resolver.dns.DnsServerAddresses; import org.junit.jupiter.api.BeforeEach; import org.redisson.api.NatMapper; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.redisson.config.Protocol; +import org.redisson.connection.SequentialDnsAddressResolverFactory; import org.redisson.misc.RedisURI; import org.testcontainers.containers.*; import org.testcontainers.containers.startupcheck.MinimumDurationRunningStartupCheckStrategy; @@ -17,6 +23,7 @@ import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; import java.io.File; import java.io.IOException; +import java.net.InetSocketAddress; import java.time.Duration; import java.util.*; import java.util.function.BiConsumer; @@ -154,6 +161,8 @@ public class RedisDockerTest { protected void withSentinel(BiConsumer>, Config> callback, int slaves) throws InterruptedException { Network network = Network.newNetwork(); + SimpleDnsServer dnsServer = new SimpleDnsServer(); + List>> nodes = new ArrayList<>(); GenericContainer master = @@ -245,6 +254,16 @@ public class RedisDockerTest { Config config = new Config(); config.setProtocol(protocol); + + config.setAddressResolverGroupFactory(new SequentialDnsAddressResolverFactory() { + @Override + public AddressResolverGroup create(Class channelType, Class socketChannelType, DnsServerAddressStreamProvider nameServerProvider) { + return super.create(channelType, socketChannelType, hostname -> { + return DnsServerAddresses.singleton(dnsServer.getAddr()).stream(); + }); + } + }); + config.useSentinelServers() .setPingConnectionInterval(0) .setNatMapper(new NatMapper() { @@ -284,11 +303,14 @@ public class RedisDockerTest { nodes.forEach(n -> n.stop()); network.close(); + dnsServer.stop(); } protected void withSentinel(BiConsumer>, Config> callback, int slaves, String password) throws InterruptedException { Network network = Network.newNetwork(); + SimpleDnsServer dnsServer = new SimpleDnsServer(); + List>> nodes = new ArrayList<>(); GenericContainer master = @@ -387,12 +409,23 @@ public class RedisDockerTest { Config config = new Config(); config.setProtocol(protocol); + + config.setAddressResolverGroupFactory(new SequentialDnsAddressResolverFactory() { + @Override + public AddressResolverGroup create(Class channelType, Class socketChannelType, DnsServerAddressStreamProvider nameServerProvider) { + return super.create(channelType, socketChannelType, hostname -> { + return DnsServerAddresses.singleton(dnsServer.getAddr()).stream(); + }); + } + }); + config.useSentinelServers() .setPassword(password) .setNatMapper(new NatMapper() { @Override public RedisURI map(RedisURI uri) { + for (GenericContainer> node : nodes) { if (node.getContainerInfo() == null) { continue; @@ -426,6 +459,7 @@ public class RedisDockerTest { nodes.forEach(n -> n.stop()); network.close(); + dnsServer.stop(); } protected void withNewCluster(BiConsumer, RedissonClient> callback) { diff --git a/redisson/src/test/java/org/redisson/RedissonFailoverTest.java b/redisson/src/test/java/org/redisson/RedissonFailoverTest.java index 87e17a994..c9150082f 100644 --- a/redisson/src/test/java/org/redisson/RedissonFailoverTest.java +++ b/redisson/src/test/java/org/redisson/RedissonFailoverTest.java @@ -254,7 +254,7 @@ public class RedissonFailoverTest extends RedisDockerTest { assertThat(futures.get(futures.size() - 1).isDone()).isTrue(); assertThat(futures.get(futures.size() - 1).toCompletableFuture().isCompletedExceptionally()).isFalse(); - assertThat(errors).isBetween(30, 800); + assertThat(errors).isBetween(30, 1900); assertThat(readonlyErrors).isZero(); redisson.shutdown();