diff --git a/server/src/com/cloud/agent/manager/AgentMonitor.java b/server/src/com/cloud/agent/manager/AgentMonitor.java index 79c716f8e55..4e522a82e70 100755 --- a/server/src/com/cloud/agent/manager/AgentMonitor.java +++ b/server/src/com/cloud/agent/manager/AgentMonitor.java @@ -187,6 +187,10 @@ public class AgentMonitor extends Thread implements Listener { } } + if (agentsBehind.size() > 0) { + s_logger.info("Found the following agents behind on ping: " + agentsBehind); + } + return agentsBehind; } diff --git a/utils/src/com/cloud/utils/time/InaccurateClock.java b/utils/src/com/cloud/utils/time/InaccurateClock.java index d9b2ad7aca6..955085a3869 100644 --- a/utils/src/com/cloud/utils/time/InaccurateClock.java +++ b/utils/src/com/cloud/utils/time/InaccurateClock.java @@ -17,44 +17,76 @@ */ package com.cloud.utils.time; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import javax.management.StandardMBean; + +import org.apache.log4j.Logger; + +import com.cloud.utils.concurrency.NamedThreadFactory; +import com.cloud.utils.mgmt.JmxUtil; + /** - * This clock is only accurate at a second basis. - * + * This clock is only accurate at a second basis, which is useful for most applications. */ -public class InaccurateClock extends Thread { - static final InaccurateClock s_timer = new InaccurateClock(); - static { - time = System.currentTimeMillis(); - s_timer.start(); - } - - private static long time; - - protected InaccurateClock() { - super("InaccurateClock"); - } - - @Override - public void run() { - while (true) { +public class InaccurateClock extends StandardMBean implements InaccurateClockMBean { + private static final Logger s_logger = Logger.getLogger(InaccurateClock.class); + static ScheduledExecutorService s_executor = null; + static final InaccurateClock s_timer = new InaccurateClock(); + private static long time; + + public InaccurateClock() { + super(InaccurateClockMBean.class, false); + time = System.currentTimeMillis(); + restart(); + try { + JmxUtil.registerMBean("InaccurateClock", "InaccurateClock", this); + } catch (Exception e) { + s_logger.warn("Unable to initialize inaccurate clock", e); + } + } + + @Override + public synchronized String restart() { + turnOff(); + s_executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("InaccurateClock")); + s_executor.schedule(new SetTimeTask(), 60, TimeUnit.SECONDS); + return "Restarted"; + } + + @Override public String turnOff() { + if (s_executor != null) { + try { + s_executor.shutdown(); + } catch (Throwable th) { + s_logger.error("Unable to shutdown the Executor", th); + return "Unable to turn off check logs"; + } + } + s_executor = null; + return "Off"; + } + + public static long getTime() { + return s_executor != null ? time : System.currentTimeMillis(); + } + + public static long getTimeInSeconds() { + return time / 1000; + } + + protected class SetTimeTask implements Runnable { + @Override public void run() { try { time = System.currentTimeMillis(); - Thread.sleep(1000); - } catch(Exception e) { - } - } - } - - public static long getTime() { - if (s_timer.isAlive()) { - return time; - } else { - return System.currentTimeMillis(); - } - } - - public static long getTimeInSeconds() { - // This is obviously not accurate because it >> 10 is / 1024 but it's close enough since we're inaccurate. - return getTime() >> 10; - } + } catch (Throwable th) { + try { + s_logger.error("Unable to time", th); + } catch (Throwable th2) { + } + } + } + } }