1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.tool;
21
22 import org.apache.hadoop.hbase.HBaseTestingUtility;
23 import org.apache.hadoop.hbase.HColumnDescriptor;
24 import org.apache.hadoop.hbase.HConstants;
25 import org.apache.hadoop.hbase.HRegionInfo;
26 import org.apache.hadoop.hbase.ServerName;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.client.HTable;
29 import org.apache.hadoop.hbase.client.Put;
30 import org.apache.hadoop.hbase.client.Table;
31 import org.apache.hadoop.hbase.testclassification.MediumTests;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.apache.hadoop.util.ToolRunner;
34 import org.apache.log4j.Appender;
35 import org.apache.log4j.LogManager;
36 import org.apache.log4j.spi.LoggingEvent;
37 import com.google.common.collect.Iterables;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43 import org.junit.runner.RunWith;
44 import org.mockito.ArgumentMatcher;
45 import org.mockito.Mock;
46 import org.mockito.runners.MockitoJUnitRunner;
47
48 import java.util.concurrent.ExecutorService;
49 import java.util.concurrent.ScheduledThreadPoolExecutor;
50
51 import static org.junit.Assert.assertNotEquals;
52 import static org.mockito.Matchers.anyLong;
53 import static org.mockito.Matchers.eq;
54 import static org.mockito.Matchers.isA;
55 import static org.mockito.Matchers.argThat;
56 import static org.junit.Assert.assertEquals;
57 import static org.mockito.Mockito.*;
58
59 @RunWith(MockitoJUnitRunner.class)
60 @Category({MediumTests.class})
61 public class TestCanaryTool {
62
63 private HBaseTestingUtility testingUtility;
64 private static final byte[] FAMILY = Bytes.toBytes("f");
65 private static final byte[] COLUMN = Bytes.toBytes("col");
66
67 @Before
68 public void setUp() throws Exception {
69 testingUtility = new HBaseTestingUtility();
70 testingUtility.startMiniCluster();
71 LogManager.getRootLogger().addAppender(mockAppender);
72 }
73
74 @After
75 public void tearDown() throws Exception {
76 testingUtility.shutdownMiniCluster();
77 LogManager.getRootLogger().removeAppender(mockAppender);
78 }
79
80 @Mock
81 Appender mockAppender;
82
83 @Test
84 public void testBasicZookeeperCanaryWorks() throws Exception {
85 Integer port =
86 Iterables.getOnlyElement(testingUtility.getZkCluster().getClientPortList(), null);
87 testingUtility.getConfiguration().set(HConstants.ZOOKEEPER_QUORUM,
88 "localhost:" + port + "/hbase");
89 ExecutorService executor = new ScheduledThreadPoolExecutor(2);
90 Canary.ZookeeperStdOutSink sink = spy(new Canary.ZookeeperStdOutSink());
91 Canary canary = new Canary(executor, sink);
92 String[] args = { "-t", "10000", "-zookeeper" };
93 assertEquals(0, ToolRunner.run(testingUtility.getConfiguration(), canary, args));
94
95 String baseZnode = testingUtility.getConfiguration()
96 .get(HConstants.ZOOKEEPER_ZNODE_PARENT, HConstants.DEFAULT_ZOOKEEPER_ZNODE_PARENT);
97 verify(sink, atLeastOnce())
98 .publishReadTiming(eq(baseZnode), eq("localhost:" + port), anyLong());
99 }
100
101 @Test
102 public void testBasicCanaryWorks() throws Exception {
103 TableName tableName = TableName.valueOf("testTable");
104 HTable table = testingUtility.createTable(tableName, new byte[][] { FAMILY });
105
106 for (int i=0; i<1000; i++) {
107 byte[] iBytes = Bytes.toBytes(i);
108 Put p = new Put(iBytes);
109 p.addColumn(FAMILY, COLUMN, iBytes);
110 table.put(p);
111 }
112 ExecutorService executor = new ScheduledThreadPoolExecutor(1);
113 Canary.RegionStdOutSink sink = spy(new Canary.RegionStdOutSink());
114 Canary canary = new Canary(executor, sink);
115 String[] args = { "-writeSniffing", "-t", "10000", "testTable" };
116 assertEquals(0, ToolRunner.run(testingUtility.getConfiguration(), canary, args));
117 assertEquals("verify no read error count", 0, canary.getReadFailures().size());
118 assertEquals("verify no write error count", 0, canary.getWriteFailures().size());
119 verify(sink, atLeastOnce()).publishReadTiming(isA(ServerName.class), isA(HRegionInfo.class), isA(HColumnDescriptor.class), anyLong());
120 }
121
122 @Test
123 @Ignore("Intermittent argument matching failures, see HBASE-18813")
124 public void testReadTableTimeouts() throws Exception {
125 final TableName [] tableNames = new TableName[2];
126 tableNames[0] = TableName.valueOf("testReadTableTimeouts1");
127 tableNames[1] = TableName.valueOf("testReadTableTimeouts2");
128
129 for (int j = 0; j<2; j++) {
130 Table table = testingUtility.createTable(tableNames[j], new byte[][] { FAMILY });
131
132 for (int i=0; i<1000; i++) {
133 byte[] iBytes = Bytes.toBytes(i + j);
134 Put p = new Put(iBytes);
135 p.addColumn(FAMILY, COLUMN, iBytes);
136 table.put(p);
137 }
138 }
139 ExecutorService executor = new ScheduledThreadPoolExecutor(1);
140 Canary.RegionStdOutSink sink = spy(new Canary.RegionStdOutSink());
141 Canary canary = new Canary(executor, sink);
142 String configuredTimeoutStr = tableNames[0].getNameAsString() + "=" + Long.MAX_VALUE + "," +
143 tableNames[1].getNameAsString() + "=0";
144 String[] args = { "-readTableTimeouts", configuredTimeoutStr, tableNames[0].getNameAsString(), tableNames[1].getNameAsString()};
145 assertEquals(0, ToolRunner.run(testingUtility.getConfiguration(), canary, args));
146 verify(sink, times(tableNames.length)).initializeAndGetReadLatencyForTable(isA(String.class));
147 for (int i=0; i<2; i++) {
148 assertNotEquals("verify non-null read latency", null, sink.getReadLatencyMap().get(tableNames[i].getNameAsString()));
149 assertNotEquals("verify non-zero read latency", 0L, sink.getReadLatencyMap().get(tableNames[i].getNameAsString()));
150 }
151
152 verify(mockAppender, times(1)).doAppend(argThat(new ArgumentMatcher<LoggingEvent>() {
153 @Override
154 public boolean matches(Object argument) {
155 return ((LoggingEvent) argument).getRenderedMessage().contains("exceeded the configured read timeout.");
156 }
157 }));
158 verify(mockAppender, times(2)).doAppend(argThat(new ArgumentMatcher<LoggingEvent>() {
159 @Override
160 public boolean matches(Object argument) {
161 return ((LoggingEvent) argument).getRenderedMessage().contains("The configured read timeout was");
162 }
163 }));
164 }
165
166 @Test
167 @Ignore("Intermittent argument matching failures, see HBASE-18813")
168 public void testWriteTableTimeout() throws Exception {
169 ExecutorService executor = new ScheduledThreadPoolExecutor(1);
170 Canary.RegionStdOutSink sink = spy(new Canary.RegionStdOutSink());
171 Canary canary = new Canary(executor, sink);
172 String[] args = { "-writeSniffing", "-writeTableTimeout", String.valueOf(Long.MAX_VALUE)};
173 assertEquals(0, ToolRunner.run(testingUtility.getConfiguration(), canary, args));
174 assertNotEquals("verify non-null write latency", null, sink.getWriteLatency());
175 assertNotEquals("verify non-zero write latency", 0L, sink.getWriteLatency());
176 verify(mockAppender, times(1)).doAppend(argThat(new ArgumentMatcher<LoggingEvent>() {
177 @Override
178 public boolean matches(Object argument) {
179 return ((LoggingEvent) argument).getRenderedMessage().contains("The configured write timeout was");
180 }
181 }));
182 }
183
184
185 @Test
186 public void testRegionserverNoRegions() throws Exception {
187 runRegionserverCanary();
188 verify(mockAppender).doAppend(argThat(new ArgumentMatcher<LoggingEvent>() {
189 @Override
190 public boolean matches(Object argument) {
191 return ((LoggingEvent) argument).getRenderedMessage().contains("Regionserver not serving any regions");
192 }
193 }));
194 }
195
196
197 @Test
198 public void testRegionserverWithRegions() throws Exception {
199 TableName tableName = TableName.valueOf("testTable");
200 testingUtility.createTable(tableName, new byte[][] { FAMILY });
201 runRegionserverCanary();
202 verify(mockAppender, never()).doAppend(argThat(new ArgumentMatcher<LoggingEvent>() {
203 @Override
204 public boolean matches(Object argument) {
205 return ((LoggingEvent) argument).getRenderedMessage().contains("Regionserver not serving any regions");
206 }
207 }));
208 }
209
210 @Test
211 public void testRawScanConfig() throws Exception {
212 TableName tableName = TableName.valueOf("testTableRawScan");
213 Table table = testingUtility.createTable(tableName, new byte[][] { FAMILY });
214
215 for (int i=0; i<1000; i++) {
216 byte[] iBytes = Bytes.toBytes(i);
217 Put p = new Put(iBytes);
218 p.addColumn(FAMILY, COLUMN, iBytes);
219 table.put(p);
220 }
221 ExecutorService executor = new ScheduledThreadPoolExecutor(1);
222 Canary.RegionStdOutSink sink = spy(new Canary.RegionStdOutSink());
223 Canary canary = new Canary(executor, sink);
224 String[] args = { "-t", "10000", "testTableRawScan" };
225 org.apache.hadoop.conf.Configuration conf = new org.apache.hadoop.conf.Configuration(testingUtility.getConfiguration());
226 conf.setBoolean(HConstants.HBASE_CANARY_READ_RAW_SCAN_KEY, true);
227 assertEquals(0, ToolRunner.run(conf, canary, args));
228 verify(sink, atLeastOnce())
229 .publishReadTiming(isA(ServerName.class), isA(HRegionInfo.class), isA(HColumnDescriptor.class), anyLong());
230 assertEquals("verify no read error count", 0, canary.getReadFailures().size());
231 }
232
233 private void runRegionserverCanary() throws Exception {
234 ExecutorService executor = new ScheduledThreadPoolExecutor(1);
235 Canary canary = new Canary(executor, new Canary.RegionServerStdOutSink());
236 String[] args = { "-t", "10000", "-regionserver"};
237 assertEquals(0, ToolRunner.run(testingUtility.getConfiguration(), canary, args));
238 assertEquals("verify no read error count", 0, canary.getReadFailures().size());
239 }
240
241 }