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.fsdataset;
019
020 import java.io.IOException;
021 import java.util.List;
022
023 import org.apache.commons.logging.Log;
024 import org.apache.commons.logging.LogFactory;
025 import org.apache.hadoop.hdfs.StorageType;
026 import org.apache.hadoop.util.DiskChecker.DiskOutOfSpaceException;
027
028 /**
029 * Choose volumes in round-robin order.
030 */
031 public class RoundRobinVolumeChoosingPolicy<V extends FsVolumeSpi>
032 implements VolumeChoosingPolicy<V> {
033 public static final Log LOG = LogFactory.getLog(RoundRobinVolumeChoosingPolicy.class);
034
035 private int curVolume = 0;
036
037 @Override
038 public synchronized V chooseVolume(final List<V> volumes, long blockSize)
039 throws IOException {
040
041 if(volumes.size() < 1) {
042 throw new DiskOutOfSpaceException("No more available volumes");
043 }
044
045 // since volumes could've been removed because of the failure
046 // make sure we are not out of bounds
047 if(curVolume >= volumes.size()) {
048 curVolume = 0;
049 }
050
051 int startVolume = curVolume;
052 long maxAvailable = 0;
053
054 while (true) {
055 final V volume = volumes.get(curVolume);
056 curVolume = (curVolume + 1) % volumes.size();
057 long availableVolumeSize = volume.getAvailable();
058 if (availableVolumeSize > blockSize) {
059 return volume;
060 }
061
062 if (availableVolumeSize > maxAvailable) {
063 maxAvailable = availableVolumeSize;
064 }
065
066 if (curVolume == startVolume) {
067 throw new DiskOutOfSpaceException("Out of space: "
068 + "The volume with the most available space (=" + maxAvailable
069 + " B) is less than the block size (=" + blockSize + " B).");
070 }
071 }
072 }
073 }