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