Skip to content

Commit ce9d642

Browse files
KarmaGYZxintongsong
authored andcommitted
[FLINK-22505][core] Limit the scale of Resource to 8
This closes #15815
1 parent f39af7e commit ce9d642

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

flink-core/src/main/java/org/apache/flink/api/common/resources/Resource.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,18 @@
2828
import static org.apache.flink.util.Preconditions.checkArgument;
2929
import static org.apache.flink.util.Preconditions.checkNotNull;
3030

31-
/** Base class for resources one can specify. */
31+
/**
32+
* Base class for resources one can specify. Notice that the scale of value will be limited to
33+
* {@link #MAX_VALUE_SCALE} and all the trailing zeros will be stripped for readability.
34+
*/
3235
@Internal
3336
public abstract class Resource<T extends Resource<T>>
3437
implements Serializable, Comparable<Resource> {
3538

3639
private static final long serialVersionUID = 1L;
3740

41+
private static final int MAX_VALUE_SCALE = 8;
42+
3843
private final String name;
3944

4045
private final BigDecimal value;
@@ -45,11 +50,14 @@ protected Resource(String name, double value) {
4550

4651
protected Resource(String name, BigDecimal value) {
4752
checkNotNull(value);
53+
final BigDecimal valueRoundDown =
54+
value.setScale(MAX_VALUE_SCALE, RoundingMode.DOWN).stripTrailingZeros();
4855
checkArgument(
49-
value.compareTo(BigDecimal.ZERO) >= 0, "Resource value must be no less than 0");
56+
valueRoundDown.compareTo(BigDecimal.ZERO) >= 0,
57+
"Resource value must be no less than 0");
5058

5159
this.name = checkNotNull(name);
52-
this.value = value;
60+
this.value = valueRoundDown;
5361
}
5462

5563
public T merge(T other) {
@@ -80,7 +88,7 @@ public T multiply(int multiplier) {
8088
}
8189

8290
public T divide(BigDecimal by) {
83-
return create(value.divide(by, 16, RoundingMode.DOWN));
91+
return create(value.divide(by, MAX_VALUE_SCALE, RoundingMode.DOWN));
8492
}
8593

8694
public T divide(int by) {

flink-core/src/test/java/org/apache/flink/api/common/resources/ResourceTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ public void testCompareToFailDifferentName() {
205205
resource1.compareTo(resource2);
206206
}
207207

208+
/** This test assume that the scale limitation is 8. */
209+
@Test
210+
public void testValueScaleLimited() {
211+
final Resource v1 = new TestResource(0.100000001);
212+
assertTestResourceValueEquals(0.1, v1);
213+
214+
final Resource v2 = new TestResource(1.0).divide(3);
215+
assertTestResourceValueEquals(0.33333333, v2);
216+
}
217+
218+
@Test
219+
public void testStripTrailingZeros() {
220+
final Resource v = new TestResource(0.25).multiply(2);
221+
assertThat(v.getValue().toString(), is("0.5"));
222+
}
223+
208224
private static void assertTestResourceValueEquals(final double value, final Resource resource) {
209225
assertEquals(new TestResource(value), resource);
210226
}

flink-kubernetes/src/test/java/org/apache/flink/kubernetes/KubernetesResourceManagerDriverTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,12 @@ protected void validateRequestedResources(
362362
.getMebiBytes())));
363363
assertThat(
364364
resourceRequirements.getRequests().get(Constants.RESOURCE_NAME_CPU).getAmount(),
365-
is(String.valueOf(taskExecutorProcessSpec.getCpuCores().getValue())));
365+
is(
366+
String.valueOf(
367+
taskExecutorProcessSpec
368+
.getCpuCores()
369+
.getValue()
370+
.doubleValue())));
366371

367372
assertThat(
368373
resourceRequirements
@@ -376,7 +381,12 @@ protected void validateRequestedResources(
376381
.getMebiBytes())));
377382
assertThat(
378383
resourceRequirements.getLimits().get(Constants.RESOURCE_NAME_CPU).getAmount(),
379-
is(String.valueOf(taskExecutorProcessSpec.getCpuCores().getValue())));
384+
is(
385+
String.valueOf(
386+
taskExecutorProcessSpec
387+
.getCpuCores()
388+
.getValue()
389+
.doubleValue())));
380390
}
381391

382392
@Override

0 commit comments

Comments
 (0)