Riak Core Guide

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>,<0.179.0>,<0.185.0>,<0.143.0>,<0.155.0>,
 <0.164.0>,<0.182.0>,<0.170.0>,<0.161.0>,<0.140.0>,<0.146.0>,
 <0.152.0>,<0.158.0>,<0.176.0>,<0.167.0>,<0.149.0>,<0.137.0>,
 <0.125.0>,<0.128.0>,<0.131.0>,<0.134.0>]
(mfmn3@127.0.0.1)9>

这也再次证明了该节点的vnode个数为21.

向某个vnode发出Ping请求

这个例子是try-try-try的例子:

前面会携带一个Pid,而这个Pid是根据hash在ets表中索引出来的,这个Pid是vnode 的Pid,根据这个Pid可以索引到具体的vnode,最后Mod:handle_command是用户的回调函数。

如果master没有找到对应的vnode,那么他会新建一个vnode:

get_vnode(Idx, State=#state{vnode_mod=Mod}) ->
    case idx2vnode(Idx, State) of
        no_match ->
            {ok, Pid} = riak_core_vnode_sup:start_vnode(Mod, Idx),
            MonRef = erlang:monitor(process, Pid),
            add_vnode_rec(#idxrec{idx=Idx,pid=Pid,monref=MonRef}, State),
            Pid;
        X -> X
    end.

因为vnode下面是存储后端,所以只要定位到vnode就可以访问后端存储。

下面是try try try对应的代码:

start(_StartType, _StartArgs) ->
    case mfmn_sup:start_link() of
        {ok, Pid} ->
            ok = riak_core:register([{vnode_module, mfmn_vnode}]),
            ok = riak_core_ring_events:add_guarded_handler(mfmn_ring_event_handler, []),
            ok = riak_core_node_watcher_events:add_guarded_handler(mfmn_node_event_handler, []),
            ok = riak_core_node_watcher:service_up(mfmn, self()),
            {ok, Pid};
        {error, Reason} ->
            {error, Reason}
    end.

stop(_State) ->
    ok.

启动master, 注册vnode, 铁添加mfmn_ring_event_handler,mfmn_node_event_handler...

下面就讲解一下try try try 的例子,高手勿喷!!!



第二个例子
Riak Core, The vnode

这是一个Real Time Stastics,简称RTS--实时统计应用。

这个系统要解决的两个问题是解析记录和分发记录,这交给entry vnode处理;第二个时接收实时统计,交给stat
vnode
处理。

  • ###什么是vnode

    • vnode是一个虚拟节点,和物理节点不一样
    • 一个虚拟节点对应一个erlang
      process
    • 一个虚拟节点是一个behaviour - gen_fsm behaviour
    • 一个虚拟节点处理进来的请求
    • 一个虚拟节点可能会存储数据,这些数据可以被以后检索到
    • 很多虚拟节点会运行在同一个物理节点上
    • 每个虚拟机都有一个主虚拟节点,他主要用于和它的所有存活的节点保持联系

    如你所见,虚拟节点要处理很多东西,不过Basho已经帮我们处理掉,我们只要实现所提供的vnode
    behaviour
    即可。用户只要理解输入和输出,然后定义所需的回调函数即可。

  • 生命周期

    init/1和termiante/2回调函数是虚拟节点的生命边缘,既是起止和终止位置,当一个连接到vnode的进程崩溃,那么handle_exit/3将会被调用。

  • ###init([Index]) -> Result
?Index :: int() >= 0
Result :: {ok, State}
State :: term()

rts注册了3种vnode -- rts_vnoderts_entry_vnode、 rts_stat_vnode
对应的master vnode 分别为:rts_vnode_masterrts_entry_vnode_masterrts_stat_vnode_master

每种vnode负责不同服务.

rts提供了http的接口方便用户录入数据,其中rts_wm_entry:process_post的数据结构如下

Client: "progski"
Entry: "0.0.0.0 - - [21/Mar/2011:18:18:19 +0000] \"GET /blog/2011/aol_meet_riak.html HTTP/1.1\" 200 5865 \"http://www.google.com/\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.151 Safari/534.16\""

Client是?gunzip -c progski.access.log.gz | ./replay
progski
传递过来的.

然后,根据rts_entry来索引可用vnode,

PrefList = riak_core_apl:get_apl(DocIdx, 1, rts_entry),
    [IdxNode] = PrefList,
    rts_entry_vnode:entry(IdxNode, Client, Entry).

发送命令:

riak_core_vnode_master:command(IdxNode,
                                   {entry, Client, Entry},
                                   ?MASTER).

其中?MASTER为rts_entry_vnode_master.

如图:

最后交给stat vnode 进行统计。

时间: 2024-10-14 11:44:48

Riak Core Guide的相关文章

Riak Core 2

Learn Riak Core Step By Step 2 Riak Core, The Coordinator What is a Coordinator? 顾名思义, Coordinator即使一个协调者,主要工作就是用来协调进来的请求.它强行执行N, R, and W的一致性语义,并且执行想read repair的anti-entropy 服务.足药用在分布式集群中,当出现冲突时,用来同步数据. 从技术上说, 协调器是一个gen_fsm,每一个请求都会被他自己的erlang进程处理,一个

Core Bluetooth Programming Guide - Performing Common Peripheral Role Tasks 粗译

Core Bluetooth Programming Guide 粗译 续 code {padding:0.2em 0.4em; background:#e1e9ed;} pre {text-align:left; overflow-x: scroll; color: #d3bd7e; background: #202020; padding: 10pt 15pt; border-radius: 3px;} hr {border: 0px; border-top: 1px solid #ddd;

核心动画编程指南Core Animation Programming Guide - iOS

1 有关核心动画 1.1 概览 1.1.1 Core Animation 管理应用的内容 核心是 layer objects, 1.1.2 更改 layer 触发动画 Like views, layer objects have a bounds rectangle, a position onscreen, an opacity, a transform, and many other visually-oriented properties that can be modified. 更改这

a guide to Git‘s core concepts

原文引用https://www.dazhuanlan.com/2019/08/25/5d622c99b2cb2/ a guide to Git's core concepts I've never used another VCS before I learn the Git. So I do not know much about other VCS's details. But maybe this is a good thing because you must not apply you

Apache Struts 2 Documentation Core Developers Guide

http://struts.apache.org/docs/core-developers-guide.html

Core Bluetooth Programming Guide

https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html

Linux Kernel - Debug Guide (Linux内核调试指南 )

http://blog.csdn.net/blizmax6/article/details/6747601 linux内核调试指南 一些前言 作者前言 知识从哪里来 为什么撰写本文档 为什么需要汇编级调试 ***第一部分:基础知识*** 总纲:内核世界的陷阱 源码阅读的陷阱 代码调试的陷阱 原理理解的陷阱 建立调试环境 发行版的选择和安装 安装交叉编译工具 bin工具集的使用 qemu的使用 initrd.img的原理与制作 x86虚拟调试环境的建立 arm虚拟调试环境的建立 arm开发板调试环

A Full Hardware Guide to Deep Learning

A Full Hardware Guide to Deep Learning Deep Learning is very computationally intensive, so you will need a fast CPU with many cores, right? Or is it maybe wasteful to buy a fast CPU? One of the worst things you can do when building a deep learning sy

IOS Core Animation Advanced Techniques的学习笔记(一)

转载. Book Description Publication Date: August 12, 2013 Core Animation is the technology underlying Apple’s iOS user interface. By unleashing the full power of Core Animation, you can enhance your app with impressive 2D and 3D visual effects and creat