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.assertTrue;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.NavigableMap;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.*;
30 import org.apache.hadoop.hbase.client.HConnection;
31 import org.apache.hadoop.hbase.client.HConnectionManager;
32 import org.apache.hadoop.hbase.client.HConnectionTestingUtility;
33 import org.apache.hadoop.hbase.client.Result;
34 import org.apache.hadoop.hbase.client.Scan;
35 import org.apache.hadoop.hbase.ipc.HRegionInterface;
36 import org.apache.hadoop.hbase.util.Bytes;
37 import org.apache.hadoop.hbase.util.Writables;
38 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43 import org.mockito.Mockito;
44
45
46
47
48
49 @Category(MediumTests.class)
50 public class TestMetaReaderEditorNoCluster {
51 private static final Log LOG = LogFactory.getLog(TestMetaReaderEditorNoCluster.class);
52 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
53 private static final Abortable ABORTABLE = new Abortable() {
54 boolean aborted = false;
55 @Override
56 public void abort(String why, Throwable e) {
57 LOG.info(why, e);
58 this.aborted = true;
59 throw new RuntimeException(e);
60 }
61 @Override
62 public boolean isAborted() {
63 return this.aborted;
64 }
65 };
66
67 @Before
68 public void before() throws Exception {
69 UTIL.startMiniZKCluster();
70 }
71
72 @After
73 public void after() throws IOException {
74 UTIL.shutdownMiniZKCluster();
75 }
76
77
78
79
80
81
82
83
84 @Test
85 public void testRideOverServerNotRunning() throws IOException, InterruptedException {
86
87 ZooKeeperWatcher zkw = new ZooKeeperWatcher(UTIL.getConfiguration(),
88 this.getClass().getSimpleName(), ABORTABLE, true);
89
90 ServerName sn = new ServerName("example.com", 1234, System.currentTimeMillis());
91
92 HConnection connection = null;
93 CatalogTracker ct = null;
94 try {
95
96
97 final HRegionInterface implementation = Mockito.mock(HRegionInterface.class);
98
99
100
101
102
103
104
105 final long scannerid = 123L;
106 Mockito.when(implementation.openScanner((byte [])Mockito.any(),
107 (Scan)Mockito.any())).
108 thenThrow(new IOException("Server not running (1 of 3)")).
109 thenThrow(new IOException("Server not running (2 of 3)")).
110 thenThrow(new IOException("Server not running (3 of 3)")).
111 thenReturn(scannerid);
112
113
114
115
116 List<KeyValue> kvs = new ArrayList<KeyValue>();
117 final byte [] rowToVerify = Bytes.toBytes("rowToVerify");
118 kvs.add(new KeyValue(rowToVerify,
119 HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER,
120 Writables.getBytes(HRegionInfo.FIRST_META_REGIONINFO)));
121 kvs.add(new KeyValue(rowToVerify,
122 HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER,
123 Bytes.toBytes(sn.getHostAndPort())));
124 kvs.add(new KeyValue(rowToVerify,
125 HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER,
126 Bytes.toBytes(sn.getStartcode())));
127 final Result [] result = new Result [] {new Result(kvs)};
128 Mockito.when(implementation.next(Mockito.anyLong(), Mockito.anyInt(), Mockito.anyInt())).
129 thenReturn(result).
130 thenReturn(null);
131
132
133
134
135 connection = HConnectionTestingUtility.getSpiedConnection(UTIL.getConfiguration());
136
137
138 final HRegionLocation anyLocation =
139 new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, sn.getHostname(),
140 sn.getPort());
141
142
143
144
145 Mockito.doReturn(anyLocation).
146 when(connection).locateRegion((byte[]) Mockito.any(), (byte[]) Mockito.any());
147 Mockito.doReturn(anyLocation).
148 when(connection).getRegionLocation((byte[]) Mockito.any(),
149 (byte[]) Mockito.any(), Mockito.anyBoolean());
150
151
152 Mockito.doReturn(implementation).
153 when(connection).getHRegionConnection(Mockito.anyString(), Mockito.anyInt());
154
155
156 ct = new CatalogTracker(zkw, null, connection, ABORTABLE);
157 ct.start();
158
159 NavigableMap<HRegionInfo, Result> hris = MetaReader.getServerUserRegions(ct, sn);
160 assertTrue(hris.size() == 1);
161 assertTrue(hris.firstEntry().getKey().equals(HRegionInfo.FIRST_META_REGIONINFO));
162 assertTrue(Bytes.equals(rowToVerify, hris.firstEntry().getValue().getRow()));
163
164
165 Mockito.verify(implementation, Mockito.times(4)).
166 openScanner((byte [])Mockito.any(), (Scan)Mockito.any());
167 } finally {
168 if (ct != null) ct.stop();
169 HConnectionManager.deleteConnection(UTIL.getConfiguration());
170 zkw.close();
171 }
172 }
173
174 @org.junit.Rule
175 public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
176 new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
177 }
178