diff --git a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterConfigurationChangedEvent.java b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterConfigurationChangedEvent.java new file mode 100644 index 00000000000..448c51a3d79 --- /dev/null +++ b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterConfigurationChangedEvent.java @@ -0,0 +1,100 @@ +/* + * 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.servicecomb.config.center.client; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Objects; +import java.util.Set; + +/** + * This event is fired when configuration changed of config center. + */ +public class ConfigCenterConfigurationChangedEvent { + private final Map added; + + private final Map deleted; + + private final Map updated; + + private Set changed; + + private ConfigCenterConfigurationChangedEvent(Map added, Map updated, + Map deleted) { + this.added = added; + this.deleted = deleted; + this.updated = updated; + this.changed = new HashSet<>(); + this.changed.addAll(added.keySet()); + this.changed.addAll(updated.keySet()); + this.changed.addAll(deleted.keySet()); + } + + public static ConfigCenterConfigurationChangedEvent createIncremental(Map latest, + Map last) { + Map itemsCreated = new HashMap<>(); + Map itemsDeleted = new HashMap<>(); + Map itemsModified = new HashMap<>(); + + for (Map.Entry entry : latest.entrySet()) { + String itemKey = entry.getKey(); + if (!last.containsKey(itemKey)) { + itemsCreated.put(itemKey, entry.getValue()); + } else if (!Objects.equals(last.get(itemKey), latest.get(itemKey))) { + itemsModified.put(itemKey, entry.getValue()); + } + } + for (String itemKey : last.keySet()) { + if (!latest.containsKey(itemKey)) { + itemsDeleted.put(itemKey, null); + } + } + ConfigCenterConfigurationChangedEvent event = ConfigCenterConfigurationChangedEvent + .createIncremental(itemsCreated, itemsModified, itemsDeleted); + return event; + } + + public static ConfigCenterConfigurationChangedEvent createIncremental(Map added, + Map updated, + Map deleted) { + return new ConfigCenterConfigurationChangedEvent(added, updated, deleted); + } + + public static ConfigCenterConfigurationChangedEvent createIncremental(Map updated) { + return new ConfigCenterConfigurationChangedEvent(new HashMap<>(), updated, new HashMap<>()); + } + + public final Map getAdded() { + return added; + } + + + public final Map getUpdated() { + return updated; + } + + + public final Map getDeleted() { + return deleted; + } + + public final Set getChanged() { + return changed; + } +} diff --git a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java index 9a89b92a6dc..bdccc35434e 100644 --- a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java +++ b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java @@ -23,7 +23,6 @@ import org.apache.servicecomb.config.center.client.model.QueryConfigurationsRequest; import org.apache.servicecomb.config.center.client.model.QueryConfigurationsResponse; import org.apache.servicecomb.config.common.ConfigConverter; -import org.apache.servicecomb.config.common.ConfigurationChangedEvent; import org.apache.servicecomb.http.client.task.AbstractTask; import org.apache.servicecomb.http.client.task.Task; import org.slf4j.Logger; @@ -76,11 +75,14 @@ public void execute() { if (response.isChanged()) { queryConfigurationsRequest.setRevision(response.getRevision()); Map lastData = configConverter.updateData(response.getConfigurations()); - ConfigurationChangedEvent event = ConfigurationChangedEvent + ConfigCenterConfigurationChangedEvent event = ConfigCenterConfigurationChangedEvent .createIncremental(configConverter.getCurrentData(), lastData); - eventBus.post(event); + if (!event.getChanged().isEmpty()) { + eventBus.post(event); + } } - startTask(new BackOffSleepTask(configCenterConfiguration.getRefreshIntervalInMillis(), new PollConfigurationTask(0))); + startTask( + new BackOffSleepTask(configCenterConfiguration.getRefreshIntervalInMillis(), new PollConfigurationTask(0))); } catch (Exception e) { LOGGER.error("get configurations from ConfigCenter failed, and will try again.", e); startTask(new BackOffSleepTask(failCount + 1, new PollConfigurationTask(failCount + 1))); diff --git a/clients/config-common/src/test/java/org/apache/servicecomb/config/common/TestConfigurationChangedEvent.java b/clients/config-common/src/test/java/org/apache/servicecomb/config/common/TestConfigurationChangedEvent.java deleted file mode 100644 index 85c84e020af..00000000000 --- a/clients/config-common/src/test/java/org/apache/servicecomb/config/common/TestConfigurationChangedEvent.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * 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.servicecomb.config.common; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - -public class TestConfigurationChangedEvent { - @Test - public void testConfigurationChangedEvent() { - Map before = new HashMap<>(); - Map after = new HashMap<>(); - before.put("updated", "1"); - before.put("deleted", "1"); - before.put("notChanged", null); - - after.put("added", 1); - after.put("updated", 2); - after.put("addedNull", null); - after.put("notChanged", null); - - ConfigurationChangedEvent event = ConfigurationChangedEvent.createIncremental(after, before); - Assertions.assertEquals(2, event.getAdded().size()); - Assertions.assertEquals(1, event.getDeleted().size()); - Assertions.assertEquals(1, event.getUpdated().size()); - Assertions.assertEquals(4, event.getComplete().size()); - Assertions.assertTrue(event.isChanged()); - } -} diff --git a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java index 7c1eaf92383..eaa7b93d663 100644 --- a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java +++ b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java @@ -23,7 +23,6 @@ import java.util.concurrent.Executors; import org.apache.servicecomb.config.common.ConfigConverter; -import org.apache.servicecomb.config.common.ConfigurationChangedEvent; import org.apache.servicecomb.config.kie.client.model.ConfigurationsRequest; import org.apache.servicecomb.config.kie.client.model.ConfigurationsRequestFactory; import org.apache.servicecomb.config.kie.client.model.ConfigurationsResponse; @@ -90,9 +89,9 @@ private void onDataChanged() { this.configurationsRequests.forEach(r -> latestData.putAll(r.getLastRawData())); Map lastData = configConverter.updateData(latestData); - ConfigurationChangedEvent event = ConfigurationChangedEvent + KieConfigurationChangedEvent event = KieConfigurationChangedEvent .createIncremental(configConverter.getCurrentData(), lastData); - if (event.isChanged()) { + if (!event.getChanged().isEmpty()) { eventBus.post(event); } } @@ -128,9 +127,11 @@ public void execute() { onDataChanged(); } if (KieConfigManager.this.kieConfiguration.isEnableLongPolling()) { - startTask(new BackOffSleepTask(LONG_POLLING_INTERVAL, new PollConfigurationTask(0, this.configurationsRequest))); + startTask( + new BackOffSleepTask(LONG_POLLING_INTERVAL, new PollConfigurationTask(0, this.configurationsRequest))); } else { - startTask(new BackOffSleepTask(kieConfiguration.getRefreshIntervalInMillis(), new PollConfigurationTask(0, this.configurationsRequest))); + startTask(new BackOffSleepTask(kieConfiguration.getRefreshIntervalInMillis(), + new PollConfigurationTask(0, this.configurationsRequest))); } } catch (Exception e) { LOGGER.error("get configurations from KieConfigCenter failed, and will try again.", e); diff --git a/clients/config-common/src/main/java/org/apache/servicecomb/config/common/ConfigurationChangedEvent.java b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigurationChangedEvent.java similarity index 63% rename from clients/config-common/src/main/java/org/apache/servicecomb/config/common/ConfigurationChangedEvent.java rename to clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigurationChangedEvent.java index e24d061a65a..0e22c17e8b9 100644 --- a/clients/config-common/src/main/java/org/apache/servicecomb/config/common/ConfigurationChangedEvent.java +++ b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigurationChangedEvent.java @@ -15,62 +15,67 @@ * limitations under the License. */ -package org.apache.servicecomb.config.common; +package org.apache.servicecomb.config.kie.client; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Objects; +import java.util.Set; -public class ConfigurationChangedEvent { +/** + * This event is fired when configuration changed of kie. + */ +public class KieConfigurationChangedEvent { private final Map added; private final Map deleted; private final Map updated; - private final boolean changed; - - private Map complete; + private Set changed; - private ConfigurationChangedEvent(Map added, Map updated, - Map deleted, boolean changed) { + private KieConfigurationChangedEvent(Map added, Map updated, + Map deleted) { this.added = added; this.deleted = deleted; this.updated = updated; - this.changed = changed; + this.changed = new HashSet<>(); + this.changed.addAll(added.keySet()); + this.changed.addAll(updated.keySet()); + this.changed.addAll(deleted.keySet()); } - public static ConfigurationChangedEvent createIncremental(Map latest, Map last) { + public static KieConfigurationChangedEvent createIncremental(Map latest, Map last) { Map itemsCreated = new HashMap<>(); Map itemsDeleted = new HashMap<>(); Map itemsModified = new HashMap<>(); - boolean changed = false; for (Map.Entry entry : latest.entrySet()) { String itemKey = entry.getKey(); if (!last.containsKey(itemKey)) { itemsCreated.put(itemKey, entry.getValue()); - changed = true; } else if (!Objects.equals(last.get(itemKey), latest.get(itemKey))) { itemsModified.put(itemKey, entry.getValue()); - changed = true; } } for (String itemKey : last.keySet()) { if (!latest.containsKey(itemKey)) { itemsDeleted.put(itemKey, null); - changed = true; } } - ConfigurationChangedEvent event = ConfigurationChangedEvent - .createIncremental(itemsCreated, itemsModified, itemsDeleted, changed); - event.complete = latest; + KieConfigurationChangedEvent event = KieConfigurationChangedEvent + .createIncremental(itemsCreated, itemsModified, itemsDeleted); return event; } - private static ConfigurationChangedEvent createIncremental(Map added, Map updated, - Map deleted, boolean changed) { - return new ConfigurationChangedEvent(added, updated, deleted, changed); + public static KieConfigurationChangedEvent createIncremental(Map added, Map updated, + Map deleted) { + return new KieConfigurationChangedEvent(added, updated, deleted); + } + + public static KieConfigurationChangedEvent createIncremental(Map updated) { + return new KieConfigurationChangedEvent(new HashMap<>(), updated, new HashMap<>()); } public final Map getAdded() { @@ -87,11 +92,7 @@ public final Map getDeleted() { return deleted; } - public final Map getComplete() { - return complete; - } - - public final boolean isChanged() { + public final Set getChanged() { return changed; } } diff --git a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/cc/ConfigCenterDynamicPropertiesSource.java b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/cc/ConfigCenterDynamicPropertiesSource.java index 52f1167274c..614c44d39c6 100644 --- a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/cc/ConfigCenterDynamicPropertiesSource.java +++ b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/cc/ConfigCenterDynamicPropertiesSource.java @@ -28,15 +28,16 @@ import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.servicecomb.config.BootStrapProperties; +import org.apache.servicecomb.config.ConfigurationChangedEvent; import org.apache.servicecomb.config.DynamicPropertiesSource; import org.apache.servicecomb.config.center.client.ConfigCenterAddressManager; import org.apache.servicecomb.config.center.client.ConfigCenterClient; +import org.apache.servicecomb.config.center.client.ConfigCenterConfigurationChangedEvent; import org.apache.servicecomb.config.center.client.ConfigCenterManager; import org.apache.servicecomb.config.center.client.model.ConfigCenterConfiguration; import org.apache.servicecomb.config.center.client.model.QueryConfigurationsRequest; import org.apache.servicecomb.config.center.client.model.QueryConfigurationsResponse; import org.apache.servicecomb.config.common.ConfigConverter; -import org.apache.servicecomb.config.common.ConfigurationChangedEvent; import org.apache.servicecomb.foundation.auth.AuthHeaderProvider; import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx; import org.apache.servicecomb.foundation.common.event.EventManager; @@ -108,10 +109,13 @@ private QueryConfigurationsRequest firstPull(ConfigCenterConfig configCenterConf } @Subscribe - public void onConfigurationChangedEvent(ConfigurationChangedEvent event) { + public void onConfigurationChangedEvent(ConfigCenterConfigurationChangedEvent event) { + LOGGER.info("Dynamic configuration changed: {}", event.getChanged()); data.putAll(event.getAdded()); data.putAll(event.getUpdated()); event.getDeleted().forEach((k, v) -> data.remove(k)); + EventManager.post(ConfigurationChangedEvent.createIncremental(event.getAdded(), + event.getUpdated(), event.getDeleted())); } private QueryConfigurationsRequest createQueryConfigurationsRequest(Environment environment) { diff --git a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieDynamicPropertiesSource.java b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieDynamicPropertiesSource.java index b06ae113022..13a25be6f1f 100644 --- a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieDynamicPropertiesSource.java +++ b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieDynamicPropertiesSource.java @@ -29,11 +29,12 @@ import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.servicecomb.config.BootStrapProperties; +import org.apache.servicecomb.config.ConfigurationChangedEvent; import org.apache.servicecomb.config.DynamicPropertiesSource; import org.apache.servicecomb.config.common.ConfigConverter; -import org.apache.servicecomb.config.common.ConfigurationChangedEvent; import org.apache.servicecomb.config.kie.client.KieClient; import org.apache.servicecomb.config.kie.client.KieConfigManager; +import org.apache.servicecomb.config.kie.client.KieConfigurationChangedEvent; import org.apache.servicecomb.config.kie.client.model.KieAddressManager; import org.apache.servicecomb.config.kie.client.model.KieConfiguration; import org.apache.servicecomb.foundation.auth.AuthHeaderProvider; @@ -43,12 +44,16 @@ import org.apache.servicecomb.http.client.auth.RequestAuthHeaderProvider; import org.apache.servicecomb.http.client.common.HttpTransport; import org.apache.servicecomb.http.client.common.HttpTransportFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; import org.springframework.core.env.MapPropertySource; import com.google.common.eventbus.Subscribe; public class KieDynamicPropertiesSource implements DynamicPropertiesSource { + private static final Logger LOGGER = LoggerFactory.getLogger(KieDynamicPropertiesSource.class); + public static final String SOURCE_NAME = "kie"; private final Map data = new ConcurrentHashMapEx<>(); @@ -84,10 +89,13 @@ private void init(Environment environment) { } @Subscribe - public void onConfigurationChangedEvent(ConfigurationChangedEvent event) { + public void onConfigurationChangedEvent(KieConfigurationChangedEvent event) { + LOGGER.info("Dynamic configuration changed: {}", event.getChanged()); data.putAll(event.getAdded()); data.putAll(event.getUpdated()); event.getDeleted().forEach((k, v) -> data.remove(k)); + EventManager.post(ConfigurationChangedEvent.createIncremental(event.getAdded(), + event.getUpdated(), event.getDeleted())); } private KieConfiguration createKieConfiguration(KieConfig kieConfig, Environment environment) { diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java index 9e58a5f97eb..2c0532a6543 100644 --- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java +++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java @@ -23,6 +23,11 @@ import java.util.Objects; import java.util.Set; +/** + * This event is fired when configuration changed. And the change is already applied to Environment. + * + * Listeners can use Environment to get the latest value. + */ public class ConfigurationChangedEvent { private final Map added; diff --git a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesImpl.java b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesImpl.java index 63cf7679d6a..84f623d74d9 100644 --- a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesImpl.java +++ b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesImpl.java @@ -33,17 +33,28 @@ import com.google.common.eventbus.Subscribe; public class DynamicPropertiesImpl implements DynamicProperties { - private final Map>> stringCallbacks = new HashMap<>(); + private static class Holder { + C callback; - private final Map> intCallbacks = new HashMap<>(); + D defaultValue; - private final Map> longCallbacks = new HashMap<>(); + Holder(C callback, D defaultValue) { + this.callback = callback; + this.defaultValue = defaultValue; + } + } + + private final Map, String>>> stringCallbacks = new HashMap<>(); + + private final Map>> intCallbacks = new HashMap<>(); - private final Map> floatCallbacks = new HashMap<>(); + private final Map>> longCallbacks = new HashMap<>(); - private final Map> doubleCallbacks = new HashMap<>(); + private final Map>> floatCallbacks = new HashMap<>(); - private final Map>> booleanCallbacks = new HashMap<>(); + private final Map>> doubleCallbacks = new HashMap<>(); + + private final Map, Boolean>>> booleanCallbacks = new HashMap<>(); private final Environment environment; @@ -63,79 +74,46 @@ public void onConfigurationChangedEvent(ConfigurationChangedEvent event) { } for (Entry entry : event.getDeleted().entrySet()) { - updateDefault(entry); - } - } - - private void updateDefault(Entry entry) { - if (stringCallbacks.containsKey(entry.getKey())) { - for (Consumer callbacks : stringCallbacks.get(entry.getKey())) { - callbacks.accept(null); - } - } - if (intCallbacks.containsKey(entry.getKey())) { - for (IntConsumer callbacks : intCallbacks.get(entry.getKey())) { - callbacks.accept(0); - } - } - if (longCallbacks.containsKey(entry.getKey())) { - for (LongConsumer callbacks : longCallbacks.get(entry.getKey())) { - callbacks.accept(0L); - } - } - if (floatCallbacks.containsKey(entry.getKey())) { - for (DoubleConsumer callbacks : floatCallbacks.get(entry.getKey())) { - callbacks.accept(0F); - } - } - if (doubleCallbacks.containsKey(entry.getKey())) { - for (DoubleConsumer callbacks : doubleCallbacks.get(entry.getKey())) { - callbacks.accept(0D); - } - } - if (booleanCallbacks.containsKey(entry.getKey())) { - for (Consumer callbacks : booleanCallbacks.get(entry.getKey())) { - callbacks.accept(false); - } + updateValue(entry); } } private void updateValue(Entry entry) { if (stringCallbacks.containsKey(entry.getKey())) { - for (Consumer callbacks : stringCallbacks.get(entry.getKey())) { - callbacks.accept((String) entry.getValue()); + for (Holder, String> callbacks : stringCallbacks.get(entry.getKey())) { + callbacks.callback.accept(environment.getProperty(entry.getKey(), callbacks.defaultValue)); } } if (intCallbacks.containsKey(entry.getKey())) { - for (IntConsumer callbacks : intCallbacks.get(entry.getKey())) { - callbacks.accept((int) entry.getValue()); + for (Holder callbacks : intCallbacks.get(entry.getKey())) { + callbacks.callback.accept(environment.getProperty(entry.getKey(), Integer.class, callbacks.defaultValue)); } } if (longCallbacks.containsKey(entry.getKey())) { - for (LongConsumer callbacks : longCallbacks.get(entry.getKey())) { - callbacks.accept((long) entry.getValue()); + for (Holder callbacks : longCallbacks.get(entry.getKey())) { + callbacks.callback.accept(environment.getProperty(entry.getKey(), Long.class, callbacks.defaultValue)); } } if (floatCallbacks.containsKey(entry.getKey())) { - for (DoubleConsumer callbacks : floatCallbacks.get(entry.getKey())) { - callbacks.accept((float) entry.getValue()); + for (Holder callbacks : floatCallbacks.get(entry.getKey())) { + callbacks.callback.accept(environment.getProperty(entry.getKey(), Float.class, callbacks.defaultValue)); } } if (doubleCallbacks.containsKey(entry.getKey())) { - for (DoubleConsumer callbacks : doubleCallbacks.get(entry.getKey())) { - callbacks.accept((double) entry.getValue()); + for (Holder callbacks : doubleCallbacks.get(entry.getKey())) { + callbacks.callback.accept(environment.getProperty(entry.getKey(), Double.class, callbacks.defaultValue)); } } if (booleanCallbacks.containsKey(entry.getKey())) { - for (Consumer callbacks : booleanCallbacks.get(entry.getKey())) { - callbacks.accept((Boolean) entry.getValue()); + for (Holder, Boolean> callbacks : booleanCallbacks.get(entry.getKey())) { + callbacks.callback.accept(environment.getProperty(entry.getKey(), Boolean.class, callbacks.defaultValue)); } } } @Override public String getStringProperty(String propertyName, Consumer consumer, String defaultValue) { - stringCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(consumer); + stringCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(new Holder<>(consumer, defaultValue)); return environment.getProperty(propertyName, defaultValue); } @@ -146,7 +124,7 @@ public String getStringProperty(String propertyName, String defaultValue) { @Override public int getIntProperty(String propertyName, IntConsumer consumer, int defaultValue) { - intCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(consumer); + intCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(new Holder<>(consumer, defaultValue)); return environment.getProperty(propertyName, int.class, defaultValue); } @@ -157,7 +135,7 @@ public int getIntProperty(String propertyName, int defaultValue) { @Override public long getLongProperty(String propertyName, LongConsumer consumer, long defaultValue) { - longCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(consumer); + longCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(new Holder<>(consumer, defaultValue)); return environment.getProperty(propertyName, long.class, defaultValue); } @@ -168,7 +146,7 @@ public long getLongProperty(String propertyName, long defaultValue) { @Override public float getFloatProperty(String propertyName, DoubleConsumer consumer, float defaultValue) { - floatCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(consumer); + floatCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(new Holder<>(consumer, defaultValue)); return environment.getProperty(propertyName, float.class, defaultValue); } @@ -179,7 +157,7 @@ public float getFloatProperty(String propertyName, float defaultValue) { @Override public double getDoubleProperty(String propertyName, DoubleConsumer consumer, double defaultValue) { - doubleCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(consumer); + doubleCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(new Holder<>(consumer, defaultValue)); return environment.getProperty(propertyName, double.class, defaultValue); } @@ -190,7 +168,7 @@ public double getDoubleProperty(String propertyName, double defaultValue) { @Override public boolean getBooleanProperty(String propertyName, Consumer consumer, boolean defaultValue) { - booleanCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(consumer); + booleanCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(new Holder<>(consumer, defaultValue)); return environment.getProperty(propertyName, boolean.class, defaultValue); } diff --git a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/DynamicPropertiesTest.java b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/DynamicPropertiesTest.java index ecf7bf77928..3f72af9b2cb 100644 --- a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/DynamicPropertiesTest.java +++ b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/DynamicPropertiesTest.java @@ -114,7 +114,7 @@ public void observesSpecifiedStringProperty() throws Exception { String newValue = uniquify("newValue"); - Mockito.when(environment.getProperty(stringPropertyName, stringOldValue)).thenReturn(newValue); + Mockito.when(environment.getProperty(stringPropertyName, (String) null)).thenReturn(newValue); HashMap updated = new HashMap<>(); updated.put(stringPropertyName, newValue); EventManager.post(ConfigurationChangedEvent.createIncremental(updated)); @@ -132,7 +132,7 @@ public void observesSpecifiedIntProperty() throws Exception { int newValue = Randomness.nextInt(); - Mockito.when(environment.getProperty(intPropertyName, int.class, intOldValue)).thenReturn(newValue); + Mockito.when(environment.getProperty(intPropertyName, Integer.class, 0)).thenReturn(newValue); HashMap updated = new HashMap<>(); updated.put(intPropertyName, newValue); EventManager.post(ConfigurationChangedEvent.createIncremental(updated)); @@ -142,20 +142,27 @@ public void observesSpecifiedIntProperty() throws Exception { @Test public void observesSpecifiedLongProperty() throws Exception { + Mockito.when(environment.getProperty(longPropertyName, long.class, 3L)).thenReturn(longOldValue); + long property = dynamicProperties.getLongProperty(longPropertyName, 0); assertThat(property, is(longOldValue)); - property = dynamicProperties.getLongProperty(longPropertyName, value -> longPropertyValue = value, 0); + property = dynamicProperties.getLongProperty(longPropertyName, value -> longPropertyValue = value, 3L); assertThat(property, is(longOldValue)); long newValue = Randomness.nextLong(); - Mockito.when(environment.getProperty(longPropertyName, long.class, longOldValue)).thenReturn(newValue); + Mockito.when(environment.getProperty(longPropertyName, Long.class, 3L)).thenReturn(newValue); HashMap updated = new HashMap<>(); updated.put(longPropertyName, newValue); EventManager.post(ConfigurationChangedEvent.createIncremental(updated)); - poller.assertEventually(() -> longPropertyValue == newValue); + Mockito.when(environment.getProperty(longPropertyName, Long.class, 3L)).thenReturn(3L); + HashMap deleted = new HashMap<>(); + deleted.put(longPropertyName, newValue); + EventManager.post(ConfigurationChangedEvent.createIncremental(new HashMap<>(), new HashMap<>(), deleted)); + + poller.assertEventually(() -> longPropertyValue == 3L); } @Test @@ -163,12 +170,12 @@ public void observesSpecifiedFloatProperty() throws Exception { double property = dynamicProperties.getFloatProperty(floatPropertyName, 0); assertThat(property, closeTo(floatOldValue, ERROR)); - property = dynamicProperties.getFloatProperty(floatPropertyName, value -> floatPropertyValue = value, 0); + property = dynamicProperties.getFloatProperty(floatPropertyName, value -> floatPropertyValue = value, 0f); assertThat(property, closeTo(floatOldValue, ERROR)); float newValue = Double.valueOf(Randomness.nextDouble()).floatValue(); - Mockito.when(environment.getProperty(floatPropertyName, float.class, floatOldValue)).thenReturn(newValue); + Mockito.when(environment.getProperty(floatPropertyName, Float.class, 0f)).thenReturn(newValue); HashMap updated = new HashMap<>(); updated.put(floatPropertyName, newValue); EventManager.post(ConfigurationChangedEvent.createIncremental(updated)); @@ -181,12 +188,12 @@ public void observesSpecifiedDoubleProperty() throws Exception { double property = dynamicProperties.getDoubleProperty(doublePropertyName, 0); assertThat(property, closeTo(doubleOldValue, ERROR)); - property = dynamicProperties.getDoubleProperty(doublePropertyName, value -> doublePropertyValue = value, 0); + property = dynamicProperties.getDoubleProperty(doublePropertyName, value -> doublePropertyValue = value, 0d); assertThat(property, closeTo(doubleOldValue, ERROR)); double newValue = Randomness.nextDouble(); - Mockito.when(environment.getProperty(doublePropertyName, double.class, doubleOldValue)).thenReturn(newValue); + Mockito.when(environment.getProperty(doublePropertyName, Double.class, 0d)).thenReturn(newValue); HashMap updated = new HashMap<>(); updated.put(doublePropertyName, newValue); EventManager.post(ConfigurationChangedEvent.createIncremental(updated)); @@ -207,7 +214,7 @@ public void observesSpecifiedBooleanProperty() throws Exception { boolean newValue = !booleanOldValue; - Mockito.when(environment.getProperty(booleanPropertyName, boolean.class, booleanOldValue)).thenReturn(newValue); + Mockito.when(environment.getProperty(booleanPropertyName, Boolean.class, booleanOldValue)).thenReturn(newValue); HashMap updated = new HashMap<>(); updated.put(booleanPropertyName, newValue); EventManager.post(ConfigurationChangedEvent.createIncremental(updated));