How MMAPv1 does Garbage collection

31 views
Skip to first unread message

revit

unread,
Mar 21, 2017, 5:45:06 AM3/21/17
to mongodb-dev
I wonder if and how MMAPv1 SE does Garbage collection of documents?   in particular, how does it know when a document is no longer been refereed to and it is safe to be cleaned? I appropriate pointer to the code that implements it if exists. Thanks

Kevin Adistambha

unread,
Apr 3, 2017, 11:13:32 PM4/3/17
to mongodb-dev

Hi,

I wonder if and how MMAPv1 SE does Garbage collection of documents? in particular, how does it know when a document is no longer been refereed to and it is safe to be cleaned? I appropriate pointer to the code that implements it if exists. Thanks

When deleting a document, MMAPv1 marks it as deleted, and tagged the space occupied by the document as “free space” and thus available for reuse. The relevant code implementing this functionality is located in https://github.com/mongodb/mongo/blob/v3.4/src/mongo/db/storage/mmap_v1/record_store_v1_base.cpp#L430-L491. MMAPv1 maintains a “freelist” of deleted records that it searches when trying to find space for a new (or moved) document. Please see https://github.com/mongodb/mongo/blob/v3.4/src/mongo/db/storage/mmap_v1/record_store_v1_simple.cpp#L148-L194 for details regarding this process.

Regarding your question, could you elaborate on what you are trying to achieve? If your goal is to reclaim space occupied by the MMAPv1 data files, the best method is to perform an initial sync in a rolling fashion across the whole replica set (i.e. take out a Secondary, purge its dbpath contents, reattach it to the replica set, and wait until it finishes its initial sync. Do this for all Secondaries before finally stepping down the Primary and perform the same operation on the ex-Primary).

Best regards,
Kevin

Reply all
Reply to author
Forward
0 new messages