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.coprocessor;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.io.IOException;
28  import java.security.PrivilegedExceptionAction;
29  import java.util.Arrays;
30  import java.util.List;
31  import java.util.Map;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.hadoop.conf.Configuration;
36  import org.apache.hadoop.fs.FileSystem;
37  import org.apache.hadoop.fs.Path;
38  import org.apache.hadoop.hbase.Cell;
39  import org.apache.hadoop.hbase.Coprocessor;
40  import org.apache.hadoop.hbase.HBaseConfiguration;
41  import org.apache.hadoop.hbase.HBaseTestingUtility;
42  import org.apache.hadoop.hbase.HColumnDescriptor;
43  import org.apache.hadoop.hbase.HConstants;
44  import org.apache.hadoop.hbase.HRegionInfo;
45  import org.apache.hadoop.hbase.HTableDescriptor;
46  import org.apache.hadoop.hbase.KeyValue;
47  import org.apache.hadoop.hbase.testclassification.MediumTests;
48  import org.apache.hadoop.hbase.TableName;
49  import org.apache.hadoop.hbase.client.Put;
50  import org.apache.hadoop.hbase.regionserver.HRegion;
51  import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl;
52  import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
53  import org.apache.hadoop.hbase.regionserver.wal.WALCoprocessorHost;
54  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
55  import org.apache.hadoop.hbase.wal.DefaultWALProvider;
56  import org.apache.hadoop.hbase.wal.WAL;
57  import org.apache.hadoop.hbase.wal.WALFactory;
58  import org.apache.hadoop.hbase.wal.WALKey;
59  import org.apache.hadoop.hbase.wal.WALSplitter;
60  import org.apache.hadoop.hbase.security.User;
61  import org.apache.hadoop.hbase.util.Bytes;
62  import org.apache.hadoop.hbase.util.EnvironmentEdge;
63  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
64  import org.apache.hadoop.hbase.util.FSUtils;
65  import org.junit.After;
66  import org.junit.AfterClass;
67  import org.junit.Before;
68  import org.junit.BeforeClass;
69  import org.junit.Rule;
70  import org.junit.Test;
71  import org.junit.rules.TestName;
72  import org.junit.experimental.categories.Category;
73  
74  /**
75   * Tests invocation of the
76   * {@link org.apache.hadoop.hbase.coprocessor.MasterObserver} interface hooks at
77   * all appropriate times during normal HMaster operations.
78   */
79  @Category(MediumTests.class)
80  public class TestWALObserver {
81    private static final Log LOG = LogFactory.getLog(TestWALObserver.class);
82    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
83  
84    private static byte[] TEST_TABLE = Bytes.toBytes("observedTable");
85    private static byte[][] TEST_FAMILY = { Bytes.toBytes("fam1"),
86        Bytes.toBytes("fam2"), Bytes.toBytes("fam3"), };
87    private static byte[][] TEST_QUALIFIER = { Bytes.toBytes("q1"),
88        Bytes.toBytes("q2"), Bytes.toBytes("q3"), };
89    private static byte[][] TEST_VALUE = { Bytes.toBytes("v1"),
90        Bytes.toBytes("v2"), Bytes.toBytes("v3"), };
91    private static byte[] TEST_ROW = Bytes.toBytes("testRow");
92  
93    @Rule
94    public TestName currentTest = new TestName();
95  
96    private Configuration conf;
97    private FileSystem fs;
98    private Path dir;
99    private Path hbaseRootDir;
100   private Path hbaseWALRootDir;
101   private String logName;
102   private Path oldLogDir;
103   private Path logDir;
104   private WALFactory wals;
105 
106   @BeforeClass
107   public static void setupBeforeClass() throws Exception {
108     Configuration conf = TEST_UTIL.getConfiguration();
109     conf.setStrings(CoprocessorHost.WAL_COPROCESSOR_CONF_KEY,
110         SampleRegionWALObserver.class.getName(), SampleRegionWALObserver.Legacy.class.getName());
111     conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
112         SampleRegionWALObserver.class.getName());
113     conf.setBoolean("dfs.support.append", true);
114     conf.setInt("dfs.client.block.recovery.retries", 2);
115 
116     TEST_UTIL.startMiniCluster(1);
117     Path hbaseRootDir = TEST_UTIL.getDFSCluster().getFileSystem()
118         .makeQualified(new Path("/hbase"));
119     Path hbaseWALRootDir = TEST_UTIL.getDFSCluster().getFileSystem()
120             .makeQualified(new Path("/hbaseLogRoot"));
121     LOG.info("hbase.rootdir=" + hbaseRootDir);
122     FSUtils.setRootDir(conf, hbaseRootDir);
123     FSUtils.setWALRootDir(conf, hbaseWALRootDir);
124   }
125 
126   @AfterClass
127   public static void teardownAfterClass() throws Exception {
128     TEST_UTIL.shutdownMiniCluster();
129   }
130 
131   @Before
132   public void setUp() throws Exception {
133     this.conf = HBaseConfiguration.create(TEST_UTIL.getConfiguration());
134     // this.cluster = TEST_UTIL.getDFSCluster();
135     this.fs = TEST_UTIL.getDFSCluster().getFileSystem();
136     this.hbaseRootDir = FSUtils.getRootDir(conf);
137     this.hbaseWALRootDir = FSUtils.getWALRootDir(conf);
138     this.dir = new Path(this.hbaseRootDir, TestWALObserver.class.getName());
139     this.oldLogDir = new Path(this.hbaseWALRootDir,
140         HConstants.HREGION_OLDLOGDIR_NAME);
141     this.logDir = new Path(this.hbaseWALRootDir,
142         DefaultWALProvider.getWALDirectoryName(currentTest.getMethodName()));
143     this.logName = HConstants.HREGION_LOGDIR_NAME;
144 
145     if (TEST_UTIL.getDFSCluster().getFileSystem().exists(this.hbaseRootDir)) {
146       TEST_UTIL.getDFSCluster().getFileSystem().delete(this.hbaseRootDir, true);
147     }
148     if (TEST_UTIL.getDFSCluster().getFileSystem().exists(this.hbaseWALRootDir)) {
149       TEST_UTIL.getDFSCluster().getFileSystem().delete(this.hbaseWALRootDir, true);
150     }
151     this.wals = new WALFactory(conf, null, currentTest.getMethodName());
152   }
153 
154   @After
155   public void tearDown() throws Exception {
156     try {
157       wals.shutdown();
158     } catch (IOException exception) {
159       // one of our tests splits out from under our wals.
160       LOG.warn("Ignoring failure to close wal factory. " + exception.getMessage());
161       LOG.debug("details of failure to close wal factory.", exception);
162     }
163     TEST_UTIL.getDFSCluster().getFileSystem().delete(this.hbaseRootDir, true);
164     TEST_UTIL.getDFSCluster().getFileSystem().delete(this.hbaseWALRootDir, true);
165   }
166 
167   /**
168    * Test WAL write behavior with WALObserver. The coprocessor monitors a
169    * WALEdit written to WAL, and ignore, modify, and add KeyValue's for the
170    * WALEdit.
171    */
172   @Test
173   public void testWALObserverWriteToWAL() throws Exception {
174     final WAL log = wals.getWAL(UNSPECIFIED_REGION, null);
175     verifyWritesSeen(log, getCoprocessor(log, SampleRegionWALObserver.class), false);
176   }
177 
178   /**
179    * Test WAL write behavior with WALObserver. The coprocessor monitors a
180    * WALEdit written to WAL, and ignore, modify, and add KeyValue's for the
181    * WALEdit.
182    */
183   @Test
184   public void testLegacyWALObserverWriteToWAL() throws Exception {
185     final WAL log = wals.getWAL(UNSPECIFIED_REGION, null);
186     verifyWritesSeen(log, getCoprocessor(log, SampleRegionWALObserver.Legacy.class), true);
187   }
188 
189   private void verifyWritesSeen(final WAL log, final SampleRegionWALObserver cp,
190       final boolean seesLegacy) throws Exception {
191     HRegionInfo hri = createBasic3FamilyHRegionInfo(Bytes.toString(TEST_TABLE));
192     final HTableDescriptor htd = createBasic3FamilyHTD(Bytes
193         .toString(TEST_TABLE));
194 
195     Path basedir = new Path(this.hbaseRootDir, Bytes.toString(TEST_TABLE));
196     deleteDir(basedir);
197     fs.mkdirs(new Path(basedir, hri.getEncodedName()));
198 
199     // TEST_FAMILY[0] shall be removed from WALEdit.
200     // TEST_FAMILY[1] value shall be changed.
201     // TEST_FAMILY[2] shall be added to WALEdit, although it's not in the put.
202     cp.setTestValues(TEST_TABLE, TEST_ROW, TEST_FAMILY[0], TEST_QUALIFIER[0],
203         TEST_FAMILY[1], TEST_QUALIFIER[1], TEST_FAMILY[2], TEST_QUALIFIER[2]);
204 
205     assertFalse(cp.isPreWALWriteCalled());
206     assertFalse(cp.isPostWALWriteCalled());
207     assertFalse(cp.isPreWALWriteDeprecatedCalled());
208     assertFalse(cp.isPostWALWriteDeprecatedCalled());
209 
210     // TEST_FAMILY[2] is not in the put, however it shall be added by the tested
211     // coprocessor.
212     // Use a Put to create familyMap.
213     Put p = creatPutWith2Families(TEST_ROW);
214 
215     Map<byte[], List<Cell>> familyMap = p.getFamilyCellMap();
216     WALEdit edit = new WALEdit();
217     addFamilyMapToWALEdit(familyMap, edit);
218 
219     boolean foundFamily0 = false;
220     boolean foundFamily2 = false;
221     boolean modifiedFamily1 = false;
222 
223     List<Cell> cells = edit.getCells();
224 
225     for (Cell cell : cells) {
226       if (Arrays.equals(cell.getFamily(), TEST_FAMILY[0])) {
227         foundFamily0 = true;
228       }
229       if (Arrays.equals(cell.getFamily(), TEST_FAMILY[2])) {
230         foundFamily2 = true;
231       }
232       if (Arrays.equals(cell.getFamily(), TEST_FAMILY[1])) {
233         if (!Arrays.equals(cell.getValue(), TEST_VALUE[1])) {
234           modifiedFamily1 = true;
235         }
236       }
237     }
238     assertTrue(foundFamily0);
239     assertFalse(foundFamily2);
240     assertFalse(modifiedFamily1);
241 
242     // it's where WAL write cp should occur.
243     long now = EnvironmentEdgeManager.currentTime();
244     // we use HLogKey here instead of WALKey directly to support legacy coprocessors.
245     long txid = log.append(htd, hri, new HLogKey(hri.getEncodedNameAsBytes(), hri.getTable(), now),
246         edit, true);
247     log.sync(txid);
248 
249     // the edit shall have been change now by the coprocessor.
250     foundFamily0 = false;
251     foundFamily2 = false;
252     modifiedFamily1 = false;
253     for (Cell cell : cells) {
254       if (Arrays.equals(cell.getFamily(), TEST_FAMILY[0])) {
255         foundFamily0 = true;
256       }
257       if (Arrays.equals(cell.getFamily(), TEST_FAMILY[2])) {
258         foundFamily2 = true;
259       }
260       if (Arrays.equals(cell.getFamily(), TEST_FAMILY[1])) {
261         if (!Arrays.equals(cell.getValue(), TEST_VALUE[1])) {
262           modifiedFamily1 = true;
263         }
264       }
265     }
266     assertFalse(foundFamily0);
267     assertTrue(foundFamily2);
268     assertTrue(modifiedFamily1);
269 
270     assertTrue(cp.isPreWALWriteCalled());
271     assertTrue(cp.isPostWALWriteCalled());
272     assertEquals(seesLegacy, cp.isPreWALWriteDeprecatedCalled());
273     assertEquals(seesLegacy, cp.isPostWALWriteDeprecatedCalled());
274   }
275 
276   @Test
277   public void testNonLegacyWALKeysDoNotExplode() throws Exception {
278     TableName tableName = TableName.valueOf(TEST_TABLE);
279     final HTableDescriptor htd = createBasic3FamilyHTD(Bytes
280         .toString(TEST_TABLE));
281     final HRegionInfo hri = new HRegionInfo(tableName, null, null);
282     MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl();
283 
284     fs.mkdirs(new Path(FSUtils.getTableDir(hbaseRootDir, tableName), hri.getEncodedName()));
285 
286     final Configuration newConf = HBaseConfiguration.create(this.conf);
287 
288     final WAL wal = wals.getWAL(UNSPECIFIED_REGION, null);
289     final SampleRegionWALObserver newApi = getCoprocessor(wal, SampleRegionWALObserver.class);
290     newApi.setTestValues(TEST_TABLE, TEST_ROW, null, null, null, null, null, null);
291     final SampleRegionWALObserver oldApi = getCoprocessor(wal,
292         SampleRegionWALObserver.Legacy.class);
293     oldApi.setTestValues(TEST_TABLE, TEST_ROW, null, null, null, null, null, null);
294 
295     LOG.debug("ensuring wal entries haven't happened before we start");
296     assertFalse(newApi.isPreWALWriteCalled());
297     assertFalse(newApi.isPostWALWriteCalled());
298     assertFalse(newApi.isPreWALWriteDeprecatedCalled());
299     assertFalse(newApi.isPostWALWriteDeprecatedCalled());
300     assertFalse(oldApi.isPreWALWriteCalled());
301     assertFalse(oldApi.isPostWALWriteCalled());
302     assertFalse(oldApi.isPreWALWriteDeprecatedCalled());
303     assertFalse(oldApi.isPostWALWriteDeprecatedCalled());
304 
305     LOG.debug("writing to WAL with non-legacy keys.");
306     final int countPerFamily = 5;
307     for (HColumnDescriptor hcd : htd.getFamilies()) {
308       addWALEdits(tableName, hri, TEST_ROW, hcd.getName(), countPerFamily,
309           EnvironmentEdgeManager.getDelegate(), wal, htd, mvcc);
310     }
311 
312     LOG.debug("Verify that only the non-legacy CP saw edits.");
313     assertTrue(newApi.isPreWALWriteCalled());
314     assertTrue(newApi.isPostWALWriteCalled());
315     assertFalse(newApi.isPreWALWriteDeprecatedCalled());
316     assertFalse(newApi.isPostWALWriteDeprecatedCalled());
317     // wish we could test that the log message happened :/
318     assertFalse(oldApi.isPreWALWriteCalled());
319     assertFalse(oldApi.isPostWALWriteCalled());
320     assertFalse(oldApi.isPreWALWriteDeprecatedCalled());
321     assertFalse(oldApi.isPostWALWriteDeprecatedCalled());
322 
323     LOG.debug("reseting cp state.");
324     newApi.setTestValues(TEST_TABLE, TEST_ROW, null, null, null, null, null, null);
325     oldApi.setTestValues(TEST_TABLE, TEST_ROW, null, null, null, null, null, null);
326 
327     LOG.debug("write a log edit that supports legacy cps.");
328     final long now = EnvironmentEdgeManager.currentTime();
329     final WALKey legacyKey = new HLogKey(hri.getEncodedNameAsBytes(), hri.getTable(), now);
330     final WALEdit edit = new WALEdit();
331     final byte[] nonce = Bytes.toBytes("1772");
332     edit.add(new KeyValue(TEST_ROW, TEST_FAMILY[0], nonce, now, nonce));
333     final long txid = wal.append(htd, hri, legacyKey, edit, true);
334     wal.sync(txid);
335 
336     LOG.debug("Make sure legacy cps can see supported edits after having been skipped.");
337     assertTrue("non-legacy WALObserver didn't see pre-write.", newApi.isPreWALWriteCalled());
338     assertTrue("non-legacy WALObserver didn't see post-write.", newApi.isPostWALWriteCalled());
339     assertFalse("non-legacy WALObserver shouldn't have seen legacy pre-write.",
340         newApi.isPreWALWriteDeprecatedCalled());
341     assertFalse("non-legacy WALObserver shouldn't have seen legacy post-write.",
342         newApi.isPostWALWriteDeprecatedCalled());
343     assertTrue("legacy WALObserver didn't see pre-write.", oldApi.isPreWALWriteCalled());
344     assertTrue("legacy WALObserver didn't see post-write.", oldApi.isPostWALWriteCalled());
345     assertTrue("legacy WALObserver didn't see legacy pre-write.",
346         oldApi.isPreWALWriteDeprecatedCalled());
347     assertTrue("legacy WALObserver didn't see legacy post-write.",
348         oldApi.isPostWALWriteDeprecatedCalled());
349   }
350 
351   /**
352    * Coprocessors shouldn't get notice of empty waledits.
353    */
354   @Test
355   public void testEmptyWALEditAreNotSeen() throws Exception {
356     final HRegionInfo hri = createBasic3FamilyHRegionInfo(Bytes.toString(TEST_TABLE));
357     final HTableDescriptor htd = createBasic3FamilyHTD(Bytes.toString(TEST_TABLE));
358     final MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl();
359 
360     WAL log = wals.getWAL(UNSPECIFIED_REGION, null);
361     try {
362       SampleRegionWALObserver cp = getCoprocessor(log, SampleRegionWALObserver.class);
363 
364       cp.setTestValues(TEST_TABLE, null, null, null, null, null, null, null);
365 
366       assertFalse(cp.isPreWALWriteCalled());
367       assertFalse(cp.isPostWALWriteCalled());
368 
369       final long now = EnvironmentEdgeManager.currentTime();
370       long txid = log.append(htd, hri,
371           new WALKey(hri.getEncodedNameAsBytes(), hri.getTable(), now, mvcc),
372           new WALEdit(), true);
373       log.sync(txid);
374 
375       assertFalse("Empty WALEdit should skip coprocessor evaluation.", cp.isPreWALWriteCalled());
376       assertFalse("Empty WALEdit should skip coprocessor evaluation.", cp.isPostWALWriteCalled());
377     } finally {
378       log.close();
379     }
380   }
381 
382   /**
383    * Test WAL replay behavior with WALObserver.
384    */
385   @Test
386   public void testWALCoprocessorReplay() throws Exception {
387     // WAL replay is handled at HRegion::replayRecoveredEdits(), which is
388     // ultimately called by HRegion::initialize()
389     TableName tableName = TableName.valueOf("testWALCoprocessorReplay");
390     final HTableDescriptor htd = getBasic3FamilyHTableDescriptor(tableName);
391     MultiVersionConcurrencyControl mvcc = new MultiVersionConcurrencyControl();
392     // final HRegionInfo hri =
393     // createBasic3FamilyHRegionInfo(Bytes.toString(tableName));
394     // final HRegionInfo hri1 =
395     // createBasic3FamilyHRegionInfo(Bytes.toString(tableName));
396     final HRegionInfo hri = new HRegionInfo(tableName, null, null);
397 
398     final Path basedir =
399         FSUtils.getTableDir(this.hbaseRootDir, tableName);
400     deleteDir(basedir);
401     fs.mkdirs(new Path(basedir, hri.getEncodedName()));
402 
403     final Configuration newConf = HBaseConfiguration.create(this.conf);
404 
405     // WAL wal = new WAL(this.fs, this.dir, this.oldLogDir, this.conf);
406     WAL wal = wals.getWAL(UNSPECIFIED_REGION, null);
407     // Put p = creatPutWith2Families(TEST_ROW);
408     WALEdit edit = new WALEdit();
409     long now = EnvironmentEdgeManager.currentTime();
410     // addFamilyMapToWALEdit(p.getFamilyMap(), edit);
411     final int countPerFamily = 1000;
412     // for (HColumnDescriptor hcd: hri.getTableDesc().getFamilies()) {
413     for (HColumnDescriptor hcd : htd.getFamilies()) {
414       addWALEdits(tableName, hri, TEST_ROW, hcd.getName(), countPerFamily,
415           EnvironmentEdgeManager.getDelegate(), wal, htd, mvcc);
416     }
417     wal.append(htd, hri, new WALKey(hri.getEncodedNameAsBytes(), tableName, now, mvcc), edit, true);
418     // sync to fs.
419     wal.sync();
420 
421     User user = HBaseTestingUtility.getDifferentUser(newConf,
422         ".replay.wal.secondtime");
423     user.runAs(new PrivilegedExceptionAction() {
424       public Object run() throws Exception {
425         Path p = runWALSplit(newConf);
426         LOG.info("WALSplit path == " + p);
427         FileSystem newFS = FileSystem.get(newConf);
428         // Make a new wal for new region open.
429         final WALFactory wals2 = new WALFactory(conf, null, currentTest.getMethodName()+"2");
430         WAL wal2 = wals2.getWAL(UNSPECIFIED_REGION, null);;
431         HRegion region = HRegion.openHRegion(newConf, FileSystem.get(newConf), hbaseRootDir,
432             hri, htd, wal2, TEST_UTIL.getHBaseCluster().getRegionServer(0), null);
433         long seqid2 = region.getOpenSeqNum();
434 
435         SampleRegionWALObserver cp2 =
436           (SampleRegionWALObserver)region.getCoprocessorHost().findCoprocessor(
437               SampleRegionWALObserver.class.getName());
438         // TODO: asserting here is problematic.
439         assertNotNull(cp2);
440         assertTrue(cp2.isPreWALRestoreCalled());
441         assertTrue(cp2.isPostWALRestoreCalled());
442         assertFalse(cp2.isPreWALRestoreDeprecatedCalled());
443         assertFalse(cp2.isPostWALRestoreDeprecatedCalled());
444         region.close();
445         wals2.close();
446         return null;
447       }
448     });
449   }
450 
451   /**
452    * Test to see CP loaded successfully or not. There is a duplication at
453    * TestHLog, but the purpose of that one is to see whether the loaded CP will
454    * impact existing WAL tests or not.
455    */
456   @Test
457   public void testWALObserverLoaded() throws Exception {
458     WAL log = wals.getWAL(UNSPECIFIED_REGION, null);
459     assertNotNull(getCoprocessor(log, SampleRegionWALObserver.class));
460   }
461 
462   @Test
463   public void testWALObserverRoll() throws Exception {
464     final WAL wal = wals.getWAL(UNSPECIFIED_REGION, null);
465     final SampleRegionWALObserver cp = getCoprocessor(wal, SampleRegionWALObserver.class);
466     cp.setTestValues(TEST_TABLE, null, null, null, null, null, null, null);
467 
468     assertFalse(cp.isPreWALRollCalled());
469     assertFalse(cp.isPostWALRollCalled());
470 
471     wal.rollWriter(true);
472     assertTrue(cp.isPreWALRollCalled());
473     assertTrue(cp.isPostWALRollCalled());
474   }
475 
476   private SampleRegionWALObserver getCoprocessor(WAL wal,
477       Class<? extends SampleRegionWALObserver> clazz) throws Exception {
478     WALCoprocessorHost host = wal.getCoprocessorHost();
479     Coprocessor c = host.findCoprocessor(clazz.getName());
480     return (SampleRegionWALObserver) c;
481   }
482 
483   /*
484    * Creates an HRI around an HTD that has <code>tableName</code> and three
485    * column families named.
486    * 
487    * @param tableName Name of table to use when we create HTableDescriptor.
488    */
489   private HRegionInfo createBasic3FamilyHRegionInfo(final String tableName) {
490     HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
491 
492     for (int i = 0; i < TEST_FAMILY.length; i++) {
493       HColumnDescriptor a = new HColumnDescriptor(TEST_FAMILY[i]);
494       htd.addFamily(a);
495     }
496     return new HRegionInfo(htd.getTableName(), null, null, false);
497   }
498 
499   /*
500    * @param p Directory to cleanup
501    */
502   private void deleteDir(final Path p) throws IOException {
503     if (this.fs.exists(p)) {
504       if (!this.fs.delete(p, true)) {
505         throw new IOException("Failed remove of " + p);
506       }
507     }
508   }
509 
510   private Put creatPutWith2Families(byte[] row) throws IOException {
511     Put p = new Put(row);
512     for (int i = 0; i < TEST_FAMILY.length - 1; i++) {
513       p.add(TEST_FAMILY[i], TEST_QUALIFIER[i], TEST_VALUE[i]);
514     }
515     return p;
516   }
517 
518   /**
519    * Copied from HRegion.
520    * 
521    * @param familyMap
522    *          map of family->edits
523    * @param walEdit
524    *          the destination entry to append into
525    */
526   private void addFamilyMapToWALEdit(Map<byte[], List<Cell>> familyMap,
527       WALEdit walEdit) {
528     for (List<Cell> edits : familyMap.values()) {
529       for (Cell cell : edits) {
530         // KeyValue v1 expectation. Cast for now until we go all Cell all the time. TODO.
531         walEdit.add(cell);
532       }
533     }
534   }
535 
536   private Path runWALSplit(final Configuration c) throws IOException {
537     List<Path> splits = WALSplitter.split(
538       hbaseRootDir, logDir, oldLogDir, FileSystem.get(c), c, wals);
539     // Split should generate only 1 file since there's only 1 region
540     assertEquals(1, splits.size());
541     // Make sure the file exists
542     assertTrue(fs.exists(splits.get(0)));
543     LOG.info("Split file=" + splits.get(0));
544     return splits.get(0);
545   }
546 
547   private static final byte[] UNSPECIFIED_REGION = new byte[]{};
548 
549   private void addWALEdits(final TableName tableName, final HRegionInfo hri, final byte[] rowName,
550       final byte[] family, final int count, EnvironmentEdge ee, final WAL wal,
551       final HTableDescriptor htd, final MultiVersionConcurrencyControl mvcc) throws IOException {
552     String familyStr = Bytes.toString(family);
553     long txid = -1;
554     for (int j = 0; j < count; j++) {
555       byte[] qualifierBytes = Bytes.toBytes(Integer.toString(j));
556       byte[] columnBytes = Bytes.toBytes(familyStr + ":" + Integer.toString(j));
557       WALEdit edit = new WALEdit();
558       edit.add(new KeyValue(rowName, family, qualifierBytes, ee.currentTime(), columnBytes));
559       // uses WALKey instead of HLogKey on purpose. will only work for tests where we don't care
560       // about legacy coprocessors
561       txid = wal.append(htd, hri, new WALKey(hri.getEncodedNameAsBytes(), tableName,
562           ee.currentTime(), mvcc), edit, true);
563     }
564     if (-1 != txid) {
565       wal.sync(txid);
566     }
567   }
568 
569   private HTableDescriptor getBasic3FamilyHTableDescriptor(
570       final TableName tableName) {
571     HTableDescriptor htd = new HTableDescriptor(tableName);
572 
573     for (int i = 0; i < TEST_FAMILY.length; i++) {
574       HColumnDescriptor a = new HColumnDescriptor(TEST_FAMILY[i]);
575       htd.addFamily(a);
576     }
577     return htd;
578   }
579 
580   private HTableDescriptor createBasic3FamilyHTD(final String tableName) {
581     HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
582     HColumnDescriptor a = new HColumnDescriptor(Bytes.toBytes("a"));
583     htd.addFamily(a);
584     HColumnDescriptor b = new HColumnDescriptor(Bytes.toBytes("b"));
585     htd.addFamily(b);
586     HColumnDescriptor c = new HColumnDescriptor(Bytes.toBytes("c"));
587     htd.addFamily(c);
588     return htd;
589   }
590 }