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.io.asyncfs; 019 020import java.io.File; 021import java.io.IOException; 022 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.fs.Path; 025import org.apache.hadoop.hbase.HBaseCommonTestingUtility; 026import org.apache.hadoop.hbase.trace.TraceUtil; 027import org.apache.hadoop.hdfs.MiniDFSCluster; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031public abstract class AsyncFSTestBase { 032 033 private static final Logger LOG = LoggerFactory.getLogger(AsyncFSTestBase.class); 034 035 protected static final HBaseCommonTestingUtility UTIL = new HBaseCommonTestingUtility(); 036 037 protected static File CLUSTER_TEST_DIR; 038 039 protected static MiniDFSCluster CLUSTER; 040 041 private static boolean deleteOnExit() { 042 String v = System.getProperty("hbase.testing.preserve.testdir"); 043 // Let default be true, to delete on exit. 044 return v == null ? true : !Boolean.parseBoolean(v); 045 } 046 047 /** 048 * Creates a directory for the cluster, under the test data 049 */ 050 protected static void setupClusterTestDir() { 051 // Using randomUUID ensures that multiple clusters can be launched by 052 // a same test, if it stops & starts them 053 Path testDir = 054 UTIL.getDataTestDir("cluster_" + HBaseCommonTestingUtility.getRandomUUID().toString()); 055 CLUSTER_TEST_DIR = new File(testDir.toString()).getAbsoluteFile(); 056 // Have it cleaned up on exit 057 boolean b = deleteOnExit(); 058 if (b) { 059 CLUSTER_TEST_DIR.deleteOnExit(); 060 } 061 LOG.info("Created new mini-cluster data directory: {}, deleteOnExit={}", CLUSTER_TEST_DIR, b); 062 } 063 064 private static String createDirAndSetProperty(final String property) { 065 return createDirAndSetProperty(property, property); 066 } 067 068 private static String createDirAndSetProperty(final String relPath, String property) { 069 String path = UTIL.getDataTestDir(relPath).toString(); 070 System.setProperty(property, path); 071 UTIL.getConfiguration().set(property, path); 072 new File(path).mkdirs(); 073 LOG.info("Setting " + property + " to " + path + " in system properties and HBase conf"); 074 return path; 075 } 076 077 private static void createDirsAndSetProperties() throws IOException { 078 setupClusterTestDir(); 079 System.setProperty("test.build.data", CLUSTER_TEST_DIR.getPath()); 080 createDirAndSetProperty("test.cache.data"); 081 createDirAndSetProperty("hadoop.tmp.dir"); 082 083 // Frustrate yarn's and hdfs's attempts at writing /tmp. 084 // Below is fragile. Make it so we just interpolate any 'tmp' reference. 085 createDirAndSetProperty("dfs.journalnode.edits.dir"); 086 createDirAndSetProperty("dfs.datanode.shared.file.descriptor.paths"); 087 createDirAndSetProperty("nfs.dump.dir"); 088 createDirAndSetProperty("java.io.tmpdir"); 089 createDirAndSetProperty("dfs.journalnode.edits.dir"); 090 createDirAndSetProperty("dfs.provided.aliasmap.inmemory.leveldb.dir"); 091 createDirAndSetProperty("fs.s3a.committer.staging.tmp.path"); 092 } 093 094 protected static void startMiniDFSCluster(int servers) throws IOException { 095 if (CLUSTER != null) { 096 throw new IllegalStateException("Already started"); 097 } 098 createDirsAndSetProperties(); 099 100 Configuration conf = UTIL.getConfiguration(); 101 // Error level to skip some warnings specific to the minicluster. See HBASE-4709 102 org.apache.log4j.Logger.getLogger(org.apache.hadoop.metrics2.util.MBeans.class) 103 .setLevel(org.apache.log4j.Level.ERROR); 104 org.apache.log4j.Logger.getLogger(org.apache.hadoop.metrics2.impl.MetricsSystemImpl.class) 105 .setLevel(org.apache.log4j.Level.ERROR); 106 107 TraceUtil.initTracer(conf); 108 CLUSTER = new MiniDFSCluster.Builder(conf).numDataNodes(servers).build(); 109 CLUSTER.waitClusterUp(); 110 } 111 112 protected static void shutdownMiniDFSCluster() { 113 if (CLUSTER != null) { 114 CLUSTER.shutdown(true); 115 CLUSTER = null; 116 } 117 } 118}