question about postcommit hooks and patch

Andy Gross andy at basho.com
Fri May 14 02:02:47 EDT 2010


Hi Bruce,

Thanks for the patch - it's definitely worthwhile and we'll likely commit it
to tip soon.

- Andy

--
Andy Gross <andy at basho.com>
VP, Engineering
Basho Technologies, Inc.


On Thu, May 13, 2010 at 6:16 PM, Bruce Lowekamp <bruce.lowekamp at skype.net>wrote:

> I've been playing with using postcommit hooks in some code.  I
> couldn't find an example, so looking at the source, I think the right
> way to set one up is something like:
>
> PHook = {struct, [ {<<"mod">>, <<?MODULE_STRING>>}, {<<"fun">>,
> <<"notify_change">>}]},
> RiakClient:set_bucket(<<?MY_KEY>>, [{postcommit, [PHook]}]),
>
> Is there a better way?
>
>
> Also, in debugging my hook, I found that wrapping the hook so I could
> see exceptions made it much easier.  I made the following patch that I
> think might be useful to others, as well:
>
> diff -r e836ea266eca apps/riak_kv/src/riak_kv_put_fsm.erl
> --- a/apps/riak_kv/src/riak_kv_put_fsm.erl      Thu May 13 17:28:01 2010
> -0400
> +++ b/apps/riak_kv/src/riak_kv_put_fsm.erl      Thu May 13 14:50:07 2010
> -0700
> @@ -314,7 +314,7 @@
>  invoke_hook(precommit, Mod0, Fun0, undefined, RObj) ->
>     Mod = binary_to_atom(Mod0, utf8),
>     Fun = binary_to_atom(Fun0, utf8),
> -    Mod:Fun(RObj);
> +    wrap_hook(Mod, Fun, RObj);
>  invoke_hook(precommit, undefined, undefined, JSName, RObj) ->
>     case riak_kv_js_manager:blocking_dispatch({{jsfun, JSName}, RObj}) of
>         {ok, <<"fail">>} ->
> @@ -331,13 +331,22 @@
>  invoke_hook(postcommit, Mod0, Fun0, undefined, Obj) ->
>     Mod = binary_to_atom(Mod0, utf8),
>     Fun = binary_to_atom(Fun0, utf8),
> -    proc_lib:spawn(fun() -> Mod:Fun(Obj) end);
> +    proc_lib:spawn(fun() -> wrap_hook(Mod,Fun,Obj) end);
>  invoke_hook(postcommit, undefined, undefined, _JSName, _Obj) ->
>     error_logger:warning_msg("Javascript post-commit hooks aren't
> implemented");
>  %% NOP to handle all other cases
>  invoke_hook(_, _, _, _, RObj) ->
>     RObj.
>
> +wrap_hook(Mod, Fun, Obj)->
> +    try Mod:Fun(Obj)
> +    catch
> +        EType:X ->
> +            error_logger:error_msg("problem invoking hook ~p:~p ->
> ~p:~p~n~p~n",
> +                                   [Mod, Fun, EType, X,
> erlang:get_stacktrace()]),
> +            fail
> +    end.
> +
>  merge_robjs(RObjs0,AllowMult) ->
>     RObjs1 = [X || X <- [riak_kv_util:obj_not_deleted(O) ||
>                             O <- RObjs0], X /= undefined],
>
>
>
> Bruce
>
> _______________________________________________
> riak-users mailing list
> riak-users at lists.basho.com
> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.basho.com/pipermail/riak-users_lists.basho.com/attachments/20100514/19eae5d2/attachment.html>


More information about the riak-users mailing list