|  | 
|  | 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