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(apollo-client): add method interestedChangedKeys to ConfigChangeEvent #3666

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
817c9a7
feat(apollo-client): add method interestedChangedKeys to ConfigChange…
Anilople May 8, 2021
2b28a27
docs: add Apache LICENSE-2.0
Anilople May 8, 2021
1e40d80
fix(apollo-client): null exception
Anilople May 8, 2021
2dda71b
Merge branch 'master' into feature/interested/key/20210508
nobodyiam May 15, 2021
67d49bc
Merge branch 'master' into feature/interested/key/20210508
Anilople Jun 5, 2021
835823d
delete getChanges in ConfigChangeEvent
Anilople Jun 5, 2021
e6c20a0
docs: update LICENSE
Anilople Jun 5, 2021
66d59ee
feat: use InterestedConfigChangeEvent
Anilople Jun 5, 2021
cca7e5b
revert a test back
Anilople Jun 5, 2021
0ce9957
test: interested key config change event
Anilople Jun 5, 2021
428f461
Merge branch 'master' into feature/interested/key/20210508
Anilople Jun 7, 2021
a3a5cfa
test: add another key
Anilople Jun 7, 2021
c475174
Merge branch 'feature/interested/key/20210508' of https://github.com/…
Anilople Jun 7, 2021
2880a4f
Update CHANGES.md
Anilople Jun 7, 2021
3064aaf
Merge branch 'master' into feature/interested/key/20210508
Anilople Jun 9, 2021
e78a706
feat: fireConfigChange return how many listener be fired
Anilople Jun 9, 2021
259cda1
test: expose bug in fireConfigChange
Anilople Jun 9, 2021
cd30d53
fix: notify multiple time if exists multiple listener
Anilople Jun 9, 2021
5759735
feat: move InterestedConfigChangeEvent, it cannot be use by other pac…
Anilople Jun 9, 2021
479a5d2
rollback fireConfigChange return type to 'void'
Anilople Jun 10, 2021
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Apollo 1.9.0
* [Remove redundant invoke of trySyncFromUpstream](https://github.com/ctripcorp/apollo/pull/3699)
* [add apollo team introduction and community releated contents](https://github.com/ctripcorp/apollo/pull/3713)
* [fix oidc sql](https://github.com/ctripcorp/apollo/pull/3720)
* [feat(apollo-client): add method interestedChangedKeys to ConfigChangeEvent](https://github.com/ctripcorp/apollo/pull/3666)
------------------
All issues and pull requests are [here](https://github.com/ctripcorp/apollo/milestone/6?closed=1)

Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import java.util.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicLong;
Expand Down Expand Up @@ -454,33 +451,67 @@ protected void clearConfigCache() {
}
}

/**
* @param changes map's key is config property's key
*/
protected void fireConfigChange(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);
this.notifyAsync(listener, interestedConfigChangeEvent);
}
}

/**
* Fire the listeners by event.
*/
protected void fireConfigChange(final ConfigChangeEvent changeEvent) {
for (final ConfigChangeListener listener : m_listeners) {
final List<ConfigChangeListener> listeners = this
.findMatchedConfigChangeListeners(changeEvent.changedKeys());

// notify those listeners
for (ConfigChangeListener listener : listeners) {
this.notifyAsync(listener, changeEvent);
}
}

private List<ConfigChangeListener> findMatchedConfigChangeListeners(Set<String> changedKeys) {
final List<ConfigChangeListener> configChangeListeners = new ArrayList<>();
for (ConfigChangeListener configChangeListener : this.m_listeners) {
// check whether the listener is interested in this change event
if (!isConfigChangeListenerInterested(listener, changeEvent)) {
continue;
if (this.isConfigChangeListenerInterested(configChangeListener, changedKeys)) {
configChangeListeners.add(configChangeListener);
}
m_executorService.submit(new Runnable() {
@Override
public void run() {
String listenerName = listener.getClass().getName();
Transaction transaction = Tracer.newTransaction("Apollo.ConfigChangeListener", listenerName);
try {
listener.onChange(changeEvent);
transaction.setStatus(Transaction.SUCCESS);
} catch (Throwable ex) {
transaction.setStatus(ex);
Tracer.logError(ex);
logger.error("Failed to invoke config change listener {}", listenerName, ex);
} finally {
transaction.complete();
}
}
});
}
return configChangeListeners;
}

private boolean isConfigChangeListenerInterested(ConfigChangeListener configChangeListener, ConfigChangeEvent configChangeEvent) {
private void notifyAsync(final ConfigChangeListener listener, final ConfigChangeEvent changeEvent) {
m_executorService.submit(new Runnable() {
@Override
public void run() {
String listenerName = listener.getClass().getName();
Transaction transaction = Tracer.newTransaction("Apollo.ConfigChangeListener", listenerName);
try {
listener.onChange(changeEvent);
transaction.setStatus(Transaction.SUCCESS);
} catch (Throwable ex) {
transaction.setStatus(ex);
Tracer.logError(ex);
logger.error("Failed to invoke config change listener {}", listenerName, ex);
} finally {
transaction.complete();
}
}
});
}

private boolean isConfigChangeListenerInterested(ConfigChangeListener configChangeListener, Set<String> changedKeys) {
Set<String> interestedKeys = m_interestedKeys.get(configChangeListener);
Set<String> interestedKeyPrefixes = m_interestedKeyPrefixes.get(configChangeListener);

Expand All @@ -491,15 +522,15 @@ private boolean isConfigChangeListenerInterested(ConfigChangeListener configChan

if (interestedKeys != null) {
for (String interestedKey : interestedKeys) {
if (configChangeEvent.isChanged(interestedKey)) {
if (changedKeys.contains(interestedKey)) {
return true;
}
}
}

if (interestedKeyPrefixes != null) {
for (String prefix : interestedKeyPrefixes) {
for (final String changedKey : configChangeEvent.changedKeys()) {
for (final String changedKey : changedKeys) {
if (changedKey.startsWith(prefix)) {
return true;
}
Expand All @@ -510,6 +541,32 @@ private boolean isConfigChangeListenerInterested(ConfigChangeListener configChan
return false;
}

private Set<String> resolveInterestedChangedKeys(ConfigChangeListener configChangeListener, Set<String> changedKeys) {
Set<String> interestedChangedKeys = new HashSet<>();

if (this.m_interestedKeys.containsKey(configChangeListener)) {
Set<String> interestedKeys = this.m_interestedKeys.get(configChangeListener);
for (String interestedKey : interestedKeys) {
if (changedKeys.contains(interestedKey)) {
interestedChangedKeys.add(interestedKey);
}
}
}

if (this.m_interestedKeyPrefixes.containsKey(configChangeListener)) {
Set<String> interestedKeyPrefixes = this.m_interestedKeyPrefixes.get(configChangeListener);
for (String interestedKeyPrefix : interestedKeyPrefixes) {
for (String changedKey : changedKeys) {
if (changedKey.startsWith(interestedKeyPrefix)) {
interestedChangedKeys.add(changedKey);
}
}
}
}

return Collections.unmodifiableSet(interestedChangedKeys);
}

List<ConfigChange> calcPropertyChanges(String namespace, Properties previous,
Properties current) {
if (previous == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ExceptionUtil;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -163,7 +162,7 @@ public synchronized void onRepositoryChange(String namespace, Properties newProp
return;
}

this.fireConfigChange(new ConfigChangeEvent(m_namespace, actualChanges));
this.fireConfigChange(m_namespace, actualChanges);

Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2021 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.internals;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

/**
* In {@link ApolloConfigChangeListener} you set some interested key's rule, you can get those keys
* by this class's instance.
*
* @author wxq
*/
class InterestedConfigChangeEvent extends ConfigChangeEvent {
nobodyiam marked this conversation as resolved.
Show resolved Hide resolved

/**
* @see Config#addChangeListener(ConfigChangeListener, Set)
* @see Config#addChangeListener(ConfigChangeListener, Set, Set)
* @see ApolloConfigChangeListener#interestedKeys()
* @see ApolloConfigChangeListener#interestedKeyPrefixes()
*/
private final Set<String> m_interestedChangedKeys;

public InterestedConfigChangeEvent(String namespace,
Map<String, ConfigChange> changes, Set<String> interestedChangedKeys) {
super(namespace, changes);
this.m_interestedChangedKeys = interestedChangedKeys;
}

/**
* @return interested and changed keys
*/
@Override
public Set<String> interestedChangedKeys() {
return Collections.unmodifiableSet(this.m_interestedChangedKeys);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.slf4j.LoggerFactory;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.tracer.Tracer;
import com.ctrip.framework.apollo.util.ExceptionUtil;
import com.google.common.base.Function;
Expand Down Expand Up @@ -112,7 +111,7 @@ public String apply(ConfigChange input) {
updateConfig(newConfigProperties, m_configRepository.getSourceType());
clearConfigCache();

this.fireConfigChange(new ConfigChangeEvent(m_namespace, changeMap));
this.fireConfigChange(m_namespace, changeMap);

Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.ctrip.framework.apollo.model;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

Expand All @@ -26,16 +27,15 @@
public class ConfigChangeEvent {
private final String m_namespace;
private final Map<String, ConfigChange> m_changes;

/**
* Constructor.
* @param namespace the namespace of this change
* @param changes the actual changes
*/
public ConfigChangeEvent(String namespace,
Map<String, ConfigChange> changes) {
m_namespace = namespace;
m_changes = changes;
this.m_namespace = namespace;
this.m_changes = changes;
}

/**
Expand All @@ -46,6 +46,15 @@ public Set<String> changedKeys() {
return m_changes.keySet();
}

/**
* Maybe subclass override this method.
*
* @return interested and changed keys
*/
public Set<String> interestedChangedKeys() {
return Collections.emptySet();
}

/**
* Get a specific change instance for the key specified.
* @param key the changed key
Expand Down
Loading