How to check secondary is synced now or not

后端 未结 4 648
深忆病人
深忆病人 2021-02-01 04:12

there is the replica with three member (primary,secondary,secondary). Suppose one of secondaries down for a day, after return secondary back to replica how can i find, i

4条回答
  •  臣服心动
    2021-02-01 04:50

    I wrote a little script for mongoDB shell. It shows a diff between optime and optimeDate. You can use it instead of comparing manually replica set members.

    var isMaster = rs.isMaster();
    var me = isMaster.me;
    
    if(!isMaster.ismaster && isMaster.secondary)
    {
        var status = rs.status();
        var master = isMaster.primary;
    
        var masterOptime = 0;
        var masterOptimeDate = 0;
        var myOptime = 0;
        var myOptimeDate = 0;
    
        for(var i = 0 ; i < status.members.length ; i++)
        {
            var member = status.members[i];
            if(member.name == me)
            {
                if(member.stateStr == "SECONDARY") {
                    myOptime = member.optime.getTime();
                    myOptimeDate = member.optimeDate.getTime();
                }
                else
                {
                    print(me + ' is out of sync ' + member.stateStr);
                    break;
                }
            }
            else if(member.name == master)
            {
                masterOptime = member.optime.getTime();
                masterOptimeDate = member.optimeDate.getTime();
            }
    
        }
    
        if(myOptime && myOptimeDate)
        {
            var optimeDiff = masterOptime - myOptime;
            var optimeDateDiff = masterOptimeDate - myOptimeDate;
    
            print('optime diff: ' + optimeDiff);
            print('optimeDate diff: ' + optimeDateDiff);
        }
    
    }
    else
    {
        print(me + ' is not secondary');
    }
    

提交回复
热议问题