hadoop ResourceVector 源码
haddop ResourceVector 代码
文件路径:/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/ResourceVector.java
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity;
import org.apache.hadoop.yarn.api.records.Resource;
import org.apache.hadoop.yarn.api.records.ResourceInformation;
import org.apache.hadoop.yarn.util.resource.ResourceUtils;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Represents a simple resource floating point value storage
* grouped by resource names.
*/
public class ResourceVector implements Iterable<Map.Entry<String, Float>> {
private final Map<String, Float> resourcesByName = new HashMap<>();
/**
* Creates a new {@code ResourceVector} with all pre-defined resources set to
* zero.
* @return zero resource vector
*/
public static ResourceVector newInstance() {
ResourceVector zeroResourceVector = new ResourceVector();
for (ResourceInformation resource : ResourceUtils.getResourceTypesArray()) {
zeroResourceVector.setValue(resource.getName(), 0);
}
return zeroResourceVector;
}
/**
* Creates a new {@code ResourceVector} with all pre-defined resources set to
* the same value.
* @param value the value to set all resources to
* @return uniform resource vector
*/
public static ResourceVector of(float value) {
ResourceVector emptyResourceVector = new ResourceVector();
for (ResourceInformation resource : ResourceUtils.getResourceTypesArray()) {
emptyResourceVector.setValue(resource.getName(), value);
}
return emptyResourceVector;
}
/**
* Creates a new {@code ResourceVector} with the values set in a
* {@code Resource} object.
* @param resource resource object the resource vector will be based on
* @return uniform resource vector
*/
public static ResourceVector of(Resource resource) {
ResourceVector resourceVector = new ResourceVector();
for (ResourceInformation resourceInformation : resource.getResources()) {
resourceVector.setValue(resourceInformation.getName(),
resourceInformation.getValue());
}
return resourceVector;
}
/**
* Subtract values for each resource defined in the given resource vector.
* @param otherResourceVector rhs resource vector of the subtraction
*/
public void subtract(ResourceVector otherResourceVector) {
for (Map.Entry<String, Float> resource : otherResourceVector) {
setValue(resource.getKey(), getValue(resource.getKey()) - resource.getValue());
}
}
/**
* Increments the given resource by the specified value.
* @param resourceName name of the resource
* @param value value to be added to the resource's current value
*/
public void increment(String resourceName, float value) {
setValue(resourceName, getValue(resourceName) + value);
}
public Float getValue(String resourceName) {
return resourcesByName.get(resourceName);
}
public void setValue(String resourceName, float value) {
resourcesByName.put(resourceName, value);
}
@Override
public Iterator<Map.Entry<String, Float>> iterator() {
return resourcesByName.entrySet().iterator();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return this.resourcesByName.equals(((ResourceVector) o).resourcesByName);
}
@Override
public int hashCode() {
return resourcesByName.hashCode();
}
}
相关信息
相关文章
hadoop AbstractAutoCreatedLeafQueue 源码
hadoop AbstractManagedParentQueue 源码
hadoop AppPriorityACLConfigurationParser 源码
hadoop AutoCreatedLeafQueue 源码
hadoop AutoCreatedLeafQueueConfig 源码
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
7、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦