Skip to main content

Riak Core Tutorial Part 7: Coverage Calls

The content of this chapter is in the `05-coverage` branch.

How it Works

Since bucket and key are hashed together to decide to which vnode a request will go it means that the keys for a given bucket may be distributed in multiple vnodes, and in case you are running in a cluster this means your keys are distributed in multiple physical nodes.

This means that to list all the keys from a bucket we have to ask all the vnodes for the keys on a given bucket and then put the responses together and return the set of all responses.

For this Riak Core provides something called coverage calls, which are a way to handle this process of running a command on all vnodes and gathering the responses.

In this chapter we are going to implement the tanodb:keys(Bucket) function using coverage calls.

In this case we call tanodb_coverage_fsm:start({keys, Bucket}, Timeout), which is a new module, it implements a behavior called riak_core_coverage_fsm, short for riak_core_coverage finite state machine, it implements some predefined callbacks that are called on different states of a finite state machine.

The start function calls tanodb_coverage_fsm_sup:start_fsm([ReqId, self(), Request, Timeout]) which starts a supervisor for this new process.

When we start the fsm with a command {keys, Bucket} and a timeout in milliseconds, it starts a supervisor that starts the finite state machine process, it first calls the init function which initializes the state of the process and returns some information to riak_core so it knows what kind of coverage call we want to do, then riak_core calls the handle_coverage function on each vnode and with each response it calls process_result in our process.

When all the results are received or if an error happens (such as a timeout) it will call the finish callback there we send the results to the calling process which is waiting for it.

The handle_coverage implementation is really simple, it uses the ets:match/2 function to match against all the entries with the given bucket and returns the key from the matched results.

You can read more about ets match specs in the match spec chapter on the Erlang documentation.

Implementing it

Code in tanodb.erl is really simple:

keys(Bucket, Opts) ->
    Timeout = maps:get(timeout, Opts, ?TIMEOUT),
    tanodb_coverage_fsm:start({keys, Bucket}, Timeout).

In tanodb_vnode.erl we need to implement the handle_coverage callback:

handle_coverage({keys, Bucket}, _KeySpaces, {_, RefId, _},
                State=#state{kv_state=KvState}) ->
    {Keys, KvState1} = tanodb_kv_ets:keys(KvState, Bucket),
    {reply, {RefId, Keys}, State#state{kv_state=KvState1}};

We add two new modules:

The FSM implementation for the coverage call.
The supervisor for the FSM processes.

We also add the tanodb_coverage_fsm_sup to the tanodb_sup supervisor tree.

Trying it

Nums = lists:seq(1, 10).
Buckets = lists:map(fun (N) -> list_to_binary("bucket-" ++ integer_to_list(N)) end,
Keys = lists:map(fun (N) -> list_to_binary("key-" ++ integer_to_list(N)) end, Nums).

GenValue = fun (Bucket, Key) -> [{bucket, Bucket}, {key, Key}] end.

lists:foreach(fun (Bucket) ->
        lists:foreach(fun (Key) ->
                Val = GenValue(Bucket, Key),
                tanodb:put(Bucket, Key, Val)
        end, Keys)
end, Buckets).

{ok, Items} = tanodb:keys(<<"bucket-1">>).
[{Partition, Node, Keys} || {Partition, Node, Keys} <- Items, Keys =/= []].
  'tanodb@', [<<"key-10">>]},
  'tanodb@', [<<"key-4">>]},
  'tanodb@', [<<"key-8">>]},
  'tanodb@', [<<"key-9">>]},
  'tanodb@', [<<"key-2">>]},
  'tanodb@', [<<"key-9">>]},
  'tanodb@', [<<"key-10">>]},
  'tanodb@', [<<"key-5">>]},
  'tanodb@', [<<"key-4">>,<<"key-10">>]},
  'tanodb@', [<<"key-4">>]},
  'tanodb@', [<<"key-2">>]},
  'tanodb@', [<<"key-5">>]},
  'tanodb@', [<<"key-2">>]},
  'tanodb@', [<<"key-9">>]},
  'tanodb@', [<<"key-8">>]},
  'tanodb@', [<<"key-8">>]},
  'tanodb@', [<<"key-5">>]},
  'tanodb@', [<<"key-3">>]},
  'tanodb@', [<<"key-3">>]},
  'tanodb@', [<<"key-6">>]},
  'tanodb@', [<<"key-3">>]},
  'tanodb@', [<<"key-7">>,<<"key-1">>]},
  'tanodb@', [<<"key-7">>]},
  'tanodb@', [<<"key-"...>>]},
  'tanodb@', [<<...>>]},
  'tanodb@', [...]},


Comments powered by Disqus