|
| 1 | +{- |
| 2 | + Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + All rights reserved. |
| 4 | +
|
| 5 | + This source code is licensed under the BSD-style license found in the |
| 6 | + LICENSE file in the root directory of this source tree. |
| 7 | +-} |
| 8 | + |
| 9 | +module StorageRocksDBTest where |
| 10 | + |
| 11 | +import Data.Default |
| 12 | +import Data.Maybe (isJust) |
| 13 | +import System.IO.Temp (withSystemTempDirectory) |
| 14 | +import Test.HUnit |
| 15 | + |
| 16 | +import Glean.Database.Storage (Storage(..)) |
| 17 | +import qualified Glean.Database.Storage.RocksDB as RocksDB |
| 18 | +import qualified Glean.ServerConfig.Types as ServerConfig |
| 19 | +import TestRunner |
| 20 | +import Glean.Init |
| 21 | +import Glean.Util.Disk (getDiskSize) |
| 22 | + |
| 23 | + |
| 24 | +main :: IO () |
| 25 | +main = withUnitTest $ testRunner $ TestList |
| 26 | + [ TestLabel "Test no disk limit" $ |
| 27 | + TestCase $ withSystemTempDirectory "rocksdb-test" $ \tmpDir -> do |
| 28 | + storage <- RocksDB.newStorage tmpDir def |
| 29 | + assertEqual "No disk limit is set" Nothing |
| 30 | + (RocksDB.rocksMaxDiskSize storage) |
| 31 | + |
| 32 | + fullDiskCapacity <- getDiskSize tmpDir |
| 33 | + Just rocksTotalCapacity <- getTotalCapacity storage |
| 34 | + assertEqual "rocks has full disk capacity" fullDiskCapacity |
| 35 | + rocksTotalCapacity |
| 36 | + |
| 37 | + , TestLabel "Test disk limit with disk-to-mem ratio" $ |
| 38 | + TestCase $ withSystemTempDirectory "rocksdb-test" $ \tmpDir -> do |
| 39 | + let config = def |
| 40 | + { ServerConfig.config_db_rocksdb_disk_mem_capacity_ratio_limit = |
| 41 | + Just 20 |
| 42 | + } |
| 43 | + storage <- RocksDB.newStorage tmpDir config |
| 44 | + assertBool "Disk limit exist" $ |
| 45 | + isJust (RocksDB.rocksMaxDiskSize storage) |
| 46 | + |
| 47 | + , TestLabel "Test getTotalCapacity" $ TestCase $ |
| 48 | + withSystemTempDirectory "rocksdb-test" $ \tmpDir -> do |
| 49 | + storage <- RocksDB.newStorage tmpDir def |
| 50 | + |
| 51 | + -- limit is definitely smaller than the actual disk size |
| 52 | + -- limit will be applied |
| 53 | + let maxDiskSmall = 2 * 1024 * 1024 * 1024 -- 2 GB |
| 54 | + storageMocked = storage |
| 55 | + { RocksDB.rocksMaxDiskSize = Just maxDiskSmall } |
| 56 | + totalCapacity <- getTotalCapacity storageMocked |
| 57 | + assertEqual |
| 58 | + "RocksDB disk is limited" |
| 59 | + (Just maxDiskSmall) |
| 60 | + totalCapacity |
| 61 | + |
| 62 | + -- limit is definitely bigger than actual disk size |
| 63 | + -- actual disk capacity should be used |
| 64 | + let maxDiskBig = maxBound :: Int -- ~8000 Petabytes |
| 65 | + storageMocked = storage |
| 66 | + { RocksDB.rocksMaxDiskSize = Just maxDiskBig } |
| 67 | + fullDiskCapacity <- getDiskSize tmpDir |
| 68 | + totalCapacity <- getTotalCapacity storageMocked |
| 69 | + assertEqual |
| 70 | + "RocksDB ignores too big disk limit" |
| 71 | + (Just fullDiskCapacity) |
| 72 | + totalCapacity |
| 73 | + |
| 74 | + -- If totalCapacity is Nothing, Meta's internal load balacing will be |
| 75 | + -- at risk since it depends on this value |
| 76 | + assertBool "totalCapacity is not empty" (isJust totalCapacity) |
| 77 | + ] |
0 commit comments