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.
Modifier and Type | Field and Description |
---|---|
static ResourceNameNormalizer |
RESOURCE_NAME_NORMALIZER |
Constructor and Description |
---|
NormalizedResources(Map<String,Double> normalizedResources)
Create a new normalized set of resources.
|
NormalizedResources(NormalizedResources other)
Copy constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
add(NormalizedResources other) |
void |
add(WorkerResources value)
Add the resources from a worker to this.
|
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(NormalizedResources other,
ResourceMetrics resourceMetrics)
Remove the other resources from this.
|
boolean |
remove(WorkerResources value)
Remove the resources of a worker 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.
|
public static ResourceNameNormalizer RESOURCE_NAME_NORMALIZER
public NormalizedResources(NormalizedResources other)
Copy constructor.
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.
normalizedResources
- the normalized resource mappublic 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.
public double getTotalCpu()
Get the total amount of cpu.
public void add(NormalizedResources other)
public void add(WorkerResources value)
Add the resources from a worker to this.
value
- the worker resources that should be added to this.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.
other
- the resources we want removed.resourceMetrics
- The resource related metricspublic boolean remove(WorkerResources value)
Remove the resources of a worker from this.
value
- the worker resources that should be removed from this.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.
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.
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 otherpublic 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.
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 otherpublic 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.
used
- the amount of resources used.totalMemoryMb
- The total memory in MBusedMemoryMb
- The used memory in MBIllegalArgumentException
- 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.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.
used
- the amount of resources used.totalMemoryMb
- The total memory in MBusedMemoryMb
- The used memory in MBIllegalArgumentException
- 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.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.
request
- the requested resources.public void clear()
Set all resources to 0.
public boolean areAnyOverZero()
Are any of the resources positive.
Copyright © 2022 The Apache Software Foundation. All rights reserved.