in wrapper/src/main/java/software/amazon/jdbc/plugin/strategy/fastestresponse/NodeResponseTimeMonitor.java [129:197]
public void run() {
TelemetryContext telemetryContext = telemetryFactory.openTelemetryContext(
"node response time thread", TelemetryTraceLevel.TOP_LEVEL);
telemetryContext.setAttribute("url", hostSpec.getUrl());
try {
while (!this.stopped.get()) {
this.openConnection();
if (this.monitoringConn != null) {
long responseTimeSum = 0;
int count = 0;
for (int i = 0; i < NUM_OF_MEASURES; i++) {
if (this.stopped.get()) {
break;
}
long startTime = this.getCurrentTime();
if (this.pluginService.getTargetDriverDialect().ping(this.monitoringConn)) {
long responseTime = this.getCurrentTime() - startTime;
responseTimeSum += responseTime;
count++;
}
}
if (count > 0) {
this.responseTime.set((int) TimeUnit.NANOSECONDS.toMillis(responseTimeSum / count));
} else {
this.responseTime.set(Integer.MAX_VALUE);
}
this.checkTimestamp.set(this.getCurrentTime());
LOGGER.finest(() -> Messages.get(
"NodeResponseTimeMonitor.responseTime",
new Object[] {this.hostSpec.getHost(), this.responseTime.get()}));
}
TimeUnit.MILLISECONDS.sleep(this.intervalMs);
}
} catch (final InterruptedException intEx) {
// exit thread
LOGGER.finest(
() -> Messages.get(
"NodeResponseTimeMonitor.interruptedExceptionDuringMonitoring",
new Object[] {this.hostSpec.getHost()}));
} catch (final Exception ex) {
// this should not be reached; log and exit thread
if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.log(
Level.FINEST,
Messages.get(
"NodeResponseTimeMonitor.exceptionDuringMonitoringStop",
new Object[]{this.hostSpec.getHost()}),
ex); // We want to print full trace stack of the exception.
}
} finally {
this.stopped.set(true);
if (this.monitoringConn != null) {
try {
this.monitoringConn.close();
} catch (final SQLException ex) {
// ignore
}
}
if (telemetryContext != null) {
telemetryContext.closeContext();
}
}
}