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"), EnvironmentEdgeManager.currentTime(),
101      value));
102    long txid = log.appendData(regionInfo,
103      getWalKey(EnvironmentEdgeManager.currentTime(), regionInfo, 0), edit);
104    log.sync(txid);
105    assertEquals("Expect 1 log have been created", 1, getWALFiles(walFs, walRootDir).size());
106    log.rollWriter();
107    // Create 1 more WAL
108    assertEquals(2,
109      getWALFiles(walFs, new Path(walRootDir, HConstants.HREGION_LOGDIR_NAME)).size());
110    edit.add(new KeyValue(rowName, family, Bytes.toBytes("2"), EnvironmentEdgeManager.currentTime(),
111      value));
112    txid = log.appendData(regionInfo,
113      getWalKey(EnvironmentEdgeManager.currentTime(), regionInfo, 1), edit);
114    log.sync(txid);
115    log.rollWriter();
116    log.shutdown();
117
118    assertEquals("Expect 3 logs in WALs dir", 3,
119      getWALFiles(walFs, 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) 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}