forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-7928] Move Resource out of ResourceSpec
- Loading branch information
1 parent
5643d15
commit 76e3156
Showing
5 changed files
with
209 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
flink-core/src/main/java/org/apache/flink/api/common/resources/GPUResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* 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 | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.flink.api.common.resources; | ||
|
||
import org.apache.flink.annotation.Internal; | ||
import org.apache.flink.api.common.operators.ResourceSpec; | ||
|
||
/** | ||
* The GPU resource. | ||
*/ | ||
@Internal | ||
public class GPUResource extends Resource { | ||
|
||
private static final long serialVersionUID = -2276080061777135142L; | ||
|
||
public GPUResource(double value) { | ||
this(value, ResourceAggregateType.AGGREGATE_TYPE_SUM); | ||
} | ||
|
||
private GPUResource(double value, ResourceAggregateType type) { | ||
super(ResourceSpec.GPU_NAME, value, type); | ||
} | ||
|
||
@Override | ||
public Resource create(double value, ResourceAggregateType type) { | ||
return new GPUResource(value, type); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
flink-core/src/main/java/org/apache/flink/api/common/resources/Resource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* 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 | ||
* | ||
* http:https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* 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.flink.api.common.resources; | ||
|
||
import org.apache.flink.annotation.Internal; | ||
import org.apache.flink.util.Preconditions; | ||
|
||
import java.io.Serializable; | ||
|
||
import static org.apache.flink.util.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Base class for resources one can specify. | ||
*/ | ||
@Internal | ||
public abstract class Resource implements Serializable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
/** | ||
* Enum defining how resources are aggregated. | ||
*/ | ||
public enum ResourceAggregateType { | ||
/** | ||
* Denotes keeping the sum of the values with same name when merging two resource specs for operator chaining. | ||
*/ | ||
AGGREGATE_TYPE_SUM, | ||
|
||
/** | ||
* Denotes keeping the max of the values with same name when merging two resource specs for operator chaining. | ||
*/ | ||
AGGREGATE_TYPE_MAX | ||
} | ||
|
||
private final String name; | ||
|
||
private final double value; | ||
|
||
private final ResourceAggregateType resourceAggregateType; | ||
|
||
protected Resource(String name, double value, ResourceAggregateType type) { | ||
this.name = checkNotNull(name); | ||
this.value = value; | ||
this.resourceAggregateType = checkNotNull(type); | ||
} | ||
|
||
public Resource merge(Resource other) { | ||
Preconditions.checkArgument(getClass() == other.getClass(), "Merge with different resource resourceAggregateType"); | ||
Preconditions.checkArgument(this.name.equals(other.name), "Merge with different resource name"); | ||
Preconditions.checkArgument(this.resourceAggregateType == other.resourceAggregateType, "Merge with different aggregate resourceAggregateType"); | ||
|
||
final double aggregatedValue; | ||
switch (resourceAggregateType) { | ||
case AGGREGATE_TYPE_MAX : | ||
aggregatedValue = Math.max(other.value, this.value); | ||
break; | ||
|
||
case AGGREGATE_TYPE_SUM: | ||
default: | ||
aggregatedValue = this.value + other.value; | ||
} | ||
|
||
return create(aggregatedValue, resourceAggregateType); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} else if (o != null && getClass() == o.getClass()) { | ||
Resource other = (Resource) o; | ||
|
||
return name.equals(other.name) && resourceAggregateType == other.resourceAggregateType && value == other.value; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = name.hashCode(); | ||
result = 31 * result + resourceAggregateType.ordinal(); | ||
result = 31 * result + (int) value; | ||
return result; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public ResourceAggregateType getResourceAggregateType() { | ||
return this.resourceAggregateType; | ||
} | ||
|
||
public double getValue() { | ||
return this.value; | ||
} | ||
|
||
/** | ||
* Create a resource of the same resource resourceAggregateType. | ||
* | ||
* @param value The value of the resource | ||
* @param type The aggregate resourceAggregateType of the resource | ||
* @return A new instance of the sub resource | ||
*/ | ||
protected abstract Resource create(double value, ResourceAggregateType type); | ||
} |
Oops, something went wrong.