Skip to content

Commit

Permalink
code format
Browse files Browse the repository at this point in the history
  • Loading branch information
jackie-coming committed Dec 21, 2024
1 parent 5a76da9 commit a4a2d05
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ public ConfigController(

@GetMapping(value = "/{appId}/{clusterName}/{namespace:.+}")
public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String clusterName,
@PathVariable String namespace,
@RequestParam(value = "dataCenter", required = false) String dataCenter,
@RequestParam(value = "releaseKey", defaultValue = "-1") String clientSideReleaseKey,
@RequestParam(value = "ip", required = false) String clientIp,
@RequestParam(value = "label", required = false) String clientLabel,
@RequestParam(value = "messages", required = false) String messagesAsString,
HttpServletRequest request, HttpServletResponse response) throws IOException {
@PathVariable String namespace,
@RequestParam(value = "dataCenter", required = false) String dataCenter,
@RequestParam(value = "releaseKey", defaultValue = "-1") String clientSideReleaseKey,
@RequestParam(value = "ip", required = false) String clientIp,
@RequestParam(value = "label", required = false) String clientLabel,
@RequestParam(value = "messages", required = false) String messagesAsString,
HttpServletRequest request, HttpServletResponse response) throws IOException {
String originalNamespace = namespace;
//strip out .properties suffix
namespace = namespaceUtil.filterNamespaceName(namespace);
Expand All @@ -112,8 +112,7 @@ public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String

String appClusterNameLoaded = clusterName;
if (!ConfigConsts.NO_APPID_PLACEHOLDER.equalsIgnoreCase(appId)) {
Release currentAppRelease = configService.loadConfig(appId, clientIp, clientLabel, appId,
clusterName, namespace,
Release currentAppRelease = configService.loadConfig(appId, clientIp, clientLabel, appId, clusterName, namespace,
dataCenter, clientMessages);

if (currentAppRelease != null) {
Expand All @@ -125,8 +124,7 @@ public ApolloConfig queryConfig(@PathVariable String appId, @PathVariable String

//if namespace does not belong to this appId, should check if there is a public configuration
if (!namespaceBelongsToAppId(appId, namespace)) {
Release publicRelease = this.findPublicConfig(appId, clientIp, clientLabel, clusterName,
namespace,
Release publicRelease = this.findPublicConfig(appId, clientIp, clientLabel, clusterName, namespace,
dataCenter, clientMessages);
if (Objects.nonNull(publicRelease)) {
releases.add(publicRelease);
Expand Down Expand Up @@ -220,9 +218,8 @@ private boolean namespaceBelongsToAppId(String appId, String namespaceName) {
* @param namespace the namespace
* @param dataCenter the datacenter
*/
private Release findPublicConfig(String clientAppId, String clientIp, String clientLabel,
String clusterName,
String namespace, String dataCenter, ApolloNotificationMessages clientMessages) {
private Release findPublicConfig(String clientAppId, String clientIp, String clientLabel, String clusterName,
String namespace, String dataCenter, ApolloNotificationMessages clientMessages) {
AppNamespace appNamespace = appNamespaceService.findPublicNamespaceByName(namespace);

//check whether the namespace's appId equals to current one
Expand All @@ -232,13 +229,13 @@ private Release findPublicConfig(String clientAppId, String clientIp, String cli

String publicConfigAppId = appNamespace.getAppId();

return configService.loadConfig(clientAppId, clientIp, clientLabel, publicConfigAppId,
clusterName, namespace, dataCenter,
return configService.loadConfig(clientAppId, clientIp, clientLabel, publicConfigAppId, clusterName, namespace, dataCenter,
clientMessages);
}

/**
* Merge configurations of releases. Release in lower index override those in higher index
* Merge configurations of releases.
* Release in lower index override those in higher index
*/
Map<String, String> mergeReleaseConfigurations(List<Release> releases) {
Map<String, String> result = Maps.newLinkedHashMap();
Expand All @@ -257,7 +254,7 @@ private String assembleKey(String appId, String cluster, String namespace, Strin
}

private void auditReleases(String appId, String cluster, String dataCenter, String clientIp,
List<Release> releases) {
List<Release> releases) {
if (Strings.isNullOrEmpty(clientIp)) {
//no need to audit instance config when there is no ip
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages;

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

import com.google.common.collect.Lists;
Expand All @@ -45,13 +44,11 @@ protected AbstractConfigService(final GrayReleaseRulesHolder grayReleaseRulesHol
}

@Override
public Release loadConfig(String clientAppId, String clientIp, String clientLabel,
String configAppId, String configClusterName,
public Release loadConfig(String clientAppId, String clientIp, String clientLabel, String configAppId, String configClusterName,
String configNamespace, String dataCenter, ApolloNotificationMessages clientMessages) {
// load from specified cluster first
if (!Objects.equals(ConfigConsts.CLUSTER_NAME_DEFAULT, configClusterName)) {
Release clusterRelease = findRelease(clientAppId, clientIp, clientLabel, configAppId,
configClusterName, configNamespace,
Release clusterRelease = findRelease(clientAppId, clientIp, clientLabel, configAppId, configClusterName, configNamespace,
clientMessages);

if (Objects.nonNull(clusterRelease)) {
Expand All @@ -61,37 +58,33 @@ public Release loadConfig(String clientAppId, String clientIp, String clientLabe

// try to load via data center
if (!Strings.isNullOrEmpty(dataCenter) && !Objects.equals(dataCenter, configClusterName)) {
Release dataCenterRelease = findRelease(clientAppId, clientIp, clientLabel, configAppId,
dataCenter, configNamespace,
Release dataCenterRelease = findRelease(clientAppId, clientIp, clientLabel, configAppId, dataCenter, configNamespace,
clientMessages);
if (Objects.nonNull(dataCenterRelease)) {
return dataCenterRelease;
}
}

// fallback to default release
return findRelease(clientAppId, clientIp, clientLabel, configAppId,
ConfigConsts.CLUSTER_NAME_DEFAULT, configNamespace,
return findRelease(clientAppId, clientIp, clientLabel, configAppId, ConfigConsts.CLUSTER_NAME_DEFAULT, configNamespace,
clientMessages);
}

/**
* Find release
*
* @param clientAppId the client's app id
* @param clientIp the client ip
* @param clientLabel the client label
* @param configAppId the requested config's app id
* @param clientAppId the client's app id
* @param clientIp the client ip
* @param clientLabel the client label
* @param configAppId the requested config's app id
* @param configClusterName the requested config's cluster name
* @param configNamespace the requested config's namespace name
* @param clientMessages the messages received in client side
* @param configNamespace the requested config's namespace name
* @param clientMessages the messages received in client side
* @return the release
*/
private Release findRelease(String clientAppId, String clientIp, String clientLabel,
String configAppId, String configClusterName,
private Release findRelease(String clientAppId, String clientIp, String clientLabel, String configAppId, String configClusterName,
String configNamespace, ApolloNotificationMessages clientMessages) {
Long grayReleaseId = grayReleaseRulesHolder.findReleaseIdFromGrayReleaseRule(clientAppId,
clientIp, clientLabel, configAppId,
Long grayReleaseId = grayReleaseRulesHolder.findReleaseIdFromGrayReleaseRule(clientAppId, clientIp, clientLabel, configAppId,
configClusterName, configNamespace);

Release release = null;
Expand All @@ -101,8 +94,7 @@ private Release findRelease(String clientAppId, String clientIp, String clientLa
}

if (release == null) {
release = findLatestActiveRelease(configAppId, configClusterName, configNamespace,
clientMessages);
release = findLatestActiveRelease(configAppId, configClusterName, configNamespace, clientMessages);
}

return release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
* @author Jason Song([email protected])
*/
public class ConfigServiceWithCache extends AbstractConfigService {

private static final Logger logger = LoggerFactory.getLogger(ConfigServiceWithCache.class);
private static final long DEFAULT_EXPIRED_AFTER_ACCESS_IN_MINUTES = 60;//1 hour
private static final String TRACER_EVENT_CACHE_INVALIDATE = "ConfigCache.Invalidate";
Expand Down Expand Up @@ -101,7 +100,7 @@ protected Release findActiveOne(long id, ApolloNotificationMessages clientMessag

@Override
protected Release findLatestActiveRelease(String appId, String clusterName, String namespaceName,
ApolloNotificationMessages clientMessages) {
ApolloNotificationMessages clientMessages) {
String messageKey = ReleaseMessageKeyGenerator.generate(appId, clusterName, namespaceName);
String cacheKey = messageKey;

Expand Down Expand Up @@ -132,8 +131,7 @@ private void invalidate(String key) {
@Override
public void handleMessage(ReleaseMessage message, String channel) {
logger.info("message received - channel: {}, message: {}", channel, message);
if (!Topics.APOLLO_RELEASE_TOPIC.equals(channel) || Strings.isNullOrEmpty(
message.getMessage())) {
if (!Topics.APOLLO_RELEASE_TOPIC.equals(channel) || Strings.isNullOrEmpty(message.getMessage())) {
return;
}

Expand Down Expand Up @@ -170,19 +168,15 @@ public ConfigCacheEntry load(String key) throws Exception {

Transaction transaction = Tracer.newTransaction(TRACER_EVENT_CACHE_LOAD, key);
try {
ReleaseMessage latestReleaseMessage = releaseMessageService.findLatestReleaseMessageForMessages(
Lists
.newArrayList(key));
Release latestRelease = releaseService.findLatestActiveRelease(namespaceInfo.get(0),
namespaceInfo.get(1),
namespaceInfo.get(2));
ReleaseMessage latestReleaseMessage = releaseMessageService.findLatestReleaseMessageForMessages(Lists
.newArrayList(key));
Release latestRelease = releaseService.findLatestActiveRelease(namespaceInfo.get(0), namespaceInfo.get(1),
namespaceInfo.get(2));

transaction.setStatus(Transaction.SUCCESS);

long notificationId =
latestReleaseMessage == null ? ConfigConsts.NOTIFICATION_ID_PLACEHOLDER
: latestReleaseMessage
.getId();
long notificationId = latestReleaseMessage == null ? ConfigConsts.NOTIFICATION_ID_PLACEHOLDER : latestReleaseMessage
.getId();

if (notificationId == ConfigConsts.NOTIFICATION_ID_PLACEHOLDER && latestRelease == null) {
return nullConfigCacheEntry;
Expand Down Expand Up @@ -213,8 +207,7 @@ private void buildConfigIdCache() {
configIdCache = configIdCacheBuilder.build(new CacheLoader<Long, Optional<Release>>() {
@Override
public Optional<Release> load(Long key) throws Exception {
Transaction transaction = Tracer.newTransaction(TRACER_EVENT_CACHE_LOAD_ID,
String.valueOf(key));
Transaction transaction = Tracer.newTransaction(TRACER_EVENT_CACHE_LOAD_ID, String.valueOf(key));
try {
Release release = releaseService.findActiveOne(key);

Expand All @@ -237,7 +230,6 @@ public Optional<Release> load(Long key) throws Exception {
}

private static class ConfigCacheEntry {

private final long notificationId;
private final Release release;

Expand Down

0 comments on commit a4a2d05

Please sign in to comment.