MONGO集群修改linux主機時間后的影響有哪些-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)建站從2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務公司,擁有項目成都網(wǎng)站設計、成都網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元興文做網(wǎng)站,已為上家服務,為興文各地企業(yè)和個人服務,聯(lián)系電話:18980820575

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

原來時間是5月3日,當前是 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)整后當前的時間:

[root@cwdtest1 bin]# date

Fri Apr 26 12:39:31 CST 2019

當完成調(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 時間比當前的大

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ù)獲取當前時間。

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

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

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

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

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

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

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ù)獲取當前時間。

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

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

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

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

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

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

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日志不存在不同步的問題,也就是實際上集群的同步是正常的。

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

看完了這篇文章,相信你對“MONGO集群修改linux主機時間后的影響有哪些”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設公司行業(yè)資訊頻道,感謝各位的閱讀!

文章題目:MONGO集群修改linux主機時間后的影響有哪些-創(chuàng)新互聯(lián)
標題路徑:http://bm7419.com/article28/hcsjp.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設網(wǎng)站營銷、搜索引擎優(yōu)化品牌網(wǎng)站建設、面包屑導航、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設