|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package com.facebook.presto.memory; |
| 15 | + |
| 16 | +import org.testng.annotations.Test; |
| 17 | + |
| 18 | +import static org.testng.Assert.assertEquals; |
| 19 | + |
| 20 | +/** |
| 21 | + * Behavioral tests for worker-advertised capacity capping of query limits in ClusterMemoryManager. |
| 22 | + * Verifies that effective user/total limits are computed correctly from config and worker capacity. |
| 23 | + */ |
| 24 | +public class TestClusterMemoryManager |
| 25 | +{ |
| 26 | + private static final long CONFIG_MAX_USER_BYTES = 20L << 30; // 20 GB |
| 27 | + private static final long CONFIG_MAX_TOTAL_BYTES = 40L << 30; // 40 GB |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testEffectiveLimitsCappedByWorkerCapacityWhenEnabledAndNotSchedulingOnCoordinator() |
| 31 | + { |
| 32 | + // Worker capacity smaller than configured limits -> effective limits are capped by worker capacity |
| 33 | + long workerCapacityBytes = 10L << 30; // 10 GB |
| 34 | + long[] effective = ClusterMemoryManager.computeEffectiveQueryMemoryLimits( |
| 35 | + CONFIG_MAX_USER_BYTES, |
| 36 | + CONFIG_MAX_TOTAL_BYTES, |
| 37 | + true, // useWorkerAdvertisedMemoryForLimit |
| 38 | + false, // isWorkScheduledOnCoordinator (coordinator does not run tasks) |
| 39 | + workerCapacityBytes); |
| 40 | + |
| 41 | + assertEquals(effective[0], workerCapacityBytes, "effective max user memory should be capped by worker capacity"); |
| 42 | + assertEquals(effective[1], workerCapacityBytes, "effective max total memory should be capped by worker capacity"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testEffectiveLimitsUseConfigWhenWorkerCapacityLargerThanConfig() |
| 47 | + { |
| 48 | + // Worker capacity larger than configured limits -> configured limits still apply |
| 49 | + long workerCapacityBytes = 100L << 30; // 100 GB |
| 50 | + long[] effective = ClusterMemoryManager.computeEffectiveQueryMemoryLimits( |
| 51 | + CONFIG_MAX_USER_BYTES, |
| 52 | + CONFIG_MAX_TOTAL_BYTES, |
| 53 | + true, |
| 54 | + false, |
| 55 | + workerCapacityBytes); |
| 56 | + |
| 57 | + assertEquals(effective[0], CONFIG_MAX_USER_BYTES, "effective max user memory should remain config limit"); |
| 58 | + assertEquals(effective[1], CONFIG_MAX_TOTAL_BYTES, "effective max total memory should remain config limit"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testEffectiveLimitsUnchangedWhenUseWorkerAdvertisedDisabled() |
| 63 | + { |
| 64 | + // useWorkerAdvertisedMemoryForLimit = false -> config limits used regardless of worker capacity |
| 65 | + long workerCapacityBytes = 10L << 30; // 10 GB (smaller than config) |
| 66 | + long[] effective = ClusterMemoryManager.computeEffectiveQueryMemoryLimits( |
| 67 | + CONFIG_MAX_USER_BYTES, |
| 68 | + CONFIG_MAX_TOTAL_BYTES, |
| 69 | + false, // useWorkerAdvertisedMemoryForLimit |
| 70 | + false, |
| 71 | + workerCapacityBytes); |
| 72 | + |
| 73 | + assertEquals(effective[0], CONFIG_MAX_USER_BYTES, "effective max user memory should be config when flag disabled"); |
| 74 | + assertEquals(effective[1], CONFIG_MAX_TOTAL_BYTES, "effective max total memory should be config when flag disabled"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testEffectiveLimitsUnchangedWhenCoordinatorSchedulesWork() |
| 79 | + { |
| 80 | + // isWorkScheduledOnCoordinator = true -> config limits used (coordinator is a worker) |
| 81 | + long workerCapacityBytes = 10L << 30; // 10 GB |
| 82 | + long[] effective = ClusterMemoryManager.computeEffectiveQueryMemoryLimits( |
| 83 | + CONFIG_MAX_USER_BYTES, |
| 84 | + CONFIG_MAX_TOTAL_BYTES, |
| 85 | + true, |
| 86 | + true, // isWorkScheduledOnCoordinator |
| 87 | + workerCapacityBytes); |
| 88 | + |
| 89 | + assertEquals(effective[0], CONFIG_MAX_USER_BYTES, "effective max user memory should be config when coordinator schedules work"); |
| 90 | + assertEquals(effective[1], CONFIG_MAX_TOTAL_BYTES, "effective max total memory should be config when coordinator schedules work"); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void testEffectiveLimitsUseConfigWhenWorkerCapacityZero() |
| 95 | + { |
| 96 | + // No workers reported yet (workerTotalCapacity = 0) -> config limits used |
| 97 | + long[] effective = ClusterMemoryManager.computeEffectiveQueryMemoryLimits( |
| 98 | + CONFIG_MAX_USER_BYTES, |
| 99 | + CONFIG_MAX_TOTAL_BYTES, |
| 100 | + true, |
| 101 | + false, |
| 102 | + 0); |
| 103 | + |
| 104 | + assertEquals(effective[0], CONFIG_MAX_USER_BYTES, "effective max user memory should be config when no worker capacity"); |
| 105 | + assertEquals(effective[1], CONFIG_MAX_TOTAL_BYTES, "effective max total memory should be config when no worker capacity"); |
| 106 | + } |
| 107 | +} |
0 commit comments