Skip to content

Commit 46073c7

Browse files
beiwei30zonghaishang
authored andcommitted
Code review (apache#3094)
* movde compareTo into Configurator as a default method * adjust javado * optimize diamond * polish the code
1 parent 383a37e commit 46073c7

File tree

3 files changed

+150
-126
lines changed

3 files changed

+150
-126
lines changed

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/Configurator.java

+38-13
Original file line numberDiff line numberDiff line change
@@ -35,32 +35,36 @@
3535
public interface Configurator extends Comparable<Configurator> {
3636

3737
/**
38-
* get the configurator url.
38+
* Get the configurator url.
3939
*
4040
* @return configurator url.
4141
*/
4242
URL getUrl();
4343

4444
/**
4545
* Configure the provider url.
46-
* O
4746
*
48-
* @param url - old rovider url.
47+
* @param url - old provider url.
4948
* @return new provider url.
5049
*/
5150
URL configure(URL url);
5251

5352

5453
/**
55-
* Convert override urls to map for use when re-refer.
56-
* Send all rules every time, the urls will be reassembled and calculated
54+
* Convert override urls to map for use when re-refer. Send all rules every time, the urls will be reassembled and
55+
* calculated
5756
*
58-
* @param urls Contract:
59-
* </br>1.override://0.0.0.0/...( or override://ip:port...?anyhost=true)&para1=value1... means global rules (all of the providers take effect)
60-
* </br>2.override://ip:port...?anyhost=false Special rules (only for a certain provider)
61-
* </br>3.override:// rule is not supported... ,needs to be calculated by registry itself.
62-
* </br>4.override://0.0.0.0/ without parameters means clearing the override
63-
* @return
57+
* URL contract:
58+
* <ol>
59+
* <li>override://0.0.0.0/...( or override://ip:port...?anyhost=true)&para1=value1... means global rules
60+
* (all of the providers take effect)</li>
61+
* <li>override://ip:port...?anyhost=false Special rules (only for a certain provider)</li>
62+
* <li>override:// rule is not supported... ,needs to be calculated by registry itself</li>
63+
* <li>override://0.0.0.0/ without parameters means clearing the override</li>
64+
* </ol>
65+
*
66+
* @param urls URL list to convert
67+
* @return converted configurator list
6468
*/
6569
static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
6670
if (CollectionUtils.isEmpty(urls)) {
@@ -70,13 +74,13 @@ static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
7074
ConfiguratorFactory configuratorFactory = ExtensionLoader.getExtensionLoader(ConfiguratorFactory.class)
7175
.getAdaptiveExtension();
7276

73-
List<Configurator> configurators = new ArrayList<Configurator>(urls.size());
77+
List<Configurator> configurators = new ArrayList<>(urls.size());
7478
for (URL url : urls) {
7579
if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
7680
configurators.clear();
7781
break;
7882
}
79-
Map<String, String> override = new HashMap<String, String>(url.getParameters());
83+
Map<String, String> override = new HashMap<>(url.getParameters());
8084
//The anyhost parameter of override may be added automatically, it can't change the judgement of changing url
8185
override.remove(Constants.ANYHOST_KEY);
8286
if (override.size() == 0) {
@@ -88,4 +92,25 @@ static Optional<List<Configurator>> toConfigurators(List<URL> urls) {
8892
Collections.sort(configurators);
8993
return Optional.of(configurators);
9094
}
95+
96+
/**
97+
* Sort by host, then by priority
98+
* 1. the url with a specific host ip should have higher priority than 0.0.0.0
99+
* 2. if two url has the same host, compare by priority value;
100+
*/
101+
default int compareTo(Configurator o) {
102+
if (o == null) {
103+
return -1;
104+
}
105+
106+
int ipCompare = getUrl().getHost().compareTo(o.getUrl().getHost());
107+
// host is the same, sort by priority
108+
if (ipCompare == 0) {
109+
int i = getUrl().getParameter(Constants.PRIORITY_KEY, 0);
110+
int j = o.getUrl().getParameter(Constants.PRIORITY_KEY, 0);
111+
return Integer.compare(i, j);
112+
} else {
113+
return ipCompare;
114+
}
115+
}
91116
}

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/configurator/AbstractConfigurator.java

-26
Original file line numberDiff line numberDiff line change
@@ -131,32 +131,6 @@ private URL configureIfMatch(String host, URL url) {
131131
return url;
132132
}
133133

134-
/**
135-
* Sort by host, priority
136-
* 1. the url with a specific host ip should have higher priority than 0.0.0.0
137-
* 2. if two url has the same host, compare by priority value;
138-
*
139-
* @param o
140-
* @return
141-
*/
142-
@Override
143-
public int compareTo(Configurator o) {
144-
if (o == null) {
145-
return -1;
146-
}
147-
148-
int ipCompare = getUrl().getHost().compareTo(o.getUrl().getHost());
149-
if (ipCompare == 0) {//host is the same, sort by priority
150-
int i = getUrl().getParameter(Constants.PRIORITY_KEY, 0),
151-
j = o.getUrl().getParameter(Constants.PRIORITY_KEY, 0);
152-
return Integer.compare(i, j);
153-
} else {
154-
return ipCompare;
155-
}
156-
157-
158-
}
159-
160134
protected abstract URL doConfigure(URL currentUrl, URL configUrl);
161135

162136
}

0 commit comments

Comments
 (0)