提交 674845c1 编写于 作者: K kohsuke

modified to attempt an access 5 times before aborting the system.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@12251 71c3de6d-444a-0410-be80-ed276b4c234a
上级 19b3202a
......@@ -20,43 +20,41 @@ import java.util.logging.Logger;
* @author Kohsuke Kawaguchi
*/
public class ResponseTimeMonitor extends NodeMonitor {
/**
* Returns the HTML representation of the result.
*/
public String toHtml(long ms) {
if(ms<0)
return Util.wrapToErrorSpan("Time out");
return ms+"ms";
}
public AbstractNodeMonitorDescriptor getDescriptor() {
return DESCRIPTOR;
}
public static final AbstractNodeMonitorDescriptor<Long> DESCRIPTOR = new AbstractNodeMonitorDescriptor<Long>(ResponseTimeMonitor.class) {
protected Long monitor(Computer c) throws IOException, InterruptedException {
public static final AbstractNodeMonitorDescriptor<Data> DESCRIPTOR = new AbstractNodeMonitorDescriptor<Data>(ResponseTimeMonitor.class) {
protected Data monitor(Computer c) throws IOException, InterruptedException {
Data old = get(c);
Data d;
long start = System.nanoTime();
Future<String> f = c.getChannel().callAsync(new NoopTask());
try {
f.get(5, TimeUnit.SECONDS);
f.get(TIMEOUT, TimeUnit.MILLISECONDS);
long end = System.nanoTime();
return TimeUnit2.NANOSECONDS.toMillis(end-start);
d = new Data(old,TimeUnit2.NANOSECONDS.toMillis(end-start));
} catch (ExecutionException e) {
throw new IOException(e.getCause()); // I don't think this is possible
} catch (TimeoutException e) {
// special constant to indicate that the processing timed out.
d = new Data(old,-1L);
}
if(d.hasTooManyTimeouts()) {
// TODO: this scheme should be generalized, so that Hudson can remember why it's marking the node
// as offline, as well as allowing the user to force Hudson to use it.
if(!c.isTemporarilyOffline()) {
LOGGER.warning("Making "+c.getName()+" offline temporarily because it's not responding");
LOGGER.warning(Messages.ResponseTimeMonitor_MarkedOffline(c.getName()));
c.setTemporarilyOffline(true);
}
// special constant to indicate that the processing timed out.
return -1L;
}
return d;
}
public String getDisplayName() {
return "Response Time";
return Messages.ResponseTimeMonitor_DisplayName();
}
public NodeMonitor newInstance(StaplerRequest req, JSONObject formData) throws FormException {
......@@ -64,6 +62,70 @@ public class ResponseTimeMonitor extends NodeMonitor {
}
};
/**
* Immutable representation of the monitoring data.
*/
public static final class Data {
/**
* Record of the past 5 times. -1 if time out. Otherwise in milliseconds.
* Old ones first.
*/
private final long[] past5;
private Data(Data old, long newDataPoint) {
if(old==null)
past5 = new long[] {newDataPoint};
else {
past5 = new long[Math.min(5,old.past5.length+1)];
int copyLen = past5.length - 1;
System.arraycopy(old.past5, old.past5.length-copyLen, this.past5, 0, copyLen);
past5[past5.length-1] = newDataPoint;
}
}
/**
* Computes the recurrence of the time out
*/
private int failureCount() {
int cnt=0;
for(int i=past5.length-1; i>=0 && past5[i]<0; i--, cnt++)
;
return cnt;
}
/**
* Computes the average response time, by taking the time out into account.
*/
public long average() {
long total=0;
for (long l : past5) {
if(l<0) total += TIMEOUT;
else total += l;
}
return total/past5.length;
}
public boolean hasTooManyTimeouts() {
return failureCount()>=5;
}
/**
* HTML rendering of the data
*/
public String toString() {
// StringBuilder buf = new StringBuilder();
// for (long l : past5) {
// if(buf.length()>0) buf.append(',');
// buf.append(l);
// }
// return buf.toString();
int fc = failureCount();
if(fc>0)
return Util.wrapToErrorSpan(Messages.ResponseTimeMonitor_TimeOut(fc));
return average()+"ms";
}
}
private static class NoopTask implements Callable<String,RuntimeException> {
public String call() {
return null;
......@@ -76,5 +138,10 @@ public class ResponseTimeMonitor extends NodeMonitor {
LIST.add(DESCRIPTOR);
}
/**
* Time out interval in milliseconds.
*/
private static final long TIMEOUT = 5000;
private static final Logger LOGGER = Logger.getLogger(ResponseTimeMonitor.class.getName());
}
......@@ -4,7 +4,7 @@
<td align="right" data="-2">N/A</td>
</j:when>
<j:otherwise>
<td align="right" data="${data}">${from.toHtml(data)}</td>
<td align="right" data="${data.average()}">${data}</td>
</j:otherwise>
</j:choose>
</j:jelly>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册