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.SpanDataMatchers.hasEnded;
021import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasException;
022import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasName;
023import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasParentSpanId;
024import static org.apache.hadoop.hbase.client.trace.hamcrest.SpanDataMatchers.hasStatusWithCode;
025import static org.hamcrest.MatcherAssert.assertThat;
026import static org.hamcrest.Matchers.allOf;
027import static org.hamcrest.Matchers.hasItem;
028import static org.hamcrest.Matchers.startsWith;
029
030import io.opentelemetry.api.trace.StatusCode;
031import io.opentelemetry.sdk.trace.data.SpanData;
032import java.util.List;
033import java.util.Objects;
034import java.util.function.Supplier;
035import java.util.stream.Collectors;
036import org.apache.hadoop.hbase.HBaseClassTestRule;
037import org.apache.hadoop.hbase.client.trace.StringTraceRenderer;
038import org.apache.hadoop.hbase.testclassification.ClientTests;
039import org.apache.hadoop.hbase.testclassification.LargeTests;
040import org.hamcrest.Matcher;
041import org.junit.ClassRule;
042import org.junit.experimental.categories.Category;
043import org.junit.runner.RunWith;
044import org.junit.runners.Parameterized;
045import org.junit.runners.Parameterized.Parameter;
046import org.junit.runners.Parameterized.Parameters;
047import org.slf4j.Logger;
048import org.slf4j.LoggerFactory;
049
050@RunWith(Parameterized.class)
051@Category({ LargeTests.class, ClientTests.class })
052public class TestAsyncTableScanAll extends AbstractTestAsyncTableScan {
053  private static final Logger logger = LoggerFactory.getLogger(TestAsyncTableScanAll.class);
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestAsyncTableScanAll.class);
058
059  @Parameter(0)
060  public String tableType;
061
062  @Parameter(1)
063  public Supplier<AsyncTable<?>> getTable;
064
065  @Parameter(2)
066  public String scanType;
067
068  @Parameter(3)
069  public Supplier<Scan> scanCreator;
070
071  @Parameters(name = "{index}: table={0}, scan={2}")
072  public static List<Object[]> params() {
073    return getTableAndScanCreatorParams();
074  }
075
076  @Override
077  protected Scan createScan() {
078    return scanCreator.get();
079  }
080
081  @Override
082  protected List<Result> doScan(Scan scan, int closeAfter) throws Exception {
083    List<Result> results = getTable.get().scanAll(scan).get();
084    if (scan.getBatch() > 0) {
085      results = convertFromBatchResult(results);
086    }
087    // we can't really close the scan early for scanAll, but to keep the assertions
088    // simple in AbstractTestAsyncTableScan we'll just sublist here instead.
089    if (closeAfter > 0 && closeAfter < results.size()) {
090      results = results.subList(0, closeAfter);
091    }
092    return results;
093  }
094
095  @Override
096  protected void assertTraceContinuity() {
097    final String parentSpanName = testName.getMethodName();
098    final Matcher<SpanData> parentSpanMatcher =
099      allOf(hasName(parentSpanName), hasStatusWithCode(StatusCode.OK), hasEnded());
100    waitForSpan(parentSpanMatcher);
101
102    final List<SpanData> spans =
103      otelClassRule.getSpans().stream().filter(Objects::nonNull).collect(Collectors.toList());
104    if (logger.isDebugEnabled()) {
105      StringTraceRenderer stringTraceRenderer = new StringTraceRenderer(spans);
106      stringTraceRenderer.render(logger::debug);
107    }
108
109    final String parentSpanId = spans.stream().filter(parentSpanMatcher::matches)
110      .map(SpanData::getSpanId).findAny().orElseThrow(AssertionError::new);
111
112    final Matcher<SpanData> scanOperationSpanMatcher =
113      allOf(hasName(startsWith("SCAN " + TABLE_NAME.getNameWithNamespaceInclAsString())),
114        hasParentSpanId(parentSpanId), hasStatusWithCode(StatusCode.OK), hasEnded());
115    assertThat(spans, hasItem(scanOperationSpanMatcher));
116  }
117
118  @Override
119  protected void
120    assertTraceError(Matcher<io.opentelemetry.api.common.Attributes> exceptionMatcher) {
121    final String parentSpanName = testName.getMethodName();
122    final Matcher<SpanData> parentSpanMatcher = allOf(hasName(parentSpanName), hasEnded());
123    waitForSpan(parentSpanMatcher);
124
125    final List<SpanData> spans =
126      otelClassRule.getSpans().stream().filter(Objects::nonNull).collect(Collectors.toList());
127    if (logger.isDebugEnabled()) {
128      StringTraceRenderer stringTraceRenderer = new StringTraceRenderer(spans);
129      stringTraceRenderer.render(logger::debug);
130    }
131
132    final String parentSpanId = spans.stream().filter(parentSpanMatcher::matches)
133      .map(SpanData::getSpanId).findAny().orElseThrow(AssertionError::new);
134
135    final Matcher<SpanData> scanOperationSpanMatcher =
136      allOf(hasName(startsWith("SCAN " + TABLE_NAME.getNameWithNamespaceInclAsString())),
137        hasParentSpanId(parentSpanId), hasStatusWithCode(StatusCode.ERROR),
138        hasException(exceptionMatcher), hasEnded());
139    assertThat(spans, hasItem(scanOperationSpanMatcher));
140  }
141}