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.client;
019
020import static org.apache.hadoop.hbase.client.trace.hamcrest.AttributesMatchers.containsEntryWithStringValuesOf;
021import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasAttributes;
022import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasKind;
023import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasStatusWithCode;
024import static org.apache.hadoop.hbase.client.trace.hamcrest.TraceTestUtil.buildConnectionAttributesMatcher;
025import static org.apache.hadoop.hbase.client.trace.hamcrest.TraceTestUtil.buildTableAttributesMatcher;
026import static org.hamcrest.MatcherAssert.assertThat;
027import static org.hamcrest.Matchers.allOf;
028import static org.hamcrest.Matchers.containsInAnyOrder;
029
030import io.opentelemetry.api.trace.SpanKind;
031import io.opentelemetry.api.trace.StatusCode;
032import io.opentelemetry.sdk.trace.data.SpanData;
033import java.io.IOException;
034import java.util.Arrays;
035import java.util.Collections;
036import org.apache.hadoop.hbase.HBaseClassTestRule;
037import org.apache.hadoop.hbase.HConstants;
038import org.apache.hadoop.hbase.HRegionLocation;
039import org.apache.hadoop.hbase.TableName;
040import org.apache.hadoop.hbase.security.UserProvider;
041import org.apache.hadoop.hbase.testclassification.ClientTests;
042import org.apache.hadoop.hbase.testclassification.MediumTests;
043import org.junit.After;
044import org.junit.Before;
045import org.junit.ClassRule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048
049import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
050
051@Category({ ClientTests.class, MediumTests.class })
052public class TestRegionLocatorTracing extends TestTracingBase {
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestRegionLocatorTracing.class);
056
057  ConnectionImplementation conn;
058
059  @Override
060  @Before
061  public void setUp() throws Exception {
062    super.setUp();
063    conn = new ConnectionImplementation(conf, null, UserProvider.instantiate(conf).getCurrent(),
064      Collections.emptyMap());
065  }
066
067  @After
068  public void tearDown() throws IOException {
069    Closeables.close(conn, true);
070  }
071
072  @Test
073  public void testGetRegionLocation() throws IOException {
074    conn.getRegionLocator(TableName.META_TABLE_NAME).getRegionLocation(HConstants.EMPTY_START_ROW);
075    SpanData span = waitSpan("HRegionLocator.getRegionLocation");
076    assertThat(span,
077      allOf(hasStatusWithCode(StatusCode.OK), hasKind(SpanKind.INTERNAL),
078        buildConnectionAttributesMatcher(conn),
079        buildTableAttributesMatcher(TableName.META_TABLE_NAME),
080        hasAttributes(containsEntryWithStringValuesOf("db.hbase.regions",
081          META_REGION_LOCATION.getDefaultRegionLocation().getRegion().getRegionNameAsString()))));
082  }
083
084  @Test
085  public void testGetRegionLocations() throws IOException {
086    conn.getRegionLocator(TableName.META_TABLE_NAME).getRegionLocations(HConstants.EMPTY_START_ROW);
087    SpanData span = waitSpan("HRegionLocator.getRegionLocations");
088    // TODO: Use a value of `META_REGION_LOCATION` that contains multiple region locations.
089    String[] expectedRegions =
090      Arrays.stream(META_REGION_LOCATION.getRegionLocations()).map(HRegionLocation::getRegion)
091        .map(RegionInfo::getRegionNameAsString).toArray(String[]::new);
092    assertThat(span, allOf(hasStatusWithCode(StatusCode.OK), hasKind(SpanKind.INTERNAL),
093      buildConnectionAttributesMatcher(conn),
094      buildTableAttributesMatcher(TableName.META_TABLE_NAME), hasAttributes(
095        containsEntryWithStringValuesOf("db.hbase.regions", containsInAnyOrder(expectedRegions)))));
096  }
097
098  @Test
099  public void testGetAllRegionLocations() throws IOException {
100    conn.getRegionLocator(TableName.META_TABLE_NAME).getAllRegionLocations();
101    SpanData span = waitSpan("HRegionLocator.getAllRegionLocations");
102    // TODO: Use a value of `META_REGION_LOCATION` that contains multiple region locations.
103    String[] expectedRegions =
104      Arrays.stream(META_REGION_LOCATION.getRegionLocations()).map(HRegionLocation::getRegion)
105        .map(RegionInfo::getRegionNameAsString).toArray(String[]::new);
106    assertThat(span, allOf(hasStatusWithCode(StatusCode.OK), hasKind(SpanKind.INTERNAL),
107      buildConnectionAttributesMatcher(conn),
108      buildTableAttributesMatcher(TableName.META_TABLE_NAME), hasAttributes(
109        containsEntryWithStringValuesOf("db.hbase.regions", containsInAnyOrder(expectedRegions)))));
110  }
111
112  @Test
113  public void testClearRegionLocationCache() throws IOException {
114    conn.getRegionLocator(TableName.META_TABLE_NAME).clearRegionLocationCache();
115    SpanData span = waitSpan("HRegionLocator.clearRegionLocationCache");
116    assertThat(span,
117      allOf(hasStatusWithCode(StatusCode.OK), hasKind(SpanKind.INTERNAL),
118        buildConnectionAttributesMatcher(conn),
119        buildTableAttributesMatcher(TableName.META_TABLE_NAME)));
120  }
121
122}