Disruptor實際應用示例
簡介
Disruptor它是一個開源的併發框架,並獲得2011 Duke’s 程序框架創新獎,能夠在無鎖的情況下實現Queue併發操作。同學們代碼中是否會使用到BlockingQueue<?> queue用於緩存通知消息呢?本文介紹的Disruptor則可以完全取代BlockingQueue,帶來更快的速度。其它簡單內容可能參考百度,熟悉此類需求的同學知道,我們需要兩類核心概念功能,一類是事件,一類是針對事件的處理器。
下圖爲比較常用的的一類使用:
告警處理需求
如果你用過ODL,那你應該知道ODL中串聯起整個系統的神經是notification,ODL使用的正是disruptor。我們的原始需求爲,一個系統中有很多告警信息,如資源不足、數據異常、業務異常等,系統中需要針對上述的這些異常有對應的處理邏輯。
分析後需明確的是:
- 告警信息
- 處理邏輯
告警信息
在disruptor中實例化的構造函數中,需要指定消息的類型或工廠,如下:
disruptor = new Disruptor<Event>(Event.FACTORY,
4 * 4, executor, ProducerType.SINGLE,
new BlockingWaitStrategy());
爲了能夠通用,畢竟系統的disruptor是想處理任務消息的,而不僅僅是告警信息。所以定義了一個通用的消息結構,而不同的具體消息封裝在其中。如下:
package com.zte.sunquan.demo.disruptor.event;
import com.lmax.disruptor.EventFactory;
public class Event<T> {
public static final EventFactory<Event> FACTORY = new EventFactory<Event>() {
@Override
public Event newInstance() {
return new Event();
}
};
private T value;
private Event() {
}
public Event(T value) {
this.value = value;
}
public void setValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
}
而告警消息的結構則爲:
package com.zte.sunquan.demo.disruptor.main;
public class AlarmEvent {
private AlarmType value;
public AlarmEvent(AlarmType value) {
this.value = value;
}
enum AlarmType {
NO_POWER, HARDWARE_DAMAGE, SOFT_DAMAGE;
}
public AlarmType getValue() {
return value;
}
}
在構建往disruptor發送的消息時,進行AlarmEvent的封裝。
Event commEvent = new Event(event);
處理器
在完成了消息的定義後,下面則需要定義消息的處理器,這裏利用了**@Handler**註解用於定義處理器。
package com.zte.sunquan.demo.disruptor.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by 10184538 on 2018/9/10.
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Handler {
String value() default "toString";
}
通過註解可以方便地在系統的各個地方定義消息處理模塊。示例處理邏輯如下:
package com.zte.sunquan.demo.disruptor.main;
import com.zte.sunquan.demo.disruptor.annotation.Handler;
import com.zte.sunquan.demo.disruptor.handler.AbstractEventHandler;
@Handler
public class AlarmEventHandler extends AbstractEventHandler<AlarmEvent> {
@Override
public void handler(AlarmEvent alarmEvent) {
AlarmEvent.AlarmType alarmType = alarmEvent.getValue();
System.out.println("Got alarm:" + alarmType.name());
switch (alarmType) {
case NO_POWER:
System.out.println("charging");
break;
case HARDWARE_DAMAGE:
System.out.println("repair");
break;
case SOFT_DAMAGE:
System.out.println("reinstall");
break;
default:
System.out.println("ignore");
break;
}
}
}
調度
至此有了消息和消息處理邏輯,如何將兩者通過disruptor串聯起來?
DisruptorApplication
定義該類,作爲調試的核心,繼承了AbstractApplication
代碼參考:
package com.zte.sunquan.demo.disruptor;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.zte.sunquan.demo.disruptor.event.Event;
import com.zte.sunquan.demo.disruptor.handler.EventHandlerProcess;
import com.zte.sunquan.demo.thread.UserThreadFactory;
public class AbstractApplication {
private static final int CPUS = Runtime.getRuntime().availableProcessors();
private static final UserThreadFactory threadFactory = UserThreadFactory.build("disruptor-test");
private ExecutorService executor;
protected Disruptor<Event> disruptor;
private EventHandlerProcess process = new EventHandlerProcess();
private Class eventClass;
public AbstractApplication(Class eventClass) {
this.eventClass = eventClass;
init();
try {
process();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public void init() {
executor = Executors.newFixedThreadPool(CPUS * 2, threadFactory);
disruptor = new Disruptor<Event>(Event.FACTORY,
4 * 4, executor, ProducerType.SINGLE,
new BlockingWaitStrategy());
}
private void process() throws InstantiationException, IllegalAccessException {
Set<EventHandler> eventHandlers = process.scanHandlers();
for (EventHandler handler : eventHandlers) {
// if (filterHandler(handler)) {
disruptor.handleEventsWith(handler);
// }
}
disruptor.start();
}
private boolean filterHandler(EventHandler handler) {
if (handler != null) {
//繼承類
Type genericSuperclass = handler.getClass().getGenericSuperclass();
if(genericSuperclass==Object.class)
return false;
ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
//實現接口handler.getClass().getGenericInterfaces()
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
return actualTypeArguments[0] == eventClass;
}
return false;
}
public void close() {
disruptor.shutdown();
executor.shutdown();
}
}
package com.zte.sunquan.demo.disruptor;
import com.zte.sunquan.demo.disruptor.event.Event;
/**
* Created by 10184538 on 2018/9/10.
*/
public class DisruptorApplication extends AbstractApplication {
public DisruptorApplication(Class eventClass) {
super(eventClass);
}
public <T> void publishEvent(T event) {
Event commEvent = new Event(event);
final long seq = disruptor.getRingBuffer().next();
Event userEvent = (Event) disruptor.get(seq);
userEvent.setValue(commEvent.getValue());
disruptor.getRingBuffer().publish(seq);
}
}
EventHandlerProcess
需要注意的是在AbstractApplicaton的process中藉助EventHandlerProcess處理**@Handler**註解,具體邏輯如下:
package com.zte.sunquan.demo.disruptor.handler;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import com.google.common.base.Preconditions;
import com.lmax.disruptor.EventHandler;
import com.zte.sunquan.demo.disruptor.annotation.Handler;
import com.zte.sunquan.demo.disruptor.util.ClassUtil;
/**
* Created by 10184538 on 2018/9/10.
*/
public class EventHandlerProcess {
public Set<EventHandler> scanHandlers(){
List<Class<?>> clsList = ClassUtil
.getAllClassByPackageName("com.zte.sunquan.demo.disruptor");
return clsList.stream()
.filter(p->p.getAnnotation(Handler.class)!=null)
.map(c-> {
try {
return (EventHandler)c.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
})
.collect(Collectors.toSet());
}
public EventHandler scanJavaClass(Class cls) throws IllegalAccessException, InstantiationException {
Annotation annotation = cls.getAnnotation(Handler.class);
if (annotation != null) {
Object instance = cls.newInstance();
//是否可以做成動態字節碼生成,框架內部完成接口類的實現
Preconditions.checkState(instance instanceof EventHandler);
return (EventHandler) instance;
}
return null;
}
public static void main(String[] args) throws InstantiationException, IllegalAccessException {
String classPath = (String) System.getProperties().get("java.class.path");
List<String> packages = Arrays.stream(classPath.split(";")).filter(p -> p.contains("demo-disruptor-project")).collect(Collectors.toList());
//List<Class<?>> clsList = ClassUtil.getAllClassByPackageName(BlackPeople.class.getPackage());
List<Class<?>> clsList = ClassUtil.getAllClassByPackageName("com.zte.sunquan.demo.disruptor");
System.out.println(clsList);
// EventHandlerProcess process = new EventHandlerProcess();
// EventHandler eventHandler = process.scanJavaClass(LogEventHandler.class);
// System.out.println(eventHandler);
// Properties properties = System.getProperties();
// System.out.println(properties);
}
}
ClassUtil
用於查找加了@Handler註解類,該方式可以進一步優先,直接搜索class路徑下的所有類,而不是指定文件夾裏的。
package com.zte.sunquan.demo.disruptor.util;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
public class ClassUtil {
public static List<Class<?>> getAllClassByPackageName(String packageName) {
// 獲取當前包下以及子包下所以的類
List<Class<?>> returnClassList = getClasses(packageName);
return returnClassList;
}
/**
* 通過包名獲取包內所有類
*
* @param pkg
* @return
*/
public static List<Class<?>> getAllClassByPackageName(Package pkg) {
String packageName = pkg.getName();
// 獲取當前包下以及子包下所以的類
List<Class<?>> returnClassList = getClasses(packageName);
return returnClassList;
}
/**
* 通過接口名取得某個接口下所有實現這個接口的類
*/
public static List<Class<?>> getAllClassByInterface(Class<?> c) {
List<Class<?>> returnClassList = null;
if (c.isInterface()) {
// 獲取當前的包名
String packageName = c.getPackage().getName();
// 獲取當前包下以及子包下所以的類
List<Class<?>> allClass = getClasses(packageName);
if (allClass != null) {
returnClassList = new ArrayList<Class<?>>();
for (Class<?> cls : allClass) {
// 判斷是否是同一個接口
if (c.isAssignableFrom(cls)) {
// 本身不加入進去
if (!c.equals(cls)) {
returnClassList.add(cls);
}
}
}
}
}
return returnClassList;
}
/**
* 取得某一類所在包的所有類名 不含迭代
*/
public static String[] getPackageAllClassName(String classLocation, String packageName) {
// 將packageName分解
String[] packagePathSplit = packageName.split("[.]");
String realClassLocation = classLocation;
int packageLength = packagePathSplit.length;
for (int i = 0; i < packageLength; i++) {
realClassLocation = realClassLocation + File.separator + packagePathSplit[i];
}
File packeageDir = new File(realClassLocation);
if (packeageDir.isDirectory()) {
String[] allClassName = packeageDir.list();
return allClassName;
}
return null;
}
/**
* 從包package中獲取所有的Class
*
* @param pack
* @return
*/
private static List<Class<?>> getClasses(String packageName) {
// 第一個class類的集合
List<Class<?>> classes = new ArrayList<Class<?>>();
// 是否循環迭代
boolean recursive = true;
// 獲取包的名字 並進行替換
String packageDirName = packageName.replace('.', '/');
// 定義一個枚舉的集合 並進行循環來處理這個目錄下的things
Enumeration<URL> dirs;
try {
dirs = Thread.currentThread().getContextClassLoader().getResources(packageDirName);
// 循環迭代下去
while (dirs.hasMoreElements()) {
// 獲取下一個元素
URL url = dirs.nextElement();
// 得到協議的名稱
String protocol = url.getProtocol();
// 如果是以文件的形式保存在服務器上
if ("file".equals(protocol)) {
// 獲取包的物理路徑
String filePath = URLDecoder.decode(url.getFile(), "UTF-8");
// 以文件的方式掃描整個包下的文件 並添加到集合中
findAndAddClassesInPackageByFile(packageName, filePath, recursive, classes);
} else if ("jar".equals(protocol)) {
// 如果是jar包文件
// 定義一個JarFile
JarFile jar;
try {
// 獲取jar
jar = ((JarURLConnection) url.openConnection()).getJarFile();
// 從此jar包 得到一個枚舉類
Enumeration<JarEntry> entries = jar.entries();
// 同樣的進行循環迭代
while (entries.hasMoreElements()) {
// 獲取jar裏的一個實體 可以是目錄 和一些jar包裏的其他文件 如META-INF等文件
JarEntry entry = entries.nextElement();
String name = entry.getName();
// 如果是以/開頭的
if (name.charAt(0) == '/') {
// 獲取後面的字符串
name = name.substring(1);
}
// 如果前半部分和定義的包名相同
if (name.startsWith(packageDirName)) {
int idx = name.lastIndexOf('/');
// 如果以"/"結尾 是一個包
if (idx != -1) {
// 獲取包名 把"/"替換成"."
packageName = name.substring(0, idx).replace('/', '.');
}
// 如果可以迭代下去 並且是一個包
if ((idx != -1) || recursive) {
// 如果是一個.class文件 而且不是目錄
if (name.endsWith(".class") && !entry.isDirectory()) {
// 去掉後面的".class" 獲取真正的類名
String className = name.substring(packageName.length() + 1, name.length() - 6);
try {
// 添加到classes
classes.add(Class.forName(packageName + '.' + className));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return classes;
}
/**
* 以文件的形式來獲取包下的所有Class
*
* @param packageName
* @param packagePath
* @param recursive
* @param classes
*/
private static void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive, List<Class<?>> classes) {
// 獲取此包的目錄 建立一個File
File dir = new File(packagePath);
// 如果不存在或者 也不是目錄就直接返回
if (!dir.exists() || !dir.isDirectory()) {
return;
}
// 如果存在 就獲取包下的所有文件 包括目錄
File[] dirfiles = dir.listFiles(new FileFilter() {
// 自定義過濾規則 如果可以循環(包含子目錄) 或則是以.class結尾的文件(編譯好的java類文件)
public boolean accept(File file) {
return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));
}
});
// 循環所有文件
for (File file : dirfiles) {
// 如果是目錄 則繼續掃描
if (file.isDirectory()) {
findAndAddClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive, classes);
} else {
// 如果是java類文件 去掉後面的.class 只留下類名
String className = file.getName().substring(0, file.getName().length() - 6);
try {
// 添加到集合中去
classes.add(Class.forName(packageName + '.' + className));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
}
}
測試
用例爲:
package com.zte.sunquan.demo.disruptor.main;
import org.junit.After;
import org.junit.Before;
import com.zte.sunquan.demo.disruptor.DisruptorApplication;
public class Test {
private DisruptorApplication application;
@Before
public void setUp() {
application = new DisruptorApplication(AlarmEvent.class);
}
@org.junit.Test
public void testDisruptor() throws InterruptedException {
//1.準備AlarmEvent
AlarmEvent event = new AlarmEvent(AlarmEvent.AlarmType.HARDWARE_DAMAGE);
//2.發送AlarmEvent
application.publishEvent(event);
}
@org.junit.Test
public void testDisruptor2() throws InterruptedException {
for (int i = 0; i < 100; i++) {
AlarmEvent event = new AlarmEvent(AlarmEvent.AlarmType.NO_POWER);
application.publishEvent(event);
}
}
@After
public void after() throws InterruptedException {
Thread.sleep(1000);
application.close();
}
}
在AbstractApplication中filterHandler用於對消息過濾,因爲按上面的實現一個disruptor只能處理一類消息,相關泛型被擦出,無法匹配判斷。所以用例中使用
new DisruptorApplication(AlarmEvent.class);
將類型信息顯示的傳入。
如果做到這一步,雖然滿足了要求,但功能太過封閉。其它的消息類型如果增加?按上述方式,只能重新定義DisruptorApplication。
AbstractEventHandler
在該類中增加了filter方法
public abstract class AbstractEventHandler<T> implements EventHandler<Event<T>> {
@Override
public void onEvent(Event<T> tEvent, long l, boolean b) throws Exception {
if(filter(tEvent)) {
T t = tEvent.getValue();
handler(t);
}
}
public boolean filter(Event event){
return false;
}
public abstract void handler(T t);
}
這就要求每個消息處理類,都要顯示的指定自身能處理的消息
@Handler
public class UnknownEventHandler extends AbstractEventHandler<OtherEvent> {
@Override
public boolean filter(Event event) {
if (event.getValue().getClass() == OtherEvent.class) {
return true;
}
return false;
}
在AlarmEventHandler中增加filter
@Handler
public class AlarmEventHandler extends AbstractEventHandler<AlarmEvent> {
@Override
public boolean filter(Event event) {
if (event.getValue().getClass() == AlarmEvent.class) {
return true;
}
return false;
}
新的測試用例爲:
@org.junit.Test
public void testDisruptorSuper() throws InterruptedException {
application = new DisruptorApplication();
//1.準備AlarmEvent
AlarmEvent event = new AlarmEvent(AlarmEvent.AlarmType.HARDWARE_DAMAGE);
//2.發送AlarmEvent
application.publishEvent(event);
}
看到構造函數DisruptorApplication中去掉了入參,同時AlarmEventHandler正確處理了消息。
也許你會好奇ODL中是如何做的,他系統範圍內也只有一個Disruptor,在ODL中,disruptor的Handler只是作了轉發Handler,該Handler的工作纔是尋找對應的EventHandler。
ODL實現
private DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) {
this.executor = Preconditions.checkNotNull(executor);
disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, strategy);
disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
disruptor.start();
}
而Handler的onEvent爲
private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS = new EventHandler<DOMNotificationRouterEvent>() {
@Override
public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) throws Exception {
event.deliverNotification();
onEvnetCount.incrementAndGet();
}
};
默認先執行了** event.deliverNotification()**,注意是event中的方法,具體實現:
void deliverNotification() {
LOG.trace("Start delivery of notification {}", notification);
for (ListenerRegistration<? extends DOMNotificationListener> r : subscribers) {
final DOMNotificationListener listener = r.getInstance();
if (listener != null) {
try {
LOG.trace("Notifying listener {}", listener);
listener.onNotification(notification);
LOG.trace("Listener notification completed");
} catch (Exception e) {
LOG.error("Delivery of notification {} caused an error in listener {}", notification, listener, e);
}
}
}
LOG.trace("Delivery completed");
}
可以看到其在一個subscribers的列表中尋找對應的Listenen進行方法調用執行。
看到這,應該明白了ODL的handler只簡單負責轉換,真正的選擇執行對象在** event.deliverNotification()**,那一個事件,如何知道有哪些定閱者呢?必然要存在一個定閱或註冊的過程。代碼如下:
@Override
public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener, final Collection<SchemaPath> types) {
final ListenerRegistration<T> reg = new AbstractListenerRegistration<T>(listener) {
@Override
protected void removeRegistration() {
final ListenerRegistration<T> me = this;
synchronized (DOMNotificationRouter.this) {
replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate<ListenerRegistration<? extends DOMNotificationListener>>() {
@Override
public boolean apply(final ListenerRegistration<? extends DOMNotificationListener> input) {
if (input == me) {
logDomNotificationChanges(listener, null, "removed");
}
return input != me;
}
})));
}
}
};
if (!types.isEmpty()) {
final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b = ImmutableMultimap.builder();
b.putAll(listeners);
for (final SchemaPath t : types) {
b.put(t, reg);
logDomNotificationChanges(listener, t, "added");
}
replaceListeners(b.build());
}
return reg;
}
根據YANG中定義的scheam進行了註冊。這樣在Notification註冊時,則綁定好了事件類型與處理邏輯的對應關係。而在封裝消息時,將subscribe傳遞給了event如下:
private ListenableFuture<Void> publish(final long seq, final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
final DOMNotificationRouterEvent event = disruptor.get(seq);
final ListenableFuture<Void> future = event.initialize(notification, subscribers);
logDomNotificationExecute(subscribers, notification, seq, "publish");
disruptor.getRingBuffer().publish(seq);
publishCount.incrementAndGet();
return future;
}
與上文的實現方式相比,避免了每一個消息,都進行全範圍的Handler的filter判斷。