Added transaction retry logic.
This commit is contained in:
parent
48c5fc3f6c
commit
4494e9736d
|
@ -12,7 +12,7 @@
|
||||||
txn_commit/0, txn_commit/1, txn_abort/0,
|
txn_commit/0, txn_commit/1, txn_abort/0,
|
||||||
get_cache_size/0, set_cache_size/3,
|
get_cache_size/0, set_cache_size/3,
|
||||||
get_txn_timeout/0, set_txn_timeout/1,
|
get_txn_timeout/0, set_txn_timeout/1,
|
||||||
transaction/1,
|
transaction/1, transaction/2,
|
||||||
put/3, put/4,
|
put/3, put/4,
|
||||||
put_r/3, put_r/4,
|
put_r/3, put_r/4,
|
||||||
put_commit/3, put_commit/4,
|
put_commit/3, put_commit/4,
|
||||||
|
@ -24,6 +24,8 @@
|
||||||
|
|
||||||
-include("bdberl.hrl").
|
-include("bdberl.hrl").
|
||||||
|
|
||||||
|
-define(is_lock_error(Error), (Error =:= deadlock orelse Error =:= lock_not_granted)).
|
||||||
|
|
||||||
|
|
||||||
open(Name, Type) ->
|
open(Name, Type) ->
|
||||||
open(Name, Type, [create]).
|
open(Name, Type, [create]).
|
||||||
|
@ -99,15 +101,31 @@ txn_abort() ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
transaction(Fun) ->
|
transaction(Fun) ->
|
||||||
|
transaction(Fun, infinity).
|
||||||
|
|
||||||
|
transaction(_Fun, 0) ->
|
||||||
|
txn_abort(),
|
||||||
|
{error, {transaction_failed, retry_limit_reached}};
|
||||||
|
transaction(Fun, Retries) ->
|
||||||
txn_begin(),
|
txn_begin(),
|
||||||
try Fun() of
|
try Fun() of
|
||||||
abort ->
|
abort ->
|
||||||
txn_abort(),
|
txn_abort(),
|
||||||
{error, transaction_aborted};
|
{error, transaction_aborted};
|
||||||
|
|
||||||
Value ->
|
Value ->
|
||||||
txn_commit(),
|
txn_commit(),
|
||||||
{ok, Value}
|
{ok, Value}
|
||||||
catch
|
catch
|
||||||
|
throw : {error, {_Op, Error}} when ?is_lock_error(Error) ->
|
||||||
|
txn_abort(),
|
||||||
|
erlang:yield(),
|
||||||
|
R = case Retries of
|
||||||
|
infinity -> infinity;
|
||||||
|
Retries -> Retries - 1
|
||||||
|
end,
|
||||||
|
transaction(Fun, R);
|
||||||
|
|
||||||
_ : Reason ->
|
_ : Reason ->
|
||||||
txn_abort(),
|
txn_abort(),
|
||||||
{error, {transaction_failed, Reason}}
|
{error, {transaction_failed, Reason}}
|
||||||
|
@ -143,7 +161,6 @@ put_commit_r(Db, Key, Value, Opts) ->
|
||||||
Error -> throw(Error)
|
Error -> throw(Error)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
|
||||||
get(Db, Key) ->
|
get(Db, Key) ->
|
||||||
get(Db, Key, []).
|
get(Db, Key, []).
|
||||||
|
|
||||||
|
@ -178,7 +195,7 @@ update(Db, Key, Fun) ->
|
||||||
|
|
||||||
update(Db, Key, Fun, Args) ->
|
update(Db, Key, Fun, Args) ->
|
||||||
F = fun() ->
|
F = fun() ->
|
||||||
Value = case get(Db, Key, [rmw]) of
|
Value = case get_r(Db, Key, [rmw]) of
|
||||||
not_found -> not_found;
|
not_found -> not_found;
|
||||||
{ok, Val} -> Val
|
{ok, Val} -> Val
|
||||||
end,
|
end,
|
||||||
|
@ -186,7 +203,7 @@ update(Db, Key, Fun, Args) ->
|
||||||
undefined -> Fun(Key, Value);
|
undefined -> Fun(Key, Value);
|
||||||
Args -> Fun(Key, Value, Args)
|
Args -> Fun(Key, Value, Args)
|
||||||
end,
|
end,
|
||||||
ok = put_commit(Db, Key, NewValue),
|
put_commit_r(Db, Key, NewValue),
|
||||||
NewValue
|
NewValue
|
||||||
end,
|
end,
|
||||||
transaction(F).
|
transaction(F).
|
||||||
|
|
|
@ -29,7 +29,7 @@ wait_for_finish(0) ->
|
||||||
wait_for_finish(Count) ->
|
wait_for_finish(Count) ->
|
||||||
receive
|
receive
|
||||||
{finished, Pid} ->
|
{finished, Pid} ->
|
||||||
io:format("~p is done; ~p remaining.\n", [Pid, Count-1]),
|
ct:print("~p is done; ~p remaining.\n", [Pid, Count-1]),
|
||||||
wait_for_finish(Count-1)
|
wait_for_finish(Count-1)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
@ -47,34 +47,16 @@ thrash_run(Owner) ->
|
||||||
thrash_incr_loop(Owner, 0) ->
|
thrash_incr_loop(Owner, 0) ->
|
||||||
Owner ! {finished, self()};
|
Owner ! {finished, self()};
|
||||||
thrash_incr_loop(Owner, Count) ->
|
thrash_incr_loop(Owner, Count) ->
|
||||||
ct:print("~p\n", [Count]),
|
ct:print("~p", [Count]),
|
||||||
%% Choose random key
|
%% Choose random key
|
||||||
Key = random:uniform(1200),
|
Key = random:uniform(1200),
|
||||||
|
|
||||||
%% Start a txn that will read the current value of the key and increment by 1
|
%% Start a txn that will read the current value of the key and increment by 1
|
||||||
F = fun() ->
|
F = fun(_Key, Value) ->
|
||||||
case bdberl:get(0, Key, [rmw]) of
|
case Value of
|
||||||
not_found ->
|
not_found -> 0;
|
||||||
Value = 0;
|
Value -> Value + 1
|
||||||
|
end
|
||||||
{ok, Value} ->
|
|
||||||
Value
|
|
||||||
end,
|
end,
|
||||||
ok = bdberl:put(0, Key, Value)
|
{ok, _} = bdberl:update(0, Key, F),
|
||||||
end,
|
|
||||||
ok = do_txn(F, 0),
|
|
||||||
thrash_incr_loop(Owner, Count-1).
|
thrash_incr_loop(Owner, Count-1).
|
||||||
|
|
||||||
do_txn(F, Count) ->
|
|
||||||
case bdberl:txn_begin() of
|
|
||||||
ok ->
|
|
||||||
case catch(F()) of
|
|
||||||
{'EXIT', _Reason} ->
|
|
||||||
io:format("Txn attempt ~p failed; retrying", [Count]),
|
|
||||||
do_txn(F, Count+1);
|
|
||||||
_Other ->
|
|
||||||
ok = bdberl:txn_commit()
|
|
||||||
end;
|
|
||||||
{error, _Reason} ->
|
|
||||||
do_txn(F, Count+1)
|
|
||||||
end.
|
|
||||||
|
|
Loading…
Reference in a new issue