@Override
  public V chooseVolume(List<V> volumes, long replicaSize, String storageId)
      throws IOException {
    if (volumes.size() < 1) {
      throw new DiskOutOfSpaceException("No more available volumes");
    }
    // As all the items in volumes are with the same storage type,
    // so only need to get the storage type index of the first item in volumes
    StorageType storageType = volumes.get(0).getStorageType();
    int index = storageType != null ?
            storageType.ordinal() : StorageType.DEFAULT.ordinal();

    synchronized (syncLocks[index]) {
      return doChooseVolume(volumes, replicaSize, storageId);
    }
  }
@Test(timeout=60000)
  public void testSameAvailableVolumeSpace() throws Exception {
    @SuppressWarnings("unchecked")
    final AvailableSpaceVolumeChoosingPolicy<FsVolumeSpi> policy =
            ReflectionUtils.newInstance(AvailableSpaceVolumeChoosingPolicy.class, null);

    List<FsVolumeSpi> volumes = new ArrayList<FsVolumeSpi>();

    // first volume with 1MB free space
    volumes.add(Mockito.mock(FsVolumeSpi.class));
    Mockito.when(volumes.get(0).getAvailable()).thenReturn(1024L * 1024L);

    // Second volume with 1MB free space
    volumes.add(Mockito.mock(FsVolumeSpi.class));
    Mockito.when(volumes.get(1).getAvailable()).thenReturn(1024L * 1024L);

    // Third volume with 1MB free space
    volumes.add(Mockito.mock(FsVolumeSpi.class));
    Mockito.when(volumes.get(2).getAvailable()).thenReturn(1024L * 1024L);

    // Fourth volume with 1MB free space
    volumes.add(Mockito.mock(FsVolumeSpi.class));
    Mockito.when(volumes.get(3).getAvailable()).thenReturn(1024L * 1024L);

    // We should alternate assigning between all the above volumes
    // for they have the same available space
    initPolicy(policy, 0, 1.0f);
    Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 100,
            null));
    Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 100,
            null));
    Assert.assertEquals(volumes.get(2), policy.chooseVolume(volumes, 100,
            null));
    Assert.assertEquals(volumes.get(3), policy.chooseVolume(volumes, 100,
            null));

    // We should alternate assigning between all the above volumes
    // for they have the same available space
    initPolicy(policy, 0, 0.0f);
    Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 100,
            null));
    Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 100,
            null));
    Assert.assertEquals(volumes.get(2), policy.chooseVolume(volumes, 100,
            null));
    Assert.assertEquals(volumes.get(3), policy.chooseVolume(volumes, 100,
            null));
  }