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.NavigableSet;
024import org.apache.hadoop.fs.FileStatus;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.fs.PathFilter;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
030import org.apache.hadoop.hbase.testclassification.RegionServerTests;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.apache.hadoop.hbase.util.CommonFSUtils;
033import org.junit.AfterClass;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041@Category({ RegionServerTests.class, SmallTests.class })
042public class TestReadWriteSeqIdFiles {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestReadWriteSeqIdFiles.class);
047
048  private static final Logger LOG = LoggerFactory.getLogger(TestReadWriteSeqIdFiles.class);
049
050  private static final HBaseCommonTestingUtility UTIL = new HBaseCommonTestingUtility();
051
052  private static FileSystem walFS;
053
054  private static Path REGION_DIR;
055
056  @BeforeClass
057  public static void setUp() throws IOException {
058    walFS = FileSystem.getLocal(UTIL.getConfiguration());
059    REGION_DIR = UTIL.getDataTestDir();
060  }
061
062  @AfterClass
063  public static void tearDown() throws IOException {
064    UTIL.cleanupTestDir();
065  }
066
067  @Test
068  public void test() throws IOException {
069    WALSplitUtil.writeRegionSequenceIdFile(walFS, REGION_DIR, 1000L);
070    assertEquals(1000L, WALSplitUtil.getMaxRegionSequenceId(walFS, REGION_DIR));
071    WALSplitUtil.writeRegionSequenceIdFile(walFS, REGION_DIR, 2000L);
072    assertEquals(2000L, WALSplitUtil.getMaxRegionSequenceId(walFS, REGION_DIR));
073    // can not write a sequence id which is smaller
074    try {
075      WALSplitUtil.writeRegionSequenceIdFile(walFS, REGION_DIR, 1500L);
076    } catch (IOException e) {
077      // expected
078      LOG.info("Expected error", e);
079    }
080
081    Path editsdir = WALSplitUtil.getRegionDirRecoveredEditsDir(REGION_DIR);
082    FileStatus[] files = CommonFSUtils.listStatus(walFS, editsdir, new PathFilter() {
083      @Override
084      public boolean accept(Path p) {
085        return WALSplitUtil.isSequenceIdFile(p);
086      }
087    });
088    // only one seqid file should exist
089    assertEquals(1, files.length);
090
091    // verify all seqId files aren't treated as recovered.edits files
092    NavigableSet<Path> recoveredEdits = WALSplitUtil.getSplitEditFilesSorted(walFS, REGION_DIR);
093    assertEquals(0, recoveredEdits.size());
094  }
095}