|
2 | 2 |
|
3 | 3 | import com.navercorp.pinpoint.common.hbase.HbaseColumnFamily; |
4 | 4 | import com.navercorp.pinpoint.common.hbase.HbaseTables; |
5 | | -import com.navercorp.pinpoint.common.server.util.pair.LongPair; |
6 | 5 | import org.apache.hadoop.hbase.CompareOperator; |
7 | 6 | import org.apache.hadoop.hbase.filter.BinaryComponentComparator; |
8 | 7 | import org.apache.hadoop.hbase.filter.BinaryPrefixComparator; |
|
17 | 16 | import java.util.List; |
18 | 17 |
|
19 | 18 | public class TraceIndexFilterBuilder { |
| 19 | + private static final long DEFAULT_ELAPSED_MIN = 0L; |
| 20 | + private static final long DEFAULT_ELAPSED_MAX = Integer.MAX_VALUE; |
| 21 | + private static final byte MIN_ELAPSED_BYTE = TraceIndexRowKeyUtils.toElapsedByte(DEFAULT_ELAPSED_MIN); |
| 22 | + |
20 | 23 | private static final HbaseColumnFamily INDEX = HbaseTables.TRACE_INDEX; |
21 | 24 | private static final HbaseColumnFamily META = HbaseTables.TRACE_INDEX_META; |
22 | 25 | private static final byte[] META_QUALIFIER_RPC = HbaseTables.TRACE_INDEX_META_QUALIFIER_RPC; |
23 | 26 | private static final int ROW_FILTER_OFFSET = TraceIndexRowKeyUtils.SALTED_ROW_TIMESTAMP_OFFSET + 8 + 8; |
24 | 27 |
|
25 | | - private LongPair elapsedMinMax; |
| 28 | + private long elapsedMin = DEFAULT_ELAPSED_MIN; |
| 29 | + private long elapsedMax = DEFAULT_ELAPSED_MAX; |
26 | 30 | private Boolean success; |
27 | 31 | private String agentId; |
28 | 32 | private String rpcRegex; |
29 | 33 |
|
30 | 34 | public TraceIndexFilterBuilder() { |
31 | 35 | } |
32 | 36 |
|
33 | | - public Filter build(boolean enableAdditionalRowFilters, boolean enableValueFilter) { |
| 37 | + public FilterList build(boolean enableAdditionalRowFilters, boolean enableValueFilter) { |
34 | 38 | List<Filter> filters = new ArrayList<>(); |
35 | 39 | // row filters |
36 | 40 | if (enableAdditionalRowFilters) { |
37 | | - if (success != null) { |
38 | | - filters.add(createSuccessRowFilter(success)); |
39 | | - } |
40 | | - if (agentId != null) { |
41 | | - filters.add(createAgentIdHashRowFilter(agentId)); |
42 | | - } |
43 | | - if (elapsedMinMax != null) { |
44 | | - filters.add(createElapsedByteRowFilter(elapsedMinMax)); |
45 | | - } |
| 41 | + filters.addAll(createSuccessRowFilter(success)); |
| 42 | + filters.addAll(createAgentIdHashRowFilter(agentId)); |
| 43 | + filters.addAll(createElapsedByteRowFilter(elapsedMin, elapsedMax)); |
46 | 44 | } |
47 | 45 |
|
48 | 46 | // value filters |
49 | 47 | if (enableValueFilter) { |
50 | | - if (elapsedMinMax != null) { |
51 | | - filters.add(createElapsedValueFilter(elapsedMinMax)); |
52 | | - } |
53 | | - if (rpcRegex != null) { |
54 | | - filters.add(createRpcRegexValueFilter(rpcRegex)); |
55 | | - } |
| 48 | + filters.addAll(createElapsedValueFilter(elapsedMin, elapsedMax)); |
| 49 | + filters.addAll(createRpcRegexValueFilter(rpcRegex)); |
56 | 50 | } |
57 | 51 | return new FilterList(filters); |
58 | 52 | } |
59 | 53 |
|
60 | | - private Filter createElapsedByteRowFilter(LongPair elapsedMinMax) { |
61 | | - List<Byte> allowedBytes = TraceIndexRowKeyUtils.toElapsedByteList(elapsedMinMax); |
62 | | - if (allowedBytes.size() == 1) { |
63 | | - return new RowFilter(CompareOperator.EQUAL, new BinaryComponentComparator(new byte[]{allowedBytes.get(0)}, ROW_FILTER_OFFSET)); |
64 | | - } else { |
65 | | - return new FilterList( |
66 | | - new RowFilter(CompareOperator.GREATER_OR_EQUAL, new BinaryComponentComparator(new byte[]{allowedBytes.get(0)}, ROW_FILTER_OFFSET)), |
67 | | - new RowFilter(CompareOperator.LESS_OR_EQUAL, new BinaryComponentComparator(new byte[]{allowedBytes.get(allowedBytes.size() - 1)}, ROW_FILTER_OFFSET)) |
68 | | - ); |
| 54 | + private List<Filter> createElapsedByteRowFilter(long elapsedMin, long elapsedMax) { |
| 55 | + if (elapsedMin <= DEFAULT_ELAPSED_MIN && elapsedMax >= DEFAULT_ELAPSED_MAX) { |
| 56 | + return List.of(); |
| 57 | + } |
| 58 | + Byte minByte = null; |
| 59 | + Byte maxByte = null; |
| 60 | + if (elapsedMin > DEFAULT_ELAPSED_MIN) { |
| 61 | + minByte = TraceIndexRowKeyUtils.toElapsedByte(elapsedMin); |
| 62 | + } |
| 63 | + if (elapsedMax < DEFAULT_ELAPSED_MAX) { |
| 64 | + maxByte = TraceIndexRowKeyUtils.toElapsedByte(elapsedMax); |
| 65 | + } |
| 66 | + |
| 67 | + // if min and max are the same, use EQUAL filter |
| 68 | + if (minByte != null && minByte.equals(maxByte)) { |
| 69 | + return List.of(new RowFilter(CompareOperator.EQUAL, new BinaryComponentComparator(new byte[]{minByte}, ROW_FILTER_OFFSET))); |
| 70 | + } |
| 71 | + |
| 72 | + // otherwise, use range filters |
| 73 | + List<Filter> filters = new ArrayList<>(2); |
| 74 | + if (minByte != null && minByte > MIN_ELAPSED_BYTE) { |
| 75 | + filters.add(new RowFilter(CompareOperator.GREATER_OR_EQUAL, new BinaryComponentComparator(new byte[]{minByte}, ROW_FILTER_OFFSET))); |
| 76 | + } |
| 77 | + if (maxByte != null) { |
| 78 | + filters.add(new RowFilter(CompareOperator.LESS_OR_EQUAL, new BinaryComponentComparator(new byte[]{maxByte}, ROW_FILTER_OFFSET))); |
69 | 79 | } |
| 80 | + return filters; |
70 | 81 | } |
71 | 82 |
|
72 | | - private Filter createSuccessRowFilter(boolean success) { |
| 83 | + private List<Filter> createSuccessRowFilter(Boolean success) { |
| 84 | + if (success == null) { |
| 85 | + return List.of(); |
| 86 | + } |
73 | 87 | // 0 for success |
74 | 88 | BinaryComponentComparator comparator = new BinaryComponentComparator(new byte[]{0}, ROW_FILTER_OFFSET + 1); // elapsed(1) |
75 | 89 | if (success) { |
76 | | - return new RowFilter(CompareOperator.EQUAL, comparator); |
| 90 | + return List.of(new RowFilter(CompareOperator.EQUAL, comparator)); |
77 | 91 | } else { |
78 | | - return new RowFilter(CompareOperator.NOT_EQUAL, comparator); |
| 92 | + return List.of(new RowFilter(CompareOperator.NOT_EQUAL, comparator)); |
79 | 93 | } |
80 | 94 | } |
81 | 95 |
|
82 | | - private Filter createAgentIdHashRowFilter(String agentId) { |
| 96 | + private List<Filter> createAgentIdHashRowFilter(String agentId) { |
| 97 | + if (agentId == null) { |
| 98 | + return List.of(); |
| 99 | + } |
83 | 100 | short agentIdHash = TraceIndexRowKeyUtils.toAgentIdHash(agentId); |
84 | | - return new RowFilter(CompareOperator.EQUAL, new BinaryComponentComparator(Bytes.toBytes(agentIdHash), ROW_FILTER_OFFSET + 2)); // elapsed(1) + error(1) |
| 101 | + return List.of(new RowFilter(CompareOperator.EQUAL, new BinaryComponentComparator(Bytes.toBytes(agentIdHash), ROW_FILTER_OFFSET + 2))); // elapsed(1) + error(1) |
| 102 | + } |
| 103 | + |
| 104 | + private List<Filter> createElapsedValueFilter(long elapsedMin, long elapsedMax) { |
| 105 | + List<Filter> filters = new ArrayList<>(); |
| 106 | + if (elapsedMin > DEFAULT_ELAPSED_MIN) { |
| 107 | + int yLow = (int) elapsedMin; |
| 108 | + filters.add(new SingleColumnValueFilter(INDEX.getName(), INDEX.getName(), CompareOperator.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes(yLow)))); |
| 109 | + } |
| 110 | + if (elapsedMax < DEFAULT_ELAPSED_MAX) { |
| 111 | + int yHigh = (int) elapsedMax; |
| 112 | + filters.add(new SingleColumnValueFilter(INDEX.getName(), INDEX.getName(), CompareOperator.LESS_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes(yHigh)))); |
| 113 | + } |
| 114 | + return filters; |
85 | 115 | } |
86 | 116 |
|
87 | | - private Filter createElapsedValueFilter(LongPair elapsedMinMax) { |
88 | | - int yLow = (int) elapsedMinMax.first(); |
89 | | - int yHigh = (int) elapsedMinMax.second(); |
90 | | - return new FilterList( |
91 | | - new SingleColumnValueFilter(INDEX.getName(), INDEX.getName(), CompareOperator.GREATER_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes(yLow))), |
92 | | - new SingleColumnValueFilter(INDEX.getName(), INDEX.getName(), CompareOperator.LESS_OR_EQUAL, new BinaryPrefixComparator(Bytes.toBytes(yHigh))) |
93 | | - ); |
| 117 | + private List<Filter> createRpcRegexValueFilter(String rpcRegex) { |
| 118 | + if (rpcRegex == null) { |
| 119 | + return List.of(); |
| 120 | + } |
| 121 | + return List.of(new SingleColumnValueFilter(META.getName(), META_QUALIFIER_RPC, CompareOperator.EQUAL, new RegexStringComparator(rpcRegex))); |
94 | 122 | } |
95 | 123 |
|
96 | | - private Filter createRpcRegexValueFilter(String rpcRegex) { |
97 | | - return new SingleColumnValueFilter(META.getName(), META_QUALIFIER_RPC, CompareOperator.EQUAL, new RegexStringComparator(rpcRegex)); |
| 124 | + public void setElapsedMin(long elapsedMin) { |
| 125 | + this.elapsedMin = elapsedMin; |
98 | 126 | } |
99 | 127 |
|
100 | | - public void setElapsedMinMax(LongPair elapsedMinMax) { |
101 | | - this.elapsedMinMax = elapsedMinMax; |
| 128 | + public void setElapsedMax(long elapsedMax) { |
| 129 | + this.elapsedMax = elapsedMax; |
102 | 130 | } |
103 | 131 |
|
104 | 132 | public void setSuccess(Boolean success) { |
|
0 commit comments