前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【redis源码学习】从源码角度看主从复制(1):前期提要

【redis源码学习】从源码角度看主从复制(1):前期提要

作者头像
看、未来
发布2022-01-10 09:51:38
2380
发布2022-01-10 09:51:38
举报
文章被收录于专栏:CSDN搜“看,未来”

文章目录

主从复制概述

关于主从复制的概述半年前写过,篇实践性,有兴趣的小伙伴可以看一下:

【redis】跟我一起动手玩玩redis主从复制和哨兵模式

一般来说,主从、哨兵、集群,三个我都放一起讲的,所以我再放一下关于集群的实践篇吧:

【redis】闲得无聊,来聊聊当下爆火的 redis集群,顺便搭一个玩玩呗

不过源码入手的话实在是庞大,光一块儿就要拆成好几篇来写。

主从复制机制主要有以下作用:

代码语言:javascript
复制
1、数据冗余
2、读写分离,主写从读
3、故障恢复,主机挂了哨兵马上扶持从机上位

主从复制流程概述:

代码语言:javascript
复制
1、握手:主从连接成功之后,从节点需要向主节点“自我介绍”
2、同步:连接之后,需要同步数据,使数据达到一致
3、复制:主节点运行阶段将执行的写命令传播给从节点,使用的是异步复制,主节点默认发出即收到,可以提高性能,但是可能在短期内主从数据不一致

同步机制:

代码语言:javascript
复制
全量同步:简单交涉之后,主节点打包RDB数据发送给从节点,一般用于第一次同步数据。
部分同步:主节点将复制积压区中offset偏移量之后的命令发送给从节点,常用于主从断开重连之后同步数据。

主从复制使用结构

代码语言:javascript
复制
struct redisServer {
	/* Replication (master) */
    char replid[CONFIG_RUN_ID_SIZE+1];  // Redis服务器的运行ID,随机字符
    char replid2[CONFIG_RUN_ID_SIZE+1]; // 从主服务器中复制出来的ID
    long long master_repl_offset;   /* My current replication offset */
    long long second_replid_offset; /* Accept offsets up to this for replid2. */
    int slaveseldb;                 /* Last SELECTed DB in replication output */
    int repl_ping_slave_period;     // 发送心跳包的周期
    char *repl_backlog;             // 复制缓冲区
    long long repl_backlog_size;    // 复制缓冲区中第一个字节的偏移量
    long long repl_backlog_histlen; // 缓冲区中存储的数据真实长度
    long long repl_backlog_idx;     // 复制缓冲区中最后一个字节的位置
    long long repl_backlog_off;     /* Replication "master offset" of first
                                       byte in the replication backlog buffer.*/
    time_t repl_backlog_time_limit; /* Time without slaves after the backlog
                                       gets released. */
    time_t repl_no_slaves_since;    /* We have no slaves since that time.
                                       Only valid if server.slaves len is 0. */
    int repl_min_slaves_to_write;   /* Min number of slaves to write. */
    int repl_min_slaves_max_lag;    /* Max lag of <count> slaves to write. */
    int repl_good_slaves_count;     // 当前有效从服务器数量
    int repl_diskless_sync;         /* Master send RDB to slaves sockets directly. */
    int repl_diskless_load;         /* Slave parse RDB directly from the socket.
                                     * see REPL_DISKLESS_LOAD_* enum */
    int repl_diskless_sync_delay;   /* Delay to start a diskless repl BGSAVE. */
    
    /* Replication (slave) */
    char *masteruser;               /* AUTH with this user and masterauth with master */
    char *masterauth;               /* AUTH with this password with master */
    char *masterhost;               /* Hostname of master */
    int masterport;                 /* Port of master */
    int repl_timeout;               /* Timeout after N seconds of master idle */
    client *master;     // 连接成功后,主从服务器互为客户端
    client *cached_master; /* Cached master to be reused for PSYNC. */
    int repl_syncio_timeout; /* Timeout for synchronous I/O calls */
    int repl_state;          /* Replication status if the instance is a slave */
    off_t repl_transfer_size; /* Size of RDB to read from master during sync. */
    off_t repl_transfer_read; /* Amount of RDB read from master during sync. */
    off_t repl_transfer_last_fsync_off; /* Offset when we fsync-ed last time. */
    connection *repl_transfer_s;     /* Slave -> Master SYNC connection */
    int repl_transfer_fd;    /* Slave -> Master SYNC temp file descriptor */
    char *repl_transfer_tmpfile; /* Slave-> master SYNC temp file name */
    time_t repl_transfer_lastio; /* Unix time of the latest read, for timeout */
    int repl_serve_stale_data; // 连接断开之后,是否继续处理数据
    int repl_slave_ro;          // 从机是否被设置为只读
    int repl_slave_ignore_maxmemory;    /* If true slaves do not evict. */
    time_t repl_down_since; /* Unix time at which link with master went down */
    int repl_disable_tcp_nodelay;   /* Disable TCP_NODELAY after SYNC? */
    int slave_priority;             /* Reported in INFO and used by Sentinel. */
    int slave_announce_port;        /* Give the master this listening port. */
    char *slave_announce_ip;        /* Give the master this ip address. */
    /* The following two fields is where we store master PSYNC replid/offset
     * while the PSYNC is in progress. At the end we'll copy the fields into
     * the server->master client structure. */
    char master_replid[CONFIG_RUN_ID_SIZE+1];  /* Master PSYNC runid. */
    long long master_initial_offset;           /* Master PSYNC offset. */
    int repl_slave_lazy_flush;          /* Lazy FLUSHALL before loading DB? */
   
    /* Replication script cache. */
    dict *repl_scriptcache_dict;        /* SHA1 all slaves are aware of. */
    list *repl_scriptcache_fifo;        /* First in, first out LRU eviction. */
    unsigned int repl_scriptcache_size; /* Max number of elements. */
    /* Synchronous replication. */
    list *clients_waiting_acks;         /* Clients waiting in WAIT command. */
    int get_ack_from_slaves;            /* If true we send REPLCONF GETACK. */
};
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/12/29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 主从复制概述
  • 主从复制使用结构
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档