1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.wal;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.IOException;
27 import java.util.HashSet;
28 import java.util.Random;
29 import java.util.Set;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.hadoop.conf.Configuration;
34 import org.apache.hadoop.fs.FileStatus;
35 import org.apache.hadoop.fs.FileSystem;
36 import org.apache.hadoop.fs.Path;
37 import org.apache.hadoop.hbase.HBaseTestingUtility;
38 import org.apache.hadoop.hbase.HColumnDescriptor;
39 import org.apache.hadoop.hbase.HConstants;
40 import org.apache.hadoop.hbase.HRegionInfo;
41 import org.apache.hadoop.hbase.HTableDescriptor;
42 import org.apache.hadoop.hbase.KeyValue;
43 import org.apache.hadoop.hbase.testclassification.MediumTests;
44 import org.apache.hadoop.hbase.ServerName;
45 import org.apache.hadoop.hbase.TableName;
46 import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl;
47 import org.apache.hadoop.hbase.util.Bytes;
48 import org.apache.hadoop.hbase.util.FSUtils;
49 import org.junit.After;
50 import org.junit.AfterClass;
51 import org.junit.Before;
52 import org.junit.BeforeClass;
53 import org.junit.Rule;
54 import org.junit.Test;
55 import org.junit.experimental.categories.Category;
56 import org.junit.rules.TestName;
57
58
59 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
60
61 @Category(MediumTests.class)
62 public class TestDefaultWALProvider {
63 private static final Log LOG = LogFactory.getLog(TestDefaultWALProvider.class);
64
65 protected static Configuration conf;
66 protected static FileSystem fs;
67 protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
68 protected MultiVersionConcurrencyControl mvcc;
69
70 @Rule
71 public final TestName currentTest = new TestName();
72
73 @Before
74 public void setUp() throws Exception {
75 mvcc = new MultiVersionConcurrencyControl();
76 FileStatus[] entries = fs.listStatus(new Path("/"));
77 for (FileStatus dir : entries) {
78 fs.delete(dir.getPath(), true);
79 }
80 }
81
82 @After
83 public void tearDown() throws Exception {
84 }
85
86 @BeforeClass
87 public static void setUpBeforeClass() throws Exception {
88
89 TEST_UTIL.getConfiguration().setInt("dfs.blocksize", 1024 * 1024);
90
91 TEST_UTIL.getConfiguration().setInt("dfs.namenode.heartbeat.recheck-interval", 5000);
92 TEST_UTIL.getConfiguration().setInt("dfs.heartbeat.interval", 1);
93 TEST_UTIL.getConfiguration().setInt("dfs.client.socket-timeout", 5000);
94
95
96 TEST_UTIL.getConfiguration()
97 .setInt("hbase.ipc.client.connect.max.retries", 1);
98 TEST_UTIL.getConfiguration().setInt(
99 "dfs.client.block.recovery.retries", 1);
100 TEST_UTIL.getConfiguration().setInt(
101 "hbase.ipc.client.connection.maxidletime", 500);
102 TEST_UTIL.startMiniDFSCluster(3);
103
104
105 TEST_UTIL.createRootDir();
106 conf = TEST_UTIL.getConfiguration();
107 fs = TEST_UTIL.getDFSCluster().getFileSystem();
108 }
109
110 @AfterClass
111 public static void tearDownAfterClass() throws Exception {
112 TEST_UTIL.shutdownMiniCluster();
113 }
114
115 static String getName() {
116 return "TestDefaultWALProvider";
117 }
118
119 @Test
120 public void testGetServerNameFromWALDirectoryName() throws IOException {
121 ServerName sn = ServerName.valueOf("hn", 450, 1398);
122 String hl = FSUtils.getRootDir(conf) + "/" +
123 DefaultWALProvider.getWALDirectoryName(sn.toString());
124
125
126 assertNull(DefaultWALProvider.getServerNameFromWALDirectoryName(conf, null));
127 assertNull(DefaultWALProvider.getServerNameFromWALDirectoryName(conf,
128 FSUtils.getRootDir(conf).toUri().toString()));
129 assertNull(DefaultWALProvider.getServerNameFromWALDirectoryName(conf, ""));
130 assertNull(DefaultWALProvider.getServerNameFromWALDirectoryName(conf, " "));
131 assertNull(DefaultWALProvider.getServerNameFromWALDirectoryName(conf, hl));
132 assertNull(DefaultWALProvider.getServerNameFromWALDirectoryName(conf, hl + "qdf"));
133 assertNull(DefaultWALProvider.getServerNameFromWALDirectoryName(conf, "sfqf" + hl + "qdf"));
134
135 final String wals = "/WALs/";
136 ServerName parsed = DefaultWALProvider.getServerNameFromWALDirectoryName(conf,
137 FSUtils.getRootDir(conf).toUri().toString() + wals + sn +
138 "/localhost%2C32984%2C1343316388997.1343316390417");
139 assertEquals("standard", sn, parsed);
140
141 parsed = DefaultWALProvider.getServerNameFromWALDirectoryName(conf, hl + "/qdf");
142 assertEquals("subdir", sn, parsed);
143
144 parsed = DefaultWALProvider.getServerNameFromWALDirectoryName(conf,
145 FSUtils.getRootDir(conf).toUri().toString() + wals + sn +
146 "-splitting/localhost%3A57020.1340474893931");
147 assertEquals("split", sn, parsed);
148 }
149
150 protected void addEdits(WAL log, HRegionInfo hri, HTableDescriptor htd,
151 int times) throws IOException {
152 final byte[] row = Bytes.toBytes("row");
153 for (int i = 0; i < times; i++) {
154 long timestamp = System.currentTimeMillis();
155 WALEdit cols = new WALEdit();
156 cols.add(new KeyValue(row, row, row, timestamp, row));
157 log.append(htd, hri, getWalKey(hri.getEncodedNameAsBytes(), htd.getTableName(), timestamp),
158 cols, true);
159 }
160 log.sync();
161 }
162
163
164
165
166 WALKey getWalKey(final byte[] info, final TableName tableName, final long timestamp) {
167 return new WALKey(info, tableName, timestamp, mvcc);
168 }
169
170
171
172
173
174
175 protected void flushRegion(WAL wal, byte[] regionEncodedName, Set<byte[]> flushedFamilyNames) {
176 wal.startCacheFlush(regionEncodedName, flushedFamilyNames);
177 wal.completeCacheFlush(regionEncodedName);
178 }
179
180 private static final byte[] UNSPECIFIED_REGION = new byte[]{};
181
182 @Test
183 public void testLogCleaning() throws Exception {
184 LOG.info("testLogCleaning");
185 final HTableDescriptor htd =
186 new HTableDescriptor(TableName.valueOf("testLogCleaning")).addFamily(new HColumnDescriptor(
187 "row"));
188 final HTableDescriptor htd2 =
189 new HTableDescriptor(TableName.valueOf("testLogCleaning2"))
190 .addFamily(new HColumnDescriptor("row"));
191 final Configuration localConf = new Configuration(conf);
192 localConf.set(WALFactory.WAL_PROVIDER, DefaultWALProvider.class.getName());
193 final WALFactory wals = new WALFactory(localConf, null, currentTest.getMethodName());
194 try {
195 HRegionInfo hri = new HRegionInfo(htd.getTableName(),
196 HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW);
197 HRegionInfo hri2 = new HRegionInfo(htd2.getTableName(),
198 HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW);
199
200 final WAL log = wals.getWAL(UNSPECIFIED_REGION, null);
201
202
203
204 addEdits(log, hri, htd, 1);
205 log.rollWriter();
206 assertEquals(1, DefaultWALProvider.getNumRolledLogFiles(log));
207
208
209 addEdits(log, hri, htd, 2);
210 log.rollWriter();
211 assertEquals(2, DefaultWALProvider.getNumRolledLogFiles(log));
212
213
214 addEdits(log, hri, htd, 1);
215 addEdits(log, hri2, htd2, 1);
216 addEdits(log, hri, htd, 1);
217 addEdits(log, hri2, htd2, 1);
218 log.rollWriter();
219 assertEquals(3, DefaultWALProvider.getNumRolledLogFiles(log));
220
221
222
223 addEdits(log, hri2, htd2, 1);
224 log.startCacheFlush(hri.getEncodedNameAsBytes(), htd.getFamiliesKeys());
225 log.completeCacheFlush(hri.getEncodedNameAsBytes());
226 log.rollWriter();
227 assertEquals(2, DefaultWALProvider.getNumRolledLogFiles(log));
228
229
230
231
232 addEdits(log, hri2, htd2, 1);
233 log.startCacheFlush(hri2.getEncodedNameAsBytes(), htd2.getFamiliesKeys());
234 log.completeCacheFlush(hri2.getEncodedNameAsBytes());
235 log.rollWriter();
236 assertEquals(0, DefaultWALProvider.getNumRolledLogFiles(log));
237 } finally {
238 if (wals != null) {
239 wals.close();
240 }
241 }
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256 @Test
257 public void testWALArchiving() throws IOException {
258 LOG.debug("testWALArchiving");
259 HTableDescriptor table1 =
260 new HTableDescriptor(TableName.valueOf("t1")).addFamily(new HColumnDescriptor("row"));
261 HTableDescriptor table2 =
262 new HTableDescriptor(TableName.valueOf("t2")).addFamily(new HColumnDescriptor("row"));
263 final Configuration localConf = new Configuration(conf);
264 localConf.set(WALFactory.WAL_PROVIDER, DefaultWALProvider.class.getName());
265 final WALFactory wals = new WALFactory(localConf, null, currentTest.getMethodName());
266 try {
267 final WAL wal = wals.getWAL(UNSPECIFIED_REGION, null);
268 assertEquals(0, DefaultWALProvider.getNumRolledLogFiles(wal));
269 HRegionInfo hri1 =
270 new HRegionInfo(table1.getTableName(), HConstants.EMPTY_START_ROW,
271 HConstants.EMPTY_END_ROW);
272 HRegionInfo hri2 =
273 new HRegionInfo(table2.getTableName(), HConstants.EMPTY_START_ROW,
274 HConstants.EMPTY_END_ROW);
275
276 hri1.setSplit(false);
277 hri2.setSplit(false);
278
279
280 addEdits(wal, hri1, table1, 1);
281 wal.rollWriter();
282
283 assertEquals(1, DefaultWALProvider.getNumRolledLogFiles(wal));
284
285 addEdits(wal, hri1, table1, 1);
286 wal.rollWriter();
287
288 assertEquals(2, DefaultWALProvider.getNumRolledLogFiles(wal));
289
290 addEdits(wal, hri1, table1, 3);
291 flushRegion(wal, hri1.getEncodedNameAsBytes(), table1.getFamiliesKeys());
292
293 wal.rollWriter();
294 assertEquals(0, DefaultWALProvider.getNumRolledLogFiles(wal));
295
296 addEdits(wal, hri2, table2, 1);
297 wal.rollWriter();
298 assertEquals(1, DefaultWALProvider.getNumRolledLogFiles(wal));
299
300 addEdits(wal, hri1, table1, 2);
301 wal.rollWriter();
302 assertEquals(2, DefaultWALProvider.getNumRolledLogFiles(wal));
303
304 addEdits(wal, hri2, table2, 2);
305 flushRegion(wal, hri1.getEncodedNameAsBytes(), table2.getFamiliesKeys());
306
307
308
309
310
311 wal.rollWriter();
312 assertEquals(2, DefaultWALProvider.getNumRolledLogFiles(wal));
313
314 addEdits(wal, hri2, table2, 2);
315 flushRegion(wal, hri2.getEncodedNameAsBytes(), table2.getFamiliesKeys());
316 wal.rollWriter();
317 assertEquals(0, DefaultWALProvider.getNumRolledLogFiles(wal));
318 } finally {
319 if (wals != null) {
320 wals.close();
321 }
322 }
323 }
324
325
326
327
328
329 @Test
330 public void testConcurrentWrites() throws Exception {
331
332
333 int errCode = WALPerformanceEvaluation.
334 innerMain(new Configuration(TEST_UTIL.getConfiguration()),
335 new String [] {"-threads", "3", "-verify", "-noclosefs", "-iterations", "3000"});
336 assertEquals(0, errCode);
337 }
338
339
340
341
342 @Test
343 public void setMembershipDedups() throws IOException {
344 final Configuration localConf = new Configuration(conf);
345 localConf.set(WALFactory.WAL_PROVIDER, DefaultWALProvider.class.getName());
346 final WALFactory wals = new WALFactory(localConf, null, currentTest.getMethodName());
347 try {
348 final Set<WAL> seen = new HashSet<WAL>(1);
349 final Random random = new Random();
350 assertTrue("first attempt to add WAL from default provider should work.",
351 seen.add(wals.getWAL(Bytes.toBytes(random.nextInt()), null)));
352 for (int i = 0; i < 1000; i++) {
353 assertFalse("default wal provider is only supposed to return a single wal, which should "
354 + "compare as .equals itself.",
355 seen.add(wals.getWAL(Bytes.toBytes(random.nextInt()), null)));
356 }
357 } finally {
358 wals.close();
359 }
360 }
361 }