|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.client; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertEquals; |
| 21 | + |
| 22 | +import io.opentelemetry.api.trace.SpanKind; |
| 23 | +import io.opentelemetry.api.trace.StatusCode; |
| 24 | +import io.opentelemetry.sdk.testing.junit4.OpenTelemetryRule; |
| 25 | +import io.opentelemetry.sdk.trace.data.SpanData; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.concurrent.CompletableFuture; |
| 28 | +import org.apache.hadoop.conf.Configuration; |
| 29 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 30 | +import org.apache.hadoop.hbase.HBaseConfiguration; |
| 31 | +import org.apache.hadoop.hbase.ServerName; |
| 32 | +import org.apache.hadoop.hbase.Waiter; |
| 33 | +import org.apache.hadoop.hbase.security.UserProvider; |
| 34 | +import org.apache.hadoop.hbase.testclassification.ClientTests; |
| 35 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 36 | +import org.apache.hadoop.hbase.trace.TraceUtil; |
| 37 | +import org.junit.After; |
| 38 | +import org.junit.Before; |
| 39 | +import org.junit.ClassRule; |
| 40 | +import org.junit.Rule; |
| 41 | +import org.junit.Test; |
| 42 | +import org.junit.experimental.categories.Category; |
| 43 | + |
| 44 | +import org.apache.hbase.thirdparty.com.google.common.io.Closeables; |
| 45 | + |
| 46 | +@Category({ ClientTests.class, MediumTests.class }) |
| 47 | +public class TestAsyncConnectionTracing { |
| 48 | + |
| 49 | + @ClassRule |
| 50 | + public static final HBaseClassTestRule CLASS_RULE = |
| 51 | + HBaseClassTestRule.forClass(TestAsyncConnectionTracing.class); |
| 52 | + |
| 53 | + private static Configuration CONF = HBaseConfiguration.create(); |
| 54 | + |
| 55 | + private ServerName masterServer = |
| 56 | + ServerName.valueOf("localhost", 12345, System.currentTimeMillis()); |
| 57 | + |
| 58 | + private AsyncConnection conn; |
| 59 | + |
| 60 | + @Rule |
| 61 | + public OpenTelemetryRule traceRule = OpenTelemetryRule.create(); |
| 62 | + |
| 63 | + @Before |
| 64 | + public void setUp() throws IOException { |
| 65 | + ConnectionRegistry registry = new DoNothingConnectionRegistry(CONF) { |
| 66 | + |
| 67 | + @Override |
| 68 | + public CompletableFuture<ServerName> getActiveMaster() { |
| 69 | + return CompletableFuture.completedFuture(masterServer); |
| 70 | + } |
| 71 | + }; |
| 72 | + conn = new AsyncConnectionImpl(CONF, registry, "test", null, |
| 73 | + UserProvider.instantiate(CONF).getCurrent()); |
| 74 | + } |
| 75 | + |
| 76 | + @After |
| 77 | + public void tearDown() throws IOException { |
| 78 | + Closeables.close(conn, true); |
| 79 | + } |
| 80 | + |
| 81 | + private void assertTrace(String methodName, ServerName serverName) { |
| 82 | + Waiter.waitFor(CONF, 1000, |
| 83 | + () -> traceRule.getSpans().stream() |
| 84 | + .anyMatch(span -> span.getName().equals("AsyncConnection." + methodName) && |
| 85 | + span.getKind() == SpanKind.INTERNAL && span.hasEnded())); |
| 86 | + SpanData data = traceRule.getSpans().stream() |
| 87 | + .filter(s -> s.getName().equals("AsyncConnection." + methodName)).findFirst().get(); |
| 88 | + assertEquals(StatusCode.OK, data.getStatus().getStatusCode()); |
| 89 | + if (serverName != null) { |
| 90 | + assertEquals(serverName.getServerName(), data.getAttributes().get(TraceUtil.SERVER_NAME_KEY)); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testHbck() { |
| 96 | + conn.getHbck().join(); |
| 97 | + assertTrace("getHbck", masterServer); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testHbckWithServerName() throws IOException { |
| 102 | + ServerName serverName = ServerName.valueOf("localhost", 23456, System.currentTimeMillis()); |
| 103 | + conn.getHbck(serverName); |
| 104 | + assertTrace("getHbck", serverName); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testClose() throws IOException { |
| 109 | + conn.close(); |
| 110 | + assertTrace("close", null); |
| 111 | + } |
| 112 | +} |
0 commit comments