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.FSUtils; 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 = FSUtils.getRootDirFileSystem(conf); 081 walFs = FSUtils.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 = log.append(regionInfo, 102 getWalKey(System.currentTimeMillis(), regionInfo, 0), edit, true); 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.append(regionInfo, getWalKey(System.currentTimeMillis(), regionInfo, 1), 113 edit, true); 114 log.sync(txid); 115 log.rollWriter(); 116 log.shutdown(); 117 118 assertEquals("Expect 3 logs in WALs dir", 3, getWALFiles(walFs, 119 new Path(walRootDir, HConstants.HREGION_LOGDIR_NAME)).size()); 120 } 121 122 private WALKeyImpl getWalKey(final long time, RegionInfo hri, final long startPoint) { 123 return new WALKeyImpl(hri.getEncodedNameAsBytes(), tableName, time, 124 new MultiVersionConcurrencyControl(startPoint)); 125 } 126 127 private List<FileStatus> getWALFiles(FileSystem fs, Path dir) 128 throws IOException { 129 List<FileStatus> result = new ArrayList<FileStatus>(); 130 LOG.debug("Scanning " + dir.toString() + " for WAL files"); 131 132 FileStatus[] files = fs.listStatus(dir); 133 if (files == null) return Collections.emptyList(); 134 for (FileStatus file : files) { 135 if (file.isDirectory()) { 136 // recurse into sub directories 137 result.addAll(getWALFiles(fs, file.getPath())); 138 } else { 139 String name = file.getPath().toString(); 140 if (!name.startsWith(".")) { 141 result.add(file); 142 } 143 } 144 } 145 return result; 146 } 147 148 private static void cleanup() throws Exception{ 149 walFs.delete(walRootDir, true); 150 fs.delete(rootDir, true); 151 } 152 153} 154