NikunjB
Contributor
Contributor

Reserve Space on the Datastore

Howdy All,

Suppose if current freespace on the datastore is 200GB.

I have 2 programs, P1 and P2. Both needs a total of 150GB freespace to work on ( creating number of VMs and all. ) .

P1 starts and checks for freespace. It succeeds and proceeds with his work.

At the same time,

P2 starts and checks for freespace, it also succeeds.

In situation like these, How can I reserve 150GB for P1 so that when P2 checks, It fails and stops execution.

Powercli - 4.1

Thanks

Nikunj

Reply
0 Kudos
OptismTraining
Contributor
Contributor

If you're going to be using the space anyway why not provision as thick disks and increase your write speed at the same time?

Regards

----

Optism Training

www.optismtraining.com

Personal & Enterprise VMware Lab Rentals and training services.

---- Optism Training www.optismtraining.com Personal & Enterprise VMware Lab Rentals and training services.
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

You could use a mutual exclusion to prevent both programs to run at the same time. You can use the following code and wrap it around your programs:

Try {
  $MutexName  = "Global\Mutex"

  # Start mutual exclusion
  $Mutex = New-Object -TypeName System.Threading.Mutex -ArgumentList $false, $MutexName
  $Mutex.WaitOne()

  #
  # Your program comes here
  #
  
  # Stop mutual exclusion
  $Mutex.ReleaseMutex()
  $Mutex = $null
}
Catch { 
  # Stop mutual exclusion
  $Mutex.ReleaseMutex()
  $Mutex = $null
}

The first program that runs will get the mutual exclusion and will continue. The second program will wait untill the first program relases the mutual exclusion. This prevents both programs to run at the same time. That will assure that if P2 runs it fails and stop execution.

The Try - Catch construction ensures that if an error is generated in your code, the mutex is still released before the program stops.

Regards, Robert

Message was edited by: RvdNieuwendijk Added the Try - Catch explanation.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

Even if the created disks are thick provisioned, if both programs check the free space at the same time, they will both see enough free space and continue. By using the mutual exclusion they are not allowed to check for the free space at the same time. This will prevent the second program to see space that is already reserved by the first program as free space.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
NikunjB
Contributor
Contributor

Hi,

Thanks for the inputs.

My program is multithreaded in nature. It requires to do so. Otherwise the time taken to complete execution would be very large.

So mutexes I cannot use.

Thanks

Nikunj

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

You could do your own bookkeeping and make a system in which programs reserve datastore space. In the most simple form you can make a .csv file with colums Datastore, Total Space, Space Reserved. Your program can read the .csv file. Search for a datastore with enough free space. Increase the reserved space for that datastore with the amount of space the program is going to use on the datastore. And write the new reserved space to the .csv file.

To protect the .csv file from being used by two programs at the same time you still have to use the mutex. However in this case the mutex is only used for the bookkeeping and not during the creation of the VM's. So your programs can do more in parallel.

An example of the .csv file:

"Datastore","TotalSpace","SpaceReserved"
"datastore1","1000","950"
"datastore2","500","350"
"datastore3","1000","500"

You can use the following PowerShell script to reserve space on a datastore in the .csv file.

param([int]$SpaceNeeded=150)

$path = "Datastores.csv"

Try {
  # Get the mutex
  $MutexName  = "Global\Mutex"
  $Mutex = New-Object -TypeName System.Threading.Mutex -ArgumentList $false, $MutexName
  $Mutex.WaitOne() | Out-Null

  # Find the first datastore with enough free space
  $DatastoreFound = $false
  $Datastores = Import-Csv -Path $Path | ForEach-Object {
    if ($_) {
      if (-not $DatastoreFound -and ($_.TotalSpace - $_.SpaceReserved -ge $SpaceNeeded)) {
       $DatastoreFound = $_.Datastore
        [int] $_.SpaceReserved = [int] $_.SpaceReserved + $SpaceNeeded
      }
      $_
    }
 }

  # Write the datastore reserved space information back to the .csv file
  if ($Datastores) {
    $Datastores | Export-Csv -Path $Path -NoTypeInformation
  }

  # Release the mutex
  $Mutex.ReleaseMutex() | Out-Null
  $Mutex = $null
}
Catch {
  # Release the mutex when something went wrong
  $Mutex.ReleaseMutex() | Out-Null
  $Mutex = $null
}

# Return the name of the first datastore found with enough free space
# Return $false if no datastore found
$DatastoreFound

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos