Skip to content
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

feat: support incremental configuration synchronization client #5325

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ public boolean isConfigServiceCacheKeyIgnoreCase() {
return getBooleanProperty("config-service.cache.key.ignore-case", false);
}

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);
}
Comment on lines +122 to +124
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

Add null check and documentation.

Consider these improvements:

  1. Add null check for the releaseKey parameter
  2. Add Javadoc explaining the method's purpose and return value
  3. Consider returning Optional instead of nullable Release
+  /**
+   * Find a release by its unique release key.
+   *
+   * @param releaseKey the unique identifier of the release
+   * @return the Release if found, null otherwise
+   * @throws IllegalArgumentException if releaseKey is null or empty
+   */
   public Release findByReleaseKey(String releaseKey) {
+    if (releaseKey == null || releaseKey.trim().isEmpty()) {
+      throw new IllegalArgumentException("Release key cannot be null or empty");
+    }
     return releaseRepository.findByReleaseKey(releaseKey);
   }
📝 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
public Release findByReleaseKey(String releaseKey) {
return releaseRepository.findByReleaseKey(releaseKey);
}
/**
* Find a release by its unique release key.
*
* @param releaseKey the unique identifier of the release
* @return the Release if found, null otherwise
* @throws IllegalArgumentException if releaseKey is null or empty
*/
public Release findByReleaseKey(String releaseKey) {
if (releaseKey == null || releaseKey.trim().isEmpty()) {
throw new IllegalArgumentException("Release key cannot be null or empty");
}
return releaseRepository.findByReleaseKey(releaseKey);
}
🧰 Tools
🪛 GitHub Actions: build

[warning] Uses unchecked or unsafe operations


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 @@ -30,6 +30,7 @@
import com.ctrip.framework.apollo.configservice.service.ReleaseMessageServiceWithCache;
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.ConfigServiceWithChangeCache;
import com.ctrip.framework.apollo.configservice.service.config.DefaultConfigService;
import com.ctrip.framework.apollo.configservice.util.AccessKeyUtil;
import io.micrometer.core.instrument.MeterRegistry;
Expand Down Expand Up @@ -69,13 +70,16 @@ public GrayReleaseRulesHolder grayReleaseRulesHolder() {

@Bean
public ConfigService configService() {
if (bizConfig.isConfigServiceIncrementalChangeEnabled()) {
return new ConfigServiceWithChangeCache(releaseService, releaseMessageService,
grayReleaseRulesHolder(), bizConfig, meterRegistry);
}
if (bizConfig.isConfigServiceCacheEnabled()) {
return new ConfigServiceWithCache(releaseService, releaseMessageService,
grayReleaseRulesHolder(), bizConfig, meterRegistry);
}
return new DefaultConfigService(releaseService, grayReleaseRulesHolder());
}

@Bean
public static NoOpPasswordEncoder passwordEncoder() {
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
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;
Expand All @@ -26,12 +27,16 @@
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.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 +47,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 @@ -59,21 +67,25 @@ public class ConfigController {
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 AppNamespaceServiceWithCache appNamespaceService,
final NamespaceUtil namespaceUtil,
final InstanceConfigAuditUtil instanceConfigAuditUtil,
final Gson gson) {
final Gson gson,
final BizConfig bizConfig) {
this.configService = configService;
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 +144,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 +156,44 @@ 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);

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

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

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

List<ConfigurationChange> configurationChanges = configService.calcConfigurationChanges(
latestConfigurations, historyConfigurations);

apolloConfig.setConfigurationChanges(configurationChanges);

apolloConfig.setConfigSyncType(ConfigSyncType.INCREMENTAL_SYNC.getValue());
return apolloConfig;
}

}

apolloConfig.setConfigurations(latestConfigurations);
Comment on lines +163 to +196
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

Handle invalid or missing releaseKeys more gracefully.

  1. When clientSideReleaseKey is -1 or empty, parsing may yield invalid sets of keys.
  2. If some historical releases are missing, the logic proceeds with a full sync by returning null for historyReleases checks.
  3. Consider logging a message or providing partial change sets to avoid possibly confusing behavior for clients.


Tracer.logEvent("Apollo.Config.Found", assembleKey(appId, appClusterNameLoaded,
originalNamespace, dataCenter));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages;

import com.ctrip.framework.apollo.core.dto.ConfigurationChange;
import com.google.common.base.Strings;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* @author Jason Song([email protected])
Expand Down Expand Up @@ -93,6 +100,52 @@ private Release findRelease(String clientAppId, String clientIp, String clientLa
return release;
}

public List<ConfigurationChange> calcConfigurationChanges(
Map<String, String> latestReleaseConfigurations, Map<String, String> historyConfigurations) {
if (latestReleaseConfigurations == null) {
latestReleaseConfigurations = new HashMap<>();
}

if (historyConfigurations == null) {
historyConfigurations = new HashMap<>();
}

Set<String> previousKeys = historyConfigurations.keySet();
Set<String> currentKeys = latestReleaseConfigurations.keySet();

Set<String> commonKeys = Sets.intersection(previousKeys, currentKeys);
Set<String> newKeys = Sets.difference(currentKeys, commonKeys);
Set<String> removedKeys = Sets.difference(previousKeys, commonKeys);

List<ConfigurationChange> changes = Lists.newArrayList();

for (String newKey : newKeys) {
changes.add(new ConfigurationChange(newKey, latestReleaseConfigurations.get(newKey),
"ADDED"));
}

for (String removedKey : removedKeys) {
changes.add(new ConfigurationChange(removedKey, null, "DELETED"));
}

for (String commonKey : commonKeys) {
String previousValue = historyConfigurations.get(commonKey);
String currentValue = latestReleaseConfigurations.get(commonKey);
if (com.google.common.base.Objects.equal(previousValue, currentValue)) {
continue;
}
changes.add(
new ConfigurationChange(commonKey, currentValue, "MODIFIED"));
}

return changes;
}
Comment on lines +103 to +142
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

Enhance robustness and maintainability of configuration change calculation.

Several improvements can be made to the implementation:

  1. Add null checks for map values
  2. Extract change types as constants
  3. Replace deprecated com.google.common.base.Objects.equal with java.util.Objects.equals

Here's the suggested implementation:

+ private static final String CHANGE_TYPE_ADDED = "ADDED";
+ private static final String CHANGE_TYPE_DELETED = "DELETED";
+ private static final String CHANGE_TYPE_MODIFIED = "MODIFIED";

  public List<ConfigurationChange> calcConfigurationChanges(
      Map<String, String> latestReleaseConfigurations, Map<String, String> historyConfigurations) {
    if (latestReleaseConfigurations == null) {
      latestReleaseConfigurations = new HashMap<>();
    }

    if (historyConfigurations == null) {
      historyConfigurations = new HashMap<>();
    }

    Set<String> previousKeys = historyConfigurations.keySet();
    Set<String> currentKeys = latestReleaseConfigurations.keySet();

    Set<String> commonKeys = Sets.intersection(previousKeys, currentKeys);
    Set<String> newKeys = Sets.difference(currentKeys, commonKeys);
    Set<String> removedKeys = Sets.difference(previousKeys, commonKeys);

    List<ConfigurationChange> changes = Lists.newArrayList();

    for (String newKey : newKeys) {
+     String newValue = latestReleaseConfigurations.get(newKey);
+     if (newValue != null) {
-       changes.add(new ConfigurationChange(newKey, latestReleaseConfigurations.get(newKey),
-           "ADDED"));
+       changes.add(new ConfigurationChange(newKey, newValue, CHANGE_TYPE_ADDED));
+     }
    }

    for (String removedKey : removedKeys) {
-     changes.add(new ConfigurationChange(removedKey, null, "DELETED"));
+     changes.add(new ConfigurationChange(removedKey, null, CHANGE_TYPE_DELETED));
    }

    for (String commonKey : commonKeys) {
      String previousValue = historyConfigurations.get(commonKey);
      String currentValue = latestReleaseConfigurations.get(commonKey);
-     if (com.google.common.base.Objects.equal(previousValue, currentValue)) {
+     if (java.util.Objects.equals(previousValue, currentValue)) {
        continue;
      }
      changes.add(
-         new ConfigurationChange(commonKey, currentValue, "MODIFIED"));
+         new ConfigurationChange(commonKey, currentValue, CHANGE_TYPE_MODIFIED));
    }

    return changes;
  }
📝 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
public List<ConfigurationChange> calcConfigurationChanges(
Map<String, String> latestReleaseConfigurations, Map<String, String> historyConfigurations) {
if (latestReleaseConfigurations == null) {
latestReleaseConfigurations = new HashMap<>();
}
if (historyConfigurations == null) {
historyConfigurations = new HashMap<>();
}
Set<String> previousKeys = historyConfigurations.keySet();
Set<String> currentKeys = latestReleaseConfigurations.keySet();
Set<String> commonKeys = Sets.intersection(previousKeys, currentKeys);
Set<String> newKeys = Sets.difference(currentKeys, commonKeys);
Set<String> removedKeys = Sets.difference(previousKeys, commonKeys);
List<ConfigurationChange> changes = Lists.newArrayList();
for (String newKey : newKeys) {
changes.add(new ConfigurationChange(newKey, latestReleaseConfigurations.get(newKey),
"ADDED"));
}
for (String removedKey : removedKeys) {
changes.add(new ConfigurationChange(removedKey, null, "DELETED"));
}
for (String commonKey : commonKeys) {
String previousValue = historyConfigurations.get(commonKey);
String currentValue = latestReleaseConfigurations.get(commonKey);
if (com.google.common.base.Objects.equal(previousValue, currentValue)) {
continue;
}
changes.add(
new ConfigurationChange(commonKey, currentValue, "MODIFIED"));
}
return changes;
}
private static final String CHANGE_TYPE_ADDED = "ADDED";
private static final String CHANGE_TYPE_DELETED = "DELETED";
private static final String CHANGE_TYPE_MODIFIED = "MODIFIED";
public List<ConfigurationChange> calcConfigurationChanges(
Map<String, String> latestReleaseConfigurations, Map<String, String> historyConfigurations) {
if (latestReleaseConfigurations == null) {
latestReleaseConfigurations = new HashMap<>();
}
if (historyConfigurations == null) {
historyConfigurations = new HashMap<>();
}
Set<String> previousKeys = historyConfigurations.keySet();
Set<String> currentKeys = latestReleaseConfigurations.keySet();
Set<String> commonKeys = Sets.intersection(previousKeys, currentKeys);
Set<String> newKeys = Sets.difference(currentKeys, commonKeys);
Set<String> removedKeys = Sets.difference(previousKeys, commonKeys);
List<ConfigurationChange> changes = Lists.newArrayList();
for (String newKey : newKeys) {
String newValue = latestReleaseConfigurations.get(newKey);
if (newValue != null) {
changes.add(new ConfigurationChange(newKey, newValue, CHANGE_TYPE_ADDED));
}
}
for (String removedKey : removedKeys) {
changes.add(new ConfigurationChange(removedKey, null, CHANGE_TYPE_DELETED));
}
for (String commonKey : commonKeys) {
String previousValue = historyConfigurations.get(commonKey);
String currentValue = latestReleaseConfigurations.get(commonKey);
if (java.util.Objects.equals(previousValue, currentValue)) {
continue;
}
changes.add(
new ConfigurationChange(commonKey, currentValue, CHANGE_TYPE_MODIFIED));
}
return changes;
}


@Override
public Map<String, Release> findReleasesByReleaseKeys(Set<String> releaseKeys) {
return null;
}
Comment on lines +144 to +147
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Return empty map instead of null.

Returning null violates the interface contract and forces unnecessary null checks in clients. Return an empty map instead.

   @Override
   public Map<String, Release> findReleasesByReleaseKeys(Set<String> releaseKeys) {
-    return null;
+    return new HashMap<>();
   }
📝 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
@Override
public Map<String, Release> findReleasesByReleaseKeys(Set<String> releaseKeys) {
return null;
}
@Override
public Map<String, Release> findReleasesByReleaseKeys(Set<String> releaseKeys) {
return new HashMap<>();
}

Comment on lines +144 to +147
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Implementation required: findReleasesByReleaseKeys returns null.

The method should not return null as it's part of the interface contract. Implement proper release lookup logic.


/**
* Find active release by id
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* @author Jason Song([email protected])
*/
public interface ConfigService extends ReleaseMessageListener {
public interface ConfigService extends ReleaseMessageListener, IncrementalSyncConfigService {

/**
* Load config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public class ConfigServiceWithCache extends AbstractConfigService {
private static final String TRACER_EVENT_CACHE_GET = "ConfigCache.Get";
private static final String TRACER_EVENT_CACHE_GET_ID = "ConfigCache.GetById";

private final ReleaseService releaseService;
protected final ReleaseService releaseService;
private final ReleaseMessageService releaseMessageService;
private final BizConfig bizConfig;
protected final BizConfig bizConfig;
private final MeterRegistry meterRegistry;

private LoadingCache<String, ConfigCacheEntry> configCache;
Expand Down
Loading
Loading