Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Allow var lookups to bypass cache with pdict value #1508

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions src/blockchain_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -878,14 +878,20 @@ get_vars(VarList, Ledger) ->
VarsNonce :: non_neg_integer(),
Ledger :: blockchain_ledger_v1:ledger()) -> {ok, any()} | {error, any()}.
get_var_(VarName, HasAux, VarsNonce, Ledger) ->
Cache = persistent_term:get(?var_cache),
cream:cache(
Cache,
{HasAux, VarsNonce, VarName},
fun() ->
get_var_(VarName, Ledger)
end
).
case get('__speculative_absorb') of
true ->
%% do not update the cache
get_var_(VarName, Ledger);
_ ->
Cache = persistent_term:get(?var_cache),
cream:cache(
Cache,
{HasAux, VarsNonce, VarName},
fun() ->
get_var_(VarName, Ledger)
end
)
end.

-spec get_var(VarName :: atom(), Ledger :: blockchain_ledger_v1:ledger()) -> {ok, any()} | {error, any()}.
get_var(VarName, Ledger) ->
Expand Down
43 changes: 25 additions & 18 deletions src/region/blockchain_region_v1.erl
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,31 @@ h3_to_region(H3, Ledger, RegionBins) ->
Res = polyfill_resolution(Ledger),
HasAux = blockchain_ledger_v1:has_aux(Ledger),
Parent = h3:parent(H3, Res),
Cache = persistent_term:get(?region_cache),
cream:cache(
Cache,
{HasAux, VarsNonce, Parent},
fun() ->
MaybeBins =
case RegionBins of
no_prefetch -> get_all_region_bins(Ledger);
{error, _} = Err -> Err;
B -> {ok, B}
end,
case MaybeBins of
{ok, Bins} ->
h3_to_region_(Parent, Bins);
{error, _} = Error -> Error
end
end
).
LookupFun = fun() ->
MaybeBins =
case RegionBins of
no_prefetch -> get_all_region_bins(Ledger);
{error, _} = Err -> Err;
B -> {ok, B}
end,
case MaybeBins of
{ok, Bins} ->
h3_to_region_(Parent, Bins);
{error, _} = Error -> Error
end
end,
%% don't contaminate the cache when doing speculative absorbs
case get('__speculative_absorb') of
true ->
LookupFun();
_ ->
Cache = persistent_term:get(?region_cache),
cream:cache(
Cache,
{HasAux, VarsNonce, Parent},
LookupFun
)
end.

-spec h3_in_region(
H3 :: h3:h3_index(),
Expand Down
2 changes: 2 additions & 0 deletions src/transactions/blockchain_txn_mgr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ init(Args) ->
Tabs
end,
ok = blockchain_event:add_handler(self()),
%% indicate this process does speculative absorbs to determine txn validity
put('__speculative_absorb', true),
{ok, #state{txn_cache = TxnCache, rejections_deferred = []}}.

handle_cast({set_chain, Chain}, State=#state{chain = undefined}) ->
Expand Down
26 changes: 16 additions & 10 deletions src/transactions/v1/blockchain_txn_vars_v1.erl
Original file line number Diff line number Diff line change
Expand Up @@ -616,18 +616,24 @@ delayed_absorb(Txn, Ledger) ->
ok = blockchain_ledger_v1:master_key(Key, Ledger)
end
end,
blockchain_utils:teardown_var_cache(),
case blockchain_ledger_v1:mode(Ledger) of
active ->
%% we've invalidated the region cache, so prewarm it.
spawn(fun() ->
timer:sleep(30000),
blockchain_region_v1:prewarm_cache(blockchain_ledger_v1:remove_context(Ledger))
end);
case get('__speculative_absorb') of
true ->
ok;
_ ->
%% arguably this is unnecessary because the var nonce is part of the cache key
blockchain_utils:teardown_var_cache(),
case blockchain_ledger_v1:mode(Ledger) of
active ->
%% we've invalidated the region cache, so prewarm it.
spawn(fun() ->
timer:sleep(30000),
blockchain_region_v1:prewarm_cache(blockchain_ledger_v1:remove_context(Ledger))
end);
_ ->
ok
end,
ok
end,
ok.
end.

sum_higher(Target, Proplist) ->
sum_higher(Target, Proplist, 0).
Expand Down