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