You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/03/15 11:02:02 UTC

[incubator-servicecomb-java-chassis] branch master updated: SCB-360 write simpler eventBus to optimize performance

This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new eca2b3d  SCB-360 write simpler eventBus to optimize performance
eca2b3d is described below

commit eca2b3dc89007f19c6dd2cd70a67fab00e34e2ba
Author: wujimin <wu...@huawei.com>
AuthorDate: Mon Mar 5 17:26:16 2018 +0800

    SCB-360 write simpler eventBus to optimize performance
---
 .../com/google/common/eventbus/SimpleEventBus.java | 86 +++++++++++++++++++
 .../foundation/common/event/EventManager.java      |  3 +-
 .../google/common/eventbus/TestSimpleEventBus.java | 98 ++++++++++++++++++++++
 3 files changed, 186 insertions(+), 1 deletion(-)

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 0000000..55c3708
--- /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 f26c367..d50e767 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 0000000..8aef0cb
--- /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"));
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.