Handle errors properly for ordered updates

This commit is contained in:
Victor Denisov 2016-10-23 21:52:10 -07:00
parent f81d5ec42e
commit 316ae02ecc

View file

@ -645,16 +645,19 @@ update' ordered col updateDocs = do
updates updates
let lens = map length chunks let lens = map length chunks
let lSums = 0 : (zipWith (+) lSums lens) let lSums = 0 : (zipWith (+) lSums lens)
exceptionThrown <- liftIO $ newIORef False errorDetected <- liftIO $ newIORef False
blocks <- forM (zip lSums chunks) $ \b -> do
ctx <- ask ctx <- ask
liftIO $ do blocks <- forM (zip lSums chunks) $ \b -> liftIO $ do
et <- readIORef exceptionThrown ed <- readIORef errorDetected
if et && ordered if ed && ordered
then return $ UpdateResult True 0 Nothing [] [] [] -- TODO probably should be revised then return $ UpdateResult True 0 Nothing [] [] []
else runReaderT (updateBlock ordered col b) ctx else do
ur <- runReaderT (updateBlock ordered col b) ctx
when (failed ur) $ do
writeIORef errorDetected True
return ur
`catch` \(e :: SomeException) -> do `catch` \(e :: SomeException) -> do
writeIORef exceptionThrown True writeIORef errorDetected True
return $ UpdateResult True 0 Nothing [] [] [] -- TODO probably should be revised return $ UpdateResult True 0 Nothing [] [] [] -- TODO probably should be revised
let failedTotal = or $ map failed blocks let failedTotal = or $ map failed blocks
let updatedTotal = sum $ map nMatched blocks let updatedTotal = sum $ map nMatched blocks
@ -700,8 +703,14 @@ updateBlockLegacy :: (MonadIO m)
updateBlockLegacy ordered col (prevCount, docs) = do updateBlockLegacy ordered col (prevCount, docs) = do
db <- thisDatabase db <- thisDatabase
ctx <- ask ctx <- ask
errorDetected <- liftIO $ newIORef False
results <- results <-
liftIO $ forM (zip [prevCount, (prevCount + 1) ..] docs) $ \(i, updateDoc) -> do liftIO $ forM (zip [prevCount, (prevCount + 1) ..] docs) $ \(i, updateDoc) -> do
ed <- readIORef errorDetected
if ed && ordered
then do
return $ UpdateResult True 0 Nothing [] [] []
else do
let doc = (at "u" updateDoc) :: Document let doc = (at "u" updateDoc) :: Document
let sel = (at "q" updateDoc) :: Document let sel = (at "q" updateDoc) :: Document
let upsrt = if at "upsert" updateDoc then [Upsert] else [] let upsrt = if at "upsert" updateDoc then [Upsert] else []
@ -726,11 +735,13 @@ updateBlockLegacy ordered col (prevCount, docs) = do
let c = fromMaybe defaultCode eCode let c = fromMaybe defaultCode eCode
if wtimeout if wtimeout
then do then do
writeIORef errorDetected True
return $ UpdateResult True 0 Nothing [] [] [WriteConcernError c errV] return $ UpdateResult True 0 Nothing [] [] [WriteConcernError c errV]
else do else do
writeIORef errorDetected True
return $ UpdateResult True 0 Nothing [] [WriteError i c errV] [] return $ UpdateResult True 0 Nothing [] [WriteError i c errV] []
`catch` \(e :: SomeException) -> do `catch` \(e :: SomeException) -> do
when ordered $ liftIO $ throwIO e writeIORef errorDetected True
return $ UpdateResult True 0 Nothing [] [WriteError i 0 (show e)] [] return $ UpdateResult True 0 Nothing [] [WriteError i 0 (show e)] []
return $ foldl1' mergeUpdateResults results return $ foldl1' mergeUpdateResults results
@ -859,7 +870,7 @@ deleteBlock ordered col docs = do
liftIO $ forM docs $ \deleteDoc -> do liftIO $ forM docs $ \deleteDoc -> do
let sel = (at "q" deleteDoc) :: Document let sel = (at "q" deleteDoc) :: Document
let opts = if at "limit" deleteDoc == (1 :: Int) then [SingleRemove] else [] let opts = if at "limit" deleteDoc == (1 :: Int) then [SingleRemove] else []
runReaderT (write (Delete (db <.> col) opts sel)) ctx _ <- runReaderT (write (Delete (db <.> col) opts sel)) ctx
return Nothing return Nothing
`catch` \(e :: SomeException) -> do `catch` \(e :: SomeException) -> do
when ordered $ liftIO $ throwIO e when ordered $ liftIO $ throwIO e