Skip to content

wrap SqliteCodebase.syncInternal in an immediate transaction #1966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions codebase2/codebase-sqlite/U/Codebase/Sqlite/Queries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,14 @@ headMay :: [a] -> Maybe a
headMay [] = Nothing
headMay (a : _) = Just a

-- | low-level transaction stuff
beginTransaction, beginImmediateTransaction, beginExclusiveTransaction, commitTransaction, rollbackTransaction :: DB m => m ()
beginTransaction = execute_ "BEGIN TRANSACTION"
beginImmediateTransaction = execute_ "BEGIN IMMEDIATE TRANSACTION"
beginExclusiveTransaction = execute_ "BEGIN EXCLUSIVE TRANSACTION"
commitTransaction = execute_ "COMMIT TRANSACTION"
rollbackTransaction = execute_ "ROLLBACK TRANSACTION"

-- * orphan instances

deriving via Text instance ToField Base32Hex
Expand Down
18 changes: 14 additions & 4 deletions parser-typechecker/src/Unison/Codebase/SqliteCodebase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.IO as TextIO
import Data.Traversable (for)
import qualified Data.Validation as Validation
import Data.Word (Word64)
import Database.SQLite.Simple (Connection)
import qualified Database.SQLite.Simple as Sqlite
Expand Down Expand Up @@ -99,9 +98,10 @@ import UnliftIO.Directory (canonicalizePath, createDirectoryIfMissing, doesDirec
import UnliftIO.STM
import U.Codebase.Sqlite.DbId (SchemaVersion(SchemaVersion))

debug, debugProcessBranches :: Bool
debug, debugProcessBranches, debugCommitFailedTransaction :: Bool
debug = False
debugProcessBranches = False
debugCommitFailedTransaction = False

codebasePath :: FilePath
codebasePath = ".unison" </> "v2" </> "unison.sqlite3"
Expand Down Expand Up @@ -720,7 +720,9 @@ sqliteCodebase root = do
Connection ->
Branch m ->
m ()
syncInternal progress srcConn destConn b = do
syncInternal progress srcConn destConn b = time "syncInternal" do
runDB srcConn Q.beginTransaction
runDB destConn Q.beginImmediateTransaction
result <- runExceptT do
let syncEnv = Sync22.Env srcConn destConn (16 * 1024 * 1024)
-- we want to use sync22 wherever possible
Expand Down Expand Up @@ -780,7 +782,15 @@ sqliteCodebase root = do
testWatchRefs <- lift . fmap concat $ for [WK.TestWatch] \wk ->
fmap (Sync22.W wk) <$> flip runReaderT srcConn (Q.loadWatchesByWatchKind wk)
se . r $ Sync.sync sync progress' testWatchRefs
pure $ Validation.valueOr (error . show) result
let
onSuccess a = runDB destConn Q.commitTransaction *> pure a
onFailure e = do
if debugCommitFailedTransaction
then runDB destConn Q.commitTransaction
else runDB destConn Q.rollbackTransaction
error (show e)
runDB srcConn Q.rollbackTransaction -- (we don't write to the src anyway)
either onFailure onSuccess result
let finalizer :: MonadIO m => m ()
finalizer = do
liftIO $ Sqlite.close conn
Expand Down