Change Scheduled to FixedRate in Traffic Counter (#9245)

Motivation:

Traffic shaping needs more accurate execution than scheduled one. So the
use of FixedRate instead.
Moreover the current implementation tends to create as many threads as
channels use a ChannelTrafficShapingHandlern, which is unnecessary.

Modifications:

Change the executor.schedule to executor.scheduleAtFixedRate in the
start and remove the reschedule call from run monitor thread since it
will be restarted by the Fixed rate executor.
Also fix a minor bug where restart was only doing start() without stop()
before.

Result:

Threads are more stable in number of cached and precision of traffic
shaping is enhanced.
This commit is contained in:
Frédéric Brégier 2019-06-18 09:34:49 +02:00 committed by Norman Maurer
parent 93414db1f3
commit b1fb40e42d
2 changed files with 4 additions and 6 deletions

View File

@ -78,8 +78,6 @@ public class GlobalChannelTrafficCounter extends TrafficCounter {
perChannel.channelTrafficCounter.resetAccounting(newLastTime);
}
trafficShapingHandler1.doAccounting(counter);
counter.scheduledFuture = counter.executor.schedule(this, counter.checkInterval.get(),
TimeUnit.MILLISECONDS);
}
}
@ -97,7 +95,7 @@ public class GlobalChannelTrafficCounter extends TrafficCounter {
monitorActive = true;
monitor = new MixedTrafficMonitoringTask((GlobalChannelTrafficShapingHandler) trafficShapingHandler, this);
scheduledFuture =
executor.schedule(monitor, localCheckInterval, TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(monitor, 0, localCheckInterval, TimeUnit.MILLISECONDS);
}
}

View File

@ -174,7 +174,6 @@ public class TrafficCounter {
if (trafficShapingHandler != null) {
trafficShapingHandler.doAccounting(TrafficCounter.this);
}
scheduledFuture = executor.schedule(this, checkInterval.get(), TimeUnit.MILLISECONDS);
}
}
@ -192,7 +191,7 @@ public class TrafficCounter {
monitorActive = true;
monitor = new TrafficMonitoringTask();
scheduledFuture =
executor.schedule(monitor, localCheckInterval, TimeUnit.MILLISECONDS);
executor.scheduleAtFixedRate(monitor, 0, localCheckInterval, TimeUnit.MILLISECONDS);
}
}
@ -317,7 +316,8 @@ public class TrafficCounter {
// No more active monitoring
lastTime.set(milliSecondFromNano());
} else {
// Start if necessary
// Restart
stop();
start();
}
}