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