1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.catalog;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.NavigableMap;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.hbase.Abortable;
33 import org.apache.hadoop.hbase.CellScannable;
34 import org.apache.hadoop.hbase.CellUtil;
35 import org.apache.hadoop.hbase.TableName;
36 import org.apache.hadoop.hbase.HBaseTestingUtility;
37 import org.apache.hadoop.hbase.HConstants;
38 import org.apache.hadoop.hbase.HRegionInfo;
39 import org.apache.hadoop.hbase.HRegionLocation;
40 import org.apache.hadoop.hbase.KeyValue;
41 import org.apache.hadoop.hbase.MediumTests;
42 import org.apache.hadoop.hbase.ServerName;
43 import org.apache.hadoop.hbase.client.HConnection;
44 import org.apache.hadoop.hbase.client.HConnectionManager;
45 import org.apache.hadoop.hbase.client.HConnectionTestingUtility;
46 import org.apache.hadoop.hbase.client.Result;
47 import org.apache.hadoop.hbase.ipc.PayloadCarryingRpcController;
48 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
49 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ResultCellMeta;
50 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
51 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanResponse;
52 import org.apache.hadoop.hbase.util.Bytes;
53 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
54 import org.junit.After;
55 import org.junit.Before;
56 import org.junit.Test;
57 import org.junit.experimental.categories.Category;
58 import org.mockito.Mockito;
59 import org.mockito.invocation.InvocationOnMock;
60 import org.mockito.stubbing.Answer;
61
62 import com.google.protobuf.RpcController;
63 import com.google.protobuf.ServiceException;
64
65
66
67
68
69 @Category(MediumTests.class)
70 public class TestMetaReaderEditorNoCluster {
71 private static final Log LOG = LogFactory.getLog(TestMetaReaderEditorNoCluster.class);
72 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
73 private static final Abortable ABORTABLE = new Abortable() {
74 boolean aborted = false;
75 @Override
76 public void abort(String why, Throwable e) {
77 LOG.info(why, e);
78 this.aborted = true;
79 throw new RuntimeException(e);
80 }
81 @Override
82 public boolean isAborted() {
83 return this.aborted;
84 }
85 };
86
87 @Before
88 public void before() throws Exception {
89 UTIL.startMiniZKCluster();
90 }
91
92 @After
93 public void after() throws IOException {
94 UTIL.shutdownMiniZKCluster();
95 }
96
97 @Test
98 public void testGetHRegionInfo() throws IOException {
99 assertNull(HRegionInfo.getHRegionInfo(new Result()));
100
101 List<KeyValue> kvs = new ArrayList<KeyValue>();
102 Result r = new Result(kvs);
103 assertNull(HRegionInfo.getHRegionInfo(r));
104
105 byte [] f = HConstants.CATALOG_FAMILY;
106
107 kvs.add(new KeyValue(HConstants.EMPTY_BYTE_ARRAY, f,
108 HConstants.SERVER_QUALIFIER, f));
109 r = new Result(kvs);
110 assertNull(HRegionInfo.getHRegionInfo(r));
111
112 kvs.add(new KeyValue(HConstants.EMPTY_BYTE_ARRAY, f,
113 HConstants.REGIONINFO_QUALIFIER, f));
114 HRegionInfo hri = HRegionInfo.getHRegionInfo(new Result(kvs));
115 assertTrue(hri == null);
116
117 kvs.clear();
118 kvs.add(new KeyValue(HConstants.EMPTY_BYTE_ARRAY, f,
119 HConstants.REGIONINFO_QUALIFIER,
120 HRegionInfo.FIRST_META_REGIONINFO.toByteArray()));
121 hri = HRegionInfo.getHRegionInfo(new Result(kvs));
122 assertNotNull(hri);
123 assertTrue(hri.equals(HRegionInfo.FIRST_META_REGIONINFO));
124 }
125
126
127
128
129
130
131
132
133 @Test
134 public void testRideOverServerNotRunning()
135 throws IOException, InterruptedException, ServiceException {
136
137 ZooKeeperWatcher zkw = new ZooKeeperWatcher(UTIL.getConfiguration(),
138 this.getClass().getSimpleName(), ABORTABLE, true);
139
140 ServerName sn = new ServerName("example.com", 1234, System.currentTimeMillis());
141
142 HConnection connection;
143 CatalogTracker ct = null;
144 try {
145
146
147 final ClientProtos.ClientService.BlockingInterface implementation =
148 Mockito.mock(ClientProtos.ClientService.BlockingInterface.class);
149
150
151
152
153
154
155
156 List<KeyValue> kvs = new ArrayList<KeyValue>();
157 final byte [] rowToVerify = Bytes.toBytes("rowToVerify");
158 kvs.add(new KeyValue(rowToVerify,
159 HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
160 HRegionInfo.FIRST_META_REGIONINFO.toByteArray()));
161 kvs.add(new KeyValue(rowToVerify,
162 HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
163 Bytes.toBytes(sn.getHostAndPort())));
164 kvs.add(new KeyValue(rowToVerify,
165 HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
166 Bytes.toBytes(sn.getStartcode())));
167 final List<CellScannable> cellScannables = new ArrayList<CellScannable>(1);
168 cellScannables.add(new Result(kvs));
169 final ScanResponse.Builder builder = ScanResponse.newBuilder();
170 ResultCellMeta.Builder metaBuilder = ResultCellMeta.newBuilder();
171 for (CellScannable result : cellScannables) {
172 metaBuilder.addCellsLength(((Result)result).size());
173 }
174 builder.setResultCellMeta(metaBuilder.build());
175 Mockito.when(implementation.scan((RpcController) Mockito.any(), (ScanRequest) Mockito.any()))
176 .thenThrow(new ServiceException("Server not running (1 of 3)"))
177 .thenThrow(new ServiceException("Server not running (2 of 3)"))
178 .thenThrow(new ServiceException("Server not running (3 of 3)"))
179 .thenReturn(ScanResponse.newBuilder().setScannerId(1234567890L).build())
180 .thenAnswer(new Answer<ScanResponse>() {
181 public ScanResponse answer(InvocationOnMock invocation) throws Throwable {
182 ((PayloadCarryingRpcController) invocation.getArguments()[0]).setCellScanner(CellUtil
183 .createCellScanner(cellScannables));
184 return builder.build();
185 }
186 }).thenReturn(ScanResponse.newBuilder().setMoreResults(false).build());
187
188
189
190 connection = HConnectionTestingUtility.getSpiedConnection(UTIL.getConfiguration());
191
192
193 final HRegionLocation anyLocation =
194 new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, sn);
195
196
197
198
199 Mockito.doReturn(anyLocation).
200 when(connection).locateRegion((TableName) Mockito.any(), (byte[]) Mockito.any());
201 Mockito.doReturn(anyLocation).
202 when(connection).getRegionLocation((TableName) Mockito.any(),
203 (byte[]) Mockito.any(), Mockito.anyBoolean());
204
205
206 Mockito.doReturn(implementation).
207 when(connection).getClient(Mockito.any(ServerName.class));
208
209
210 ct = new CatalogTracker(zkw, null, connection, ABORTABLE);
211 ct.start();
212
213 NavigableMap<HRegionInfo, Result> hris = MetaReader.getServerUserRegions(ct, sn);
214 assertEquals(1, hris.size());
215 assertTrue(hris.firstEntry().getKey().equals(HRegionInfo.FIRST_META_REGIONINFO));
216 assertTrue(Bytes.equals(rowToVerify, hris.firstEntry().getValue().getRow()));
217
218
219 Mockito.verify(implementation, Mockito.times(6)).
220 scan((RpcController)Mockito.any(), (ScanRequest)Mockito.any());
221 } finally {
222 if (ct != null) ct.stop();
223 HConnectionManager.deleteConnection(UTIL.getConfiguration());
224 zkw.close();
225 }
226 }
227 }