Learn Riak Core Step By Step
riak core 是 riak的主要组成部分,主要负责分布式的部分,虽然官方有自己的存储后端,但是我们也可以使用其他的后端存储。
Partitioning & Distributing Work
riak core 在每个节点上都是使用master/worker配置,这样作为一个工作单元来执行,riak core的worker为vnode worker, 在每个节点上由riak_core_sup生成,vnode worker对应的模块为riak_core_vnode
。
如:
节点1:
Node: 'mfmn2@127.0.0.1', Process: <0.80.0>
[{
registered_name,riak_core_vnode_sup},
{
current_function,{
gen_server,loop,6}},
{
initial_call,{
proc_lib,init_p,5}},
{
status,waiting},
{
message_queue_len,0},
{
messages,[]},
{
links,[<0.148.0>,<0.152.0>,<0.154.0>,<0.155.0>,<0.153.0>,<0.150.0>,
<0.151.0>,<0.149.0>,<0.140.0>,<0.144.0>,<0.146.0>,<0.147.0>,
<0.145.0>,<0.142.0>,<0.143.0>,<0.141.0>,<0.136.0>,<0.138.0>,
<0.139.0>,<0.137.0>,<0.77.0>,<0.135.0>]},
{
dictionary,[{
'$ancestors',[riak_core_sup,<0.76.0>]},
{
'$initial_call',{
supervisor_pre_r14b04,init,1}}]},
{
trap_exit,true},
{
error_handler,error_handler},
{
priority,normal},
{
group_leader,<0.75.0>},
{
total_heap_size,3571},
{
heap_size,2584},
{
stack_size,9},
{
reductions,4359},
{
garbage_collection,[{
min_bin_vheap_size,46368},
{
min_heap_size,233},
{
fullsweep_after,10},
{
minor_gcs,6}]},
{
suspending,[]}]
节点2:
Node: 'mfmn1@127.0.0.1', Process: <0.80.0>
[{
registered_name,riak_core_vnode_sup},
{
current_function,{
gen_server,loop,6}},
{
initial_call,{
proc_lib,init_p,5}},
{
status,waiting},
{
message_queue_len,0},
{
messages,[]},
{
links,[<0.183.0>,<0.274.0>,<0.337.0>,<0.375.0>,<0.387.0>,<0.371.0>,
<0.310.0>,<0.326.0>,<0.226.0>,<0.262.0>,<0.210.0>,<0.218.0>,
<0.153.0>,<0.165.0>,<0.177.0>,<0.171.0>,<0.159.0>,<0.135.0>,
<0.147.0>,<0.141.0>,<0.123.0>,<0.129.0>,<0.77.0>]},
{
dictionary,[{
'$ancestors',[riak_core_sup,<0.76.0>]},
{
'$initial_call',{
supervisor_pre_r14b04,init,1}}]},
{
trap_exit,true},
{
error_handler,error_handler},
{
priority,normal},
{
group_leader,<0.75.0>},
{
total_heap_size,1974},
{
heap_size,987},
{
stack_size,9},
{
reductions,8777},
{
garbage_collection,[{
min_bin_vheap_size,46368},
{
min_heap_size,233},
{
fullsweep_after,10},
{
minor_gcs,2}]},
{
suspending,[]}]
节点3:
Node: 'mfmn3@127.0.0.1', Process: <0.80.0>
[{
registered_name,riak_core_vnode_sup},
{
current_function,{
gen_server,loop,6}},
{
initial_call,{
proc_lib,init_p,5}},
{
status,waiting},
{
message_queue_len,0},
{
messages,[]},
{
links,[<0.152.0>,<0.167.0>,<0.179.0>,<0.185.0>,<0.182.0>,<0.173.0>,
<0.176.0>,<0.170.0>,<0.161.0>,<0.164.0>,<0.158.0>,<0.155.0>,
<0.137.0>,<0.143.0>,<0.149.0>,<0.146.0>,<0.140.0>,<0.128.0>,
<0.134.0>,<0.131.0>,<0.125.0>,<0.77.0>]},
{
dictionary,[{
'$ancestors',[riak_core_sup,<0.76.0>]},
{
'$initial_call',{
supervisor_pre_r14b04,init,1}}]},
{
trap_exit,true},
{
error_handler,error_handler},
{
priority,normal},
{
group_leader,<0.75.0>},
{
total_heap_size,3194},
{
heap_size,2584},
{
stack_size,9},
{
reductions,4507},
{
garbage_collection,[{
min_bin_vheap_size,46368},
{
min_heap_size,233},
{
fullsweep_after,10},
{
minor_gcs,10}]},
{
suspending,[]}]
3个节点的vnode worker 加起来刚好22 + 23 + 22 - 3 = 64
(mfmn3@127.0.0.1)3> supervisor:count_children(riak_core_vnode_sup).
[{
specs,1},{
active,21},{
supervisors,0},{
workers,21}]
(mfmn3@127.0.0.1)4>
减去3的原因。
从上面那张图可以看出, riak_core_vnode_master负责与vnode的通信,这些vnode都是fsm,如:
获取当前节点的所有vnode:
(mfmn3@127.0.0.1)8> riak_core_vnode_master:all_nodes(mfmn_vnode).
[<0.173.0