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