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