Add test/mrdb_bench.erl, remove some debug logging

This commit is contained in:
Ulf Wiger 2022-04-07 18:48:24 +02:00
parent 2339232f59
commit 0830cecbf9
3 changed files with 85 additions and 37 deletions

View File

@ -885,9 +885,9 @@ whereis_proc(Alias, Tab) ->
db_insert(Ref, Obj, Opts, St) -> db_insert(Ref, Obj, Opts, St) ->
try mrdb:insert(Ref, Obj, Opts) of try mrdb:insert(Ref, Obj, Opts) of
ok -> ok; ok -> ok
{error, _} = Error -> %% {error, _} = Error ->
write_error(insert, [Ref, Obj, Opts], Error, St) %% write_error(insert, [Ref, Obj, Opts], Error, St)
catch catch
Cat:Exception:T -> Cat:Exception:T ->
write_error(insert, [Ref, Obj, Opts], {Cat, Exception, T}, St) write_error(insert, [Ref, Obj, Opts], {Cat, Exception, T}, St)
@ -895,9 +895,9 @@ db_insert(Ref, Obj, Opts, St) ->
db_delete(Ref, K, Opts, St) -> db_delete(Ref, K, Opts, St) ->
try mrdb:delete(Ref, K, Opts) of try mrdb:delete(Ref, K, Opts) of
ok -> ok; ok -> ok
{error, _} = Error -> %% {error, _} = Error ->
write_error(delete, [Ref, K, Opts], Error, St) %% write_error(delete, [Ref, K, Opts], Error, St)
catch catch
Cat:Exception:T -> Cat:Exception:T ->
write_error(delete, [Ref, K, Opts], {Cat, Exception, T}, St) write_error(delete, [Ref, K, Opts], {Cat, Exception, T}, St)
@ -906,7 +906,8 @@ db_delete(Ref, K, Opts, St) ->
write_error(_Op, _Args, _Error, #st{on_error = OnErr}) when OnErr =/= fatal -> write_error(_Op, _Args, _Error, #st{on_error = OnErr}) when OnErr =/= fatal ->
ok; ok;
write_error(Op, Args, Error, _) -> write_error(Op, Args, Error, _) ->
mnesia:fatal("mnesia_rocksdb write_error: ~p ~p -> ~p", [Op, Args, Error]). mnesia_lib:fatal("mnesia_rocksdb write_error: ~p ~p -> ~p",
[Op, Args, Error]).
%% ---------------------------------------------------------------------------- %% ----------------------------------------------------------------------------
%% COMMON PRIVATE %% COMMON PRIVATE

View File

@ -64,7 +64,7 @@
-type req() :: {create_table, table(), properties()} -type req() :: {create_table, table(), properties()}
| {delete_table, table()} | {delete_table, table()}
| {load_table, table()} | {load_table, table(), properties()}
| {related_resources, table()} | {related_resources, table()}
| {get_ref, table()} | {get_ref, table()}
| {add_aliases, [alias()]} | {add_aliases, [alias()]}
@ -392,21 +392,16 @@ handle_cast(_Msg, St) ->
-spec handle_info(any(), st()) -> gen_server_noreply(). -spec handle_info(any(), st()) -> gen_server_noreply().
handle_info({mnesia_table_event, Event}, St) -> handle_info({mnesia_table_event, Event}, St) ->
?log(debug, "Table event: ~p", [Event]),
case Event of case Event of
{write, {schema, Tab, Props}, _} -> {write, {schema, Tab, Props}, _} ->
case find_cf(Tab, St) of case find_cf(Tab, St) of
error -> error ->
?log(debug, "No Cf found (~p)", [Tab]),
{noreply, St}; {noreply, St};
#{} = Cf -> #{} = Cf ->
?log(debug, "Located Cf: ~p", [Cf]),
case try_refresh_cf(Cf, Props, St) of case try_refresh_cf(Cf, Props, St) of
false -> false ->
?log(debug, "Nothing changed (~p)", [Tab]),
{noreply, St}; {noreply, St};
{true, NewCf, St1} -> {true, NewCf, St1} ->
?log(debug, "NewCf = ~p", [NewCf]),
maybe_update_pt(Tab, NewCf), maybe_update_pt(Tab, NewCf),
{noreply, St1} {noreply, St1}
end end
@ -457,26 +452,21 @@ intersection(A, B) ->
handle_req(Alias, {create_table, Name, Props}, Backend, St) -> handle_req(Alias, {create_table, Name, Props}, Backend, St) ->
case create_trec(Alias, Name, Props, Backend, St) of case create_trec(Alias, Name, Props, Backend, St) of
{ok, NewCf} -> {ok, NewCf} ->
?log(debug, "NewCf = ~p", [NewCf]),
St1 = update_cf(Alias, Name, NewCf, St), St1 = update_cf(Alias, Name, NewCf, St),
{reply, {ok, NewCf}, St1}; {reply, {ok, NewCf}, St1};
{error, _} = Error -> {error, _} = Error ->
{reply, Error, St} {reply, Error, St}
end; end;
handle_req(Alias, {load_table, Name, Props}, Backend, St) -> handle_req(Alias, {load_table, Name, Props}, Backend, St) ->
?log(debug, "load_table, ~p, ~p", [Name, Props]),
try try
case find_cf(Alias, Name, Backend, St) of case find_cf(Alias, Name, Backend, St) of
{ok, #{status := open}} -> {ok, #{status := open}} ->
?log(info, "load_table(~p) when table already loaded", [Name]),
{reply, ok, St}; {reply, ok, St};
{ok, #{status := created} = TRec} -> {ok, #{status := created} = TRec} ->
handle_load_table_req(Alias, Name, TRec, Backend, St); handle_load_table_req(Alias, Name, TRec, Backend, St);
_ -> _ ->
?log(debug, "load_table (~p) without preceding create_table", [Name]),
case create_trec(Alias, Name, Props, Backend, St) of case create_trec(Alias, Name, Props, Backend, St) of
{ok, NewCf} -> {ok, NewCf} ->
?log(debug, "NewCf = ~p", [NewCf]),
St1 = update_cf(Alias, Name, NewCf, St), St1 = update_cf(Alias, Name, NewCf, St),
Backend1 = maps:get(Alias, St1#st.backends), Backend1 = maps:get(Alias, St1#st.backends),
{ok, TRec} = find_cf(Alias, Name, Backend1, St1), {ok, TRec} = find_cf(Alias, Name, Backend1, St1),
@ -711,16 +701,13 @@ create_table_from_trec(Alias, Name, #{type := column_family} = TRec,
false -> false ->
create_table_as_cf(Alias, Name, TRec#{db_ref => DbRef}, St); create_table_as_cf(Alias, Name, TRec#{db_ref => DbRef}, St);
{false, MP} -> {false, MP} ->
?log(debug, "will create ~p as standalone (no migrate)", [Name]),
create_table_as_standalone(Alias, Name, true, MP, TRec, St); create_table_as_standalone(Alias, Name, true, MP, TRec, St);
{true, MP} -> {true, MP} ->
?log(debug, "will create ~p as standalone and migrate", [Name]), ?log(debug, "will create ~p as standalone and migrate", [Name]),
case create_table_as_standalone(Alias, Name, false, MP, TRec, St) of case create_table_as_standalone(Alias, Name, false, MP, TRec, St) of
{ok, OldTRec, _} -> {ok, OldTRec, _} ->
create_cf_and_migrate(Alias, Name, OldTRec, TRec, Backend, St); create_cf_and_migrate(Alias, Name, OldTRec, TRec, Backend, St);
Other -> _Other ->
?log(info, "Couldn't open what seems to be a standalone table"
" (~p): ~p", [Name, Other]),
create_table_as_cf(Alias, Name, TRec#{db_ref => DbRef}, St) create_table_as_cf(Alias, Name, TRec#{db_ref => DbRef}, St)
end end
end; end;
@ -739,13 +726,10 @@ create_cf_and_migrate(Alias, Name, OldTRec, TRec, #{db_ref := DbRef}, St) ->
should_we_migrate_standalone(#{name := Name}) -> should_we_migrate_standalone(#{name := Name}) ->
case table_exists_as_standalone(Name) of case table_exists_as_standalone(Name) of
{true, MP} -> {true, MP} ->
?log(debug, "table ~p exists as standalone: ~p", [Name, MP]),
case auto_migrate_to_cf(Name) of case auto_migrate_to_cf(Name) of
true -> true ->
?log(debug, "auto_migrate(~p): true", [Name]),
{true, MP}; {true, MP};
false -> false ->
?log(debug, "auto_migrate(~p): false", [Name]),
{false, MP} {false, MP}
end; end;
{false, _} -> {false, _} ->
@ -953,7 +937,6 @@ props_to_map({Tab,_,_}, _) ->
try_refresh_cf(#{alias := Alias, name := Name, properties := Ps} = Cf, Props, St) -> try_refresh_cf(#{alias := Alias, name := Name, properties := Ps} = Cf, Props, St) ->
PMap = props_to_map(Name, Props), PMap = props_to_map(Name, Props),
?log(debug, "PMap = ~p", [PMap]),
case PMap =:= Ps of case PMap =:= Ps of
true -> false; true -> false;
false -> false ->
@ -1011,13 +994,11 @@ do_open_standalone(CreateIfMissing, Alias, Name, Exists, MP, TRec0,
case open_db_(MP, Alias, Opts, [], CreateIfMissing) of case open_db_(MP, Alias, Opts, [], CreateIfMissing) of
{ok, #{ cf_info := CfI }} -> {ok, #{ cf_info := CfI }} ->
DbRec = maps:get({ext,Alias,"default"}, CfI), DbRec = maps:get({ext,Alias,"default"}, CfI),
?log(debug, "successfully opened db ~p", [Name]),
CfNames = maps:keys(CfI), CfNames = maps:keys(CfI),
DbRec1 = DbRec#{ cfs => CfNames, DbRec1 = DbRec#{ cfs => CfNames,
mountpoint => MP }, mountpoint => MP },
TRec = maps:merge(TRec0, DbRec#{type => standalone}), TRec = maps:merge(TRec0, DbRec#{type => standalone}),
TRec1 = guess_table_vsn_and_encoding(Exists, TRec), TRec1 = guess_table_vsn_and_encoding(Exists, TRec),
?log(debug, "TRec1 = ~p", [TRec1]),
{ok, TRec1, St#st{standalone = Ts#{{Alias, Name} => DbRec1}}}; {ok, TRec1, St#st{standalone = Ts#{{Alias, Name} => DbRec1}}};
{error, _} = Err -> {error, _} = Err ->
?log(debug, "open_db error: ~p", [Err]), ?log(debug, "open_db error: ~p", [Err]),
@ -1038,13 +1019,10 @@ guess_table_vsn_and_encoding(false, TRec) ->
TRec; TRec;
guess_table_vsn_and_encoding(true, #{properties := #{attributes := As}, guess_table_vsn_and_encoding(true, #{properties := #{attributes := As},
alias := Alias, name := Name} = R) -> alias := Alias, name := Name} = R) ->
?log(debug, "guess_vsn_and_encoding(R = ~p)", [R]),
case read_admin_info(standalone_vsn_and_enc, Alias, Name) of case read_admin_info(standalone_vsn_and_enc, Alias, Name) of
{ok, {V, E}} -> {ok, {V, E}} ->
?log(debug, "admin_info exists: ~p", [{V,E}]),
R#{vsn => V, encoding => E}; R#{vsn => V, encoding => E};
error -> error ->
?log(debug, "no admin_info; will iterate", []),
R1 = set_default_guess(R), R1 = set_default_guess(R),
mrdb:with_rdb_iterator( mrdb:with_rdb_iterator(
R1, fun(I) -> R1, fun(I) ->
@ -1068,21 +1046,16 @@ guess_table_vsn_and_encoding_({ok, K, V}, _I, As, R) ->
try _ = {mnesia_rocksdb_lib:decode(EncK, sext), try _ = {mnesia_rocksdb_lib:decode(EncK, sext),
mnesia_rocksdb_lib:decode(V, term)}, mnesia_rocksdb_lib:decode(V, term)},
%% This is a vsn 1 standalone table %% This is a vsn 1 standalone table
?log(debug, "Found info tag; this is a vsn 1", []),
R#{vsn => 1, encoding => {sext, {object, term}}} R#{vsn => 1, encoding => {sext, {object, term}}}
catch catch
error:_ -> error:_ ->
?log(debug, "caught bad guess K=~p, V=~p", [K,V]),
R R
end; end;
_ -> _ ->
?log(debug, "not info obj K=~p", [K]),
Enc = guess_obj_encoding(K, V, Arity), Enc = guess_obj_encoding(K, V, Arity),
?log(debug, "guessed Enc = ~p", [Enc]),
R#{encoding => Enc} R#{encoding => Enc}
end; end;
guess_table_vsn_and_encoding_(Other, _, _, R) -> guess_table_vsn_and_encoding_(_Other, _, _, R) ->
?log(debug, "Iter Other=~p", [Other]),
R. R.
guess_obj_encoding(K, V, Arity) -> guess_obj_encoding(K, V, Arity) ->

74
test/mrdb_bench.erl Normal file
View File

@ -0,0 +1,74 @@
-module(mrdb_bench).
-compile(export_all).
init() ->
mnesia:delete_schema([node()]),
mnesia_rocksdb:create_schema([node()]),
mnesia:start(),
[mnesia:create_table(Name, [{Type, [node()]}, {record_name, r}])
|| {Name, Type} <- tabs()],
ok.
tabs() ->
[{rc, ram_copies},
{dc, disc_copies},
{do, disc_only_copies},
{rocks, rocksdb_copies},
{rdb, rocksdb_copies}].
fill(N) ->
[{T, timer:tc(fun() -> fill_(T, N) end)} || {T,_} <- tabs()].
fill_(_, 0) ->
ok;
fill_(T, N) when N > 0 ->
write(T, {r, N, <<"1234567890">>}),
fill_(T, N-1).
write(rdb, Obj) ->
mrdb:insert(rdb, Obj);
write(T, Obj) ->
mnesia:dirty_write(T, Obj).
fold() ->
[{T, timer:tc(fun() -> fold_(T) end)} || {T,_} <- tabs()].
fold_(rdb) ->
mrdb:fold(rdb, fun(_, Acc) -> Acc end, ok);
fold_(T) ->
mnesia:activity(
async_dirty,
fun() ->
mnesia:foldl(fun(_, Acc) -> Acc end, ok, T)
end).
tx(N) ->
[{T, timer:tc(fun() -> tx_(T, N) end)} || {T,_} <- tabs()].
%% tx_(T, N) ->
%% tx_(T, N, N, 10).
tx_(_, 0) -> ok;
tx_(T, N) when N > 0 ->
one_tx(T, N),
tx_(T, N-1).
one_tx(rdb, N) ->
mrdb:activity(transaction, rocksdb_copies,
fun() ->
[{r, N, Str}] = mrdb:read(rdb, N),
Str2 = <<Str/binary, "x">>,
mrdb:insert(rdb, {r, N, Str2}),
[{r, N, Str2}] = mrdb:read(rdb, N)
end);
one_tx(T, N) ->
mnesia:activity(transaction,
fun() ->
[{r, N, Str}] = mnesia:read(T, N),
Str2 = <<Str/binary, "x">>,
mnesia:write(T, {r, N, Str2}, write),
[{r, N, Str2}] = mnesia:read(T, N)
end).