wip, fixing

This commit is contained in:
voidlizard 2025-08-22 19:45:01 +03:00
parent 0869b57971
commit 8d58c5d818
3 changed files with 133 additions and 110 deletions

View File

@ -169,6 +169,7 @@ ncqStorageRun ncq@NCQStorage{..} = withSem ncqRunSem $ flip runContT pure do
readTVarIO ncqFileLock >>= mapM_ FL.unlockFile readTVarIO ncqFileLock >>= mapM_ FL.unlockFile
ContT $ bracket none $ const $ liftIO do ContT $ bracket none $ const $ liftIO do
void $ ncqStateDump ncq
debug "storage done" debug "storage done"
ncqRemoveGarbage ncq ncqRemoveGarbage ncq
@ -242,10 +243,13 @@ ncqStorageRun ncq@NCQStorage{..} = withSem ncqRunSem $ flip runContT pure do
b <- ncqIndexCompactStep ncq b <- ncqIndexCompactStep ncq
pure $ a || b pure $ a || b
flip fix RunNew $ \loop -> \case flip fix RunNew $ \loop s -> do
-- debug $ viaShow s
case s of
RunFin mfh -> do RunFin mfh -> do
liftIO $ for_ mfh closeFd liftIO $ for_ mfh closeFd
debug "exit storage" rest <- readTVarIO ncqWriteQ <&> Seq.length
debug $ "exit storage" <+> pretty rest
atomically $ pollSTM indexer >>= maybe STM.retry (const none) atomically $ pollSTM indexer >>= maybe STM.retry (const none)
RunNew -> do RunNew -> do
@ -275,11 +279,12 @@ ncqStorageRun ncq@NCQStorage{..} = withSem ncqRunSem $ flip runContT pure do
liftIO (fileSynchronisePortable fh) liftIO (fileSynchronisePortable fh)
flushReplaces fk flushReplaces fk
ncqStateUpdate ncq do
ncqStateAddFact (P (PData (DataFile fk) ss))
-- ss <- liftIO (PFS.getFdStatus fh) <&> fromIntegral . PFS.fileSize -- ss <- liftIO (PFS.getFdStatus fh) <&> fromIntegral . PFS.fileSize
-- atomically $ ncqDeferredWriteOpSTM ncq do -- atomically $ ncqDeferredWriteOpSTM ncq do
ncqStateUpdate ncq do
ncqStateAddFact (P (PData (DataFile fk) ss))
atomically do atomically do
writeTVar ncqSyncReq False writeTVar ncqSyncReq False
@ -308,7 +313,7 @@ ncqStorageRun ncq@NCQStorage{..} = withSem ncqRunSem $ flip runContT pure do
sy <- readTVar ncqSyncReq sy <- readTVar ncqSyncReq
chunk <- if not stop then chunk <- if not stop then
stateTVar ncqWriteQ (Seq.splitAt ncqWriteBlock) stateTVar ncqWriteQ (Seq.splitAt 1)
else do else do
r <- readTVar ncqWriteQ r <- readTVar ncqWriteQ
modifyTVar ncqWriteQ mempty modifyTVar ncqWriteQ mempty
@ -318,14 +323,17 @@ ncqStorageRun ncq@NCQStorage{..} = withSem ncqRunSem $ flip runContT pure do
| Seq.null chunk && not (stop || sy) -> STM.retry | Seq.null chunk && not (stop || sy) -> STM.retry
| otherwise -> pure $ Right chunk | otherwise -> pure $ Right chunk
stop <- readTVarIO ncqStopReq
case chunk of case chunk of
Nothing -> do Nothing -> do
liftIO $ join $ readTVarIO ncqOnRunWriteIdle liftIO $ join $ readTVarIO ncqOnRunWriteIdle
if w == 0 then do stop <- readTVarIO ncqStopReq
if w == 0 && not stop then do
loop $ RunWrite (fk,fh,w,total') loop $ RunWrite (fk,fh,w,total')
else do else do
atomically $ writeTVar ncqSyncReq True atomically $ writeTVar ncqSyncReq True
loop $ RunSync (fk, fh, w, total', True) -- exit () loop $ RunSync (fk, fh, w, total', not stop) -- exit ()
Just (Left{}) -> loop $ RunSync (fk, fh, w, total', False) -- exit () Just (Left{}) -> loop $ RunSync (fk, fh, w, total', False) -- exit ()
@ -335,14 +343,20 @@ ncqStorageRun ncq@NCQStorage{..} = withSem ncqRunSem $ flip runContT pure do
Just (NCQEntry w, EntryHere bs) -> do Just (NCQEntry w, EntryHere bs) -> do
off <- fromIntegral <$> liftIO (fdSeek fh RelativeSeek 0) off <- fromIntegral <$> liftIO (fdSeek fh RelativeSeek 0)
n <- lift (appendSection fh bs) n <- lift (appendSection fh bs)
let op = writeTVar w (EntryThere (FileLocation fk off (fromIntegral n)))
let op = do
readTVar w >>= \case
EntryHere bs1 | bs1 == bs -> do
writeTVar w (EntryThere (FileLocation fk off (fromIntegral n)))
_ -> none
atomically $ modifyTVar ncqReplQueue (HM.insertWith (<>) fk [op]) atomically $ modifyTVar ncqReplQueue (HM.insertWith (<>) fk [op])
pure n pure n
_ -> pure 0 _ -> pure 0
let written = sum ws let written = sum ws
loop $ RunSync (fk, fh, w + written, total' + written, True) loop $ RunSync (fk, fh, w + written, total' + written, not stop)
mapM_ wait [indexer] mapM_ wait [indexer]
@ -350,11 +364,11 @@ ncqStorageRun ncq@NCQStorage{..} = withSem ncqRunSem $ flip runContT pure do
setAlive = atomically $ writeTVar ncqAlive True setAlive = atomically $ writeTVar ncqAlive True
unsetAlive = atomically $ writeTVar ncqAlive False unsetAlive = atomically $ writeTVar ncqAlive False
dropReplaces :: forall m . MonadIO m => FileKey -> m () dropReplaces :: forall m1 . MonadIO m1 => FileKey -> m1 ()
dropReplaces fk = atomically do dropReplaces fk = atomically do
modifyTVar ncqReplQueue (HM.delete fk) modifyTVar ncqReplQueue (HM.delete fk)
flushReplaces :: forall m . MonadIO m => FileKey -> m () flushReplaces :: forall m1 . MonadIO m1 => FileKey -> m1 ()
flushReplaces fk = do flushReplaces fk = do
atomically do atomically do
ncqDelCachedDataSTM ncq fk ncqDelCachedDataSTM ncq fk
@ -368,7 +382,9 @@ ncqStorageRun ncq@NCQStorage{..} = withSem ncqRunSem $ flip runContT pure do
atomically $ modifyTVar ncqCurrentFossils (HS.insert fk) atomically $ modifyTVar ncqCurrentFossils (HS.insert fk)
ncqStateUpdate ncq (ncqStateAddDataFile fk) ncqStateUpdate ncq do
ncqStateAddFact (P (PData (DataFile fk) 0))
ncqStateAddDataFile fk
let fname = ncqGetFileName ncq (DataFile fk) let fname = ncqGetFileName ncq (DataFile fk)
-- touch fname -- touch fname
@ -425,7 +441,6 @@ ncqStorageRun ncq@NCQStorage{..} = withSem ncqRunSem $ flip runContT pure do
when (k2 == k1) $ waitState k2 when (k2 == k1) $ waitState k2
data RunSt = data RunSt =
RunNew RunNew
| RunWrite (FileKey, Fd, Int, Int) | RunWrite (FileKey, Fd, Int, Int)

View File

@ -27,6 +27,17 @@ newtype StateOP a =
{- HLINT ignore "Eta reduce"-} {- HLINT ignore "Eta reduce"-}
ncqStateDump :: MonadIO m
=> NCQStorage
-> m FileKey
ncqStateDump ncq@NCQStorage{..} = do
state <- readTVarIO ncqState
key <- ncqGetNewFileKey ncq StateFile
let snkFile = ncqGetFileName ncq (StateFile key)
liftIO $ withBinaryFileDurableAtomic snkFile WriteMode $ \fh -> do
IO.hPrint fh (pretty state)
pure key
ncqStateUpdateLoop :: MonadIO m ncqStateUpdateLoop :: MonadIO m
=> NCQStorage => NCQStorage
-> m () -> m ()
@ -43,10 +54,7 @@ ncqStateUpdateLoop ncq@NCQStorage{..} = do
stop <- readTVar ncqStopReq stop <- readTVar ncqStopReq
if s1 == s0 && not stop then STM.retry else pure s1 if s1 == s0 && not stop then STM.retry else pure s1
key <- ncqGetNewFileKey ncq StateFile key <- ncqStateDump ncq
let snkFile = ncqGetFileName ncq (StateFile key)
liftIO $ withBinaryFileDurableAtomic snkFile WriteMode $ \fh -> do
IO.hPrint fh (pretty state)
done <- atomically do done <- atomically do
writeTVar ncqStateKey key writeTVar ncqStateKey key

View File

@ -141,12 +141,11 @@ ncq3Tests = do
h <- ncqPutBS sto (Just B) Nothing bs h <- ncqPutBS sto (Just B) Nothing bs
found <- ncqLocate sto h <&> isJust found <- ncqLocate sto h <&> isJust
liftIO $ assertBool (show $ "found" <+> pretty h) found liftIO $ assertBool (show $ "found" <+> pretty h) found
debug $ "written" <+> pretty h <+> pretty (BS.length bs)
atomically do atomically do
writeTQueue hq h writeTQueue hq h
modifyTVar w1 succ modifyTVar w1 succ
ncqStorageStop sto
ncqWithStorage testEnvDir $ \sto -> do ncqWithStorage testEnvDir $ \sto -> do
notice $ "reopen/lookup" <+> pretty num notice $ "reopen/lookup" <+> pretty num
hh <- atomically $ STM.flushTQueue hq hh <- atomically $ STM.flushTQueue hq
@ -494,13 +493,13 @@ ncq3Tests = do
>>= orThrowUser ("missed" <+> pretty h) >>= orThrowUser ("missed" <+> pretty h)
unless (ncqEntrySize loc == ncqTombEntrySize) do unless (ncqEntrySize loc == ncqTombEntrySize) do
notice $ pretty h <+> pretty (ncqEntrySize loc) <+> pretty ncqTombEntrySize err $ red (pretty h) <+> pretty (ncqEntrySize loc) <+> pretty ncqTombEntrySize
liftIO $ assertBool (show $ "tomb/1" <+> pretty h) (ncqIsTomb loc) -- liftIO $ assertBool (show $ "tomb/1" <+> pretty h) (ncqIsTomb loc)
-- ncqIndexCompactFull sto -- ncqIndexCompactFull sto
-- ncqStorageStop -- ncqStorageStop
pause @'Seconds 11 -- pause @'Seconds 11
ncqWithStorage dir $ \sto -> do ncqWithStorage dir $ \sto -> do
-- notice "check deleted" -- notice "check deleted"
@ -509,9 +508,10 @@ ncq3Tests = do
for_ hashes $ \h -> do for_ hashes $ \h -> do
ncqLocate sto h >>= \case ncqLocate sto h >>= \case
Nothing -> notice $ "not-found" <+> pretty h Nothing -> err $ red "not-found" <+> pretty h
Just loc -> do Just loc -> do
liftIO $ assertBool (show $ "tomb/1" <+> pretty h) (ncqIsTomb loc) unless (ncqIsTomb loc) do
err $ red ("tomb/1" <+> pretty h)
entry $ bindMatch "test:ncq3:del2" $ nil_ $ \syn -> do entry $ bindMatch "test:ncq3:del2" $ nil_ $ \syn -> do