Skip to content
Snippets Groups Projects
Commit deaeafa7 authored by Julian Cerruti's avatar Julian Cerruti Committed by GitHub
Browse files

Merge pull request #244 from jubeira/fix/ntp_provider_logging

Avoid flooding log with error messages on NTP sync failure
parents c746aed1 8c895e48
No related branches found
No related tags found
No related merge requests found
...@@ -44,7 +44,7 @@ public class NtpTimeProvider implements TimeProvider { ...@@ -44,7 +44,7 @@ public class NtpTimeProvider implements TimeProvider {
private static final boolean DEBUG = false; private static final boolean DEBUG = false;
private static final Log log = LogFactory.getLog(NtpTimeProvider.class); private static final Log log = LogFactory.getLog(NtpTimeProvider.class);
private static final int SAMPLE_SIZE = 11; private int sampleSize = 11;
private final InetAddress host; private final InetAddress host;
private final ScheduledExecutorService scheduledExecutorService; private final ScheduledExecutorService scheduledExecutorService;
...@@ -76,11 +76,11 @@ public class NtpTimeProvider implements TimeProvider { ...@@ -76,11 +76,11 @@ public class NtpTimeProvider implements TimeProvider {
public void updateTime() throws IOException { public void updateTime() throws IOException {
List<Long> offsets = Lists.newArrayList(); List<Long> offsets = Lists.newArrayList();
int failures = 0; int failures = 0;
for (int i = 0; i < SAMPLE_SIZE; i++) { for (int i = 0; i < sampleSize; i++) {
try { try {
offsets.add(computeOffset()); offsets.add(computeOffset());
} catch (IOException e) { } catch (IOException e) {
if ( ++failures > SAMPLE_SIZE/2 ) { if (++failures > sampleSize / 2) {
throw e; throw e;
} }
} }
...@@ -97,7 +97,9 @@ public class NtpTimeProvider implements TimeProvider { ...@@ -97,7 +97,9 @@ public class NtpTimeProvider implements TimeProvider {
try { try {
time = ntpClient.getTime(host); time = ntpClient.getTime(host);
} catch (IOException e) { } catch (IOException e) {
if (DEBUG) {
log.error("Failed to read time from NTP server: " + host.getHostName(), e); log.error("Failed to read time from NTP server: " + host.getHostName(), e);
}
throw e; throw e;
} }
time.computeDetails(); time.computeDetails();
...@@ -147,4 +149,14 @@ public class NtpTimeProvider implements TimeProvider { ...@@ -147,4 +149,14 @@ public class NtpTimeProvider implements TimeProvider {
Time currentTime = wallTimeProvider.getCurrentTime(); Time currentTime = wallTimeProvider.getCurrentTime();
return currentTime.add(Duration.fromMillis(offset)); return currentTime.add(Duration.fromMillis(offset));
} }
/**
* Sets how many samples will be taken from the NTP server
* when calling {@link #updateTime()}.
* @param sampleSize Number of samples to take. It has to be > 0.
*/
public void setUpdateTimeSampleSize(int sampleSize) {
Preconditions.checkArgument(sampleSize > 0);
this.sampleSize = sampleSize;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment