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