Skip to content

Commit 8e0b849

Browse files
committed
[lmdb facebookincubator#8] Add server_config field to select storage
* db_create_storage config to select storage backend ("rocksdb" or "lmdb") * make DB versions Storage-specific, as they should be * fix DatabaseJanitorTest and enable it
1 parent a27f81f commit 8e0b849

15 files changed

Lines changed: 85 additions & 61 deletions

File tree

glean.cabal.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,8 +2140,6 @@ test-suite dbjanitor
21402140
glean:schema,
21412141
glean:config,
21422142
glean:util
2143-
-- https://github.com/facebookincubator/Glean/issues/224
2144-
buildable: False
21452143

21462144
test-suite dbderive
21472145
import: test

glean/config/server/server_config.thrift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ struct Config {
236236
16: i32 db_lookup_cache_limit_mb = 1000;
237237

238238
// What binary representation to use for newly created databases
239-
// (nothing means use latest supported).
239+
// (nothing means use latest supported). See also db_create_storage.
240240
19: optional DBVersion db_create_version;
241241

242242
// Disable completed dependencies check in stored predicates derivation
@@ -325,6 +325,10 @@ struct Config {
325325
// runtime cost to decompress the DB on demand. Mounting requires
326326
// squashfs-tools and squashfuse to be installed on Linux.
327327
40: bool db_lmdb_restore_unpack = true;
328+
329+
// Default storage backend for newly created databases. Can be overriden
330+
// by command-line options. See also db_create_version.
331+
41: optional string db_create_storage;
328332
}
329333

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

glean/db/Glean/Database/Config.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ data DataStore = DataStore
116116
ServerConfig.Config ->
117117
((HashMap StorageName (Some Storage), Some Catalog.Store) -> IO a) ->
118118
IO a
119-
, defaultStorage :: StorageName
119+
, defaultStorage :: (StorageName, Bool)
120+
-- ^ Default storage, Bool => explicitly selected on CLI
121+
-- (otherwise can be overriden by ServerConfig.config_db_create_storage)
120122
, dataStoreTag :: String
121123
}
122124

@@ -137,7 +139,7 @@ fileDataStore path = DataStore
137139
],
138140
Some (Catalog.fileCatalog path)
139141
)
140-
, defaultStorage = rocksdbName
142+
, defaultStorage = (rocksdbName, False)
141143
, dataStoreTag = "db:" <> path
142144
}
143145

@@ -146,7 +148,7 @@ tmpDataStore = DataStore
146148
{ withStorage = \scfg f -> withSystemTempDirectory "glean" $ \tmp -> do
147149
logInfo $ "Storing temporary DBs in " <> tmp
148150
withStorage (fileDataStore tmp) scfg f
149-
, defaultStorage = rocksdbName
151+
, defaultStorage = (rocksdbName, False)
150152
, dataStoreTag = dataStoreTag (fileDataStore "<tmp>")
151153
}
152154

@@ -156,7 +158,7 @@ memoryDataStore = DataStore
156158
cat <- Catalog.memoryCatalog
157159
mem <- Memory.newStorage
158160
f (HashMap.fromList [(memoryName, Some mem)], Some cat)
159-
, defaultStorage = memoryName
161+
, defaultStorage = (memoryName, False)
160162
, dataStoreTag = "memory"
161163
}
162164

@@ -518,7 +520,7 @@ options = do
518520
help "Directory containing databases")
519521
lmdb <- switch (long "lmdb")
520522
pure $
521-
(if lmdb then \s -> s { defaultStorage = lmdbName } else id) $
523+
(if lmdb then \s -> s { defaultStorage = (lmdbName, True) } else id) $
522524
fileDataStore path
523525
dbTmp = tmpDataStore <$ flag' () (
524526
long "db-tmp" <>

glean/db/Glean/Database/Create.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,14 @@ kickOffDatabase env@Env{..} kickOff@Thrift.KickOff{..}
105105
, timestampRepoHash =
106106
posixEpochTimeToUTCTime <$> kickOff_repo_hash_time
107107
}
108+
withDefaultStorage env $ \storageName storage -> do
109+
108110
version <-
109-
fromMaybe Storage.currentVersion . ServerConfig.config_db_create_version
111+
fromMaybe (Storage.currentVersion storage) .
112+
ServerConfig.config_db_create_version
110113
<$> Observed.get envServerConfig
111-
when (not $ Storage.canOpenVersion Storage.ReadWrite version) $
114+
115+
when (not $ Storage.canOpenVersion storage Storage.ReadWrite version) $
112116
dbError kickOff_repo
113117
"can't create databases (unsupported binary version)"
114118
db <- atomically $ newDB kickOff_repo
@@ -122,7 +126,7 @@ kickOffDatabase env@Env{..} kickOff@Thrift.KickOff{..}
122126
-- concept of deleting DBs will change with the new metadata handling so
123127
-- it's not worth fixing at this point, especially since we aren't
124128
-- supposed to be kicking off DBs we've previously deleted.
125-
let meta = newMeta envDefaultStorage version time
129+
let meta = newMeta storageName version time
126130
(Incomplete def) allProps
127131
(lightDeps kickOff_dependencies')
128132
bracket_

glean/db/Glean/Database/Env.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ withDatabases evb cfg cfgapi act =
8989
initEnv
9090
:: EventBaseDataplane
9191
-> HashMap StorageName (Some Storage.Storage)
92-
-> StorageName
92+
-> (StorageName, Bool)
9393
-> Catalog.Catalog
9494
-> SomeShardManager
9595
-> Config
@@ -222,7 +222,7 @@ spawnThreads env@Env{..} = do
222222

223223
-- Disk usage counters
224224
Warden.spawn_ envWarden $ doPeriodically (seconds 600) $
225-
withDefaultStorage env $ \storage -> do
225+
withDefaultStorage env $ \_ storage -> do
226226
diskSize <- Storage.getTotalCapacity storage
227227
diskUsed <- Storage.getUsedCapacity storage
228228

glean/db/Glean/Database/Open.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ withOpenDatabase env@Env{..} repo action =
9999
-- Note: Finalizing also needs to be ReadWrite,
100100
-- because compaction modifies the DB.
101101
where completeness = metaCompleteness meta
102-
when (not $ canOpenVersion mode version) $ dbError dbRepo
103-
$ "can't open database version " ++ show (unDBVersion version)
104102
writeTVar dbState Opening
105103
return $ Right (version, mode)
106104
case r of
@@ -484,10 +482,13 @@ asyncOpenDB env@Env{..} storage db@DB{..} version mode deps
484482
on_success on_failure =
485483
-- Be paranoid about 'spawnMask' itself throwing.
486484
handling_failures $ Warden.spawnMask envWarden $ \restore ->
487-
loggingAction (runLogRepo "open" env dbRepo) (const mempty) $
485+
loggingAction (runLogRepo "open" env dbRepo) (const mempty) $ do
486+
when (not $ canOpenVersion storage mode version) $
487+
dbError dbRepo $
488+
"can't open database version " ++ show (unDBVersion version)
488489
bracket_
489490
(atomically $ acquireDB db)
490-
(atomically $ releaseDB envCatalog envActive db) $
491+
(atomically $ releaseDB envCatalog envActive db) $ do
491492
handling_failures $ do
492493
logInfo $ inRepo dbRepo "opening"
493494
bracketOnError

glean/db/Glean/Database/Storage.hs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ module Glean.Database.Storage
1717
, WriteLock(..)
1818
, canOpenVersion
1919
, currentVersion
20-
, writableVersions
2120
) where
2221

2322
import Data.ByteString (ByteString)
@@ -36,26 +35,18 @@ import qualified Glean.ServerConfig.Types as ServerConfig
3635
import Glean.Types (PredicateStats, Repo, SchemaId)
3736
import Glean.Util.Some
3837

39-
-- | List of binary representation versions we can read
40-
readableVersions :: [DBVersion]
41-
readableVersions = [DBVersion 3]
42-
43-
-- | List of binary representation versions we can write
44-
writableVersions :: [DBVersion]
45-
writableVersions = [DBVersion 3]
46-
4738
-- | Check whether we can open a particular database version
48-
canOpenVersion :: Mode -> DBVersion -> Bool
49-
canOpenVersion mode version = version `elem` versions
39+
canOpenVersion :: Storage s => s -> Mode -> DBVersion -> Bool
40+
canOpenVersion s mode version = version `elem` versions
5041
where
5142
versions = case mode of
52-
ReadOnly -> readableVersions
53-
ReadWrite -> writableVersions
54-
Create{} -> writableVersions
43+
ReadOnly -> readableVersions s
44+
ReadWrite -> writableVersions s
45+
Create{} -> writableVersions s
5546

5647
-- | Default current binary representation version
57-
currentVersion :: DBVersion
58-
currentVersion = maximum writableVersions
48+
currentVersion :: Storage s => s -> DBVersion
49+
currentVersion = maximum . writableVersions
5950

6051
-- Choose which schema goes into a newly created DB
6152
data CreateSchema
@@ -87,6 +78,12 @@ class DatabaseOps (Database s) => Storage s where
8778
-- | A short, user-readable description of the storage
8879
describe :: s -> String
8980

81+
-- | List of binary representation versions we can read
82+
readableVersions :: s -> [DBVersion]
83+
84+
-- | List of binary representation versions we can write
85+
writableVersions :: s -> [DBVersion]
86+
9087
-- | Open a database
9188
open :: s -> Repo -> Mode -> DBVersion -> IO (Database s)
9289

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ instance CanLookup (Database LMDB) where
7070
instance Storage LMDB where
7171
describe db = "lmdb:" <> lmdbRoot db
7272

73+
-- we started at 3 because that's what RocksDB was on
74+
readableVersions _ = [DBVersion 3]
75+
writableVersions _ = [DBVersion 3]
76+
7377
open lmdb repo mode (DBVersion version) = do
7478
(cmode, start, ownership) <- case mode of
7579
ReadOnly -> do

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ instance Storage Memory where
4747

4848
describe = const "memory:"
4949

50+
readableVersions _ = [DBVersion 0]
51+
writableVersions _ = [DBVersion 0]
52+
5053
open (Memory v) repo (Create start _unit _) _ = do
5154
facts <- FactSet.new start
5255
atomically $ do

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ newtype instance Database RocksDB = Database DB
107107
instance Storage RocksDB where
108108
describe rocks = "rocksdb:" <> rocksRoot rocks
109109

110+
readableVersions _ = [DBVersion 3]
111+
writableVersions _ = [DBVersion 3]
112+
110113
open rocks repo mode (DBVersion version) = do
111114
(cmode, start, ownership) <- case mode of
112115
ReadOnly -> return (0, invalidFid, Nothing)

0 commit comments

Comments
 (0)