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 */ 018 019package org.apache.hadoop.hdfs.server.blockmanagement; 020 021import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_AVAILABLE_SPACE_BLOCK_PLACEMENT_POLICY_BALANCED_SPACE_PREFERENCE_FRACTION_KEY; 022import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_NAMENODE_AVAILABLE_SPACE_BLOCK_PLACEMENT_POLICY_BALANCED_SPACE_PREFERENCE_FRACTION_DEFAULT; 023 024import java.util.Random; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hdfs.DFSConfigKeys; 030import org.apache.hadoop.net.NetworkTopology; 031 032/** 033 * Space balanced block placement policy. 034 */ 035public class AvailableSpaceBlockPlacementPolicy extends 036 BlockPlacementPolicyDefault { 037 private static final Log LOG = LogFactory 038 .getLog(AvailableSpaceBlockPlacementPolicy.class); 039 private static final Random RAND = new Random(); 040 private int balancedPreference = 041 (int) (100 * DFS_NAMENODE_AVAILABLE_SPACE_BLOCK_PLACEMENT_POLICY_BALANCED_SPACE_PREFERENCE_FRACTION_DEFAULT); 042 043 @Override 044 public void initialize(Configuration conf, FSClusterStats stats, 045 NetworkTopology clusterMap, Host2NodesMap host2datanodeMap) { 046 super.initialize(conf, stats, clusterMap, host2datanodeMap); 047 float balancedPreferencePercent = 048 conf.getFloat( 049 DFS_NAMENODE_AVAILABLE_SPACE_BLOCK_PLACEMENT_POLICY_BALANCED_SPACE_PREFERENCE_FRACTION_KEY, 050 DFS_NAMENODE_AVAILABLE_SPACE_BLOCK_PLACEMENT_POLICY_BALANCED_SPACE_PREFERENCE_FRACTION_DEFAULT); 051 052 LOG.info("Available space block placement policy initialized: " 053 + DFSConfigKeys.DFS_NAMENODE_AVAILABLE_SPACE_BLOCK_PLACEMENT_POLICY_BALANCED_SPACE_PREFERENCE_FRACTION_KEY 054 + " = " + balancedPreferencePercent); 055 056 if (balancedPreferencePercent > 1.0) { 057 LOG.warn("The value of " 058 + DFS_NAMENODE_AVAILABLE_SPACE_BLOCK_PLACEMENT_POLICY_BALANCED_SPACE_PREFERENCE_FRACTION_KEY 059 + " is greater than 1.0 but should be in the range 0.0 - 1.0"); 060 } 061 if (balancedPreferencePercent < 0.5) { 062 LOG.warn("The value of " 063 + DFS_NAMENODE_AVAILABLE_SPACE_BLOCK_PLACEMENT_POLICY_BALANCED_SPACE_PREFERENCE_FRACTION_KEY 064 + " is less than 0.5 so datanodes with more used percent will" 065 + " receive more block allocations."); 066 } 067 balancedPreference = (int) (100 * balancedPreferencePercent); 068 } 069 070 @Override 071 protected DatanodeDescriptor chooseDataNode(String scope) { 072 DatanodeDescriptor a = (DatanodeDescriptor) clusterMap.chooseRandom(scope); 073 DatanodeDescriptor b = (DatanodeDescriptor) clusterMap.chooseRandom(scope); 074 if (a != null && b != null){ 075 int ret = compareDataNode(a, b); 076 if (ret == 0) { 077 return a; 078 } else if (ret < 0) { 079 return (RAND.nextInt(100) < balancedPreference) ? a : b; 080 } else { 081 return (RAND.nextInt(100) < balancedPreference) ? b : a; 082 } 083 } else { 084 return a == null ? b : a; 085 } 086 } 087 088 /** 089 * Compare the two data nodes. 090 */ 091 protected int compareDataNode(final DatanodeDescriptor a, 092 final DatanodeDescriptor b) { 093 if (a.equals(b) 094 || Math.abs(a.getDfsUsedPercent() - b.getDfsUsedPercent()) < 5) { 095 return 0; 096 } 097 return a.getDfsUsedPercent() < b.getDfsUsedPercent() ? -1 : 1; 098 } 099}