Skip to content

Commit 79f0b7c

Browse files
kezhenxu94ralf0131
authored andcommitted
Polish code and fix some documentation errors (#3655)
1 parent b2bfbc7 commit 79f0b7c

File tree

1 file changed

+48
-57
lines changed
  • dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd

1 file changed

+48
-57
lines changed

dubbo-registry/dubbo-registry-etcd3/src/main/java/org/apache/dubbo/registry/etcd/EtcdRegistry.java

Lines changed: 48 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import org.apache.dubbo.rpc.RpcException;
5050

5151
import java.util.ArrayList;
52+
import java.util.Arrays;
5253
import java.util.List;
5354
import java.util.Optional;
5455
import java.util.Set;
@@ -69,11 +70,10 @@ public class EtcdRegistry extends FailbackRegistry {
6970

7071
private final String root;
7172

72-
private final Set<String> anyServices = new ConcurrentHashSet<String>();
73+
private final Set<String> anyServices = new ConcurrentHashSet<>();
7374

7475
private final ConcurrentMap<URL, ConcurrentMap<NotifyListener, ChildListener>> etcdListeners = new ConcurrentHashMap<>();
7576
private final EtcdClient etcdClient;
76-
private long expirePeriod;
7777

7878
public EtcdRegistry(URL url, EtcdTransporter etcdTransporter) {
7979
super(url);
@@ -121,7 +121,7 @@ public void doRegister(URL url) {
121121
} catch (Throwable e) {
122122
throw new RpcException("Failed to register " + url + " to etcd " + getUrl()
123123
+ ", cause: " + (OptionUtil.isProtocolError(e)
124-
? "etcd3 registy maybe not supported yet or etcd3 registry not available."
124+
? "etcd3 registry may not be supported yet or etcd3 registry is not available."
125125
: e.getMessage()), e);
126126
}
127127
}
@@ -142,9 +142,9 @@ public void doSubscribe(URL url, NotifyListener listener) {
142142
if (Constants.ANY_VALUE.equals(url.getServiceInterface())) {
143143
String root = toRootPath();
144144

145-
/**
146-
* if we interesting all interfaces,
147-
* we find current or create container for url, put or get only once.
145+
/*
146+
* if we are interested in all interfaces,
147+
* find out the current container or create one for the url, put or get only once.
148148
*/
149149
ConcurrentMap<NotifyListener, ChildListener> listeners =
150150
Optional.ofNullable(etcdListeners.get(url))
@@ -154,42 +154,40 @@ public void doSubscribe(URL url, NotifyListener listener) {
154154
return prev != null ? prev : container;
155155
});
156156

157-
/**
158-
* if we have not interface watcher listener,
159-
* we find current or create listener for current root, put or get only once.
157+
/*
158+
* if we have no interface watcher listener,
159+
* find the current listener or create one for the current root, put or get only once.
160160
*/
161161
ChildListener interfaceListener =
162162
Optional.ofNullable(listeners.get(listener))
163163
.orElseGet(() -> {
164164
ChildListener childListener, prev;
165-
prev = listeners.putIfAbsent(listener, childListener = new ChildListener() {
166-
public void childChanged(String parentPath, List<String> currentChildren) {
167-
/**
168-
* because etcd3 not support direct children watch events,
169-
* we should filter not interface events. if we watch /dubbo
170-
* and /dubbo/interface, when we put key-value pair {/dubbo/interface/hello hello},
171-
* we will got events in watching path /dubbo.
172-
*/
173-
for (String child : currentChildren) {
174-
child = URL.decode(child);
175-
if (!anyServices.contains(child)) {
176-
anyServices.add(child);
177-
/**
178-
* if new interface event arrived, we watching direct children,
179-
* eg: /dubbo/interface, /dubbo/interface and so on.
180-
*/
181-
subscribe(url.setPath(child).addParameters(Constants.INTERFACE_KEY, child,
182-
Constants.CHECK_KEY, String.valueOf(false)), listener);
183-
}
165+
prev = listeners.putIfAbsent(listener, childListener = (parentPath, currentChildren) -> {
166+
/*
167+
* because etcd3 does not support direct children watch events,
168+
* we should filter not interface events. if we watch /dubbo
169+
* and /dubbo/interface, when we put a key-value pair {/dubbo/interface/hello hello},
170+
* we will got events in watching path /dubbo.
171+
*/
172+
for (String child : currentChildren) {
173+
child = URL.decode(child);
174+
if (!anyServices.contains(child)) {
175+
anyServices.add(child);
176+
/*
177+
* if new interface event arrived, we watch their direct children,
178+
* eg: /dubbo/interface, /dubbo/interface and so on.
179+
*/
180+
subscribe(url.setPath(child).addParameters(Constants.INTERFACE_KEY, child,
181+
Constants.CHECK_KEY, String.valueOf(false)), listener);
184182
}
185183
}
186184
});
187185
return prev != null ? prev : childListener;
188186
});
189187

190188
etcdClient.create(root);
191-
/**
192-
* first time, we want pull already interface and then watching direct children,
189+
/*
190+
* at the first time, we want to pull already interface and then watch their direct children,
193191
* eg: /dubbo/interface, /dubbo/interface and so on.
194192
*/
195193
List<String> services = etcdClient.addChildListener(root, interfaceListener);
@@ -200,47 +198,42 @@ public void childChanged(String parentPath, List<String> currentChildren) {
200198
Constants.CHECK_KEY, String.valueOf(false)), listener);
201199
}
202200
} else {
203-
List<URL> urls = new ArrayList<URL>();
201+
List<URL> urls = new ArrayList<>();
204202
for (String path : toCategoriesPath(url)) {
205203

206-
/**
207-
* if we interesting special categories (providers, consumers, routers and so on),
208-
* we find current or create container for url, put or get only once.
204+
/*
205+
* if we are interested in special categories (providers, consumers, routers and so on),
206+
* we find out the current container or create one for the url, put or get only once.
209207
*/
210208
ConcurrentMap<NotifyListener, ChildListener> listeners =
211209
Optional.ofNullable(etcdListeners.get(url))
212210
.orElseGet(() -> {
213211
ConcurrentMap<NotifyListener, ChildListener> container, prev;
214212
prev = etcdListeners.putIfAbsent(url,
215-
container = new ConcurrentHashMap<NotifyListener, ChildListener>());
213+
container = new ConcurrentHashMap<>());
216214
return prev != null ? prev : container;
217215
});
218216

219-
/**
217+
/*
220218
* if we have no category watcher listener,
221-
* we find current or create listener for current category, put or get only once.
219+
* we find out the current listener or create one for the current category, put or get only once.
222220
*/
223221
ChildListener childListener =
224222
Optional.ofNullable(listeners.get(listener))
225223
.orElseGet(() -> {
226224
ChildListener watchListener, prev;
227-
prev = listeners.putIfAbsent(listener, watchListener = new ChildListener() {
228-
public void childChanged(String parentPath, List<String> currentChildren) {
229-
EtcdRegistry.this.notify(url, listener,
230-
toUrlsWithEmpty(url, parentPath, currentChildren));
231-
}
232-
});
225+
prev = listeners.putIfAbsent(listener, watchListener = (parentPath, currentChildren) -> EtcdRegistry.this.notify(url, listener,
226+
toUrlsWithEmpty(url, parentPath, currentChildren)));
233227
return prev != null ? prev : watchListener;
234228
});
235229

236230
etcdClient.create(path);
237-
/**
238-
* first time, we want pull already category and then watching direct children,
231+
/*
232+
* at the first time, we want to pull already category and then watch their direct children,
239233
* eg: /dubbo/interface/providers, /dubbo/interface/consumers and so on.
240234
*/
241235
List<String> children = etcdClient.addChildListener(path, childListener);
242236
if (children != null) {
243-
final String watchPath = path;
244237
urls.addAll(toUrlsWithEmpty(url, path, children));
245238
}
246239
}
@@ -249,7 +242,7 @@ public void childChanged(String parentPath, List<String> currentChildren) {
249242
} catch (Throwable e) {
250243
throw new RpcException("Failed to subscribe " + url + " to etcd " + getUrl()
251244
+ ", cause: " + (OptionUtil.isProtocolError(e)
252-
? "etcd3 registy maybe not supported yet or etcd3 registry not available."
245+
? "etcd3 registry may not be supported yet or etcd3 registry is not available."
253246
: e.getMessage()), e);
254247
}
255248
}
@@ -260,7 +253,7 @@ public void doUnsubscribe(URL url, NotifyListener listener) {
260253
if (listeners != null) {
261254
ChildListener etcdListener = listeners.get(listener);
262255
if (etcdListener != null) {
263-
// maybe url has many subscribe path
256+
// maybe url has many subscribed paths
264257
for (String path : toUnsubscribedPath(url)) {
265258
etcdClient.removeChildListener(path, etcdListener);
266259
}
@@ -303,16 +296,16 @@ protected String toServicePath(URL url) {
303296
}
304297

305298
protected String[] toCategoriesPath(URL url) {
306-
String[] categroies;
299+
String[] categories;
307300
if (Constants.ANY_VALUE.equals(url.getParameter(Constants.CATEGORY_KEY))) {
308-
categroies = new String[]{Constants.PROVIDERS_CATEGORY, Constants.CONSUMERS_CATEGORY,
301+
categories = new String[]{Constants.PROVIDERS_CATEGORY, Constants.CONSUMERS_CATEGORY,
309302
Constants.ROUTERS_CATEGORY, Constants.CONFIGURATORS_CATEGORY};
310303
} else {
311-
categroies = url.getParameter(Constants.CATEGORY_KEY, new String[]{Constants.DEFAULT_CATEGORY});
304+
categories = url.getParameter(Constants.CATEGORY_KEY, new String[]{Constants.DEFAULT_CATEGORY});
312305
}
313-
String[] paths = new String[categroies.length];
314-
for (int i = 0; i < categroies.length; i++) {
315-
paths[i] = toServicePath(url) + Constants.PATH_SEPARATOR + categroies[i];
306+
String[] paths = new String[categories.length];
307+
for (int i = 0; i < categories.length; i++) {
308+
paths[i] = toServicePath(url) + Constants.PATH_SEPARATOR + categories[i];
316309
}
317310
return paths;
318311
}
@@ -335,9 +328,7 @@ protected List<String> toUnsubscribedPath(URL url) {
335328
categories.add(group);
336329
return categories;
337330
} else {
338-
for (String path : toCategoriesPath(url)) {
339-
categories.add(path);
340-
}
331+
categories.addAll(Arrays.asList(toCategoriesPath(url)));
341332
}
342333
return categories;
343334
}

0 commit comments

Comments
 (0)