Skip to content

Commit

Permalink
Revert#12888 for potential scheduling problems (#13021)
Browse files Browse the repository at this point in the history
Motivation:

The code I commited in #12888 may cause unexpected task scheduling problems.


Modification:

Revert this commit.


Result:

Fixes #13018 .
  • Loading branch information
needmorecode committed Nov 28, 2022
1 parent 3bff0be commit b64a6e2
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions common/src/main/java/io/netty/util/HashedWheelTimer.java
Expand Up @@ -116,7 +116,6 @@ public class HashedWheelTimer implements Timer {
private final AtomicLong pendingTimeouts = new AtomicLong(0);
private final long maxPendingTimeouts;
private final Executor taskExecutor;
private long currRound;

private volatile long startTime;

Expand Down Expand Up @@ -497,14 +496,11 @@ public void run() {
final long deadline = waitForNextTick();
if (deadline > 0) {
int idx = (int) (tick & mask);
if (idx == 0 && tick > 0) {
currRound ++;
}
processCancelledTasks();
HashedWheelBucket bucket =
wheel[idx];
transferTimeoutsToBuckets();
bucket.expireTimeouts(deadline, currRound);
bucket.expireTimeouts(deadline);
tick++;
}
} while (WORKER_STATE_UPDATER.get(HashedWheelTimer.this) == WORKER_STATE_STARTED);
Expand Down Expand Up @@ -540,7 +536,7 @@ private void transferTimeoutsToBuckets() {
}

long calculated = timeout.deadline / tickDuration;
timeout.execRound = calculated / wheel.length;
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);
Expand Down Expand Up @@ -630,9 +626,9 @@ private static final class HashedWheelTimeout implements Timeout, Runnable {
@SuppressWarnings({"unused", "FieldMayBeFinal", "RedundantFieldInitialization" })
private volatile int state = ST_INIT;

// execRound will be calculated and set by Worker.transferTimeoutsToBuckets() before the
// remainingRounds will be calculated and set by Worker.transferTimeoutsToBuckets() before the
// HashedWheelTimeout will be added to the correct HashedWheelBucket.
long execRound;
long remainingRounds;

// This will be used to chain timeouts in HashedWheelTimerBucket via a double-linked-list.
// As only the workerThread will act on it there is no need for synchronization / volatile.
Expand Down Expand Up @@ -782,13 +778,13 @@ public void addTimeout(HashedWheelTimeout timeout) {
/**
* Expire all {@link HashedWheelTimeout}s for the given {@code deadline}.
*/
public void expireTimeouts(long deadline, long currRound) {
public void expireTimeouts(long deadline) {
HashedWheelTimeout timeout = head;

// process all timeouts
while (timeout != null) {
HashedWheelTimeout next = timeout.next;
if (timeout.execRound <= currRound) {
if (timeout.remainingRounds <= 0) {
next = remove(timeout);
if (timeout.deadline <= deadline) {
timeout.expire();
Expand All @@ -800,7 +796,7 @@ public void expireTimeouts(long deadline, long currRound) {
} else if (timeout.isCancelled()) {
next = remove(timeout);
} else {
break;
timeout.remainingRounds --;
}
timeout = next;
}
Expand Down

0 comments on commit b64a6e2

Please sign in to comment.