@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)); }
@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)); }
@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)); }
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));
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++) {