设为首页 收藏本站
查看: 986|回复: 0

[经验分享] PostgreSQL启动过程中的那些事七:初始化共享内存和信号十八:shmem中初始化WalSender和WalRecv相关结构

[复制链接]

尚未签到

发表于 2016-11-21 08:17:08 | 显示全部楼层 |阅读模式
  1先上个图,看一下函数调用过程梗概,中间略过部分细节
  
DSC0000.bmp
初始化walsender和walreceiver相关结构方法调用流程图

 

2初始化xlog相关结构
话说main()->…->PostmasterMain()->…->reset_shared() ->CreateSharedMemoryAndSemaphores()>…->WalSndShmemInit(),调用ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"Wal Sender Ctl",如果没有,就在shmemIndex中给"Wal Sender Ctl"分一个HashElement和ShmemIndexEntentry),在其中的Entry中写上"WalSender Ctl"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"WalSender Ctl"相关结构(见下面“Wal Sender CtlWal Receiver Ctl相关结构图”)分配空间,设置entry(在这儿及ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,最后返回WalSndShmemInit(),让WalSndCtlData*类型全局变量WalSndCtl指向所分配内存,初始化WalSndCtlData结构类型的成员值。
接着WalRcvShmemInit()-> ShmemInitStruct(),在其中调用hash_search()在哈希表索引"ShmemIndex"中查找"Wal Receiver Ctl",如果没有,就在shmemIndex中给"Wal Receiver Ctl"分一个HashElement和ShmemIndexEntentry),在其中的Entry中写上"WalReceiver Ctl"。返回ShmemInitStruct(),再调用ShmemAlloc()在共享内存上给"WalReceiver Ctl"相关结构(见下面“Wal Sender CtlWal Receiver Ctl相关结构图”)分配空间,设置entry(在这儿及ShmemIndexEnt类型变量)的成员location指向该空间,size成员记录该空间大小,最后返回WalRcvShmemInit(),让WalRcvData*类型全局变量WalRcv指向所分配内存,初始化WalRcvData结构类型的成员值。
 
相关结构定义和图见下面:
typedef enum WalSndState

{

    WALSNDSTATE_STARTUP = 0,

    WALSNDSTATE_BACKUP,

    WALSNDSTATE_CATCHUP,

    WALSNDSTATE_STREAMING

} WalSndState;

 

/*

 * Each walsenderhas a WalSnd structin shared memory.

 */

typedef struct WalSnd

{

    pid_t      pid;          /* this walsender's process id, or 0 */

    WalSndState state;          /* this walsender's state */

    XLogRecPtr sentPtr;      /* WAL has been sent up to this point */

 

    /*

     * The xloglocations that have been written, flushed, and applied by

     * standby-side. These maybe invalid if the standby-side has not offered

     * values yet.

     */

    XLogRecPtr write;

    XLogRecPtr flush;

    XLogRecPtr apply;

 

    /* Protects shared variables shown above.*/

    slock_t       mutex;

 

    /*

     * Latch used by backendsto wake up this walsenderwhen it has work to

     * do.

     */

    Latch      latch;

 

    /*

     * The priority order ofthe standby managed by this WALSender, as listed

     * insynchronous_standby_names, or 0 if not-listed. Protected by

     * SyncRepLock.

     */

    int        sync_standby_priority;

} WalSnd;

 

extern WalSnd *MyWalSnd;

 

/* There is one WalSndCtl structfor the whole database cluster */

typedef struct

{

    /*

     * Synchronous replicationqueue. Protected by SyncRepLock.

     */

    SHM_QUEUE  SyncRepQueue;

 

    /*

     * Current location of thehead of the queue. All waiters should have a

     * waitLSN that followsthis value. Protected by SyncRepLock.

     */

    XLogRecPtr lsn;

 

    /*

     * Are any sync standbysdefined?  Waiting backendscan'treload the

     * configfilesafely, so WAL writer updates this value as needed.

     * Protected bySyncRepLock.

     */

    bool       sync_standbys_defined;

 

    WalSnd     walsnds[1];       /* VARIABLE LENGTH ARRAY */

} WalSndCtlData;

 

extern WalSndCtlData *WalSndCtl;
 

typedef enum

{

    WALRCV_STOPPED,             /* stopped and mustn't start up again */

    WALRCV_STARTING,         /* launched, but the process hasn't

                             *initialized yet */

    WALRCV_RUNNING,             /* walreceiveris running */

    WALRCV_STOPPING             /* requested to stop, but still running */

} WalRcvState;

 

/* Shared memory area formanagement of walreceiverprocess */

typedef struct

{

    /*

     * PID of currently active walreceiverprocess, its current state and

     * start time (actually,the time at which it was requested to be

     * started).

     */

    pid_t      pid;

    WalRcvState walRcvState;

    pg_time_t  startTime;

 

    /*

     * receiveStart is thefirst byte position that will be received. When

     * startup process startsthe walreceiver, it sets receiveStart to the

     * point where it wants thestreaming to begin.

     */

    XLogRecPtr receiveStart;

 

    /*

     * receivedUpto-1 is thelast byte position that has already been

     * received.  At the first startup of walreceiver,receivedUpto is set to

     * receiveStart. Afterthat, walreceiverupdates this whenever it flushes

     * the received WAL todisk.

     */

    XLogRecPtr receivedUpto;

 

    /*

     * latestChunkStart is thestarting byte position of the current "batch"

     * of received WAL.  It's actually the same as the previous valueof

     * receivedUpto before thelast flush to disk.   Startup process canuse

     * this to detect whetherit's keeping up or not.

     */

    XLogRecPtr latestChunkStart;

 

    /*

     * connection string; isused for walreceiverto connect with the primary.

     */

    char       conninfo[MAXCONNINFO];

 

    slock_t       mutex;        /* locks shared variables shown above */

} WalRcvData;

 

extern WalRcvData *WalRcv;

 
DSC0001.bmp

初始化完Wal Receiver CtlWal Receiver Ctl相关结构的共享内存结构图

  
       为了精简上图,把创建shmem的哈希表索引"ShmemIndex"时创建的HCTL结构删掉了,这个结构的作用是记录创建可扩展哈希表的相关信息,不过这个结构在"ShmemIndex"创建完成后也会由于出了对象作用域而消失。增加了左边灰色底的部分,描述共享内存/shmem里各变量物理布局概览,由下往上,由低地址到高地址。图中黄色的索引项就是本节新增加的索引项。
 
DSC0002.bmp

 

Wal Sender CtlWal Receiver Ctl相关结构图

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-303156-1-1.html 上篇帖子: PostgreSQL启动过程中的那些事七:初始化共享内存和信号十七:shmem中初始化AutoVacuum相关结构 下篇帖子: PostgreSQL启动过程中的那些事七:初始化共享内存和信号十九:shmem中初始化BTree相关结构
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表