Class NormalizedResources
- java.lang.Object
-
- org.apache.storm.scheduler.resource.normalization.NormalizedResources
-
public class NormalizedResources extends Object
Resources that have been normalized. This class is intended as a delegate for more specific types of normalized resource set, since it does not keep track of memory as a resource.
-
-
Field Summary
Fields Modifier and Type Field Description static ResourceNameNormalizer
RESOURCE_NAME_NORMALIZER
-
Constructor Summary
Constructors Constructor Description NormalizedResources(Map<String,Double> normalizedResources)
Create a new normalized set of resources.NormalizedResources(NormalizedResources other)
Copy constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(WorkerResources value)
Add the resources from a worker to this.void
add(NormalizedResources other)
boolean
anyNonCpuOverZero()
Are any of the non cpu resources positive.boolean
areAnyOverZero()
Are any of the resources positive.double
calculateAveragePercentageUsedBy(NormalizedResources used, double totalMemoryMb, double usedMemoryMb)
Calculate the average resource usage percentage with this being the total resources and used being the amounts used.double
calculateMinPercentageUsedBy(NormalizedResources used, double totalMemoryMb, double usedMemoryMb)
Calculate the minimum resource usage percentage with this being the total resources and used being the amounts used.void
clear()
Set all resources to 0.boolean
couldHoldIgnoringSharedMemory(NormalizedResources other, double thisTotalMemoryMb, double otherTotalMemoryMb)
A simple sanity check to see if all of the resources in this would be large enough to hold the resources in other ignoring memory.boolean
couldHoldIgnoringSharedMemoryAndCpu(NormalizedResources other, double thisTotalMemoryMb, double otherTotalMemoryMb)
A simple sanity check to see if all of the resources in this would be large enough to hold the resources in other ignoring memory.double
getTotalCpu()
Get the total amount of cpu.boolean
remove(WorkerResources value)
Remove the resources of a worker from this.boolean
remove(NormalizedResources other, ResourceMetrics resourceMetrics)
Remove the other resources from this.static void
resetResourceNames()
This is for testing only.Map<String,Double>
toNormalizedMap()
Return a Map of the normalized resource name to a double.String
toString()
void
updateForRareResourceAffinity(NormalizedResources request)
If a node or rack has a kind of resource not in a request, make that resource negative so when sorting that node or rack will be less likely to be selected.
-
-
-
Field Detail
-
RESOURCE_NAME_NORMALIZER
public static ResourceNameNormalizer RESOURCE_NAME_NORMALIZER
-
-
Constructor Detail
-
NormalizedResources
public NormalizedResources(NormalizedResources other)
Copy constructor.
-
NormalizedResources
public NormalizedResources(Map<String,Double> normalizedResources)
Create a new normalized set of resources. Note that memory is not managed by this class, as it is not consistent in requests vs offers because of how on heap vs off heap is used.- Parameters:
normalizedResources
- the normalized resource map
-
-
Method Detail
-
resetResourceNames
public static void resetResourceNames()
This is for testing only. It allows a test to reset the static state relating to resource names. We reset the mapping because some algorithms sadly have different behavior if a resource exists or not.
-
getTotalCpu
public double getTotalCpu()
Get the total amount of cpu.- Returns:
- the amount of cpu.
-
add
public void add(NormalizedResources other)
-
add
public void add(WorkerResources value)
Add the resources from a worker to this.- Parameters:
value
- the worker resources that should be added to this.
-
remove
public boolean remove(NormalizedResources other, ResourceMetrics resourceMetrics)
Remove the other resources from this. This is the same as subtracting the resources in other from this.- Parameters:
other
- the resources we want removed.resourceMetrics
- The resource related metrics- Returns:
- true if the resources would have gone negative, but were clamped to 0.
-
remove
public boolean remove(WorkerResources value)
Remove the resources of a worker from this.- Parameters:
value
- the worker resources that should be removed from this.
-
toNormalizedMap
public Map<String,Double> toNormalizedMap()
Return a Map of the normalized resource name to a double. This should only be used when returning thrift resource requests to the end user.
-
couldHoldIgnoringSharedMemory
public boolean couldHoldIgnoringSharedMemory(NormalizedResources other, double thisTotalMemoryMb, double otherTotalMemoryMb)
A simple sanity check to see if all of the resources in this would be large enough to hold the resources in other ignoring memory. It does not check memory because with shared memory it is beyond the scope of this.- Parameters:
other
- the resources that we want to check if they would fit in this.thisTotalMemoryMb
- The total memory in MB of thisotherTotalMemoryMb
- The total memory in MB of other- Returns:
- true if it might fit, else false if it could not possibly fit.
-
couldHoldIgnoringSharedMemoryAndCpu
public boolean couldHoldIgnoringSharedMemoryAndCpu(NormalizedResources other, double thisTotalMemoryMb, double otherTotalMemoryMb)
A simple sanity check to see if all of the resources in this would be large enough to hold the resources in other ignoring memory. It does not check memory because with shared memory it is beyond the scope of this. It also does not check CPU.- Parameters:
other
- the resources that we want to check if they would fit in this.thisTotalMemoryMb
- The total memory in MB of thisotherTotalMemoryMb
- The total memory in MB of other- Returns:
- true if it might fit, else false if it could not possibly fit.
-
calculateAveragePercentageUsedBy
public double calculateAveragePercentageUsedBy(NormalizedResources used, double totalMemoryMb, double usedMemoryMb)
Calculate the average resource usage percentage with this being the total resources and used being the amounts used. Used must be a subset of the total resources. If a resource in the total has a value of zero, it will be skipped in the calculation to avoid division by 0. If all resources are skipped the result is defined to be 100.0.- Parameters:
used
- the amount of resources used.totalMemoryMb
- The total memory in MBusedMemoryMb
- The used memory in MB- Returns:
- the average percentage used 0.0 to 100.0.
- Throws:
IllegalArgumentException
- if any resource in used has a greater value than the same resource in the total, or used has generic resources that are not present in the total.
-
calculateMinPercentageUsedBy
public double calculateMinPercentageUsedBy(NormalizedResources used, double totalMemoryMb, double usedMemoryMb)
Calculate the minimum resource usage percentage with this being the total resources and used being the amounts used. Used must be a subset of the total resources. If a resource in the total has a value of zero, it will be skipped in the calculation to avoid division by 0. If all resources are skipped the result is defined to be 100.0.- Parameters:
used
- the amount of resources used.totalMemoryMb
- The total memory in MBusedMemoryMb
- The used memory in MB- Returns:
- the minimum percentage used 0.0 to 100.0.
- Throws:
IllegalArgumentException
- if any resource in used has a greater value than the same resource in the total, or used has generic resources that are not present in the total.
-
updateForRareResourceAffinity
public void updateForRareResourceAffinity(NormalizedResources request)
If a node or rack has a kind of resource not in a request, make that resource negative so when sorting that node or rack will be less likely to be selected. If the resource is in the request, make that resource positive.- Parameters:
request
- the requested resources.
-
clear
public void clear()
Set all resources to 0.
-
areAnyOverZero
public boolean areAnyOverZero()
Are any of the resources positive.- Returns:
- true of any of the resources are positive. False if they are all <= 0.
-
anyNonCpuOverZero
public boolean anyNonCpuOverZero()
Are any of the non cpu resources positive.- Returns:
- true of any of the non cpu resources are positive. False if they are all <= 0.
-
-