Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert #12888 for potential task scheduling problems #13021

Merged
merged 1 commit into from Nov 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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