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.regionserver;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileStatus;
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseCommonTestingUtil;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.RegionInfoBuilder;
034import org.apache.hadoop.hbase.testclassification.RegionServerTests;
035import org.apache.hadoop.hbase.testclassification.SmallTests;
036import org.apache.hadoop.hbase.util.FSTableDescriptors;
037import org.junit.AfterClass;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043@Category({ RegionServerTests.class, SmallTests.class })
044public class TestReadAndWriteRegionInfoFile {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048    HBaseClassTestRule.forClass(TestReadAndWriteRegionInfoFile.class);
049
050  private static final HBaseCommonTestingUtil UTIL = new HBaseTestingUtil();
051
052  private static final Configuration CONF = UTIL.getConfiguration();
053
054  private static FileSystem FS;
055
056  private static Path ROOT_DIR;
057
058  @BeforeClass
059  public static void setUp() throws IOException {
060    ROOT_DIR = UTIL.getDataTestDir();
061    FS = ROOT_DIR.getFileSystem(CONF);
062  }
063
064  @AfterClass
065  public static void tearDown() {
066    UTIL.cleanupTestDir();
067  }
068
069  @Test
070  public void testReadAndWriteRegionInfoFile() throws IOException, InterruptedException {
071    RegionInfo ri = RegionInfoBuilder.FIRST_META_REGIONINFO;
072    // Create a region. That'll write the .regioninfo file.
073    FSTableDescriptors fsTableDescriptors = new FSTableDescriptors(FS, ROOT_DIR);
074    FSTableDescriptors.tryUpdateAndGetMetaTableDescriptor(CONF, FS, ROOT_DIR);
075    HRegion r = HBaseTestingUtil.createRegionAndWAL(ri, ROOT_DIR, CONF,
076      fsTableDescriptors.get(TableName.META_TABLE_NAME));
077    // Get modtime on the file.
078    long modtime = getModTime(r);
079    HBaseTestingUtil.closeRegionAndWAL(r);
080    Thread.sleep(1001);
081    r = HRegion.openHRegion(ROOT_DIR, ri, fsTableDescriptors.get(TableName.META_TABLE_NAME), null,
082      CONF);
083    // Ensure the file is not written for a second time.
084    long modtime2 = getModTime(r);
085    assertEquals(modtime, modtime2);
086    // Now load the file.
087    HRegionFileSystem.loadRegionInfoFileContent(r.getRegionFileSystem().getFileSystem(),
088      r.getRegionFileSystem().getRegionDir());
089    HBaseTestingUtil.closeRegionAndWAL(r);
090  }
091
092  private long getModTime(final HRegion r) throws IOException {
093    FileStatus[] statuses = r.getRegionFileSystem().getFileSystem().listStatus(
094      new Path(r.getRegionFileSystem().getRegionDir(), HRegionFileSystem.REGION_INFO_FILE));
095    assertTrue(statuses != null && statuses.length == 1);
096    return statuses[0].getModificationTime();
097  }
098}