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