001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.wal; 019 020import static org.junit.Assert.assertEquals; 021 022import java.io.IOException; 023import java.util.ArrayList; 024import java.util.Collections; 025import java.util.List; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.FileStatus; 028import org.apache.hadoop.fs.FileSystem; 029import org.apache.hadoop.fs.Path; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtility; 032import org.apache.hadoop.hbase.HConstants; 033import org.apache.hadoop.hbase.KeyValue; 034import org.apache.hadoop.hbase.TableName; 035import org.apache.hadoop.hbase.client.RegionInfo; 036import org.apache.hadoop.hbase.client.RegionInfoBuilder; 037import org.apache.hadoop.hbase.regionserver.MultiVersionConcurrencyControl; 038import org.apache.hadoop.hbase.testclassification.MediumTests; 039import org.apache.hadoop.hbase.util.Bytes; 040import org.apache.hadoop.hbase.util.CommonFSUtils; 041import org.junit.AfterClass; 042import org.junit.Before; 043import org.junit.BeforeClass; 044import org.junit.ClassRule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047import org.slf4j.Logger; 048import org.slf4j.LoggerFactory; 049 050@Category(MediumTests.class) 051public class TestWALRootDir { 052 053 @ClassRule 054 public static final HBaseClassTestRule CLASS_RULE = 055 HBaseClassTestRule.forClass(TestWALRootDir.class); 056 057 private static final Logger LOG = LoggerFactory.getLogger(TestWALRootDir.class); 058 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 059 private static Configuration conf; 060 private static FileSystem fs; 061 private static FileSystem walFs; 062 private static final TableName tableName = TableName.valueOf("TestWALWALDir"); 063 private static final byte [] rowName = Bytes.toBytes("row"); 064 private static final byte [] family = Bytes.toBytes("column"); 065 private static Path walRootDir; 066 private static Path rootDir; 067 private static WALFactory wals; 068 069 @Before 070 public void setUp() throws Exception { 071 cleanup(); 072 } 073 074 @BeforeClass 075 public static void setUpBeforeClass() throws Exception { 076 conf = TEST_UTIL.getConfiguration(); 077 TEST_UTIL.startMiniDFSCluster(1); 078 rootDir = TEST_UTIL.createRootDir(); 079 walRootDir = TEST_UTIL.createWALRootDir(); 080 fs = CommonFSUtils.getRootDirFileSystem(conf); 081 walFs = CommonFSUtils.getWALFileSystem(conf); 082 } 083 084 @AfterClass 085 public static void tearDownAfterClass() throws Exception { 086 cleanup(); 087 TEST_UTIL.shutdownMiniDFSCluster(); 088 } 089 090 @Test 091 public void testWALRootDir() throws Exception { 092 RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).build(); 093 wals = new WALFactory(conf, "testWALRootDir"); 094 WAL log = wals.getWAL(regionInfo); 095 096 assertEquals(1, getWALFiles(walFs, walRootDir).size()); 097 byte [] value = Bytes.toBytes("value"); 098 WALEdit edit = new WALEdit(); 099 edit.add(new KeyValue(rowName, family, Bytes.toBytes("1"), 100 System.currentTimeMillis(), value)); 101 long txid = 102 log.appendData(regionInfo, getWalKey(System.currentTimeMillis(), regionInfo, 0), edit); 103 log.sync(txid); 104 assertEquals("Expect 1 log have been created", 1, 105 getWALFiles(walFs, walRootDir).size()); 106 log.rollWriter(); 107 //Create 1 more WAL 108 assertEquals(2, getWALFiles(walFs, new Path(walRootDir, 109 HConstants.HREGION_LOGDIR_NAME)).size()); 110 edit.add(new KeyValue(rowName, family, Bytes.toBytes("2"), 111 System.currentTimeMillis(), value)); 112 txid = log.appendData(regionInfo, getWalKey(System.currentTimeMillis(), regionInfo, 1), edit); 113 log.sync(txid); 114 log.rollWriter(); 115 log.shutdown(); 116 117 assertEquals("Expect 3 logs in WALs dir", 3, getWALFiles(walFs, 118 new Path(walRootDir, HConstants.HREGION_LOGDIR_NAME)).size()); 119 } 120 121 private WALKeyImpl getWalKey(final long time, RegionInfo hri, final long startPoint) { 122 return new WALKeyImpl(hri.getEncodedNameAsBytes(), tableName, time, 123 new MultiVersionConcurrencyControl(startPoint)); 124 } 125 126 private List<FileStatus> getWALFiles(FileSystem fs, Path dir) 127 throws IOException { 128 List<FileStatus> result = new ArrayList<FileStatus>(); 129 LOG.debug("Scanning " + dir.toString() + " for WAL files"); 130 131 FileStatus[] files = fs.listStatus(dir); 132 if (files == null) return Collections.emptyList(); 133 for (FileStatus file : files) { 134 if (file.isDirectory()) { 135 // recurse into sub directories 136 result.addAll(getWALFiles(fs, file.getPath())); 137 } else { 138 String name = file.getPath().toString(); 139 if (!name.startsWith(".")) { 140 result.add(file); 141 } 142 } 143 } 144 return result; 145 } 146 147 private static void cleanup() throws Exception{ 148 walFs.delete(walRootDir, true); 149 fs.delete(rootDir, true); 150 } 151 152} 153