Skip to content

Commit 10ca11a

Browse files
committed
HBASE-27153 Improvements to read-path tracing
Signed-off-by: Andrew Purtell <[email protected]>
1 parent dbad3d1 commit 10ca11a

File tree

9 files changed

+669
-191
lines changed

9 files changed

+669
-191
lines changed

hbase-client/src/test/java/org/apache/hadoop/hbase/client/trace/hamcrest/AttributesMatchers.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.hamcrest.Matchers.allOf;
2121
import static org.hamcrest.Matchers.equalTo;
2222
import static org.hamcrest.Matchers.hasProperty;
23+
import static org.hamcrest.Matchers.is;
2324

2425
import io.opentelemetry.api.common.AttributeKey;
2526
import io.opentelemetry.api.common.Attributes;
@@ -54,6 +55,10 @@ public static Matcher<Attributes> containsEntry(String key, String value) {
5455
return containsEntry(AttributeKey.stringKey(key), value);
5556
}
5657

58+
public static Matcher<Attributes> containsEntry(String key, long value) {
59+
return containsEntry(AttributeKey.longKey(key), value);
60+
}
61+
5762
public static Matcher<Attributes> containsEntryWithStringValuesOf(String key, String... values) {
5863
return containsEntry(AttributeKey.stringArrayKey(key), Arrays.asList(values));
5964
}
@@ -63,6 +68,10 @@ public static Matcher<Attributes> containsEntryWithStringValuesOf(String key,
6368
return new IsAttributesContaining<>(equalTo(AttributeKey.stringArrayKey(key)), matcher);
6469
}
6570

71+
public static Matcher<Attributes> isEmpty() {
72+
return hasProperty("empty", is(true));
73+
}
74+
6675
private static final class IsAttributesContaining<T> extends TypeSafeMatcher<Attributes> {
6776
private final Matcher<AttributeKey<? super T>> keyMatcher;
6877
private final Matcher<? super T> valueMatcher;
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.io.hfile.trace;
19+
20+
import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.CHECKSUM_KEY;
21+
import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.COMPRESSION_ALGORITHM_KEY;
22+
import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.DATA_BLOCK_ENCODING_KEY;
23+
import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.ENCRYPTION_CIPHER_KEY;
24+
import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.HFILE_NAME_KEY;
25+
import static org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.READ_TYPE_KEY;
26+
27+
import io.opentelemetry.api.common.AttributesBuilder;
28+
import io.opentelemetry.context.Context;
29+
import io.opentelemetry.context.ContextKey;
30+
import java.util.Objects;
31+
import java.util.function.Consumer;
32+
import org.apache.hadoop.hbase.io.hfile.HFileContext;
33+
import org.apache.hadoop.hbase.trace.HBaseSemanticAttributes.ReadType;
34+
import org.apache.hadoop.hbase.util.ChecksumType;
35+
import org.apache.yetus.audience.InterfaceAudience;
36+
37+
/**
38+
* <p>
39+
* Populate fields on an {@link AttributesBuilder} based on an {@link HFileContext}. Passed around
40+
* inside an active {@link Context}, indexed under {@link #CONTEXT_KEY}. The class is designed such
41+
* that calls to the {@link #accept(AttributesBuilder)} method are idempotent with regards to the
42+
* instance of this class.
43+
* </p>
44+
* <p>
45+
* The true and truly ridiculous class name should be something more like
46+
* {@code HFileContext_ContextAttributes_AttributesBuilder_Consumer}.
47+
* </p>
48+
*/
49+
@InterfaceAudience.Private
50+
public class HFileContextAttributesBuilderConsumer implements Consumer<AttributesBuilder> {
51+
52+
/**
53+
* Used to place extract attributes pertaining to the {@link HFileContext} that scopes the active
54+
* {@link Context}.
55+
*/
56+
public static final ContextKey<Consumer<AttributesBuilder>> CONTEXT_KEY =
57+
ContextKey.named("db.hbase.io.hfile.context_attributes");
58+
59+
private final HFileContext hFileContext;
60+
61+
private boolean skipChecksum = false;
62+
private ReadType readType = null;
63+
64+
public HFileContextAttributesBuilderConsumer(final HFileContext hFileContext) {
65+
this.hFileContext = Objects.requireNonNull(hFileContext);
66+
}
67+
68+
/**
69+
* Specify that the {@link ChecksumType} should not be included in the attributes.
70+
*/
71+
public HFileContextAttributesBuilderConsumer setSkipChecksum(final boolean skipChecksum) {
72+
this.skipChecksum = skipChecksum;
73+
return this;
74+
}
75+
76+
/**
77+
* Specify the {@link ReadType} involced in this IO operation.
78+
*/
79+
public HFileContextAttributesBuilderConsumer setReadType(final ReadType readType) {
80+
// TODO: this is not a part of the HFileBlock, its context of the operation. Should track this
81+
// detail elsewhere.
82+
this.readType = readType;
83+
return this;
84+
}
85+
86+
@Override
87+
public void accept(AttributesBuilder builder) {
88+
if (hFileContext.getHFileName() != null) {
89+
builder.put(HFILE_NAME_KEY, hFileContext.getHFileName());
90+
}
91+
if (hFileContext.getCompression() != null) {
92+
builder.put(COMPRESSION_ALGORITHM_KEY, hFileContext.getCompression().getName());
93+
}
94+
if (hFileContext.getDataBlockEncoding() != null) {
95+
builder.put(DATA_BLOCK_ENCODING_KEY, hFileContext.getDataBlockEncoding().name());
96+
}
97+
if (
98+
hFileContext.getEncryptionContext() != null
99+
&& hFileContext.getEncryptionContext().getCipher() != null
100+
) {
101+
builder.put(ENCRYPTION_CIPHER_KEY, hFileContext.getEncryptionContext().getCipher().getName());
102+
}
103+
if (!skipChecksum && hFileContext.getChecksumType() != null) {
104+
builder.put(CHECKSUM_KEY, hFileContext.getChecksumType().getName());
105+
}
106+
if (readType != null) {
107+
builder.put(READ_TYPE_KEY, readType.name());
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)