Skip to content

Commit ab8fc99

Browse files
committed
[lmdb facebookincubator#6] Move mksquashfs flags to a config field
1 parent 9a6c409 commit ab8fc99

8 files changed

Lines changed: 24 additions & 18 deletions

File tree

glean/config/server/server_config.thrift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,10 @@ struct Config {
320320
// How much of total memory capacity to use for rocksdb cache.
321321
// Overrides db_rocksdb_cache_mb if set.
322322
38: optional float db_rocksdb_cache_to_mem_ratio;
323+
324+
// Args to pass to mksquashfs when creating a backup of an LMDB
325+
39: list<string> db_lmdb_mksquashfs_args =
326+
[ "-comp", "zstd", "-Xcompression-level", "8" ];
323327
}
324328

325329
// The following were automatically generated and may benefit from renaming.

glean/db/Glean/Database/Backup.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ doBackup env@Env{..} repo prefix site =
236236
say log s = log $ inRepo repo $ "backup: " ++ s
237237

238238
backup = loggingAction (runLogRepo "backup" env repo) (const mempty) $ do
239-
ServerConfig.Config{..} <- Observed.get envServerConfig
239+
cfg@ServerConfig.Config{..} <- Observed.get envServerConfig
240240
meta <- atomically $ Catalog.readMeta envCatalog repo
241241
let excluded =
242242
hasExcludeProperty repo (metaProperties meta) config_retention
@@ -253,7 +253,7 @@ doBackup env@Env{..} repo prefix site =
253253
withStorageFor env repo meta $ \storage -> do
254254

255255
Backend.Data{..} <- withScratchDirectory storage repo $ \scratch ->
256-
Storage.backup odbHandle scratch $ \path Data{dataSize} -> do
256+
Storage.backup odbHandle cfg scratch $ \path Data{dataSize} -> do
257257
say logInfo "uploading"
258258
let policy = ServerConfig.databaseBackupPolicy_repos config_backup
259259
ttl = case Map.lookup (repo_name repo) policy of

glean/db/Glean/Database/Storage.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Glean.RTS.Foreign.Lookup (CanLookup(..), Lookup)
3232
import Glean.RTS.Foreign.Ownership hiding (computeDerivedOwnership)
3333
import Glean.RTS.Types (Fid, Pid)
3434
import Glean.ServerConfig.Types (DBVersion(..))
35+
import qualified Glean.ServerConfig.Types as ServerConfig
3536
import Glean.Types (PredicateStats, Repo, SchemaId)
3637
import Glean.Util.Some
3738

@@ -202,6 +203,7 @@ class CanLookup db => DatabaseOps db where
202203
-- the operation completes.
203204
backup
204205
:: db -- ^ database
206+
-> ServerConfig.Config -- ^ server config
205207
-> FilePath -- ^ scratch directory
206208
-> (FilePath -> Data -> IO a)
207209
-- ^ function which expects the serialised database

glean/db/Glean/Database/Storage/DB.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Foreign.ForeignPtr
2323
import Foreign.Marshal.Array
2424
import Foreign.Ptr
2525
import Foreign.Storable
26+
import System.Directory
2627
import System.FilePath
2728

2829
import Util.FFI
@@ -156,12 +157,12 @@ instance DatabaseOps DB where
156157
unsafeWithForeignPtr (dbPtr db) $ \db_ptr ->
157158
invoke $ glean_rocksdb_prepare_fact_owner_cache db_ptr
158159

159-
backup db scratch process = do
160+
backup db _ scratch process = do
161+
let path = scratch </> "backup"
162+
createDirectoryIfMissing True path
160163
withContainer db $ \s_ptr ->
161164
withCString path $ invoke . glean_rocksdb_container_backup s_ptr
162165
process path (Data 0)
163-
where
164-
path = scratch </> "backup"
165166

166167
newtype Container = Container (Ptr Container)
167168
deriving(Storable)

glean/db/Glean/Database/Storage/LMDB.hs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Glean.Database.Storage.LMDB
1313

1414
import Control.Exception
1515
import Data.Int
16+
import qualified Data.Text as Text
1617
import Foreign.C.String
1718
import Foreign.C.Types
1819
import Foreign.ForeignPtr
@@ -126,7 +127,6 @@ instance Storage LMDB where
126127
createDirectoryIfMissing True $ takeDirectory target
127128
renameDirectory db target
128129

129-
130130
containerPath :: LMDB -> Repo -> FilePath
131131
containerPath LMDB{..} repo = databasePath lmdbRoot repo </> "db"
132132

@@ -148,17 +148,16 @@ instance DatabaseOps (Database LMDB) where
148148
cacheOwnership (Database db) = cacheOwnership db
149149
prepareFactOwnerCache (Database db) = prepareFactOwnerCache db
150150

151-
backup (Database db) scratch process = do
152-
createDirectoryIfMissing True (scratch </> "backup")
153-
backup db scratch $ \_ _ -> do
151+
backup (Database db) cfg scratch process =
152+
backup db cfg scratch $ \path _ -> do
154153
withTempDirectory scratch "out" $ \tmpdir -> do
155154
let out = tmpdir </> "db.squashfs"
156-
callProcess "mksquashfs" [
157-
scratch </> "backup", out,
158-
"-comp", "zstd", "-Xcompression-level", "8" ]
155+
callProcess "mksquashfs" $ [ path, out ] <>
156+
map Text.unpack (ServerConfig.config_db_lmdb_mksquashfs_args cfg)
159157
size <- getFileSize out
160158
process out (Data $ fromIntegral size)
161159

160+
162161
foreign import ccall safe glean_lmdb_container_open
163162
:: CString
164163
-> CInt

glean/db/Glean/Database/Storage/Memory.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,4 @@ instance DatabaseOps (Database Memory) where
117117
prepareFactOwnerCache _ = return ()
118118

119119
-- TODO
120-
backup db _ _ = dbError (dbRepo db) "unimplemented 'backup'"
120+
backup db _ _ _ = dbError (dbRepo db) "unimplemented 'backup'"

glean/db/Glean/Database/Storage/RocksDB.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,11 @@ instance DatabaseOps (Database RocksDB) where
211211
cacheOwnership (Database db) = cacheOwnership db
212212
prepareFactOwnerCache (Database db) = prepareFactOwnerCache db
213213

214-
backup (Database db) scratch process = do
215-
createDirectoryIfMissing True (scratch </> "backup")
216-
backup db scratch $ \_ _ ->
214+
backup (Database db) cfg scratch process = do
215+
backup db cfg scratch $ \path _ -> do
216+
let (base, dir) = splitFileName path
217217
withTempFile $ \tarFile -> do
218-
tar ["-cf", tarFile, "-C", scratch, "backup"]
218+
tar ["-cf", tarFile, "-C", base, dir]
219219
size <- getFileSize tarFile
220220
process tarFile (Data $ fromIntegral size)
221221

glean/test/tests/DatabaseJanitorTest.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ makeFakeCloudDB schema backupDir repo dbtime completeness opts = do
229229
storeSchema hdl $ toStoredSchema schema
230230
tmpDir <- getCanonicalTemporaryDirectory
231231
withTempDirectory tmpDir "scratch" $ \scratch ->
232-
Storage.backup hdl scratch $ \file _data ->
232+
Storage.backup hdl def scratch $ \file _data ->
233233
void $ backup (mockSite backupDir) repo props Nothing file
234234
)
235235
where

0 commit comments

Comments
 (0)