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