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.trace; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertTrue; 022 023import java.util.Collection; 024import java.util.LinkedList; 025import java.util.List; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtility; 028import org.apache.hadoop.hbase.StartMiniClusterOption; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.Waiter; 031import org.apache.hadoop.hbase.client.Put; 032import org.apache.hadoop.hbase.client.Table; 033import org.apache.hadoop.hbase.testclassification.MediumTests; 034import org.apache.hadoop.hbase.testclassification.MiscTests; 035import org.apache.htrace.core.POJOSpanReceiver; 036import org.apache.htrace.core.Sampler; 037import org.apache.htrace.core.Span; 038import org.apache.htrace.core.TraceScope; 039import org.junit.AfterClass; 040import org.junit.BeforeClass; 041import org.junit.ClassRule; 042import org.junit.Ignore; 043import org.junit.Rule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046import org.junit.rules.TestName; 047 048import org.apache.hbase.thirdparty.com.google.common.collect.Sets; 049 050@Ignore // We don't support htrace in hbase-2.0.0 and this flakey is a little flakey. 051@Category({MiscTests.class, MediumTests.class}) 052public class TestHTraceHooks { 053 054 @ClassRule 055 public static final HBaseClassTestRule CLASS_RULE = 056 HBaseClassTestRule.forClass(TestHTraceHooks.class); 057 058 private static final byte[] FAMILY_BYTES = "family".getBytes(); 059 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 060 private static POJOSpanReceiver rcvr; 061 062 @Rule 063 public TestName name = new TestName(); 064 065 @BeforeClass 066 public static void before() throws Exception { 067 StartMiniClusterOption option = StartMiniClusterOption.builder() 068 .numMasters(2).numRegionServers(3).numDataNodes(3).build(); 069 TEST_UTIL.startMiniCluster(option); 070 rcvr = new POJOSpanReceiver(new HBaseHTraceConfiguration(TEST_UTIL.getConfiguration())); 071 TraceUtil.addReceiver(rcvr); 072 TraceUtil.addSampler(new Sampler() { 073 @Override 074 public boolean next() { 075 return true; 076 } 077 }); 078 } 079 080 @AfterClass 081 public static void after() throws Exception { 082 TEST_UTIL.shutdownMiniCluster(); 083 TraceUtil.removeReceiver(rcvr); 084 rcvr = null; 085 } 086 087 @Test 088 public void testTraceCreateTable() throws Exception { 089 Table table; 090 Span createTableSpan; 091 try (TraceScope scope = TraceUtil.createTrace("creating table")) { 092 createTableSpan = scope.getSpan(); 093 table = TEST_UTIL.createTable(TableName.valueOf(name.getMethodName()), FAMILY_BYTES); 094 } 095 096 // Some table creation is async. Need to make sure that everything is full in before 097 // checking to see if the spans are there. 098 TEST_UTIL.waitFor(10000, new Waiter.Predicate<Exception>() { 099 @Override public boolean evaluate() throws Exception { 100 return (rcvr == null) ? true : rcvr.getSpans().size() >= 5; 101 } 102 }); 103 104 Collection<Span> spans = Sets.newHashSet(rcvr.getSpans()); 105 List<Span> roots = new LinkedList<>(); 106 TraceTree traceTree = new TraceTree(spans); 107 roots.addAll(traceTree.getSpansByParent().find(createTableSpan.getSpanId())); 108 109 // Roots was made 3 in hbase2. It used to be 1. We changed it back to 1 on upgrade to 110 // htrace-4.2 just to get the test to pass (traces are not wholesome in hbase2; TODO). 111 assertEquals(1, roots.size()); 112 assertEquals("creating table", createTableSpan.getDescription()); 113 114 if (spans != null) { 115 assertTrue(spans.size() > 5); 116 } 117 118 Put put = new Put("row".getBytes()); 119 put.addColumn(FAMILY_BYTES, "col".getBytes(), "value".getBytes()); 120 121 Span putSpan; 122 123 try (TraceScope scope = TraceUtil.createTrace("doing put")) { 124 putSpan = scope.getSpan(); 125 table.put(put); 126 } 127 128 spans = rcvr.getSpans(); 129 traceTree = new TraceTree(spans); 130 roots.clear(); 131 roots.addAll(traceTree.getSpansByParent().find(putSpan.getSpanId())); 132 assertEquals(1, roots.size()); 133 } 134}