fix: Change opts passing to include woody_context (#47)

* fix: Change opts passing to include woody_context

* test: Fix tests

* fix: More opts passing

* fix: Switch from proc_lib spawn to usual

* fix: Fix dmt_client_api opts parsing
This commit is contained in:
Yaroslav Rogov 2021-06-30 18:26:34 +03:00 committed by GitHub
parent a137b5ff5b
commit 53924f4de4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 76 deletions

View File

@ -53,7 +53,7 @@
-export_type([domain_object/0]).
-export_type([domain/0]).
-export_type([history/0]).
-export_type([transport_opts/0]).
-export_type([opts/0]).
-include_lib("damsel/include/dmsl_domain_config_thrift.hrl").
@ -71,15 +71,18 @@
-type versioned_object() :: dmsl_domain_config_thrift:'VersionedObject'().
-type domain() :: dmsl_domain_thrift:'Domain'().
-type history() :: dmsl_domain_config_thrift:'History'().
-type transport_opts() :: woody_client_thrift_http_transport:transport_options() | undefined.
-type opts() :: #{
transport_opts => woody_client_thrift_http_transport:transport_options(),
woody_context => woody_context:ctx()
}.
%%% API
-spec checkout(version()) -> snapshot() | no_return().
checkout(Reference) ->
checkout(Reference, undefined).
checkout(Reference, #{}).
-spec checkout(version(), transport_opts()) -> snapshot() | no_return().
-spec checkout(version(), opts()) -> snapshot() | no_return().
checkout(Reference, Opts) ->
Version = ref_to_version(Reference),
case dmt_client_cache:get(Version, Opts) of
@ -95,9 +98,9 @@ checkout_object(ObjectReference) ->
-spec checkout_object(version(), object_ref()) -> domain_object() | no_return().
checkout_object(Reference, ObjectReference) ->
checkout_object(Reference, ObjectReference, undefined).
checkout_object(Reference, ObjectReference, #{}).
-spec checkout_object(version(), object_ref(), transport_opts()) -> domain_object() | no_return().
-spec checkout_object(version(), object_ref(), opts()) -> domain_object() | no_return().
checkout_object(Reference, ObjectReference, Opts) ->
Version = ref_to_version(Reference),
unwrap(dmt_client_cache:get_object(Version, ObjectReference, Opts)).
@ -108,9 +111,9 @@ checkout_versioned_object(ObjectReference) ->
-spec checkout_versioned_object(version(), object_ref()) -> versioned_object() | no_return().
checkout_versioned_object(Reference, ObjectReference) ->
checkout_versioned_object(Reference, ObjectReference, undefined).
checkout_versioned_object(Reference, ObjectReference, #{}).
-spec checkout_versioned_object(version(), object_ref(), transport_opts()) -> versioned_object() | no_return().
-spec checkout_versioned_object(version(), object_ref(), opts()) -> versioned_object() | no_return().
checkout_versioned_object(Reference, ObjectReference, Opts) ->
Version = ref_to_version(Reference),
#'VersionedObject'{version = Version, object = checkout_object(Reference, ObjectReference, Opts)}.
@ -121,9 +124,9 @@ checkout_objects_by_type(ObjectType) ->
-spec checkout_objects_by_type(version(), object_type()) -> [domain_object()] | no_return().
checkout_objects_by_type(Reference, ObjectType) ->
checkout_objects_by_type(Reference, ObjectType, undefined).
checkout_objects_by_type(Reference, ObjectType, #{}).
-spec checkout_objects_by_type(version(), object_type(), transport_opts()) -> [domain_object()] | no_return().
-spec checkout_objects_by_type(version(), object_type(), opts()) -> [domain_object()] | no_return().
checkout_objects_by_type(Reference, ObjectType, Opts) ->
Version = ref_to_version(Reference),
unwrap(dmt_client_cache:get_objects_by_type(Version, ObjectType, Opts)).
@ -134,10 +137,9 @@ checkout_filter_objects(Filter) ->
-spec checkout_filter_objects(version(), object_filter()) -> [{object_type(), domain_object()}] | no_return().
checkout_filter_objects(Reference, Filter) ->
checkout_filter_objects(Reference, Filter, undefined).
checkout_filter_objects(Reference, Filter, #{}).
-spec checkout_filter_objects(version(), object_filter(), transport_opts()) ->
[{object_type(), domain_object()}] | no_return().
-spec checkout_filter_objects(version(), object_filter(), opts()) -> [{object_type(), domain_object()}] | no_return().
checkout_filter_objects(Reference, Filter, Opts) ->
Folder = fun(Type, Object, Acc) ->
case Filter(Type, Object) of
@ -153,18 +155,18 @@ checkout_fold_objects(Folder, Acc) ->
-spec checkout_fold_objects(version(), object_folder(Acc), Acc) -> Acc | no_return().
checkout_fold_objects(Reference, Folder, Acc) ->
checkout_fold_objects(Reference, Folder, Acc, undefined).
checkout_fold_objects(Reference, Folder, Acc, #{}).
-spec checkout_fold_objects(version(), object_folder(Acc), Acc, transport_opts()) -> Acc | no_return().
-spec checkout_fold_objects(version(), object_folder(Acc), Acc, opts()) -> Acc | no_return().
checkout_fold_objects(Reference, Folder, Acc, Opts) ->
Version = ref_to_version(Reference),
unwrap(dmt_client_cache:fold_objects(Version, Folder, Acc, Opts)).
-spec commit(version(), commit()) -> version() | no_return().
commit(Version, Commit) ->
commit(Version, Commit, undefined).
commit(Version, Commit, #{}).
-spec commit(version(), commit(), transport_opts()) -> version() | no_return().
-spec commit(version(), commit(), opts()) -> version() | no_return().
commit(Version, Commit, Opts) ->
dmt_client_backend:commit(Version, Commit, Opts).
@ -174,9 +176,9 @@ get_last_version() ->
-spec pull_range(version(), limit()) -> history() | no_return().
pull_range(Version, Limit) ->
pull_range(Version, Limit, undefined).
pull_range(Version, Limit, #{}).
-spec pull_range(version(), limit(), transport_opts()) -> history() | no_return().
-spec pull_range(version(), limit(), opts()) -> history() | no_return().
pull_range(Version, Limit, Opts) ->
dmt_client_backend:pull_range(Version, Limit, Opts).

View File

@ -7,35 +7,49 @@
-export([pull_range/3]).
-export([checkout_object/3]).
-spec commit(dmt_client:vsn(), dmt_client:commit(), dmt_client:transport_opts()) -> dmt_client:vsn() | no_return().
-spec commit(dmt_client:vsn(), dmt_client:commit(), dmt_client:opts()) -> dmt_client:vsn() | no_return().
commit(Version, Commit, Opts) ->
call('Repository', 'Commit', {Version, Commit}, Opts).
-spec checkout(dmt_client:ref(), dmt_client:transport_opts()) -> dmt_client:snapshot() | no_return().
-spec checkout(dmt_client:ref(), dmt_client:opts()) -> dmt_client:snapshot() | no_return().
checkout(Reference, Opts) ->
call('Repository', 'Checkout', {Reference}, Opts).
-spec pull_range(dmt_client:vsn(), dmt_client:limit(), dmt_client:transport_opts()) ->
dmt_client:history() | no_return().
-spec pull_range(dmt_client:vsn(), dmt_client:limit(), dmt_client:opts()) -> dmt_client:history() | no_return().
pull_range(After, Limit, Opts) ->
call('Repository', 'PullRange', {After, Limit}, Opts).
-spec checkout_object(dmt_client:ref(), dmt_client:object_ref(), dmt_client:transport_opts()) ->
-spec checkout_object(dmt_client:ref(), dmt_client:object_ref(), dmt_client:opts()) ->
dmsl_domain_thrift:'DomainObject'() | no_return().
checkout_object(Reference, ObjectReference, Opts) ->
call('RepositoryClient', 'checkoutObject', {Reference, ObjectReference}, Opts).
call(ServiceName, Function, Args, TransportOpts) ->
call(ServiceName, Function, Args, Opts) ->
Url = get_service_url(ServiceName),
Service = get_service_modname(ServiceName),
Call = {Service, Function, Args},
Opts = #{
TransportOpts =
maps:merge(
#{recv_timeout => 60000, connect_timeout => 1000},
maps:merge(
genlib_app:env(dmt_client, transport_opts, #{}),
maps:get(transport_opts, Opts, #{})
)
),
CallOpts = #{
url => Url,
event_handler => get_event_handlers(),
transport_opts => ensure_transport_opts(TransportOpts)
transport_opts => TransportOpts
},
Context = woody_context:new(),
case woody_client:call(Call, Opts, Context) of
Context =
case maps:find(woody_context, Opts) of
error -> woody_context:new();
{ok, Ctx} -> Ctx
end,
case woody_client:call(Call, CallOpts, Context) of
{ok, Response} ->
Response;
{exception, Exception} ->
@ -55,10 +69,3 @@ get_service_module('RepositoryClient') ->
get_event_handlers() ->
genlib_app:env(dmt_client, woody_event_handlers, []).
-spec ensure_transport_opts(dmt_client:transport_opts()) -> woody_client_thrift_http_transport:transport_options().
ensure_transport_opts(Opts) when is_map(Opts) ->
Opts;
ensure_transport_opts(undefined) ->
Default = #{recv_timeout => 60000, connect_timeout => 1000},
genlib_app:env(dmt_client, transport_opts, Default).

View File

@ -7,33 +7,31 @@
%%% Behaviour callbacks
-callback commit(dmt_client:vsn(), dmt_client:commit(), dmt_client:transport_opts()) -> dmt_client:vsn() | no_return().
-callback commit(dmt_client:vsn(), dmt_client:commit(), dmt_client:opts()) -> dmt_client:vsn() | no_return().
-callback checkout(dmt_client:ref(), dmt_client:transport_opts()) -> dmt_client:snapshot() | no_return().
-callback checkout(dmt_client:ref(), dmt_client:opts()) -> dmt_client:snapshot() | no_return().
-callback checkout_object(dmt_client:ref(), dmt_client:object_ref(), dmt_client:transport_opts()) ->
-callback checkout_object(dmt_client:ref(), dmt_client:object_ref(), dmt_client:opts()) ->
dmsl_domain_thrift:'DomainObject'() | no_return().
-callback pull_range(dmt_client:vsn(), dmt_client:limit(), dmt_client:transport_opts()) ->
dmt_client:history() | no_return().
-callback pull_range(dmt_client:vsn(), dmt_client:limit(), dmt_client:opts()) -> dmt_client:history() | no_return().
%%% API
-spec commit(dmt_client:vsn(), dmt_client:commit(), dmt_client:transport_opts()) -> dmt_client:vsn() | no_return().
-spec commit(dmt_client:vsn(), dmt_client:commit(), dmt_client:opts()) -> dmt_client:vsn() | no_return().
commit(Version, Commit, Opts) ->
call(commit, [Version, Commit, Opts]).
-spec checkout(dmt_client:ref(), dmt_client:transport_opts()) -> dmt_client:snapshot() | no_return().
-spec checkout(dmt_client:ref(), dmt_client:opts()) -> dmt_client:snapshot() | no_return().
checkout(Reference, Opts) ->
call(checkout, [Reference, Opts]).
-spec checkout_object(dmt_client:ref(), dmt_client:object_ref(), dmt_client:transport_opts()) ->
-spec checkout_object(dmt_client:ref(), dmt_client:object_ref(), dmt_client:opts()) ->
dmsl_domain_thrift:'DomainObject'() | no_return().
checkout_object(Reference, ObjectReference, Opts) ->
call(checkout_object, [Reference, ObjectReference, Opts]).
-spec pull_range(dmt_client:vsn(), dmt_client:limit(), dmt_client:transport_opts()) ->
dmt_client:history() | no_return().
-spec pull_range(dmt_client:vsn(), dmt_client:limit(), dmt_client:opts()) -> dmt_client:history() | no_return().
pull_range(After, Limit, Opts) ->
call(pull_range, [After, Limit, Opts]).

View File

@ -87,7 +87,7 @@
start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).
-spec get(dmt_client:vsn(), dmt_client:transport_opts()) ->
-spec get(dmt_client:vsn(), dmt_client:opts()) ->
{ok, dmt_client:snapshot()} | {error, version_not_found | woody_error()}.
get(Version, Opts) ->
case ensure_version(Version, Opts) of
@ -97,7 +97,7 @@ get(Version, Opts) ->
Error
end.
-spec get_object(dmt_client:vsn(), dmt_client:object_ref(), dmt_client:transport_opts()) ->
-spec get_object(dmt_client:vsn(), dmt_client:object_ref(), dmt_client:opts()) ->
{ok, dmt_client:domain_object()} | {error, version_not_found | object_not_found | woody_error()}.
get_object(Version, ObjectRef, Opts) ->
case ensure_version(Version, Opts) of
@ -105,7 +105,7 @@ get_object(Version, ObjectRef, Opts) ->
{error, _} = Error -> Error
end.
-spec get_objects_by_type(dmt_client:vsn(), dmt_client:object_type(), dmt_client:transport_opts()) ->
-spec get_objects_by_type(dmt_client:vsn(), dmt_client:object_type(), dmt_client:opts()) ->
{ok, [dmt_client:domain_object()]} | {error, version_not_found | woody_error()}.
get_objects_by_type(Version, ObjectType, Opts) ->
case ensure_version(Version, Opts) of
@ -113,7 +113,7 @@ get_objects_by_type(Version, ObjectType, Opts) ->
{error, _} = Error -> Error
end.
-spec fold_objects(dmt_client:vsn(), dmt_client:object_folder(Acc), Acc, dmt_client:transport_opts()) ->
-spec fold_objects(dmt_client:vsn(), dmt_client:object_folder(Acc), Acc, dmt_client:opts()) ->
{ok, Acc} | {error, version_not_found | woody_error()}.
fold_objects(Version, Folder, Acc, Opts) ->
case ensure_version(Version, Opts) of
@ -311,15 +311,14 @@ get_all_snaps() ->
ets:tab2list(?TABLE).
update(From, State) ->
restart_timer(fetch_by_reference({head, #'Head'{}}, From, undefined, State)).
restart_timer(fetch_by_reference({head, #'Head'{}}, From, #{}, State)).
fetch_by_reference(Reference, From, Opts, #state{waiters = Waiters} = State) ->
DispatchFun = fun dispatch_reply/2,
NewWaiters = maybe_fetch(Reference, From, DispatchFun, Waiters, Opts),
State#state{waiters = NewWaiters}.
-spec maybe_fetch(dmt_client:ref(), from() | undefined, dispatch_fun(), waiters(), dmt_client:transport_opts()) ->
waiters().
-spec maybe_fetch(dmt_client:ref(), from() | undefined, dispatch_fun(), waiters(), dmt_client:opts()) -> waiters().
maybe_fetch(Reference, ReplyTo, DispatchFun, Waiters, Opts) ->
Prev =
case maps:find(Reference, Waiters) of
@ -331,9 +330,9 @@ maybe_fetch(Reference, ReplyTo, DispatchFun, Waiters, Opts) ->
end,
Waiters#{Reference => [{ReplyTo, DispatchFun} | Prev]}.
-spec schedule_fetch(dmt_client:ref(), dmt_client:transport_opts()) -> pid().
-spec schedule_fetch(dmt_client:ref(), dmt_client:opts()) -> pid().
schedule_fetch(Reference, Opts) ->
proc_lib:spawn_link(
spawn_link(
fun() ->
Result =
case fetch(Reference, Opts) of
@ -343,11 +342,12 @@ schedule_fetch(Reference, Opts) ->
{error, _} = Error ->
Error
end,
cast({dispatch, Reference, Result})
end
).
-spec fetch(dmt_client:ref(), dmt_client:transport_opts()) -> fetch_result().
-spec fetch(dmt_client:ref(), dmt_client:opts()) -> fetch_result().
fetch(Reference, Opts) ->
try
Snapshot = do_fetch(Reference, Opts),
@ -359,7 +359,7 @@ fetch(Reference, Opts) ->
{error, Error}
end.
-spec do_fetch(dmt_client:ref(), dmt_client:transport_opts()) -> dmt_client:snapshot() | no_return().
-spec do_fetch(dmt_client:ref(), dmt_client:opts()) -> dmt_client:snapshot() | no_return().
do_fetch({head, #'Head'{}}, Opts) ->
case latest_snapshot() of
{ok, OldHead} ->
@ -383,10 +383,8 @@ update_head(Head, PullLimit, Opts) ->
-spec dispatch_reply(from() | undefined, fetch_result()) -> _.
dispatch_reply(undefined, _Result) ->
ok;
dispatch_reply(From, {ok, Version}) ->
gen_server:reply(From, {ok, Version});
dispatch_reply(From, Error) ->
gen_server:reply(From, Error).
dispatch_reply(From, Response) ->
gen_server:reply(From, Response).
-spec build_snapshot(snap()) -> {ok, dmt_client:snapshot()} | {error, version_not_found}.
build_snapshot(#snap{vsn = Version, tid = TID}) ->
@ -557,7 +555,7 @@ test_last_access() ->
ok = put_snapshot(#'Snapshot'{version = 3, domain = dmt_domain:new()}),
ok = put_snapshot(#'Snapshot'{version = 2, domain = dmt_domain:new()}),
Ref = {category, #'domain_CategoryRef'{id = 1}},
{error, object_not_found} = get_object(3, Ref, undefined),
{error, object_not_found} = get_object(3, Ref, #{}),
ok = put_snapshot(#'Snapshot'{version = 1, domain = dmt_domain:new()}),
cleanup(),
[
@ -577,7 +575,7 @@ test_get_object() ->
ok = put_snapshot(#'Snapshot'{version = Version, domain = Domain}),
{ok, {category, Cat}} = get_object(Version, {category, Ref}, undefined).
{ok, {category, Cat}} = get_object(Version, {category, Ref}, #{}).
-spec test_get_object_by_type() -> _.
test_get_object_by_type() ->
@ -590,7 +588,7 @@ test_get_object_by_type() ->
ok = put_snapshot(#'Snapshot'{version = Version, domain = Domain}),
{ok, Objects} = get_objects_by_type(Version, category, undefined),
{ok, Objects} = get_objects_by_type(Version, category, #{}),
[Cat1, Cat2] = lists:sort(Objects).
-spec test_fold() -> _.
@ -617,7 +615,7 @@ test_fold() ->
Acc
end,
ordsets:new(),
undefined
#{}
),
[1, 2] = ordsets:to_list(OrdSet).

View File

@ -83,11 +83,11 @@ end_per_suite(C) ->
%%% Dummy API
-spec commit(dmt_client:vsn(), dmt_client:commit(), dmt_client:transport_opts()) -> dmt_client:vsn() | no_return().
-spec commit(dmt_client:vsn(), dmt_client:commit(), dmt_client:opts()) -> dmt_client:vsn() | no_return().
commit(Version, _Commit, _Opts) ->
Version.
-spec checkout(dmt_client:ref(), dmt_client:transport_opts()) -> dmt_client:snapshot() | no_return().
-spec checkout(dmt_client:ref(), dmt_client:opts()) -> dmt_client:snapshot() | no_return().
checkout({version, ?notfound_version}, _Opts) ->
erlang:throw(#'VersionNotFound'{});
checkout({version, ?unavailable_version}, _Opts) ->
@ -99,13 +99,12 @@ checkout({head, #'Head'{}}, _Opts) ->
timer:sleep(5000),
#'Snapshot'{version = ?existing_version, domain = dmt_domain:new()}.
-spec checkout_object(dmt_client:ref(), dmt_client:object_ref(), dmt_client:transport_opts()) ->
-spec checkout_object(dmt_client:ref(), dmt_client:object_ref(), dmt_client:opts()) ->
dmsl_domain_thrift:'DomainObject'() | no_return().
checkout_object(_Reference, _ObjectReference, _Opts) ->
erlang:throw(#'ObjectNotFound'{}).
-spec pull_range(dmt_client:vsn(), dmt_client:limit(), dmt_client:transport_opts()) ->
dmt_client:history() | no_return().
-spec pull_range(dmt_client:vsn(), dmt_client:limit(), dmt_client:opts()) -> dmt_client:history() | no_return().
pull_range(_Version, _Limit, _Opts) ->
timer:sleep(5000),
#{}.
@ -114,18 +113,18 @@ pull_range(_Version, _Limit, _Opts) ->
-spec get_snapshot_success(config()) -> any().
get_snapshot_success(_C) ->
{ok, #'Snapshot'{}} = dmt_client_cache:get(?existing_version, undefined).
{ok, #'Snapshot'{}} = dmt_client_cache:get(?existing_version, #{}).
-spec snapshot_not_found(config()) -> any().
snapshot_not_found(_C) ->
{error, version_not_found} = dmt_client_cache:get(?notfound_version, undefined).
{error, version_not_found} = dmt_client_cache:get(?notfound_version, #{}).
-spec woody_error(config()) -> any().
woody_error(_C) ->
{error, {woody_error, _}} = dmt_client_cache:get(?unavailable_version, undefined).
{error, {woody_error, _}} = dmt_client_cache:get(?unavailable_version, #{}).
-spec object_not_found(config()) -> any().
object_not_found(_C) ->
Ref = {category, #'domain_CategoryRef'{id = 1}},
{error, version_not_found} = dmt_client_cache:get_object(?notfound_version, Ref, undefined),
{error, object_not_found} = dmt_client_cache:get_object(?existing_version, Ref, undefined).
{error, version_not_found} = dmt_client_cache:get_object(?notfound_version, Ref, #{}),
{error, object_not_found} = dmt_client_cache:get_object(?existing_version, Ref, #{}).

View File

@ -58,7 +58,7 @@ poll(_C) ->
Ref = fixture_object_ref(1),
#'ObjectNotFound'{} = (catch dmt_client:checkout_object(latest, Ref)),
#'Snapshot'{version = Version1} = dmt_client:checkout(latest),
Version2 = dmt_client_api:commit(Version1, #'Commit'{ops = [{insert, #'InsertOp'{object = Object}}]}, undefined),
Version2 = dmt_client_api:commit(Version1, #'Commit'{ops = [{insert, #'InsertOp'{object = Object}}]}, #{}),
true = Version1 < Version2,
_ = dmt_client_cache:update(),
#'Snapshot'{version = Version2} = dmt_client:checkout(latest),