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.HBaseTestingUtil;
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.apache.hadoop.hbase.util.EnvironmentEdgeManager;
042import org.junit.AfterClass;
043import org.junit.Before;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048import org.slf4j.Logger;
049import org.slf4j.LoggerFactory;
050
051@Category(MediumTests.class)
052public class TestWALRootDir {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestWALRootDir.class);
057
058  private static final Logger LOG = LoggerFactory.getLogger(TestWALRootDir.class);
059  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
060  private static Configuration conf;
061  private static FileSystem fs;
062  private static FileSystem walFs;
063  private static final TableName tableName = TableName.valueOf("TestWALWALDir");
064  private static final byte [] rowName = Bytes.toBytes("row");
065  private static final byte [] family = Bytes.toBytes("column");
066  private static Path walRootDir;
067  private static Path rootDir;
068  private static WALFactory wals;
069
070  @Before
071  public void setUp() throws Exception {
072    cleanup();
073  }
074
075  @BeforeClass
076  public static void setUpBeforeClass() throws Exception {
077    conf = TEST_UTIL.getConfiguration();
078    TEST_UTIL.startMiniDFSCluster(1);
079    rootDir = TEST_UTIL.createRootDir();
080    walRootDir = TEST_UTIL.createWALRootDir();
081    fs = CommonFSUtils.getRootDirFileSystem(conf);
082    walFs = CommonFSUtils.getWALFileSystem(conf);
083  }
084
085  @AfterClass
086  public static void tearDownAfterClass() throws Exception {
087    cleanup();
088    TEST_UTIL.shutdownMiniDFSCluster();
089  }
090
091  @Test
092  public void testWALRootDir() throws Exception {
093    RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).build();
094    wals = new WALFactory(conf, "testWALRootDir");
095    WAL log = wals.getWAL(regionInfo);
096
097    assertEquals(1, getWALFiles(walFs, walRootDir).size());
098    byte [] value = Bytes.toBytes("value");
099    WALEdit edit = new WALEdit();
100    edit.add(new KeyValue(rowName, family, Bytes.toBytes("1"),
101      EnvironmentEdgeManager.currentTime(), value));
102    long txid = log.appendData(regionInfo, getWalKey(EnvironmentEdgeManager.currentTime(),
103      regionInfo, 0), edit);
104    log.sync(txid);
105    assertEquals("Expect 1 log have been created", 1,
106        getWALFiles(walFs, walRootDir).size());
107    log.rollWriter();
108    //Create 1 more WAL
109    assertEquals(2, getWALFiles(walFs, new Path(walRootDir,
110        HConstants.HREGION_LOGDIR_NAME)).size());
111    edit.add(new KeyValue(rowName, family, Bytes.toBytes("2"),
112      EnvironmentEdgeManager.currentTime(), value));
113    txid = log.appendData(regionInfo, getWalKey(EnvironmentEdgeManager.currentTime(),
114      regionInfo, 1), edit);
115    log.sync(txid);
116    log.rollWriter();
117    log.shutdown();
118
119    assertEquals("Expect 3 logs in WALs dir", 3, getWALFiles(walFs,
120        new Path(walRootDir, HConstants.HREGION_LOGDIR_NAME)).size());
121  }
122
123  private WALKeyImpl getWalKey(final long time, RegionInfo hri, final long startPoint) {
124    return new WALKeyImpl(hri.getEncodedNameAsBytes(), tableName, time,
125        new MultiVersionConcurrencyControl(startPoint));
126  }
127
128  private List<FileStatus> getWALFiles(FileSystem fs, Path dir)
129      throws IOException {
130    List<FileStatus> result = new ArrayList<FileStatus>();
131    LOG.debug("Scanning " + dir.toString() + " for WAL files");
132
133    FileStatus[] files = fs.listStatus(dir);
134    if (files == null) return Collections.emptyList();
135    for (FileStatus file : files) {
136      if (file.isDirectory()) {
137        // recurse into sub directories
138        result.addAll(getWALFiles(fs, file.getPath()));
139      } else {
140        String name = file.getPath().toString();
141        if (!name.startsWith(".")) {
142          result.add(file);
143        }
144      }
145    }
146    return result;
147  }
148
149  private static void cleanup() throws Exception{
150    walFs.delete(walRootDir, true);
151    fs.delete(rootDir, true);
152  }
153
154}
155