/// <exception cref="System.Exception"/>
        public static void TestRR(VolumeChoosingPolicy <FsVolumeSpi> policy)
        {
            IList <FsVolumeSpi> volumes = new AList <FsVolumeSpi>();

            // First volume, with 100 bytes of space.
            volumes.AddItem(Org.Mockito.Mockito.Mock <FsVolumeSpi>());
            Org.Mockito.Mockito.When(volumes[0].GetAvailable()).ThenReturn(100L);
            // Second volume, with 200 bytes of space.
            volumes.AddItem(Org.Mockito.Mockito.Mock <FsVolumeSpi>());
            Org.Mockito.Mockito.When(volumes[1].GetAvailable()).ThenReturn(200L);
            // Test two rounds of round-robin choosing
            NUnit.Framework.Assert.AreEqual(volumes[0], policy.ChooseVolume(volumes, 0));
            NUnit.Framework.Assert.AreEqual(volumes[1], policy.ChooseVolume(volumes, 0));
            NUnit.Framework.Assert.AreEqual(volumes[0], policy.ChooseVolume(volumes, 0));
            NUnit.Framework.Assert.AreEqual(volumes[1], policy.ChooseVolume(volumes, 0));
            // The first volume has only 100L space, so the policy should
            // wisely choose the second one in case we ask for more.
            NUnit.Framework.Assert.AreEqual(volumes[1], policy.ChooseVolume(volumes, 150));
            // Fail if no volume can be chosen?
            try
            {
                policy.ChooseVolume(volumes, long.MaxValue);
                NUnit.Framework.Assert.Fail();
            }
            catch (IOException)
            {
            }
        }
Beispiel #2
0
 /// <exception cref="System.IO.IOException"/>
 private FsVolumeReference ChooseVolume(IList <FsVolumeImpl> list, long blockSize)
 {
     while (true)
     {
         FsVolumeImpl volume = blockChooser.ChooseVolume(list, blockSize);
         try
         {
             return(volume.ObtainReference());
         }
         catch (ClosedChannelException)
         {
             FsDatasetImpl.Log.Warn("Chosen a closed volume: " + volume);
             // blockChooser.chooseVolume returns DiskOutOfSpaceException when the list
             // is empty, indicating that all volumes are closed.
             list.Remove(volume);
         }
     }
 }
        /// <exception cref="System.Exception"/>
        public static void TestRRPolicyExceptionMessage(VolumeChoosingPolicy <FsVolumeSpi>
                                                        policy)
        {
            IList <FsVolumeSpi> volumes = new AList <FsVolumeSpi>();

            // First volume, with 500 bytes of space.
            volumes.AddItem(Org.Mockito.Mockito.Mock <FsVolumeSpi>());
            Org.Mockito.Mockito.When(volumes[0].GetAvailable()).ThenReturn(500L);
            // Second volume, with 600 bytes of space.
            volumes.AddItem(Org.Mockito.Mockito.Mock <FsVolumeSpi>());
            Org.Mockito.Mockito.When(volumes[1].GetAvailable()).ThenReturn(600L);
            int blockSize = 700;

            try
            {
                policy.ChooseVolume(volumes, blockSize);
                NUnit.Framework.Assert.Fail("expected to throw DiskOutOfSpaceException");
            }
            catch (DiskChecker.DiskOutOfSpaceException e)
            {
                NUnit.Framework.Assert.AreEqual("Not returnig the expected message", "Out of space: The volume with the most available space (="
                                                + 600 + " B) is less than the block size (=" + blockSize + " B).", e.Message);
            }
        }
Beispiel #4
0
 /// <exception cref="System.IO.IOException"/>
 public virtual V ChooseVolume(IList <V> volumes, long replicaSize)
 {
     lock (this)
     {
         if (volumes.Count < 1)
         {
             throw new DiskChecker.DiskOutOfSpaceException("No more available volumes");
         }
         AvailableSpaceVolumeChoosingPolicy.AvailableSpaceVolumeList volumesWithSpaces = new
                                                                                         AvailableSpaceVolumeChoosingPolicy.AvailableSpaceVolumeList(this, volumes);
         if (volumesWithSpaces.AreAllVolumesWithinFreeSpaceThreshold())
         {
             // If they're actually not too far out of whack, fall back on pure round
             // robin.
             V volume = roundRobinPolicyBalanced.ChooseVolume(volumes, replicaSize);
             if (Log.IsDebugEnabled())
             {
                 Log.Debug("All volumes are within the configured free space balance " + "threshold. Selecting "
                           + volume + " for write of block size " + replicaSize);
             }
             return(volume);
         }
         else
         {
             V volume = null;
             // If none of the volumes with low free space have enough space for the
             // replica, always try to choose a volume with a lot of free space.
             long mostAvailableAmongLowVolumes = volumesWithSpaces.GetMostAvailableSpaceAmongVolumesWithLowAvailableSpace
                                                     ();
             IList <V> highAvailableVolumes = ExtractVolumesFromPairs(volumesWithSpaces.GetVolumesWithHighAvailableSpace
                                                                          ());
             IList <V> lowAvailableVolumes = ExtractVolumesFromPairs(volumesWithSpaces.GetVolumesWithLowAvailableSpace
                                                                         ());
             float preferencePercentScaler = (highAvailableVolumes.Count * balancedPreferencePercent
                                              ) + (lowAvailableVolumes.Count * (1 - balancedPreferencePercent));
             float scaledPreferencePercent = (highAvailableVolumes.Count * balancedPreferencePercent
                                              ) / preferencePercentScaler;
             if (mostAvailableAmongLowVolumes < replicaSize || random.NextFloat() < scaledPreferencePercent)
             {
                 volume = roundRobinPolicyHighAvailable.ChooseVolume(highAvailableVolumes, replicaSize
                                                                     );
                 if (Log.IsDebugEnabled())
                 {
                     Log.Debug("Volumes are imbalanced. Selecting " + volume + " from high available space volumes for write of block size "
                               + replicaSize);
                 }
             }
             else
             {
                 volume = roundRobinPolicyLowAvailable.ChooseVolume(lowAvailableVolumes, replicaSize
                                                                    );
                 if (Log.IsDebugEnabled())
                 {
                     Log.Debug("Volumes are imbalanced. Selecting " + volume + " from low available space volumes for write of block size "
                               + replicaSize);
                 }
             }
             return(volume);
         }
     }
 }