Skip to content

Commit

Permalink
merge branch
Browse files Browse the repository at this point in the history
  • Loading branch information
TerryLam2010 committed Sep 5, 2024
1 parent 6b0d4c7 commit 38469c4
Show file tree
Hide file tree
Showing 82 changed files with 1,299 additions and 535 deletions.
11 changes: 3 additions & 8 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ Changes by Version
==================
Release Notes.

Apollo Java 2.3.0
Apollo Java 2.4.0

------------------
* [add an initialize method to avoid DefaultProviderManager's logic being triggered when using custom ProviderManager.](https://github.com/apolloconfig/apollo-java/pull/50)
* [Implement parsing time based on pattern for @ApolloJsonValue](https://github.com/apolloconfig/apollo-java/pull/53)
* [Enhance to load mocked properties from apollo.cache-dir](https://github.com/apolloconfig/apollo-java/pull/58)
* [perf: speed up the first loading of namespace when startup meet 404](https://github.com/apolloconfig/apollo-java/pull/61)
* [perf: speed up when startup meets timeout](https://github.com/apolloconfig/apollo-java/pull/64)
* [upgrade spring boot to 2.7.18](https://github.com/apolloconfig/apollo-java/pull/68)
*

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/3?closed=1)
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/4?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ public PureApolloConfig(String namespace,
super(namespace, configRepository);
}

/**
* Constructor.
*
* @param appId the appId of this config instance
* @param namespace the namespace of this config instance
* @param configRepository the config repository for this config instance
*/
public PureApolloConfig(String appId, String namespace,
ConfigRepository configRepository) {
super(appId, namespace, configRepository);
}

@Override
public String getProperty(String key, String defaultValue) {
// step 1: check local cached properties file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
public class PureApolloConfigFactory extends DefaultConfigFactory implements ConfigFactory {

@Override
protected Config createRepositoryConfig(String namespace, ConfigRepository configRepository) {
return new PureApolloConfig(namespace, configRepository);
protected Config createRepositoryConfig(String appId, String namespace, ConfigRepository configRepository) {
return new PureApolloConfig(appId, namespace, configRepository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ public interface ConfigFile {
*/
boolean hasContent();

/**
* Get the appId of this config file instance
* @return the appId
*
* @since 2.4.0
*/
default String getAppId(){
return null;
}

/**
* Get the namespace of this config file instance
* @return the namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.internals.ConfigManager;
import com.ctrip.framework.apollo.spi.ConfigFactory;
import com.ctrip.framework.apollo.spi.ConfigRegistry;
import com.ctrip.framework.apollo.util.ConfigUtil;

/**
* Entry point for client config use
Expand Down Expand Up @@ -77,6 +79,10 @@ public static Config getConfig(String namespace) {
return s_instance.getManager().getConfig(namespace);
}

public static Config getConfig(String appId, String namespace) {
return s_instance.getManager().getConfig(appId, namespace);
}

public static ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat) {
return s_instance.getManager().getConfigFile(namespace, configFileFormat);
}
Expand All @@ -93,16 +99,32 @@ static void setConfig(Config config) {
*/
static void setConfig(String namespace, final Config config) {
s_instance.getRegistry().register(namespace, new ConfigFactory() {

private final ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);

@Override
public Config create(String namespace) {
return config;
}

@Override
public Config create(String appId, String namespace) {
if(!StringUtils.equals(appId, m_configUtil.getAppId())){
throw new IllegalArgumentException("appId not match");
}
return config;
}

@Override
public ConfigFile createConfigFile(String namespace, ConfigFileFormat configFileFormat) {
return null;
}

@Override
public ConfigFile createConfigFile(String appId, String namespace, ConfigFileFormat configFileFormat) {
return null;
}

});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,15 @@ protected void clearConfigCache() {
/**
* @param changes map's key is config property's key
*/
protected void fireConfigChange(String namespace, Map<String, ConfigChange> changes) {
protected void fireConfigChange(String appId, String namespace, Map<String, ConfigChange> changes) {
final Set<String> changedKeys = changes.keySet();
final List<ConfigChangeListener> listeners = this.findMatchedConfigChangeListeners(changedKeys);

// notify those listeners
for (ConfigChangeListener listener : listeners) {
Set<String> interestedChangedKeys = resolveInterestedChangedKeys(listener, changedKeys);
InterestedConfigChangeEvent interestedConfigChangeEvent = new InterestedConfigChangeEvent(
namespace, changes, interestedChangedKeys);
appId, namespace, changes, interestedChangedKeys);
this.notifyAsync(listener, interestedConfigChangeEvent);
}
}
Expand Down Expand Up @@ -567,7 +567,7 @@ private Set<String> resolveInterestedChangedKeys(ConfigChangeListener configChan
return Collections.unmodifiableSet(interestedChangedKeys);
}

List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
List<ConfigChange> calcPropertyChanges(String appId, String namespace, Properties previous,
Properties current) {
if (previous == null) {
previous = propertiesFactory.getPropertiesInstance();
Expand All @@ -587,12 +587,12 @@ List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
List<ConfigChange> changes = Lists.newArrayList();

for (String newKey : newKeys) {
changes.add(new ConfigChange(namespace, newKey, null, current.getProperty(newKey),
changes.add(new ConfigChange(appId, namespace, newKey, null, current.getProperty(newKey),
PropertyChangeType.ADDED));
}

for (String removedKey : removedKeys) {
changes.add(new ConfigChange(namespace, removedKey, previous.getProperty(removedKey), null,
changes.add(new ConfigChange(appId, namespace, removedKey, previous.getProperty(removedKey), null,
PropertyChangeType.DELETED));
}

Expand All @@ -602,7 +602,7 @@ List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
if (Objects.equal(previousValue, currentValue)) {
continue;
}
changes.add(new ConfigChange(namespace, commonKey, previousValue, currentValue,
changes.add(new ConfigChange(appId, namespace, commonKey, previousValue, currentValue,
PropertyChangeType.MODIFIED));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
private static final Logger logger = DeferredLoggerFactory.getLogger(AbstractConfigFile.class);
private static ExecutorService m_executorService;
protected final ConfigRepository m_configRepository;
protected final String m_appId;
protected final String m_namespace;
protected final AtomicReference<Properties> m_configProperties;
private final List<ConfigFileChangeListener> m_listeners = Lists.newCopyOnWriteArrayList();
Expand All @@ -57,8 +58,9 @@ public abstract class AbstractConfigFile implements ConfigFile, RepositoryChange
.create("ConfigFile", true));
}

public AbstractConfigFile(String namespace, ConfigRepository configRepository) {
public AbstractConfigFile(String appId, String namespace, ConfigRepository configRepository) {
m_configRepository = configRepository;
m_appId = appId;
m_namespace = namespace;
m_configProperties = new AtomicReference<>();
propertiesFactory = ApolloInjector.getInstance(PropertiesFactory.class);
Expand All @@ -80,6 +82,11 @@ private void initialize() {
}
}

@Override
public String getAppId() {
return m_appId;
}

@Override
public String getNamespace() {
return m_namespace;
Expand All @@ -89,6 +96,11 @@ public String getNamespace() {

@Override
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
this.onRepositoryChange(m_appId, m_namespace, newProperties);
}

@Override
public synchronized void onRepositoryChange(String appId, String namespace, Properties newProperties) {
if (newProperties.equals(m_configProperties.get())) {
return;
}
Expand All @@ -110,7 +122,7 @@ public synchronized void onRepositoryChange(String namespace, Properties newProp
changeType = PropertyChangeType.DELETED;
}

this.fireConfigChange(new ConfigFileChangeEvent(m_namespace, oldValue, newValue, changeType));
this.fireConfigChange(new ConfigFileChangeEvent(m_appId, m_namespace, oldValue, newValue, changeType));

Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public void removeChangeListener(RepositoryChangeListener listener) {
m_listeners.remove(listener);
}

protected void fireRepositoryChange(String namespace, Properties newProperties) {
protected void fireRepositoryChange(String appId, String namespace, Properties newProperties) {
for (RepositoryChangeListener listener : m_listeners) {
try {
listener.onRepositoryChange(namespace, newProperties);
listener.onRepositoryChange(appId, namespace, newProperties);
} catch (Throwable ex) {
Tracer.logError(ex);
logger.error("Failed to invoke repository change listener {}", listener.getClass(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,28 @@ public interface ConfigManager {
*/
Config getConfig(String namespace);

/**
* Get the config instance for the namespace and appId specified.
* @param appId the appId
* @param namespace the namespace
* @return the config instance for the appId and namespace
*/
Config getConfig(String appId,String namespace);

/**
* Get the config file instance for the namespace specified.
* @param namespace the namespace
* @param configFileFormat the config file format
* @return the config file instance for the namespace
*/
ConfigFile getConfigFile(String namespace, ConfigFileFormat configFileFormat);

/**
* Get the config file instance for the namespace specified.
* @param appId the appId
* @param namespace the namespace
* @param configFileFormat the config file format
* @return the config file instance for the appId and namespace
*/
ConfigFile getConfigFile(String appId, String namespace, ConfigFileFormat configFileFormat);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
*/
package com.ctrip.framework.apollo.internals;

import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.enums.ConfigSourceType;
import com.ctrip.framework.apollo.util.ConfigUtil;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.io.IOException;
Expand Down Expand Up @@ -46,6 +49,7 @@
public class DefaultConfig extends AbstractConfig implements RepositoryChangeListener {

private static final Logger logger = DeferredLoggerFactory.getLogger(DefaultConfig.class);
private final String m_appId;
private final String m_namespace;
private final Properties m_resourceProperties;
private final AtomicReference<Properties> m_configProperties;
Expand All @@ -61,8 +65,23 @@ public class DefaultConfig extends AbstractConfig implements RepositoryChangeLis
* @param configRepository the config repository for this config instance
*/
public DefaultConfig(String namespace, ConfigRepository configRepository) {
this(null, namespace , configRepository);
}

/**
* Constructor.
*
* @param appId the appId of this config instance
* @param namespace the namespace of this config instance
* @param configRepository the config repository for this config instance
*/
public DefaultConfig(String appId, String namespace, ConfigRepository configRepository) {
if (appId == null) {
appId = ApolloInjector.getInstance(ConfigUtil.class).getAppId();
}
m_appId = appId;
m_namespace = namespace;
m_resourceProperties = loadFromResource(m_namespace);
m_resourceProperties = loadFromResource(m_appId, m_namespace);
m_configRepository = configRepository;
m_configProperties = new AtomicReference<>();
m_warnLogRateLimiter = RateLimiter.create(0.017); // 1 warning log output per minute
Expand Down Expand Up @@ -218,6 +237,11 @@ private Set<String> stringPropertyNames(Properties properties) {

@Override
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
this.onRepositoryChange(m_appId, m_namespace, newProperties);
}

@Override
public synchronized void onRepositoryChange(String appId, String namespace, Properties newProperties) {
if (newProperties.equals(m_configProperties.get())) {
return;
}
Expand All @@ -234,7 +258,7 @@ public synchronized void onRepositoryChange(String namespace, Properties newProp
return;
}

this.fireConfigChange(m_namespace, actualChanges);
this.fireConfigChange(m_appId, m_namespace, actualChanges);

Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace);
}
Expand All @@ -247,7 +271,7 @@ private void updateConfig(Properties newConfigProperties, ConfigSourceType sourc
private Map<String, ConfigChange> updateAndCalcConfigChanges(Properties newConfigProperties,
ConfigSourceType sourceType) {
List<ConfigChange> configChanges =
calcPropertyChanges(m_namespace, m_configProperties.get(), newConfigProperties);
calcPropertyChanges(m_appId, m_namespace, m_configProperties.get(), newConfigProperties);

ImmutableMap.Builder<String, ConfigChange> actualChanges =
new ImmutableMap.Builder<>();
Expand Down Expand Up @@ -298,19 +322,25 @@ private Map<String, ConfigChange> updateAndCalcConfigChanges(Properties newConfi
return actualChanges.build();
}

private Properties loadFromResource(String namespace) {
String name = String.format("META-INF/config/%s.properties", namespace);
private Properties loadFromResource(String appId, String namespace) {
String name = String.format("META-INF/config/%s+%s.properties", appId, namespace);
InputStream in = ClassLoaderUtil.getLoader().getResourceAsStream(name);
Properties properties = null;

if (StringUtils.equals(ApolloInjector.getInstance(ConfigUtil.class).getAppId(), appId)
&& in == null) {
name = String.format("META-INF/config/%s.properties", namespace);
in = ClassLoaderUtil.getLoader().getResourceAsStream(name);
}

if (in != null) {
properties = propertiesFactory.getPropertiesInstance();

try {
properties.load(in);
} catch (IOException ex) {
Tracer.logError(ex);
logger.error("Load resource config for namespace {} failed", namespace, ex);
logger.error("Load resource config for appId {} namespace {} failed", appId, namespace, ex);
} finally {
try {
in.close();
Expand Down
Loading

0 comments on commit 38469c4

Please sign in to comment.