MONGODB: UNDERSTANDING OPLOG

The following table clearly explains what each Oplog field denotes.


FAliasDetailsOperationsValues
tsTimeSpaceHolds the “Time of the operation” & “incremental id”. For all bulk (insert/remove/update) operations “Time of the operation” remains same and id changes to an incremental value. Default value of the id is 1For All Operations(Time of the operation, incremental id)
h hasha unique ID for each operationFor All OperationsNumberLong(“7202552052460497158”)
tNANAFor All OperationsNumberLong(1)
vversion#versionFor All Operations2
opOperation TypeWrite operations (import/Insert/Delete/Update/)Write Operationsi/d/u
Commands that affect database (Create, Drop: Database/collection)Command OperationsC
Informational OperationNo-OperationN
nsNameSpaceThe database and collection affected by the operationWrite Operations(db_name.collection_name)
Command Operations(db_name.$cmd))
No-OperationBlank
OOperationInfo/Data of the OperationsInitiate Replica Set{“msg” : “initiate set”}
On Electing as Primary{“msg” : “new primary”}
Add Node1 as Secondary{“msg” : “Reconfig Set”, “version”:2}
Add Node2 as Secondary{“msg” : “Reconfig Set”, “version”:3}
Remove Node1 from RepSet{“msg” : “Reconfig Set”, “version”:4}
Create Collection{ “create” : “posts” }
Drop Collection{ “drop” : “posts” }
Drop Database{ “dropDatabase” : 1 }
Insert/Delete a Document{“_id”:1, “Name”:”ABC”}
Set () for update{ “$set” : { “Name” : “MyTest” } }
O2Update OperationHolds the _id of the document to be updated



Update Document
Oplog Default Size

When you start a replica set member for the first time, MongoDB creates an oplog of a default size.

- Default oplog size in Windows/Unix :


        5% of physical memory/free disk space


       50MB to 50GB of physical memory


       990MB to 50GB of free disk space



- Default oplog size in OS X 64 bit :


       192MB of physical memory/free disk space


Change Oplog size

- Đối với WiredTiger Storage Engine thì để thực hiện thay đổi oplog size cho mỗi member trong replicate 
  thì chỉ cần sử dụng replSetResizeOplog command. Bắt đầu thực hiện trên secondary trước rồi sau đó mới 
  thực hiên trên primary.
  Các bước thực hiện như sau :
     - 1. Connect to replica set member
     - 2. Verify the current size oplog size.
            use local
            db.oplog.rs.status().maxSize
     - 3. change the oplog size 
           db.adminCommand({replSetResizeOplog: 1, size: 16000})      (đơn vị byte)
   Note: Nếu giàm kích thước oplog size xuống thì kích thước dung lượng dư thừa trên đĩa sẽ vẫn không được thu hồi 
             đi, nên cần phải thực hiện  compact trên  oplog để lấy lại dung lương đĩa.
             commnad:
                     use local 
                     db.runCommand({"compact" : "oplog.rs"})

Why the the big difference between storageSize and dataSize:

Nguyên nhân dẫn đến vấn đề này là do sự phân mảnh ( fragmentation) của data file. MongoDB cố gắng sử dụng free space
giữa các fragmented data khi có thể và nó không có giải phóng ra chó HDH. Tuy nhiên trong WiredTiger thì StỏageSize có thể nhỏ hơn dataSize nếu compression is enabled.
- Để có thể giải phóng nhưng dung lương này thì cần phải thực hiện run compact or repair operation 

# Repair MongoDB

   MongoDB repair operation repairs all errors & inconsistencies in data storage. Nếu journaling is enable thì không cần đến repair. server uses journal to get into clean state automatically after restart. The job requires free disk space equal to the size of your current data set plus 2 gigabytes.

# Compacting MongoDB

MongoDB compact operation rewrite all document and index in a collection to contiguous blocks of disk space.
Vì compact sẽ thực hiện block all other operations trên collection đó, nên nếu trường hơp mà replication thì cần thực hiên trên secondary trước sau đó mới đên primary.

Nhận xét

Bài đăng phổ biến từ blog này

ActiveMQ 5.x

Redo and undo Log in MySQL transaction

[Kubernetes Series] - Bài 19 - Adding custom resource to Kubernetes