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 2 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
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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;
Expand All @@ -21,6 +38,7 @@
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import java.util.HashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -450,7 +468,7 @@ public void run() {
String listenerName = listener.getClass().getName();
Transaction transaction = Tracer.newTransaction("Apollo.ConfigChangeListener", listenerName);
try {
listener.onChange(changeEvent);
listener.onChange(resolve(listener, changeEvent));
Copy link
Contributor Author

@Anilople Anilople May 8, 2021

Choose a reason for hiding this comment

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

How about generate a new ConfigChangeEvent here?

Subclass's ConfigChangeEvent + resolved interestedChangedKeys ==> new ConfigChangeEvent

That will change the default behavior of subclass, but still keep compatible.

Copy link
Member

Choose a reason for hiding this comment

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

How about we create a subclass of ConfigChangeEvent, which contains the original config change event and the interested keys so that we don't need to create a new instance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When subclasses of AbstractConfig invoke method com.ctrip.framework.apollo.internals.AbstractConfig#fireConfigChange, they don't know about any information of interestedKeys because interestedKeys are saved in AbstractConfig.

If we create a subclass of ConfigChangeEvent, when method com.ctrip.framework.apollo.internals.AbstractConfig#fireConfigChange is invoking, we still need to create a new instance of subclass of ConfigChangeEvent, because the listener need to get this instance?

Copy link
Member

Choose a reason for hiding this comment

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

you are right, I was thinking we may use the composition pattern to avoid create a lot of ConfigChangeEvent instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change ConfigChangeEvent to an abstract class?

public abstract class ConfigChangeEvent {
// content
}

Or create a new class AbstractConfigChangeEvent,

public abstract class AbstractConfigChangeEvent {
  public abstract Set<String> changedKeys();
  public abstract Set<String> interestedChangedKeys();
  public abstract ConfigChange getChange(String key);
  public abstract boolean isChanged(String key);
  public abstract String getNamespace();
}

then let ConfigChangeEvent extends it?

public class ConfigChangeEvent extends AbstractConfigChangeEvent {
// ...
}

Copy link
Member

Choose a reason for hiding this comment

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

My original idea was to create an abstract class or an interface, and then use composition to wrap the original ConfigChangeEvent into InterestedConfigChangeEvent. But the current implementation also looks good to me.

transaction.setStatus(Transaction.SUCCESS);
} catch (Throwable ex) {
transaction.setStatus(ex);
Expand Down Expand Up @@ -494,6 +512,37 @@ private boolean isConfigChangeListenerInterested(ConfigChangeListener configChan
return false;
}

/**
* @return new {@link ConfigChangeEvent} contains interested and changed keys
*/
private ConfigChangeEvent resolve(ConfigChangeListener configChangeListener, ConfigChangeEvent configChangeEvent) {
Set<String> interestedKeys = m_interestedKeys.get(configChangeListener);
Set<String> interestedKeyPrefixes = m_interestedKeyPrefixes.get(configChangeListener);

Set<String> interestedChangedKeys = resolveInterestedChangedKeys(configChangeEvent.changedKeys(), interestedKeys, interestedKeyPrefixes);
return new ConfigChangeEvent(configChangeEvent.getNamespace(), configChangeEvent.getChanges(), interestedChangedKeys);
}

static Set<String> resolveInterestedChangedKeys(Set<String> changedKeys, Set<String> interestedKeys, Set<String> interestedKeyPrefixes) {
nobodyiam marked this conversation as resolved.
Show resolved Hide resolved
Set<String> interestedChangedKeys = new HashSet<>();

for (String interestedKey : interestedKeys) {
if (changedKeys.contains(interestedKey)) {
interestedChangedKeys.add(interestedKey);
}
}

for (String interestedKeyPrefix : interestedKeyPrefixes) {
for (String changedKey : changedKeys) {
if (changedKey.startsWith(interestedKeyPrefix)) {
interestedChangedKeys.add(changedKey);
}
}
}

return 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
@@ -1,5 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.model;

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

Expand All @@ -11,15 +32,29 @@ public class ConfigChangeEvent {
private final String m_namespace;
private final Map<String, ConfigChange> m_changes;

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

/**
* 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(namespace, changes, Collections.<String>emptySet());
}

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

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

/**
* @return interested and changed keys
*/
public Set<String> interestedChangedKeys() {
return Collections.unmodifiableSet(this.m_interestedChangedKeys);
}

/**
* Get a specific change instance for the key specified.
* @param key the changed key
Expand All @@ -55,4 +97,11 @@ public boolean isChanged(String key) {
public String getNamespace() {
return m_namespace;
}

/**
* @return all changed keys and instances
*/
public Map<String, ConfigChange> getChanges() {
Anilople marked this conversation as resolved.
Show resolved Hide resolved
return Collections.unmodifiableMap(this.m_changes);
}
}