This commit is contained in:
Dmitry Zuykov 2025-05-13 11:41:41 +03:00
parent 9722fa7c01
commit 77a0052ffb
2 changed files with 98 additions and 72 deletions

View File

@ -53,7 +53,8 @@ import System.Posix.IO as PosixBase
import System.Posix.Types as Posix import System.Posix.Types as Posix
import System.Posix.IO.ByteString as Posix import System.Posix.IO.ByteString as Posix
import System.Posix.Unistd import System.Posix.Unistd
import System.Posix.Files (getFileStatus, modificationTimeHiRes) import System.Posix.Files (getFileStatus, modificationTimeHiRes, getFdStatus, FileStatus(..))
import System.Posix.Files qualified as PFS
import System.IO.Error (catchIOError) import System.IO.Error (catchIOError)
import System.IO.MMap as MMap import System.IO.MMap as MMap
import System.IO.Temp (emptyTempFile) import System.IO.Temp (emptyTempFile)
@ -145,7 +146,7 @@ instance Pretty Location where
pretty = \case pretty = \case
InWriteQueue{} -> "write-queue" InWriteQueue{} -> "write-queue"
InCurrent (o,l) -> pretty $ mkForm @C "current" [mkInt o, mkInt l] InCurrent (o,l) -> pretty $ mkForm @C "current" [mkInt o, mkInt l]
InFossil _ (o,l) -> pretty $ mkForm @C "fossil " [mkList [mkInt o, mkInt l]] InFossil _ (o,l) -> pretty $ mkForm @C "fossil " [mkInt o, mkInt l]
type IsHCQKey h = ( Eq (Key h) type IsHCQKey h = ( Eq (Key h)
, Hashable (Key h) , Hashable (Key h)
@ -307,8 +308,8 @@ ncqStorageRun ncq@NCQStorage{..} = flip runContT pure do
refsWriter <- makeRefsWriter refsWriter <- makeRefsWriter
reader <- makeReader reader <- makeReader
indexer <- makeIndexer indexQ
writer <- makeWriter indexQ writer <- makeWriter indexQ
indexer <- makeIndexer writer indexQ
mapM_ waitCatch [writer,indexer,refsWriter] mapM_ waitCatch [writer,indexer,refsWriter]
-- mapM_ waitCatch [writer,indexer,refsWriter] -- ,indexer,refsWriter] -- mapM_ waitCatch [writer,indexer,refsWriter] -- ,indexer,refsWriter]
@ -361,7 +362,8 @@ ncqStorageRun ncq@NCQStorage{..} = flip runContT pure do
flush <- isEmptyTQueue myFlushQ <&> not flush <- isEmptyTQueue myFlushQ <&> not
stop <- readTVar ncqStopped stop <- readTVar ncqStopped
bytes <- readTVar ncqNotWritten bytes <- readTVar ncqNotWritten
if bytes > dumpData || flush || stop then none else STM.retry now <- readTVar ncqIndexNow <&> (>0)
if bytes > dumpData || flush || now || stop then none else STM.retry
void $ atomically (STM.flushTQueue myFlushQ) void $ atomically (STM.flushTQueue myFlushQ)
@ -410,10 +412,8 @@ ncqStorageRun ncq@NCQStorage{..} = flip runContT pure do
pure refsWriter pure refsWriter
makeIndexer indexQ = do makeIndexer w indexQ = do
indexer <- ContT $ withAsync $ untilStopped do indexer <- ContT $ withAsync $ fix \next -> do
debug $ "STARTED INDEXER"
what' <- race (pause @'Seconds 1) $ atomically do what' <- race (pause @'Seconds 1) $ atomically do
stop <- readTVar ncqStopped stop <- readTVar ncqStopped
@ -431,17 +431,19 @@ ncqStorageRun ncq@NCQStorage{..} = flip runContT pure do
(key, _) <- ncqIndexFile ncq fn <&> over _2 HS.fromList (key, _) <- ncqIndexFile ncq fn <&> over _2 HS.fromList
-- atomically do
-- r <- readTVar ncqWaitIndex <&> HPSQ.toList
-- let new = [(k,p,v) | (k,p,v) <- r, not (k `HS.member` added)]
-- writeTVar ncqWaitIndex (HPSQ.fromList new)
ncqAddTrackedFilesIO ncq [key] ncqAddTrackedFilesIO ncq [key]
atomically do atomically do
modifyTVar ncqCurrentUsage (IntMap.adjust pred (fromIntegral fd)) modifyTVar ncqCurrentUsage (IntMap.adjust pred (fromIntegral fd))
ncqLoadSomeIndexes ncq [fromString key] ncqLoadSomeIndexes ncq [fromString key]
down <- atomically do
writerDown <- pollSTM w <&> isJust
stopped <- readTVar ncqStopped
pure (stopped && writerDown)
unless down next
link indexer link indexer
pure indexer pure indexer
@ -509,6 +511,12 @@ ncqStorageRun ncq@NCQStorage{..} = flip runContT pure do
when (fromIntegral size >= ncqMinLog || indexNow > 0) do when (fromIntegral size >= ncqMinLog || indexNow > 0) do
fsize <- readTVarIO ncqCurrentHandleR
>>= getFdStatus
<&> PFS.fileSize
unless (fsize == 0) do
(n,u) <- atomically do (n,u) <- atomically do
r <- readTVar ncqCurrentHandleR <&> fromIntegral r <- readTVar ncqCurrentHandleR <&> fromIntegral
u <- readTVar ncqCurrentUsage <&> fromMaybe 0 . IntMap.lookup r u <- readTVar ncqCurrentUsage <&> fromMaybe 0 . IntMap.lookup r
@ -518,7 +526,7 @@ ncqStorageRun ncq@NCQStorage{..} = flip runContT pure do
fossilized <- ncqGetNewFossilName ncq fossilized <- ncqGetNewFossilName ncq
warn $ "NEED TRUNCATE" <+> pretty current <+> viaShow size <+> pretty n <+> pretty u debug $ "NEED TRUNCATE" <+> pretty current <+> viaShow size <+> pretty n <+> pretty u
mv current fossilized mv current fossilized

View File

@ -182,6 +182,24 @@ main = do
e -> throwIO $ BadFormException @C (mkList e) e -> throwIO $ BadFormException @C (mkList e)
entry $ bindMatch "ncq:fossilize" $ nil_ \case
[ isOpaqueOf @TCQ -> Just tcq ] -> lift do
ncq <- getNCQ tcq
ncqIndexRightNow ncq
e -> throwIO $ BadFormException @C (mkList e)
entry $ bindMatch "ncq:locate" $ \case
[ isOpaqueOf @TCQ -> Just tcq, HashLike hash ] -> lift do
ncq <- getNCQ tcq
ncqLocate ncq hash >>= \case
Just x -> do
parseSyntax (show $ pretty x) & either (error.show) pure
_ -> pure nil
e -> throwIO $ BadFormException @C (mkList e)
entry $ bindMatch "ncq:get" $ \case entry $ bindMatch "ncq:get" $ \case
[ isOpaqueOf @TCQ -> Just tcq, HashLike hash ] -> lift do [ isOpaqueOf @TCQ -> Just tcq, HashLike hash ] -> lift do
ncq <- getNCQ tcq ncq <- getNCQ tcq