View Javadoc

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.trace;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.util.Collection;
25  
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.testclassification.MediumTests;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.Waiter;
30  import org.apache.hadoop.hbase.client.Put;
31  import org.apache.hadoop.hbase.client.Table;
32  import org.apache.htrace.Sampler;
33  import org.apache.htrace.Span;
34  import org.apache.htrace.Trace;
35  import org.apache.htrace.TraceScope;
36  import org.apache.htrace.TraceTree;
37  import org.apache.htrace.impl.POJOSpanReceiver;
38  import org.junit.AfterClass;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  import com.google.common.collect.Multimap;
44  
45  @Category(MediumTests.class)
46  public class TestHTraceHooks {
47  
48    private static final byte[] FAMILY_BYTES = "family".getBytes();
49    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
50    private static POJOSpanReceiver rcvr;
51  
52    @BeforeClass
53    public static void before() throws Exception {
54      TEST_UTIL.startMiniCluster(2, 3);
55      rcvr = new POJOSpanReceiver(new HBaseHTraceConfiguration(TEST_UTIL.getConfiguration()));
56      Trace.addReceiver(rcvr);
57    }
58  
59    @AfterClass
60    public static void after() throws Exception {
61      TEST_UTIL.shutdownMiniCluster();
62      Trace.removeReceiver(rcvr);
63      rcvr = null;
64    }
65  
66    @Test
67    public void testTraceCreateTable() throws Exception {
68      TraceScope tableCreationSpan = Trace.startSpan("creating table", Sampler.ALWAYS);
69      Table table;
70      try {
71  
72        table = TEST_UTIL.createTable(TableName.valueOf("table"),
73          FAMILY_BYTES);
74      } finally {
75        tableCreationSpan.close();
76      }
77  
78      // Some table creation is async.  Need to make sure that everything is full in before
79      // checking to see if the spans are there.
80      TEST_UTIL.waitFor(1000, new Waiter.Predicate<Exception>() {
81        @Override
82        public boolean evaluate() throws Exception {
83          return rcvr.getSpans().size() >= 5;
84        }
85      });
86  
87      Collection<Span> spans = rcvr.getSpans();
88      TraceTree traceTree = new TraceTree(spans);
89      Collection<Span> roots = traceTree.getSpansByParent().find(Span.ROOT_SPAN_ID);
90  
91      assertEquals(1, roots.size());
92      Span createTableRoot = roots.iterator().next();
93  
94      assertEquals("creating table", createTableRoot.getDescription());
95  
96      int createTableCount = 0;
97  
98      for (Span s : traceTree.getSpansByParent().find(createTableRoot.getSpanId())) {
99        if (s.getDescription().startsWith("MasterService.CreateTable")) {
100         createTableCount++;
101       }
102     }
103 
104     assertTrue(createTableCount >= 1);
105     assertTrue(traceTree.getSpansByParent().find(createTableRoot.getSpanId()).size() > 3);
106     assertTrue(spans.size() > 5);
107 
108     Put put = new Put("row".getBytes());
109     put.add(FAMILY_BYTES, "col".getBytes(), "value".getBytes());
110 
111     TraceScope putSpan = Trace.startSpan("doing put", Sampler.ALWAYS);
112     try {
113       table.put(put);
114     } finally {
115       putSpan.close();
116     }
117 
118     spans = rcvr.getSpans();
119     traceTree = new TraceTree(spans);
120     roots = traceTree.getSpansByParent().find(Span.ROOT_SPAN_ID);
121 
122     assertEquals(2, roots.size());
123     Span putRoot = null;
124     for (Span root : roots) {
125       if (root.getDescription().equals("doing put")) {
126         putRoot = root;
127       }
128     }
129 
130     assertNotNull(putRoot);
131   }
132 }