Skip to content

Commit

Permalink
Add a worker ID definer for use in logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwigway committed Jul 16, 2015
1 parent 8fea95a commit 334dbc7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.opentripplanner.analyst.cluster;

import ch.qos.logback.core.PropertyDefinerBase;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
Expand Down Expand Up @@ -54,6 +53,18 @@
*
*/
public class AnalystWorker implements Runnable {
/**
* worker ID - just a random ID so we can differentiate machines used for computation.
* Useful to isolate the logs from a particular machine, as well as to evaluate any
* variation in performance coming from variation in the performance of the underlying
* VMs.
*
* This needs to be static so the logger can access it; see the static member class
* WorkerIdDefiner. A side effect is that only one worker can run in a given JVM. It also
* needs to be defined before the logger is defined, so that it is initialized before the
* logger is.
*/
public static final String machineId = UUID.randomUUID().toString().replaceAll("-", "");

private static final Logger LOG = LoggerFactory.getLogger(AnalystWorker.class);

Expand All @@ -72,8 +83,6 @@ public class AnalystWorker implements Runnable {

String BROKER_BASE_URL = "http://localhost:9001";

String s3Prefix = "analyst-dev";

static final HttpClient httpClient;

/** Cache RAPTOR data by Job ID */
Expand All @@ -96,9 +105,6 @@ public class AnalystWorker implements Runnable {
.build();
}

private final String workerId = UUID.randomUUID().toString().replace("-", ""); // a unique identifier for each worker so the broker can catalog them


// Of course this will eventually need to be shared between multiple AnalystWorker threads.
ClusterGraphBuilder clusterGraphBuilder;

Expand All @@ -117,17 +123,6 @@ public class AnalystWorker implements Runnable {
/** aws instance type, or null if not running on AWS */
private String instanceType;

/**
* worker ID - just a random ID so we can differentiate machines used for computation.
* Useful to isolate the logs from a particular machine, as well as to evaluate any
* variation in performance coming from variation in the performance of the underlying
* VMs.
*
* This needs to be static so the logger can access it; see the static member class
* WorkerIdDefiner. A side effect is that only one worker can run in a given JVM.
*/
public static final String machineId = UUID.randomUUID().toString().replaceAll("-", "");

boolean isSinglePoint = false;

public AnalystWorker(Properties config) {
Expand Down Expand Up @@ -362,7 +357,7 @@ public List<AnalystClusterRequest> getSomeWork() {
// Run a POST request (long-polling for work) indicating which graph this worker prefers to work on
String url = BROKER_BASE_URL + "/dequeue/" + graphId;
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(new BasicHeader(WORKER_ID_HEADER, workerId));
httpPost.setHeader(new BasicHeader(WORKER_ID_HEADER, machineId));
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
Expand Down Expand Up @@ -502,15 +497,4 @@ public static void main(String[] args) {

new AnalystWorker(config).run();
}

/**
* A class that allows the logging framework to access the worker ID; with a custom logback config
* this can be used to print the machine ID with each log message. This is useful if you have multiple
* workers logging to the same log aggregation service.
*/
public static class WorkerIdDefiner extends PropertyDefinerBase {
@Override public String getPropertyValue() {
return AnalystWorker.machineId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.opentripplanner.analyst.cluster;

import ch.qos.logback.core.PropertyDefinerBase;

/**
* A class that allows the logging framework to access the worker ID; with a custom logback config
* this can be used to print the machine ID with each log message. This is useful if you have multiple
* workers logging to the same log aggregation service.
*
* This does seem like it should be a static class of AnalystWorker, but AnalystWorker needs this
* class loaded to initialize its logger which is a static field, so it would have to be at the top
* of the file, above the logger definition, which is ugly and confusing so we leave it as its own
* bona fide class.
*
* It would seem that Mapped Diagnostic Contexts would be ideal for this purpose, but they are
* thread-scoped, and computation takes place in multiple threads; we need this to be JVM-scoped.
*/
public class WorkerIdDefiner extends PropertyDefinerBase {
@Override public String getPropertyValue() {
return AnalystWorker.machineId;
}
}

0 comments on commit 334dbc7

Please sign in to comment.