-
Notifications
You must be signed in to change notification settings - Fork 820
[SCB-360] write simpler eventBus to optimize performance #570
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
foundations/foundation-common/src/main/java/com/google/common/eventbus/SimpleEventBus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| /* | ||
| * 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.google.common.eventbus; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Map.Entry; | ||
| import java.util.Set; | ||
| import java.util.concurrent.CopyOnWriteArrayList; | ||
|
|
||
| import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx; | ||
|
|
||
| import com.google.common.collect.Multimap; | ||
|
|
||
| /** | ||
| * <p>EventBus has performance problem, we create a new simpler eventBus | ||
| * | ||
| * <p>extend from EventBus is not necessary<br> | ||
| * but EventManager has a public EventBus field, we need to make compatible | ||
| * | ||
| * <p>different between this class and EventBus:<br> | ||
| * 1. not support cycle trigger:<br> | ||
| * subscribe a, when handle a, trigger event b<br> | ||
| * subscribe b, when handle b, trigger event a | ||
| */ | ||
| public class SimpleEventBus extends com.google.common.eventbus.EventBus { | ||
| private AnnotatedSubscriberFinder finder = new AnnotatedSubscriberFinder(); | ||
|
|
||
| // key is event type | ||
| // value is CopyOnWriteArrayList | ||
| private Map<Class<?>, List<EventSubscriber>> subscribersByType = new ConcurrentHashMapEx<>(); | ||
|
|
||
| @Override | ||
| public void register(Object listener) { | ||
| Multimap<Class<?>, EventSubscriber> methodsInListener = finder.findAllSubscribers(listener); | ||
|
|
||
| for (Entry<Class<?>, Collection<EventSubscriber>> entry : methodsInListener.asMap().entrySet()) { | ||
| List<EventSubscriber> exists = subscribersByType.computeIfAbsent(entry.getKey(), cls -> { | ||
| return new CopyOnWriteArrayList<>(); | ||
| }); | ||
| exists.addAll(entry.getValue()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void unregister(Object listener) { | ||
| Multimap<Class<?>, EventSubscriber> methodsInListener = finder.findAllSubscribers(listener); | ||
|
|
||
| for (Entry<Class<?>, Collection<EventSubscriber>> entry : methodsInListener.asMap().entrySet()) { | ||
| List<EventSubscriber> exists = subscribersByType.get(entry.getKey()); | ||
| if (exists == null) { | ||
| continue; | ||
| } | ||
|
|
||
| exists.removeAll(entry.getValue()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void post(Object event) { | ||
| Set<Class<?>> dispatchTypes = flattenHierarchy(event.getClass()); | ||
| for (Class<?> dispatchType : dispatchTypes) { | ||
| List<EventSubscriber> subscribers = subscribersByType.getOrDefault(dispatchType, Collections.emptyList()); | ||
|
|
||
| for (EventSubscriber subscriber : subscribers) { | ||
| dispatch(event, subscriber); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...ations/foundation-common/src/test/java/com/google/common/eventbus/TestSimpleEventBus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * 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.google.common.eventbus; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.hamcrest.Matchers; | ||
| import org.junit.Assert; | ||
| import org.junit.Test; | ||
|
|
||
| public class TestSimpleEventBus { | ||
| private SimpleEventBus eventBus = new SimpleEventBus(); | ||
|
|
||
| private List<Object> events = new ArrayList<>(); | ||
|
|
||
| class SubscriberForTest { | ||
| @Subscribe | ||
| @AllowConcurrentEvents | ||
| public void s1(Integer event) { | ||
| events.add(event); | ||
| } | ||
|
|
||
| @Subscribe | ||
| public void s2(String event) { | ||
| events.add(event); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void unregister() { | ||
| Object obj = new SubscriberForTest(); | ||
|
|
||
| // unregister not exist obj, should no problem | ||
| eventBus.unregister(obj); | ||
|
|
||
| eventBus.register(obj); | ||
| eventBus.unregister(obj); | ||
|
|
||
| // unregister again, should no problem | ||
| eventBus.unregister(obj); | ||
| } | ||
|
|
||
| @Test | ||
| public void oneSubscriber() { | ||
| Object obj = new SubscriberForTest(); | ||
|
|
||
| eventBus.register(obj); | ||
|
|
||
| eventBus.post(0.1); | ||
| eventBus.post(1); | ||
| eventBus.post("str"); | ||
| Assert.assertThat(events, Matchers.contains(1, "str")); | ||
|
|
||
| eventBus.unregister(obj); | ||
|
|
||
| events.clear(); | ||
| eventBus.post(0.1); | ||
| eventBus.post(1); | ||
| eventBus.post("str"); | ||
| Assert.assertThat(events, Matchers.empty()); | ||
| } | ||
|
|
||
| @Test | ||
| public void twoSubscriber() { | ||
| Object obj1 = new SubscriberForTest(); | ||
| Object obj2 = new SubscriberForTest(); | ||
|
|
||
| eventBus.register(obj1); | ||
| eventBus.register(obj2); | ||
|
|
||
| eventBus.post(0.1); | ||
| eventBus.post(1); | ||
| eventBus.post("str"); | ||
| Assert.assertThat(events, Matchers.contains(1, 1, "str", "str")); | ||
|
|
||
| events.clear(); | ||
| eventBus.unregister(obj1); | ||
| eventBus.post(0.1); | ||
| eventBus.post(1); | ||
| eventBus.post("str"); | ||
| Assert.assertThat(events, Matchers.contains(1, "str")); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation will block event senders if subscribers processed not very fast.
It's better to mention this in the class description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
registrer and unregister will not block post
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
post action follow guava definition, just make it simpler(see comments in class level)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. This will just make events out of order when trigger events in events handling logic.