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 27, 2024
1 parent 21259e5 commit 5f1e105
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 5 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 Down Expand Up @@ -248,6 +250,11 @@ private ApolloConfig loadApolloConfig() {

ApolloConfig result = response.getBody();

ConfigSyncType configSyncType=ConfigSyncType.fromString(result.getConfigSyncType());
if(configSyncType!=null&&configSyncType.equals(ConfigSyncType.INCREMENTALSYNC)){
result.setConfigurations(mergeConfigurations(m_configCache.get().getConfigurations(),result.getConfigurationChanges()));
}

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

return result;
Expand Down Expand Up @@ -354,4 +361,24 @@ private List<ServiceDTO> getConfigServices() {

return services;
}

private Map<String, String> mergeConfigurations(Map<String, String> configurations,List<ConfigurationChange> configurationChanges) {
Map<String, String> newConfigurations = Maps.newHashMap(configurations);
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,49 @@ 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 testLoadConfigWithOrderedProperties() throws Exception {
String someKey = "someKey";
Expand Down Expand Up @@ -374,6 +417,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,9 @@
*/
package com.ctrip.framework.apollo.core.dto;

import com.google.common.collect.Lists;

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

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

private String releaseKey;

private String configSyncType;

private List<ConfigurationChange> configurationChanges;

public ApolloConfig() {
}

Expand Down Expand Up @@ -62,6 +69,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 +97,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,71 @@
/*
* 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 String newValue;
private 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;
}

public void setNewValue(String newValue) {
this.newValue = newValue;
}

public void setConfigurationChangeType(ConfigurationChangeType configurationChangeType) {
this.configurationChangeType = 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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.enums;

import com.ctrip.framework.apollo.core.utils.StringUtils;

import java.util.stream.Stream;

/**
* This enum represents all the possible Configuration sync from apollo-config
*
* @author jason
*/
public enum ConfigSyncType {
FULLSYNC("FullSync"), INCREMENTALSYNC("IncrementalSync");

private final String value;

ConfigSyncType(String value) {
this.value = value;
}



/**
* Transforms a given string to its matching {@link ConfigSyncType}.
*
* @param value the string that matches
* @return the matching {@link ConfigSyncType}
* @throws IllegalArgumentException in case the <code>value</code> is empty or there is no
* matching {@link ConfigSyncType}
*/
public static ConfigSyncType fromString(String value) {
if (StringUtils.isEmpty(value)) {
return FULLSYNC;
}
return Stream.of(ConfigSyncType.values()).filter(type -> type.value.equals(value))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(value + " can not map enum"));
}

/**
* @return The string representation of the given {@link ConfigSyncType}
*/
public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.enums;


/**
* @author jason
*/
public enum ConfigurationChangeType {
ADDED, MODIFIED, DELETED
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
</developers>

<properties>
<revision>2.2.0-SNAPSHOT</revision>
<revision>2.5.0-SNAPSHOT</revision>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring-boot.version>2.6.8</spring-boot.version>
Expand Down

0 comments on commit 5f1e105

Please sign in to comment.