From 7c1c370f2fcde83b702011046a8b6fb0b01a263e Mon Sep 17 00:00:00 2001 From: Balazs Meszaros Date: Mon, 27 Nov 2017 16:53:25 +0100 Subject: [PATCH] HBASE-18601 Update Htrace to 4.2 (addendum) - TraceTree fix Signed-off-by: Chia-Ping Tsai --- .../hadoop/hbase/trace/TestHTraceHooks.java | 2 - .../apache/hadoop/hbase/trace/TraceTree.java | 52 ++++++++++++------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/trace/TestHTraceHooks.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/trace/TestHTraceHooks.java index 631d98bad49..0b8c03934a4 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/trace/TestHTraceHooks.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/trace/TestHTraceHooks.java @@ -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(); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/trace/TraceTree.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/trace/TraceTree.java index bba4ee5f64b..eb209d0ee15 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/trace/TraceTree.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/trace/TraceTree.java @@ -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 set; + private static Comparator COMPARATOR = + new Comparator() { + @Override + public int compare(Span a, Span b) { + return a.getSpanId().compareTo(b.getSpanId()); + } + }; + + private final TreeSet treeSet; private final HashMap> parentToSpans; SpansByParent(Collection spans) { - set = new LinkedHashSet(); + TreeSet treeSet = new TreeSet(COMPARATOR); parentToSpans = new HashMap>(); - if(spans == null) { - return; - } for (Span span : spans) { - set.add(span); + treeSet.add(span); for (SpanId parent : span.getParents()) { LinkedList list = parentToSpans.get(parent); if (list == null) { @@ -56,15 +61,15 @@ public class TraceTree { list.add(span); } if (span.getParents().length == 0) { - LinkedList list = parentToSpans.get(Long.valueOf(0L)); + LinkedList list = parentToSpans.get(SpanId.INVALID); if (list == null) { list = new LinkedList(); - parentToSpans.put(new SpanId(Long.MIN_VALUE, Long.MIN_VALUE), list); + parentToSpans.put(SpanId.INVALID, list); } list.add(span); } } - + this.treeSet = treeSet; } public List find(SpanId parentId) { @@ -76,25 +81,31 @@ public class TraceTree { } public Iterator iterator() { - return Collections.unmodifiableSet(set).iterator(); + return Collections.unmodifiableSortedSet(treeSet).iterator(); } } public static class SpansByProcessId { - private final Set set; + private static Comparator COMPARATOR = + new Comparator() { + @Override + public int compare(Span a, Span b) { + return a.getSpanId().compareTo(b.getSpanId()); + } + }; + + private final TreeSet treeSet; SpansByProcessId(Collection spans) { - set = new LinkedHashSet(); - if(spans == null) { - return; - } + TreeSet treeSet = new TreeSet(COMPARATOR); for (Span span : spans) { - set.add(span); + treeSet.add(span); } + this.treeSet = treeSet; } public Iterator 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 spans) { + if (spans == null) { + spans = Collections.emptySet(); + } this.spansByParent = new SpansByParent(spans); this.spansByProcessId = new SpansByProcessId(spans); }