溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MONGO集群修改linux主機時間后的影響有哪些

發(fā)布時間:2021-11-09 11:06:51 來源:億速云 閱讀:172 作者:小新 欄目:關(guān)系型數(shù)據(jù)庫

小編給大家分享一下MONGO集群修改linux主機時間后的影響有哪些,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

生產(chǎn)環(huán)境是 一主一從一仲裁3分片的集群,現(xiàn)在發(fā)現(xiàn)其中一個節(jié)點比當(dāng)前時間大了好幾天,后使用NTP將時間往回調(diào)整副本集上。

原來時間是5月3日,當(dāng)前是 4月26日,對此進行了調(diào)整。

[root@cwdtest1 bin]# date

Fri May  3 13:20:31 CST 2019

[root@cwdtest1 bin]# ntpdate -u 10.205.34.171

26 Apr 12:39:23 ntpdate[14568]: step time server 10.205.34.171 offset -607507.747595 sec

[root@cwdtest1 bin]# hwclock --systohc

調(diào)整后當(dāng)前的時間:

[root@cwdtest1 bin]# date

Fri Apr 26 12:39:31 CST 2019

當(dāng)完成調(diào)整時間之后,發(fā)現(xiàn)兩個問題:

1.副本集無法同步新的oplog,由此出現(xiàn)了延遲

shard2:PRIMARY> db.printSlaveReplicationInfo();

source: 10.3.252.231:27002

syncedTo: Fri May 03 2019 13:24:23 GMT+0800 (CST)

8 secs (0 hrs) behind the primary

2. 查看oplog的 tLast 時間比當(dāng)前的大

shard2:PRIMARY> db.getReplicationInfo()

{

"logSizeMB" : 1383.3396482467651,

"usedMB" : 154.49,

"timeDiff" : 17015711,

"timeDiffHours" : 4726.59,

"tFirst" : "Thu Oct 18 2018 14:49:20 GMT+0800 (CST)",

"tLast" : "Fri May 03 2019 13:24:31 GMT+0800 (CST)",

"now" : "Fri Apr 26 2019 13:57:01 GMT+0800 (CST)"

}

shard2:PRIMARY> db.printReplicationInfo()

configured oplog size:   1383.3396482467651MB

log length start to end: 17015711secs (4726.59hrs)

oplog first event time:  Thu Oct 18 2018 14:49:20 GMT+0800 (CST)

oplog last event time:   Fri May 03 2019 13:24:31 GMT+0800 (CST)

now:                     Fri Apr 26 2019 15:46:27 GMT+0800 (CST)

查看db.getReplicationInfo中,我們找出tLast 和now兩個時間是從哪里得到的?

shard2:PRIMARY> db.getReplicationInfo
function () {
        var localdb = this.getSiblingDB("local");
 
        var result = {};
        var oplog;
        var localCollections = localdb.getCollectionNames();
        if (localCollections.indexOf('oplog.rs') >= 0) {
            oplog = 'oplog.rs';
        } else if (localCollections.indexOf('oplog.$main') >= 0) {
            oplog = 'oplog.$main';
        } else {
            result.errmsg = "neither master/slave nor replica set replication detected";
            return result;
        }
 
        var ol = localdb.getCollection(oplog);
        var ol_stats = ol.stats();
        if (ol_stats && ol_stats.maxSize) {
            result.logSizeMB = ol_stats.maxSize / (1024 * 1024);
        } else {
            result.errmsg = "Could not get stats for local." + oplog + " collection. " +
                "collstats returned: " + tojson(ol_stats);
            return result;
        }
 
        result.usedMB = ol_stats.size / (1024 * 1024);
        result.usedMB = Math.ceil(result.usedMB * 100) / 100;
 
        var firstc = ol.find().sort({$natural: 1}).limit(1);
        var lastc = ol.find().sort({$natural: -1}).limit(1);
        if (!firstc.hasNext() || !lastc.hasNext()) {
            result.errmsg =
                "objects not found in local.oplog.$main -- is this a new and empty db instance?";
            result.oplogMainRowCount = ol.count();
            return result;
        }
 
        var first = firstc.next();
        var last = lastc.next();
        var tfirst = first.ts;
        var tlast = last.ts;
 
        if (tfirst && tlast) {
            tfirst = DB.tsToSeconds(tfirst);
            tlast = DB.tsToSeconds(tlast);
            result.timeDiff = tlast - tfirst;
            result.timeDiffHours = Math.round(result.timeDiff / 36) / 100;
            result.tFirst = (new Date(tfirst * 1000)).toString();
            result.tLast = (new Date(tlast * 1000)).toString();
            result.now = Date();
        } else {
            result.errmsg = "ts element not found in oplog objects";
        }
 
        return result;
    }

從以上可以看出:

 var ol = localdb.getCollection(oplog);

var lastc = ol.find().sort({$natural: -1}).limit(1);

  var last = lastc.next();

   var tlast = last.ts;

            result.tLast = (new Date(tlast * 1000)).toString();

            result.now = Date();

tLast 的時間是獲取oplog.rs 集合中最后一條數(shù)據(jù)的ts時間。

Now 的時間是調(diào)用 Date()函數(shù)獲取當(dāng)前時間。

于是,此時我懷疑副本集 無法同步,是因為oplog中存放比當(dāng)前時間大的日志,而當(dāng)調(diào)整時間后新生成的oplog日志記錄并不是最新的,因此副本集在對比時發(fā)現(xiàn)最新的日志一直不變,便無法同步。

大概說下mongodb同步的機制(借鑒網(wǎng)絡(luò)):

1.執(zhí)行寫語句時,在primary上完成寫操作

2.在primary上記錄一條oplog日志,日志中包含一個ts字段,值為寫操作執(zhí)行的時間,比如本例中記為t

3.secondary從primary拉取oplog,獲取到剛才那一次寫操作的日志

4.secondary按獲取到的日志執(zhí)行相應(yīng)的寫操作

5.執(zhí)行完成后,secondary再獲取新的日志,其向primary上拉取oplog的條件為{ts:{$gt:t}}

6.primary此時收到secondary的請求,了解到secondary在請求時間大于t的寫操作日志,所以他知道操作在t之前的日志都已經(jīng)成功執(zhí)行了

于是,我在primary 執(zhí)行一次插入測試,來驗證懷疑。

shard2:PRIMARY> use shtest

switched to db shtest

shard2:PRIMARY> db.coll.insert( {x:3339876})

WriteResult({ "nInserted" : 1 })

查詢主節(jié)點最后一條操作記錄:

rs.debug.getLastOpWritten()

從以上可以看出:

 var ol = localdb.getCollection(oplog);

var lastc = ol.find().sort({$natural: -1}).limit(1);

  var last = lastc.next();

   var tlast = last.ts;

            result.tLast = (new Date(tlast * 1000)).toString();

            result.now = Date();

tLast 的時間是獲取oplog.rs 集合中最后一條數(shù)據(jù)的ts時間。

Now 的時間是調(diào)用 Date()函數(shù)獲取當(dāng)前時間。

于是,此時我懷疑副本集 無法同步,是因為oplog中存放比當(dāng)前時間大的日志,而當(dāng)調(diào)整時間后新生成的oplog日志記錄并不是最新的,因此副本集在對比時發(fā)現(xiàn)最新的日志一直不變,便無法同步。

大概說下mongodb同步的機制(借鑒網(wǎng)絡(luò)):

1.執(zhí)行寫語句時,在primary上完成寫操作

2.在primary上記錄一條oplog日志,日志中包含一個ts字段,值為寫操作執(zhí)行的時間,比如本例中記為t

3.secondary從primary拉取oplog,獲取到剛才那一次寫操作的日志

4.secondary按獲取到的日志執(zhí)行相應(yīng)的寫操作

5.執(zhí)行完成后,secondary再獲取新的日志,其向primary上拉取oplog的條件為{ts:{$gt:t}}

6.primary此時收到secondary的請求,了解到secondary在請求時間大于t的寫操作日志,所以他知道操作在t之前的日志都已經(jīng)成功執(zhí)行了

于是,我在primary 執(zhí)行一次插入測試,來驗證懷疑。

shard2:PRIMARY> use shtest

switched to db shtest

shard2:PRIMARY> db.coll.insert( {x:3339876})

WriteResult({ "nInserted" : 1 })

查詢主節(jié)點最后一條操作記錄:

rs.debug.getLastOpWritten()

MONGO集群修改linux主機時間后的影響有哪些

查詢副本節(jié)點上的最后條操作記錄:

 MONGO集群修改linux主機時間后的影響有哪些

發(fā)現(xiàn)此時數(shù)據(jù)可以同步到副本集上,即說明oplog日志不存在不同步的問題,也就是實際上集群的同步是正常的。

當(dāng)時發(fā)現(xiàn),每條oplog記錄的ts 一直保持著調(diào)整主機時間前的時間,5月3日的時間,且一直不變。關(guān)于原因,就需要進一步分析了。

看完了這篇文章,相信你對“MONGO集群修改linux主機時間后的影響有哪些”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI