forked from alibaba/nacos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[alibaba#3141]Move the related notify test cases code to common modul…
…e, fix NPE and improve peformance. (alibaba#3145) * [alibaba#3141]fix typo and move unit test cases codes from core to common module. * [alibaba#3141]implement DefaultSharePublisher to separate sharePublish from default publisher and fix typo. * [alibaba#3141]fix typo and NPE issue. * [alibaba#3141]improve performance and fix typo. * [alibaba#3141]add removeSubscriber logical codes. * [alibaba#3141]rename the unit test method's names. * [alibaba#3141]fix typo. * [alibaba#3141]fix thread unsafe and atomic issue.
- Loading branch information
1 parent
f5e1025
commit ac0eabf
Showing
4 changed files
with
695 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
113 changes: 113 additions & 0 deletions
113
common/src/main/java/com/alibaba/nacos/common/notify/DefaultSharePublisher.java
This file contains 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,113 @@ | ||
/* | ||
* Copyright 1999-2018 Alibaba Group Holding Ltd. | ||
* | ||
* 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.alibaba.nacos.common.notify; | ||
|
||
import com.alibaba.nacos.common.notify.listener.Subscriber; | ||
import com.alibaba.nacos.common.utils.ConcurrentHashSet; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* The default share event publisher implementation for slow event. | ||
* | ||
* @author zongtanghu | ||
*/ | ||
public class DefaultSharePublisher extends DefaultPublisher { | ||
|
||
private final Map<Class<? extends SlowEvent>, Set<Subscriber>> subMappings = new ConcurrentHashMap<Class<? extends SlowEvent>, Set<Subscriber>>(); | ||
|
||
private final Lock lock = new ReentrantLock(); | ||
|
||
/** | ||
* Add listener for default share publisher. | ||
* | ||
* @param subscriber {@link Subscriber} | ||
* @param subscribeType subscribe event type, such as slow event or general event. | ||
*/ | ||
public void addSubscriber(Subscriber subscriber, Class<? extends Event> subscribeType) { | ||
// Actually, do a classification based on the slowEvent type. | ||
Class<? extends SlowEvent> subSlowEventType = (Class<? extends SlowEvent>) subscribeType; | ||
// For adding to parent class attributes synchronization. | ||
subscribers.add(subscriber); | ||
|
||
lock.lock(); | ||
try { | ||
Set<Subscriber> sets = subMappings.get(subSlowEventType); | ||
if (sets == null) { | ||
Set<Subscriber> newSet = new ConcurrentHashSet<Subscriber>(); | ||
newSet.add(subscriber); | ||
subMappings.put(subSlowEventType, newSet); | ||
return; | ||
} | ||
sets.add(subscriber); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Remove listener for default share publisher. | ||
* | ||
* @param subscriber {@link Subscriber} | ||
* @param subscribeType subscribe event type, such as slow event or general event. | ||
*/ | ||
public void removeSubscriber(Subscriber subscriber, Class<? extends Event> subscribeType) { | ||
// Actually, do a classification based on the slowEvent type. | ||
Class<? extends SlowEvent> subSlowEventType = (Class<? extends SlowEvent>) subscribeType; | ||
// For removing to parent class attributes synchronization. | ||
subscribers.remove(subscriber); | ||
|
||
lock.lock(); | ||
try { | ||
Set<Subscriber> sets = subMappings.get(subSlowEventType); | ||
|
||
if (sets != null && sets.contains(subscriber)) { | ||
sets.remove(subscriber); | ||
} | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public void receiveEvent(Event event) { | ||
|
||
final long currentEventSequence = event.sequence(); | ||
// get subscriber set based on the slow EventType. | ||
final Class<? extends SlowEvent> slowEventType = (Class<? extends SlowEvent>) event.getClass(); | ||
|
||
// Get for Map, the algorithm is O(1). | ||
Set<Subscriber> subscribers = subMappings.get(slowEventType); | ||
|
||
// Notification single event subscriber | ||
for (Subscriber subscriber : subscribers) { | ||
// Whether to ignore expiration events | ||
if (subscriber.ignoreExpireEvent() && lastEventSequence > currentEventSequence) { | ||
LOGGER.debug("[NotifyCenter] the {} is unacceptable to this subscriber, because had expire", | ||
event.getClass()); | ||
continue; | ||
} | ||
|
||
// Notify single subscriber for slow event. | ||
notifySubscriber(subscriber, event); | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.