|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.flink.autoscaler.tuning; |
| 19 | + |
| 20 | +import org.apache.flink.api.common.JobID; |
| 21 | +import org.apache.flink.autoscaler.JobAutoScalerContext; |
| 22 | +import org.apache.flink.autoscaler.ScalingSummary; |
| 23 | +import org.apache.flink.autoscaler.TestingAutoscalerUtils; |
| 24 | +import org.apache.flink.autoscaler.config.AutoScalerOptions; |
| 25 | +import org.apache.flink.autoscaler.metrics.EvaluatedMetrics; |
| 26 | +import org.apache.flink.autoscaler.metrics.EvaluatedScalingMetric; |
| 27 | +import org.apache.flink.autoscaler.metrics.ScalingMetric; |
| 28 | +import org.apache.flink.configuration.MemorySize; |
| 29 | +import org.apache.flink.runtime.jobgraph.JobVertexID; |
| 30 | + |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | + |
| 34 | +import java.util.Map; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | + |
| 38 | +class MemoryScalingTest { |
| 39 | + |
| 40 | + JobAutoScalerContext<JobID> context = TestingAutoscalerUtils.createResourceAwareContext(); |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + void setup() { |
| 44 | + context.getConfiguration().set(AutoScalerOptions.MEMORY_TUNING_ENABLED, true); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void testMemoryScalingDownscaling() { |
| 49 | + int currentParallelism = 20; |
| 50 | + int rescaleParallelism = 10; |
| 51 | + MemorySize currentMemory = MemorySize.parse("10 gb"); |
| 52 | + MemorySize maxMemory = MemorySize.parse("30 gb"); |
| 53 | + |
| 54 | + assertThat( |
| 55 | + runMemoryScaling( |
| 56 | + currentParallelism, |
| 57 | + rescaleParallelism, |
| 58 | + context, |
| 59 | + currentMemory, |
| 60 | + maxMemory)) |
| 61 | + .isEqualTo(MemorySize.parse("20 gb")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void testMemoryScalingUpscaling() { |
| 66 | + int currentParallelism = 10; |
| 67 | + int rescaleParallelism = 20; |
| 68 | + MemorySize currentMemory = MemorySize.parse("10 gb"); |
| 69 | + MemorySize maxMemory = MemorySize.parse("30 gb"); |
| 70 | + |
| 71 | + assertThat( |
| 72 | + runMemoryScaling( |
| 73 | + currentParallelism, |
| 74 | + rescaleParallelism, |
| 75 | + context, |
| 76 | + currentMemory, |
| 77 | + maxMemory)) |
| 78 | + .isEqualTo(MemorySize.parse("10 gb")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void testMemoryScalingDisabled() { |
| 83 | + context.getConfiguration().set(AutoScalerOptions.MEMORY_SCALING_ENABLED, false); |
| 84 | + MemorySize currentMemory = MemorySize.parse("10 gb"); |
| 85 | + MemorySize maxMemory = MemorySize.parse("30 gb"); |
| 86 | + |
| 87 | + assertThat( |
| 88 | + MemoryScaling.applyMemoryScaling( |
| 89 | + currentMemory, maxMemory, context, Map.of(), null)) |
| 90 | + .isEqualTo(currentMemory); |
| 91 | + } |
| 92 | + |
| 93 | + private static MemorySize runMemoryScaling( |
| 94 | + int currentParallelism, |
| 95 | + int rescaleParallelism, |
| 96 | + JobAutoScalerContext<JobID> context, |
| 97 | + MemorySize currentMemory, |
| 98 | + MemorySize maxMemory) { |
| 99 | + var globalMetrics = |
| 100 | + Map.of( |
| 101 | + ScalingMetric.NUM_TASK_SLOTS_USED, |
| 102 | + EvaluatedScalingMetric.of(currentParallelism)); |
| 103 | + var jobVertex1 = new JobVertexID(); |
| 104 | + var jobVertex2 = new JobVertexID(); |
| 105 | + var vertexMetrics = |
| 106 | + Map.of( |
| 107 | + jobVertex1, |
| 108 | + Map.of( |
| 109 | + ScalingMetric.PARALLELISM, |
| 110 | + EvaluatedScalingMetric.of(currentParallelism)), |
| 111 | + jobVertex2, |
| 112 | + Map.of( |
| 113 | + ScalingMetric.PARALLELISM, |
| 114 | + EvaluatedScalingMetric.of(currentParallelism))); |
| 115 | + var metrics = new EvaluatedMetrics(vertexMetrics, globalMetrics); |
| 116 | + |
| 117 | + Map<JobVertexID, ScalingSummary> scalingSummaries = |
| 118 | + Map.of( |
| 119 | + jobVertex1, |
| 120 | + new ScalingSummary( |
| 121 | + currentParallelism, rescaleParallelism, Map.of()), |
| 122 | + jobVertex2, |
| 123 | + new ScalingSummary( |
| 124 | + currentParallelism, rescaleParallelism, Map.of())); |
| 125 | + |
| 126 | + return MemoryScaling.applyMemoryScaling( |
| 127 | + currentMemory, maxMemory, context, scalingSummaries, metrics); |
| 128 | + } |
| 129 | +} |
0 commit comments