View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.tool;
21  
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.HColumnDescriptor;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.ServerName;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.client.HTable;
30  import org.apache.hadoop.hbase.client.Put;
31  import org.apache.hadoop.hbase.master.HMaster;
32  import org.apache.hadoop.hbase.testclassification.MediumTests;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.apache.hadoop.hbase.zookeeper.ZKUtil;
35  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
36  import org.apache.hadoop.util.ToolRunner;
37  import org.apache.log4j.Appender;
38  import org.apache.log4j.LogManager;
39  import org.apache.log4j.spi.LoggingEvent;
40  import org.junit.After;
41  import org.junit.Before;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  import org.junit.runner.RunWith;
45  import org.mockito.ArgumentMatcher;
46  import org.mockito.Mock;
47  import org.mockito.runners.MockitoJUnitRunner;
48  
49  
50  import java.util.List;
51  import java.util.concurrent.ExecutorService;
52  import java.util.concurrent.ScheduledThreadPoolExecutor;
53  
54  import static org.junit.Assert.*;
55  import static org.mockito.Matchers.anyLong;
56  import static org.mockito.Matchers.isA;
57  import static org.mockito.Mockito.atLeastOnce;
58  import static org.mockito.Mockito.spy;
59  import static org.mockito.Mockito.verify;
60  import static org.mockito.Matchers.argThat;
61  import static org.mockito.Mockito.never;
62  
63  @RunWith(MockitoJUnitRunner.class)
64  @Category({MediumTests.class})
65  public class TestCanaryTool {
66  
67    private HBaseTestingUtility testingUtility;
68    private static final byte[] FAMILY = Bytes.toBytes("f");
69    private static final byte[] COLUMN = Bytes.toBytes("col");
70  
71    @Before
72    public void setUp() throws Exception {
73      testingUtility = new HBaseTestingUtility();
74      testingUtility.startMiniCluster();
75      LogManager.getRootLogger().addAppender(mockAppender);
76    }
77  
78    @After
79    public void tearDown() throws Exception {
80      testingUtility.shutdownMiniCluster();
81      LogManager.getRootLogger().removeAppender(mockAppender);
82    }
83  
84    @Mock
85    Appender mockAppender;
86  
87    @Test
88    public void testBasicCanaryWorks() throws Exception {
89      TableName tableName = TableName.valueOf("testTable");
90      HTable table = testingUtility.createTable(tableName, new byte[][] { FAMILY });
91      // insert some test rows
92      for (int i=0; i<1000; i++) {
93        byte[] iBytes = Bytes.toBytes(i);
94        Put p = new Put(iBytes);
95        p.addColumn(FAMILY, COLUMN, iBytes);
96        table.put(p);
97      }
98      ExecutorService executor = new ScheduledThreadPoolExecutor(1);
99      Canary.RegionServerStdOutSink sink = spy(new Canary.RegionServerStdOutSink());
100     Canary canary = new Canary(executor, sink);
101     String[] args = { "-t", "10000", "testTable" };
102     ToolRunner.run(testingUtility.getConfiguration(), canary, args);
103     verify(sink, atLeastOnce())
104         .publishReadTiming(isA(HRegionInfo.class), isA(HColumnDescriptor.class), anyLong());
105   }
106 
107   //no table created, so there should be no regions
108   @Test
109   public void testRegionserverNoRegions() throws Exception {
110     runRegionserverCanary();
111     verify(mockAppender).doAppend(argThat(new ArgumentMatcher<LoggingEvent>() {
112       @Override
113       public boolean matches(Object argument) {
114         return ((LoggingEvent) argument).getRenderedMessage().contains("Regionserver not serving any regions");
115       }
116     }));
117   }
118 
119   //by creating a table, there shouldn't be any region servers not serving any regions
120   @Test
121   public void testRegionserverWithRegions() throws Exception {
122     TableName tableName = TableName.valueOf("testTable");
123     testingUtility.createTable(tableName, new byte[][] { FAMILY });
124     runRegionserverCanary();
125     verify(mockAppender, never()).doAppend(argThat(new ArgumentMatcher<LoggingEvent>() {
126       @Override
127       public boolean matches(Object argument) {
128         return ((LoggingEvent) argument).getRenderedMessage().contains("Regionserver not serving any regions");
129       }
130     }));
131   }
132 
133   private void runRegionserverCanary() throws Exception {
134     ExecutorService executor = new ScheduledThreadPoolExecutor(1);
135     Canary canary = new Canary(executor, new Canary.RegionServerStdOutSink());
136     String[] args = { "-t", "10000", "-regionserver"};
137     ToolRunner.run(testingUtility.getConfiguration(), canary, args);
138   }
139 
140 }
141