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 package org.apache.hadoop.hdfs.server.datanode; 019 020 import java.io.IOException; 021 import java.util.List; 022 023 import org.apache.hadoop.hdfs.server.datanode.FSDataset.FSVolume; 024 import org.apache.hadoop.util.DiskChecker.DiskOutOfSpaceException; 025 026 public class RoundRobinVolumesPolicy implements BlockVolumeChoosingPolicy { 027 028 private int curVolume = 0; 029 030 @Override 031 public synchronized FSVolume chooseVolume(List<FSVolume> volumes, long blockSize) 032 throws IOException { 033 if(volumes.size() < 1) { 034 throw new DiskOutOfSpaceException("No more available volumes"); 035 } 036 037 // since volumes could've been removed because of the failure 038 // make sure we are not out of bounds 039 if(curVolume >= volumes.size()) { 040 curVolume = 0; 041 } 042 043 int startVolume = curVolume; 044 long maxAvailable = 0; 045 046 while (true) { 047 FSVolume volume = volumes.get(curVolume); 048 curVolume = (curVolume + 1) % volumes.size(); 049 long availableVolumeSize = volume.getAvailable(); 050 if (availableVolumeSize > blockSize) { return volume; } 051 052 if (availableVolumeSize > maxAvailable) { 053 maxAvailable = availableVolumeSize; 054 } 055 056 if (curVolume == startVolume) { 057 throw new DiskOutOfSpaceException( 058 "Insufficient space for an additional block. Volume with the most available space has " 059 + maxAvailable 060 + " bytes free, configured block size is " 061 + blockSize); 062 } 063 } 064 } 065 066 }