|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.catalog; |
| 19 | + |
| 20 | +import org.apache.doris.common.Config; |
| 21 | +import org.apache.doris.common.util.DebugPointUtil; |
| 22 | +import org.apache.doris.thrift.TStorageMedium; |
| 23 | + |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.Assert; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +/** |
| 30 | + * Unit tests for DiskInfo class, focusing on exceedLimit() method |
| 31 | + * and debug point functionality for storage medium capacity checks. |
| 32 | + */ |
| 33 | +public class DiskInfoTest { |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setUp() { |
| 37 | + // Enable debug points for testing |
| 38 | + Config.enable_debug_points = true; |
| 39 | + |
| 40 | + // Set test-friendly config values |
| 41 | + // Note: We directly set values without saving originals because |
| 42 | + // in unit test environment Config may not be fully initialized |
| 43 | + Config.storage_min_left_capacity_bytes = 100L * 1024 * 1024; // 100MB |
| 44 | + Config.storage_high_watermark_usage_percent = 85; |
| 45 | + Config.storage_flood_stage_left_capacity_bytes = 50L * 1024 * 1024; // 50MB |
| 46 | + Config.storage_flood_stage_usage_percent = 95; |
| 47 | + |
| 48 | + // Clear any existing debug points |
| 49 | + DebugPointUtil.clearDebugPoints(); |
| 50 | + } |
| 51 | + |
| 52 | + @After |
| 53 | + public void tearDown() { |
| 54 | + // Disable debug points |
| 55 | + Config.enable_debug_points = false; |
| 56 | + |
| 57 | + // Reset config values to reasonable defaults |
| 58 | + Config.storage_min_left_capacity_bytes = 2L * 1024 * 1024 * 1024; // 2GB (default) |
| 59 | + Config.storage_high_watermark_usage_percent = 85; // default |
| 60 | + Config.storage_flood_stage_left_capacity_bytes = 1L * 1024 * 1024 * 1024; // 1GB (default) |
| 61 | + Config.storage_flood_stage_usage_percent = 95; // default |
| 62 | + |
| 63 | + // Clear all debug points |
| 64 | + DebugPointUtil.clearDebugPoints(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Test basic capacity check without debug points |
| 69 | + */ |
| 70 | + @Test |
| 71 | + public void testBasicCapacityCheck() { |
| 72 | + // Case 1: Disk has enough capacity |
| 73 | + DiskInfo diskInfo = new DiskInfo("/data/disk1"); |
| 74 | + diskInfo.setTotalCapacityB(1024L * 1024 * 1024); // 1GB |
| 75 | + diskInfo.setAvailableCapacityB(500L * 1024 * 1024); // 500MB available |
| 76 | + diskInfo.setStorageMedium(TStorageMedium.HDD); |
| 77 | + |
| 78 | + Assert.assertFalse("Disk with 500MB available should not exceed limit", |
| 79 | + diskInfo.exceedLimit(false)); |
| 80 | + |
| 81 | + // Case 2: Disk exceeds capacity limit (low available space) |
| 82 | + diskInfo.setAvailableCapacityB(50L * 1024 * 1024); // 50MB available (< 100MB min) |
| 83 | + Assert.assertTrue("Disk with only 50MB available should exceed limit", |
| 84 | + diskInfo.exceedLimit(false)); |
| 85 | + |
| 86 | + // Case 3: Disk exceeds capacity limit (high usage percentage) |
| 87 | + diskInfo.setAvailableCapacityB(100L * 1024 * 1024); // 100MB available |
| 88 | + diskInfo.setTotalCapacityB(500L * 1024 * 1024); // 500MB total (80% used) |
| 89 | + Assert.assertFalse("Disk with 80% usage should not exceed 85% limit", |
| 90 | + diskInfo.exceedLimit(false)); |
| 91 | + |
| 92 | + diskInfo.setAvailableCapacityB(50L * 1024 * 1024); // 50MB available (90% used) |
| 93 | + Assert.assertTrue("Disk with 90% usage should exceed 85% limit", |
| 94 | + diskInfo.exceedLimit(false)); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Test flood stage capacity check |
| 99 | + */ |
| 100 | + @Test |
| 101 | + public void testFloodStageCheck() { |
| 102 | + DiskInfo diskInfo = new DiskInfo("/data/disk1"); |
| 103 | + diskInfo.setTotalCapacityB(1024L * 1024 * 1024); // 1GB |
| 104 | + diskInfo.setStorageMedium(TStorageMedium.HDD); |
| 105 | + |
| 106 | + // Flood stage uses AND condition (both must be true) |
| 107 | + // Case 1: Low space but not high percentage -> should not exceed |
| 108 | + // 40MB available (< 50MB), but 984MB/1024MB = 96% used is still high |
| 109 | + diskInfo.setAvailableCapacityB(40L * 1024 * 1024); // 40MB available (< 50MB flood stage) |
| 110 | + // Used = Total - Available = 1024MB - 40MB = 984MB (96% > 95%) |
| 111 | + // This WILL exceed because both conditions are met! |
| 112 | + Assert.assertTrue("Low space AND high percentage -> should exceed", |
| 113 | + diskInfo.exceedLimit(true)); |
| 114 | + |
| 115 | + // Case 2: High percentage but enough space -> should not exceed |
| 116 | + diskInfo.setAvailableCapacityB(100L * 1024 * 1024); // 100MB available (> 50MB) |
| 117 | + // Used = 1024MB - 100MB = 924MB (90% < 95%) |
| 118 | + Assert.assertFalse("High percentage but enough space -> should not exceed", |
| 119 | + diskInfo.exceedLimit(true)); |
| 120 | + |
| 121 | + // Case 3: Low percentage but not enough space -> should not exceed |
| 122 | + diskInfo.setTotalCapacityB(500L * 1024 * 1024); // 500MB total |
| 123 | + diskInfo.setAvailableCapacityB(40L * 1024 * 1024); // 40MB available (< 50MB) |
| 124 | + // Used = 500MB - 40MB = 460MB (92% < 95%) |
| 125 | + Assert.assertFalse("Low space but low percentage -> should not exceed", |
| 126 | + diskInfo.exceedLimit(true)); |
| 127 | + |
| 128 | + // Case 4: Both conditions met -> should exceed |
| 129 | + diskInfo.setTotalCapacityB(1024L * 1024 * 1024); // 1GB |
| 130 | + diskInfo.setAvailableCapacityB(30L * 1024 * 1024); // 30MB available (< 50MB) |
| 131 | + // Used = 1024MB - 30MB = 994MB (97% > 95%) |
| 132 | + Assert.assertTrue("Flood stage with both conditions should exceed", |
| 133 | + diskInfo.exceedLimit(true)); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Test debug point: DiskInfo.exceedLimit.ssd.alwaysTrue |
| 138 | + * Forces SSD disks to report as exceed limit |
| 139 | + */ |
| 140 | + @Test |
| 141 | + public void testDebugPointSsdAlwaysTrue() { |
| 142 | + DiskInfo diskInfo = new DiskInfo("/data/ssd1"); |
| 143 | + diskInfo.setTotalCapacityB(1024L * 1024 * 1024); // 1GB |
| 144 | + diskInfo.setAvailableCapacityB(500L * 1024 * 1024); // 500MB available (plenty) |
| 145 | + diskInfo.setStorageMedium(TStorageMedium.SSD); |
| 146 | + |
| 147 | + // Without debug point - should not exceed |
| 148 | + Assert.assertFalse("SSD with plenty space should not exceed", |
| 149 | + diskInfo.exceedLimit(false)); |
| 150 | + |
| 151 | + // Enable debug point |
| 152 | + DebugPointUtil.addDebugPoint("DiskInfo.exceedLimit.ssd.alwaysTrue"); |
| 153 | + |
| 154 | + // With debug point - should always exceed |
| 155 | + Assert.assertTrue("SSD with debug point should always exceed", |
| 156 | + diskInfo.exceedLimit(false)); |
| 157 | + |
| 158 | + // HDD should not be affected |
| 159 | + diskInfo.setStorageMedium(TStorageMedium.HDD); |
| 160 | + Assert.assertFalse("HDD should not be affected by SSD debug point", |
| 161 | + diskInfo.exceedLimit(false)); |
| 162 | + |
| 163 | + // Clear debug point |
| 164 | + DebugPointUtil.clearDebugPoints(); |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Test debug point: DiskInfo.exceedLimit.ssd.alwaysFalse |
| 169 | + * Forces SSD disks to report as available |
| 170 | + */ |
| 171 | + @Test |
| 172 | + public void testDebugPointSsdAlwaysFalse() { |
| 173 | + DiskInfo diskInfo = new DiskInfo("/data/ssd1"); |
| 174 | + diskInfo.setTotalCapacityB(1024L * 1024 * 1024); // 1GB |
| 175 | + diskInfo.setAvailableCapacityB(50L * 1024 * 1024); // 50MB available (low) |
| 176 | + diskInfo.setStorageMedium(TStorageMedium.SSD); |
| 177 | + |
| 178 | + // Without debug point - should exceed |
| 179 | + Assert.assertTrue("SSD with low space should exceed", |
| 180 | + diskInfo.exceedLimit(false)); |
| 181 | + |
| 182 | + // Enable debug point |
| 183 | + DebugPointUtil.addDebugPoint("DiskInfo.exceedLimit.ssd.alwaysFalse"); |
| 184 | + |
| 185 | + // With debug point - should never exceed |
| 186 | + Assert.assertFalse("SSD with debug point should never exceed", |
| 187 | + diskInfo.exceedLimit(false)); |
| 188 | + |
| 189 | + // HDD should not be affected |
| 190 | + diskInfo.setStorageMedium(TStorageMedium.HDD); |
| 191 | + Assert.assertTrue("HDD should not be affected by SSD debug point", |
| 192 | + diskInfo.exceedLimit(false)); |
| 193 | + |
| 194 | + // Clear debug point |
| 195 | + DebugPointUtil.clearDebugPoints(); |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Test debug point: DiskInfo.exceedLimit.hdd.alwaysTrue |
| 200 | + * Forces HDD disks to report as exceed limit |
| 201 | + */ |
| 202 | + @Test |
| 203 | + public void testDebugPointHddAlwaysTrue() { |
| 204 | + DiskInfo diskInfo = new DiskInfo("/data/hdd1"); |
| 205 | + diskInfo.setTotalCapacityB(1024L * 1024 * 1024); // 1GB |
| 206 | + diskInfo.setAvailableCapacityB(500L * 1024 * 1024); // 500MB available (plenty) |
| 207 | + diskInfo.setStorageMedium(TStorageMedium.HDD); |
| 208 | + |
| 209 | + // Without debug point - should not exceed |
| 210 | + Assert.assertFalse("HDD with plenty space should not exceed", |
| 211 | + diskInfo.exceedLimit(false)); |
| 212 | + |
| 213 | + // Enable debug point |
| 214 | + DebugPointUtil.addDebugPoint("DiskInfo.exceedLimit.hdd.alwaysTrue"); |
| 215 | + |
| 216 | + // With debug point - should always exceed |
| 217 | + Assert.assertTrue("HDD with debug point should always exceed", |
| 218 | + diskInfo.exceedLimit(false)); |
| 219 | + |
| 220 | + // SSD should not be affected |
| 221 | + diskInfo.setStorageMedium(TStorageMedium.SSD); |
| 222 | + Assert.assertFalse("SSD should not be affected by HDD debug point", |
| 223 | + diskInfo.exceedLimit(false)); |
| 224 | + |
| 225 | + // Clear debug point |
| 226 | + DebugPointUtil.clearDebugPoints(); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * Test debug point: DiskInfo.exceedLimit.hdd.alwaysFalse |
| 231 | + * Forces HDD disks to report as available |
| 232 | + */ |
| 233 | + @Test |
| 234 | + public void testDebugPointHddAlwaysFalse() { |
| 235 | + DiskInfo diskInfo = new DiskInfo("/data/hdd1"); |
| 236 | + diskInfo.setTotalCapacityB(1024L * 1024 * 1024); // 1GB |
| 237 | + diskInfo.setAvailableCapacityB(50L * 1024 * 1024); // 50MB available (low) |
| 238 | + diskInfo.setStorageMedium(TStorageMedium.HDD); |
| 239 | + |
| 240 | + // Without debug point - should exceed |
| 241 | + Assert.assertTrue("HDD with low space should exceed", |
| 242 | + diskInfo.exceedLimit(false)); |
| 243 | + |
| 244 | + // Enable debug point |
| 245 | + DebugPointUtil.addDebugPoint("DiskInfo.exceedLimit.hdd.alwaysFalse"); |
| 246 | + |
| 247 | + // With debug point - should never exceed |
| 248 | + Assert.assertFalse("HDD with debug point should never exceed", |
| 249 | + diskInfo.exceedLimit(false)); |
| 250 | + |
| 251 | + // SSD should not be affected |
| 252 | + diskInfo.setStorageMedium(TStorageMedium.SSD); |
| 253 | + Assert.assertTrue("SSD should not be affected by HDD debug point", |
| 254 | + diskInfo.exceedLimit(false)); |
| 255 | + |
| 256 | + // Clear debug point |
| 257 | + DebugPointUtil.clearDebugPoints(); |
| 258 | + } |
| 259 | + |
| 260 | + /** |
| 261 | + * Test multiple debug points interaction |
| 262 | + */ |
| 263 | + @Test |
| 264 | + public void testMultipleDebugPoints() { |
| 265 | + DiskInfo ssdDisk = new DiskInfo("/data/ssd1"); |
| 266 | + ssdDisk.setTotalCapacityB(1024L * 1024 * 1024); |
| 267 | + ssdDisk.setAvailableCapacityB(500L * 1024 * 1024); |
| 268 | + ssdDisk.setStorageMedium(TStorageMedium.SSD); |
| 269 | + |
| 270 | + DiskInfo hddDisk = new DiskInfo("/data/hdd1"); |
| 271 | + hddDisk.setTotalCapacityB(1024L * 1024 * 1024); |
| 272 | + hddDisk.setAvailableCapacityB(500L * 1024 * 1024); |
| 273 | + hddDisk.setStorageMedium(TStorageMedium.HDD); |
| 274 | + |
| 275 | + // Enable both alwaysTrue debug points |
| 276 | + DebugPointUtil.addDebugPoint("DiskInfo.exceedLimit.ssd.alwaysTrue"); |
| 277 | + DebugPointUtil.addDebugPoint("DiskInfo.exceedLimit.hdd.alwaysTrue"); |
| 278 | + |
| 279 | + // Both should report exceed limit |
| 280 | + Assert.assertTrue("SSD should exceed with debug point", ssdDisk.exceedLimit(false)); |
| 281 | + Assert.assertTrue("HDD should exceed with debug point", hddDisk.exceedLimit(false)); |
| 282 | + |
| 283 | + // Clear and set alwaysFalse for both |
| 284 | + DebugPointUtil.clearDebugPoints(); |
| 285 | + DebugPointUtil.addDebugPoint("DiskInfo.exceedLimit.ssd.alwaysFalse"); |
| 286 | + DebugPointUtil.addDebugPoint("DiskInfo.exceedLimit.hdd.alwaysFalse"); |
| 287 | + |
| 288 | + // Both should report available |
| 289 | + Assert.assertFalse("SSD should not exceed with debug point", ssdDisk.exceedLimit(false)); |
| 290 | + Assert.assertFalse("HDD should not exceed with debug point", hddDisk.exceedLimit(false)); |
| 291 | + |
| 292 | + // Clear debug points |
| 293 | + DebugPointUtil.clearDebugPoints(); |
| 294 | + } |
| 295 | +} |
| 296 | + |
0 commit comments