I am attempting to create a function that calculates correct volume size based on a desired percentage used. I am encountering what I believe to be a computational issue inside the MVEL where it doesn't behave consistently. Maybe this is a bug, or maybe the issue is between my ears? I need some help, though.
Here is the code:
def calculateTargetVolumeSize(vol_used,vol_avail,snap_percent,snap_used,target_percent)
{
int percent_used = (vol_used / (vol_used + vol_avail)) * 100;
if (percent_used < target_percent ) {
throwException("Percent used " + percent_used + "% is already below designated percentage of " + target_percent + "%.\nThe following values were passed: " + vol_used + ", " + vol_avail + ", " + snap_percent + ", " + snap_used);
}
int new_avail_size = (int)(vol_used / (target_percent / 100));
int new_vol_size = (int)(new_avail_size / (1 - (snap_percent / 100)));
int new_snap_size = new_vol_size - new_avail_size;
if (snap_used > new_snap_size) {
new_vol_size = new_avail_size + snap_used;
}
if (new_vol_size < 20 ) {
new_vol_size = 20;
}
// throwException("new size: " + new_vol_size);
return new_vol_size;
}
I am leveraging this function in a workflow that uses the Resize volume command. The function is in the TargetSize value. It returns the following error:
Function: 'calculateTargetVolumeSize'
With parameters: v1.used_size_mb, v1.available_size_mb, v1.snapshot_reserved_percent, v1.snapshot_used_mb, 70
Threw an Exception with a message: Percent used 9.99997731789257E-4% is already below designated percentage of 70%.
The following values were passed: 237883, 53957, 5, 50035
I cannot for the life of me figure out how ( 237883 / (237883 + 53957)) * 100 = 9.999977331789257E-4
When I run the function as a test using this syntax: calculateTargetVolumeSize(237883,53957,5,50035,70)
It returns the following value correctly: 389867
Any ideas?
Thanks,
Will