Skip to content

feat: support incremental configuration synchronization client #5373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4e8a699
init
jackie-coming Oct 31, 2024
26a0e1b
init config
jackie-coming Nov 18, 2024
2dd9352
code review
jackie-coming Nov 18, 2024
b13968c
add test
jackie-coming Nov 20, 2024
24bda1d
add test
jackie-coming Nov 21, 2024
cf5145f
add test
jackie-coming Nov 26, 2024
8cafda2
code format
jackie-coming Nov 27, 2024
72676c0
code format
jackie-coming Nov 27, 2024
065a092
feat: support incremental configuration synchronization client
jackie-coming Oct 31, 2024
e69f098
Merge remote-tracking branch 'origin/feautre/IncrementalSync' into fe…
jackie-coming Nov 27, 2024
a6a412a
code format
jackie-coming Nov 27, 2024
07bb41c
feat: support incremental configuration synchronization client
jackie-coming Nov 27, 2024
a4b356c
code format
jackie-coming Dec 8, 2024
5e37f55
update pom version
jackie-coming Dec 14, 2024
8512776
code format
jackie-coming Dec 20, 2024
5a76da9
code format
jackie-coming Dec 21, 2024
a4a2d05
code format
jackie-coming Dec 21, 2024
04f50ac
Merge branch 'master' into feautre/IncrementalSync
nobodyiam Dec 28, 2024
1b20e4e
update flag
jackie-coming Dec 29, 2024
4f06613
Merge remote-tracking branch 'origin/feautre/IncrementalSync' into fe…
jackie-coming Dec 29, 2024
26e8db5
Merge branch 'up_master' into feautre/IncrementalSync
jackie-coming Jan 28, 2025
56df3a8
code refactor
jackie-coming Jan 28, 2025
3e9151f
merge master
jackie-coming Apr 1, 2025
34b390d
refactor IncrementalSync
jackie-coming Apr 14, 2025
9bba12c
refactor IncrementalSync
jackie-coming Apr 23, 2025
d4ec343
Merge branch 'master' into feautre/IncrementalSync
nobodyiam May 4, 2025
ab0264a
Update apollo-configservice/src/main/java/com/ctrip/framework/apollo/…
jackie-coming May 17, 2025
2d09cb3
Update apollo-configservice/src/main/java/com/ctrip/framework/apollo/…
jackie-coming May 17, 2025
b27adb0
Update pom.xml
jackie-coming May 17, 2025
5fb6535
Update docs/zh/deployment/distributed-deployment-guide.md
jackie-coming May 17, 2025
e4b686f
refactor IncrementalSync
jackie-coming May 17, 2025
6e8f215
Merge remote-tracking branch 'origin/feautre/IncrementalSync' into fe…
jackie-coming May 17, 2025
ca4a81b
refactor IncrementalSync
jackie-coming May 17, 2025
a0ee155
add test
jackie-coming May 17, 2025
ea5337d
add test
jackie-coming May 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Current File",
"request": "launch",
"mainClass": "${file}"
},
{
"type": "java",
"name": "LocalAdminServiceApplication",
"request": "launch",
"mainClass": "com.ctrip.framework.apollo.LocalAdminServiceApplication",
"projectName": "apollo-adminservice"
},
{
"type": "java",
"name": "AdminServiceApplication",
"request": "launch",
"mainClass": "com.ctrip.framework.apollo.adminservice.AdminServiceApplication",
"projectName": "apollo-adminservice"
},
{
"type": "java",
"name": "ApolloApplication",
"request": "launch",
"mainClass": "com.ctrip.framework.apollo.assembly.ApolloApplication",
"projectName": "apollo-assembly"
},
{
"type": "java",
"name": "LocalApolloApplication",
"request": "launch",
"mainClass": "com.ctrip.framework.apollo.assembly.LocalApolloApplication",
"projectName": "apollo-assembly"
},
{
"type": "java",
"name": "ApolloSqlConverter",
"request": "launch",
"mainClass": "com.ctrip.framework.apollo.build.sql.converter.ApolloSqlConverter",
"projectName": "apollo-build-sql-converter"
},
{
"type": "java",
"name": "LocalConfigServiceApplication",
"request": "launch",
"mainClass": "com.ctrip.framework.apollo.LocalConfigServiceApplication",
"projectName": "apollo-configservice"
},
{
"type": "java",
"name": "ConfigServiceApplication",
"request": "launch",
"mainClass": "com.ctrip.framework.apollo.configservice.ConfigServiceApplication",
"projectName": "apollo-configservice"
},
{
"type": "java",
"name": "LocalPortalApplication",
"request": "launch",
"mainClass": "com.ctrip.framework.apollo.LocalPortalApplication",
"projectName": "apollo-portal"
},
{
"type": "java",
"name": "PortalApplication",
"request": "launch",
"mainClass": "com.ctrip.framework.apollo.portal.PortalApplication",
"projectName": "apollo-portal"
}
]
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"java.compile.nullAnalysis.mode": "automatic",
"java.configuration.updateBuildConfiguration": "automatic"
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ public long getInstanceConfigAuditTimeThresholdInMilli() {
return TimeUnit.MINUTES.toMillis(timeThreshold);
}

public boolean isConfigServiceIncrementalChangeEnabled() {
return getBooleanProperty("config-service.incremental.change.enabled", false);
}

int checkInt(int value, int min, int max, int defaultValue) {
if (value >= min && value <= max) {
return value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ Release findFirstByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseOrderBy

Release findByIdAndIsAbandonedFalse(long id);

Release findByReleaseKey(String releaseKey);

List<Release> findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(String appId, String clusterName, String namespaceName, Pageable page);

List<Release> findByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseOrderByIdDesc(String appId, String clusterName, String namespaceName, Pageable page);

List<Release> findByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseAndIdBetweenOrderByIdDesc(String appId, String clusterName, String namespaceName, long fromId, long toId);

List<Release> findByReleaseKeyIn(Set<String> releaseKey);
List<Release> findByReleaseKeyIn(Set<String> releaseKeys);

List<Release> findByIdIn(Set<Long> releaseIds);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public List<Release> findByReleaseKeys(Set<String> releaseKeys) {
return releaseRepository.findByReleaseKeyIn(releaseKeys);
}

public Release findByReleaseKey(String releaseKey) {
return releaseRepository.findByReleaseKey(releaseKey);
}

public Release findLatestActiveRelease(Namespace namespace) {
return findLatestActiveRelease(namespace.getAppId(),
namespace.getClusterName(), namespace.getNamespaceName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.ctrip.framework.apollo.configservice.service.config.ConfigService;
import com.ctrip.framework.apollo.configservice.service.config.ConfigServiceWithCache;
import com.ctrip.framework.apollo.configservice.service.config.DefaultConfigService;
import com.ctrip.framework.apollo.configservice.service.config.DefaultIncrementalSyncService;
import com.ctrip.framework.apollo.configservice.service.config.IncrementalSyncService;
import com.ctrip.framework.apollo.configservice.util.AccessKeyUtil;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
Expand Down Expand Up @@ -69,13 +71,18 @@ public GrayReleaseRulesHolder grayReleaseRulesHolder() {

@Bean
public ConfigService configService() {
// enable local cache
if (bizConfig.isConfigServiceCacheEnabled()) {
return new ConfigServiceWithCache(releaseService, releaseMessageService,
grayReleaseRulesHolder(), bizConfig, meterRegistry);
}
return new DefaultConfigService(releaseService, grayReleaseRulesHolder());
}

@Bean
public IncrementalSyncService incrementalSyncService() {
return new DefaultIncrementalSyncService();
}
@Bean
public static NoOpPasswordEncoder passwordEncoder() {
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,29 @@
*/
package com.ctrip.framework.apollo.configservice.controller;

import com.ctrip.framework.apollo.biz.config.BizConfig;
import com.ctrip.framework.apollo.biz.entity.Release;
import com.ctrip.framework.apollo.common.entity.AppNamespace;
import com.ctrip.framework.apollo.common.utils.WebUtils;
import com.ctrip.framework.apollo.configservice.service.AppNamespaceServiceWithCache;
import com.ctrip.framework.apollo.configservice.service.config.ConfigService;
import com.ctrip.framework.apollo.configservice.service.config.IncrementalSyncService;
import com.ctrip.framework.apollo.configservice.util.InstanceConfigAuditUtil;
import com.ctrip.framework.apollo.configservice.util.NamespaceUtil;
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.enums.ConfigSyncType;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -42,6 +49,9 @@
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -55,25 +65,33 @@
public class ConfigController {

private final ConfigService configService;

private final IncrementalSyncService incrementalSyncService;
private final AppNamespaceServiceWithCache appNamespaceService;
private final NamespaceUtil namespaceUtil;
private final InstanceConfigAuditUtil instanceConfigAuditUtil;
private final Gson gson;
private final BizConfig bizConfig;


private static final Type configurationTypeReference = new TypeToken<Map<String, String>>() {
}.getType();
}.getType();

public ConfigController(
final ConfigService configService,
final IncrementalSyncService incrementalSyncService,
final AppNamespaceServiceWithCache appNamespaceService,
final NamespaceUtil namespaceUtil,
final InstanceConfigAuditUtil instanceConfigAuditUtil,
final Gson gson) {
final Gson gson,
final BizConfig bizConfig) {
this.configService = configService;
this.incrementalSyncService = incrementalSyncService;
this.appNamespaceService = appNamespaceService;
this.namespaceUtil = namespaceUtil;
this.instanceConfigAuditUtil = instanceConfigAuditUtil;
this.gson = gson;
this.bizConfig = bizConfig;
}

@GetMapping(value = "/{appId}/{clusterName}/{namespace:.+}")
Expand Down Expand Up @@ -132,10 +150,10 @@ public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String

auditReleases(appId, clusterName, dataCenter, clientIp, releases);

String mergedReleaseKey = releases.stream().map(Release::getReleaseKey)
.collect(Collectors.joining(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR));
String latestMergedReleaseKey = releases.stream().map(Release::getReleaseKey)
.collect(Collectors.joining(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR));

if (mergedReleaseKey.equals(clientSideReleaseKey)) {
if (latestMergedReleaseKey.equals(clientSideReleaseKey)) {
// Client side configuration is the same with server side, return 304
response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
Tracer.logEvent("Apollo.Config.NotModified",
Expand All @@ -144,8 +162,51 @@ public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String
}

ApolloConfig apolloConfig = new ApolloConfig(appId, appClusterNameLoaded, originalNamespace,
mergedReleaseKey);
apolloConfig.setConfigurations(mergeReleaseConfigurations(releases));
latestMergedReleaseKey);

Map<String, String> latestConfigurations = mergeReleaseConfigurations(releases);

try {
if (bizConfig.isConfigServiceIncrementalChangeEnabled()) {
LinkedHashSet<String> clientSideReleaseKeys = Sets.newLinkedHashSet(
Arrays.stream(
clientSideReleaseKey.split(Pattern.quote(ConfigConsts.CLUSTER_NAMESPACE_SEPARATOR)))
.collect(Collectors.toList()));

Map<String, Release> clientSideReleases = configService.findReleasesByReleaseKeys(
clientSideReleaseKeys);
//find history releases
if (clientSideReleases != null) {
//order by clientSideReleaseKeys
List<Release> historyReleasesWithOrder = new ArrayList<>();
for (String item : clientSideReleaseKeys) {
Release release = clientSideReleases.get(item);
if (release != null) {
historyReleasesWithOrder.add(release);
}
}

Map<String, String> clientSideConfigurations = mergeReleaseConfigurations
(historyReleasesWithOrder);

List<ConfigurationChange> configurationChanges = incrementalSyncService.getConfigurationChanges(
latestMergedReleaseKey,
latestConfigurations, clientSideReleaseKey, clientSideConfigurations);

apolloConfig.setConfigurationChanges(configurationChanges);

apolloConfig.setConfigSyncType(ConfigSyncType.INCREMENTAL_SYNC.getValue());
Tracer.logEvent("Apollo.Config.Found", assembleKey(appId, appClusterNameLoaded,
originalNamespace, dataCenter));
return apolloConfig;
}

}
} catch (ExecutionException e) {
//fallback to full sync
}
Comment on lines +205 to +207
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve exception handling for better troubleshooting

The current exception handling silently falls back to full sync without logging the error details, which could make debugging difficult in production.

Add logging to help troubleshoot sync issues:

-  } catch (ExecutionException e) {
-    //fallback to full sync
+  } catch (ExecutionException e) {
+    Tracer.logError("Failed to do incremental sync, fallback to full sync", e);
+    //fallback to full sync
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
} catch (ExecutionException e) {
//fallback to full sync
}
} catch (ExecutionException e) {
Tracer.logError("Failed to do incremental sync, fallback to full sync", e);
//fallback to full sync
}
🤖 Prompt for AI Agents
In
apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/controller/ConfigController.java
around lines 205 to 207, the catch block for ExecutionException silently falls
back to full sync without logging the exception details. Modify the catch block
to log the exception message and stack trace using the appropriate logger to aid
in troubleshooting sync issues.


apolloConfig.setConfigurations(latestConfigurations);

Tracer.logEvent("Apollo.Config.Found", assembleKey(appId, appClusterNameLoaded,
originalNamespace, dataCenter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.ctrip.framework.apollo.biz.entity.Release;
import com.ctrip.framework.apollo.biz.message.ReleaseMessageListener;
import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

/**
* @author Jason Song(song_s@ctrip.com)
Expand All @@ -40,4 +43,11 @@ public interface ConfigService extends ReleaseMessageListener {
*/
Release loadConfig(String clientAppId, String clientIp, String clientLabel, String configAppId, String
configClusterName, String configNamespace, String dataCenter, ApolloNotificationMessages clientMessages);


/**
* @param releaseKeys
* @return the ReleaseMap
*/
Map<String, Release> findReleasesByReleaseKeys(Set<String> releaseKeys) throws ExecutionException;
}
Loading
Loading