Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Riak

Introduction to Riak

BOBkonf, 2015
Berlin, Germany
Tutorial

Christopher Meiklejohn

January 23, 2015
Tweet

More Decks by Christopher Meiklejohn

Other Decks in Programming

Transcript

  1. $ curl -O http://s3.amazonaws.com/downloads.basho.com/erlang/ otp_src_R16B02-basho5.tar.gz $ tar -xvf otp_src_R16B02-basho5.tar.gz $

    cd otp_src_R16B02-basho5 $ ./configure && make && sudo make install Installing Erlang
  2. =============================== Staged Changes ================================ Action Nodes(s) ------------------------------------------------------------------------------- join '[email protected]' join

    '[email protected]' join '[email protected]' join '[email protected]' ------------------------------------------------------------------------------- NOTE: Applying these changes will result in 1 cluster transition ############################################################################### After cluster transition 1/1 ############################################################################### ================================= Membership ================================== Status Ring Pending Node ------------------------------------------------------------------------------- valid 100.0% 20.3% '[email protected]' valid 0.0% 20.3% '[email protected]' valid 0.0% 20.3% '[email protected]' valid 0.0% 20.3% '[email protected]' valid 0.0% 18.8% '[email protected]' ------------------------------------------------------------------------------- Valid:5 / Leaving:0 / Exiting:0 / Joining:0 / Down:0 Transfers resulting from cluster changes: 51 12 transfers from '[email protected]' to '[email protected]' 13 transfers from '[email protected]' to '[email protected]' 13 transfers from '[email protected]' to '[email protected]' 13 transfers from '[email protected]' to '[email protected]' View a staged plan
  3. ================================= Membership ================================== Status Ring Pending Node ------------------------------------------------------------------------------- valid 20.3%

    -- '[email protected]' valid 20.3% -- '[email protected]' valid 20.3% -- '[email protected]' valid 20.3% -- '[email protected]' valid 18.8% -- '[email protected]' ------------------------------------------------------------------------------- Valid:5 / Leaving:0 / Exiting:0 / Joining:0 / Down:0 View members of cluster
  4. node 0 node 1 node 2 Replicas are stored to

    the N - 1 contiguous partitions
  5. Anatomy of a Request get(users/clay-davis) Get Handler (FSM) client Riak

    hash(users/clay-davis) == 10, 11, 12 Coordinating node Cluster 6 7 8 9 10 11 12 13 14 15 16 The Ring
  6. Anatomy of a Request get(users/clay-davis) Get Handler (FSM) client Riak

    get(users/clay-davis) Coordinating node Cluster 6 7 8 9 10 11 12 13 14 15 16 The Ring
  7. Anatomy of a Request get(users/clay-davis) Get Handler (FSM) client Riak

    Coordinating node Cluster 6 7 8 9 10 11 12 13 14 15 16 The Ring R=2
  8. Anatomy of a Request get(users/clay-davis) Get Handler (FSM) client Riak

    Coordinating node Cluster 6 7 8 9 10 11 12 13 14 15 16 The Ring R=2 obj
  9. merkle tree to track changes coordinated at the vnode level

    runs as a background process exchange with neighbor vnodes for inconsistencies resolution semantics: trigger read-repair
  10. Eventual Consistency Eventual consistency is a consistency model used in

    distributed computing that informally guarantees that, if no new updates are made to a given data item, eventually all accesses to that item will return the last updated value. Wikipedia
  11. [{a, v1, t1}] [{b, v1, t2}] [{a, v1, t1}] [{b,

    v1, t2}] [{b, v1, t2}] [{b, v1, t2}] Last Writer Wins
  12. [{a, v1, t1}] [{b, v1, t2}] [{a, v1, t1}] [[{a,

    v1, t1}, {b, v1, t2}] [[{a, v1, t1}, {b, v1, t2}] [[{a, v1, t1}, {b, v1, t2}] Allow Mult
  13. Distributed Consensus The problem of reaching agreement among remote processes

    is one of the most fundamental problems in distributed computing and is at the core of many algorithms for distributed data processing, distributed file management, and fault-tolerant distributed applications. Fischer, Lynch, Paterson
  14. Node 1 Node 2 Node 3 N++ prepare(N) promise(N, Vb)

    promise(N, Vc) Vn = f(Va, Vb, Vc) commit(N, Vn) accept(N)
  15. Node 1 Node 2 Node 3 N++; I = 0

    prepare(N, I) promise(N, I, Vb) promise(N, I, Vc) Vn = f(Va, Vb, Vc) commit(N, I, Vn) accept(N, I)
  16. Node 1 Node 2 Node 3 obj.epoch < epoch get(key)

    reply(Epochb, Seqb, Valb) Val = latest(Vala, Valb, Valc) Val.epoch = epoch write(Epoch, ++Seq, Val) ack(Epoch, Seq) reply(Epochc, Seqc, Valc)
  17. Node 1 Node 2 Node 3 obj.epoch < epoch get(key)

    reply(Epochb, Seqb, Valb) Latest = latest(Vala, Valb, Valc) Val = modify(Latest) write(Epoch, ++Seq, Val) ack(Epoch, Seq) reply(Epochc, Seqc, Valc)
  18. Node 1 Node 2 Node 3 obj.epoch == epoch Latest

    = local_get(Key) Val = modify(Latest) write(Epoch, ++Seq, Val) ack(Epoch, Seq)
  19. Existing Ensemble Joining Ensemble riak_01 riak_02 riak_03 riak_07 riak_08 riak_09

    [{riak_01}, {riak_02}, {riak_03}] [{riak_07}, {riak_08}, {riak_09}]
  20. $ curl -XPUT http://localhost:10018/buckets/training/keys/foo \ -H 'Content-Type: text/plain' \ -d

    'caremad data goes here' $ curl -XPUT http://localhost:10018/buckets/training/keys/bar \ -H 'Content-Type: text/plain' \ -d 'caremad caremad caremad caremad' $ curl -XPUT http://localhost:10018/buckets/training/keys/baz \ -H 'Content-Type: text/plain' \ -d 'nothing to see here' $ curl -XPUT http://localhost:10018/buckets/training/keys/bam \ -H 'Content-Type: text/plain' \ -d 'caremad caremad caremad' Create some objects; http://docs.basho.com/riak/latest/dev/using/mapreduce/
  21. > ReFun = fun(O, _, Re) -> case re:run(riak_object:get_value(O), Re,

    [global]) of {match, Matches} -> [{riak_object:key(O), length(Matches)}]; nomatch -> [{riak_object:key(O), 0}] end end. > {ok, Re} = re:compile("caremad"). > {ok, Riak} = riakc_pb_socket:start_link("127.0.0.1", 8087). > riakc_pb_socket:mapred_bucket(Riak, <<"training">>, [{map, {qfun, ReFun}, Re, true}]). Run Erlang MapReduce; http://docs.basho.com/riak/latest/dev/using/mapreduce/
  22. $ curl -XPOST localhost:8098/types/mytype/buckets/users/keys/ john_smith \ -H 'x-riak-index-twitter_bin: jsmith123' \

    -H 'x-riak-index-email_bin: [email protected]' \ -H 'Content-Type: application/json' \ -d '{"userData":"data"}' Create values with secondary index tags; http://docs.basho.com/riak/latest/dev/using/2i/
  23. $ curl -XPUT http://localhost:10018/search/index/famous \ -H 'Content-Type: application/json' \ -d

    '{"schema":"_yz_default"}' Create default index using default schema; http://docs.basho.com/riak/latest/dev/using/search/
  24. $ riak-admin bucket-type create animals '{"props":{}}' $ riak-admin bucket-type activate

    animals Create bucket type for search; http://docs.basho.com/riak/latest/dev/using/search/
  25. $ curl -XPUT http://localhost:10018/types/animals/buckets/cats/props \ -H 'Content-Type: application/json' \ -d

    '{"props":{"search_index":"famous"}}' Associate bucket, bucket type, and index; http://docs.basho.com/riak/latest/dev/using/search/
  26. $ curl -XPUT http://localhost:10018/types/animals/buckets/cats/keys/liono \ -H 'Content-Type: application/json' \ -d

    '{"name_s":"Lion-o", "age_i":30, "leader_b":true}' $ curl -XPUT http://localhost:10018/types/animals/buckets/cats/keys/cheetara \ -H 'Content-Type: application/json' \ -d '{"name_s":"Cheetara", "age_i":28, "leader_b":false}' $ curl -XPUT http://localhost:10018/types/animals/buckets/cats/keys/snarf \ -H 'Content-Type: application/json' \ -d '{"name_s":"Snarf", "age_i":43}' $ curl -XPUT http://localhost:10018/types/animals/buckets/cats/keys/panthro \ -H 'Content-Type: application/json' \ -d '{"name_s":"Panthro", "age_i":36}' Store some values; http://docs.basho.com/riak/latest/dev/using/search/
  27. $ curl “http://localhost:10018/search/query/famous? wt=json&q=name_s:Lion*” | jsonpp $ curl “http://localhost:10018/search/query/famous?wt=json&q=age_i: %5B30%20TO%20*%5D”

    | jsonpp $ curl “http://localhost:10018/search/query/famous? wt=json&q=leader_b:true%20AND%20age_i:%5B25%20TO%20*%5D” | jsonpp Perform search queries; http://docs.basho.com/riak/latest/dev/using/search/
  28. $ riak-admin bucket-type create strongly_consistent \ ‘{"props":{"consistent":true}}' $ riak-admin bucket-type

    status strongly_consistent $ riak-admin bucket-type activate strongly_consistent Enable strong consistency; http://docs.basho.com/riak/latest/dev/advanced/strong-consistency/
  29. $ riak-admin bucket-type create maps \ '{"props":{"datatype":"map"}}' $ riak-admin bucket-type

    create sets \ '{"props":{"datatype":"set"}}' $ riak-admin bucket-type create counters \ ‘{“props":{"datatype":"counter"}}' $ riak-admin bucket-type status maps $ riak-admin bucket-type activate maps Create bucket type for data types; http://docs.basho.com/riak/latest/dev/using/data-types/
  30. $ curl -XPOST http://localhost:10018/types/counters/buckets/counters/ datatypes/traffic_tickets \ -H "Content-Type: application/json" \

    -d '{"increment": 1}’ $ curl http://localhost:10018/types/counters/buckets/counters/ datatypes/traffic_tickets Operate on counters; http://docs.basho.com/riak/latest/dev/using/data-types/
  31. $ curl -XPOST http://localhost:10018/types/sets/buckets/travel/ datatypes/cities \ -H "Content-Type: application/json" \

    -d '{"add_all":["Toronto", “Montreal"]}' $ curl -XPOST http://localhost:10018/types/sets/buckets/travel/ datatypes/cities \ -H "Content-Type: application/json" \ -d '{"remove": “Montreal"}' $ curl http://localhost:10018/types/sets/buckets/travel/datatypes/ cities Operate on sets; http://docs.basho.com/riak/latest/dev/using/data-types/
  32. $ curl -XPOST http://localhost:10018/types/maps/buckets/customers/ datatypes/ahmed_info \ -H "Content-Type: application/json" \

    -d ' { "update": { "first_name_register": "Ahmed", "phone_number_register": "5551234567" } }' $ curl -XPOST http://localhost:8098/types/maps/buckets/customers/ datatypes/ahmed_info \ -H "Content-Type: application/json" \ -d ' { "update": { "annika_info_map": { "update": { "interests_set": { "add": "tango dancing" } } } } } ' Operate on maps; http://docs.basho.com/riak/latest/dev/using/data-types/