Skip to content

Commit

Permalink
Generalized GC (corrected base) (#4106)
Browse files Browse the repository at this point in the history
* Generalized GC (corrected base)

* Add GC doc to mkdocs.yml
  • Loading branch information
uwiger committed Mar 24, 2023
1 parent 814be6f commit 55ab1ec
Show file tree
Hide file tree
Showing 27 changed files with 953 additions and 663 deletions.
7 changes: 6 additions & 1 deletion apps/aechannel/src/aesc_state_tree.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
mtree_iterator/1,
new_with_backend/1,
new_with_dirty_backend/1,
root_hash/1]).
root_hash/1,
db/1]).

-export([ from_binary_without_backend/1
, to_binary_without_backend/1
Expand Down Expand Up @@ -91,6 +92,10 @@ lookup(PubKey, Tree) ->
root_hash(Tree) ->
aeu_mtrees:root_hash(Tree).

-spec db(tree()) -> {'ok', aeu_mp_trees:db()}.
db(Tree) ->
aeu_mtrees:db(Tree).

-spec to_binary_without_backend(tree()) -> binary().
to_binary_without_backend(Tree) ->
Bin = aeu_mtrees:serialize(Tree),
Expand Down
17 changes: 15 additions & 2 deletions apps/aecontract/src/aect_call_state_tree.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
, iterator/1
, prune/2
, prune_without_backend/1
, root_hash/1]).
, root_hash/1
, db/1 ]).

-export([ from_binary_without_backend/1
, to_binary_without_backend/1
Expand Down Expand Up @@ -88,12 +89,20 @@ new_with_dirty_backend(Hash) ->
%% Calls and return values are only kept for one block.
-spec prune(aec_blocks:height(), aec_trees:trees()) -> aec_trees:trees().
prune(_,Trees) ->
aec_trees:set_calls(Trees, empty_with_backend()).
CTree = aec_trees:calls(Trees),
Empty = case has_backend(CTree) of
true -> empty_with_backend();
false -> empty()
end,
aec_trees:set_calls(Trees, Empty).

-spec prune_without_backend(aec_trees:trees()) -> aec_trees:trees().
prune_without_backend(Trees) ->
aec_trees:set_calls(Trees, empty()).

has_backend(#call_tree{calls = CtTree}) ->
aeu_mtrees:has_backend(CtTree).

-spec insert_call(aect_call:call(), tree()) -> tree().
insert_call(Call, Tree) ->
CtCallId = aect_call:ct_call_id(Call),
Expand Down Expand Up @@ -138,6 +147,10 @@ call_id(<<_:?PUB_SIZE/unit:8, CallId/binary>> = _CallTreeId) ->
root_hash(#call_tree{calls = CtTree}) ->
aeu_mtrees:root_hash(CtTree).

-spec db(tree()) -> {ok, aeu_mp_trees:db()}.
db(#call_tree{calls = CtTree}) ->
aeu_mtrees:db(CtTree).

%% -- Commit to db --

-spec commit_to_db(tree()) -> tree().
Expand Down
7 changes: 6 additions & 1 deletion apps/aecontract/src/aect_state_tree.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
, new_with_backend/1
, new_with_dirty_backend/1
, gc_cache/1
, root_hash/1]).
, root_hash/1
, db/1 ]).

%% API - Proof of inclusion
-export([ add_poi/3
Expand Down Expand Up @@ -265,6 +266,10 @@ lookup_contract_with_code(Pubkey, Tree, Options) ->
root_hash(#contract_tree{contracts = CtTree}) ->
aeu_mtrees:root_hash(CtTree).

-spec db(tree()) -> {ok, aeu_mp_trees:db()}.
db(#contract_tree{contracts = CtTree}) ->
aeu_mtrees:db(CtTree).

-spec add_poi(aect_contracts:pubkey(), aect_state_tree:tree(), aec_poi:poi()) ->
{'ok', aec_poi:poi()}
| {'error', 'not_present' | 'wrong_root_hash'}.
Expand Down
2 changes: 1 addition & 1 deletion apps/aecontract/test/aect_test_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ new_state() ->
#{}.

trees(#{} = S) ->
maps:get(trees, S, aec_trees:new()).
maps:get(trees, S, aec_trees:new_without_backend()).

set_trees(Trees, S) ->
S#{trees => Trees}.
Expand Down
6 changes: 2 additions & 4 deletions apps/aecore/src/aec_chain_state.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1484,7 +1484,5 @@ key_headers_height_store_migration_step(Time, N, {TimeRead, {Headers, Cont}}) ->
).

is_gc_disabled() ->
case aec_db_gc:config() of
#{enabled := Bool} when is_boolean(Bool) ->
Bool
end.
[Bool] = aec_db_gc:info([enabled]),
Bool.
13 changes: 9 additions & 4 deletions apps/aecore/src/aec_conductor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1409,7 +1409,7 @@ handle_successfully_added_block(Block, Hash, true, PrevKeyHeader, Events, State,
ok; %% Don't spend time when we are the leader.
false ->
aec_tx_pool:garbage_collect(),
[ maybe_garbage_collect_accounts() || BlockType == key ]
[ maybe_garbage_collect(NewTopBlock) || BlockType == key ]
end,
{ok, setup_loop(State2, true, IsLeader, Origin)}
end.
Expand Down Expand Up @@ -1506,13 +1506,18 @@ get_pending_key_block(TopHash, State) ->
%%
%% To avoid starting of the GC process just for EUNIT
-ifdef(EUNIT).
maybe_garbage_collect_accounts() -> nop.
maybe_garbage_collect(_) -> nop.
-else.

%% This should be called when there are no processes modifying the block state
%% (e.g. aec_conductor on specific places)
maybe_garbage_collect_accounts() ->
gen_statem:call(aec_db_gc, maybe_garbage_collect).
maybe_garbage_collect(Block) ->
T0 = erlang:system_time(microsecond),
Header = aec_blocks:to_header(Block),
Res = aec_db_gc:maybe_garbage_collect(Header),
T1 = erlang:system_time(microsecond),
lager:debug("Result -> ~p (time: ~p us)", [Res, T1-T0]),
Res.
-endif.

consensus_module(#state{ consensus = #consensus{consensus_module =
Expand Down

0 comments on commit 55ab1ec

Please sign in to comment.