Skip to content

Commit

Permalink
fix NPE if stop without start (#914)
Browse files Browse the repository at this point in the history
Refactor the condition for the stop method to avoid
all shutdown actions if the registry hasn't been
started. Before it could result in an NPE because
the thread pool for the publisher would be null.
  • Loading branch information
brharrington authored Sep 14, 2021
1 parent 523a3f7 commit 8ca72d6
Showing 1 changed file with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,42 +189,42 @@ public void start() {
* Stop the scheduler reporting Atlas data.
*/
public void stop() {
// Shutdown backround tasks to collect data
if (scheduler != null) {
if (scheduler == null) {
logger.warn("registry stopped, but was never started");
} else {
// Shutdown background tasks to collect data
scheduler.shutdown();
scheduler = null;
logger.info("stopped collecting metrics every {}ms reporting to {}", step, uri);
} else {
logger.warn("registry stopped, but was never started");
}

// Flush data to Atlas
try {
// Get current time at start to avoid drift while flushing
OverridableClock overridableClock = (OverridableClock) clock();
long now = clock().wallTime();
overridableClock.setWallTime(now); // used set time rather than underlying clock

// Data for the previous interval may not have already been written, go ahead and
// try to write it out
logger.info("flushing data for previous interval to Atlas");
sendToAtlas();

// Move to end of next interval and ensure it gets written out
logger.info("flushing data for final interval to Atlas");
overridableClock.setWallTime(now / lwcStepMillis * lwcStepMillis + lwcStepMillis);
pollMeters(overridableClock.wallTime());
overridableClock.setWallTime(now / stepMillis * stepMillis + stepMillis);
sendToAtlas();
} catch (Exception e) {
logger.warn("failed to flush data to Atlas", e);
}
// Flush data to Atlas
try {
// Get current time at start to avoid drift while flushing
OverridableClock overridableClock = (OverridableClock) clock();
long now = clock().wallTime();
overridableClock.setWallTime(now); // use set time rather than underlying clock

// Data for the previous interval may not have already been written, go ahead and
// try to write it out
logger.info("flushing data for previous interval to Atlas");
sendToAtlas();

// Move to end of next interval and ensure it gets written out
logger.info("flushing data for final interval to Atlas");
overridableClock.setWallTime(now / lwcStepMillis * lwcStepMillis + lwcStepMillis);
pollMeters(overridableClock.wallTime());
overridableClock.setWallTime(now / stepMillis * stepMillis + stepMillis);
sendToAtlas();
} catch (Exception e) {
logger.warn("failed to flush data to Atlas", e);
}

// Shutdown publisher used for sending metrics
try {
publisher.close();
} catch (Exception e) {
logger.debug("failed to cleanly shutdown publisher");
// Shutdown publisher used for sending metrics
try {
publisher.close();
} catch (Exception e) {
logger.debug("failed to cleanly shutdown publisher");
}
}
}

Expand Down

0 comments on commit 8ca72d6

Please sign in to comment.