Skip to content

Commit

Permalink
feat: support incremental configuration synchronization client
Browse files Browse the repository at this point in the history
  • Loading branch information
jackie-coming committed Nov 28, 2024
1 parent 21259e5 commit f654cb3
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.dto.ApolloConfig;
import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages;
import com.ctrip.framework.apollo.core.dto.ConfigurationChange;
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.ctrip.framework.apollo.core.enums.ConfigSyncType;
import com.ctrip.framework.apollo.core.schedule.ExponentialSchedulePolicy;
import com.ctrip.framework.apollo.core.schedule.SchedulePolicy;
import com.ctrip.framework.apollo.core.signature.Signature;
Expand All @@ -46,10 +48,8 @@
import com.google.common.net.UrlEscapers;
import com.google.common.util.concurrent.RateLimiter;
import com.google.gson.Gson;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -247,6 +247,24 @@ private ApolloConfig loadApolloConfig() {
}

ApolloConfig result = response.getBody();
if(result!=null){
ConfigSyncType configSyncType=ConfigSyncType.fromString(result.getConfigSyncType());

if(configSyncType!=null&&configSyncType.equals(ConfigSyncType.INCREMENTALSYNC)){

Map<String, String> previousConfigurations=null;

ApolloConfig previousConfig = m_configCache.get();

if(previousConfig!=null){
previousConfigurations=previousConfig.getConfigurations();
}

result.setConfigurations(mergeConfigurations(previousConfigurations,result.getConfigurationChanges()));

}

}

logger.debug("Loaded config for {}: {}", m_namespace, result);

Expand Down Expand Up @@ -354,4 +372,33 @@ private List<ServiceDTO> getConfigServices() {

return services;
}

public Map<String, String> mergeConfigurations(Map<String, String> previousConfigurations,List<ConfigurationChange> configurationChanges) {
Map<String, String> newConfigurations = new HashMap<>();

if(previousConfigurations!=null){
newConfigurations=Maps.newHashMap(previousConfigurations);
}

if (configurationChanges == null) {
return newConfigurations;
}

for (ConfigurationChange change : configurationChanges) {
switch (change.getConfigurationChangeType()) {
case ADDED:
case MODIFIED:
newConfigurations.put(change.getKey(), change.getNewValue());
break;
case DELETED:
newConfigurations.remove(change.getKey());
break;
default:
//do nothing
break;
}
}

return newConfigurations;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
import static org.mockito.Mockito.when;

import com.ctrip.framework.apollo.build.MockInjector;
import com.ctrip.framework.apollo.core.dto.ApolloConfig;
import com.ctrip.framework.apollo.core.dto.ApolloConfigNotification;
import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages;
import com.ctrip.framework.apollo.core.dto.ServiceDTO;
import com.ctrip.framework.apollo.core.dto.*;
import com.ctrip.framework.apollo.core.enums.ConfigSyncType;
import com.ctrip.framework.apollo.core.enums.ConfigurationChangeType;
import com.ctrip.framework.apollo.core.signature.Signature;
import com.ctrip.framework.apollo.enums.ConfigSourceType;
import com.ctrip.framework.apollo.exceptions.ApolloConfigException;
Expand All @@ -53,6 +52,7 @@
import com.google.common.util.concurrent.SettableFuture;
import com.google.gson.Gson;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
Expand Down Expand Up @@ -153,6 +153,116 @@ public void testLoadConfig() throws Exception {
remoteConfigLongPollService.stopLongPollingRefresh();
}

@Test
public void testLoadConfigWithIncrementalSync() throws Exception {

String someKey = "someKey";
String someValue = "someValue";
String someKey1 = "someKey1";
String someValue1 = "someKey1";
Map<String, String> configurations = Maps.newHashMap();
configurations.put(someKey, someValue);
configurations.put(someKey1, someValue1);
ApolloConfig someApolloConfig = assembleApolloConfig(configurations);

when(someResponse.getStatusCode()).thenReturn(200);
when(someResponse.getBody()).thenReturn(someApolloConfig);

RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);

remoteConfigRepository.sync();


List<ConfigurationChange> configurationChanges=new ArrayList<>();
String someNewValue = "someNewValue";
configurationChanges.add(new ConfigurationChange(someKey, someNewValue, ConfigurationChangeType.MODIFIED));
configurationChanges.add(new ConfigurationChange(someKey1, null, ConfigurationChangeType.DELETED));
String someKey2 = "someKey2";
String someValue2 = "someValue2";
configurationChanges.add(new ConfigurationChange(someKey2, someValue2, ConfigurationChangeType.ADDED));
ApolloConfig someApolloConfigWithIncrementalSync = assembleApolloConfigWithIncrementalSync(configurationChanges);

when(someResponse.getStatusCode()).thenReturn(200);
when(someResponse.getBody()).thenReturn(someApolloConfigWithIncrementalSync);

remoteConfigRepository.sync();

Properties config = remoteConfigRepository.getConfig();

assertEquals(2, config.size());
assertEquals("someNewValue", config.getProperty("someKey"));
assertEquals("someValue2", config.getProperty("someKey2"));
assertEquals(ConfigSourceType.REMOTE, remoteConfigRepository.getSourceType());
remoteConfigLongPollService.stopLongPollingRefresh();
}

@Test
public void testMergeConfigurations() throws Exception {
String key1 = "key1";
String value1 = "value1";
String anotherValue1 = "anotherValue1";

String key3 = "key3";
String value3 = "value3";
Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1,key3,value3);

List<ConfigurationChange> configurationChanges=new ArrayList<>();
configurationChanges.add(new ConfigurationChange(key1, anotherValue1, ConfigurationChangeType.MODIFIED));
String key2 = "key2";
String value2 = "value2";
configurationChanges.add(new ConfigurationChange(key2, value2, ConfigurationChangeType.ADDED));
configurationChanges.add(new ConfigurationChange(key3, null, ConfigurationChangeType.DELETED));

RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
Map<String, String> result=remoteConfigRepository.mergeConfigurations(previousConfigurations, configurationChanges);

assertEquals(2, result.size());
assertEquals(anotherValue1, result.get(key1));
assertEquals(value2, result.get(key2));
}
@Test
public void testMergeConfigurationWithPreviousConfigurationsIsNULL() throws Exception {
String key1 = "key1";
String value1 = "value1";

String key3 = "key3";
String value3 = "value3";

Map<String, String> previousConfigurations = ImmutableMap.of(key1, value1,key3,value3);


RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
Map<String, String> result=remoteConfigRepository.mergeConfigurations(previousConfigurations, null);

assertEquals(2, result.size());
assertEquals(value1, result.get(key1));
assertEquals(value3, result.get(key3));
}

@Test
public void testMergeConfigurationWithChangesIsNULL() throws Exception {
String key1 = "key1";
String value1 = "value1";
String anotherValue1 = "anotherValue1";

String key3 = "key3";
String value3 = "value3";

List<ConfigurationChange> configurationChanges=new ArrayList<>();
configurationChanges.add(new ConfigurationChange(key1, anotherValue1, ConfigurationChangeType.MODIFIED));
String key2 = "key2";
String value2 = "value2";
configurationChanges.add(new ConfigurationChange(key2, value2, ConfigurationChangeType.ADDED));
configurationChanges.add(new ConfigurationChange(key3, null, ConfigurationChangeType.DELETED));

RemoteConfigRepository remoteConfigRepository = new RemoteConfigRepository(someNamespace);
Map<String, String> result=remoteConfigRepository.mergeConfigurations(null, configurationChanges);

assertEquals(2, result.size());
assertEquals(anotherValue1, result.get(key1));
assertEquals(value2, result.get(key2));
}

@Test
public void testLoadConfigWithOrderedProperties() throws Exception {
String someKey = "someKey";
Expand Down Expand Up @@ -374,6 +484,17 @@ private ApolloConfig assembleApolloConfig(Map<String, String> configurations) {

return apolloConfig;
}
private ApolloConfig assembleApolloConfigWithIncrementalSync(List<ConfigurationChange> configurationChanges) {
String someAppId = "appId";
String someClusterName = "cluster";
String someReleaseKey = "1";
ApolloConfig apolloConfig =
new ApolloConfig(someAppId, someClusterName, someNamespace, someReleaseKey);

apolloConfig.setConfigSyncType(ConfigSyncType.INCREMENTALSYNC.getValue());
apolloConfig.setConfigurationChanges(configurationChanges);
return apolloConfig;
}

public static class MockConfigUtil extends ConfigUtil {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.ctrip.framework.apollo.core.dto;


import java.util.List;
import java.util.Map;

/**
Expand All @@ -33,6 +35,10 @@ public class ApolloConfig {

private String releaseKey;

private String configSyncType;

private List<ConfigurationChange> configurationChanges;

public ApolloConfig() {
}

Expand Down Expand Up @@ -62,6 +68,14 @@ public String getReleaseKey() {
return releaseKey;
}

public String getConfigSyncType() {
return configSyncType;
}

public List<ConfigurationChange> getConfigurationChanges() {
return configurationChanges;
}

public Map<String, String> getConfigurations() {
return configurations;
}
Expand All @@ -82,10 +96,20 @@ public void setReleaseKey(String releaseKey) {
this.releaseKey = releaseKey;
}

public void setConfigSyncType(String configSyncType) {
this.configSyncType = configSyncType;
}


public void setConfigurations(Map<String, String> configurations) {
this.configurations = configurations;
}

public void setConfigurationChanges(List<ConfigurationChange> configurationChanges) {
this.configurationChanges = configurationChanges;
}


@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ApolloConfig{");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed 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 com.ctrip.framework.apollo.core.dto;


import com.ctrip.framework.apollo.core.enums.ConfigurationChangeType;

/**
* Holds the information for a Configuration change.
* @author jason
*/
public class ConfigurationChange {
private final String key;
private final String newValue;
private final ConfigurationChangeType configurationChangeType;

/**
* Constructor.
* @param key the key whose value is changed
* @param newValue the value after change
* @param configurationChangeType the change type
*/
public ConfigurationChange(String key, String newValue, ConfigurationChangeType configurationChangeType) {
this.key = key;
this.newValue = newValue;
this.configurationChangeType = configurationChangeType;
}

public String getKey() {
return key;
}
public String getNewValue() {
return newValue;
}

public ConfigurationChangeType getConfigurationChangeType() {
return configurationChangeType;
}


@Override
public String toString() {
final StringBuilder sb = new StringBuilder("ConfigChange{");
sb.append(" key='").append(key).append('\'');
sb.append(", newValue='").append(newValue).append('\'');
sb.append(", configurationChangeType=").append(configurationChangeType);
sb.append('}');
return sb.toString();
}
}
Loading

0 comments on commit f654cb3

Please sign in to comment.