perf[net]: 在task,event,scheduler线程执行的异步请求,请求成功过后依然在相同的线程执行回调任务

This commit is contained in:
godotg
2022-07-28 15:35:44 +08:00
parent 412592110b
commit 004d96a3dd
21 changed files with 202 additions and 230 deletions
@@ -16,7 +16,11 @@ package com.zfoo.event.manager;
import com.zfoo.event.model.event.IEvent;
import com.zfoo.event.model.vo.IEventReceiver;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.util.AssertionUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.util.SafeRunnable;
import com.zfoo.util.math.RandomUtils;
import io.netty.util.concurrent.FastThreadLocalThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -24,12 +28,11 @@ import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author jaysunxiao
* @author godotg
* @version 3.0
*/
public abstract class EventBus {
@@ -43,12 +46,43 @@ public abstract class EventBus {
private static final ExecutorService[] executors = new ExecutorService[EXECUTORS_SIZE];
private static final Map<Long, ExecutorService> threadMap = new ConcurrentHashMap<>();
private static final Map<Class<? extends IEvent>, List<IEventReceiver>> receiverMap = new HashMap<>();
static {
for (int i = 0; i < executors.length; i++) {
var namedThreadFactory = new EventThreadFactory(i + 1);
executors[i] = Executors.newSingleThreadExecutor(namedThreadFactory);
var executor = Executors.newSingleThreadExecutor(namedThreadFactory);
namedThreadFactory.executor = executor;
executors[i] = executor;
}
}
public static class EventThreadFactory implements ThreadFactory {
public ExecutorService executor;
private int poolNumber;
private AtomicInteger threadNumber = new AtomicInteger(1);
private ThreadGroup group;
public EventThreadFactory(int poolNumber) {
var s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
this.poolNumber = poolNumber;
}
@Override
public Thread newThread(Runnable runnable) {
var threadName = StringUtils.format("event-p{}-t{}", poolNumber, threadNumber.getAndIncrement());
var thread = new FastThreadLocalThread(group, runnable, threadName, 0);
thread.setDaemon(false);
thread.setPriority(Thread.NORM_PRIORITY);
thread.setUncaughtExceptionHandler((t, e) -> logger.error(t.toString(), e));
AssertionUtils.notNull(executor);
threadMap.put(thread.getId(), executor);
return thread;
}
}
@@ -77,19 +111,11 @@ public abstract class EventBus {
return;
}
executors[Math.abs(event.threadId() % EXECUTORS_SIZE)].execute(new Runnable() {
@Override
public void run() {
doSubmit(event, list);
}
});
executors[Math.abs(event.threadId() % EXECUTORS_SIZE)].execute(() -> doSubmit(event, list));
}
/**
* 随机获取一个线程
*/
public static Executor asyncExecute() {
return executors[RandomUtils.randomInt(EXECUTORS_SIZE)];
public static void asyncExecute(SafeRunnable runnable) {
execute(RandomUtils.randomInt(EXECUTORS_SIZE), runnable);
}
/**
@@ -98,8 +124,8 @@ public abstract class EventBus {
* @param hashcode
* @return
*/
public static Executor execute(int hashcode) {
return executors[Math.abs(hashcode % EXECUTORS_SIZE)];
public static void execute(int hashcode, SafeRunnable runnable) {
executors[Math.abs(hashcode % EXECUTORS_SIZE)].execute(runnable);
}
/**
@@ -123,6 +149,7 @@ public abstract class EventBus {
/**
* 注册事件及其对应观察者
*
*
* @param eventType
* @param receiver
*/
@@ -130,6 +157,9 @@ public abstract class EventBus {
receiverMap.computeIfAbsent(eventType, it -> new LinkedList<>()).add(receiver);
}
public static Executor threadExecutor(long currentThreadId) {
return threadMap.get(currentThreadId);
}
}
@@ -1,50 +0,0 @@
/*
* Copyright (C) 2020 The zfoo Authors
*
* 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.zfoo.event.manager;
import io.netty.util.concurrent.FastThreadLocalThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author jaysunxiao
* @version 3.0
*/
public class EventThreadFactory implements ThreadFactory {
private static final Logger logger = LoggerFactory.getLogger(EventThreadFactory.class);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
public EventThreadFactory(int poolNumber) {
var s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = "event-p" + poolNumber + "-t";
}
@Override
public Thread newThread(Runnable runnable) {
var t = new FastThreadLocalThread(group, runnable, namePrefix + threadNumber.getAndIncrement(), 0);
t.setDaemon(false);
t.setPriority(Thread.NORM_PRIORITY);
t.setUncaughtExceptionHandler((thread, e) -> logger.error(thread.toString(), e));
return t;
}
}