|
| 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 Glean.Database.Storage.LMDB |
| 10 | + ( LMDB(..) |
| 11 | + , newStorage |
| 12 | + ) where |
| 13 | + |
| 14 | +import Control.Exception |
| 15 | +import Data.Int |
| 16 | +import Foreign.C.String |
| 17 | +import Foreign.C.Types |
| 18 | +import Foreign.ForeignPtr |
| 19 | +import Foreign.Ptr |
| 20 | +import System.Directory |
| 21 | +import System.IO.Temp (withTempDirectory) |
| 22 | +import System.FilePath |
| 23 | +import System.Process (callProcess) |
| 24 | + |
| 25 | +import Util.FFI |
| 26 | +import Util.IO (safeRemovePathForcibly) |
| 27 | + |
| 28 | +import Glean.Database.Backup.Backend (Data(Data)) |
| 29 | +import Glean.Database.Repo (databasePath) |
| 30 | +import Glean.Database.Storage |
| 31 | +import Glean.Database.Storage.DB |
| 32 | +import Glean.FFI |
| 33 | +import Glean.RTS.Foreign.Lookup (CanLookup(..)) |
| 34 | +import Glean.RTS.Foreign.Ownership as Ownership |
| 35 | + hiding (computeDerivedOwnership) |
| 36 | +import Glean.RTS.Types (Fid(..), invalidFid) |
| 37 | +import qualified Glean.ServerConfig.Types as ServerConfig |
| 38 | +import Glean.Types (Repo) |
| 39 | +import Glean.Util.Disk |
| 40 | +import Glean.Impl.MemoryReader |
| 41 | +import System.IO.Extra (withTempFile) |
| 42 | + |
| 43 | +data LMDB = LMDB |
| 44 | + { lmdbRoot :: FilePath |
| 45 | + , lmdbMaxDiskSize :: Maybe Int |
| 46 | + -- ^ virtual limit to report capped disk capacities. The limit is |
| 47 | + -- not enforced. It's up to each io usage to check diskspace before writing. |
| 48 | + -- We're using this to avoid serving too many dbs on query servers, |
| 49 | + -- and smarter sharding. |
| 50 | + } |
| 51 | + |
| 52 | +newStorage :: FilePath -> ServerConfig.Config -> IO LMDB |
| 53 | +newStorage root ServerConfig.Config{..} = do |
| 54 | + mem_capacity <- totalMemCapacityKB |
| 55 | + return LMDB |
| 56 | + { lmdbRoot = root |
| 57 | + , lmdbMaxDiskSize = case mem_capacity of |
| 58 | + Just mem -> (* (mem * 1024)) . fromIntegral <$> |
| 59 | + config_db_rocksdb_disk_mem_capacity_ratio_limit |
| 60 | + Nothing -> Nothing |
| 61 | + } |
| 62 | + |
| 63 | +newtype instance Database LMDB = Database DB |
| 64 | + deriving (CanLookup) |
| 65 | + |
| 66 | +instance Storage LMDB where |
| 67 | + describe db = "lmdb:" <> lmdbRoot db |
| 68 | + |
| 69 | + open lmdb repo mode (DBVersion version) = do |
| 70 | + (cmode, start, ownership) <- case mode of |
| 71 | + ReadOnly -> return (0, invalidFid, Nothing) |
| 72 | + ReadWrite -> return (1, invalidFid, Nothing) |
| 73 | + Create start ownership _ -> do |
| 74 | + createDirectoryIfMissing True path |
| 75 | + return (2, start, ownership) |
| 76 | + withCString path $ \cpath -> |
| 77 | + using (invoke $ glean_lmdb_container_open cpath cmode) |
| 78 | + $ \container -> do |
| 79 | + fp <- mask_ $ do |
| 80 | + first_unit_id <- maybe (return firstUsetId) nextUsetId ownership |
| 81 | + p <- invoke $ |
| 82 | + glean_lmdb_container_open_database container start |
| 83 | + first_unit_id version |
| 84 | + newForeignPtr glean_rocksdb_database_free p |
| 85 | + return (Database (DB (castForeignPtr fp) repo)) |
| 86 | + where |
| 87 | + path = containerPath lmdb repo |
| 88 | + |
| 89 | + delete lmdb = safeRemovePathForcibly . containerPath lmdb |
| 90 | + |
| 91 | + safeRemoveForcibly lmdb = |
| 92 | + safeRemovePathForcibly . databasePath (lmdbRoot lmdb) |
| 93 | + |
| 94 | + getTotalCapacity lmdb = do |
| 95 | + exists <- doesDirectoryExist (lmdbRoot lmdb) |
| 96 | + if exists |
| 97 | + then do |
| 98 | + fullDiskCapacity <- getDiskSize (lmdbRoot lmdb) |
| 99 | + return $ Just $ case lmdbMaxDiskSize lmdb of |
| 100 | + Just maxDiskSize -> min maxDiskSize fullDiskCapacity |
| 101 | + Nothing -> fullDiskCapacity |
| 102 | + else return Nothing |
| 103 | + |
| 104 | + getUsedCapacity lmdb = do |
| 105 | + exists <- doesDirectoryExist (lmdbRoot lmdb) |
| 106 | + if exists |
| 107 | + then Just <$> getUsedDiskSpace (lmdbRoot lmdb) |
| 108 | + else return Nothing |
| 109 | + |
| 110 | + getFreeCapacity lmdb = do |
| 111 | + used <- getUsedCapacity lmdb |
| 112 | + total <- getTotalCapacity lmdb |
| 113 | + case (used,total) of |
| 114 | + (Just used, Just total) -> return $ total - used |
| 115 | + _ -> getFreeDiskSpace (lmdbRoot lmdb) -- not aware of disk limit |
| 116 | + |
| 117 | + withScratchRoot rocks f = f $ lmdbRoot rocks </> ".scratch" |
| 118 | + |
| 119 | + restore lmdb repo scratch scratch_file = |
| 120 | + withTempDirectory scratch "restore" $ \scratch_restore -> do |
| 121 | + let scratch_restore_backup = scratch_restore </> "backup" |
| 122 | + callProcess "lz4" ["-qd", scratch_file, scratch_restore_backup ] |
| 123 | + -- to avoid retaining an extra copy of the DB during restore, |
| 124 | + -- delete the input file now. |
| 125 | + removeFile scratch_file |
| 126 | + |
| 127 | + let db = scratch </> "db" |
| 128 | + createDirectoryIfMissing True db |
| 129 | + withCString db $ \p_target -> |
| 130 | + withCString scratch_restore_backup $ \p_source -> |
| 131 | + invoke $ glean_lmdb_restore p_target p_source |
| 132 | + |
| 133 | + let target = containerPath lmdb repo |
| 134 | + createDirectoryIfMissing True $ takeDirectory target |
| 135 | + renameDirectory db target |
| 136 | + |
| 137 | + |
| 138 | +containerPath :: LMDB -> Repo -> FilePath |
| 139 | +containerPath LMDB{..} repo = databasePath lmdbRoot repo </> "db" |
| 140 | + |
| 141 | +instance DatabaseOps (Database LMDB) where |
| 142 | + close (Database db) = close db |
| 143 | + predicateStats (Database db) = predicateStats db |
| 144 | + store (Database db) = store db |
| 145 | + retrieve (Database db) = retrieve db |
| 146 | + commit (Database db) = commit db |
| 147 | + addOwnership (Database db) = addOwnership db |
| 148 | + optimize (Database db) = optimize db |
| 149 | + computeOwnership (Database db) = computeOwnership db |
| 150 | + storeOwnership (Database db) = storeOwnership db |
| 151 | + getOwnership (Database db) = getOwnership db |
| 152 | + getUnitId (Database db) = getUnitId db |
| 153 | + getUnit (Database db) = getUnit db |
| 154 | + addDefineOwnership (Database db) = addDefineOwnership db |
| 155 | + computeDerivedOwnership (Database db) = computeDerivedOwnership db |
| 156 | + cacheOwnership (Database db) = cacheOwnership db |
| 157 | + prepareFactOwnerCache (Database db) = prepareFactOwnerCache db |
| 158 | + |
| 159 | + backup (Database db) scratch process = do |
| 160 | + backup db scratch $ \path _ -> |
| 161 | + withTempFile $ \zFile -> do |
| 162 | + callProcess "lz4" ["-qf", path, zFile] |
| 163 | + size <- getFileSize zFile |
| 164 | + process zFile (Data $ fromIntegral size) |
| 165 | + |
| 166 | +foreign import ccall safe glean_lmdb_container_open |
| 167 | + :: CString |
| 168 | + -> CInt |
| 169 | + -> Ptr Container |
| 170 | + -> IO CString |
| 171 | + |
| 172 | +foreign import ccall safe glean_lmdb_container_open_database |
| 173 | + :: Container |
| 174 | + -> Fid |
| 175 | + -> UsetId |
| 176 | + -> Int64 |
| 177 | + -> Ptr (Ptr (Database LMDB)) |
| 178 | + -> IO CString |
| 179 | +foreign import ccall safe "&glean_rocksdb_database_free" |
| 180 | + glean_rocksdb_database_free :: Destroy (Database LMDB) |
| 181 | + |
| 182 | +foreign import ccall safe glean_lmdb_restore |
| 183 | + :: CString |
| 184 | + -> CString |
| 185 | + -> IO CString |
0 commit comments