From 1319607059694e22539c95bdbe517def297b389e Mon Sep 17 00:00:00 2001 From: Nikita Date: Tue, 13 Dec 2016 20:27:23 +0300 Subject: [PATCH] Fixed - Config can not handle underscore in host #722 --- .../java/org/redisson/client/RedisClient.java | 10 +-- .../cluster/ClusterConnectionManager.java | 54 +++++------ .../org/redisson/cluster/ClusterNodeInfo.java | 9 +- .../redisson/cluster/ClusterPartition.java | 33 ++++--- .../redisson/config/ClusterServersConfig.java | 17 ++-- .../org/redisson/config/ConfigSupport.java | 10 +++ .../config/ElasticacheServersConfig.java | 13 +-- .../config/MasterSlaveServersConfig.java | 33 +++---- .../config/SentinelServersConfig.java | 17 ++-- .../redisson/config/SingleServerConfig.java | 18 ++-- .../connection/ConnectionManager.java | 4 +- .../ElasticacheConnectionManager.java | 14 +-- .../MasterSlaveConnectionManager.java | 6 +- .../redisson/connection/MasterSlaveEntry.java | 7 +- .../connection/SentinelConnectionManager.java | 24 ++--- .../balancer/WeightedRoundRobinBalancer.java | 6 +- .../java/org/redisson/misc/URIBuilder.java | 37 -------- .../java/org/redisson/misc/URLBuilder.java | 89 +++++++++++++++++++ .../test/java/org/redisson/RedissonTest.java | 26 +++++- .../decoder/ClusterNodesDecoderTest.java | 1 + 20 files changed, 263 insertions(+), 165 deletions(-) delete mode 100644 redisson/src/main/java/org/redisson/misc/URIBuilder.java create mode 100644 redisson/src/main/java/org/redisson/misc/URLBuilder.java diff --git a/redisson/src/main/java/org/redisson/client/RedisClient.java b/redisson/src/main/java/org/redisson/client/RedisClient.java index 9d48878ac1a..60310e74b3f 100644 --- a/redisson/src/main/java/org/redisson/client/RedisClient.java +++ b/redisson/src/main/java/org/redisson/client/RedisClient.java @@ -16,7 +16,7 @@ package org.redisson.client; import java.net.InetSocketAddress; -import java.net.URI; +import java.net.URL; import java.util.Map; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -31,7 +31,7 @@ import org.redisson.client.protocol.RedisCommands; import org.redisson.misc.RPromise; import org.redisson.misc.RedissonPromise; -import org.redisson.misc.URIBuilder; +import org.redisson.misc.URLBuilder; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; @@ -70,15 +70,15 @@ public class RedisClient { private boolean hasOwnGroup; public RedisClient(String address) { - this(URIBuilder.create(address)); + this(URLBuilder.create(address)); } - public RedisClient(URI address) { + public RedisClient(URL address) { this(new HashedWheelTimer(), Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2), new NioEventLoopGroup(), address); hasOwnGroup = true; } - public RedisClient(Timer timer, ExecutorService executor, EventLoopGroup group, URI address) { + public RedisClient(Timer timer, ExecutorService executor, EventLoopGroup group, URL address) { this(timer, executor, group, address.getHost(), address.getPort()); } diff --git a/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java b/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java index bbb4a6c30a1..8646744f90e 100644 --- a/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java +++ b/redisson/src/main/java/org/redisson/cluster/ClusterConnectionManager.java @@ -15,7 +15,7 @@ */ package org.redisson.cluster; -import java.net.URI; +import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -66,13 +66,13 @@ public class ClusterConnectionManager extends MasterSlaveConnectionManager { private final Logger log = LoggerFactory.getLogger(getClass()); - private final Map nodeConnections = PlatformDependent.newConcurrentHashMap(); + private final Map nodeConnections = PlatformDependent.newConcurrentHashMap(); private final ConcurrentMap lastPartitions = PlatformDependent.newConcurrentHashMap(); private ScheduledFuture monitorFuture; - private volatile URI lastClusterNode; + private volatile URL lastClusterNode; public ClusterConnectionManager(ClusterServersConfig cfg, Config config) { super(config); @@ -84,7 +84,7 @@ public ClusterConnectionManager(ClusterServersConfig cfg, Config config) { Throwable lastException = null; List failedMasters = new ArrayList(); - for (URI addr : cfg.getNodeAddresses()) { + for (URL addr : cfg.getNodeAddresses()) { RFuture connectionFuture = connect(cfg, addr); try { RedisConnection connection = connectionFuture.syncUninterruptibly().getNow(); @@ -158,7 +158,7 @@ private void close(RedisConnection conn) { } } - private RFuture connect(ClusterServersConfig cfg, final URI addr) { + private RFuture connect(ClusterServersConfig cfg, final URL addr) { RedisConnection connection = nodeConnections.get(addr); if (connection != null) { return newSucceededFuture(connection); @@ -308,22 +308,22 @@ public void operationComplete(Future future) throws Exception { return result; } - private void scheduleClusterChangeCheck(final ClusterServersConfig cfg, final Iterator iterator) { + private void scheduleClusterChangeCheck(final ClusterServersConfig cfg, final Iterator iterator) { monitorFuture = GlobalEventExecutor.INSTANCE.schedule(new Runnable() { @Override public void run() { AtomicReference lastException = new AtomicReference(); - Iterator nodesIterator = iterator; + Iterator nodesIterator = iterator; if (nodesIterator == null) { - List nodes = new ArrayList(); - List slaves = new ArrayList(); + List nodes = new ArrayList(); + List slaves = new ArrayList(); for (ClusterPartition partition : getLastPartitions()) { if (!partition.isMasterFail()) { nodes.add(partition.getMasterAddress()); } - Set partitionSlaves = new HashSet(partition.getSlaveAddresses()); + Set partitionSlaves = new HashSet(partition.getSlaveAddresses()); partitionSlaves.removeAll(partition.getFailedSlaveAddresses()); slaves.addAll(partitionSlaves); } @@ -339,7 +339,7 @@ public void run() { }, cfg.getScanInterval(), TimeUnit.MILLISECONDS); } - private void checkClusterState(final ClusterServersConfig cfg, final Iterator iterator, final AtomicReference lastException) { + private void checkClusterState(final ClusterServersConfig cfg, final Iterator iterator, final AtomicReference lastException) { if (!iterator.hasNext()) { log.error("Can't update cluster state", lastException.get()); scheduleClusterChangeCheck(cfg, null); @@ -348,7 +348,7 @@ private void checkClusterState(final ClusterServersConfig cfg, final Iterator connectionFuture = connect(cfg, uri); connectionFuture.addListener(new FutureListener() { @Override @@ -366,7 +366,7 @@ public void operationComplete(Future future) throws Exception { }); } - private void updateClusterState(final ClusterServersConfig cfg, final RedisConnection connection, final Iterator iterator, final URI uri) { + private void updateClusterState(final ClusterServersConfig cfg, final RedisConnection connection, final Iterator iterator, final URL uri) { RFuture> future = connection.async(RedisCommands.CLUSTER_NODES); future.addListener(new FutureListener>() { @Override @@ -415,7 +415,7 @@ private void checkSlaveNodesChange(Collection newPartitions) { MasterSlaveEntry entry = getEntry(currentPart.getMasterAddr()); // should be invoked first in order to remove stale failedSlaveAddresses - Set addedSlaves = addRemoveSlaves(entry, currentPart, newPart); + Set addedSlaves = addRemoveSlaves(entry, currentPart, newPart); // Do some slaves have changed state from failed to alive? upDownSlaves(entry, currentPart, newPart, addedSlaves); @@ -424,20 +424,20 @@ private void checkSlaveNodesChange(Collection newPartitions) { } } - private void upDownSlaves(final MasterSlaveEntry entry, final ClusterPartition currentPart, final ClusterPartition newPart, Set addedSlaves) { - Set aliveSlaves = new HashSet(currentPart.getFailedSlaveAddresses()); + private void upDownSlaves(final MasterSlaveEntry entry, final ClusterPartition currentPart, final ClusterPartition newPart, Set addedSlaves) { + Set aliveSlaves = new HashSet(currentPart.getFailedSlaveAddresses()); aliveSlaves.removeAll(addedSlaves); aliveSlaves.removeAll(newPart.getFailedSlaveAddresses()); - for (URI uri : aliveSlaves) { + for (URL uri : aliveSlaves) { currentPart.removeFailedSlaveAddress(uri); if (entry.slaveUp(uri.getHost(), uri.getPort(), FreezeReason.MANAGER)) { log.info("slave: {} has up for slot ranges: {}", uri, currentPart.getSlotRanges()); } } - Set failedSlaves = new HashSet(newPart.getFailedSlaveAddresses()); + Set failedSlaves = new HashSet(newPart.getFailedSlaveAddresses()); failedSlaves.removeAll(currentPart.getFailedSlaveAddresses()); - for (URI uri : failedSlaves) { + for (URL uri : failedSlaves) { currentPart.addFailedSlaveAddress(uri); if (entry.slaveDown(uri.getHost(), uri.getPort(), FreezeReason.MANAGER)) { log.warn("slave: {} has down for slot ranges: {}", uri, currentPart.getSlotRanges()); @@ -445,11 +445,11 @@ private void upDownSlaves(final MasterSlaveEntry entry, final ClusterPartition c } } - private Set addRemoveSlaves(final MasterSlaveEntry entry, final ClusterPartition currentPart, final ClusterPartition newPart) { - Set removedSlaves = new HashSet(currentPart.getSlaveAddresses()); + private Set addRemoveSlaves(final MasterSlaveEntry entry, final ClusterPartition currentPart, final ClusterPartition newPart) { + Set removedSlaves = new HashSet(currentPart.getSlaveAddresses()); removedSlaves.removeAll(newPart.getSlaveAddresses()); - for (URI uri : removedSlaves) { + for (URL uri : removedSlaves) { currentPart.removeSlaveAddress(uri); if (entry.slaveDown(uri.getHost(), uri.getPort(), FreezeReason.MANAGER)) { @@ -457,9 +457,9 @@ private Set addRemoveSlaves(final MasterSlaveEntry entry, final ClusterPart } } - Set addedSlaves = new HashSet(newPart.getSlaveAddresses()); + Set addedSlaves = new HashSet(newPart.getSlaveAddresses()); addedSlaves.removeAll(currentPart.getSlaveAddresses()); - for (final URI uri : addedSlaves) { + for (final URL uri : addedSlaves) { RFuture future = entry.addSlave(uri.getHost(), uri.getPort()); future.addListener(new FutureListener() { @Override @@ -516,8 +516,8 @@ private RFuture checkMasterNodesChange(ClusterServersConfig cfg, Collectio if (!newMasterPart.getMasterAddress().equals(currentPart.getMasterAddress())) { log.info("changing master from {} to {} for {}", currentPart.getMasterAddress(), newMasterPart.getMasterAddress(), slot); - URI newUri = newMasterPart.getMasterAddress(); - URI oldUri = currentPart.getMasterAddress(); + URL newUri = newMasterPart.getMasterAddress(); + URL oldUri = currentPart.getMasterAddress(); changeMaster(slot, newUri.getHost(), newUri.getPort()); @@ -720,7 +720,7 @@ private HashSet getLastPartitions() { } @Override - public URI getLastClusterNode() { + public URL getLastClusterNode() { return lastClusterNode; } diff --git a/redisson/src/main/java/org/redisson/cluster/ClusterNodeInfo.java b/redisson/src/main/java/org/redisson/cluster/ClusterNodeInfo.java index 2416259812a..17a78c5ca63 100644 --- a/redisson/src/main/java/org/redisson/cluster/ClusterNodeInfo.java +++ b/redisson/src/main/java/org/redisson/cluster/ClusterNodeInfo.java @@ -16,10 +16,11 @@ package org.redisson.cluster; import java.net.URI; +import java.net.URL; import java.util.HashSet; import java.util.Set; -import org.redisson.misc.URIBuilder; +import org.redisson.misc.URLBuilder; /** * @@ -33,7 +34,7 @@ public enum Flag {NOFLAGS, SLAVE, MASTER, MYSELF, FAIL, HANDSHAKE, NOADDR}; private final String nodeInfo; private String nodeId; - private URI address; + private URL address; private final Set flags = new HashSet(); private String slaveOf; @@ -50,11 +51,11 @@ public void setNodeId(String nodeId) { this.nodeId = nodeId; } - public URI getAddress() { + public URL getAddress() { return address; } public void setAddress(String address) { - this.address = URIBuilder.create(address); + this.address = URLBuilder.create(address); } public void addSlotRange(ClusterSlotRange range) { diff --git a/redisson/src/main/java/org/redisson/cluster/ClusterPartition.java b/redisson/src/main/java/org/redisson/cluster/ClusterPartition.java index ddfa42b7bb8..60eb1974b50 100644 --- a/redisson/src/main/java/org/redisson/cluster/ClusterPartition.java +++ b/redisson/src/main/java/org/redisson/cluster/ClusterPartition.java @@ -16,20 +16,25 @@ package org.redisson.cluster; import java.net.InetSocketAddress; -import java.net.URI; +import java.net.URL; import java.util.Collections; import java.util.HashSet; import java.util.Set; -import org.redisson.misc.URIBuilder; +import org.redisson.misc.URLBuilder; +/** + * + * @author Nikita Koksharov + * + */ public class ClusterPartition { private final String nodeId; private boolean masterFail; - private URI masterAddress; - private final Set slaveAddresses = new HashSet(); - private final Set failedSlaves = new HashSet(); + private URL masterAddress; + private final Set slaveAddresses = new HashSet(); + private final Set failedSlaves = new HashSet(); private final Set slots = new HashSet(); private final Set slotRanges = new HashSet(); @@ -85,33 +90,33 @@ public InetSocketAddress getMasterAddr() { return new InetSocketAddress(masterAddress.getHost(), masterAddress.getPort()); } - public URI getMasterAddress() { + public URL getMasterAddress() { return masterAddress; } public void setMasterAddress(String masterAddress) { - setMasterAddress(URIBuilder.create(masterAddress)); + setMasterAddress(URLBuilder.create(masterAddress)); } - public void setMasterAddress(URI masterAddress) { + public void setMasterAddress(URL masterAddress) { this.masterAddress = masterAddress; } - public void addFailedSlaveAddress(URI address) { + public void addFailedSlaveAddress(URL address) { failedSlaves.add(address); } - public Set getFailedSlaveAddresses() { + public Set getFailedSlaveAddresses() { return Collections.unmodifiableSet(failedSlaves); } - public void removeFailedSlaveAddress(URI uri) { + public void removeFailedSlaveAddress(URL uri) { failedSlaves.remove(uri); } - public void addSlaveAddress(URI address) { + public void addSlaveAddress(URL address) { slaveAddresses.add(address); } - public Set getSlaveAddresses() { + public Set getSlaveAddresses() { return Collections.unmodifiableSet(slaveAddresses); } - public void removeSlaveAddress(URI uri) { + public void removeSlaveAddress(URL uri) { slaveAddresses.remove(uri); failedSlaves.remove(uri); } diff --git a/redisson/src/main/java/org/redisson/config/ClusterServersConfig.java b/redisson/src/main/java/org/redisson/config/ClusterServersConfig.java index dbe81d2575f..03ac2317af8 100644 --- a/redisson/src/main/java/org/redisson/config/ClusterServersConfig.java +++ b/redisson/src/main/java/org/redisson/config/ClusterServersConfig.java @@ -15,18 +15,23 @@ */ package org.redisson.config; -import java.net.URI; +import java.net.URL; import java.util.ArrayList; import java.util.List; -import org.redisson.misc.URIBuilder; +import org.redisson.misc.URLBuilder; +/** + * + * @author Nikita Koksharov + * + */ public class ClusterServersConfig extends BaseMasterSlaveServersConfig { /** * Redis cluster node urls list */ - private List nodeAddresses = new ArrayList(); + private List nodeAddresses = new ArrayList(); /** * Redis cluster scan interval in milliseconds @@ -50,14 +55,14 @@ public ClusterServersConfig() { */ public ClusterServersConfig addNodeAddress(String ... addresses) { for (String address : addresses) { - nodeAddresses.add(URIBuilder.create(address)); + nodeAddresses.add(URLBuilder.create(address)); } return this; } - public List getNodeAddresses() { + public List getNodeAddresses() { return nodeAddresses; } - void setNodeAddresses(List nodeAddresses) { + void setNodeAddresses(List nodeAddresses) { this.nodeAddresses = nodeAddresses; } diff --git a/redisson/src/main/java/org/redisson/config/ConfigSupport.java b/redisson/src/main/java/org/redisson/config/ConfigSupport.java index 2198c94a0b8..dae4b19bf70 100644 --- a/redisson/src/main/java/org/redisson/config/ConfigSupport.java +++ b/redisson/src/main/java/org/redisson/config/ConfigSupport.java @@ -47,7 +47,13 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import org.redisson.codec.CodecProvider; import org.redisson.liveobject.provider.ResolverProvider; +import org.redisson.misc.URLBuilder; +/** + * + * @author Nikita Koksharov + * + */ public class ConfigSupport { @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, property = "class") @@ -111,6 +117,10 @@ public static class ConfigMixIn { private final ObjectMapper jsonMapper = createMapper(null); private final ObjectMapper yamlMapper = createMapper(new YAMLFactory()); + public ConfigSupport() { + URLBuilder.init(); + } + public T fromJSON(String content, Class configType) throws IOException { return jsonMapper.readValue(content, configType); } diff --git a/redisson/src/main/java/org/redisson/config/ElasticacheServersConfig.java b/redisson/src/main/java/org/redisson/config/ElasticacheServersConfig.java index 8e35a5d50db..a66019a4175 100644 --- a/redisson/src/main/java/org/redisson/config/ElasticacheServersConfig.java +++ b/redisson/src/main/java/org/redisson/config/ElasticacheServersConfig.java @@ -15,24 +15,25 @@ */ package org.redisson.config; -import java.net.URI; +import java.net.URL; import java.util.ArrayList; import java.util.List; -import org.redisson.misc.URIBuilder; +import org.redisson.misc.URLBuilder; /** * Configuration for an AWS ElastiCache replication group. A replication group is composed * of a single master endpoint and multiple read slaves. * * @author Steve Ungerer + * @author Nikita Koksharov */ public class ElasticacheServersConfig extends BaseMasterSlaveServersConfig { /** * Replication group node urls list */ - private List nodeAddresses = new ArrayList(); + private List nodeAddresses = new ArrayList(); /** * Replication group scan interval in milliseconds @@ -62,14 +63,14 @@ public ElasticacheServersConfig() { */ public ElasticacheServersConfig addNodeAddress(String ... addresses) { for (String address : addresses) { - nodeAddresses.add(URIBuilder.create(address)); + nodeAddresses.add(URLBuilder.create(address)); } return this; } - public List getNodeAddresses() { + public List getNodeAddresses() { return nodeAddresses; } - void setNodeAddresses(List nodeAddresses) { + void setNodeAddresses(List nodeAddresses) { this.nodeAddresses = nodeAddresses; } diff --git a/redisson/src/main/java/org/redisson/config/MasterSlaveServersConfig.java b/redisson/src/main/java/org/redisson/config/MasterSlaveServersConfig.java index 1cb5a74853d..9a5023a9470 100644 --- a/redisson/src/main/java/org/redisson/config/MasterSlaveServersConfig.java +++ b/redisson/src/main/java/org/redisson/config/MasterSlaveServersConfig.java @@ -15,25 +15,28 @@ */ package org.redisson.config; -import java.net.URI; -import java.util.Collections; +import java.net.URL; import java.util.HashSet; -import java.util.List; import java.util.Set; -import org.redisson.misc.URIBuilder; +import org.redisson.misc.URLBuilder; +/** + * + * @author Nikita Koksharov + * + */ public class MasterSlaveServersConfig extends BaseMasterSlaveServersConfig { /** * Redis slave servers addresses */ - private Set slaveAddresses = new HashSet(); + private Set slaveAddresses = new HashSet(); /** * Redis master server address */ - private List masterAddress; + private URL masterAddress; /** * Database index used for Redis connection @@ -59,19 +62,19 @@ public MasterSlaveServersConfig() { */ public MasterSlaveServersConfig setMasterAddress(String masterAddress) { if (masterAddress != null) { - this.masterAddress = Collections.singletonList(URIBuilder.create(masterAddress)); + this.masterAddress = URLBuilder.create(masterAddress); } return this; } - public URI getMasterAddress() { + public URL getMasterAddress() { if (masterAddress != null) { - return masterAddress.get(0); + return masterAddress; } return null; } - public void setMasterAddress(URI masterAddress) { + public void setMasterAddress(URL masterAddress) { if (masterAddress != null) { - this.masterAddress = Collections.singletonList(masterAddress); + this.masterAddress = masterAddress; } } @@ -83,18 +86,18 @@ public void setMasterAddress(URI masterAddress) { */ public MasterSlaveServersConfig addSlaveAddress(String ... addresses) { for (String address : addresses) { - slaveAddresses.add(URIBuilder.create(address)); + slaveAddresses.add(URLBuilder.create(address)); } return this; } - public MasterSlaveServersConfig addSlaveAddress(URI slaveAddress) { + public MasterSlaveServersConfig addSlaveAddress(URL slaveAddress) { slaveAddresses.add(slaveAddress); return this; } - public Set getSlaveAddresses() { + public Set getSlaveAddresses() { return slaveAddresses; } - public void setSlaveAddresses(Set readAddresses) { + public void setSlaveAddresses(Set readAddresses) { this.slaveAddresses = readAddresses; } diff --git a/redisson/src/main/java/org/redisson/config/SentinelServersConfig.java b/redisson/src/main/java/org/redisson/config/SentinelServersConfig.java index c86abd8cf24..f52741045bc 100644 --- a/redisson/src/main/java/org/redisson/config/SentinelServersConfig.java +++ b/redisson/src/main/java/org/redisson/config/SentinelServersConfig.java @@ -15,15 +15,20 @@ */ package org.redisson.config; -import java.net.URI; +import java.net.URL; import java.util.ArrayList; import java.util.List; -import org.redisson.misc.URIBuilder; +import org.redisson.misc.URLBuilder; +/** + * + * @author Nikita Koksharov + * + */ public class SentinelServersConfig extends BaseMasterSlaveServersConfig { - private List sentinelAddresses = new ArrayList(); + private List sentinelAddresses = new ArrayList(); private String masterName; @@ -64,14 +69,14 @@ public String getMasterName() { */ public SentinelServersConfig addSentinelAddress(String ... addresses) { for (String address : addresses) { - sentinelAddresses.add(URIBuilder.create(address)); + sentinelAddresses.add(URLBuilder.create(address)); } return this; } - public List getSentinelAddresses() { + public List getSentinelAddresses() { return sentinelAddresses; } - void setSentinelAddresses(List sentinelAddresses) { + void setSentinelAddresses(List sentinelAddresses) { this.sentinelAddresses = sentinelAddresses; } diff --git a/redisson/src/main/java/org/redisson/config/SingleServerConfig.java b/redisson/src/main/java/org/redisson/config/SingleServerConfig.java index a43a4331127..d5707ed6b27 100644 --- a/redisson/src/main/java/org/redisson/config/SingleServerConfig.java +++ b/redisson/src/main/java/org/redisson/config/SingleServerConfig.java @@ -15,11 +15,9 @@ */ package org.redisson.config; -import java.net.URI; -import java.util.Collections; -import java.util.List; +import java.net.URL; -import org.redisson.misc.URIBuilder; +import org.redisson.misc.URLBuilder; /** * @@ -32,7 +30,7 @@ public class SingleServerConfig extends BaseConfig { * Redis server address * */ - private List address; + private URL address; /** * Minimum idle subscription connection amount @@ -129,19 +127,19 @@ public int getSubscriptionConnectionPoolSize() { */ public SingleServerConfig setAddress(String address) { if (address != null) { - this.address = Collections.singletonList(URIBuilder.create(address)); + this.address = URLBuilder.create(address); } return this; } - public URI getAddress() { + public URL getAddress() { if (address != null) { - return address.get(0); + return address; } return null; } - void setAddress(URI address) { + void setAddress(URL address) { if (address != null) { - this.address = Collections.singletonList(address); + this.address = address; } } diff --git a/redisson/src/main/java/org/redisson/connection/ConnectionManager.java b/redisson/src/main/java/org/redisson/connection/ConnectionManager.java index 9171851aa1e..1626fa0bde6 100644 --- a/redisson/src/main/java/org/redisson/connection/ConnectionManager.java +++ b/redisson/src/main/java/org/redisson/connection/ConnectionManager.java @@ -16,7 +16,7 @@ package org.redisson.connection; import java.net.InetSocketAddress; -import java.net.URI; +import java.net.URL; import java.util.Collection; import java.util.Set; import java.util.concurrent.ExecutorService; @@ -47,7 +47,7 @@ public interface ConnectionManager { ExecutorService getExecutor(); - URI getLastClusterNode(); + URL getLastClusterNode(); boolean isClusterMode(); diff --git a/redisson/src/main/java/org/redisson/connection/ElasticacheConnectionManager.java b/redisson/src/main/java/org/redisson/connection/ElasticacheConnectionManager.java index eead266144f..000d364477f 100644 --- a/redisson/src/main/java/org/redisson/connection/ElasticacheConnectionManager.java +++ b/redisson/src/main/java/org/redisson/connection/ElasticacheConnectionManager.java @@ -15,7 +15,7 @@ */ package org.redisson.connection; -import java.net.URI; +import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; @@ -55,9 +55,9 @@ public class ElasticacheConnectionManager extends MasterSlaveConnectionManager { private final Logger log = LoggerFactory.getLogger(getClass()); - private AtomicReference currentMaster = new AtomicReference(); + private AtomicReference currentMaster = new AtomicReference(); - private final Map nodeConnections = new HashMap(); + private final Map nodeConnections = new HashMap(); private ScheduledFuture monitorFuture; @@ -72,7 +72,7 @@ public ElasticacheConnectionManager(ElasticacheServersConfig cfg, Config config) this.config = create(cfg); initTimer(this.config); - for (URI addr : cfg.getNodeAddresses()) { + for (URL addr : cfg.getNodeAddresses()) { RFuture connectionFuture = connect(cfg, addr); connectionFuture.awaitUninterruptibly(); RedisConnection connection = connectionFuture.getNow(); @@ -110,7 +110,7 @@ protected MasterSlaveServersConfig create(BaseMasterSlaveServersConfig cfg) { return res; } - private RFuture connect(BaseMasterSlaveServersConfig cfg, final URI addr) { + private RFuture connect(BaseMasterSlaveServersConfig cfg, final URL addr) { RedisConnection connection = nodeConnections.get(addr); if (connection != null) { return newSucceededFuture(connection); @@ -158,11 +158,11 @@ private void scheduleMasterChangeCheck(final ElasticacheServersConfig cfg) { monitorFuture = GlobalEventExecutor.INSTANCE.schedule(new Runnable() { @Override public void run() { - final URI master = currentMaster.get(); + final URL master = currentMaster.get(); log.debug("Current master: {}", master); final AtomicInteger count = new AtomicInteger(cfg.getNodeAddresses().size()); - for (final URI addr : cfg.getNodeAddresses()) { + for (final URL addr : cfg.getNodeAddresses()) { RFuture connectionFuture = connect(cfg, addr); connectionFuture.addListener(new FutureListener() { @Override diff --git a/redisson/src/main/java/org/redisson/connection/MasterSlaveConnectionManager.java b/redisson/src/main/java/org/redisson/connection/MasterSlaveConnectionManager.java index e59a490be26..246602f757b 100644 --- a/redisson/src/main/java/org/redisson/connection/MasterSlaveConnectionManager.java +++ b/redisson/src/main/java/org/redisson/connection/MasterSlaveConnectionManager.java @@ -16,7 +16,7 @@ package org.redisson.connection; import java.net.InetSocketAddress; -import java.net.URI; +import java.net.URL; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -280,7 +280,7 @@ protected void initEntry(MasterSlaveServersConfig config) { protected MasterSlaveEntry createMasterSlaveEntry(MasterSlaveServersConfig config, HashSet slots) { MasterSlaveEntry entry = new MasterSlaveEntry(slots, this, config); - List> fs = entry.initSlaveBalancer(java.util.Collections.emptySet()); + List> fs = entry.initSlaveBalancer(java.util.Collections.emptySet()); for (RFuture future : fs) { future.syncUninterruptibly(); } @@ -835,7 +835,7 @@ public ExecutorService getExecutor() { return executor; } - public URI getLastClusterNode() { + public URL getLastClusterNode() { return null; } } diff --git a/redisson/src/main/java/org/redisson/connection/MasterSlaveEntry.java b/redisson/src/main/java/org/redisson/connection/MasterSlaveEntry.java index 28c8c91e94c..07b9b194302 100644 --- a/redisson/src/main/java/org/redisson/connection/MasterSlaveEntry.java +++ b/redisson/src/main/java/org/redisson/connection/MasterSlaveEntry.java @@ -16,7 +16,7 @@ package org.redisson.connection; import java.net.InetSocketAddress; -import java.net.URI; +import java.net.URL; import java.util.Collection; import java.util.HashSet; import java.util.LinkedList; @@ -37,7 +37,6 @@ import org.redisson.config.ReadMode; import org.redisson.connection.ClientConnectionsEntry.FreezeReason; import org.redisson.connection.balancer.LoadBalancerManager; -import org.redisson.connection.balancer.LoadBalancerManager; import org.redisson.connection.pool.MasterConnectionPool; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -80,7 +79,7 @@ public MasterSlaveEntry(Set slotRanges, ConnectionManager conn writeConnectionHolder = new MasterConnectionPool(config, connectionManager, this); } - public List> initSlaveBalancer(Collection disconnectedNodes) { + public List> initSlaveBalancer(Collection disconnectedNodes) { boolean freezeMasterAsSlave = !config.getSlaveAddresses().isEmpty() && config.getReadMode() == ReadMode.SLAVE && disconnectedNodes.size() < config.getSlaveAddresses().size(); @@ -88,7 +87,7 @@ public List> initSlaveBalancer(Collection disconnectedNodes) List> result = new LinkedList>(); RFuture f = addSlave(config.getMasterAddress().getHost(), config.getMasterAddress().getPort(), freezeMasterAsSlave, NodeType.MASTER); result.add(f); - for (URI address : config.getSlaveAddresses()) { + for (URL address : config.getSlaveAddresses()) { f = addSlave(address.getHost(), address.getPort(), disconnectedNodes.contains(address), NodeType.SLAVE); result.add(f); } diff --git a/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java b/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java index cc6b469b55a..4c38ede69d7 100755 --- a/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java +++ b/redisson/src/main/java/org/redisson/connection/SentinelConnectionManager.java @@ -16,7 +16,7 @@ package org.redisson.connection; import java.net.InetSocketAddress; -import java.net.URI; +import java.net.URL; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -41,7 +41,7 @@ import org.redisson.config.ReadMode; import org.redisson.config.SentinelServersConfig; import org.redisson.connection.ClientConnectionsEntry.FreezeReason; -import org.redisson.misc.URIBuilder; +import org.redisson.misc.URLBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -62,7 +62,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager { private final AtomicReference currentMaster = new AtomicReference(); private final ConcurrentMap slaves = PlatformDependent.newConcurrentHashMap(); - private final Set disconnectedSlaves = new HashSet(); + private final Set disconnectedSlaves = new HashSet(); public SentinelConnectionManager(SentinelServersConfig cfg, Config config) { super(config); @@ -70,7 +70,7 @@ public SentinelConnectionManager(SentinelServersConfig cfg, Config config) { final MasterSlaveServersConfig c = create(cfg); initTimer(c); - for (URI addr : cfg.getSentinelAddresses()) { + for (URL addr : cfg.getSentinelAddresses()) { RedisClient client = createClient(addr.getHost(), addr.getPort(), c.getConnectTimeout(), c.getRetryInterval() * c.getRetryAttempts()); try { RedisConnection connection = client.connect(); @@ -104,7 +104,7 @@ public SentinelConnectionManager(SentinelServersConfig cfg, Config config) { log.info("slave: {} added", host); if (flags.contains("s_down") || flags.contains("disconnected")) { - URI url = URIBuilder.create(host); + URL url = URLBuilder.create(host); disconnectedSlaves.add(url); log.warn("slave: {} is down", host); } @@ -123,7 +123,7 @@ public SentinelConnectionManager(SentinelServersConfig cfg, Config config) { init(c); List> connectionFutures = new ArrayList>(cfg.getSentinelAddresses().size()); - for (URI addr : cfg.getSentinelAddresses()) { + for (URL addr : cfg.getSentinelAddresses()) { RFuture future = registerSentinel(cfg, addr, c); connectionFutures.add(future); } @@ -146,7 +146,7 @@ protected MasterSlaveEntry createMasterSlaveEntry(MasterSlaveServersConfig confi return entry; } - private RFuture registerSentinel(final SentinelServersConfig cfg, final URI addr, final MasterSlaveServersConfig c) { + private RFuture registerSentinel(final SentinelServersConfig cfg, final URL addr, final MasterSlaveServersConfig c) { RedisClient client = createClient(addr.getHost(), addr.getPort(), c.getConnectTimeout(), c.getRetryInterval() * c.getRetryAttempts()); RedisClient oldClient = sentinels.putIfAbsent(addr.getHost() + ":" + addr.getPort(), client); if (oldClient != null) { @@ -208,12 +208,12 @@ protected void onSentinelAdded(SentinelServersConfig cfg, String msg, MasterSlav String port = parts[3]; String addr = ip + ":" + port; - URI uri = URIBuilder.create(addr); + URL uri = URLBuilder.create(addr); registerSentinel(cfg, uri, c); } } - protected void onSlaveAdded(URI addr, String msg) { + protected void onSlaveAdded(URL addr, String msg) { String[] parts = msg.split(" "); if (parts.length > 4 @@ -250,7 +250,7 @@ public void operationComplete(Future future) throws Exception { } } - private void onNodeDown(URI sentinelAddr, String msg) { + private void onNodeDown(URL sentinelAddr, String msg) { String[] parts = msg.split(" "); if (parts.length > 3) { @@ -298,7 +298,7 @@ private void slaveDown(String ip, String port) { } } - private void onNodeUp(URI addr, String msg) { + private void onNodeUp(URL addr, String msg) { String[] parts = msg.split(" "); if (parts.length > 3) { @@ -337,7 +337,7 @@ private void slaveUp(String ip, String port) { } } - private void onMasterChange(SentinelServersConfig cfg, URI addr, String msg) { + private void onMasterChange(SentinelServersConfig cfg, URL addr, String msg) { String[] parts = msg.split(" "); if (parts.length > 3) { diff --git a/redisson/src/main/java/org/redisson/connection/balancer/WeightedRoundRobinBalancer.java b/redisson/src/main/java/org/redisson/connection/balancer/WeightedRoundRobinBalancer.java index c5227783cc4..e5224013c78 100644 --- a/redisson/src/main/java/org/redisson/connection/balancer/WeightedRoundRobinBalancer.java +++ b/redisson/src/main/java/org/redisson/connection/balancer/WeightedRoundRobinBalancer.java @@ -16,7 +16,7 @@ package org.redisson.connection.balancer; import java.net.InetSocketAddress; -import java.net.URI; +import java.net.URL; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -28,7 +28,7 @@ import java.util.concurrent.atomic.AtomicInteger; import org.redisson.connection.ClientConnectionsEntry; -import org.redisson.misc.URIBuilder; +import org.redisson.misc.URLBuilder; import io.netty.util.internal.PlatformDependent; @@ -78,7 +78,7 @@ public void resetWeightCounter() { */ public WeightedRoundRobinBalancer(Map weights, int defaultWeight) { for (Entry entry : weights.entrySet()) { - URI uri = URIBuilder.create(entry.getKey()); + URL uri = URLBuilder.create(entry.getKey()); InetSocketAddress addr = new InetSocketAddress(uri.getHost(), uri.getPort()); if (entry.getValue() <= 0) { throw new IllegalArgumentException("Weight can't be less than or equal zero"); diff --git a/redisson/src/main/java/org/redisson/misc/URIBuilder.java b/redisson/src/main/java/org/redisson/misc/URIBuilder.java deleted file mode 100644 index c838798596a..00000000000 --- a/redisson/src/main/java/org/redisson/misc/URIBuilder.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2016 Nikita Koksharov - * - * 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 org.redisson.misc; - -import java.net.URI; - -public class URIBuilder { - - public static URI create(String uri) { - String[] parts = uri.split(":"); - if (parts.length-1 >= 3) { - String port = parts[parts.length-1]; - String newPort = port.split("[^\\d]")[0]; - uri = "[" + uri.replace(":" + port, "") + "]:" + newPort; - } else { - String port = parts[parts.length-1]; - String newPort = port.split("[^\\d]")[0]; - uri = uri.replace(":" + port, "") + ":" + newPort; - } - - return URI.create("//" + uri); - } - -} diff --git a/redisson/src/main/java/org/redisson/misc/URLBuilder.java b/redisson/src/main/java/org/redisson/misc/URLBuilder.java new file mode 100644 index 00000000000..a4d3337f783 --- /dev/null +++ b/redisson/src/main/java/org/redisson/misc/URLBuilder.java @@ -0,0 +1,89 @@ +/** + * Copyright 2016 Nikita Koksharov + * + * 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 org.redisson.misc; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.net.URLStreamHandlerFactory; + +/** + * + * @author Nikita Koksharov + * + */ +public class URLBuilder { + + private static volatile boolean init = false; + + static { + init(); + } + + public static void init() { + if (init) { + return; + } + init = true; + URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() { + @Override + public URLStreamHandler createURLStreamHandler(String protocol) { + if ("redis".equals(protocol)) { + return new URLStreamHandler() { + @Override + protected URLConnection openConnection(URL u) throws IOException { + throw new UnsupportedOperationException(); + }; + + @Override + protected boolean equals(URL u1, URL u2) { + return u1.toString().equals(u2.toString()); + } + + @Override + protected int hashCode(URL u) { + return u.toString().hashCode(); + } + + }; + } + return null; + } + }); + } + + public static URL create(String url) { + try { + String[] parts = url.split(":"); + if (parts.length-1 >= 3) { + String port = parts[parts.length-1]; + String newPort = port.split("[^\\d]")[0]; + String host = url.replace(":" + port, ""); + return new URL("redis://[" + host + "]:" + newPort); + } else { + String port = parts[parts.length-1]; + String newPort = port.split("[^\\d]")[0]; + String host = url.replace(":" + port, ""); + return new URL("redis://" + host + ":" + newPort); + } + } catch (MalformedURLException e) { + throw new IllegalArgumentException(e); + } + } + +} diff --git a/redisson/src/test/java/org/redisson/RedissonTest.java b/redisson/src/test/java/org/redisson/RedissonTest.java index 6298895bf09..be9ccfceb1d 100644 --- a/redisson/src/test/java/org/redisson/RedissonTest.java +++ b/redisson/src/test/java/org/redisson/RedissonTest.java @@ -1,5 +1,6 @@ package org.redisson; +import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; import java.util.Collections; @@ -40,7 +41,7 @@ public class RedissonTest { protected RedissonClient redisson; protected static RedissonClient defaultRedisson; - + @Test public void testSmallPool() throws InterruptedException { Config config = new Config(); @@ -311,23 +312,40 @@ public void testClusterConfig() throws IOException { } @Test - public void testSingleConfig() throws IOException { + public void testSingleConfigJSON() throws IOException { RedissonClient r = BaseTest.createInstance(); String t = r.getConfig().toJSON(); Config c = Config.fromJSON(t); assertThat(c.toJSON()).isEqualTo(t); } + + @Test + public void testSingleConfigYAML() throws IOException { + RedissonClient r = BaseTest.createInstance(); + String t = r.getConfig().toYAML(); + Config c = Config.fromYAML(t); + assertThat(c.toYAML()).isEqualTo(t); + } + @Test - public void testMasterSlaveConfig() throws IOException { + public void testMasterSlaveConfigJSON() throws IOException { Config c2 = new Config(); c2.useMasterSlaveServers().setMasterAddress("123.1.1.1:1231").addSlaveAddress("82.12.47.12:1028"); - String t = c2.toJSON(); Config c = Config.fromJSON(t); assertThat(c.toJSON()).isEqualTo(t); } + @Test + public void testMasterSlaveConfigYAML() throws IOException { + Config c2 = new Config(); + c2.useMasterSlaveServers().setMasterAddress("123.1.1.1:1231").addSlaveAddress("82.12.47.12:1028"); + String t = c2.toYAML(); + Config c = Config.fromYAML(t); + assertThat(c.toYAML()).isEqualTo(t); + } + // @Test public void testCluster() { NodesGroup nodes = redisson.getClusterNodesGroup(); diff --git a/redisson/src/test/java/org/redisson/client/protocol/decoder/ClusterNodesDecoderTest.java b/redisson/src/test/java/org/redisson/client/protocol/decoder/ClusterNodesDecoderTest.java index a3cb7e95f67..69fe85e6069 100644 --- a/redisson/src/test/java/org/redisson/client/protocol/decoder/ClusterNodesDecoderTest.java +++ b/redisson/src/test/java/org/redisson/client/protocol/decoder/ClusterNodesDecoderTest.java @@ -28,6 +28,7 @@ public void test() throws IOException { buf.writeBytes(src); List nodes = decoder.decode(buf, null); ClusterNodeInfo node = nodes.get(0); + Assert.assertEquals("192.168.234.129", node.getAddress().getHost()); Assert.assertEquals(7001, node.getAddress().getPort()); }