Skip to content

Instantly share code, notes, and snippets.

@bsparrow435
Last active March 29, 2021 10:33
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save bsparrow435/7818782 to your computer and use it in GitHub Desktop.
Save bsparrow435/7818782 to your computer and use it in GitHub Desktop.
Get/Put path walk through

Put

API

Riak_client

FSM

Setup

Coordination

Riak_kv_put_core

Finalize

Vnode

Coordinating Put

Normal Put

  • Exactly the same as coordinating PUT except it does not have the coord option set. This operation is run on the remaining vnodes in the preflist after the coordinating put has been succesful.
  • The main operations are do_put and prepare_put which eventually calls Mod:put responding back to the FSM before and after sending the put to the backend.

Backend

Riak_kv_bitcask_backend

Riak_kv_eleveldb_backend

Riak_kv_memory_backend

Riak_kv_multi_backend

@martinsumner
Copy link

martinsumner commented Jan 18, 2018

Some additional information on https://gist.github.com/bsparrow435/7818782#finalize.

Unlike the GET FSM, the PUT FSM will close without all remote responses being received (and without the request timeout being reached). Adding here (copied from postriak slack) as I got confused by this when looking at a potential sidejob issue, and the use of immediate timeouts to achieve this makes it not necessarily immediately obvious from reviewing the code.

Just for the record, I've been reading through the PUT_FSM code and I don't think one of the things I said before was correct. The critical question is - does the PUT_FSM stay active until all vnodes have responded to the PUT (or a timeout triggers).

The answer, I think is actually "no". The PUT FSM (unlike the GET FSM) can effectively finish/stop when enough (rather than all) vnodes have responded.

When enough responses have been received processReply is called https://github.com/basho/riak_kv/blob/2.1.7/src/riak_kv_put_fsm.erl#L638 - and this triggers a new_state_timeout/2 call https://github.com/basho/riak_kv/blob/2.1.7/src/riak_kv_put_fsm.erl#L792

This function call will set the state to post_commit, but also trigger a timeout message immediately so that the next timeout action will happen straight away.

Each postcommit hook is then processed, with the return setting the timeout to 0 after each one causing another immediate timeout trigger - https://github.com/basho/riak_kv/blob/2.1.7/src/riak_kv_put_fsm.erl#L655-L657

When the postcommit hooks have been exhausted - there is another new_state_timeout call which will trigger a timeout message on the finish state - https://github.com/basho/riak_kv/blob/2.1.7/src/riak_kv_put_fsm.erl#L646

Handling timeout in the finish state will always result in "stop" - https://github.com/basho/riak_kv/blob/2.1.7/src/riak_kv_put_fsm.erl#L676-L697

So although the code will handle late replys in all states (e.g. https://github.com/basho/riak_kv/blob/2.1.7/src/riak_kv_put_fsm.erl#L662) the FSM will close once enough replys have been replied and the postcommit hook functions have been called (which may have to wait). A late reply is either ignored as the FSM has terminated, or taken as a message and effectively ignored by the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment