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

This commit is contained in:
godotg committed 2022-07-28 15:35:44 +08:00
1 parent 412592110b
commit 004d96a3dd
21 files changed
+202 -230

No files matched your search

@@ -14,22 +14,23 @@
package com.zfoo.scheduler.manager;
import com.zfoo.protocol.collection.CollectionUtils;
import com.zfoo.protocol.util.StringUtils;
import com.zfoo.scheduler.SchedulerContext;
import com.zfoo.scheduler.model.vo.SchedulerDefinition;
import com.zfoo.scheduler.util.TimeUtils;
import com.zfoo.util.SafeRunnable;
import io.netty.util.concurrent.FastThreadLocalThread;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Instant;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author jaysunxiao
* @author godotg
* @version 3.0
*/
public abstract class SchedulerBus {
@@ -41,6 +42,9 @@ public abstract class SchedulerBus {
* scheduler默认只有一个单线程的线程池
*/
private static final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new SchedulerThreadFactory(1));
private static long threadId = 0;
/**
* 上一次trigger触发时间
*/
@@ -63,6 +67,31 @@ public abstract class SchedulerBus {
}, 0, TRIGGER_MILLIS_INTERVAL, TimeUnit.MILLISECONDS);
}
public static class SchedulerThreadFactory implements ThreadFactory {
private int poolNumber;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final ThreadGroup group;
public SchedulerThreadFactory(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("scheduler-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));
threadId = thread.getId();
return thread;
}
}
public static void refreshMinTriggerTimestamp() {
var minTimestamp = Long.MAX_VALUE;
@@ -134,46 +163,24 @@ public abstract class SchedulerBus {
/**
* 不断执行的周期循环任务
*/
public static void scheduleAtFixedRate(Runnable runnable, long period, TimeUnit unit) {
public static void scheduleAtFixedRate(SafeRunnable runnable, long period, TimeUnit unit) {
if (SchedulerContext.isStop()) {
return;
}
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
runnable.run();
} catch (Exception e) {
logger.error("scheduleAtFixedRate未知exception异常", e);
} catch (Throwable t) {
logger.error("scheduleAtFixedRate未知error异常", t);
}
}
}, 0, period, unit);
executor.scheduleAtFixedRate(runnable, 0, period, unit);
}
/**
* 固定延迟执行的任务
*/
public static void schedule(Runnable runnable, long delay, TimeUnit unit) {
public static void schedule(SafeRunnable runnable, long delay, TimeUnit unit) {
if (SchedulerContext.isStop()) {
return;
}
executor.schedule(new Runnable() {
@Override
public void run() {
try {
runnable.run();
} catch (Exception e) {
logger.error("schedule未知exception异常", e);
} catch (Throwable t) {
logger.error("schedule未知error异常", t);
}
}
}, delay, unit);
executor.schedule(runnable, delay, unit);
}
/**
@@ -186,4 +193,8 @@ public abstract class SchedulerBus {
schedulerDefList.add(SchedulerDefinition.valueOf(cron, runnable));
}
public static Executor threadExecutor(long currentThreadId) {
return threadId == currentThreadId ? executor : null;
}
}
@@ -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.scheduler.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 SchedulerThreadFactory implements ThreadFactory {
private static final Logger logger = LoggerFactory.getLogger(SchedulerThreadFactory.class);
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
public SchedulerThreadFactory(int poolNumber) {
var s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = "scheduler-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;
}
}