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