Skip to content

Instantly share code, notes, and snippets.

@angrycub
Last active December 12, 2015 07:18
Show Gist options
  • Save angrycub/4735349 to your computer and use it in GitHub Desktop.
Save angrycub/4735349 to your computer and use it in GitHub Desktop.
Simple Riak Replication Hook Sample

To install the sample hook,

  • Compile riak_repl_hook_sample.erl

    Note on the Erlang Compiler: You must use the Erlang compiler (erlc) associated with the Riak installation or the version of Erlang used when compiling Riak from source. For packaged Riak installations, you can consult Table 1 below for the default location of Riak’s erlc for each supported platform. If you compiled from source, use the erlc from the Erlang version you used to compile Riak.

    Distribution Path
    CentOS & RHEL Linux /usr/lib64/riak/erts-5.9.1/bin/erlc
    Debian & Ubuntu Linux /usr/lib/riak/erts-5.9.1/bin/erlc
    FreeBSD /usr/local/lib/riak/erts-5.9.1/bin/erlc
    SmartOS /opt/local/lib/riak/erts-5.9.1/bin/erlc
    Solaris 10 /opt/riak/lib/erts-5.9.1/bin/erlc

    Table 1: Erlang compiler executable location for packaged Riak installations on supported platforms

    Once you have determined the location of the Erlang compiler, compiling (on Ubuntu for example) is as simple as:

    /usr/lib/riak/erts-5.9.1/bin/erlc riak_repl_hook_sample.erl
    
  • Copy the riak_repl_hook_sample.beam file to the subdirectory you want to store the custom hook

    cp riak_repl_hook_sample.beam /path/to/replication/hook
    
  • Add a -pa argument to your vm.args file

    -pa /path/to/replication/hook
    
  • Add a -run argument to your vm.args file

    -run riak_repl_hook_sample register
    
%% Riak Repl replication hook sample
-module(riak_repl_hook_sample).
-export([register/0]).
-export([recv/1, send/2, send_realtime/2]).
register() ->
riak_core:wait_for_service(riak_repl),
lager:log(info,self(),"Automatically registering ~p hook with riak_core",[?MODULE_STRING]),
riak_core:register([{repl_helper, ?MODULE}]),
case lists:member({undefined,?MODULE}, app_helper:get_env(riak_core,repl_helper, [])) of
true ->
lager:log(info,self(),"Successfully registered ~p hook with riak_core",[?MODULE_STRING]);
false ->
lager:log(info,self(),"Failed to register ~p hook with riak_core",[?MODULE_STRING])
end,
ok.
recv(Object) ->
% This is a BLOCKING function.
% Longer running processes should be handled asynchronously.
lager:log(info,self(),"Called recv(~p)",[riak_object:key(Object)]),
ok.
send_realtime(_Object, _RiakClient) ->
% Do Nothing function -- These hooks are called in predictable
% but complex ways especially as the number of replication sites
% or sinks increase.
ok.
send(_Object, _RiakClient) ->
% Do Nothing function -- These hooks are called in predictable
% but complex ways especially as the number of replication sites
% or sinks increase.
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment