HBASE-18601 Update Htrace to 4.2 (addendum)

- TraceTree fix

Signed-off-by: Chia-Ping Tsai <chia7712@gmail.com>
This commit is contained in:
Balazs Meszaros 2017-11-27 16:53:25 +01:00 committed by Chia-Ping Tsai
parent 5c1acf4792
commit 7c1c370f2f
2 changed files with 33 additions and 21 deletions

View File

@ -31,7 +31,6 @@ import org.apache.hadoop.hbase.testclassification.MiscTests;
import org.apache.htrace.core.POJOSpanReceiver;
import org.apache.htrace.core.Sampler;
import org.apache.htrace.core.Span;
import org.apache.htrace.core.SpanId;
import org.apache.htrace.core.TraceScope;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@ -50,7 +49,6 @@ public class TestHTraceHooks {
private static final byte[] FAMILY_BYTES = "family".getBytes();
private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
private static POJOSpanReceiver rcvr;
private static SpanId ROOT_SPAN_ID = new SpanId(0, 0);
@Rule
public TestName name = new TestName();

View File

@ -22,12 +22,12 @@ import org.apache.htrace.core.SpanId;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/**
* Used to create the graph formed by spans.
@ -35,18 +35,23 @@ import java.util.Set;
public class TraceTree {
public static class SpansByParent {
private final Set<Span> set;
private static Comparator<Span> COMPARATOR =
new Comparator<Span>() {
@Override
public int compare(Span a, Span b) {
return a.getSpanId().compareTo(b.getSpanId());
}
};
private final TreeSet<Span> treeSet;
private final HashMap<SpanId, LinkedList<Span>> parentToSpans;
SpansByParent(Collection<Span> spans) {
set = new LinkedHashSet<Span>();
TreeSet<Span> treeSet = new TreeSet<Span>(COMPARATOR);
parentToSpans = new HashMap<SpanId, LinkedList<Span>>();
if(spans == null) {
return;
}
for (Span span : spans) {
set.add(span);
treeSet.add(span);
for (SpanId parent : span.getParents()) {
LinkedList<Span> list = parentToSpans.get(parent);
if (list == null) {
@ -56,15 +61,15 @@ public class TraceTree {
list.add(span);
}
if (span.getParents().length == 0) {
LinkedList<Span> list = parentToSpans.get(Long.valueOf(0L));
LinkedList<Span> list = parentToSpans.get(SpanId.INVALID);
if (list == null) {
list = new LinkedList<Span>();
parentToSpans.put(new SpanId(Long.MIN_VALUE, Long.MIN_VALUE), list);
parentToSpans.put(SpanId.INVALID, list);
}
list.add(span);
}
}
this.treeSet = treeSet;
}
public List<Span> find(SpanId parentId) {
@ -76,25 +81,31 @@ public class TraceTree {
}
public Iterator<Span> iterator() {
return Collections.unmodifiableSet(set).iterator();
return Collections.unmodifiableSortedSet(treeSet).iterator();
}
}
public static class SpansByProcessId {
private final Set<Span> set;
private static Comparator<Span> COMPARATOR =
new Comparator<Span>() {
@Override
public int compare(Span a, Span b) {
return a.getSpanId().compareTo(b.getSpanId());
}
};
private final TreeSet<Span> treeSet;
SpansByProcessId(Collection<Span> spans) {
set = new LinkedHashSet<Span>();
if(spans == null) {
return;
}
TreeSet<Span> treeSet = new TreeSet<Span>(COMPARATOR);
for (Span span : spans) {
set.add(span);
treeSet.add(span);
}
this.treeSet = treeSet;
}
public Iterator<Span> iterator() {
return Collections.unmodifiableSet(set).iterator();
return Collections.unmodifiableSortedSet(treeSet).iterator();
}
}
@ -108,6 +119,9 @@ public class TraceTree {
* have at least one root span.
*/
public TraceTree(Collection<Span> spans) {
if (spans == null) {
spans = Collections.emptySet();
}
this.spansByParent = new SpansByParent(spans);
this.spansByProcessId = new SpansByProcessId(spans);
}