This commit is contained in:
nkorange 2019-02-14 17:50:28 +08:00
parent eac25d6160
commit f497f6ced0
4 changed files with 27 additions and 76 deletions

View File

@ -35,11 +35,6 @@ import java.util.concurrent.ConcurrentHashMap;
public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implements Cloneable {
private static final String CLUSTER_NAME_SYNTAX = "[0-9a-zA-Z-]+";
/**
* in fact this is CIDR(Classless Inter-Domain Routing). for naming it 'submask' it has historical reasons
*/
private String submask = "0.0.0.0/0";
/**
* a addition for same site routing, can group multiple sites into a region, like Hangzhou, Shanghai, etc.
*/
@ -348,12 +343,6 @@ public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implement
defIPPort = cluster.getDefIPPort();
}
if (!StringUtils.equals(submask, cluster.getSubmask())) {
Loggers.SRV_LOG.info("[CLUSTER-UPDATE] {}:{}, submask: {} -> {}",
cluster.getDom().getName(), cluster.getName(), submask, cluster.getSubmask());
submask = cluster.getSubmask();
}
if (!StringUtils.equals(sitegroup, cluster.getSitegroup())) {
Loggers.SRV_LOG.info("[CLUSTER-UPDATE] {}:{}, sitegroup: {} -> {}",
cluster.getDom().getName(), cluster.getName(), sitegroup, cluster.getSitegroup());
@ -369,18 +358,6 @@ public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implement
metadata = cluster.getMetadata();
}
public String getSyncKey() {
return "";
}
public String getSubmask() {
return submask;
}
public void setSubmask(String submask) {
this.submask = submask;
}
public String getSitegroup() {
return sitegroup;
}
@ -400,16 +377,5 @@ public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implement
if (!getName().matches(CLUSTER_NAME_SYNTAX)) {
throw new IllegalArgumentException("cluster name can only have these characters: 0-9a-zA-Z-, current: " + getName());
}
String[] cidrGroups = submask.split("\\|");
for (String cidrGroup : cidrGroups) {
String[] cidrs = cidrGroup.split(",");
for (String cidr : cidrs) {
if (!cidr.matches(UtilsAndCommons.CIDR_REGEX)) {
throw new IllegalArgumentException("malformed submask: " + submask + " for cluster: " + getName());
}
}
}
}
}

View File

@ -50,7 +50,7 @@ public class IpAddress extends Instance implements Comparable {
private String app;
public static final Pattern IP_PATTERN
= Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):?(\\d{1,5})?");
= Pattern.compile("(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}):?(\\d{1,5})?");
public static final String SPLITER = "_";
@ -134,13 +134,13 @@ public class IpAddress extends Instance implements Comparable {
if (ipAddressAttributes.length > minimumLength) {
// determine 'valid':
if (Boolean.TRUE.toString().equals(ipAddressAttributes[minimumLength]) ||
Boolean.FALSE.toString().equals(ipAddressAttributes[minimumLength])) {
Boolean.FALSE.toString().equals(ipAddressAttributes[minimumLength])) {
ipAddress.setValid(Boolean.parseBoolean(ipAddressAttributes[minimumLength]));
}
// determine 'cluster':
if (!Boolean.TRUE.toString().equals(ipAddressAttributes[ipAddressAttributes.length - 1]) &&
!Boolean.FALSE.toString().equals(ipAddressAttributes[ipAddressAttributes.length - 1])) {
!Boolean.FALSE.toString().equals(ipAddressAttributes[ipAddressAttributes.length - 1])) {
ipAddress.setClusterName(ipAddressAttributes[ipAddressAttributes.length - 1]);
}
}
@ -150,7 +150,7 @@ public class IpAddress extends Instance implements Comparable {
if (ipAddressAttributes.length > minimumLength) {
// determine 'marked':
if (Boolean.TRUE.toString().equals(ipAddressAttributes[minimumLength]) ||
Boolean.FALSE.toString().equals(ipAddressAttributes[minimumLength])) {
Boolean.FALSE.toString().equals(ipAddressAttributes[minimumLength])) {
ipAddress.setMarked(Boolean.parseBoolean(ipAddressAttributes[minimumLength]));
}
}
@ -194,6 +194,11 @@ public class IpAddress extends Instance implements Comparable {
} else if (ip.getWeight() < MIN_WEIGHT_VALUE) {
ip.setWeight(0.0D);
}
if (!ip.validate()) {
throw new IllegalArgumentException("malfomed ip config: " + json);
}
return ip;
}
@ -298,6 +303,20 @@ public class IpAddress extends Instance implements Comparable {
return getIp() + "#" + getPort() + "#" + getClusterName() + "#" + getServiceName();
}
public boolean validate() {
Matcher matcher = IP_PATTERN.matcher(getIp() + ":" + getPort());
if (!matcher.matches()) {
return false;
}
if (getWeight() > MAX_WEIGHT_VALUE || getWeight() < MIN_WEIGHT_VALUE) {
return false;
}
return true;
}
@Override
public int compareTo(Object o) {
if (!(o instanceof IpAddress)) {

View File

@ -394,7 +394,6 @@ public class VirtualClusterDomain implements Domain, RaftListener {
clusters.put("defCkport", cluster.getDefCkport());
clusters.put("defIPPort", cluster.getDefIPPort());
clusters.put("useIPPort4Check", cluster.isUseIPPort4Check());
clusters.put("submask", cluster.getSubmask());
clusters.put("sitegroup", cluster.getSitegroup());
clustersList.add(clusters);
@ -605,29 +604,8 @@ public class VirtualClusterDomain implements Domain, RaftListener {
if (!name.matches(DOMAIN_NAME_SYNTAX)) {
throw new IllegalArgumentException("dom name can only have these characters: 0-9a-zA-Z-._:, current: " + name);
}
Map<String, List<String>> map = new HashMap<>(clusterMap.size());
for (Cluster cluster : clusterMap.values()) {
if (StringUtils.isEmpty(cluster.getSyncKey())) {
continue;
}
List<String> list = map.get(cluster.getSyncKey());
if (list == null) {
list = new ArrayList<>();
map.put(cluster.getSyncKey(), list);
}
list.add(cluster.getName());
cluster.valid();
}
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
List<String> list = entry.getValue();
if (list.size() > 1) {
String msg = "clusters' config can not be the same: " + list;
Loggers.SRV_LOG.warn(msg);
throw new IllegalArgumentException(msg);
}
}
}
}

View File

@ -534,6 +534,10 @@ public class ApiCommands {
ipAddress.setClusterName(cluster);
ipAddress.setEnabled(enabled);
if (!ipAddress.validate()) {
throw new IllegalArgumentException("malfomed ip config: " + ipAddress);
}
return ipAddress;
}
@ -718,17 +722,6 @@ public class ApiCommands {
cluster.setDefIPPort(Integer.parseInt(defIPPort));
}
String submask = WebUtils.optional(request, "submask", StringUtils.EMPTY);
if (!StringUtils.isEmpty(submask)) {
Cluster cluster
= dom.getClusterMap().get(WebUtils.optional(request, "cluster", UtilsAndCommons.DEFAULT_CLUSTER_NAME));
if (cluster == null) {
throw new IllegalStateException("cluster not found");
}
cluster.setSubmask(submask);
}
String ipPort4Check = WebUtils.optional(request, "ipPort4Check", StringUtils.EMPTY);
if (!StringUtils.isEmpty(ipPort4Check)) {
Cluster cluster
@ -2081,10 +2074,6 @@ public class ApiCommands {
cluster.setHealthChecker(config);
}
cluster.setSitegroup(siteGroup);
if (!StringUtils.isEmpty(submask)) {
cluster.setSubmask(submask);
}
}
cluster.setDom(domObj);
cluster.init();
@ -2417,7 +2406,6 @@ public class ApiCommands {
clusterPac.put("defCkport", cluster.getDefCkport());
clusterPac.put("defIPPort", cluster.getDefIPPort());
clusterPac.put("useIPPort4Check", cluster.isUseIPPort4Check());
clusterPac.put("submask", cluster.getSubmask());
clusterPac.put("sitegroup", cluster.getSitegroup());
clusterPac.put("metadatas", cluster.getMetadata());