MMORPG战斗系统随笔(二)、浅谈场寻路Flow Field PathFinding算法

        6)
以current为着力,寻找周边8独方向(具体方向上下左右和四单对角方向),在获取点之时光做一个可是走路判断,只塞入可走的区域点,得到neighborPoints;

自我用于平等台机械及试验redis集群模式,为这个,我索要创造6个redis实例,其中3个master,另外3单凡是slave。它们的端口是7000-7005。

  好了,今天有关场寻路算法就说到这时,后面会就写一些AI相关的篇章,下篇文章表现

# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:0-999,5461-11922 (7462 slots) master   1 additional replica(s)S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

     
 2、从openset中取出第一单要素,寻找8只样子的相邻点;

 

       
8)在该操作中,对于目前节点neighbor,首先以目前标准点current,然后更新当前节点neighbor的消耗也F1,更新其消耗F为F1+H(neighbor,endPoint),
接着进行下同样步操作9)

  1. master-slave模式3.1 配置redis
1、地图生成
    openset/closeset初始化
    while openset.length > 0 
        get the first node of openset and remove from openset and insert into closeset
        get the eight neighbor node of the current node
        search all the neighborNodes 
            if not in closeset
                get the cur distance of the node
                if the distance is original or bigger than the current distance + 1
                    update the distance to the current node distance + 1 
                insert the node into the openset

    over the search and get the map that all the valid node has newly distance to the goal

    based on the map, create the vector map of all the node

2、路径生成
    get the node position of the start point from the vector map
    get the minimum point of the node
    repeat until get the goal point
    revert the finding process, get the path!

4.4.10 Stop/Start集群

  文章说交这,也许有些同仁会给闹另外的寻路解决办法,可以留言在底下,一起读书。

 

  1、A*算法及其优化

解决:

        3)
做一个循环往复判断,判断标准是openset表中元素个数为0,此时跳出判断;

咱俩利用于redis源代码中拷贝的redis-trib.rb来创造集群。它是一个ruby脚步,为了失它能够运转,还索要如下准备工作:

        1) 设置初始点 startPoint,
目标点endPoint,地图相关的数:宽度width,高度height,地图对应的顺序格子的拦截与否数据mapData;

# mkdir -p /var/run/redis/log# mkdir -p /var/run/redis/rdb# mkdir -p /var/run/redis/pid# mkdir -p /var/run/redis/nodes

        4)
进入循环判断,取出openset表的率先个元素current,判断该是否为重要,如果是,则寻路完成,对路线表pathset进行操作,得到最终的不二法门链表;

可见:

  以就篇稿子中,大概讲述了摆寻路算法的思想,当然我未曾逐字细看,不了那个中心考虑大概了解,基于这个,我越摸索了连带的算法,找到国外的一个RTS论坛,得到部分启发:怎么被同放缓RTS游戏,
在里面提到了Flow
Field算法,并让起肯定的贯彻代码。虽然并无是全的代码,而且由于和AI控制中之steeringBehaviour混合在一起,初期还有一定之迷惑性,后来自成类的主导节点刷新,利用中涉及的Dijkstra算法的更优化,总算推导出文章被所涉嫌的寻路思想。

 

  
 基本的思绪就是是这么,网上也发出许多秋的代码,可以寻找后参考实现即可。

 

 

vim tests/integration/replication-2.tcl

     
 1、设置两摆放表openset和closeset,将目标点goal塞入;

 

       
9)在马上步操作着,主要是时下节点neighbor塞入到openSet中,然后即可进行相同不善堆积排序,当然是极度小堆排序,这样保证openSet的率先只元素永远是吃最小的节点。返回到步骤7)中

布局实例6389:

  今天深受大家带来同样首游戏受寻路算法的博客。去年,我加入一磨蹭RTS的游艺项目,负责支付中的交锋系统,战斗系统的连锁知识,属于游戏被比散乱的局部。今天即说说内部的寻路的贯彻思想,当然,由于牵扯工作保密,我无见面贴出中心代码,那么就是用简单的图代码表达核心思想即可:D

4.4.5 添加节点4.4.5.1 添加master

     
 3、对寻找出来的8独临近点,如果其莫以closeset中,则计算更新其吃,然后塞入到openset中

 

     
 4、重复2、3步,最后更新得的路径点,其中的离便由此时此刻点至对象点用有些步的值。(这儿的步,是八单趋势的,分为前后左右跟季独倾斜方向)

手动fail over必须在slave上执行:

  接着说,这时候否定了Unity自带的navigation组件,感觉思维似乎走至一个节点了。如果深入之优化A*,最终效果啊似乎以10ms左右,不可知安居乐业的符游戏之统筹。这时候,偶然发现一个论坛,其中讨论了《军团要填》这款打之寻路。军团要塞游戏中,也是近似之RTS游戏,会起同屏几十森单战斗单位,进行寻路和AI操作。其中的寻路算法就是V社好提出的场寻路算法Flow
Field
PathFinding。不得不说RTS游戏于寻路算法的献是不可忽略的,针对多人同屏操作的特性优化,其中的主脑就是是寻路的优化。这儿被有翻到之场寻路算法的链接:flow-field-pathfinding/

 

  于形似的单机游戏中,或者ARPG,亦可能优化比较好之MMORPG游戏中,基于A*优化的寻路都得满足基本的寻路要求。但是于差不多总人口RTS游戏受,如果同屏几十森独玩家,每个玩家展开同样不善寻路,就会耗费10-20ms左右,那么累积下来的年月耗就会见格外充分。而自我做的RTS游戏而是一款帧同步的游玩,如果我们锁定帧数为30/s,那么游戏受之对象每帧的更新频率在33ms左右,如果分出10-20ms用于寻路,那么,只能看游戏对象找路了,其他的征战系统还运行不起来了:D

搬迁过程中,另一个终端的IO持续没刹车。迁移完成以后,检查现在hash
slot的布:

       
10)步骤7的neighborPoints都遍历完晚,返回到步骤4)

 

  基于这样的设想,只有Pass通用的A*算法,我们利用的是Unity3D的戏引擎,那么,自然就是会见设想下一个靶:Unity中于带的Navigation寻路。这实在是一个比理想之寻路组件,unity内部封装的组件,而且做了同一多元之优化,执行之频率比高。具体的Navigation介绍,可以参考这篇稿子:unity
navigation社区index/,,
这儿,我就是可基本上的深深讨论这个寻路组件了。由于我们应用的凡帧同步的联名机制,对于每个游戏对象的计算都是在挨家挨户客户端计算,为了保证计结果的同等,需要保证各自的寻路计算是平的。如果采取navigation,那么不肯定能够确保在不同之机型下计算得到的寻路节点一致,如果起寻路节点不相同,就见面在不同之机型上冒出同一个游乐对象不同的寻路行为,从而吸引一系列之无同台,最终就店打于判定为作弊Orz

 

  这儿,我虽因故大概的合计代码来列举A*的主干过程吧:

# yum install tcl.x86_64

  说及寻路算法,很多丁的率先影响就是A*算法,是的,这是正规的影响,而且A*业已当一部分玩耍中论证了事实上用性。无论是基本的A*算法还是后期基于A*的优化算法,以及延伸出的形成算法,比如B*、JPS算法等,都对准游乐之寻路算法就了大幅度的推进作用。下面我会见受来一个中心优化版本的A*算法原理同初步代码。

此外,除了手动迁移之外,redis还会见自动迁移slave。前面介绍配置项cluster-migration-barrier时也简要说明了:

  转载请标明出处http://www.cnblogs.com/zblade/  

# /usr/local/bin/redis-trib.rb del-node 127.0.0.1:7000 6d8675118da6b492c28844395ee6915506c73b3a>>> Removing node 6d8675118da6b492c28844395ee6915506c73b3a from cluster 127.0.0.1:7000>>> Sending CLUSTER FORGET messages to the cluster...>>> SHUTDOWN the node.# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006       <---- 7006现在没有slave了   slots:0-999,5461-7460 (3000 slots) master   0 additional replica(s)M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:7461-11922 (4462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

  博客写的徐,马上以要国庆了,下一样首等我国庆放完假再回到更新吧~

 

       
7)对neighborPoints点进行逐计算和判,首先,不以closeset表中,然后计算时标准点current到即节点neighbor代价F1,如果低于当前节点neighbor的代价,则设置为新的路径点,进行下一个操作8)

 

        2)
首先用初始点塞入开放表openset,计算其F,如果对计量不做优化,H可以就此哈曼计算,计算其直线距离即可;

# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6147326f5c592aff26f822881b552888a23711c6S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006   slots:0-999,5461-7460,11923-16383 (7461 slots) master   1 additional replica(s)M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:7461-11922 (4462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

次、两栽寻路算法的基本原理和实现代码

3125:M 12 Jul 15:24:16.937 * No cluster configuration found, I'm b6be6eb409d0207e698997d79bab9adaa90348f0

一致、游戏受的常用寻路算法

与此同时以本终端测试resharding:

        5)
将该点从openset表移除,塞入到closeset中,更改相关标记;

 

A* F = G + H

1. startPoint/endPoint/width/height/mapData 初始化
2. openset/closedset/pathSet 初始化
3. startPoint->openset 
4. while openset.length > 0  基本循环
5.    current = openset[1]   取首节点
6.    if current == endPoint then 判断
7.        find the path and get the key points from pathSet and return the key points 获取节点
8.    insert the current into closedset and change it's state
9.    get the neighborNodes based on the current
10.   foreach neighbor in neighborNodes 判断
11.        if neighbor not in closedset and G_cur->neighbor < G_neighbor
12.            pathSet <- current
13.            G_neighbor <- G_cur->neighbor
14.            F_neighbor <- G_neighbor + H
15.            insert the neight into openset
16.            sort the openset immediately with the MinHeap algorithm
17. if the openset.length <0 and get none the key points to endPoint, return fail

 

     
 有利也来弊端,场寻路算法带来无限便捷的寻路,其对应之限就是矢量图一律差好成后,不可知重修改,如果欲更设置目标点,那么用再次生成一布置新的矢量图。在大气单位拟同类操作的历程中,可以就此如此的寻路算法来解决大气单位带的寻路消耗。在针对玩家随意操作寻路的游玩类受到,显然不如A*要JPS等寻路算法。所以,具体的寻路算法,是索要结合实际的戏使用场景来促成的,只有极端漂亮最适于的算法:D

 

  经过多少的解析比,整个会寻路的时间耗,主要在早期的Dijkstra算法生成地图的历程,这个进程会趁地图的变大而变长,当然,这个进程得于前期的嬉戏加载中不怕展开,这样的日坐至加载时间被,显得影响不充分。那么根据生成的矢量图寻路的进程,是绝迅速的,测量了100*100的地形图,寻路的经过不交1ms,基本得以忽略不计,这样快速便捷的寻路算法,简直对特定场合的寻路应用是最最诱惑的。

它们循环为redis集群set这样的键值对:

       
最后,要么我们找到了至目标点的门径,要么没路径点,这就算是全体A*的算法过程,其中的优化一个凡利用数据结构的办法,动态设置每个节点在openSet还是closeSet,一个是运堆排序的章程保持openSet的率先只元素一定为吃最小之临近点。

  1. standalone模式2.1 配置redis

  说到Flow Field
PathFinding算法,就得补充说一下Dijkstra算法。基于A*算法,在寻找节点的时,是无需遍历所有的节点的,只待能够在openset中找到目标点,那么这次寻路就算了。Dijkstra算法,实现之思路是针对性有的节点都施行同样浅寻路消耗计算,最终抱有地图及但及点及对象点之消耗。基于这样的消耗值,我们好更的绘图一布置各个点到对象点之门径的矢量图,这样更就可博得我们需要的次第点至对象点的矢量路线。整体的算法思想,就是这么,接下去好就此简单代码来展现Dijkstra算法的长河,基于这样的过程,不难推导出持续的矢量图绘制和寻路操作。

反省7002是不是都深受清空:

     
 5、基于第四步得到的新的路径点数据,可以更的生成一张矢量图。矢量,顾名思义,就是一个触及指于其他一个沾的操作。

首先单象征:slave是不过读的;

     
还是用代码写过程吧,感觉要习惯了描写自己的灵魂代码:D

 

        2、Flow Field
PathFinding算法原理及其实现

 

  下面我就重用我之魂写法,写一个骨干的代码思路吧,
哈哈:

 4.3 创建redis集群

   这儿说的A*算法,是针对性格子类型的地图的寻路算法,现在生许多讲解A*算法的篇章,可以参照很多底文章,其主导之盘算是针对性个别张列表的操作,开放表openset和关闭表closeset,用一个列表pathSet作为路径点列表。其主干的函数:
F = G + H, 这儿就不再赘言,可以查相关的算法讲解。

# /usr/local/bin/redis-server /usr/local/etc/redis_7001.conf# ./redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)M: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots:0-999,5461-11922 (7462 slots) master   1 additional replica(s)S: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001             <---- 7001已变成7004的slave   slots: (0 slots) slave   replicates 026e747386106ad2f68e1c89543b506d5d96c79e[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

 

以其余一样极端,修改key:81728|502047|15681480|key_8715的值:

  1. 对此新的slave或者无法通过一些共上同等的重连slave,而得开展整体同步,即传送一个RDB文件。master有一定量种艺术来传者RDB文件:

1.3 测试

为了测试master-slave模式,我得以一个host上启动2独redis实例(有格的语,当然好使多只host,每个host运行一个redis实例)。为这个,需要将redis.conf复制多份:

# ruby example.rb 127.0.0.1 7000;

master于内存中记录有replication积压量;重连的slave与master就replication
offset和master run id进行商榷:若master run
id没换(即master没有重启),并且slave请求的replication
offset在积压量里,就可以起offset开始开展有共来上平等。这点儿个规格外一个不满足,就亟须开展总体同步了。repl-backlog-size
1mb就是用以配置replication积压量大小的。

 

可经过redis-cli来说明前面的尽结果。

足见,由于slave没启动,不满足于10秒内,至少1独slave
ping过master的格,故出错。

 

redis在特定的时刻,试图把所有极其多slave的master的slave迁移给没有slave的master;有矣立即同样建制,你可概括地朝网添加一些slave,不用指定它们的master是谁。当有master失去了独具slave时(slave一个个还故障了),系统便会自行迁移给它。cluster-migration-barrier:设置自动迁移时,一个master最少保留几单slave。例如,这个价设置为2,我发3个slave,你未曾经常,我会见叫你一个;我起2个slave,你没经常,我非会见给你。

# vim consistency-test.rb            # Report            sleep @delay            if Time.now.to_i != last_report                report = "#{@reads} R (#{@failed_reads} err) | " +                         "#{@writes} W (#{@failed_writes} err) | "                report += "#{@lost_writes} lost | " if @lost_writes > 0                report += "#{@not_ack_writes} noack | " if @not_ack_writes > 0                last_report = Time.now.to_i+               puts key                puts report            end

disk-backed:在磁盘上生成RDB文件,然后传送给slave;diskless:不在磁盘上生成RDB文件,而是一边生成RDB数据,一边直接写到socket;

# /usr/local/bin/redis-server /usr/local/etc/redis_6379.conf# /usr/local/bin/redis-cli127.0.0.1:6379> set country Japan(error) NOREPLICAS Not enough good slaves to write.127.0.0.1:6379>

4.4.9 集群迁移

 

 

repl-ping-slave-period:slave向master发送PING的频率,单位是秒。
以下就半码,我们从来不装:
cluster-migration-barrier:假如发生一个master有3只slave,另一个master没有任何slave。这时,需要把第一只master的slave
迁移给老二独master。但第一单master把好之slave迁移给旁人经常,自己得有所一定个数的slave。保有个数就是cluster-migration-barrier。例如,把这价设置也3时,
就不见面产生slave迁移了,因为迁移之后保有数小于3。所以,你想禁止slave迁移,把此数设置好挺即可。cluster-require-full-coverage:若设置为yes,一旦有hash
slot没为掩盖,则集群已接纳请求。在这种情况下,若集群部分宕机,导致一些slot没有为埋,则整个集群转移得无可用。你一旦要当一些节点宕机时,被覆盖的那些slot仍会服务,把其装也no。4.4
测试集群4.4.1 redis-cli的集群模式

现行底拓扑结构是:

解决:

 

 

被见了同样次,重试make test就ok了。

咱们好透过命令将一个slave分配给别的master:

 

 

 

部署实例6379:

 

4.4.8 升级节点4.4.8.1 升级slave

 

  1. 小结

测试IO是否正常:

# /usr/local/bin/redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005>>> Creating cluster>>> Performing hash slots allocation on 6 nodes...Using 3 masters:127.0.0.1:7000127.0.0.1:7001127.0.0.1:7002Adding replica 127.0.0.1:7003 to 127.0.0.1:7000Adding replica 127.0.0.1:7004 to 127.0.0.1:7001Adding replica 127.0.0.1:7005 to 127.0.0.1:7002M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:0-5460 (5461 slots) masterM: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:5461-10922 (5462 slots) masterM: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:10923-16383 (5461 slots) masterS: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   replicates b6be6eb409d0207e698997d79bab9adaa90348f0S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756Can I set the above configuration? (type 'yes' to accept): yes>>> Nodes configuration updated>>> Assign a different config epoch to each node>>> Sending CLUSTER MEET messages to join the clusterWaiting for the cluster to join...>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:0-5460 (5461 slots) masterM: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:5461-10922 (5462 slots) masterM: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:10923-16383 (5461 slots) masterM: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) master   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) master   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) master   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

 

 

4.4.5.2 添加slave

master slave

7000 7003

7001 7004

7006 7005

repl-diskless-sync用于配置利用啊种政策。对于前者,在磁盘上颇成一浅RDB文件,可以服务多只slave;而对后人,一旦传送开始,新来的slave只能排队(等时slave同步完成)。所以,master以开始传递之前,可能要推迟一会儿,希望来还多之slave,这样master就可相互的将变化的多寡传送给她们。参数repl-diskless-sync-delay
就是用来配置推迟的岁月的,单位秒。

foo1 => 1

foo2 => 2

……

苟非思量使slave,6个实例都举行master(没有备份),把”–replicas
1″去丢即可。

实例7000 ID:b6be6eb409d0207e698997d79bab9adaa90348f0

实例7001 ID:23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9

实例7002 ID:6b92f63f64d9683e2090a28ebe9eac60d05dc756

实例7003 ID:ebfa6b5ab54e1794df5786694fcabca6f9a37f42

实例7004 ID:026e747386106ad2f68e1c89543b506d5d96c79e

实例7005 ID:441896afc76d8bc06eba1800117fa97d59453564

 

# repl-backlog-size 1mbrepl-diskless-sync norepl-diskless-sync-delay 5

表示至少发生一个master能够服务满slot(16384单)了。可以看集群创建成功了。从命的出口可以看看:

说到底两码配置表示: 在10秒内,至少1只slave ping过master;

# /usr/local/bin/redis-cli -p 7000 shutdown# /usr/local/bin/redis-cli -p 7001 shutdown# /usr/local/bin/redis-cli -p 7003 shutdown# /usr/local/bin/redis-cli -p 7004 shutdown# /usr/local/bin/redis-cli -p 7005 shutdown# /usr/local/bin/redis-cli -p 7006 shutdown# ps -ef | grep redisroot      26266  23339  0 17:24 pts/2    00:00:00 grep --color=auto redis[root@localhost ~]#

2.2 启动redis

事实上,redis-cli对cluster的支撑是比较基础的,它只是利用了节点能冲slot进行重新定向的功用。例如当点的事例中,在实例7002直达安testKey时,它算得到相应之slot是5203,而5203由实例7000担,所以其重定向到实例7000。

去slave(7007)比较轻,通过del-node即可:

 

 

4.4.6.1 删除slave节点

 

Start集群:只待一个个起动各个实例(没必要更使用redis-trib.rb
create)

安装

 

You need tcl 8.5 or newer in order to run the Redis test

 

 

徐磁盘的master,可能得考虑采用diskless传送。

7379 R (0 err) | 7379 W (0 err) |8499 R (0 err) | 8499 W (0 err) |9586 R (0 err) | 9586 W (0 err) |10736 R (0 err) | 10736 W (0 err) |12416 R (0 err) | 12416 W (0 err) |Reading: Too many Cluster redirections? (last error: MOVED 11451 127.0.0.1:7001)Writing: Too many Cluster redirections? (last error: MOVED 11451 127.0.0.1:7001)13426 R (1 err) | 13426 W (1 err) |Reading: Too many Cluster redirections? (last error: MOVED 5549 127.0.0.1:7001)Writing: Too many Cluster redirections? (last error: MOVED 5549 127.0.0.1:7001)13426 R (2 err) | 13426 W (2 err) |Reading: Too many Cluster redirections? (last error: MOVED 9678 127.0.0.1:7001)Writing: Too many Cluster redirections? (last error: MOVED 9678 127.0.0.1:7001)13427 R (3 err) | 13427 W (3 err) |Reading: Too many Cluster redirections? (last error: MOVED 10649 127.0.0.1:7001)Writing: Too many Cluster redirections? (last error: MOVED 10649 127.0.0.1:7001)13427 R (4 err) | 13427 W (4 err) |Reading: Too many Cluster redirections? (last error: MOVED 9313 127.0.0.1:7001)Writing: Too many Cluster redirections? (last error: MOVED 9313 127.0.0.1:7001)13427 R (5 err) | 13427 W (5 err) |Reading: Too many Cluster redirections? (last error: MOVED 8268 127.0.0.1:7001)Writing: Too many Cluster redirections? (last error: MOVED 8268 127.0.0.1:7001)13428 R (6 err) | 13428 W (6 err) |Reading: CLUSTERDOWN The cluster is downWriting: CLUSTERDOWN The cluster is down13432 R (661 err) | 13432 W (661 err) |14786 R (661 err) | 14786 W (661 err) |15987 R (661 err) | 15987 W (661 err) |17217 R (661 err) | 17217 W (661 err) |18320 R (661 err) | 18320 W (661 err) |18737 R (661 err) | 18737 W (661 err) |18882 R (661 err) | 18882 W (661 err) |19284 R (661 err) | 19284 W (661 err) |20121 R (661 err) | 20121 W (661 err) |21433 R (661 err) | 21433 W (661 err) |22998 R (661 err) | 22998 W (661 err) |24805 R (661 err) | 24805 W (661 err) |
# vim /usr/local/etc/redis_6389.conf daemonize yes port 6389 slaveof 127.0.0.1 6379 logfile "/var/run/redis/log/redis_6389.log" pidfile /var/run/redis/pid/redis_6389.pid dbfilename redis_6389.rdb dir /var/run/redis/rdb/ repl-ping-slave-period 10

……

解决:

# make test

4.4.6.2 删除master节点

 

下一场再次make;redis的代码包里发生自带的jemalloc;

# /usr/local/bin/redis-cli shutdown  

4.4.8.2 升级master

此间自己把min-slave-to-write改呢0,为了后文验证fail
over之后,仍能读写(否则,master
crash之后,slave取代她变成master,但其从未slave,故未可知读写)。

 

 

7000、7001和7002是master;

7000包含的slot是0-5460,slave是7003

7001包含的slot是5461-10922,slave是7004

7002包含的slot是10923-16383,slave是7005

解决:

当一个极限上,运行一致性检查器consistency-test.rb(删掉了打印key的言辞)。然后于另外一个终端上效仿crash一个master:

 

 

# /usr/local/bin/redis-cli -c -p 7002127.0.0.1:7002> set country AmericaOK127.0.0.1:7002> set testKey testValue-> Redirected to slot [5203] located at 127.0.0.1:7000OK127.0.0.1:7000> exit# /usr/local/bin/redis-cli -c -p 7005127.0.0.1:7005> get country-> Redirected to slot [12695] located at 127.0.0.1:7002"America"127.0.0.1:7002> get testKey-> Redirected to slot [5203] located at 127.0.0.1:7000"testValue"127.0.0.1:7000> set foo bar-> Redirected to slot [12182] located at 127.0.0.1:7002OK127.0.0.1:7002> exit

 

 

现行,在每个实例的log里,可以瞥见接近这样一行(当然,后面那个弄错16前进制数各不相同):

master slave

7000 7003

7001 7004

7006 7005

 

# ruby consistency-test.rb 127.0.0.1 700081728|502047|15681480|key_8715568 R (0 err) | 568 W (0 err) |81728|502047|15681480|key_33731882 R (0 err) | 1882 W (0 err) |81728|502047|15681480|key_893441 R (0 err) | 3441 W (0 err) |

 

slave-read-only yes slave-serve-stale-data yes

 

 

 

 

剔除之前,先瞧时底结构

[OK] All 16384 slots covered.

本可去除7002了:

master slave

7000 7003

7001 7004

7002 7005

7006 7007

本文主要记录了redis的配备过程(包括单机模式,主备模式及cluster模式);在这历程遭到,尽量对redis系统的劳作措施开展诠释,即使不到底详尽。希望得以当作入门知识

 

2.3 测试redis

 

俺们将删掉7007(slave)和7002(master).

# cp /usr/local/etc/redis_7000.conf /usr/local/etc/redis_7006.conf# sed -i -e 's/7000/7006/' /usr/local/etc/redis_7006.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7006.conf             <---- 1. 拷贝、修改conf,并启动一个redis实例# /usr/local/bin/redis-trib.rb add-node 127.0.0.1:7006 127.0.0.1:7000    <---- 2. 把实例加入集群>>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:0-999,5461-11922 (7462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.>>> Send CLUSTER MEET to node 127.0.0.1:7006 to make it join the cluster.[OK] New node added correctly.                                           <---- 新节点添加成功# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000                      <---- 3. 检查>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006               <---- 新节点没有任何slot;所以需要手动reshard   slots: (0 slots) master   0 additional replica(s)M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:0-999,5461-11922 (7462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.# /usr/local/bin/redis-trib.rb reshard 127.0.0.1:7000                    <---- 4. reshard>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006   slots: (0 slots) master   0 additional replica(s)M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:0-999,5461-11922 (7462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.How many slots do you want to move (from 1 to 16384)? 3000                  <---- 迁移3000个slotWhat is the receiving node ID? 6147326f5c592aff26f822881b552888a23711c6     <---- 目的地是新加的节点Please enter all the source node IDs.  Type 'all' to use all the nodes as source nodes for the hash slots.  Type 'done' once you entered all the source nodes IDs.                    <---- 源是7001;由于上次reshard,它的slot非常多,所以迁走3000Source node #1:23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9Source node #2:done......    Moving slot 7456 from 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9    Moving slot 7457 from 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9    Moving slot 7458 from 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9    Moving slot 7459 from 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9    Moving slot 7460 from 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9Do you want to proceed with the proposed reshard plan (yes/no)? yes         <---- 确认# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000                         <---- 5. 再次检查>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006                   <---- 新加的节点有3000个slot   slots:0-999,5461-7460 (3000 slots) master   0 additional replica(s)M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:7461-11922 (4462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.
# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)M: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots:0-999,5461-11922 (7462 slots) master   1 additional replica(s)S: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001                <---- 7001是7004的slave   slots: (0 slots) slave   replicates 026e747386106ad2f68e1c89543b506d5d96c79e[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.# /usr/local/bin/redis-cli -p 7001 CLUSTER FAILOVER                       <---- 在slave 7001上执行fail overOK# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004               <---- 7004变成7001的slave   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001               <---- 7001变成master   slots:0-999,5461-11922 (7462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

再度开7001,它用改成7004底slave:

 

 

 

7000:4461个

1000-5460

7001:7462个

0-999

5461-11922

7002:4461个

11923-16383

 

 

少并未必要;

# /usr/local/bin/redis-cli127.0.0.1:6379> set country chinaOK127.0.0.1:6379> get country"china"127.0.0.1:6379> set country americaOK127.0.0.1:6379> get country"america"127.0.0.1:6379> exists country(integer) 1127.0.0.1:6379> del country(integer) 1127.0.0.1:6379> exists country(integer) 0127.0.0.1:6379>exit

4.4.6 删除节点

简单单括号中之(N
err)分别表示IO错误数,而休是数码不同等。数据未平等在最后一个列打印(上例被从未数未一样现象)。为了演示数据未等同现象,我修改consistency-test.rb脚步,把key打印出,然后于任何一个极端中,通过redis-cli更改key的价值。

 

# /usr/local/bin/redis-cli -p 7000127.0.0.1:7000> set country China(error) MOVED 12695 127.0.0.1:7002127.0.0.1:7000> get country(error) MOVED 12695 127.0.0.1:7002127.0.0.1:7000> exit# /usr/local/bin/redis-cli -p 7002127.0.0.1:7002> set country ChinaOK127.0.0.1:7002> get country"China"127.0.0.1:7002> set testKey testValue(error) MOVED 5203 127.0.0.1:7000127.0.0.1:7002> exit# /usr/local/bin/redis-cli -p 7000127.0.0.1:7000> set testKey testValueOK127.0.0.1:7000> exit

第二单象征:当slave在联合新数据(从master同步数据)的下,它使原的数据服务client。这叫slave是非阻塞的。

停掉;使用初本子的redis启动;

# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005           <---- 7002的slave,变成7006的了 (若设置了cluster-migration-barrier,如何?)   slots: (0 slots) slave   replicates 6147326f5c592aff26f822881b552888a23711c6S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002           <---- 7002已经被清空   slots: (0 slots) master   0 additional replica(s)                                           <---- 并且它的slave也不见了(因为没有数据,slave是浪费) !!!!!!S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006   slots:0-999,5461-7460,11923-16383 (7461 slots) master   1 additional replica(s)M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:7461-11922 (4462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

 

4.2 启动redis实例

 

 

make distclean

错误:

某某一个特定的key,只能出于特定的master来服务?

一个再度好之客户端应该力所能及缓存 hash
slot到节点地址的照射,然后就是好一直看是的节点,免于重定向。这个映射只有以集群配置发生变化的时节才得刷新,例如,fail
over之后(slave代替了master,故节点地址变更),或者管理人添加/删除了节点(hash
slot分布发生变化)。

……

4.4.3 hash slots在节点间resharding

# cp /usr/local/etc/redis.conf /usr/local/etc/redis_6379.conf# cp /usr/local/etc/redis.conf /usr/local/etc/redis_6389.conf

4.4.7 slave迁移

面添加slave时,指定了其master。也可免指定master,这样,它以随意地成为一个master的slave;然后,可以将她迁移为指定master的slave(通过CLUSTER
REPLICATE命令)。另外,还好透过当一个空的master添加,然后使用CLUSTER
REPLICATE命令把它变成slave。

# cp /usr/local/etc/redis.conf /usr/local/etc/redis_7000.conf # vim /usr/local/etc/redis_7000.conf daemonize yes port 7000 pidfile /var/run/redis/pid/redis_7000.pid logfile "/var/run/redis/log/redis_7000.log" dbfilename redis_7000.rdb dir /var/run/redis/rdb/ min-slaves-to-write 0 cluster-enabled yes cluster-config-file /var/run/redis/nodes/nodes-7000.conf cluster-node-timeout 5000 cluster-slave-validity-factor 10 repl-ping-slave-period 10

 

– after 1000

 

3.4 停止redis

 

3.2 启动master

 

于连续展开事先,先看这几码配置起之含义:

 

其中:

莫是的。原来,redis-cli需要一个 -c
来表示cluster模式。使用cluster模式时,可以以任何节点(master或者slave)上存取数据:

错误1:

fail over命令有少只选择:

 

 

 

 

4.4.4 fail over

 

+ after 10000

当一个master crash了,一段时间后(前面配置的之5秒)会活动fail
over到她的slave上。

[err]: Slave should be able to synchronize with the master in
tests/integration/replication-psync.tcl
Replication not started.

# /usr/local/bin/redis-server /usr/local/etc/redis_7000.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7001.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7003.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7004.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7005.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7006.conf# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)M: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots:0-999,5461-7460,11923-16383 (7461 slots) master   1 additional replica(s)S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:7461-11922 (4462 slots) master   1 additional replica(s)S: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006   slots: (0 slots) slave   replicates ebfa6b5ab54e1794df5786694fcabca6f9a37f42S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

手动fail over的过程:

 

# make install# cp redis.conf /usr/local/etc/# cp src/redis-trib.rb /usr/local/bin/

 

 

 

 

 

一对上,用户可能想积极fail
over,比如,想提升有master,最好让她变成slave,这样能减弱多少对集群可用性的震慑。这虽得手动fail
over。

 

# /usr/local/bin/redis-trib.rb reshard 127.0.0.1:7000 <---- reshard >>> Performing Cluster Check (using node 127.0.0.1:7000) M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000 slots:1000-5460 (4461 slots) master 1 additional replica(s) S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005 slots: (0 slots) slave replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756 S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003 slots: (0 slots) slave replicates b6be6eb409d0207e698997d79bab9adaa90348f0 M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002 slots:11923-16383 (4461 slots) master 1 additional replica(s) S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004 slots: (0 slots) slave replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006 slots:0-999,5461-7460 (3000 slots) master 0 additional replica(s) M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001 slots:7461-11922 (4462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. How many slots do you want to move (from 1 to 16384)? 4461 <---- 我们计划把7002清空,所以需要迁移其所有slot,4461个 What is the receiving node ID? 6147326f5c592aff26f822881b552888a23711c6 <---- 目的地7006 Please enter all the source node IDs. Type 'all' to use all the nodes as source nodes for the hash slots. Type 'done' once you entered all the source nodes IDs. Source node #1:6b92f63f64d9683e2090a28ebe9eac60d05dc756 <---- 源7002 Source node #2:done ...... Moving slot 16382 from 6b92f63f64d9683e2090a28ebe9eac60d05dc756 Moving slot 16383 from 6b92f63f64d9683e2090a28ebe9eac60d05dc756 Do you want to proceed with the proposed reshard plan (yes/no)? yes
# ./redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001        <---- 可见7001是一个master   slots:0-999,5461-11922 (7462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.# /usr/local/bin/redis-cli -p 7001 debug segfault                 <---- 模拟7001 crash         Error: Server closed the connection# ./redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)M: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004        <---- 7001 fail over到7004;现在7004是master,并且没有slave   slots:0-999,5461-11922 (7462 slots) master   0 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

Stop集群:只需要一个个艾各个实例

除此以外,在6389底部署文件中,有如下配置好修改(一般不需要修改):

# ruby consistency-test.rb 127.0.0.1 700081728|502047|15681480|key_8715568 R (0 err) | 568 W (0 err) |81728|502047|15681480|key_33731882 R (0 err) | 1882 W (0 err) |81728|502047|15681480|key_89......81728|502047|15681480|key_28417884 R (0 err) | 7884 W (0 err) |81728|502047|15681480|key_3088869 R (0 err) | 8869 W (0 err) | 2 lost |81728|502047|15681480|key_67719856 R (0 err) | 9856 W (0 err) | 2 lost |
# yum install gem# gem install redis
# vim etc/redis.conf    daemonize yeslogfile "/var/run/redis/log/redis.log"pidfile /var/run/redis/pid/redis_6379.piddbfilename redis.rdbdir /var/run/redis/rdb/
# /usr/local/bin/redis-trib.rb reshard 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:0-999,5461-11922 (7462 slots) master   1 additional replica(s)S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.How many slots do you want to move (from 1 to 16384)?2000                    <----  迁移多少hash slot?What is the receiving node ID? 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9      <----  迁移目的地? 7001实例Please enter all the source node IDs.  Type 'all' to use all the nodes as source nodes for the hash slots.  Type 'done' once you entered all the source nodes IDs.Source node #1:all                                                           <----  迁移源?allDo you want to proceed with the proposed reshard plan (yes/no)? yes          <----  确认

对这几件,故事是如此的:

[exception]: Executing test client: NOREPLICAS Not enough good slaves
to write..
NOREPLICAS Not enough good slaves to write.

骨子里,那错16进制数就是每个redis实例的ID。它以集群的环境下,唯一标识一个redis实例。每个redis实例通过这ID记录其他实例,而未是透过IP和port(因为IP和port可以更改)。我们这里所说的实例,就是集聚众多中之节点,这个ID也尽管凡是Node
ID。

错误3:

# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000   slots:1000-5460 (4461 slots) master   1 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005   slots: (0 slots) slave   replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756S: 6d8675118da6b492c28844395ee6915506c73b3a 127.0.0.1:7007   slots: (0 slots) slave   replicates 6147326f5c592aff26f822881b552888a23711c6S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003   slots: (0 slots) slave   replicates b6be6eb409d0207e698997d79bab9adaa90348f0M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002   slots:11923-16383 (4461 slots) master   1 additional replica(s)S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006   slots:0-999,5461-7460 (3000 slots) master   1 additional replica(s)M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:7461-11922 (4462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

4.4.4.1 自动fail over

末段一执行:

 

安装到位后,进入redis-rb-cluster-master目录,发现内部来一个example.rb,执行其:

变量的值当也2,但是丢失了(被自己改掉了)。

3.3 启动slave

 

 

1.
编译安装1.1 下载redis

 

repl-ping-slave-period代表slave向master发送PING的效率,单位凡秒。

 

# yum install tcl
# mkdir -p /var/run/redis/log# mkdir -p /var/run/redis/rdb# mkdir -p /var/run/redis/pid# /usr/local/bin/redis-server /usr/local/etc/redis.conf# ps -ef | grep redisroot      71021      1  0 15:46 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379

以展示resharding过程遭到IO不暂停,再打开一个极限,运行

 

 

 

 

 

于测试fail
over之前,先押一下富含在redis-rb-cluster中之旁一个器:consistency-test.rb;它是一个一致性检查器:累加变量,然后检查变量的价值是否正确。

# /usr/local/bin/redis-trib.rb del-node 127.0.0.1:7000 6b92f63f64d9683e2090a28ebe9eac60d05dc756>>> Removing node 6b92f63f64d9683e2090a28ebe9eac60d05dc756 from cluster 127.0.0.1:7000>>> Sending CLUSTER FORGET messages to the cluster...>>> SHUTDOWN the node.

slave告诉master,停止处理用户请求;master停止处理用户要,并转复slave它的replication
offset;slave等待她的replication
offset和master的匹配;即等待,直到于master接收至所有数据。这时候master和slave的数码一致,并且master不接收新数据;slave开始fail
over:从大多数master获取一个布局epoc(配置变更版本号,应该是cluster-config-file包含的消息的版本号),并播放新安排(在初部署中,slave已经变成master);老的master接收及新布局,重新开始拍卖用户请求:把要重定向到新master;它自己曾改成slave;

 

 

 

可见slot的分布就发生变化:

 

剔除master节点之前,必须管master是空的(没有其他slot),这可以通过reshard来完成。然后才能够去除master。

 

在意少点:

 

# vim /usr/local/etc/redis_6379.conf daemonize yes port 6379 logfile "/var/run/redis/log/redis_6379.log" pidfile /var/run/redis/pid/redis_6379.pid dbfilename redis_6379.rdb dir /var/run/redis/rdb/ min-slaves-to-write 1 min-slaves-max-lag 10
# ruby consistency-test.rb 127.0.0.1 7000198 R (0 err) | 198 W (0 err) |685 R (0 err) | 685 W (0 err) |1174 R (0 err) | 1174 W (0 err) |1675 R (0 err) | 1675 W (0 err) |2514 R (0 err) | 2514 W (0 err) |3506 R (0 err) | 3506 W (0 err) |4501 R (0 err) | 4501 W (0 err) |

 

运行脚本,我们好望见脚本操作的每个变量的key:

# /usr/local/bin/redis-cli -p 6389 shutdown# /usr/local/bin/redis-cli -p 6379 shutdown

 

手动fail
over到一个slave上;等待master变为slave;然后,作为slave升级(停掉,使用新版redis启动);再fail
over回来(可选);

4.4.4.2 手动fail over

# make
# /usr/local/bin/redis-cli -p 7003 CLUSTER REPLICATE 6147326f5c592aff26f822881b552888a23711c6    <---- 让7003做7006的slaveOK# /usr/local/bin/redis-trib.rb check 127.0.0.1:7000>>> Performing Cluster Check (using node 127.0.0.1:7000)M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000           <---- 7000没有slave   slots:1000-5460 (4461 slots) master   0 additional replica(s)S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005           <---- 7005是7006的slave   slots: (0 slots) slave   replicates 6147326f5c592aff26f822881b552888a23711c6S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003           <---- 7003是7006的slave   slots: (0 slots) slave   replicates 6147326f5c592aff26f822881b552888a23711c6S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004   slots: (0 slots) slave   replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006           <---- 7006有两个slave   slots:0-999,5461-7460,11923-16383 (7461 slots) master   2 additional replica(s)M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001   slots:7461-11922 (4462 slots) master   1 additional replica(s)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

 

接下来,可以瞥见consistency-test.rb检查出值不一致:

 

 

# /usr/local/bin/redis-server /usr/local/etc/redis_6389.conf# /usr/local/bin/redis-cli127.0.0.1:6379> set country JapanOK
  1. 对于直接维持在连日来的slave,可以经增量同步来上基本一致。

  2. 断开重连的slave可能由此一些共来达成同等(redis
    2.8后才发生立同效能,此本之前,只能与新的slave一样,通过一体化同步来达成同等),机制是:

易见,我将启动两单redis实例,一个采用端口6379(默认端口),另一个施用6389;并且,前者为master,后者为slave。

fail
over完成以后,IO错误数已增加,集群可以连续健康劳动。没有出现无雷同错误。master
crash是唯恐引致数据不一样的(slave的数目滞后于master,master
crash后,slave取代她,导致使用落后的数额),但这种场面不是非常容易发生,因为master完成新的抒写操作时,几乎在平复客户端的以,就于slave同步了。但切莫意味不可能出现。

 

# cp /usr/local/etc/redis_7000.conf /usr/local/etc/redis_7001.conf# cp /usr/local/etc/redis_7000.conf /usr/local/etc/redis_7002.conf# cp /usr/local/etc/redis_7000.conf /usr/local/etc/redis_7003.conf# cp /usr/local/etc/redis_7000.conf /usr/local/etc/redis_7004.conf# cp /usr/local/etc/redis_7000.conf /usr/local/etc/redis_7005.conf# sed -i -e 's/7000/7001/' /usr/local/etc/redis_7001.conf# sed -i -e 's/7000/7002/' /usr/local/etc/redis_7002.conf# sed -i -e 's/7000/7003/' /usr/local/etc/redis_7003.conf# sed -i -e 's/7000/7004/' /usr/local/etc/redis_7004.conf# sed -i -e 's/7000/7005/' /usr/local/etc/redis_7005.conf

错误2:

Killing still running Redis server 63439
Killing still running Redis server 63486
Killing still running Redis server 63519
Killing still running Redis server 63546
Killing still running Redis server 63574
Killing still running Redis server 63591
I/O error reading reply

测试

 

 

1.2 编译redis

 

FORCE:上面fail
over的经过被,需要master参与。若master处于失联状态(网络故障或者master崩溃了,但尚未就机关fail
over),加上FORCE选项,则fail
over不跟master进行握手,而是直接由第4步开始。TAKEOVER:上面fail
over的经过遭到,需要大多数master的授权并发生多数master产生一个新的布局变更版本号。有时,我们无思和其余master达成一致,而直接fail
over,则需TAKEOVER选项。一个实打实的用例是:master于一个数额核心,
所有slave在另外一个多少基本,当有着master宕机或者网络分裂时,集中地将具备处于另一样数基本的slave提升为master,来上数主导切换的目的。

 

1.4 安装redis

 

 

1. 拷贝、修改conf,并启动一个redis实例 # cp /usr/local/etc/redis_7000.conf /usr/local/etc/redis_7007.conf # sed -i -e 's/7000/7007/' /usr/local/etc/redis_7007.conf # /usr/local/bin/redis-server /usr/local/etc/redis_7007.conf 2. 作为slave添加,并指定其master [root@localhost ~]# /usr/local/bin/redis-trib.rb add-node --slave --master-id 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7007 127.0.0.1:7000 >>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7000 >>> Performing Cluster Check (using node 127.0.0.1:7000) M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000 slots:1000-5460 (4461 slots) master 1 additional replica(s) S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005 slots: (0 slots) slave replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756 S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003 slots: (0 slots) slave replicates b6be6eb409d0207e698997d79bab9adaa90348f0 M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002 slots:11923-16383 (4461 slots) master 1 additional replica(s) S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004 slots: (0 slots) slave replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006 slots:0-999,5461-7460 (3000 slots) master 0 additional replica(s) M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001 slots:7461-11922 (4462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. >>> Send CLUSTER MEET to node 127.0.0.1:7007 to make it join the cluster. Waiting for the cluster to join. >>> Configure node as replica of 127.0.0.1:7006. [OK] New node added correctly. 3. 检查 # /usr/local/bin/redis-trib.rb check 127.0.0.1:7000 M: b6be6eb409d0207e698997d79bab9adaa90348f0 127.0.0.1:7000 slots:1000-5460 (4461 slots) master 1 additional replica(s) S: 441896afc76d8bc06eba1800117fa97d59453564 127.0.0.1:7005 slots: (0 slots) slave replicates 6b92f63f64d9683e2090a28ebe9eac60d05dc756 S: 6d8675118da6b492c28844395ee6915506c73b3a 127.0.0.1:7007 <---- 新节点成功添加,并成为7006的slave slots: (0 slots) slave replicates 6147326f5c592aff26f822881b552888a23711c6 S: ebfa6b5ab54e1794df5786694fcabca6f9a37f42 127.0.0.1:7003 slots: (0 slots) slave replicates b6be6eb409d0207e698997d79bab9adaa90348f0 M: 6b92f63f64d9683e2090a28ebe9eac60d05dc756 127.0.0.1:7002 slots:11923-16383 (4461 slots) master 1 additional replica(s) S: 026e747386106ad2f68e1c89543b506d5d96c79e 127.0.0.1:7004 slots: (0 slots) slave replicates 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 M: 6147326f5c592aff26f822881b552888a23711c6 127.0.0.1:7006 slots:0-999,5461-7460 (3000 slots) master 1 additional replica(s) M: 23149e93876a0d1d2cd7962d1b8fbdf42ecc64e9 127.0.0.1:7001 slots:7461-11922 (4462 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
127.0.0.1:7001> set 81728|502047|15681480|key_8715 0-> Redirected to slot [12146] located at 127.0.0.1:7002OK127.0.0.1:7002>

找不到 

4.1 配置redis集群:

 

看望现在之结构:

 

 

# ruby consistency-test.rb 127.0.0.1 7000109 R (0 err) | 109 W (0 err) |661 R (0 err) | 661 W (0 err) |1420 R (0 err) | 1420 W (0 err) |2321 R (0 err) | 2321 W (0 err) |……

每当一致性检查边,可以看见在fail over过程中冒出了一些IO错误。

(node-timeout * slave-validity-factor) + repl-ping-slave-period

即使觉得过十分,数据是一味的。

 

 

每当6379之配置文件中,有如下配置可以修改(一般不需要修改):

# cd /tmp/# wget http://download.redis.io/releases/redis-3.2.1.tar.gz# tar zxvf redis-3.2.1.tar.gz# cd redis-3.2.1/

 

今得以创造集群了:

可见:

cluster-enabled:启用集群效应。cluster-config-file:集众多中之实例通过这个文件持久化集群配置:其他实例及其状态等。不是让丁来修的。redis实例在吸收信(例如其他实例状态变化的信)时重写。cluster-node-timeout:一个节点失联时间超过此价值(单位毫秒)时,就会见以为是故障(failing)的。它发出半点个重大意义:1.
master节点失联时间超过它经常,就可能给fail over到slave上;2.
一个节点如果与多数master失联的时光过它常,就已接受请求(比如由于网络分裂,一个master被切断出,与多数master失联,当失联时间过此价值经常,它便终止工作,因为以瓦解的那么一边可能就fail
over到slave上了)。cluster-slave-validity-factor:说来谈长,一个slave当发现其的多寡极其老时,它就是未会见展开fail
over。如何规定它的数量年龄也?有星星点点只反省体制:机制1.
比方发生多个slave都得以fail over,它们就是会换换信息,根据replication offset
(这个值能反映从master得到数码的有点)来规定一个级别(rank),然后因rank来延迟fail
over。Yuanguo:可能是这么的: 根据replication
offset确定谁slave的数目比新(从master得到的大多),哪个slave的数额较原始(从master得到的丢失),然后排列出一个rank。数据乍的slave尽快fail
over,数据老的slave延迟fail
over,越老延迟的越长。机制1及之布局起无关。 机制2.
每个slave记录她与master最后一不好相(PING或者是命令)以来逝去的年华。若此日子了大,则觉得数额是总的。如何确定是时空是不是“过非常”呢?这虽是以此布局起之打算,若超过

2.4 停止redis

  1. cluster + master-slave模式Cluster能做呀:
    自动的将数据集分布到不同节点上;当一些节点故障时,能够继续服务; 数据分布:redis不应用一致性hash,而是另外一样种植样式的sharding:一共用16384单hash
    slot,它们分布于不同之节点上(例如节点A包含0-4999,节点B包含5000-9999,节点C包含10000-16384)。key被射到hash
    slot上。比如,key_foo被射到slot 1000,而slot
    1000存储于节点A上,那么key_foo(以及它的价)将积存于节点A上。另外,你可让不同的key映射到与一个slot上,方法是运用{}把key的一模一样总理分括起来;在key到slot的炫耀过程被,只考虑{}之内的片段。例如,this{foo}key和another{foo}key在投中,只有”foo”被考虑,所以她必然映射到同样的slot。
    TCP
    port:每个节点使用有限独端口:服务client的端口;集群bus端口。集群bus端口
    = 服务client的端口 +
    10000。配置过程遭到单独需要点名服务client的端口,后者由redis根据这规则自行计算。集群bus端口用于故障侦测、配置更新、fail
    over鉴权等;服务client的端口除了用于服务客户,还用于节点内搬迁数据。
    服务client的端口必须对client和所有其他节点开放;集群bus端口必须对持有其他节点开放。
    一致性保证:redis集群不克担保高一致性。也就是说,在一定情景下,redis可能有失写入的数额(虽然曾对client:已成写副)。 异步复制导致数据丢失:1.
    用户写副master节点B;2. master节点B向client回复OK;3.
    master节点B拿写副数据复制到它的slave。可见,因为B不等slave确认写副就于client回复OK,若master节点B以2事后故障,就会招数据丢失。网络分裂造成数据丢失:例如有A,B,C三只master,它们的slave分别是A1,B1,C1;如果发网络分裂,B和客户端分到边上。在cluster-node-timeout之内,客户端可继承朝着B写副数据;当跨越cluster-node-timeout时,分裂的另外一侧有fail
    over,B1中选为master。客户端向B写的多少就丢了。redis(非cluster模式)本身为恐怕丢掉数据:1.
    RBD为期快照,导致快照周期内之数量丢失;2.
    AOF,虽然各个一个描写操作都记入log,但log是限期sync的,也恐怕丢掉sync周期内之数码。
    每当下文,节点和实例是可以交换的名词(因为,我是以跟一个host上试验集群,一个节点由一个实例代替)。
# ruby example.rb 127.0.0.1 7000123456^C

 

# /usr/local/bin/redis-server /usr/local/etc/redis_7000.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7001.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7002.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7003.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7004.conf# /usr/local/bin/redis-server /usr/local/etc/redis_7005.conf

4.4.2 ruby客户端:redis-rb-cluster

# cd /usr/local/# wget https://github.com/antirez/redis-rb-cluster/archive/master.zip# unzip master.zip# cd redis-rb-cluster-master

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注