Penciller Memory Refactor

Plugged the ne wpencille rmemory into the Penciller, and took advantage
of the increased speed to simplify the callbacks involved.

The outcome is much simpler code
This commit is contained in:
martinsumner 2016-10-30 18:25:30 +00:00
parent c7a56068c5
commit 95609702bd
6 changed files with 214 additions and 278 deletions

View file

@ -59,11 +59,9 @@
handle_cast/2,
handle_info/2,
terminate/2,
clerk_new/2,
mergeclerk_prompt/1,
mergeclerk_manifestchange/3,
rollclerk_levelzero/5,
rollclerk_close/1,
clerk_new/1,
clerk_prompt/1,
clerk_manifestchange/3,
code_change/3]).
-include_lib("eunit/include/eunit.hrl").
@ -73,32 +71,25 @@
-record(state, {owner :: pid(),
change_pending=false :: boolean(),
work_item :: #penciller_work{}|null,
merge_clerk = false :: boolean(),
roll_clerk = false ::boolean()}).
work_item :: #penciller_work{}|null}).
%%%============================================================================
%%% API
%%%============================================================================
clerk_new(Owner, Type) ->
clerk_new(Owner) ->
{ok, Pid} = gen_server:start(?MODULE, [], []),
ok = gen_server:call(Pid, {register, Owner, Type}, infinity),
ok = gen_server:call(Pid, {register, Owner}, infinity),
io:format("Penciller's clerk ~w started with owner ~w~n", [Pid, Owner]),
{ok, Pid}.
mergeclerk_manifestchange(Pid, Action, Closing) ->
clerk_manifestchange(Pid, Action, Closing) ->
gen_server:call(Pid, {manifest_change, Action, Closing}, infinity).
mergeclerk_prompt(Pid) ->
clerk_prompt(Pid) ->
gen_server:cast(Pid, prompt).
rollclerk_levelzero(Pid, LevelZero, LevelMinus1, LedgerSQN, PCL) ->
gen_server:cast(Pid,
{roll_levelzero, LevelZero, LevelMinus1, LedgerSQN, PCL}).
rollclerk_close(Pid) ->
gen_server:call(Pid, close, infinity).
%%%============================================================================
%%% gen_server callbacks
@ -107,18 +98,11 @@ rollclerk_close(Pid) ->
init([]) ->
{ok, #state{}}.
handle_call({register, Owner, Type}, _From, State) ->
case Type of
merge ->
{reply,
ok,
State#state{owner=Owner, merge_clerk = true},
?MIN_TIMEOUT};
roll ->
{reply,
ok,
State#state{owner=Owner, roll_clerk = true}}
end;
handle_call({register, Owner}, _From, State) ->
{reply,
ok,
State#state{owner=Owner},
?MIN_TIMEOUT};
handle_call({manifest_change, return, true}, _From, State) ->
io:format("Request for manifest change from clerk on closing~n"),
case State#state.change_pending of
@ -150,31 +134,20 @@ handle_call(close, _From, State) ->
{stop, normal, ok, State}.
handle_cast(prompt, State) ->
{noreply, State, ?MIN_TIMEOUT};
handle_cast({roll_levelzero, LevelZero, LevelMinus1, LedgerSQN, PCL}, State) ->
SW = os:timestamp(),
{NewL0, Size, MaxSQN} = leveled_penciller:roll_new_tree(LevelZero,
LevelMinus1,
LedgerSQN),
ok = leveled_penciller:pcl_updatelevelzero(PCL, NewL0, Size, MaxSQN),
io:format("Rolled tree to size ~w in ~w microseconds~n",
[Size, timer:now_diff(os:timestamp(), SW)]),
{noreply, State}.
{noreply, State, ?MIN_TIMEOUT}.
handle_info(timeout, State=#state{change_pending=Pnd}) when Pnd == false ->
if
State#state.merge_clerk ->
case requestandhandle_work(State) of
{false, Timeout} ->
{noreply, State, Timeout};
{true, WI} ->
% No timeout now as will wait for call to return manifest
% change
{noreply,
State#state{change_pending=true, work_item=WI}}
end
case requestandhandle_work(State) of
{false, Timeout} ->
{noreply, State, Timeout};
{true, WI} ->
% No timeout now as will wait for call to return manifest
% change
{noreply,
State#state{change_pending=true, work_item=WI}}
end.
terminate(Reason, _State) ->
io:format("Penciller's Clerk ~w shutdown now complete for reason ~w~n",
[self(), Reason]).