Skip to content

Commit

Permalink
Merge pull request #461 from symbiont-io/journal
Browse files Browse the repository at this point in the history
journal
  • Loading branch information
symbiont-stevan-andjelkovic authored Feb 3, 2022
2 parents 320e717 + 764dbb2 commit 4d66ebb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 75 deletions.
5 changes: 3 additions & 2 deletions src/journal/src/Journal/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ calculatePositionLimit jour = do
cleanBufferTo :: Journal -> Int -> IO ()
cleanBufferTo _ _ = return ()

backPressureStatus _ _ = do
putStrLn "backPressureStatus"
backPressureStatus :: Int64 -> Int -> IO (Either AppendError (Int64, BufferClaim))
backPressureStatus position len = do
putStrLn ("backPressureStatus, position: " ++ show position ++ ", len: " ++ show len)
return (Left BackPressure)

newPosition :: Metadata -> Either AppendError (TermOffset, BufferClaim)
Expand Down
103 changes: 30 additions & 73 deletions src/journal/test/JournalTest.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import Control.Arrow ((&&&))
import Control.Exception (IOException, catch, displayException)
import Control.Monad (unless, when)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as BS
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy as LBS
import qualified Data.ByteString.Builder as BS
import Data.Monoid (Sum(Sum))
import Data.Vector (Vector)
import qualified Data.Vector as Vector
Expand Down Expand Up @@ -43,27 +43,36 @@ startJournalFake :: FakeJournal
startJournalFake = FakeJournal Vector.empty 0

appendBSFake :: ByteString -> FakeJournal -> (FakeJournal, Either AppendError ())
appendBSFake bs fj@(FakeJournal jour ix)
| unreadBytes jour ix < limit = (FakeJournal (Vector.snoc jour bs) ix, Right ())
| otherwise = (fj, Left BackPressure)
appendBSFake bs fj@(FakeJournal bss ix)
| unreadBytes < limit = (FakeJournal (Vector.snoc bss bs) ix, Right ())
| otherwise = (fj, Left BackPressure)
where
termLen :: Int
termLen = oTermBufferLength testOptions

limit :: Int
limit = termLen `div` 2

unreadBytes :: Vector ByteString -> Int -> Int
unreadBytes bss ix = sum [ BS.length bs
| bs <- map (bss Vector.!) [ix..Vector.length bss - 1]
]
+ padding 0 0 (Vector.toList (Vector.map BS.length bss))
where
padding :: Int -> Int -> [Int] -> Int
padding acc pad [] = pad
padding acc pad (l : ls)
| acc + l + hEADER_LENGTH <= termLen = padding (acc + l + hEADER_LENGTH) pad ls
| otherwise = padding (l + hEADER_LENGTH)
unreadBytes :: Int
unreadBytes = sum [ hEADER_LENGTH + BS.length bs
| bs <- map (bss Vector.!) [ix..Vector.length bss - 1]
]
+ padBytes

padBytes :: Int
padBytes =
let
(lbss, rbss) = Vector.splitAt ix bss
(lacc, lpad) = padding 0 0 (Vector.toList (Vector.map BS.length lbss))
(racc, rpad) = padding lacc 0 (Vector.toList (Vector.map BS.length rbss))
in
rpad

padding :: Int -> Int -> [Int] -> (Int, Int)
padding acc pad [] = (acc, pad)
padding acc pad (l : ls)
| acc + l + hEADER_LENGTH <= termLen = padding (acc + l + hEADER_LENGTH) pad ls
| otherwise = padding (l + hEADER_LENGTH)
(pad + (termLen - acc)) ls

readJournalFake :: FakeJournal -> (FakeJournal, Maybe ByteString)
Expand Down Expand Up @@ -326,8 +335,8 @@ unit_bug1 = assertProgram ""
, AppendBS [(32756, 'G')]
]

unit_bug11 :: Assertion
unit_bug11 = assertProgram ""
unit_bug2 :: Assertion
unit_bug2 = assertProgram ""
[ AppendBS [(32756, 'O')]
, ReadJournal
, AppendBS [(32756, 'G')]
Expand All @@ -338,72 +347,20 @@ unit_bug11 = assertProgram ""
, AppendBS [(32756, 'J')]
]

unit_bug2 :: Assertion
unit_bug2 = assertProgram ""
unit_bug3 :: Assertion
unit_bug3 = assertProgram ""
[ AppendBS [(7,'N')]
, ReadJournal
, AppendBS [(32756,'N')]
, ReadJournal
, AppendBS [(32756,'W')]
, AppendBS [(32756,'Q')]
]
-- limit = termBufferLength / 2 - hEADER_LENGTH = 65536 / 2 - 6 = 32762

unit_bug3 :: Assertion
unit_bug3 = assertProgram ""
unit_bug4 :: Assertion
unit_bug4 = assertProgram ""
[AppendBS [(1, 'A')], AppendBS [(32755,'Q')], AppendBS [(1,'D')]]

{-
nit_bug0 :: Assertion
nit_bug0 = assertProgram "read after rotation"
[ AppendBS "AAAAAAAAAAAAAAAAA"
, AppendBS "BBBBBBBBBBBBBBBB"
, ReadJournal
, ReadJournal
, AppendBS "CCCCCCCCCCCCCCCCC"
, ReadJournal
, AppendBS "DDDDDDDDDDDDDDDD"
, ReadJournal
, AppendBS "EEEEEEEEEEEEEEEE"
, ReadJournal
, AppendBS "FFFFFFFFFFFFFFFF"
, ReadJournal
]
nit_bug1 :: Assertion
nit_bug1 = assertProgram "two rotations"
[ AppendBS "XXXXXXXXXXXXXXXXXXXX"
, AppendBS "XXXXXXXXXXXXXXXXXXXX"
, AppendBS "XXXXXXXXXXXXXXXXXXXX"
, AppendBS "XXXXXXXXXXXXXXXXXXXX"
, AppendBS "XXXXXXXXXXXXXXXXXXXX"
, AppendBS "XXXXXXXXXXXXXXXXXXXX"
, AppendBS "XXXXXXXXXXXXXXXXXXXX"
, AppendBS "XXXXXXXXXXXXXXXXXXXX"
, AppendBS "XXXXXXXXXXXXXXXXXXXX"
, AppendBS "XXXXXXXXXXXXXXXXXXXX"
]
nit_bug2 :: Assertion
nit_bug2 = assertProgram "stuck reading"
[ AppendBS "OOOOOOOOOOOOO"
, ReadJournal
, AppendBS "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" -- 116 + 6 = 122 bytes
, ReadJournal
, AppendBS "UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU" -- 116 + 6 = 122 bytes
, ReadJournal
]
nit_bug3 :: Assertion
nit_bug3 = assertProgram "two rotations reading side"
[ AppendBS "M"
, AppendBS "RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR" -- 110 + 6 = 116 bytes
, AppendBS "L"
, ReadJournal
, ReadJournal
]
-}

------------------------------------------------------------------------

assertProgram :: String -> [Command] -> Assertion
Expand Down

0 comments on commit 4d66ebb

Please sign in to comment.