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.MediumTests;
28  import org.apache.hadoop.hbase.client.HTable;
29  import org.apache.hadoop.hbase.client.Put;
30  import org.cloudera.htrace.Sampler;
31  import org.cloudera.htrace.Span;
32  import org.cloudera.htrace.Trace;
33  import org.cloudera.htrace.TraceScope;
34  import org.cloudera.htrace.TraceTree;
35  import org.cloudera.htrace.impl.POJOSpanReceiver;
36  import org.junit.AfterClass;
37  import org.junit.BeforeClass;
38  import org.junit.Test;
39  import org.junit.experimental.categories.Category;
40  
41  import com.google.common.collect.Multimap;
42  
43  @Category(MediumTests.class)
44  public class TestHTraceHooks {
45  
46    private static final byte[] FAMILY_BYTES = "family".getBytes();
47    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
48    private static final POJOSpanReceiver rcvr = new POJOSpanReceiver();
49  
50    @BeforeClass
51    public static void before() throws Exception {
52      TEST_UTIL.startMiniCluster(2, 3);
53      Trace.addReceiver(rcvr);
54    }
55  
56    @AfterClass
57    public static void after() throws Exception {
58      TEST_UTIL.shutdownMiniCluster();
59      Trace.removeReceiver(rcvr);
60    }
61  
62    @Test
63    public void testTraceCreateTable() throws Exception {
64      TraceScope tableCreationSpan = Trace.startSpan("creating table", Sampler.ALWAYS);
65      HTable table; 
66      try {
67  
68        table = TEST_UTIL.createTable("table".getBytes(),
69          FAMILY_BYTES);
70      } finally {
71        tableCreationSpan.close();
72      }
73  
74      Collection<Span> spans = rcvr.getSpans();
75      TraceTree traceTree = new TraceTree(spans);
76      Collection<Span> roots = traceTree.getRoots();
77  
78      assertEquals(1, roots.size());
79      Span createTableRoot = roots.iterator().next();
80  
81      assertEquals("creating table", createTableRoot.getDescription());
82      Multimap<Long, Span> spansByParentIdMap = traceTree
83          .getSpansByParentIdMap();
84  
85      int createTableCount = 0;
86  
87      for (Span s : spansByParentIdMap.get(createTableRoot.getSpanId())) {
88        if (s.getDescription().startsWith("MasterAdminService.CreateTable")) {
89          createTableCount++;
90        }
91      }
92  
93      assertTrue(createTableCount >= 1);
94      assertTrue(spansByParentIdMap.get(createTableRoot.getSpanId()).size() > 3);
95      assertTrue(spans.size() > 5);
96      
97      Put put = new Put("row".getBytes());
98      put.add(FAMILY_BYTES, "col".getBytes(), "value".getBytes());
99  
100     TraceScope putSpan = Trace.startSpan("doing put", Sampler.ALWAYS);
101     try {
102       table.put(put);
103     } finally {
104       putSpan.close();
105     }
106 
107     spans = rcvr.getSpans();
108     traceTree = new TraceTree(spans);
109     roots = traceTree.getRoots();
110 
111     assertEquals(2, roots.size());
112     Span putRoot = null;
113     for (Span root : roots) {
114       if (root.getDescription().equals("doing put")) {
115         putRoot = root;
116       }
117     }
118     
119     assertNotNull(putRoot);
120   }
121 }