netty时间轮

本文详细解析了Netty中的HashedWheelTimer,包括其构造过程、重要参数(如时间间隔、线程工厂等)、任务添加与处理机制,以及如何保证定时任务的执行效率和准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

netty时间轮HashedWheelTimer

HashedWheelTimer里面的一些重要参数

//时间轮工作状态更新器
private volatile int workerState;
//创建是INIT -> STARTED -> SHUTDOWN
public static final int WORKER_STATE_INIT = 0;
public static final int WORKER_STATE_STARTED = 1;
public static final int WORKER_STATE_SHUTDOWN = 2;
//初始化该时间轮的runable,时间轮是单线程的,单线程执行的是该runnable,其中的run方法就是时间轮的核心逻辑
private final Worker worker = new Worker();
//workerThread 单线程用于处理所有的定时任务,它会在每个tick执行一个bucket中所有的定时任务,
//以及一些其他的操作。意味着定时任务不能有较大的阻塞和耗时,不然就会影响定时任务执行的准时性和有效性。
private final Thread workerThread;
//时间间隔
private final long tickDuration;
//时间轮数组,数组的每一个位置存放的是HashedWheelBucket类型的双向链表
private final HashedWheelBucket[] wheel;
//掩码,计算定时任务要存入的数组的下标
private final int mask;
//这个属性也很重要,在具体方法内会看到用处,先混个眼熟
private final CountDownLatch startTimeInitialized = new CountDownLatch(1);
//等待执行的定时任务的个数
private final AtomicLong pendingTimeouts = new AtomicLong(0);
//最大的任务数量。当HashedWheelTimer实例上的任务超出这个数量时会抛出错误
private final long maxPendingTimeouts;
//时间轮的启动时间
private volatile long startTime;
//放任务
private final Queue<HashedWheelTimeout> timeouts = PlatformDependent.newMpscQueue();
//放被取消的任务
private final Queue<HashedWheelTimeout> cancelledTimeouts = PlatformDependent.newMpscQueue();

HashedWheelTimer构造方法

public HashedWheelTimer(
    ThreadFactory threadFactory,
    long tickDuration, TimeUnit unit, int ticksPerWheel, boolean leakDetection,
    long maxPendingTimeouts) {

    if (threadFactory == null) {
        throw new NullPointerException("threadFactory");
    }
    if (unit == null) {
        throw new NullPointerException("unit");
    }
    if (tickDuration <= 0) {
        throw new IllegalArgumentException("tickDuration must be greater than 0: " + tickDuration);
    }
    if (ticksPerWheel <= 0) {
        throw new IllegalArgumentException("ticksPerWheel must be greater than 0: " + ticksPerWheel);
    }

    // Normalize ticksPerWheel to power of two and initialize the wheel.
    //创建一个HashedWheelBucket[] wheel 2^n次 没一个下面都是HashedWheelBucket 是一个双向链表
    wheel = createWheel(ticksPerWheel);
    // n - 1
    mask = wheel.length - 1;

    // Convert tickDuration to nanos.
    //时间换算成纳秒 100ms -> 100 * (10^6)
    long duration = unit.toNanos(tickDuration);

    // 时间不能太长
    if (duration >= Long.MAX_VALUE / wheel.length) {
        throw new IllegalArgumentException(String.format(
            "tickDuration: %d (expected: 0 < tickDuration in nanos < %d",
            tickDuration, Long.MAX_VALUE / wheel.length));
    }
	//最低1纳秒
    if (duration < MILLISECOND_NANOS) {
        if (logger.isWarnEnabled()) {
            logger.warn("Configured tickDuration %d smaller then %d, using 1ms.",
                        tickDuration, MILLISECOND_NANOS);
        }
        this.tickDuration = MILLISECOND_NANOS;
    } else {
        this.tickDuration = duration;
    }
    //workerThread 单线程用于处理所有的定时任务,它会在每个tick执行一个bucket中所有的定时任务,
    //以及一些其他的操作。意味着定时任务不能有较大的阻塞和耗时,不然就会影响定时任务执行的准时性和有效性。
    //private final Thread workerThread;
    //private final Worker worker = new Worker(); 是一个runable
    workerThread = threadFactory.newThread(worker);
	
    //内存泄漏检测
    leak = leakDetection || !workerThread.isDaemon() ? leakDetector.track(this) : null;
	//当时间轮上的定时任务数量超过该值就报警
    this.maxPendingTimeouts = maxPendingTimeouts;
	//如果创建的时间轮对象超过64个,也会报警。一个时间轮就是一个线程,线程太多也会影响性能
    if (INSTANCE_COUNTER.incrementAndGet() > INSTANCE_COUNT_LIMIT &&
        WARNED_TOO_MANY_INSTANCES.compareAndSet(false, true)) {
        reportTooManyInstances();
    }
}

newTimeout放入一个timerTask

//task一个runable
//delay 延迟多少秒
//unit 时间单位
public Timeout newTimeout(TimerTask task, long delay, TimeUnit unit) {
    if (task == null) {
        throw new NullPointerException("task");
    }
    if (unit == null) {
        throw new NullPointerException("unit");
    }
	//添加任务之后,等待执行的任务加1
    long pendingTimeoutsCount = pendingTimeouts.incrementAndGet();
	//任务是否过多
    if (maxPendingTimeouts > 0 && pendingTimeoutsCount > maxPendingTimeouts) {
        pendingTimeouts.decrementAndGet();
        throw new RejectedExecutionException("Number of pending timeouts ("
            + pendingTimeoutsCount + ") is greater than or equal to maximum allowed pending "
            + "timeouts (" + maxPendingTimeouts + ")");
    }
    //启动工作线程,并且确保只启动一次,这里面会设计线程的等待和唤醒
    start();
	//下面的代码保证startTime有值
    // Add the timeout to the timeout queue which will be processed on the next tick.
    // During processing all the queued HashedWheelTimeouts will be added to the correct HashedWheelBucket.
    long deadline = System.nanoTime() + unit.toNanos(delay) - startTime;

    // Guard against overflow.
    if (delay > 0 && deadline < 0) {
        deadline = Long.MAX_VALUE;
    }
    //包装任务
    HashedWheelTimeout timeout = new HashedWheelTimeout(this, task, deadline);
    //放任务,后面的看定时任务
    timeouts.add(timeout);
    return timeout;
}

start()

public void start() {
    //判断时间轮的工作状态
    switch (WORKER_STATE_UPDATER.get(this)) {
        case WORKER_STATE_INIT:
            //cas更新到开始状态
            if (WORKER_STATE_UPDATER.compareAndSet(this, WORKER_STATE_INIT, WORKER_STATE_STARTED)) {
                //启动work线程,该线程一旦启动,就会执行任务,所以核心在work线程要执行的runable的run方法内
                workerThread.start();
            }
            break;
            //如果启动了就什么也不做
        case WORKER_STATE_STARTED:
            break;
            //如果状态是结束,就抛出异常
        case WORKER_STATE_SHUTDOWN:
            throw new IllegalStateException("cannot be started once stopped");
        default:
            throw new Error("Invalid WorkerState");
    }
    //这里会暂停一卡,因为要等待work线程启动完全,并且starttime被赋值成功
    while (startTime == 0) {
        try {
            startTimeInitialized.await();
        } catch (InterruptedException ignore) {
            // Ignore - it will be ready very soon.
        }
    }
}

netty时间轮重要代码

worker.run()

public void run() {
    // Initialize the startTime.
    // 初始化开始的时间
    startTime = System.nanoTime();
    if (startTime == 0) {
        // We use 0 as an indicator for the uninitialized value here, so make sure it's not 0 when initialized.
        startTime = 1;
    }
	
    // Notify the other threads waiting for the initialization at start().
    //第一个人在 await()
    startTimeInitialized.countDown();
	//这边有点问题,如果这个定时任务一个地方晚了,所有的地方就会晚
    do {
        //到下一个时间点
        final long deadline = waitForNextTick();
        //如果返回的时间>0
        if (deadline > 0) {
            int idx = (int) (tick & mask);
            //获得所有取消的任务,刹车农户
            processCancelledTasks();
            //获得具体的bucket里面都是任务
            HashedWheelBucket bucket =
                wheel[idx];
            //转移任务到具体的bucket里面
            transferTimeoutsToBuckets();
            //开始执行需要执行的任务
            bucket.expireTimeouts(deadline);
            //tick++到下个wheel的区间
            tick++;
        }
        //如果不是WORKER_STATE_STARTED就不循环了
    } while (WORKER_STATE_UPDATER.get(HashedWheelTimer.this) == WORKER_STATE_STARTED);
	
    // Fill the unprocessedTimeouts so we can return them from stop() method.
    //所有没有执行的放到unprocessedTimeouts这个set里面
    for (HashedWheelBucket bucket: wheel) {
        bucket.clearTimeouts(unprocessedTimeouts);
    }
    for (;;) {
        //还没有提交的也拿出来
        HashedWheelTimeout timeout = timeouts.poll();
        if (timeout == null) {
            break;
        }
        if (!timeout.isCancelled()) {
            //如果没有cancel的放入到unprocessedTimeouts里面
            unprocessedTimeouts.add(timeout);
        }
    }
    processCancelledTasks();
}

Timeout.cancel() 没有直接取消

public boolean cancel() {
    // only update the state it will be removed from HashedWheelBucket on next tick.
    if (!compareAndSetState(ST_INIT, ST_CANCELLED)) {
        return false;
    }
    // If a task should be canceled we put this to another queue which will be processed on each tick.
    // So this means that we will have a GC latency of max. 1 tick duration which is good enough. This way
    // we can make again use of our MpscLinkedQueue and so minimize the locking / overhead as much as possible.
    //队列里面添加
    timer.cancelledTimeouts.add(this);
    return true;
}

processCancelledTasks 处理cancel的任务,没有在cancel方法里面直接处理

private void processCancelledTasks() {
    for (;;) {
        //canel的取消掉,从双向链表里面干掉
        HashedWheelTimeout timeout = cancelledTimeouts.poll();
        if (timeout == null) {
            // all processed
            break;
        }
        try {
            //双向链表里面干掉
            timeout.remove();
        } catch (Throwable t) {
            if (logger.isWarnEnabled()) {
                logger.warn("An exception was thrown while process a cancellation task", t);
            }
        }
    }
}

waitForNextTick() 计算下一个tickDuration的时间,如果还没有到下一个就sleep对应的时间,如果超过了这个时间,直接跳出

tick在每次外面循环之后都++

private long waitForNextTick() {
    long deadline = tickDuration * (tick + 1);

    for (;;) {
        final long currentTime = System.nanoTime() - startTime;
        long sleepTimeMs = (deadline - currentTime + 999999) / 1000000;
		//说明已经上一个了
        if (sleepTimeMs <= 0) {
            if (currentTime == Long.MIN_VALUE) {
                return -Long.MAX_VALUE;
            } else {
                return currentTime;
            }
        }

        // Check if we run on windows, as if thats the case we will need
        // to round the sleepTime as workaround for a bug that only affect
        // the JVM if it runs on windows.
        //
        // See https://github.com/netty/netty/issues/356
        if (PlatformDependent.isWindows()) {
            sleepTimeMs = sleepTimeMs / 10 * 10;
        }

        try {
            Thread.sleep(sleepTimeMs);
        } catch (InterruptedException ignored) {
            //响应中断,如果状态是WORKER_STATE_SHUTDOWN,返回Long.MIN_VALUE外面判断<0
            if (WORKER_STATE_UPDATER.get(HashedWheelTimer.this) == WORKER_STATE_SHUTDOWN) {
                return Long.MIN_VALUE;
            }
        }
    }
}

transferTimeoutsToBuckets从timeouts的队列放到具体的HashedWheelBucket下面

private void transferTimeoutsToBuckets() {
    // transfer only max. 100000 timeouts per tick to prevent a thread to stale the workerThread when it just
    // adds new timeouts in a loop.
    for (int i = 0; i < 100000; i++) {
        HashedWheelTimeout timeout = timeouts.poll();
        if (timeout == null) {
            // all processed
            break;
        }
        if (timeout.state() == HashedWheelTimeout.ST_CANCELLED) {
            // Was cancelled in the meantime.
            continue;
        }

        long calculated = timeout.deadline / tickDuration;
        //计算一下我有多少圈,假设我时间4S一圈,每个格子1S,我现在是14S ,remainingRounds = 3,然后落到(15 - 3*4 = 2,下标为1)位置的地方
        timeout.remainingRounds = (calculated - tick) / wheel.length;

        final long ticks = Math.max(calculated, tick); // Ensure we don't schedule for past.
        int stopIndex = (int) (ticks & mask);
		//获得具体的,放入到bucket下面
        HashedWheelBucket bucket = wheel[stopIndex];
        bucket.addTimeout(timeout);
    }
}

expireTimeouts执行具体方法的地方

public void expireTimeouts(long deadline) {
    HashedWheelTimeout timeout = head;

    // process all timeouts
    while (timeout != null) {
        HashedWheelTimeout next = timeout.next;
        if (timeout.remainingRounds <= 0) {
            //如果<=0 从HashedWheelTimeout移除
            next = remove(timeout);
            if (timeout.deadline <= deadline) {
                //执行task任务
                timeout.expire();
            } else {
                //说明时间轮有问题
                // The timeout was placed into a wrong slot. This should never happen.
                throw new IllegalStateException(String.format(
                        "timeout.deadline (%d) > deadline (%d)", timeout.deadline, deadline));
            }
            //判断是否是cancel的在执行中,直接remove
        } else if (timeout.isCancelled()) {
            next = remove(timeout);
        } else {
            //圈数-1
            timeout.remainingRounds --;
        }
        //到下一个,继续循环
        timeout = next;
    }
}

stop()方法

public Set<Timeout> stop() {
    if (Thread.currentThread() == workerThread) {
        throw new IllegalStateException(
                HashedWheelTimer.class.getSimpleName() +
                        ".stop() cannot be called from " +
                        TimerTask.class.getSimpleName());
    }

    if (!WORKER_STATE_UPDATER.compareAndSet(this, WORKER_STATE_STARTED, WORKER_STATE_SHUTDOWN)) {
        // workerState can be 0 or 2 at this moment - let it always be 2.
        if (WORKER_STATE_UPDATER.getAndSet(this, WORKER_STATE_SHUTDOWN) != WORKER_STATE_SHUTDOWN) {
            INSTANCE_COUNTER.decrementAndGet();
            if (leak != null) {
                boolean closed = leak.close(this);
                assert closed;
            }
        }

        return Collections.emptySet();
    }

    try {
        boolean interrupted = false;
        while (workerThread.isAlive()) {
            workerThread.interrupt();
            try {
                workerThread.join(100);
            } catch (InterruptedException ignored) {
                interrupted = true;
            }
        }

        if (interrupted) {
            Thread.currentThread().interrupt();
        }
    } finally {
        INSTANCE_COUNTER.decrementAndGet();
        if (leak != null) {
            boolean closed = leak.close(this);
            assert closed;
        }
    }
    //返回我们上面说的那个如果跳出那个循环都是时候
    return worker.unprocessedTimeouts();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值