-
Notifications
You must be signed in to change notification settings - Fork 26.3k
/
ConfigManager.java
511 lines (415 loc) · 16.8 KB
/
ConfigManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.dubbo.config.context;
import org.apache.dubbo.common.config.Environment;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.config.AbstractConfig;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConfigCenterConfig;
import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.MetadataReportConfig;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.config.ModuleConfig;
import org.apache.dubbo.config.MonitorConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.ProviderConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.stream.Collectors;
import static java.lang.Boolean.TRUE;
import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableSet;
import static java.util.Optional.ofNullable;
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY;
import static org.apache.dubbo.common.utils.ReflectUtils.getProperty;
import static org.apache.dubbo.common.utils.StringUtils.isNotEmpty;
import static org.apache.dubbo.config.Constants.PROTOCOLS_SUFFIX;
import static org.apache.dubbo.config.Constants.REGISTRIES_SUFFIX;
/**
* TODO
* Experimental API, should only being used internally at present.
* <p>
* Maybe we can consider open to end user in the following version by providing a fluent style builder.
*
* <pre>{@code
* public void class DubboBuilder() {
*
* public static DubboBuilder create() {
* return new DubboBuilder();
* }
*
* public DubboBuilder application(ApplicationConfig application) {
* ConfigManager.getInstance().addApplication(application);
* return this;
* }
*
* ...
*
* public void build() {
* // export all ServiceConfigs
* // refer all ReferenceConfigs
* }
* }
* }
* </pre>
* </p>
* TODO
* The properties defined here are duplicate with that in ReferenceConfig/ServiceConfig,
* the properties here are currently only used for duplication check but are still not being used in the export/refer process yet.
* Maybe we can remove the property definition in ReferenceConfig/ServiceConfig and only keep the setXxxConfig() as an entrance.
* All workflow internally can rely on ConfigManager.
*/
public class ConfigManager {
private static final Logger logger = LoggerFactory.getLogger(ConfigManager.class);
private static final ConfigManager CONFIG_MANAGER = new ConfigManager();
private final Map<Class<? extends AbstractConfig>, Map<String, AbstractConfig>> configsCache = newMap();
private final ReadWriteLock lock = new ReentrantReadWriteLock();
public static ConfigManager getInstance() {
return CONFIG_MANAGER;
}
private ConfigManager() {
}
// ApplicationConfig correlative methods
public void setApplication(ApplicationConfig application) {
addConfig(application, true);
}
public Optional<ApplicationConfig> getApplication() {
return ofNullable(getConfig(ApplicationConfig.class));
}
// MonitorConfig correlative methods
public void setMonitor(MonitorConfig monitor) {
addConfig(monitor, true);
}
public Optional<MonitorConfig> getMonitor() {
return ofNullable(getConfig(MonitorConfig.class));
}
// ModuleConfig correlative methods
public void setModule(ModuleConfig module) {
addConfig(module, true);
}
public Optional<ModuleConfig> getModule() {
return ofNullable(getConfig(ModuleConfig.class));
}
public void setMetrics(MetricsConfig metrics) {
addConfig(metrics, true);
}
public Optional<MetricsConfig> getMetrics() {
return ofNullable(getConfig(MetricsConfig.class));
}
// ConfigCenterConfig correlative methods
public void addConfigCenter(ConfigCenterConfig configCenter) {
addConfig(configCenter);
}
public void addConfigCenters(Iterable<ConfigCenterConfig> configCenters) {
configCenters.forEach(this::addConfigCenter);
}
public ConfigCenterConfig getConfigCenter(String id) {
return getConfig(ConfigCenterConfig.class, id);
}
public Collection<ConfigCenterConfig> getConfigCenters() {
return getConfigs(ConfigCenterConfig.class);
}
// MetadataReportConfig correlative methods
public void addMetadataReport(MetadataReportConfig metadataReportConfig) {
addConfig(metadataReportConfig);
}
public void addMetadataReports(Iterable<MetadataReportConfig> metadataReportConfigs) {
metadataReportConfigs.forEach(this::addMetadataReport);
}
public Collection<MetadataReportConfig> getMetadataConfigs() {
return getConfigs(MetadataReportConfig.class);
}
// MetadataReportConfig correlative methods
public void addProvider(ProviderConfig providerConfig) {
addConfig(providerConfig);
}
public void addProviders(Iterable<ProviderConfig> providerConfigs) {
providerConfigs.forEach(this::addProvider);
}
public Optional<ProviderConfig> getProvider(String id) {
return ofNullable(getConfig(ProviderConfig.class, id));
}
public Optional<ProviderConfig> getDefaultProvider() {
return getProvider(DEFAULT_KEY);
}
public Collection<ProviderConfig> getProviders() {
return getConfigs(ProviderConfig.class);
}
// ConsumerConfig correlative methods
public void addConsumer(ConsumerConfig consumerConfig) {
addConfig(consumerConfig);
}
public void addConsumers(Iterable<ConsumerConfig> consumerConfigs) {
consumerConfigs.forEach(this::addConsumer);
}
public Optional<ConsumerConfig> getConsumer(String id) {
return ofNullable(getConfig(ConsumerConfig.class, id));
}
public Optional<ConsumerConfig> getDefaultConsumer() {
return getConsumer(DEFAULT_KEY);
}
public Collection<ConsumerConfig> getConsumers() {
return getConfigs(ConsumerConfig.class);
}
// ProtocolConfig correlative methods
public void addProtocol(ProtocolConfig protocolConfig) {
addConfig(protocolConfig);
}
public void addProtocols(Iterable<ProtocolConfig> protocolConfigs) {
if (protocolConfigs != null) {
protocolConfigs.forEach(this::addProtocol);
}
}
public Optional<ProtocolConfig> getProtocol(String id) {
return ofNullable(getConfig(ProtocolConfig.class, id));
}
public List<ProtocolConfig> getDefaultProtocols() {
return getDefaultConfigs(getConfigsMap(ProtocolConfig.class));
}
public Collection<ProtocolConfig> getProtocols() {
return getConfigs(ProtocolConfig.class);
}
public Set<String> getProtocolIds() {
Set<String> protocolIds = new HashSet<>();
protocolIds.addAll(getSubProperties(Environment.getInstance()
.getExternalConfigurationMap(), PROTOCOLS_SUFFIX));
protocolIds.addAll(getSubProperties(Environment.getInstance()
.getAppExternalConfigurationMap(), PROTOCOLS_SUFFIX));
protocolIds.addAll(getConfigIds(ProtocolConfig.class));
return unmodifiableSet(protocolIds);
}
// RegistryConfig correlative methods
public void addRegistry(RegistryConfig registryConfig) {
addConfig(registryConfig);
}
public void addRegistries(Iterable<RegistryConfig> registryConfigs) {
if (registryConfigs != null) {
registryConfigs.forEach(this::addRegistry);
}
}
public Optional<RegistryConfig> getRegistry(String id) {
return ofNullable(getConfig(RegistryConfig.class, id));
}
public List<RegistryConfig> getDefaultRegistries() {
return getDefaultConfigs(getConfigsMap(RegistryConfig.class));
}
public Collection<RegistryConfig> getRegistries() {
return getConfigs(RegistryConfig.class);
}
public Set<String> getRegistryIds() {
Set<String> registryIds = new HashSet<>();
registryIds.addAll(getSubProperties(Environment.getInstance().getExternalConfigurationMap(),
REGISTRIES_SUFFIX));
registryIds.addAll(getSubProperties(Environment.getInstance().getAppExternalConfigurationMap(),
REGISTRIES_SUFFIX));
registryIds.addAll(getConfigIds(RegistryConfig.class));
return unmodifiableSet(registryIds);
}
// ServiceConfig correlative methods
public void addService(ServiceConfig<?> serviceConfig) {
addConfig(serviceConfig);
}
public void addServices(Iterable<ServiceConfig<?>> serviceConfigs) {
serviceConfigs.forEach(this::addService);
}
public Collection<ServiceConfig> getServices() {
return getConfigs(ServiceConfig.class);
}
public <T> ServiceConfig<T> getService(String id) {
return getConfig(ServiceConfig.class, id);
}
// ReferenceConfig correlative methods
public void addReference(ReferenceConfig<?> referenceConfig) {
addConfig(referenceConfig);
}
public void addReferences(Iterable<ReferenceConfig<?>> referenceConfigs) {
referenceConfigs.forEach(this::addReference);
}
public Collection<ReferenceConfig> getReferences() {
return getConfigs(ReferenceConfig.class);
}
public <T> ReferenceConfig<T> getReference(String id) {
return getConfig(ReferenceConfig.class, id);
}
protected static Set<String> getSubProperties(Map<String, String> properties, String prefix) {
return properties.keySet().stream().filter(k -> k.contains(prefix)).map(k -> {
k = k.substring(prefix.length());
return k.substring(0, k.indexOf("."));
}).collect(Collectors.toSet());
}
public void refreshAll() {
write(() -> {
// refresh all configs here,
getApplication().ifPresent(ApplicationConfig::refresh);
getMonitor().ifPresent(MonitorConfig::refresh);
getModule().ifPresent(ModuleConfig::refresh);
getProtocols().forEach(ProtocolConfig::refresh);
getRegistries().forEach(RegistryConfig::refresh);
getProviders().forEach(ProviderConfig::refresh);
getConsumers().forEach(ConsumerConfig::refresh);
});
}
// For test purpose
public void clear() {
write(() -> {
this.configsCache.clear();
});
}
/**
* Add the dubbo {@link AbstractConfig config}
*
* @param config the dubbo {@link AbstractConfig config}
*/
public void addConfig(AbstractConfig config) {
addConfig(config, false);
}
protected void addConfig(AbstractConfig config, boolean unique) {
if (config == null) {
return;
}
Class<? extends AbstractConfig> configType = config.getClass();
write(() -> {
Map<String, AbstractConfig> configsMap = configsCache.computeIfAbsent(configType, type -> newMap());
addIfAbsent(config, configsMap, unique);
});
}
protected <C extends AbstractConfig> Map<String, C> getConfigsMap(Class<? extends C> configType) {
return read(() -> (Map) configsCache.getOrDefault(configType, emptyMap()));
}
protected <C extends AbstractConfig> Collection<C> getConfigs(Class<C> configType) {
return read(() -> getConfigsMap(configType).values());
}
protected <C extends AbstractConfig> C getConfig(Class<C> configType, String id) {
return read(() -> {
Map<String, C> configsMap = (Map) configsCache.getOrDefault(configType, emptyMap());
return configsMap.get(id);
});
}
protected <C extends AbstractConfig> C getConfig(Class<C> configType) throws IllegalStateException {
return read(() -> {
Map<String, C> configsMap = (Map) configsCache.getOrDefault(configType, emptyMap());
int size = configsMap.size();
if (size < 1) {
// throw new IllegalStateException("No such " + configType.getName() + " is found");
return null;
} else if (size > 1) {
throw new IllegalStateException("The expected single matching " + configType.getName() + " but found " + size + " instances");
} else {
return configsMap.values().iterator().next();
}
});
}
protected <C extends AbstractConfig> Collection<String> getConfigIds(Class<C> configType) {
return getConfigs(configType)
.stream()
.map(AbstractConfig::getId)
.collect(Collectors.toSet());
}
private <V> V write(Callable<V> callable) {
V value = null;
Lock writeLock = lock.writeLock();
try {
writeLock.lock();
value = callable.call();
} catch (RuntimeException e) {
throw e;
} catch (Throwable e) {
throw new RuntimeException(e.getCause());
} finally {
writeLock.unlock();
}
return value;
}
private void write(Runnable runnable) {
write(() -> {
runnable.run();
return null;
});
}
private <V> V read(Callable<V> callable) {
Lock readLock = lock.readLock();
V value = null;
try {
readLock.lock();
value = callable.call();
} catch (Throwable e) {
throw new RuntimeException(e);
} finally {
readLock.unlock();
}
return value;
}
private static void checkDuplicate(AbstractConfig oldOne, AbstractConfig newOne) throws IllegalStateException {
if (oldOne != null && !oldOne.equals(newOne)) {
String configName = oldOne.getClass().getSimpleName();
throw new IllegalStateException("Duplicate Config found for " + configName + ", you should use only one unique " + configName + " for one application.");
}
}
private static Map newMap() {
return new HashMap<>();
}
static <C extends AbstractConfig> void addIfAbsent(C config, Map<String, C> configsMap, boolean unique)
throws IllegalStateException {
if (config == null || configsMap == null) {
return;
}
if (unique) { // check duplicate
configsMap.values().forEach(c -> {
checkDuplicate(c, config);
});
}
String key = getId(config);
C existedConfig = configsMap.get(key);
if (existedConfig != null && !config.equals(existedConfig)) {
if (logger.isWarnEnabled()) {
String type = config.getClass().getSimpleName();
logger.warn(String.format("Duplicate %s found, there already has one default %s or more than two %ss have the same id, " +
"you can try to give each %s a different id : %s", type, type, type, type, config));
}
} else {
configsMap.put(key, config);
}
}
static <C extends AbstractConfig> String getId(C config) {
String id = config.getId();
return isNotEmpty(id) ? id : isDefaultConfig(config) ?
config.getClass().getSimpleName() + "#" + DEFAULT_KEY : null;
}
static <C extends AbstractConfig> boolean isDefaultConfig(C config) {
Boolean isDefault = getProperty(config, "default");
return isDefault == null || TRUE.equals(isDefault);
}
static <C extends AbstractConfig> List<C> getDefaultConfigs(Map<String, C> configsMap) {
return configsMap.values()
.stream()
.filter(ConfigManager::isDefaultConfig)
.collect(Collectors.toList());
}
}