From 50d95ef6aa47019f712485c106e485db15b6d18d Mon Sep 17 00:00:00 2001 From: martinsumner Date: Mon, 17 Apr 2017 15:03:03 +0100 Subject: [PATCH] Move snapshot inside of the fold function riak_kv_sweeper gets the async fold function, then determines if the function can be called. If the system is busy the fold may be queued, and may never be acted upon. This may cause issues with snapshot timeouts etc. --- src/leveled_bookie.erl | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/src/leveled_bookie.erl b/src/leveled_bookie.erl index e70abbd..f3095c1 100644 --- a/src/leveled_bookie.erl +++ b/src/leveled_bookie.erl @@ -849,27 +849,37 @@ foldobjects_byindex(State, Tag, Bucket, foldobjects(State, Tag, StartKey, EndKey, FoldObjectsFun, DeferredFetch) -> - {ok, LedgerSnapshot, JournalSnapshot} = snapshot_store(State, store), {FoldFun, InitAcc} = case is_tuple(FoldObjectsFun) of true -> FoldObjectsFun; false -> {FoldObjectsFun, []} end, - Folder = fun() -> - AccFun = accumulate_objects(FoldFun, - JournalSnapshot, - Tag, - DeferredFetch), - Acc = leveled_penciller:pcl_fetchkeys(LedgerSnapshot, - StartKey, - EndKey, - AccFun, - InitAcc), - ok = leveled_penciller:pcl_close(LedgerSnapshot), - ok = leveled_inker:ink_close(JournalSnapshot), - Acc - end, + % For fold_objects the snapshot has been moved inside of the Folder + % function. + % + % fold_objects and fold_heads are called by the riak_kv_sweeper in Riak, + % and the sweeper prompts the fold before checking to see if the fold is + % ready to be run. This may lead to the fold being called on an old + % snapshot. + Folder = + fun() -> + {ok, + LedgerSnapshot, + JournalSnapshot} = snapshot_store(State, store), + AccFun = accumulate_objects(FoldFun, + JournalSnapshot, + Tag, + DeferredFetch), + Acc = leveled_penciller:pcl_fetchkeys(LedgerSnapshot, + StartKey, + EndKey, + AccFun, + InitAcc), + ok = leveled_penciller:pcl_close(LedgerSnapshot), + ok = leveled_inker:ink_close(JournalSnapshot), + Acc + end, {async, Folder}.