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.backup;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNull;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import java.security.PrivilegedAction;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.HBaseTestingUtil;
030import org.apache.hadoop.hbase.HConstants;
031import org.apache.hadoop.hbase.ServerName;
032import org.apache.hadoop.hbase.backup.util.BackupUtils;
033import org.apache.hadoop.hbase.master.region.MasterRegionFactory;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.apache.hadoop.hbase.util.Addressing;
036import org.apache.hadoop.hbase.util.CommonFSUtils;
037import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
038import org.apache.hadoop.security.UserGroupInformation;
039import org.junit.jupiter.api.Tag;
040import org.junit.jupiter.api.Test;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044@Tag(SmallTests.TAG)
045public class TestBackupUtils {
046
047  private static final Logger LOG = LoggerFactory.getLogger(TestBackupUtils.class);
048
049  protected static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
050  protected static Configuration conf = TEST_UTIL.getConfiguration();
051
052  @Test
053  public void testGetBulkOutputDir() {
054    // Create a user who is not the current user
055    String fooUserName = "foo1234";
056    String fooGroupName = "group1";
057    UserGroupInformation ugi =
058      UserGroupInformation.createUserForTesting(fooUserName, new String[] { fooGroupName });
059    // Get user's home directory
060    Path fooHomeDirectory = ugi.doAs(new PrivilegedAction<Path>() {
061      @Override
062      public Path run() {
063        try (FileSystem fs = FileSystem.get(conf)) {
064          return fs.getHomeDirectory();
065        } catch (IOException ioe) {
066          LOG.error("Failed to get foo's home directory", ioe);
067        }
068        return null;
069      }
070    });
071
072    Path bulkOutputDir = ugi.doAs(new PrivilegedAction<Path>() {
073      @Override
074      public Path run() {
075        try {
076          return BackupUtils.getBulkOutputDir("test", conf, false);
077        } catch (IOException ioe) {
078          LOG.error("Failed to get bulk output dir path", ioe);
079        }
080        return null;
081      }
082    });
083    // Make sure the directory is in foo1234's home directory
084    assertTrue(bulkOutputDir.toString().startsWith(fooHomeDirectory.toString()));
085  }
086
087  @Test
088  public void testFilesystemWalHostNameParsing() throws IOException {
089    String[] hosts =
090      new String[] { "10.20.30.40", "127.0.0.1", "localhost", "a-region-server.domain.com" };
091
092    Path walRootDir = CommonFSUtils.getWALRootDir(conf);
093    Path oldLogDir = new Path(walRootDir, HConstants.HREGION_OLDLOGDIR_NAME);
094
095    int port = 60030;
096    for (String host : hosts) {
097      ServerName serverName = ServerName.valueOf(host, port, 1234);
098
099      Path testOldWalPath = new Path(oldLogDir,
100        serverName + BackupUtils.LOGNAME_SEPARATOR + EnvironmentEdgeManager.currentTime());
101      assertEquals(host + Addressing.HOSTNAME_PORT_SEPARATOR + port,
102        BackupUtils.parseHostFromOldLog(testOldWalPath));
103
104      Path testMasterWalPath =
105        new Path(oldLogDir, testOldWalPath.getName() + MasterRegionFactory.ARCHIVED_WAL_SUFFIX);
106      assertNull(BackupUtils.parseHostFromOldLog(testMasterWalPath));
107
108      // org.apache.hadoop.hbase.wal.BoundedGroupingStrategy does this
109      Path testOldWalWithRegionGroupingPath = new Path(oldLogDir,
110        serverName + BackupUtils.LOGNAME_SEPARATOR + serverName + BackupUtils.LOGNAME_SEPARATOR
111          + "regiongroup-0" + BackupUtils.LOGNAME_SEPARATOR + EnvironmentEdgeManager.currentTime());
112      assertEquals(host + Addressing.HOSTNAME_PORT_SEPARATOR + port,
113        BackupUtils.parseHostFromOldLog(testOldWalWithRegionGroupingPath));
114    }
115
116  }
117}