Various renames on TrafficCounter methods

This commit is contained in:
Trustin Lee 2009-04-08 07:30:40 +00:00
parent e3fc229b03
commit cc181204c0

View File

@ -64,12 +64,12 @@ public class TrafficCounter {
/** /**
* Long life writing bytes * Long life writing bytes
*/ */
private final AtomicLong cumulativeWritingBytes = new AtomicLong(0); private final AtomicLong cumulativeWrittenBytes = new AtomicLong(0);
/** /**
* Long life reading bytes * Long life reading bytes
*/ */
private final AtomicLong cumulativeReadingBytes = new AtomicLong(0); private final AtomicLong cumulativeReadBytes = new AtomicLong(0);
/** /**
* Last writing bandwidth * Last writing bandwidth
@ -89,12 +89,12 @@ public class TrafficCounter {
/** /**
* Last written bytes number * Last written bytes number
*/ */
private long lastWritingBytes = 0; private long lastWrittenBytes = 0;
/** /**
* Last read bytes number * Last read bytes number
*/ */
private long lastReadingBytes = 0; private long lastReadBytes = 0;
/** /**
* Current Limit in B/s to apply to write * Current Limit in B/s to apply to write
@ -168,8 +168,8 @@ public class TrafficCounter {
*/ */
protected TrafficMonitoring(long checkInterval, protected TrafficMonitoring(long checkInterval,
TrafficCounterFactory factory, TrafficCounter counter) { TrafficCounterFactory factory, TrafficCounter counter) {
this.checkInterval1 = checkInterval; checkInterval1 = checkInterval;
this.factory1 = factory; factory1 = factory;
this.counter = counter; this.counter = counter;
} }
@ -179,16 +179,16 @@ public class TrafficCounter {
public void run() { public void run() {
try { try {
for (;;) { for (;;) {
if (this.checkInterval1 > 0) { if (checkInterval1 > 0) {
Thread.sleep(this.checkInterval1); Thread.sleep(checkInterval1);
} else { } else {
// Delay goes to TrafficCounterFactory.NO_STAT, so exit // Delay goes to TrafficCounterFactory.NO_STAT, so exit
return; return;
} }
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
this.counter.resetAccounting(endTime); counter.resetAccounting(endTime);
if (this.factory1 != null) { if (factory1 != null) {
this.factory1.accounting(this.counter); factory1.accounting(counter);
} }
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -201,15 +201,15 @@ public class TrafficCounter {
* *
*/ */
public void start() { public void start() {
synchronized (this.lastTime) { synchronized (lastTime) {
if (this.monitorFuture != null) { if (monitorFuture != null) {
return; return;
} }
this.lastTime.set(System.currentTimeMillis()); lastTime.set(System.currentTimeMillis());
if (this.checkInterval > 0) { if (checkInterval > 0) {
this.monitorFuture = monitorFuture =
this.executorService.submit(new TrafficMonitoring(this.checkInterval, executorService.submit(new TrafficMonitoring(checkInterval,
this.factory, this)); factory, this));
} }
} }
} }
@ -219,15 +219,15 @@ public class TrafficCounter {
* *
*/ */
public void stop() { public void stop() {
synchronized (this.lastTime) { synchronized (lastTime) {
if (this.monitorFuture == null) { if (monitorFuture == null) {
return; return;
} }
this.monitorFuture.cancel(true); monitorFuture.cancel(true);
this.monitorFuture = null; monitorFuture = null;
resetAccounting(System.currentTimeMillis()); resetAccounting(System.currentTimeMillis());
if (this.factory != null) { if (factory != null) {
this.factory.accounting(this); factory.accounting(this);
} }
setMonitoredChannel(null); setMonitoredChannel(null);
} }
@ -239,17 +239,17 @@ public class TrafficCounter {
* @param newLastTime * @param newLastTime
*/ */
protected void resetAccounting(long newLastTime) { protected void resetAccounting(long newLastTime) {
synchronized (this.lastTime) { synchronized (lastTime) {
long interval = newLastTime - this.lastTime.getAndSet(newLastTime); long interval = newLastTime - lastTime.getAndSet(newLastTime);
if (interval == 0) { if (interval == 0) {
// nothing to do // nothing to do
return; return;
} }
this.lastReadingBytes = this.currentReadingBytes.getAndSet(0); lastReadBytes = currentReadingBytes.getAndSet(0);
this.lastWritingBytes = this.currentWritingBytes.getAndSet(0); lastWrittenBytes = currentWritingBytes.getAndSet(0);
this.lastReadingThroughput = this.lastReadingBytes / interval * 1000; lastReadingThroughput = lastReadBytes / interval * 1000;
// nb byte / checkInterval in ms * 1000 (1s) // nb byte / checkInterval in ms * 1000 (1s)
this.lastWritingThroughput = this.lastWritingBytes / interval * 1000; lastWritingThroughput = lastWrittenBytes / interval * 1000;
// nb byte / checkInterval in ms * 1000 (1s) // nb byte / checkInterval in ms * 1000 (1s)
} }
} }
@ -297,11 +297,11 @@ public class TrafficCounter {
*/ */
protected void setMonitoredChannel(Channel channel) { protected void setMonitoredChannel(Channel channel) {
if (channel != null) { if (channel != null) {
this.monitoredChannel = channel; monitoredChannel = channel;
this.isPerChannel = true; isPerChannel = true;
} else { } else {
this.isPerChannel = false; isPerChannel = false;
this.monitoredChannel = null; monitoredChannel = null;
} }
} }
@ -318,8 +318,8 @@ public class TrafficCounter {
*/ */
public void configure(Channel channel, long writeLimit, public void configure(Channel channel, long writeLimit,
long readLimit) { long readLimit) {
this.limitWrite = writeLimit; limitWrite = writeLimit;
this.limitRead = readLimit; limitRead = readLimit;
setMonitoredChannel(channel); setMonitoredChannel(channel);
} }
@ -338,18 +338,18 @@ public class TrafficCounter {
*/ */
public void configure(Channel channel, long writeLimit, public void configure(Channel channel, long writeLimit,
long readLimit, long delayToSet) { long readLimit, long delayToSet) {
if (this.checkInterval != delayToSet) { if (checkInterval != delayToSet) {
this.checkInterval = delayToSet; checkInterval = delayToSet;
if (this.monitorFuture == null) { if (monitorFuture == null) {
this.configure(channel, writeLimit, readLimit); this.configure(channel, writeLimit, readLimit);
return; return;
} }
stop(); stop();
if (this.checkInterval > 0) { if (checkInterval > 0) {
start(); start();
} else { } else {
// No more active monitoring // No more active monitoring
this.lastTime.set(System.currentTimeMillis()); lastTime.set(System.currentTimeMillis());
} }
} }
this.configure(channel, writeLimit, readLimit); this.configure(channel, writeLimit, readLimit);
@ -361,13 +361,13 @@ public class TrafficCounter {
* be negative time * be negative time
*/ */
private long getReadTimeToWait() { private long getReadTimeToWait() {
synchronized (this.lastTime) { synchronized (lastTime) {
long interval = System.currentTimeMillis() - this.lastTime.get(); long interval = System.currentTimeMillis() - lastTime.get();
if (interval == 0) { if (interval == 0) {
// Time is too short, so just lets continue // Time is too short, so just lets continue
return 0; return 0;
} }
long wait = this.currentReadingBytes.get() * 1000 / this.limitRead - long wait = currentReadingBytes.get() * 1000 / limitRead -
interval; interval;
return wait; return wait;
} }
@ -379,14 +379,14 @@ public class TrafficCounter {
* be negative time * be negative time
*/ */
private long getWriteTimeToWait() { private long getWriteTimeToWait() {
synchronized (this.lastTime) { synchronized (lastTime) {
long interval = System.currentTimeMillis() - this.lastTime.get(); long interval = System.currentTimeMillis() - lastTime.get();
if (interval == 0) { if (interval == 0) {
// Time is too short, so just lets continue // Time is too short, so just lets continue
return 0; return 0;
} }
long wait = this.currentWritingBytes.get() * 1000 / long wait = currentWritingBytes.get() * 1000 /
this.limitWrite - interval; limitWrite - interval;
return wait; return wait;
} }
} }
@ -429,21 +429,21 @@ public class TrafficCounter {
*/ */
public void run() { public void run() {
try { try {
Thread.sleep(this.timeToWait); Thread.sleep(timeToWait);
} catch (InterruptedException e) { } catch (InterruptedException e) {
// interruption so exit // interruption so exit
return; return;
} }
// logger.info("WAKEUP!"); // logger.info("WAKEUP!");
if ((this.monitor != null) && if (monitor != null &&
(this.monitor.monitoredChannel != null) && monitor.monitoredChannel != null &&
this.monitor.monitoredChannel.isConnected()) { monitor.monitoredChannel.isConnected()) {
// logger.warn(" setReadable TRUE: "+timeToWait); // logger.warn(" setReadable TRUE: "+timeToWait);
if (this.ctx.getHandler() instanceof TrafficShapingHandler) { if (ctx.getHandler() instanceof TrafficShapingHandler) {
// readSuspended = false; // readSuspended = false;
this.ctx.setAttachment(null); ctx.setAttachment(null);
} }
this.monitor.monitoredChannel.setReadable(true); monitor.monitoredChannel.setReadable(true);
} }
} }
} }
@ -460,14 +460,14 @@ public class TrafficCounter {
*/ */
protected void bytesRecvFlowControl(ChannelHandlerContext ctx, long recv) protected void bytesRecvFlowControl(ChannelHandlerContext ctx, long recv)
throws InterruptedException { throws InterruptedException {
this.currentReadingBytes.addAndGet(recv); currentReadingBytes.addAndGet(recv);
this.cumulativeReadingBytes.addAndGet(recv); cumulativeReadBytes.addAndGet(recv);
if (this.limitRead == 0) { if (limitRead == 0) {
// no action // no action
return; return;
} }
if (this.isPerChannel && (this.monitoredChannel != null) && if (isPerChannel && monitoredChannel != null &&
(!this.monitoredChannel.isConnected())) { !monitoredChannel.isConnected()) {
// no action can be taken since setReadable will throw a // no action can be taken since setReadable will throw a
// NotYetConnected // NotYetConnected
return; return;
@ -476,10 +476,10 @@ public class TrafficCounter {
long wait = getReadTimeToWait(); long wait = getReadTimeToWait();
if (wait > 20) { // At least 20ms seems a minimal time in order to if (wait > 20) { // At least 20ms seems a minimal time in order to
// try to limit the traffic // try to limit the traffic
if (this.isPerChannel && (this.monitoredChannel != null) && if (isPerChannel && monitoredChannel != null &&
this.monitoredChannel.isConnected()) { monitoredChannel.isConnected()) {
// Channel version // Channel version
if (this.executorService == null) { if (executorService == null) {
// Sleep since no executor // Sleep since no executor
Thread.sleep(wait); Thread.sleep(wait);
return; return;
@ -489,9 +489,9 @@ public class TrafficCounter {
// readSuspended = true; // readSuspended = true;
ctx.setAttachment(Boolean.TRUE); ctx.setAttachment(Boolean.TRUE);
} }
this.monitoredChannel.setReadable(false); monitoredChannel.setReadable(false);
// logger.info("Read will wakeup after "+wait+" ms "+this); // logger.info("Read will wakeup after "+wait+" ms "+this);
this.executorService executorService
.submit(new ReopenRead(ctx, this, wait)); .submit(new ReopenRead(ctx, this, wait));
} else { } else {
// should be waiting: but can occurs sometime so as a FIX // should be waiting: but can occurs sometime so as a FIX
@ -515,9 +515,9 @@ public class TrafficCounter {
* @throws InterruptedException * @throws InterruptedException
*/ */
protected void bytesWriteFlowControl(long write) throws InterruptedException { protected void bytesWriteFlowControl(long write) throws InterruptedException {
this.currentWritingBytes.addAndGet(write); currentWritingBytes.addAndGet(write);
this.cumulativeWritingBytes.addAndGet(write); cumulativeWrittenBytes.addAndGet(write);
if (this.limitWrite == 0) { if (limitWrite == 0) {
return; return;
} }
// compute the number of ms to wait before continue with the channel // compute the number of ms to wait before continue with the channel
@ -534,7 +534,7 @@ public class TrafficCounter {
* in ms * in ms
*/ */
public long getCheckInterval() { public long getCheckInterval() {
return this.checkInterval; return checkInterval;
} }
/** /**
@ -542,7 +542,7 @@ public class TrafficCounter {
* @return the current Read Throughput in byte/s * @return the current Read Throughput in byte/s
*/ */
public long getLastReadThroughput() { public long getLastReadThroughput() {
return this.lastReadingThroughput; return lastReadingThroughput;
} }
/** /**
@ -550,37 +550,37 @@ public class TrafficCounter {
* @return the current Write Throughput in byte/s * @return the current Write Throughput in byte/s
*/ */
public long getLastWriteThroughput() { public long getLastWriteThroughput() {
return this.lastWritingThroughput; return lastWritingThroughput;
} }
/** /**
* *
* @return the current number of byte read since last checkInterval * @return the current number of byte read since last checkInterval
*/ */
public long getLastBytesRead() { public long getLastReadBytes() {
return this.lastReadingBytes; return lastReadBytes;
} }
/** /**
* *
* @return the current number of byte written since last checkInterval * @return the current number of byte written since last checkInterval
*/ */
public long getLastBytesWritten() { public long getLastWrittenBytes() {
return this.lastWritingBytes; return lastWrittenBytes;
} }
/** /**
* @return the cumulativeWritingBytes * @return the cumulativeWritingBytes
*/ */
public long getCumulativeWritingBytes() { public long getCumulativeWrittenBytes() {
return this.cumulativeWritingBytes.get(); return cumulativeWrittenBytes.get();
} }
/** /**
* @return the cumulativeReadingBytes * @return the cumulativeReadingBytes
*/ */
public long getCumulativeReadingBytes() { public long getCumulativeReadBytes() {
return this.cumulativeReadingBytes.get(); return cumulativeReadBytes.get();
} }
/** /**
@ -588,11 +588,11 @@ public class TrafficCounter {
*/ */
@Override @Override
public String toString() { public String toString() {
return "Monitor " + this.name + " Current Speed Read: " + return "Monitor " + name + " Current Speed Read: " +
(this.lastReadingThroughput >> 10) + " KB/s, Write: " + (lastReadingThroughput >> 10) + " KB/s, Write: " +
(this.lastWritingThroughput >> 10) + " KB/s Current Read: " + (lastWritingThroughput >> 10) + " KB/s Current Read: " +
(this.currentReadingBytes.get() >> 10) + " KB Current Write: " + (currentReadingBytes.get() >> 10) + " KB Current Write: " +
(this.currentWritingBytes.get() >> 10) + " KB"; (currentWritingBytes.get() >> 10) + " KB";
} }
} }