Search in sources :

Example 1 with ConfigChange

use of com.ctrip.framework.apollo.model.ConfigChange in project apollo by ctripcorp.

the class DefaultConfig method updateAndCalcConfigChanges.

private Map<String, ConfigChange> updateAndCalcConfigChanges(Properties newConfigProperties, ConfigSourceType sourceType) {
    List<ConfigChange> configChanges = calcPropertyChanges(m_namespace, m_configProperties.get(), newConfigProperties);
    ImmutableMap.Builder<String, ConfigChange> actualChanges = new ImmutableMap.Builder<>();
    // 1. use getProperty to update configChanges's old value
    for (ConfigChange change : configChanges) {
        change.setOldValue(this.getProperty(change.getPropertyName(), change.getOldValue()));
    }
    // 2. update m_configProperties
    updateConfig(newConfigProperties, sourceType);
    clearConfigCache();
    // 3. use getProperty to update configChange's new value and calc the final changes
    for (ConfigChange change : configChanges) {
        change.setNewValue(this.getProperty(change.getPropertyName(), change.getNewValue()));
        switch(change.getChangeType()) {
            case ADDED:
                if (Objects.equals(change.getOldValue(), change.getNewValue())) {
                    break;
                }
                if (change.getOldValue() != null) {
                    change.setChangeType(PropertyChangeType.MODIFIED);
                }
                actualChanges.put(change.getPropertyName(), change);
                break;
            case MODIFIED:
                if (!Objects.equals(change.getOldValue(), change.getNewValue())) {
                    actualChanges.put(change.getPropertyName(), change);
                }
                break;
            case DELETED:
                if (Objects.equals(change.getOldValue(), change.getNewValue())) {
                    break;
                }
                if (change.getNewValue() != null) {
                    change.setChangeType(PropertyChangeType.MODIFIED);
                }
                actualChanges.put(change.getPropertyName(), change);
                break;
            default:
                // do nothing
                break;
        }
    }
    return actualChanges.build();
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with ConfigChange

use of com.ctrip.framework.apollo.model.ConfigChange in project apollo by ctripcorp.

the class AbstractConfigTest method testFireConfigChange_event_notify_once.

@Test
public void testFireConfigChange_event_notify_once() throws ExecutionException, InterruptedException, TimeoutException {
    AbstractConfig abstractConfig = new ErrorConfig();
    final String namespace = "app-namespace-1";
    final String key = "great-key";
    final SettableFuture<ConfigChangeEvent> future1 = SettableFuture.create();
    final SettableFuture<ConfigChangeEvent> future2 = SettableFuture.create();
    final AtomicInteger invokeCount = new AtomicInteger();
    final ConfigChangeListener configChangeListener1 = spy(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            invokeCount.incrementAndGet();
            future1.set(changeEvent);
        }
    });
    final ConfigChangeListener configChangeListener2 = spy(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            invokeCount.incrementAndGet();
            future2.set(changeEvent);
        }
    });
    abstractConfig.addChangeListener(configChangeListener1, Collections.singleton(key));
    abstractConfig.addChangeListener(configChangeListener2, Collections.singleton(key));
    Map<String, ConfigChange> changes = new HashMap<>();
    changes.put(key, new ConfigChange(namespace, key, "old-value", "new-value", PropertyChangeType.MODIFIED));
    ConfigChangeEvent configChangeEvent = new ConfigChangeEvent(namespace, changes);
    abstractConfig.fireConfigChange(configChangeEvent);
    assertEquals(configChangeEvent, future1.get(500, TimeUnit.MILLISECONDS));
    assertEquals(configChangeEvent, future2.get(500, TimeUnit.MILLISECONDS));
    assertEquals(2, invokeCount.get());
    verify(configChangeListener1, times(1)).onChange(Matchers.eq(configChangeEvent));
    verify(configChangeListener2, times(1)).onChange(Matchers.eq(configChangeEvent));
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) HashMap(java.util.HashMap) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Example 3 with ConfigChange

use of com.ctrip.framework.apollo.model.ConfigChange in project apollo by ctripcorp.

the class AbstractConfigTest method testFireConfigChange_cannot_notify.

/**
 * @see AbstractConfig#fireConfigChange(ConfigChangeEvent)
 */
@Test
public void testFireConfigChange_cannot_notify() throws InterruptedException {
    AbstractConfig abstractConfig = spy(new ErrorConfig());
    final String namespace = "app-namespace-0";
    ConfigChangeListener configChangeListener = spy(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
        }
    });
    abstractConfig.addChangeListener(configChangeListener, Collections.singleton("cannot-be-match-key"));
    Map<String, ConfigChange> changes = new HashMap<>();
    changes.put("key1", new ConfigChange(namespace, "key1", null, "new-value", PropertyChangeType.ADDED));
    ConfigChangeEvent configChangeEvent = new ConfigChangeEvent(namespace, changes);
    abstractConfig.fireConfigChange(configChangeEvent);
    abstractConfig.fireConfigChange(namespace, changes);
    // wait a minute for invoking
    Thread.sleep(100);
    verify(configChangeListener, times(0)).onChange(Matchers.<ConfigChangeEvent>any());
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) HashMap(java.util.HashMap) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Example 4 with ConfigChange

use of com.ctrip.framework.apollo.model.ConfigChange in project apollo by ctripcorp.

the class SimpleConfig method onRepositoryChange.

@Override
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
    if (newProperties.equals(m_configProperties)) {
        return;
    }
    Properties newConfigProperties = propertiesFactory.getPropertiesInstance();
    newConfigProperties.putAll(newProperties);
    List<ConfigChange> changes = calcPropertyChanges(namespace, m_configProperties, newConfigProperties);
    Map<String, ConfigChange> changeMap = Maps.uniqueIndex(changes, new Function<ConfigChange, String>() {

        @Override
        public String apply(ConfigChange input) {
            return input.getPropertyName();
        }
    });
    updateConfig(newConfigProperties, m_configRepository.getSourceType());
    clearConfigCache();
    this.fireConfigChange(m_namespace, changeMap);
    Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace);
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) Properties(java.util.Properties)

Example 5 with ConfigChange

use of com.ctrip.framework.apollo.model.ConfigChange in project apollo by ctripcorp.

the class DefaultConfigTest method testFireConfigChangeWithInterestedKeys.

@Test
public void testFireConfigChangeWithInterestedKeys() throws Exception {
    String someKeyChanged = "someKeyChanged";
    String anotherKeyChanged = "anotherKeyChanged";
    String someKeyNotChanged = "someKeyNotChanged";
    String someNamespace = "someNamespace";
    Map<String, ConfigChange> changes = Maps.newHashMap();
    changes.put(someKeyChanged, mock(ConfigChange.class));
    changes.put(anotherKeyChanged, mock(ConfigChange.class));
    ConfigChangeEvent someChangeEvent = new ConfigChangeEvent(someNamespace, changes);
    final SettableFuture<ConfigChangeEvent> interestedInAllKeysFuture = SettableFuture.create();
    ConfigChangeListener interestedInAllKeys = new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            interestedInAllKeysFuture.set(changeEvent);
        }
    };
    final SettableFuture<ConfigChangeEvent> interestedInSomeKeyFuture = SettableFuture.create();
    ConfigChangeListener interestedInSomeKey = new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            interestedInSomeKeyFuture.set(changeEvent);
        }
    };
    final SettableFuture<ConfigChangeEvent> interestedInSomeKeyNotChangedFuture = SettableFuture.create();
    ConfigChangeListener interestedInSomeKeyNotChanged = new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            interestedInSomeKeyNotChangedFuture.set(changeEvent);
        }
    };
    DefaultConfig config = new DefaultConfig(someNamespace, mock(ConfigRepository.class));
    config.addChangeListener(interestedInAllKeys);
    config.addChangeListener(interestedInSomeKey, Sets.newHashSet(someKeyChanged));
    config.addChangeListener(interestedInSomeKeyNotChanged, Sets.newHashSet(someKeyNotChanged));
    config.fireConfigChange(someChangeEvent);
    ConfigChangeEvent changeEvent = interestedInAllKeysFuture.get(500, TimeUnit.MILLISECONDS);
    assertEquals(someChangeEvent, changeEvent);
    {
        // hidden variables in scope
        ConfigChangeEvent actualConfigChangeEvent = interestedInSomeKeyFuture.get(500, TimeUnit.MILLISECONDS);
        assertEquals(someChangeEvent.changedKeys(), actualConfigChangeEvent.changedKeys());
        for (String changedKey : someChangeEvent.changedKeys()) {
            ConfigChange expectConfigChange = someChangeEvent.getChange(changedKey);
            ConfigChange actualConfigChange = actualConfigChangeEvent.getChange(changedKey);
            assertEquals(expectConfigChange, actualConfigChange);
        }
    }
    assertFalse(interestedInSomeKeyNotChangedFuture.isDone());
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Aggregations

ConfigChange (com.ctrip.framework.apollo.model.ConfigChange)15 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)9 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4 ConfigSourceType (com.ctrip.framework.apollo.enums.ConfigSourceType)3 Config (com.ctrip.framework.apollo.Config)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)1 YamlConfigFile (com.ctrip.framework.apollo.internals.YamlConfigFile)1 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)1 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)1 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)1 File (java.io.File)1 Matchers.anyString (org.mockito.Matchers.anyString)1