复制集:同步初始化

预备知识:Oplog

Oplog是一个特殊的集合,对于Primary的每一项操作,都会在oplog中写入一条对应的记录。它位于local库中,是一个capped collection,当达到collection的最大大小后,新的操作记录会覆盖就的记录,因此,你需要保证oplog的大小满足应用的要求。

 

同步过程:

1、选择同步目标,同时在local.me中标识自己,并清除已存在的数据,保证自己是完全“纯洁”的以开始复制。

Mon Jan 30 11:09:18 [rsSync] replSet initial sync pending
Mon Jan 30 11:09:18 [rsSync] replSet syncing to: server-1:27017

Mon Jan 30 11:09:18 [rsSync] build index local.me { _id: 1 }
Mon Jan 30 11:09:18 [rsSync] build index done 0 records 0 secs
Mon Jan 30 11:09:18 [rsSync] replSet initial sync drop all databases
Mon Jan 30 11:09:18 [rsSync] dropAllDatabasesExceptLocal 1

2、开始拷贝数据,这是最耗时的一步

Mon Jan 30 11:09:18 [rsSync] replSet initial sync clone all databases
Mon Jan 30 11:09:18 [rsSync] replSet initial sync cloning db: db1
Mon Jan 30 11:09:18 [FileAllocator] allocating new datafile /data/db/db1.ns,
filling with zeroes...

3、在2步进行期间,Primary上可能正进行着其它的操作,这些操作将写入oplog。

Mon Jan 30 15:38:36 [rsSync] oplog sync 1 of 3
Mon Jan 30 15:38:36 [rsBackgroundSync] replSet syncing to: server-1:27017
Mon Jan 30 15:38:37 [rsSyncNotifier] replset setting oplog notifier to
server-1:27017
Mon Jan 30 15:38:37 [repl writer worker 2] replication update of non-mod
failed:
{ ts: Timestamp 1352215827000|17, h: -5618036261007523082, v: 2, op: "u",
ns: "db1.someColl", o2: { _id: ObjectId('50992a2a7852201e750012b7') },
o: { $set: { count.0: 2, count.1: 0 } } }
Mon Jan 30 15:38:37 [repl writer worker 2] replication info
adding missing object
Mon Jan 30 15:38:37 [repl writer worker 2] replication missing object
not found on source. presumably deleted later in oplog

这些操作导致的更改也需要同步。这称为oplog同步的第一阶段,oplog sync 1 of 3。

4、oplog同步的第二阶段,同步在3步过程中发生的更改,这阶段的改变通常不太大,进展较快。

Mon Jan 30 15:39:41 [rsSync] oplog sync 2 of 3

5、经过上面4步之后,数据基本赶上了Primary,开始建立索引

Mon Jan 30 15:39:43 [rsSync] replSet initial sync building indexes
Mon Jan 30 15:39:43 [rsSync] replSet initial sync cloning indexes for : db1
Mon Jan 30 15:39:43 [rsSync] build index db.allObjects { someColl: 1 }

Mon Jan 30 15:39:44 [rsSync] build index done. scanned 209844 total records.
1.96 secs

视乎数据量情况,这一步可能也是一个耗时的过程。

6、在创建索引的过程中,在Primary上也在发生着一些操作,此步将同步这些操作,这是oplog同步的第三个阶段,oplog sync 3 of 3

Tue Nov 6 16:05:59 [rsSync] oplog sync 3 of 3

7、经过上述6个步骤,数据同步完成,可以作为一个Secondary启动了

Mon Jan 30 16:07:52 [rsSync] replSet initial sync done
Mon Jan 30 16:07:52 [rsSync] replSet syncing to: server-1:27017
Mon Jan 30 16:07:52 [rsSync] replSet SECONDARY

参考:

《MongoDB:The Definitive Guide,2nd Edition》P188

评论

© 听雨残荷 | Powered by LOFTER