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.junit.Assert.assertEquals;
021
022import io.opentelemetry.api.trace.SpanKind;
023import io.opentelemetry.api.trace.StatusCode;
024import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule;
025import io.opentelemetry.sdk.trace.data.SpanData;
026import java.io.IOException;
027import java.util.concurrent.CompletableFuture;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseConfiguration;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.Waiter;
033import org.apache.hadoop.hbase.security.UserProvider;
034import org.apache.hadoop.hbase.testclassification.ClientTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.trace.HBaseSemanticAttributes;
037import org.junit.After;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Rule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043
044import org.apache.hbase.thirdparty.com.google.common.io.Closeables;
045
046@Category({ ClientTests.class, MediumTests.class })
047public class TestAsyncConnectionTracing {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestAsyncConnectionTracing.class);
052
053  private static Configuration CONF = HBaseConfiguration.create();
054
055  private ServerName masterServer =
056    ServerName.valueOf("localhost", 12345, System.currentTimeMillis());
057
058  private AsyncConnection conn;
059
060  @Rule
061  public OpenTelemetryRule traceRule = OpenTelemetryRule.create();
062
063  @Before
064  public void setUp() throws IOException {
065    ConnectionRegistry registry = new DoNothingConnectionRegistry(CONF) {
066
067      @Override
068      public CompletableFuture<ServerName> getActiveMaster() {
069        return CompletableFuture.completedFuture(masterServer);
070      }
071    };
072    conn =
073      new AsyncConnectionImpl(CONF, registry, "test", UserProvider.instantiate(CONF).getCurrent());
074  }
075
076  @After
077  public void tearDown() throws IOException {
078    Closeables.close(conn, true);
079  }
080
081  private void assertTrace(String methodName, ServerName serverName) {
082    Waiter.waitFor(CONF, 1000,
083      () -> traceRule.getSpans().stream()
084        .anyMatch(span -> span.getName().equals("AsyncConnection." + methodName)
085          && span.getKind() == SpanKind.INTERNAL && span.hasEnded()));
086    SpanData data = traceRule.getSpans().stream()
087      .filter(s -> s.getName().equals("AsyncConnection." + methodName)).findFirst().get();
088    assertEquals(StatusCode.OK, data.getStatus().getStatusCode());
089    if (serverName != null) {
090      assertEquals(serverName.getServerName(),
091        data.getAttributes().get(HBaseSemanticAttributes.SERVER_NAME_KEY));
092    }
093  }
094
095  @Test
096  public void testHbck() {
097    conn.getHbck().join();
098    assertTrace("getHbck", masterServer);
099  }
100
101  @Test
102  public void testHbckWithServerName() throws IOException {
103    ServerName serverName = ServerName.valueOf("localhost", 23456, System.currentTimeMillis());
104    conn.getHbck(serverName);
105    assertTrace("getHbck", serverName);
106  }
107
108  @Test
109  public void testClose() throws IOException {
110    conn.close();
111    assertTrace("close", null);
112  }
113}