码迷,mamicode.com
首页 > 其他好文 > 详细

ddt的用法:根据数据来生成测试用例

时间:2020-08-10 13:19:52      阅读:74      评论:0      收藏:0      [点我收藏+]

标签:循环   def   task   用例   怎么办   inf   res   item   sse   

前言

前文学习了redis的主从复制,通过配置实现了一主多从,既读写分离又互为备份。但是如果出于不可抗力,主服务器下线了,那么redis集群将失去写功能,如果可以实现主服务器掉线后,自动选举从服务器升级为主服务器,那上述问题自然迎刃而解。事实上从redis2.6版本开始,便有了这么一种解决方案——Sentinel。(PS:实验Redis版本为5.0.5)

Sentinel哨兵

? 哨兵是由一个或多个Sentinel实例组成哨兵集群,监控任意多个主服务器和从服务器。当主服务器下线后,将自动从在线的从服务器中选一台升级为主服务器。并将原主服务器标记为从服务器。实现了主从服务器的自主切换,提高了redis的可用性。

准备

前文使用三台虚拟机搭建了一主二从,今天使用另外一种方法,通过复制三份redis.conf文件,修改端口实现单机主从复制。

  1. 准备三个配置文件,分别使用端口6379、6380、6381

    # redis-6379.conf
    ...
    bind 127.0.0.1
    protected-mode yes
    port 6379
    daemonize yes
    ...
    
    # redis-6380.conf
    ...
    bind 127.0.0.1
    protected-mode yes
    port 6380
    daemonize yes
    
    # redis-6381.conf
    ...
    bind 127.0.0.1
    protected-mode yes
    port 6381
    daemonize yes
    
  2. 为从库配置追加主从复制

    # 6379 和 6381中追加以下配置
    slaveof 127.0.0.1 6380
    
  3. 启动主从服务器

    redis-server redis-6379.conf
    redis-server redis-6380.conf
    redis-server redis-6381.conf
    ---------------------------------------------------------------------------------------------
    [root@VM-0-4-centos etc]# ps -ef|grep redis
    root      6321     1  0 21:36 ?        00:00:00 redis-server 127.0.0.1:6379
    root      6342     1  0 21:36 ?        00:00:00 redis-server 127.0.0.1:6380
    root      6372     1  0 21:36 ?        00:00:00 redis-server 127.0.0.1:6381
    root      6418 31466  0 21:36 pts/3    00:00:00 grep --color=auto redis
    

配置哨兵

  1. 修改sentinel-6379.conf配置文件指定master的IP和port,quorum,其中quorum的值用于设定最少需要几个Sentinel实例对master失去连接时判定为主机下线。

    // 修改默认启动方式为后台启动,这里为方便查看日志,未做修改
    daemonize yes
    // 监听主机ip port rum
    sentinel monitor mymaster 192.168.204.121 6380 1
    
  2. 启动

    [root@VM-0-4-centos etc]# redis-sentinel sentinel-6379.conf 
    6903:X 10 Aug 2020 21:39:08.389 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    6903:X 10 Aug 2020 21:39:08.389 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=6903, just started
    6903:X 10 Aug 2020 21:39:08.389 # Configuration loaded
                    _._                                                  
               _.-``__ ‘‘-._                                             
          _.-``    `.  `_.  ‘‘-._           Redis 5.0.5 (00000000/0) 64 bit
      .-`` .-```.  ```\/    _.,_ ‘‘-._                                   
     (    ‘      ,       .-`  | `,    )     Running in sentinel mode
     |`-._`-...-` __...-.``-._|‘` _.-‘|     Port: 26379
     |    `-._   `._    /     _.-‘    |     PID: 6903
      `-._    `-._  `-./  _.-‘    _.-‘                                   
     |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
     |    `-._`-._        _.-‘_.-‘    |           http://redis.io        
      `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
     |`-._`-._    `-.__.-‘    _.-‘_.-‘|                                  
     |    `-._`-._        _.-‘_.-‘    |                                  
      `-._    `-._`-.__.-‘_.-‘    _.-‘                                   
          `-._    `-.__.-‘    _.-‘                                       
              `-._        _.-‘                                           
                  `-.__.-‘                                               
    
    6903:X 10 Aug 2020 21:39:08.390 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    6903:X 10 Aug 2020 21:39:08.390 # Sentinel ID is de4d4a52a39c8e4532dca5a91679749aeffbfccd
    6903:X 10 Aug 2020 21:39:08.390 # +monitor master mymaster 127.0.0.1 6380 quorum 1
    6903:X 10 Aug 2020 21:39:08.392 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:39:08.401 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
    

    如上所示,单哨兵启动,监控主(127.0.0.1 6380)从(127.0.0.1 6379、127.0.0.1 6381)工作状态。

  3. 验证主从切换

    # 关闭主数据库
    [root@VM-0-4-centos etc]# ps -ef|grep redis
    root      6321     1  0 21:36 ?        00:00:00 redis-server 127.0.0.1:6379
    root      6342     1  0 21:36 ?        00:00:00 redis-server 127.0.0.1:6380
    root      6372     1  0 21:36 ?        00:00:00 redis-server 127.0.0.1:6381
    root      6903 31466  0 21:39 pts/3    00:00:00 redis-sentinel *:26379 [sentinel]
    root      7425 26828  0 21:42 pts/0    00:00:00 grep --color=auto redis
    [root@VM-0-4-centos etc]# kill -9 6342
    
    ---↓↓↓哨兵日志↓↓↓-----------------------------------------------------------------------
    6903:X 10 Aug 2020 21:42:41.759 # +sdown master mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:41.759 # +odown master mymaster 127.0.0.1 6380 #quorum 1/1
    6903:X 10 Aug 2020 21:42:41.759 # +new-epoch 2
    6903:X 10 Aug 2020 21:42:41.759 # +try-failover master mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:41.764 # +vote-for-leader de4d4a52a39c8e4532dca5a91679749aeffbfccd 2
    6903:X 10 Aug 2020 21:42:41.764 # +elected-leader master mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:41.764 # +failover-state-select-slave master mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:41.835 # +selected-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:41.835 * +failover-state-send-slaveof-noone slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:41.890 * +failover-state-wait-promotion slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:42.434 # +promoted-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:42.434 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:42.519 * +slave-reconf-sent slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:43.457 * +slave-reconf-inprog slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:43.457 * +slave-reconf-done slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:43.512 # +failover-end master mymaster 127.0.0.1 6380
    6903:X 10 Aug 2020 21:42:43.512 # +switch-master mymaster 127.0.0.1 6380 127.0.0.1 6381
    6903:X 10 Aug 2020 21:42:43.512 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
    6903:X 10 Aug 2020 21:42:43.512 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
    6903:X 10 Aug 2020 21:42:46.550 # +sdown slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
    

    如上所示,主从切换经过以下几个步骤:

    1. 哨兵检测到master下线
    2. 哨兵通知所有哨兵进行测试(这里是单机,所以结果为1/1)
    3. 哨兵(集群)超过半数认定master已离线,发起纪元更新
    4. 哨兵选举执行者执行主从切换
    5. 哨兵选举的leader将当前master标记为slave
    6. 哨兵从当前slave中选取一个标记为master
    7. 标记结束,新的master(6381)连接仍存活的slave-6379以及降级为slave的6380
  4. 重新连接

    原master(6380)断线重连后,自动以slave的身份继续工作

    [root@VM-0-4-centos etc]# redis-server redis-6380.conf 
    10338:C 10 Aug 2020 22:03:17.687 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    10338:C 10 Aug 2020 22:03:17.687 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=10338, just started
    10338:C 10 Aug 2020 22:03:17.687 # Configuration loaded
    ---↓↓↓哨兵日志↓↓↓-----------------------------------------------------------------------
    6903:X 10 Aug 2020 22:03:18.262 # -sdown slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
    6903:X 10 Aug 2020 22:03:28.230 * +convert-to-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
    

    如上所示,6380重连后,哨兵将6380离线的状态解除,并将6380转为slave of 6381

哨兵集群

  1. 复制哨兵配置

    sentinel-6379.conf
    sentinel-6380.conf
    sentinel-6381.conf

  2. 修改哨兵port及myid

    这里部署3台哨兵,quorum取(n/2)+1 = 2

    # sentinel-6379.conf
    port 26379
    sentinel myid de4d4a52a39c8e4532dca5a91679749aeffbfccd
    sentinel monitor mymaster 127.0.0.1 6381 2
    
    # sentinel-6380.conf
    port 26380
    sentinel myid de4d4a52a39c8e4532dca5a91679749aeffbfccc
    sentinel monitor mymaster 127.0.0.1 6381 2
    # sentinel-6381.conf
    port 26381
    sentinel myid de4d4a52a39c8e4532dca5a91679749aeffbfcce
    sentinel monitor mymaster 127.0.0.1 6381 2
    
  3. 启动三个哨兵

    [root@VM-0-4-centos etc]# redis-sentinel sentinel-6379.conf 
    ...
    11749:X 10 Aug 2020 22:13:51.528 # Sentinel ID is de4d4a52a39c8e4532dca5a91679749aeffbfccd
    11749:X 10 Aug 2020 22:13:51.528 # +monitor master mymaster 127.0.0.1 6381 quorum 1
    
    [root@VM-0-4-centos etc]# redis-sentinel sentinel-6380.conf 
    ...
    11916:X 10 Aug 2020 22:14:57.418 # Sentinel ID is de4d4a52a39c8e4532dca5a91679749aeffbfccc
    11916:X 10 Aug 2020 22:14:57.418 # +monitor master mymaster 127.0.0.1 6381 quorum 2
    11916:X 10 Aug 2020 22:14:57.419 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
    11916:X 10 Aug 2020 22:14:57.429 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
    11916:X 10 Aug 2020 22:14:58.832 * +sentinel sentinel de4d4a52a39c8e4532dca5a91679749aeffbfccd 127.0.0.1 26379 @ mymaster 127.0.0.1 6381
    11916:X 10 Aug 2020 22:14:58.841 # +new-epoch 2
    
    [root@VM-0-4-centos ~]# cd /usr/local/redis/etc/
    [root@VM-0-4-centos etc]# redis-sentinel sentinel-6381.conf 
    ...
    12131:X 10 Aug 2020 22:16:08.834 # Sentinel ID is de4d4a52a39c8e4532dca5a91679749aeffbfcce
    12131:X 10 Aug 2020 22:16:08.834 # +monitor master mymaster 127.0.0.1 6381 quorum 2
    12131:X 10 Aug 2020 22:16:08.836 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
    12131:X 10 Aug 2020 22:16:08.840 * +slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
    12131:X 10 Aug 2020 22:16:10.244 * +sentinel sentinel de4d4a52a39c8e4532dca5a91679749aeffbfccd 127.0.0.1 26379 @ mymaster 127.0.0.1 6381
    12131:X 10 Aug 2020 22:16:10.252 # +new-epoch 2
    12131:X 10 Aug 2020 22:16:10.769 * +sentinel sentinel de4d4a52a39c8e4532dca5a91679749aeffbfccc 127.0.0.1 26380 @ mymaster 127.0.0.1 6381
    

    如上所示,哨兵只需要配置master即可自动识别slave和其他的哨兵。

    1. 验证主从切换

      [root@VM-0-4-centos ~]# ps -ef|grep redis
      root      6321     1  0 21:36 ?        00:00:02 redis-server 127.0.0.1:6379
      root      6372     1  0 21:36 ?        00:00:02 redis-server 127.0.0.1:6381
      root     10339     1  0 22:03 ?        00:00:01 redis-server 127.0.0.1:6380
      root     11749 31466  0 22:13 pts/3    00:00:00 redis-sentinel *:26379 [sentinel]
      root     11916 26828  0 22:14 pts/0    00:00:00 redis-sentinel *:26380 [sentinel]
      root     12131 12033  0 22:16 pts/1    00:00:00 redis-sentinel *:26381 [sentinel]
      root     12468 12407  0 22:18 pts/2    00:00:00 grep --color=auto redis
      [root@VM-0-4-centos ~]# kill -9 6372
      ---↓↓↓哨兵日志选取其中一台↓↓↓-----------------------------------------------------------------------
      11749:X 10 Aug 2020 22:18:41.165 # +sdown master mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:41.165 # +odown master mymaster 127.0.0.1 6381 #quorum 1/1
      11749:X 10 Aug 2020 22:18:41.165 # +new-epoch 3
      11749:X 10 Aug 2020 22:18:41.165 # +try-failover master mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:41.170 # +vote-for-leader de4d4a52a39c8e4532dca5a91679749aeffbfccd 3
      11749:X 10 Aug 2020 22:18:41.185 # de4d4a52a39c8e4532dca5a91679749aeffbfcce voted for de4d4a52a39c8e4532dca5a91679749aeffbfccd 3
      11749:X 10 Aug 2020 22:18:41.185 # de4d4a52a39c8e4532dca5a91679749aeffbfccc voted for de4d4a52a39c8e4532dca5a91679749aeffbfccd 3
      11749:X 10 Aug 2020 22:18:41.246 # +elected-leader master mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:41.246 # +failover-state-select-slave master mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:41.329 # +selected-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:41.330 * +failover-state-send-slaveof-noone slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:41.406 * +failover-state-wait-promotion slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:42.204 # +promoted-slave slave 127.0.0.1:6380 127.0.0.1 6380 @ mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:42.204 # +failover-state-reconf-slaves master mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:42.265 * +slave-reconf-sent slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:43.250 * +slave-reconf-inprog slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:43.250 * +slave-reconf-done slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:43.305 # +failover-end master mymaster 127.0.0.1 6381
      11749:X 10 Aug 2020 22:18:43.305 # +switch-master mymaster 127.0.0.1 6381 127.0.0.1 6380
      11749:X 10 Aug 2020 22:18:43.305 * +slave slave 127.0.0.1:6379 127.0.0.1 6379 @ mymaster 127.0.0.1 6380
      11749:X 10 Aug 2020 22:18:43.305 * +slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
      11749:X 10 Aug 2020 22:18:46.339 # +sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
      

      如上所示,其中一台发现master失联后,发起全员测试 -> 投票选举 -> 标记下线 -> slave升级为master -> 新master重连其余实例,同单机哨兵模式一致,只在于多了几台哨兵共同测定master是否离线。

      PS:哨兵集群最好为单数台,便于计算出中间值。

      PS:quorum值设定具有一定参考意义,但是无论设定为多少,哨兵集群只会在超过半数哨兵认定master离线时发起主从切换。

    2. 重新连接

      [root@VM-0-4-centos etc]# redis-server redis-6381.conf 
      13449:C 10 Aug 2020 22:25:48.758 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
      13449:C 10 Aug 2020 22:25:48.758 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=13449, just started
      13449:C 10 Aug 2020 22:25:48.758 # Configuration loaded
      -------------哨兵打印6381重连并且转化为slave的日志-----------------------------
      11749:X 10 Aug 2020 22:25:49.418 # -sdown slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
      11749:X 10 Aug 2020 22:25:59.352 * +convert-to-slave slave 127.0.0.1:6381 127.0.0.1 6381 @ mymaster 127.0.0.1 6380
      

    END

    1. 查看到原redis.conf中配置发生改变

      slaveof 127.0.0.1 6380
      变为:
      replicaof 127.0.0.1 6380
      
    2. sentinel.conf文件末尾追加了一部分内容,用于标记当前集群内的master、slave、sentinel信息。

      sentinel leader-epoch mymaster 3
      sentinel known-replica mymaster 127.0.0.1 6379
      sentinel known-replica mymaster 127.0.0.1 6381
      sentinel known-sentinel mymaster 127.0.0.1 26381 de4d4a52a39c8e4532dca5a91679749aeffbfcce
      sentinel known-sentinel mymaster 127.0.0.1 26380 de4d4a52a39c8e4532dca5a91679749aeffbfccc
      sentinel current-epoch 3
      

ddt的用法:根据数据来生成测试用例

标签:循环   def   task   用例   怎么办   inf   res   item   sse   

原文地址:https://www.cnblogs.com/xj-excellent/p/13468771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!