From 83ba7893ec05696af12f302c7a487cc03bcb1dff Mon Sep 17 00:00:00 2001 From: Marcos Oliveira Date: Tue, 28 Mar 2017 15:04:04 -0300 Subject: [PATCH] Creation of Utils class for optimize the use of functions. --- src/main/java/com/yahoo/viper/CheckTask.java | 6 ++++-- src/main/java/com/yahoo/viper/ExceptionLogger.java | 4 +++- src/main/java/com/yahoo/viper/HostInfo.java | 10 ++++++---- src/main/java/com/yahoo/viper/HostMonitor.java | 14 ++++++++------ src/main/java/com/yahoo/viper/util/Utils.java | 10 ++++++++++ src/test/java/com/yahoo/viper/HostMonitorTest.java | 6 ++++-- 6 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/yahoo/viper/util/Utils.java diff --git a/src/main/java/com/yahoo/viper/CheckTask.java b/src/main/java/com/yahoo/viper/CheckTask.java index 5186919..5f4174c 100644 --- a/src/main/java/com/yahoo/viper/CheckTask.java +++ b/src/main/java/com/yahoo/viper/CheckTask.java @@ -9,6 +9,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.yahoo.viper.util.Utils; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -88,7 +90,7 @@ private void doRun() throws Exception { HttpURLConnection http = null; try { // Start checking - hinfo.lastCheck = System.currentTimeMillis(); + hinfo.lastCheck = Utils.getActualTime(); if (hinfo.url == null) { // Check host and port @@ -108,7 +110,7 @@ private void doRun() throws Exception { } // Success - hinfo.lastLive = System.currentTimeMillis(); + hinfo.lastLive = Utils.getActualTime(); if (!hinfo.live) { hinfo.logger.info(String.format("[%s] %s is now live", monitor.name, hinfo.url == null ? hinfo.socketAddress : hinfo.url)); diff --git a/src/main/java/com/yahoo/viper/ExceptionLogger.java b/src/main/java/com/yahoo/viper/ExceptionLogger.java index 87f6668..5b8c733 100644 --- a/src/main/java/com/yahoo/viper/ExceptionLogger.java +++ b/src/main/java/com/yahoo/viper/ExceptionLogger.java @@ -9,6 +9,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.yahoo.viper.util.Utils; + /** * A smarter error logger for exceptions generated by background threads. * The logger helps to minimize the noise in the error logs by condensing continuous errors. @@ -57,7 +59,7 @@ public void info(String msg) { } private void log(Level newLevel, String newMsg, Throwable e) { - long now = System.currentTimeMillis(); + long now = Utils.getActualTime(); newMsg = newMsg == null ? "null" : newMsg; if (newMsg.equals(oldMsg)) { diff --git a/src/main/java/com/yahoo/viper/HostInfo.java b/src/main/java/com/yahoo/viper/HostInfo.java index 45918a9..207011b 100644 --- a/src/main/java/com/yahoo/viper/HostInfo.java +++ b/src/main/java/com/yahoo/viper/HostInfo.java @@ -9,6 +9,8 @@ import java.net.*; import java.security.InvalidParameterException; +import com.yahoo.viper.util.Utils; + /** * This class holds information about each registered host. */ @@ -77,7 +79,7 @@ public HostInfo(String name, int port) throws UnknownHostException { */ public boolean isLive() { if (live && checkTask.isChecking()) { - long time = System.currentTimeMillis() - lastLive(); + long time = Utils.getActualTime() - lastLive(); // The threshold is increased by one check period to allow time for the check itself if (time > (checkTask.monitor.retries + 2) * checkTask.monitor.checkPeriodMs) { live = false; @@ -95,7 +97,7 @@ public boolean isLive() { * @return true if the host is hung. */ public boolean isHung() { - return System.currentTimeMillis() - lastCheck() > (checkTask.monitor.retries + 2) * checkTask.monitor.checkPeriodMs; + return Utils.getActualTime() - lastCheck() > (checkTask.monitor.retries + 2) * checkTask.monitor.checkPeriodMs; } /** @@ -140,7 +142,7 @@ public InetSocketAddress socketAddress() { public String toString() { return String.format("HostInfo[%s, live=%b, lastLive=%dms, lastCheck=%dms]", url == null ? socketAddress : url, isLive(), - lastLive == 0 ? 0 : lastLive - System.currentTimeMillis(), - lastCheck - System.currentTimeMillis()); + lastLive == 0 ? 0 : lastLive - Utils.getActualTime(), + lastCheck - Utils.getActualTime()); } } diff --git a/src/main/java/com/yahoo/viper/HostMonitor.java b/src/main/java/com/yahoo/viper/HostMonitor.java index 6e395da..ef83529 100644 --- a/src/main/java/com/yahoo/viper/HostMonitor.java +++ b/src/main/java/com/yahoo/viper/HostMonitor.java @@ -9,6 +9,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.yahoo.viper.util.Utils; + import java.util.*; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -27,7 +29,7 @@ public class HostMonitor { final private ExecutorService checkerPool; // This value is used to prevent false errors during the start up of this instance - final private long startTime = System.currentTimeMillis(); + final private long startTime = Utils.getActualTime(); // Used by other classes in this package final private List hinfos; @@ -73,7 +75,7 @@ public HostMonitor(String name, List hinfos, LoadBalancingPolicy loadB liveCount = 0; // Create the check tasks - long now = System.currentTimeMillis(); + long now = Utils.getActualTime(); for (HostInfo hi : hinfos) { hi.checkTask = new CheckTask(this, hi); hi.lastCheck = hi.lastLive = 0; @@ -131,7 +133,7 @@ public HostInfo liveHost() { HostInfo hi = null; // If no live hosts are found and this instance was just created, try again try { - while ((hi = liveHost2()) == null && System.currentTimeMillis() - startTime < 2 * checkPeriodMs) { + while ((hi = liveHost2()) == null && Utils.getActualTime() - startTime < 2 * checkPeriodMs) { Thread.sleep(checkPeriodMs); } } catch (InterruptedException e) { @@ -176,7 +178,7 @@ private HostInfo liveHost2() throws InterruptedException { * If one is determined to be hung, it is interrupted. */ class BgThread extends Thread { - long now = System.currentTimeMillis(); + long now = Utils.getActualTime(); long lastInfo = now; public void run() { @@ -188,7 +190,7 @@ public void run() { StringBuilder sb = new StringBuilder(); while (runBgThread) { try { - long now = System.currentTimeMillis(); + long now = Utils.getActualTime(); int lives = 0; // Start another round of checks and tally the live hosts @@ -217,7 +219,7 @@ public void run() { // Update instance values liveCount = lives; - now = System.currentTimeMillis(); + now = Utils.getActualTime(); if (lives != lastLives || lastNumListeners != listeners.size() || now - lastInfo > 60000) { HostMonitorEvent event = new HostMonitorEvent(); event.numLiveHosts = lives; diff --git a/src/main/java/com/yahoo/viper/util/Utils.java b/src/main/java/com/yahoo/viper/util/Utils.java new file mode 100644 index 0000000..866f50a --- /dev/null +++ b/src/main/java/com/yahoo/viper/util/Utils.java @@ -0,0 +1,10 @@ +package com.yahoo.viper.util; + +public class Utils { + private static long currentTime; + + public static long getActualTime() { + currentTime = System.currentTimeMillis(); + return currentTime; + } +} \ No newline at end of file diff --git a/src/test/java/com/yahoo/viper/HostMonitorTest.java b/src/test/java/com/yahoo/viper/HostMonitorTest.java index 987dd46..63016af 100644 --- a/src/test/java/com/yahoo/viper/HostMonitorTest.java +++ b/src/test/java/com/yahoo/viper/HostMonitorTest.java @@ -7,6 +7,8 @@ package com.yahoo.viper; import com.yahoo.viper.cli.MockServer; +import com.yahoo.viper.util.Utils; + import org.testng.Assert; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeMethod; @@ -68,9 +70,9 @@ public HostMonitor createMonitor(LoadBalancingPolicy policy, int checkPeriodMs) } // Wait until all three servers are live. Up to 5 seconds. - long start = System.currentTimeMillis(); + long start = Utils.getActualTime(); int count = 0; - while (count < liveCount && System.currentTimeMillis() - start < 10 * checkPeriodMs) { + while (count < liveCount && Utils.getActualTime() - start < 10 * checkPeriodMs) { count = 0; for (HostInfo hi : hinfos) { if (hi.isLive()) {