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.master.procedure;
019
020import static io.opentelemetry.api.common.AttributeKey.longKey;
021import static org.apache.hadoop.hbase.client.trace.hamcrest.AttributesMatchers.containsEntry;
022import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasAttributes;
023import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasEnded;
024import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasName;
025import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasStatusWithCode;
026import static org.hamcrest.MatcherAssert.assertThat;
027import static org.hamcrest.Matchers.allOf;
028import static org.hamcrest.Matchers.any;
029import static org.hamcrest.Matchers.anyOf;
030import static org.hamcrest.Matchers.containsString;
031import static org.hamcrest.Matchers.hasItem;
032
033import io.opentelemetry.api.trace.StatusCode;
034import io.opentelemetry.sdk.trace.data.SpanData;
035import java.util.List;
036import java.util.concurrent.TimeUnit;
037import java.util.concurrent.atomic.AtomicReference;
038import java.util.stream.Collectors;
039import org.apache.hadoop.hbase.ConnectionRule;
040import org.apache.hadoop.hbase.HBaseClassTestRule;
041import org.apache.hadoop.hbase.HRegionLocation;
042import org.apache.hadoop.hbase.MatcherPredicate;
043import org.apache.hadoop.hbase.MiniClusterRule;
044import org.apache.hadoop.hbase.StartMiniClusterOption;
045import org.apache.hadoop.hbase.TableName;
046import org.apache.hadoop.hbase.Waiter;
047import org.apache.hadoop.hbase.client.AsyncAdmin;
048import org.apache.hadoop.hbase.client.AsyncConnection;
049import org.apache.hadoop.hbase.client.RegionInfo;
050import org.apache.hadoop.hbase.client.Table;
051import org.apache.hadoop.hbase.client.trace.StringTraceRenderer;
052import org.apache.hadoop.hbase.testclassification.MasterTests;
053import org.apache.hadoop.hbase.testclassification.MediumTests;
054import org.apache.hadoop.hbase.trace.OpenTelemetryClassRule;
055import org.apache.hadoop.hbase.trace.OpenTelemetryTestRule;
056import org.apache.hadoop.hbase.trace.TraceUtil;
057import org.apache.hadoop.hbase.util.Bytes;
058import org.hamcrest.Matcher;
059import org.hamcrest.Matchers;
060import org.junit.ClassRule;
061import org.junit.Rule;
062import org.junit.Test;
063import org.junit.experimental.categories.Category;
064import org.junit.rules.RuleChain;
065import org.junit.rules.TestName;
066import org.junit.rules.TestRule;
067import org.slf4j.Logger;
068import org.slf4j.LoggerFactory;
069
070/**
071 * Test of master ProcedureV2 tracing.
072 */
073@Category({ MasterTests.class, MediumTests.class })
074public class TestProcedureTracing {
075  private static final Logger LOG = LoggerFactory.getLogger(TestProcedureTracing.class);
076
077  @ClassRule
078  public static final HBaseClassTestRule CLASS_RULE =
079    HBaseClassTestRule.forClass(TestProcedureTracing.class);
080
081  private static final OpenTelemetryClassRule otelClassRule = OpenTelemetryClassRule.create();
082  private static final MiniClusterRule miniClusterRule = MiniClusterRule.newBuilder()
083    .setMiniClusterOption(StartMiniClusterOption.builder().numWorkers(3).build()).build();
084  protected static final ConnectionRule connectionRule =
085    ConnectionRule.createAsyncConnectionRule(miniClusterRule::createAsyncConnection);
086
087  @ClassRule
088  public static final TestRule classRule =
089    RuleChain.outerRule(otelClassRule).around(miniClusterRule).around(connectionRule);
090
091  @Rule
092  public final OpenTelemetryTestRule otelTestRule = new OpenTelemetryTestRule(otelClassRule);
093
094  @Rule
095  public TestName name = new TestName();
096
097  @Test
098  public void testCreateOpenDeleteTableSpans() throws Exception {
099    final TableName tableName = TableName.valueOf(name.getMethodName());
100    final AsyncConnection conn = connectionRule.getAsyncConnection();
101    final AsyncAdmin admin = conn.getAdmin();
102
103    final AtomicReference<List<String>> tableRegionsRef = new AtomicReference<>();
104    TraceUtil.trace(() -> {
105      try (final Table ignored = miniClusterRule.getTestingUtility()
106        .createMultiRegionTable(tableName, Bytes.toBytes("fam"), 5)) {
107        final List<String> tableRegions = conn.getRegionLocator(tableName).getAllRegionLocations()
108          .get().stream().map(HRegionLocation::getRegion).map(RegionInfo::getEncodedName)
109          .collect(Collectors.toList());
110        tableRegionsRef.set(tableRegions);
111      }
112      if (admin.tableExists(tableName).get()) {
113        if (admin.isTableEnabled(tableName).get()) {
114          admin.disableTable(tableName).get();
115        }
116        admin.deleteTable(tableName).get();
117      }
118    }, name.getMethodName());
119
120    final Matcher<SpanData> testSpanMatcher = allOf(hasName(name.getMethodName()), hasEnded());
121    Waiter.waitFor(conn.getConfiguration(), TimeUnit.MINUTES.toMillis(3),
122      new MatcherPredicate<>(otelClassRule::getSpans, hasItem(testSpanMatcher)));
123    final List<SpanData> spans = otelClassRule.getSpans();
124    final StringTraceRenderer renderer = new StringTraceRenderer(spans);
125    renderer.render(LOG::debug);
126
127    // Expect to find a span for a CreateTableProcedure for the test table
128    final Matcher<SpanData> createTableProcedureSpanMatcher = allOf(
129      hasName(allOf(containsString("CreateTableProcedure"),
130        containsString("table=" + name.getMethodName()))),
131      hasEnded(), hasStatusWithCode(StatusCode.OK),
132      hasAttributes(allOf(containsEntry(longKey("procId"), any(Long.class)),
133        containsEntry(longKey("parentProcId"), any(Long.class)))));
134    assertThat("Expected to find a span for a CreateTableProcedure for the test table", spans,
135      hasItem(createTableProcedureSpanMatcher));
136
137    // Expect to find a span for a TransitRegionStateProcedure for the test table
138    final Matcher<SpanData> transitRegionStateProcedureSpanMatcher = allOf(
139      hasName(allOf(containsString("TransitRegionStateProcedure"),
140        containsString("table=" + name.getMethodName()))),
141      hasEnded(), hasStatusWithCode(StatusCode.OK),
142      hasAttributes(allOf(containsEntry(longKey("procId"), any(Long.class)),
143        containsEntry(longKey("parentProcId"), any(Long.class)))));
144    assertThat("Expected to find a span for a TransitRegionStateProcedure for the test table",
145      spans, hasItem(transitRegionStateProcedureSpanMatcher));
146
147    // Expect to find a span for an OpenRegionProcedure for a region of the test table
148    final List<Matcher<? super String>> tableRegionMatchers =
149      tableRegionsRef.get().stream().map(Matchers::containsString).collect(Collectors.toList());
150    final Matcher<SpanData> openRegionProcedureSpanMatcher =
151      allOf(hasName(allOf(containsString("OpenRegionProcedure"), anyOf(tableRegionMatchers))),
152        hasEnded(), hasStatusWithCode(StatusCode.OK),
153        hasAttributes(allOf(containsEntry(longKey("procId"), any(Long.class)),
154          containsEntry(longKey("parentProcId"), any(Long.class)))));
155    assertThat("Expected to find a span for an OpenRegionProcedure for a region of the test table",
156      spans, hasItem(openRegionProcedureSpanMatcher));
157
158    // Expect to find a span for a CloseRegionProcedure for a region of the test table
159    final Matcher<SpanData> closeRegionProcedureSpanMatcher =
160      allOf(hasName(allOf(containsString("CloseRegionProcedure"), anyOf(tableRegionMatchers))),
161        hasEnded(), hasStatusWithCode(StatusCode.OK),
162        hasAttributes(allOf(containsEntry(longKey("procId"), any(Long.class)),
163          containsEntry(longKey("parentProcId"), any(Long.class)))));
164    assertThat("Expected to find a span for a CloseRegionProcedure for a region of the test table",
165      spans, hasItem(closeRegionProcedureSpanMatcher));
166
167    // Expect to find a span for a DisableTableProcedure for the test table
168    final Matcher<SpanData> disableTableProcedureSpanMatcher = allOf(
169      hasName(allOf(containsString("DisableTableProcedure"),
170        containsString("table=" + name.getMethodName()))),
171      hasEnded(), hasStatusWithCode(StatusCode.OK),
172      hasAttributes(allOf(containsEntry(longKey("procId"), any(Long.class)),
173        containsEntry(longKey("parentProcId"), any(Long.class)))));
174    assertThat("Expected to find a span for a DisableTableProcedure for the test table", spans,
175      hasItem(disableTableProcedureSpanMatcher));
176
177    // Expect to find a span for a DeleteTableProcedure for the test table
178    final Matcher<SpanData> deleteTableProcedureSpanMatcher = allOf(
179      hasName(allOf(containsString("DeleteTableProcedure"),
180        containsString("table=" + name.getMethodName()))),
181      hasEnded(), hasStatusWithCode(StatusCode.OK),
182      hasAttributes(allOf(containsEntry(longKey("procId"), any(Long.class)),
183        containsEntry(longKey("parentProcId"), any(Long.class)))));
184    assertThat("Expected to find a span for a DeleteTableProcedure for the test table", spans,
185      hasItem(deleteTableProcedureSpanMatcher));
186  }
187}