liubao68 closed pull request #570: [SCB-360] write simpler eventBus to optimize performance URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/570
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/foundations/foundation-common/src/main/java/com/google/common/eventbus/SimpleEventBus.java b/foundations/foundation-common/src/main/java/com/google/common/eventbus/SimpleEventBus.java new file mode 100644 index 000000000..55c370801 --- /dev/null +++ b/foundations/foundation-common/src/main/java/com/google/common/eventbus/SimpleEventBus.java @@ -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); + } + } + } +} diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java index f26c36793..d50e76778 100644 --- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java +++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java @@ -18,13 +18,14 @@ package org.apache.servicecomb.foundation.common.event; import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.SimpleEventBus; /** * EventManager for chassis events * */ public class EventManager { - public static EventBus eventBus = new EventBus(); + public static EventBus eventBus = new SimpleEventBus(); /** * Registering listener. diff --git a/foundations/foundation-common/src/test/java/com/google/common/eventbus/TestSimpleEventBus.java b/foundations/foundation-common/src/test/java/com/google/common/eventbus/TestSimpleEventBus.java new file mode 100644 index 000000000..8aef0cb45 --- /dev/null +++ b/foundations/foundation-common/src/test/java/com/google/common/eventbus/TestSimpleEventBus.java @@ -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")); + } +} ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
