TD-296: Drop mg tag remains (#30)

This commit is contained in:
Alexey S 2022-06-07 13:38:22 +03:00 committed by GitHub
parent 83ee553fd0
commit fb0570df68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 98 additions and 127 deletions

View File

@ -120,22 +120,22 @@ handle_function_(Fun, Args, _Opts) when
set_meta(ID) ->
scoper:add_meta(#{customer_id => ID}).
get_history(Ref) ->
History = hg_machine:get_history(?NS, Ref),
get_history(ID) ->
History = hg_machine:get_history(?NS, ID),
unmarshal_history(map_history_error(History)).
get_history(Ref, AfterID, Limit) ->
History = hg_machine:get_history(?NS, Ref, AfterID, Limit),
get_history(ID, AfterID, Limit) ->
History = hg_machine:get_history(?NS, ID, AfterID, Limit),
unmarshal_history(map_history_error(History)).
get_state(Ref) ->
collapse_history(get_history(Ref)).
get_state(ID) ->
collapse_history(get_history(ID)).
get_state(Ref, AfterID, Limit) ->
collapse_history(get_history(Ref, AfterID, Limit)).
get_state(ID, AfterID, Limit) ->
collapse_history(get_history(ID, AfterID, Limit)).
get_initial_state(Ref) ->
collapse_history(get_history(Ref, undefined, 1)).
get_initial_state(ID) ->
collapse_history(get_history(ID, undefined, 1)).
get_public_history(CustomerID, #payproc_EventRange{'after' = AfterID, limit = Limit}) ->
[publish_customer_event(CustomerID, Ev) || Ev <- get_history(CustomerID, AfterID, Limit)].

View File

@ -82,23 +82,16 @@
%% API
-spec get(hg_machine:ref()) -> {ok, st()} | {error, notfound}.
get(Ref) ->
case hg_machine:get_history(?NS, Ref) of
-spec get(hg_machine:id()) -> {ok, st()} | {error, notfound}.
get(Id) ->
case hg_machine:get_history(?NS, Id) of
{ok, History} ->
{ok, collapse_history(unmarshal_history(History))};
Error ->
Error
end.
-spec get_payment(hg_machine:tag() | payment_id(), st()) -> {ok, payment_st()} | {error, notfound}.
get_payment({tag, Tag}, #st{payments = Ps}) ->
case lists:dropwhile(fun({_, PS}) -> not lists:member(Tag, get_payment_tags(PS)) end, Ps) of
[{_ID, PaymentSession} | _] ->
{ok, PaymentSession};
[] ->
{error, notfound}
end;
-spec get_payment(payment_id(), st()) -> {ok, payment_st()} | {error, notfound}.
get_payment(PaymentID, St) ->
case try_get_payment_session(PaymentID, St) of
PaymentSession when PaymentSession /= undefined ->
@ -107,9 +100,6 @@ get_payment(PaymentID, St) ->
{error, notfound}
end.
get_payment_tags(PaymentSession) ->
hg_invoice_payment:get_tags(PaymentSession).
-spec get_payment_opts(st()) -> hg_invoice_payment:opts().
get_payment_opts(St = #st{invoice = Invoice, party = undefined}) ->
#{
@ -367,29 +357,25 @@ set_invoicing_meta(InvoiceID, PaymentID) ->
-spec process_callback(tag(), callback()) ->
{ok, callback_response()} | {error, invalid_callback | notfound | failed} | no_return().
process_callback(Tag, Callback) ->
MachineRef =
case hg_machine_tag:get_binding(namespace(), Tag) of
{ok, _EntityID, MachineID} ->
MachineID;
{error, not_found} ->
%% Fallback to machinegun tagging
%% TODO: Remove after migration grace period
{tag, Tag}
end,
case hg_machine:call(?NS, MachineRef, {callback, Tag, Callback}) of
{ok, _Reply} = Response ->
Response;
{exception, invalid_callback} ->
{error, invalid_callback};
case hg_machine_tag:get_binding(namespace(), Tag) of
{ok, _EntityID, MachineID} ->
case hg_machine:call(?NS, MachineID, {callback, Tag, Callback}) of
{ok, _} = Ok ->
Ok;
{exception, invalid_callback} ->
{error, invalid_callback};
{error, _} = Error ->
Error
end;
{error, _} = Error ->
Error
end.
%%
-spec fail(hg_machine:ref()) -> ok.
fail(Ref) ->
case hg_machine:call(?NS, Ref, fail) of
-spec fail(hg_machine:id()) -> ok.
fail(Id) ->
case hg_machine:call(?NS, Id, fail) of
{error, failed} ->
ok;
{error, Error} ->
@ -400,19 +386,19 @@ fail(Ref) ->
%%
get_history(Ref) ->
History = hg_machine:get_history(?NS, Ref),
get_history(ID) ->
History = hg_machine:get_history(?NS, ID),
unmarshal_history(map_history_error(History)).
get_history(Ref, AfterID, Limit) ->
History = hg_machine:get_history(?NS, Ref, AfterID, Limit),
get_history(ID, AfterID, Limit) ->
History = hg_machine:get_history(?NS, ID, AfterID, Limit),
unmarshal_history(map_history_error(History)).
get_state(Ref) ->
collapse_history(get_history(Ref)).
get_state(ID) ->
collapse_history(get_history(ID)).
get_state(Ref, AfterID, Limit) ->
collapse_history(get_history(Ref, AfterID, Limit)).
get_state(ID, AfterID, Limit) ->
collapse_history(get_history(ID, AfterID, Limit)).
get_public_history(InvoiceID, #payproc_EventRange{'after' = AfterID, limit = Limit}) ->
[publish_invoice_event(InvoiceID, Ev) || Ev <- get_history(InvoiceID, AfterID, Limit)].

View File

@ -5,8 +5,6 @@
-type msgp() :: mg_msgpack_marshalling:msgpack_value().
-type id() :: mg_proto_base_thrift:'ID'().
-type tag() :: {tag, mg_proto_base_thrift:'Tag'()}.
-type ref() :: id() | tag().
-type ns() :: mg_proto_base_thrift:'Namespace'().
-type args() :: _.
@ -60,8 +58,6 @@
}.
-export_type([id/0]).
-export_type([ref/0]).
-export_type([tag/0]).
-export_type([ns/0]).
-export_type([args/0]).
-export_type([event_id/0]).
@ -117,13 +113,13 @@
start(Ns, ID, Args) ->
call_automaton('Start', {Ns, ID, wrap_args(Args)}).
-spec thrift_call(ns(), ref(), service_name(), function_ref(), args()) -> response() | {error, notfound | failed}.
thrift_call(Ns, Ref, Service, FunRef, Args) ->
thrift_call(Ns, Ref, Service, FunRef, Args, undefined, undefined, forward).
-spec thrift_call(ns(), id(), service_name(), function_ref(), args()) -> response() | {error, notfound | failed}.
thrift_call(Ns, Id, Service, FunRef, Args) ->
thrift_call(Ns, Id, Service, FunRef, Args, undefined, undefined, forward).
-spec thrift_call(Ns, Ref, Service, FunRef, Args, After, Limit, Direction) -> Result when
-spec thrift_call(Ns, Id, Service, FunRef, Args, After, Limit, Direction) -> Result when
Ns :: ns(),
Ref :: ref(),
Id :: id(),
Service :: service_name(),
FunRef :: function_ref(),
Args :: args(),
@ -131,10 +127,10 @@ thrift_call(Ns, Ref, Service, FunRef, Args) ->
Limit :: integer() | undefined,
Direction :: forward | backward,
Result :: response() | {error, notfound | failed}.
thrift_call(Ns, Ref, Service, FunRef, Args, After, Limit, Direction) ->
thrift_call(Ns, Id, Service, FunRef, Args, After, Limit, Direction) ->
EncodedArgs = marshal_thrift_args(Service, FunRef, Args),
Call = {thrift_call, Service, FunRef, EncodedArgs},
case do_call(Ns, Ref, Call, After, Limit, Direction) of
case do_call(Ns, Id, Call, After, Limit, Direction) of
{ok, Response} ->
% should be specific to a processing interface already
unmarshal_thrift_response(Service, FunRef, Response);
@ -142,56 +138,56 @@ thrift_call(Ns, Ref, Service, FunRef, Args, After, Limit, Direction) ->
Error
end.
-spec call(ns(), ref(), Args :: term()) -> response() | {error, notfound | failed}.
call(Ns, Ref, Args) ->
call(Ns, Ref, Args, undefined, undefined, forward).
-spec call(ns(), id(), Args :: term()) -> response() | {error, notfound | failed}.
call(Ns, Id, Args) ->
call(Ns, Id, Args, undefined, undefined, forward).
-spec call(Ns, Ref, Args, After, Limit, Direction) -> Result when
-spec call(Ns, Id, Args, After, Limit, Direction) -> Result when
Ns :: ns(),
Ref :: ref(),
Id :: id(),
Args :: args(),
After :: event_id() | undefined,
Limit :: integer() | undefined,
Direction :: forward | backward,
Result :: response() | {error, notfound | failed}.
call(Ns, Ref, Args, After, Limit, Direction) ->
case do_call(Ns, Ref, {schemaless_call, Args}, After, Limit, Direction) of
call(Ns, Id, Args, After, Limit, Direction) ->
case do_call(Ns, Id, {schemaless_call, Args}, After, Limit, Direction) of
{ok, Response} ->
unmarshal_schemaless_response(Response);
{error, _} = Error ->
Error
end.
-spec repair(ns(), ref(), term()) ->
-spec repair(ns(), id(), term()) ->
{ok, term()} | {error, notfound | failed | working | {repair, {failed, binary()}}} | no_return().
repair(Ns, Ref, Args) ->
Descriptor = prepare_descriptor(Ns, Ref, #mg_stateproc_HistoryRange{}),
repair(Ns, Id, Args) ->
Descriptor = prepare_descriptor(Ns, Id, #mg_stateproc_HistoryRange{}),
call_automaton('Repair', {Descriptor, wrap_args(Args)}).
-spec get_history(ns(), ref()) -> {ok, history()} | {error, notfound} | no_return().
get_history(Ns, Ref) ->
get_history(Ns, Ref, undefined, undefined, forward).
-spec get_history(ns(), id()) -> {ok, history()} | {error, notfound} | no_return().
get_history(Ns, Id) ->
get_history(Ns, Id, undefined, undefined, forward).
-spec get_history(ns(), ref(), undefined | event_id(), undefined | non_neg_integer()) ->
-spec get_history(ns(), id(), undefined | event_id(), undefined | non_neg_integer()) ->
{ok, history()} | {error, notfound} | no_return().
get_history(Ns, Ref, AfterID, Limit) ->
get_history(Ns, Ref, AfterID, Limit, forward).
get_history(Ns, Id, AfterID, Limit) ->
get_history(Ns, Id, AfterID, Limit, forward).
-spec get_history(ns(), ref(), undefined | event_id(), undefined | non_neg_integer(), direction()) ->
-spec get_history(ns(), id(), undefined | event_id(), undefined | non_neg_integer(), direction()) ->
{ok, history()} | {error, notfound} | no_return().
get_history(Ns, Ref, AfterID, Limit, Direction) ->
case get_machine(Ns, Ref, AfterID, Limit, Direction) of
get_history(Ns, Id, AfterID, Limit, Direction) ->
case get_machine(Ns, Id, AfterID, Limit, Direction) of
{ok, #{history := History}} ->
{ok, History};
Error ->
Error
end.
-spec get_machine(ns(), ref(), undefined | event_id(), undefined | non_neg_integer(), direction()) ->
-spec get_machine(ns(), id(), undefined | event_id(), undefined | non_neg_integer(), direction()) ->
{ok, machine()} | {error, notfound} | no_return().
get_machine(Ns, Ref, AfterID, Limit, Direction) ->
get_machine(Ns, Id, AfterID, Limit, Direction) ->
Range = #mg_stateproc_HistoryRange{'after' = AfterID, limit = Limit, direction = Direction},
Descriptor = prepare_descriptor(Ns, Ref, Range),
Descriptor = prepare_descriptor(Ns, Id, Range),
case call_automaton('GetMachine', {Descriptor}) of
{ok, #mg_stateproc_Machine{} = Machine} ->
{ok, unmarshal_machine(Machine)};
@ -201,21 +197,21 @@ get_machine(Ns, Ref, AfterID, Limit, Direction) ->
%%
-spec do_call(Ns, Ref, Args, After, Limit, Direction) -> Result when
-spec do_call(Ns, Id, Args, After, Limit, Direction) -> Result when
Ns :: ns(),
Ref :: ref(),
Id :: id(),
Args :: args(),
After :: event_id() | undefined,
Limit :: integer() | undefined,
Direction :: forward | backward,
Result :: {ok, response()} | {error, notfound | failed}.
do_call(Ns, Ref, Args, After, Limit, Direction) ->
do_call(Ns, Id, Args, After, Limit, Direction) ->
HistoryRange = #mg_stateproc_HistoryRange{
'after' = After,
'limit' = Limit,
'direction' = Direction
},
Descriptor = prepare_descriptor(Ns, Ref, HistoryRange),
Descriptor = prepare_descriptor(Ns, Id, HistoryRange),
case call_automaton('Call', {Descriptor, wrap_args(Args)}) of
{ok, Response} ->
{ok, unmarshal_response(Response)};
@ -549,15 +545,10 @@ marshal_term(V) ->
unmarshal_term({bin, B}) ->
binary_to_term(B).
-spec prepare_descriptor(ns(), ref(), history_range()) -> descriptor().
prepare_descriptor(NS, Ref, Range) ->
-spec prepare_descriptor(ns(), id(), history_range()) -> descriptor().
prepare_descriptor(NS, Id, Range) ->
#mg_stateproc_MachineDescriptor{
ns = NS,
ref = prepare_ref(Ref),
ref = {id, Id},
range = Range
}.
prepare_ref(ID) when is_binary(ID) ->
{id, ID};
prepare_ref({tag, Tag}) ->
{tag, Tag}.

View File

@ -6,12 +6,12 @@
-export([create_binding/3]).
-export([create_binding/4]).
-type tag() :: mg_proto_base_thrift:'Tag'().
-type tag() :: dmsl_base_thrift:'Tag'().
-type ns() :: hg_machine:ns().
-type entity_id() :: dmsl_base_thrift:'ID'().
-type machine_id() :: hg_machine:id().
-spec get_binding(ns(), tag()) -> {ok, entity_id(), machine_id()} | {error, not_found}.
-spec get_binding(ns(), tag()) -> {ok, entity_id(), machine_id()} | {error, notfound}.
get_binding(NS, Tag) ->
WoodyContext = hg_context:get_woody_context(hg_context:load()),
case bender_client:get_internal_id(tag_to_external_id(NS, Tag), WoodyContext) of
@ -20,7 +20,7 @@ get_binding(NS, Tag) ->
{ok, EntityID, #{<<"machine-id">> := MachineID}} ->
{ok, EntityID, MachineID};
{error, internal_id_not_found} ->
{error, not_found}
{error, notfound}
end.
-spec create_binding(ns(), tag(), entity_id()) -> ok | no_return().

View File

@ -30,21 +30,19 @@ handle_function('ProcessPaymentCallback', {Tag, Callback}, _) ->
handle_function('ProcessRecurrentTokenCallback', {Tag, Callback}, _) ->
handle_callback_result(hg_recurrent_paytool:process_callback(Tag, {provider, Callback}));
handle_function('GetPayment', {Tag}, _) ->
{InvoiceRef, PaymentRef} =
case hg_machine_tag:get_binding(hg_invoice:namespace(), Tag) of
{ok, EntityID, MachineID} ->
{MachineID, EntityID};
{error, not_found} ->
%% Fallback to machinegun tagging
%% TODO: Remove after migration grace period
{{tag, Tag}, {tag, Tag}}
end,
case hg_invoice:get(InvoiceRef) of
{ok, InvoiceSt} ->
case hg_invoice:get_payment(PaymentRef, InvoiceSt) of
{ok, PaymentSt} ->
Opts = hg_invoice:get_payment_opts(InvoiceSt),
hg_invoice_payment:construct_payment_info(PaymentSt, Opts);
case hg_machine_tag:get_binding(hg_invoice:namespace(), Tag) of
{ok, PaymentID, InvoiceID} ->
case hg_invoice:get(InvoiceID) of
{ok, InvoiceSt} ->
case hg_invoice:get_payment(PaymentID, InvoiceSt) of
{ok, PaymentSt} ->
hg_invoice_payment:construct_payment_info(
PaymentSt,
hg_invoice:get_payment_opts(InvoiceSt)
);
{error, notfound} ->
hg_woody_wrapper:raise(#prxprv_PaymentNotFound{})
end;
{error, notfound} ->
hg_woody_wrapper:raise(#prxprv_PaymentNotFound{})
end;

View File

@ -751,20 +751,16 @@ dispatch_callback({provider, Payload}, St) ->
-spec process_callback(tag(), callback()) ->
{ok, callback_response()} | {error, invalid_callback | notfound | failed} | no_return().
process_callback(Tag, Callback) ->
MachineRef =
case hg_machine_tag:get_binding(namespace(), Tag) of
{ok, _EntityID, MachineID} ->
MachineID;
{error, not_found} ->
%% Fallback to machinegun tagging
%% TODO: Remove after migration grace period
{tag, Tag}
end,
case hg_machine:call(?NS, MachineRef, {callback, Callback}) of
{ok, _CallbackResponse} = Result ->
Result;
{exception, invalid_callback} ->
{error, invalid_callback};
case hg_machine_tag:get_binding(namespace(), Tag) of
{ok, _EntityID, MachineID} ->
case hg_machine:call(?NS, MachineID, {callback, Callback}) of
{ok, _} = Ok ->
Ok;
{exception, invalid_callback} ->
{error, invalid_callback};
{error, _} = Error ->
Error
end;
{error, _} = Error ->
Error
end.

View File

@ -50,7 +50,7 @@
{<<"metrics">>,{pkg,<<"metrics">>,<<"1.0.1">>},2},
{<<"mg_proto">>,
{git,"https://github.com/valitydev/machinegun-proto.git",
{ref,"e4784ab4647ae9dfdf1733ff2ec97578632f137d"}},
{ref,"7d780d5aa445e37b4816ac8a433bfaffe3715f63"}},
0},
{<<"mimerl">>,{pkg,<<"mimerl">>,<<"1.2.0">>},2},
{<<"msgpack_proto">>,