|
@@ -25,6 +25,7 @@ import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.TreeMap;
|
|
@@ -52,41 +53,36 @@ import org.apache.htrace.Trace;
|
|
|
*/
|
|
|
@InterfaceAudience.Private
|
|
|
public class SpanReceiverHost implements TraceAdminProtocol {
|
|
|
- public static final String SPAN_RECEIVERS_CONF_KEY =
|
|
|
- "hadoop.htrace.spanreceiver.classes";
|
|
|
+ public static final String SPAN_RECEIVERS_CONF_SUFFIX =
|
|
|
+ "spanreceiver.classes";
|
|
|
private static final Log LOG = LogFactory.getLog(SpanReceiverHost.class);
|
|
|
+ private static final HashMap<String, SpanReceiverHost> hosts =
|
|
|
+ new HashMap<String, SpanReceiverHost>(1);
|
|
|
private final TreeMap<Long, SpanReceiver> receivers =
|
|
|
new TreeMap<Long, SpanReceiver>();
|
|
|
+ private final String confPrefix;
|
|
|
private Configuration config;
|
|
|
private boolean closed = false;
|
|
|
private long highestId = 1;
|
|
|
|
|
|
- private final static String LOCAL_FILE_SPAN_RECEIVER_PATH =
|
|
|
- "hadoop.htrace.local-file-span-receiver.path";
|
|
|
+ private final static String LOCAL_FILE_SPAN_RECEIVER_PATH_SUFFIX =
|
|
|
+ "local-file-span-receiver.path";
|
|
|
|
|
|
- private static enum SingletonHolder {
|
|
|
- INSTANCE;
|
|
|
- Object lock = new Object();
|
|
|
- SpanReceiverHost host = null;
|
|
|
- }
|
|
|
-
|
|
|
- public static SpanReceiverHost getInstance(Configuration conf) {
|
|
|
- if (SingletonHolder.INSTANCE.host != null) {
|
|
|
- return SingletonHolder.INSTANCE.host;
|
|
|
- }
|
|
|
- synchronized (SingletonHolder.INSTANCE.lock) {
|
|
|
- if (SingletonHolder.INSTANCE.host != null) {
|
|
|
- return SingletonHolder.INSTANCE.host;
|
|
|
+ public static SpanReceiverHost get(Configuration conf, String confPrefix) {
|
|
|
+ synchronized (SpanReceiverHost.class) {
|
|
|
+ SpanReceiverHost host = hosts.get(confPrefix);
|
|
|
+ if (host != null) {
|
|
|
+ return host;
|
|
|
}
|
|
|
- SpanReceiverHost host = new SpanReceiverHost();
|
|
|
- host.loadSpanReceivers(conf);
|
|
|
- SingletonHolder.INSTANCE.host = host;
|
|
|
+ final SpanReceiverHost newHost = new SpanReceiverHost(confPrefix);
|
|
|
+ newHost.loadSpanReceivers(conf);
|
|
|
ShutdownHookManager.get().addShutdownHook(new Runnable() {
|
|
|
public void run() {
|
|
|
- SingletonHolder.INSTANCE.host.closeReceivers();
|
|
|
+ newHost.closeReceivers();
|
|
|
}
|
|
|
}, 0);
|
|
|
- return SingletonHolder.INSTANCE.host;
|
|
|
+ hosts.put(confPrefix, newHost);
|
|
|
+ return newHost;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -119,6 +115,10 @@ public class SpanReceiverHost implements TraceAdminProtocol {
|
|
|
return new File(tmp, nonce).getAbsolutePath();
|
|
|
}
|
|
|
|
|
|
+ private SpanReceiverHost(String confPrefix) {
|
|
|
+ this.confPrefix = confPrefix;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Reads the names of classes specified in the
|
|
|
* "hadoop.htrace.spanreceiver.classes" property and instantiates and registers
|
|
@@ -131,22 +131,22 @@ public class SpanReceiverHost implements TraceAdminProtocol {
|
|
|
*/
|
|
|
public synchronized void loadSpanReceivers(Configuration conf) {
|
|
|
config = new Configuration(conf);
|
|
|
- String[] receiverNames =
|
|
|
- config.getTrimmedStrings(SPAN_RECEIVERS_CONF_KEY);
|
|
|
+ String receiverKey = confPrefix + SPAN_RECEIVERS_CONF_SUFFIX;
|
|
|
+ String[] receiverNames = config.getTrimmedStrings(receiverKey);
|
|
|
if (receiverNames == null || receiverNames.length == 0) {
|
|
|
if (LOG.isTraceEnabled()) {
|
|
|
- LOG.trace("No span receiver names found in " +
|
|
|
- SPAN_RECEIVERS_CONF_KEY + ".");
|
|
|
+ LOG.trace("No span receiver names found in " + receiverKey + ".");
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
// It's convenient to have each daemon log to a random trace file when
|
|
|
// testing.
|
|
|
- if (config.get(LOCAL_FILE_SPAN_RECEIVER_PATH) == null) {
|
|
|
+ String pathKey = confPrefix + LOCAL_FILE_SPAN_RECEIVER_PATH_SUFFIX;
|
|
|
+ if (config.get(pathKey) == null) {
|
|
|
String uniqueFile = getUniqueLocalTraceFileName();
|
|
|
- config.set(LOCAL_FILE_SPAN_RECEIVER_PATH, uniqueFile);
|
|
|
+ config.set(pathKey, uniqueFile);
|
|
|
if (LOG.isTraceEnabled()) {
|
|
|
- LOG.trace("Set " + LOCAL_FILE_SPAN_RECEIVER_PATH + " to " + uniqueFile);
|
|
|
+ LOG.trace("Set " + pathKey + " to " + uniqueFile);
|
|
|
}
|
|
|
}
|
|
|
for (String className : receiverNames) {
|
|
@@ -164,7 +164,8 @@ public class SpanReceiverHost implements TraceAdminProtocol {
|
|
|
private synchronized SpanReceiver loadInstance(String className,
|
|
|
List<ConfigurationPair> extraConfig) throws IOException {
|
|
|
SpanReceiverBuilder builder =
|
|
|
- new SpanReceiverBuilder(TraceUtils.wrapHadoopConf(config, extraConfig));
|
|
|
+ new SpanReceiverBuilder(TraceUtils.
|
|
|
+ wrapHadoopConf(confPrefix, config, extraConfig));
|
|
|
SpanReceiver rcvr = builder.spanReceiverClass(className.trim()).build();
|
|
|
if (rcvr == null) {
|
|
|
throw new IOException("Failed to load SpanReceiver " + className);
|