Skip to content

Commit

Permalink
ReplicatedServersConfig introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita committed Feb 2, 2017
1 parent 642a605 commit 7142307
Show file tree
Hide file tree
Showing 7 changed files with 403 additions and 281 deletions.
50 changes: 48 additions & 2 deletions redisson/src/main/java/org/redisson/config/Config.java
Expand Up @@ -49,6 +49,8 @@ public class Config {

private ElasticacheServersConfig elasticacheServersConfig;

private ReplicatedServersConfig replicatedServersConfig;

/**
* Threads amount shared between all redis node clients
*/
Expand Down Expand Up @@ -117,6 +119,9 @@ public Config(Config oldConf) {
if (oldConf.getElasticacheServersConfig() != null) {
setElasticacheServersConfig(new ElasticacheServersConfig(oldConf.getElasticacheServersConfig()));
}
if (oldConf.getReplicatedServersConfig() != null) {
setReplicatedServersConfig(new ReplicatedServersConfig(oldConf.getReplicatedServersConfig()));
}

}

Expand Down Expand Up @@ -214,6 +219,7 @@ ClusterServersConfig useClusterServers(ClusterServersConfig config) {
checkSentinelServersConfig();
checkSingleServerConfig();
checkElasticacheServersConfig();
checkReplicatedServersConfig();

if (clusterServersConfig == null) {
clusterServersConfig = config;
Expand All @@ -230,10 +236,10 @@ void setClusterServersConfig(ClusterServersConfig clusterServersConfig) {
}

/**
* Init AWS Elasticache servers configuration.
*
* @return ElasticacheServersConfig
* Use {@link #useReplicatedServers()}
*/
@Deprecated
public ElasticacheServersConfig useElasticacheServers() {
return useElasticacheServers(new ElasticacheServersConfig());
}
Expand All @@ -258,6 +264,37 @@ void setElasticacheServersConfig(ElasticacheServersConfig elasticacheServersConf
this.elasticacheServersConfig = elasticacheServersConfig;
}

/**
* Init Replicated servers configuration.
* Most used with Azure Redis Cache or AWS Elasticache
*
* @return ReplicatedServersConfig
*/
public ReplicatedServersConfig useReplicatedServers() {
return useReplicatedServers(new ReplicatedServersConfig());
}

ReplicatedServersConfig useReplicatedServers(ReplicatedServersConfig config) {
checkClusterServersConfig();
checkMasterSlaveServersConfig();
checkSentinelServersConfig();
checkSingleServerConfig();
checkElasticacheServersConfig();

if (replicatedServersConfig == null) {
replicatedServersConfig = new ReplicatedServersConfig();
}
return replicatedServersConfig;
}

ReplicatedServersConfig getReplicatedServersConfig() {
return replicatedServersConfig;
}

void setReplicatedServersConfig(ReplicatedServersConfig replicatedServersConfig) {
this.replicatedServersConfig = replicatedServersConfig;
}

/**
* Init single server configuration.
*
Expand All @@ -272,6 +309,7 @@ SingleServerConfig useSingleServer(SingleServerConfig config) {
checkMasterSlaveServersConfig();
checkSentinelServersConfig();
checkElasticacheServersConfig();
checkReplicatedServersConfig();

if (singleServerConfig == null) {
singleServerConfig = config;
Expand Down Expand Up @@ -301,6 +339,7 @@ SentinelServersConfig useSentinelServers(SentinelServersConfig sentinelServersCo
checkSingleServerConfig();
checkMasterSlaveServersConfig();
checkElasticacheServersConfig();
checkReplicatedServersConfig();

if (this.sentinelServersConfig == null) {
this.sentinelServersConfig = sentinelServersConfig;
Expand Down Expand Up @@ -330,6 +369,7 @@ MasterSlaveServersConfig useMasterSlaveServers(MasterSlaveServersConfig config)
checkSingleServerConfig();
checkSentinelServersConfig();
checkElasticacheServersConfig();
checkReplicatedServersConfig();

if (masterSlaveServersConfig == null) {
masterSlaveServersConfig = config;
Expand Down Expand Up @@ -400,6 +440,12 @@ private void checkElasticacheServersConfig() {
}
}

private void checkReplicatedServersConfig() {
if (replicatedServersConfig != null) {
throw new IllegalStateException("Replication servers config already used!");
}
}

/**
* Activates an unix socket if servers binded to loopback interface.
* Also used for epoll transport activation.
Expand Down
7 changes: 7 additions & 0 deletions redisson/src/main/java/org/redisson/config/ConfigSupport.java
Expand Up @@ -28,6 +28,7 @@
import org.redisson.cluster.ClusterConnectionManager;
import org.redisson.connection.ConnectionManager;
import org.redisson.connection.ElasticacheConnectionManager;
import org.redisson.connection.ReplicatedConnectionManager;
import org.redisson.connection.MasterSlaveConnectionManager;
import org.redisson.connection.SentinelConnectionManager;
import org.redisson.connection.SingleConnectionManager;
Expand Down Expand Up @@ -112,6 +113,9 @@ public static class ConfigMixIn {
@JsonProperty
ElasticacheServersConfig elasticacheServersConfig;

@JsonProperty
ReplicatedServersConfig replicatedServersConfig;

}

private final ObjectMapper jsonMapper = createMapper(null);
Expand Down Expand Up @@ -241,6 +245,9 @@ public static ConnectionManager createConnectionManager(Config configCopy) {
} else if (configCopy.getElasticacheServersConfig() != null) {
validate(configCopy.getElasticacheServersConfig());
return new ElasticacheConnectionManager(configCopy.getElasticacheServersConfig(), configCopy);
} else if (configCopy.getReplicatedServersConfig() != null) {
validate(configCopy.getReplicatedServersConfig());
return new ReplicatedConnectionManager(configCopy.getReplicatedServersConfig(), configCopy);
} else {
throw new IllegalArgumentException("server(s) address(es) not defined!");
}
Expand Down
Expand Up @@ -15,92 +15,17 @@
*/
package org.redisson.config;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

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
* Use {@link org.redisson.config.ReplicatedServersConfig}
*/
public class ElasticacheServersConfig extends BaseMasterSlaveServersConfig<ElasticacheServersConfig> {

/**
* Replication group node urls list
*/
private List<URL> nodeAddresses = new ArrayList<URL>();

/**
* Replication group scan interval in milliseconds
*/
private int scanInterval = 1000;

/**
* Database index used for Redis connection
*/
private int database = 0;
@Deprecated
public class ElasticacheServersConfig extends ReplicatedServersConfig {

public ElasticacheServersConfig() {
}

ElasticacheServersConfig(ElasticacheServersConfig config) {
public ElasticacheServersConfig(ReplicatedServersConfig config) {
super(config);
setNodeAddresses(config.getNodeAddresses());
setScanInterval(config.getScanInterval());
setDatabase(config.getDatabase());
}

/**
* Add Redis cluster node address. Use follow format -- <code>host:port</code>
*
* @param addresses in <code>host:port</code> format
* @return config
*/
public ElasticacheServersConfig addNodeAddress(String ... addresses) {
for (String address : addresses) {
nodeAddresses.add(URLBuilder.create(address));
}
return this;
}
public List<URL> getNodeAddresses() {
return nodeAddresses;
}
void setNodeAddresses(List<URL> nodeAddresses) {
this.nodeAddresses = nodeAddresses;
}

public int getScanInterval() {
return scanInterval;
}
/**
* Elasticache node scan interval in milliseconds
*
* @param scanInterval in milliseconds
* @return config
*/
public ElasticacheServersConfig setScanInterval(int scanInterval) {
this.scanInterval = scanInterval;
return this;
}

/**
* Database index used for Redis connection
* Default is <code>0</code>
*
* @param database number
* @return config
*/
public ElasticacheServersConfig setDatabase(int database) {
this.database = database;
return this;
}
public int getDatabase() {
return database;
}

}
@@ -0,0 +1,106 @@
/**
* 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.config;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.redisson.misc.URLBuilder;

/**
* Configuration for an Azure Redis Cache or AWS ElastiCache servers.
* A replication group is composed of a single master endpoint and multiple read slaves.
*
* @author Steve Ungerer
* @author Nikita Koksharov
*/
public class ReplicatedServersConfig extends BaseMasterSlaveServersConfig<ReplicatedServersConfig> {

/**
* Replication group node urls list
*/
private List<URL> nodeAddresses = new ArrayList<URL>();

/**
* Replication group scan interval in milliseconds
*/
private int scanInterval = 1000;

/**
* Database index used for Redis connection
*/
private int database = 0;

public ReplicatedServersConfig() {
}

ReplicatedServersConfig(ReplicatedServersConfig config) {
super(config);
setNodeAddresses(config.getNodeAddresses());
setScanInterval(config.getScanInterval());
setDatabase(config.getDatabase());
}

/**
* Add Redis cluster node address. Use follow format -- <code>host:port</code>
*
* @param addresses in <code>host:port</code> format
* @return config
*/
public ReplicatedServersConfig addNodeAddress(String ... addresses) {
for (String address : addresses) {
nodeAddresses.add(URLBuilder.create(address));
}
return this;
}
public List<URL> getNodeAddresses() {
return nodeAddresses;
}
void setNodeAddresses(List<URL> nodeAddresses) {
this.nodeAddresses = nodeAddresses;
}

public int getScanInterval() {
return scanInterval;
}
/**
* Elasticache node scan interval in milliseconds
*
* @param scanInterval in milliseconds
* @return config
*/
public ReplicatedServersConfig setScanInterval(int scanInterval) {
this.scanInterval = scanInterval;
return this;
}

/**
* Database index used for Redis connection
* Default is <code>0</code>
*
* @param database number
* @return config
*/
public ReplicatedServersConfig setDatabase(int database) {
this.database = database;
return this;
}
public int getDatabase() {
return database;
}

}

0 comments on commit 7142307

Please sign in to comment.