mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
Improve service handling and add unit tests for threading behavior in MetricExporter
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=130790170
This commit is contained in:
parent
6915e35800
commit
b6eaba08eb
2 changed files with 56 additions and 16 deletions
|
@ -14,13 +14,18 @@
|
|||
|
||||
package google.registry.monitoring.metrics;
|
||||
|
||||
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
|
||||
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.util.concurrent.AbstractExecutionThreadService;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.BlockingQueue;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
|
@ -49,11 +54,18 @@ class MetricExporter extends AbstractExecutionThreadService {
|
|||
logger.info("Started up MetricExporter");
|
||||
while (isRunning()) {
|
||||
Optional<ImmutableList<MetricPoint<?>>> batch = writeQueue.take();
|
||||
logger.info("Got a batch of points from the writeQueue");
|
||||
if (batch.isPresent()) {
|
||||
for (MetricPoint<?> point : batch.get()) {
|
||||
writer.write(point);
|
||||
logger.info("Batch contains data, writing to MetricWriter");
|
||||
try {
|
||||
for (MetricPoint<?> point : batch.get()) {
|
||||
writer.write(point);
|
||||
}
|
||||
writer.flush();
|
||||
} catch (IOException exception) {
|
||||
logger.log(
|
||||
Level.SEVERE, "Threw an exception while writing or flushing metrics", exception);
|
||||
}
|
||||
writer.flush();
|
||||
} else {
|
||||
logger.info("Received a poison pill, stopping now");
|
||||
// An absent optional indicates that the Reporter wants this service to shut down.
|
||||
|
@ -64,6 +76,21 @@ class MetricExporter extends AbstractExecutionThreadService {
|
|||
|
||||
@Override
|
||||
protected Executor executor() {
|
||||
return Executors.newSingleThreadExecutor(threadFactory);
|
||||
final ExecutorService executor = Executors.newSingleThreadExecutor(threadFactory);
|
||||
// Make sure the ExecutorService terminates when this service does.
|
||||
addListener(
|
||||
new Listener() {
|
||||
@Override
|
||||
public void terminated(State from) {
|
||||
executor.shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void failed(State from, Throwable failure) {
|
||||
executor.shutdown();
|
||||
}
|
||||
},
|
||||
directExecutor());
|
||||
return executor;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue