001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.jupiter.api.Assertions.assertTrue;
021
022import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.TableNameTestExtension;
029import org.apache.hadoop.hbase.client.Append;
030import org.apache.hadoop.hbase.client.CheckAndMutate;
031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
032import org.apache.hadoop.hbase.client.Delete;
033import org.apache.hadoop.hbase.client.Get;
034import org.apache.hadoop.hbase.client.Increment;
035import org.apache.hadoop.hbase.client.Mutation;
036import org.apache.hadoop.hbase.client.Put;
037import org.apache.hadoop.hbase.client.RegionInfo;
038import org.apache.hadoop.hbase.client.RegionInfoBuilder;
039import org.apache.hadoop.hbase.client.Scan;
040import org.apache.hadoop.hbase.client.TableDescriptor;
041import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
042import org.apache.hadoop.hbase.testclassification.MediumTests;
043import org.apache.hadoop.hbase.testclassification.RegionServerTests;
044import org.apache.hadoop.hbase.trace.HBaseSemanticAttributes;
045import org.apache.hadoop.hbase.util.Bytes;
046import org.junit.jupiter.api.AfterAll;
047import org.junit.jupiter.api.AfterEach;
048import org.junit.jupiter.api.BeforeEach;
049import org.junit.jupiter.api.Tag;
050import org.junit.jupiter.api.Test;
051import org.junit.jupiter.api.extension.RegisterExtension;
052
053@Tag(RegionServerTests.TAG)
054@Tag(MediumTests.TAG)
055public class TestHRegionTracing {
056
057  private static HBaseTestingUtil UTIL = new HBaseTestingUtil();
058
059  private static byte[] FAMILY = Bytes.toBytes("family");
060
061  private static byte[] QUALIFIER = Bytes.toBytes("qual");
062
063  private static byte[] ROW = Bytes.toBytes("row");
064
065  private static byte[] VALUE = Bytes.toBytes("value");
066
067  @RegisterExtension
068  public static final OpenTelemetryExtension traceRule = OpenTelemetryExtension.create();
069
070  @RegisterExtension
071  public final TableNameTestExtension tableNameRule = new TableNameTestExtension();
072
073  private HRegion region;
074
075  @AfterAll
076  public static void tearDownAfterClass() throws IOException {
077    UTIL.cleanupTestDir();
078  }
079
080  @BeforeEach
081  public void setUp() throws Throwable {
082    traceRule.clearSpans();
083    TableName tableName = tableNameRule.getTableName();
084    TableDescriptor desc = TableDescriptorBuilder.newBuilder(tableName)
085      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build();
086    RegionInfo info = RegionInfoBuilder.newBuilder(tableName).build();
087    ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null,
088      MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);
089    region = UTIL.createLocalHRegion(info, desc);
090  }
091
092  @AfterEach
093  public void tearDown() throws IOException {
094    HBaseTestingUtil.closeRegionAndWAL(region);
095  }
096
097  private void assertSpan(String spanName) {
098    assertTrue(traceRule.getSpans().stream().anyMatch(span -> {
099      if (!span.getName().equals(spanName)) {
100        return false;
101      }
102      List<String> regionNames = span.getAttributes().get(HBaseSemanticAttributes.REGION_NAMES_KEY);
103      return regionNames != null && regionNames.size() == 1
104        && regionNames.get(0).equals(region.getRegionInfo().getRegionNameAsString());
105    }));
106  }
107
108  @Test
109  public void testGet() throws IOException {
110    region.get(new Get(ROW));
111    assertSpan("Region.get");
112  }
113
114  @Test
115  public void testPut() throws IOException {
116    region.put(new Put(ROW).addColumn(FAMILY, QUALIFIER, VALUE));
117    assertSpan("Region.put");
118    assertSpan("Region.getRowLock");
119  }
120
121  @Test
122  public void testDelete() throws IOException {
123    region.delete(new Delete(ROW).addColumn(FAMILY, QUALIFIER));
124    assertSpan("Region.delete");
125    assertSpan("Region.getRowLock");
126  }
127
128  @Test
129  public void testAppend() throws IOException {
130    region.append(new Append(ROW).addColumn(FAMILY, QUALIFIER, VALUE));
131    assertSpan("Region.append");
132    assertSpan("Region.getRowLock");
133  }
134
135  @Test
136  public void testIncrement() throws IOException {
137    region.increment(new Increment(ROW).addColumn(FAMILY, QUALIFIER, 1));
138    assertSpan("Region.increment");
139    assertSpan("Region.getRowLock");
140  }
141
142  @Test
143  public void testBatchMutate() throws IOException {
144    region.batchMutate(new Mutation[] { new Put(ROW).addColumn(FAMILY, QUALIFIER, VALUE) });
145    assertSpan("Region.batchMutate");
146    assertSpan("Region.getRowLock");
147  }
148
149  @Test
150  public void testCheckAndMutate() throws IOException {
151    region.checkAndMutate(CheckAndMutate.newBuilder(ROW).ifNotExists(FAMILY, QUALIFIER)
152      .build(new Put(ROW).addColumn(FAMILY, QUALIFIER, VALUE)));
153    assertSpan("Region.checkAndMutate");
154    assertSpan("Region.getRowLock");
155  }
156
157  @Test
158  public void testScanner() throws IOException {
159    try (RegionScanner scanner = region.getScanner(new Scan())) {
160      scanner.reseek(ROW);
161      scanner.next(new ArrayList<>());
162    }
163    assertSpan("Region.getScanner");
164    assertSpan("RegionScanner.reseek");
165    assertSpan("RegionScanner.close");
166  }
167}