.getMostAvailableSpaceAmongVolumesWithLowAvailableSpace(); List<V> highAvailableVolumes = extractVolumesFromPairs( volumesWithSpaces.getVolumesWithHighAvailableSpace()); List<V> lowAvailableVolumes = extractVolumesFromPairs( volumesWithSpaces.getVolumesWithLowAvailableSpace());
@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); } }
AvailableSpaceVolumeChoosingPolicy(Random random) { this.random = random; initLocks(); }
@Test(timeout=60000) public void testTwoUnbalancedVolumes() throws Exception { @SuppressWarnings("unchecked") final AvailableSpaceVolumeChoosingPolicy<FsVolumeSpi> policy = ReflectionUtils.newInstance(AvailableSpaceVolumeChoosingPolicy.class, null); initPolicy(policy, 1.0f); 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 3MB free space, which is a difference of 2MB, more // than the threshold of 1MB. volumes.add(Mockito.mock(FsVolumeSpi.class)); Mockito.when(volumes.get(1).getAvailable()).thenReturn(1024L * 1024L * 3); Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 100)); }
Random random = new Random(123L); final AvailableSpaceVolumeChoosingPolicy<FsVolumeSpi> policy = new AvailableSpaceVolumeChoosingPolicy<FsVolumeSpi>(random); long highAvailableSpaceVolumeSelected = 0; for (int i = 0; i < RANDOMIZED_ITERATIONS; i++) { FsVolumeSpi volume = policy.chooseVolume(volumes, 100); for (int j = 0; j < volumes.size(); j++) {
@Test(timeout=60000) public void testRRPolicyExceptionMessage() throws Exception { final AvailableSpaceVolumeChoosingPolicy<FsVolumeSpi> policy = new AvailableSpaceVolumeChoosingPolicy<FsVolumeSpi>(); initPolicy(policy, 1.0f); TestRoundRobinVolumeChoosingPolicy.testRRPolicyExceptionMessage(policy); }
@Test(timeout=60000) public void testAvailableSpaceChanges() throws Exception { @SuppressWarnings("unchecked") final AvailableSpaceVolumeChoosingPolicy<FsVolumeSpi> policy = ReflectionUtils.newInstance(AvailableSpaceVolumeChoosingPolicy.class, null); initPolicy(policy, 1.0f); 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 3MB free space, which is a difference of 2MB, more // than the threshold of 1MB. volumes.add(Mockito.mock(FsVolumeSpi.class)); Mockito.when(volumes.get(1).getAvailable()) .thenReturn(1024L * 1024L * 3) .thenReturn(1024L * 1024L * 3) .thenReturn(1024L * 1024L * 3) .thenReturn(1024L * 1024L * 1); // After the third check, return 1MB. // Should still be able to get a volume for the replica even though the // available space on the second volume changed. Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 100)); }
.getMostAvailableSpaceAmongVolumesWithLowAvailableSpace(); List<V> highAvailableVolumes = extractVolumesFromPairs( volumesWithSpaces.getVolumesWithHighAvailableSpace()); List<V> lowAvailableVolumes = extractVolumesFromPairs( volumesWithSpaces.getVolumesWithLowAvailableSpace());
@Test(timeout=60000) public void testNotEnoughSpaceOnSelectedVolume() 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 3MB free space, which is a difference of 2MB, more // than the threshold of 1MB. volumes.add(Mockito.mock(FsVolumeSpi.class)); Mockito.when(volumes.get(1).getAvailable()).thenReturn(1024L * 1024L * 3); // All writes should be assigned to the volume with the least free space. // However, if the volume with the least free space doesn't have enough // space to accept the replica size, and another volume does have enough // free space, that should be chosen instead. initPolicy(policy, 0.0f); Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 1024L * 1024L * 2)); }
public AvailableSpaceVolumeChoosingPolicy() { this(new Random()); initLocks(); }
.getMostAvailableSpaceAmongVolumesWithLowAvailableSpace(); List<V> highAvailableVolumes = extractVolumesFromPairs( volumesWithSpaces.getVolumesWithHighAvailableSpace()); List<V> lowAvailableVolumes = extractVolumesFromPairs( volumesWithSpaces.getVolumesWithLowAvailableSpace());
Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(2), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(2), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 100));
Assert.assertEquals(volumes.get(2), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(3), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(2), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(3), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(0), policy.chooseVolume(volumes, 100)); Assert.assertEquals(volumes.get(1), policy.chooseVolume(volumes, 100));