2

我在oneway我的 Thrift 函数定义之一中使用了修饰符:

...
oneway void secret_function(1: string x, 2: string y),
...

当通过 Thrift 生成相应的 Erlang 代码时,它被翻译成:

...
function_info('secret_function', reply_type) ->
  oneway_void;
function_info('secret_function', exceptions) ->
  {struct, []};
...

请注意oneway_void那里的原子。

执行该secret_function函数时,我收到以下错误:

=ERROR REPORT==== 2-Sep-2010::18:17:08 ===
oneway void secret_function threw error which must be ignored: {error,
                                                             function_clause,
                                                             [{thrift_protocol,
                                                               term_to_typeid,
                                                               [oneway_void]},
                                                              {thrift_protocol,
                                                               struct_write_loop,
                                                               3},
                                                              {thrift_protocol,
                                                               write,2},
                                                              {thrift_processor,
                                                               send_reply,
                                                               4},
                                                              {thrift_processor,
                                                               handle_function,
                                                               2},
                                                              {thrift_processor,
                                                               loop,1}]}

独立于用户代码中可能包含的错误,这里thrift_protocol:term_to_typeid/1使用oneway_void原子作为参数调用函数,这会导致函数子句。实际上,从代码中读取(thrift_protocol.erl):

...
term_to_typeid(void) -> ?tType_VOID;
term_to_typeid(bool) -> ?tType_BOOL;
term_to_typeid(byte) -> ?tType_BYTE;
term_to_typeid(double) -> ?tType_DOUBLE;
term_to_typeid(i16) -> ?tType_I16;
term_to_typeid(i32) -> ?tType_I32;
term_to_typeid(i64) -> ?tType_I64;
term_to_typeid(string) -> ?tType_STRING;
term_to_typeid({struct, _}) -> ?tType_STRUCT;
term_to_typeid({map, _, _}) -> ?tType_MAP;
term_to_typeid({set, _}) -> ?tType_SET;
term_to_typeid({list, _}) -> ?tType_LIST.
...

一个错误?还有其他解释吗?为什么oneway_void要传递给那个函数?

4

1 回答 1

0

我想我知道幕后发生了什么。

我的 Erlang 代码 (the secret_function/2) 返回 {ok, pid()} 而不是简单的 ok。即使自从我声明了 function 以来这在概念上是错误的,我oneway_void也花了一段时间来确定问题的原因。也许我们可以调整handle_succesThrift 中的函数,使其行为方式与 h 相同andle_function_catch。这是目前的handle_function_catch样子:

...
case {ErrType, ErrData} of
    _ when IsOneway ->
        Stack = erlang:get_stacktrace(),
        error_logger:warning_msg(
          "oneway void ~p threw error which must be ignored: ~p",
          [Function, {ErrType, ErrData, Stack}]),
        {State, ok};
...

即使函数声明为oneway_void,当引发异常时,也会报告问题。一个潜在的新 handle_success 函数,遵循相同的推理,可能如下所示:

handle_success(State = #thrift_processor{service = Service},
               Function,
               Result) ->
    ReplyType  = Service:function_info(Function, reply_type),
    StructName = atom_to_list(Function) ++ "_result",

    case Result of
        {reply, ReplyData} when ReplyType =:= oneway_void ->
            Stack = erlang:get_stacktrace(),
            error_logger:warning_msg(
              "oneway void ~p sent reply which must be ignored: ~p",
              [Function, {ReplyData, Stack}]),
            {State, ok};
                {reply, ReplyData} ->
            Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}},
            send_reply(State, Function, ?tMessageType_REPLY, Reply);

        ok when ReplyType == {struct, []} ->
            send_reply(State, Function, ?tMessageType_REPLY, {ReplyType, {StructName}});

        ok when ReplyType == oneway_void ->
            %% no reply for oneway void
            {State, ok}
    end.

在这里,我只是检查函数是否定义为oneway_void,如果这是真的并且我仍然收到与 atom 不同的返回值ok,我报告事故,仍然忽略返回值。

这是开发人员使用更新后的handle_success功能所看到的:

=ERROR REPORT==== 7-Sep-2010::11:06:43 ===
oneway void secret_function sent reply which must be ignored: {{ok,
                                                                  <0.262.0>},
                                                                 []}

这至少可以挽救你的生命一次(引)。

于 2010-09-07T10:17:19.087 回答