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.master.balancer; 019 020import java.io.IOException; 021import java.nio.charset.StandardCharsets; 022import java.nio.file.FileSystems; 023import java.nio.file.Files; 024import java.nio.file.NoSuchFileException; 025import java.nio.file.Path; 026import java.util.Collections; 027import java.util.List; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031final class HeterogeneousCostRulesTestHelper { 032 033 private static final Logger LOG = LoggerFactory.getLogger(HeterogeneousCostRulesTestHelper.class); 034 035 static final String DEFAULT_RULES_FILE_NAME = "hbase-balancer.rules"; 036 037 private HeterogeneousCostRulesTestHelper() { 038 } 039 040 /** 041 * Create rule file with the given rules. 042 * @param file Name of file to write rules into. 043 * @return Full file name of the rules file which is <code>dir</code> + DEFAULT_RULES_FILE_NAME. 044 */ 045 static String createRulesFile(String file, final List<String> rules) throws IOException { 046 cleanup(file); 047 Path path = Files.createFile(FileSystems.getDefault().getPath(file)); 048 return Files.write(path, rules, StandardCharsets.UTF_8).toString(); 049 } 050 051 /** 052 * Create rule file with empty rules. 053 * @param file Name of file to write rules into. 054 * @return Full file name of the rules file which is <code>dir</code> + DEFAULT_RULES_FILE_NAME. 055 */ 056 static String createRulesFile(String file) throws IOException { 057 return createRulesFile(file, Collections.emptyList()); 058 } 059 060 static void cleanup(String file) throws IOException { 061 try { 062 Files.delete(FileSystems.getDefault().getPath(file)); 063 } catch (NoSuchFileException nsfe) { 064 LOG.warn("FileNotFoundException for {}", file, nsfe); 065 } 066 } 067}