Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Creation of Utils class for optimize the use of functions. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/com/yahoo/viper/CheckTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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));
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/yahoo/viper/ExceptionLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)) {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/yahoo/viper/HostInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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());
}
}
14 changes: 8 additions & 6 deletions src/main/java/com/yahoo/viper/HostMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<HostInfo> hinfos;
Expand Down Expand Up @@ -73,7 +75,7 @@ public HostMonitor(String name, List<HostInfo> 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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/yahoo/viper/util/Utils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 4 additions & 2 deletions src/test/java/com/yahoo/viper/HostMonitorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down