eG Monitoring
 

Measures reported by MongoLockTest

MongoDB allows multiple clients to read and write the same data. In order to ensure consist ency, it uses locking to prevent multiple clients from modifying the same piece of data simultaneously.

MongoDB uses reader-writer locks that allow concurrent readers shared access to a resource, such as a database or collection, but in MMAPv1, give exclusive access to a single write operation.

In addition to a shared (S) locking mode for reads and an exclusive (X) locking mode for write operations, intent shared (IS) and intent exclusive (IX) modes indicate an intent to read or write a resource using a finer granularity lock. When locking at a certain granularity, all higher levels are locked using an intent lock.

For example, when locking a collection for writing (using mode X), both the corresponding database lock and the global lock must be locked in intent exclusive (IX) mode. A single database can simultaneously be locked in IS and IX mode, but an exclusive (X) lock cannot coexist with any other modes, and a shared (S) lock can only coexist with intent shared (IS) locks.

Locks are fair, with reads and writes being queued in order. However, to optimize throughput, when one request is granted, all other compatible requests will be granted at the same time, potentially releasing them before a conflicting request. For example, consider a case in which an X lock was just released, and in which the conflict queue contains the following items:

IS -> IS -> X -> X -> S -> IS

In strict first-in, first-out (FIFO) ordering, only the first two IS modes would be granted. Instead MongoDB will actually grant all IS and S modes, and once they all drain, it will grant X, even if new IS or S requests have been queued in the meantime.

Sometimes, in an attempt to release locks for compatible requests, MongoDB may end up increasing the lock wait times for certain incompatible requests. Some other times, certain long-running operations can cause the length of the queue to increase along with the waiting time for locks. Long lock wait times can adversely impact application performance. This is why, administrators will have to keep an eye on the locking activity on a MongoDB server, determine whether/not requests are waiting too long for locks, and also identify the types of locks (S, X, IS, IX, etc.) these requests are waiting for. This will enable administrators to quickly diagnose why certain lock types are taking longer to be released, and resolve the bottleneck. The MongoLockTest test helps administrators achieve this.

This test auto-discovers the different lock types/lock modes that exist currently on the target server. For each lock type/mode, the test reports the rate at which locks of that type were acquired, the rate at which requests waited for locks of that type, and the average wait time for the locks. In the process, the test promptly alerts administrators to prolonged waiting time for locks and also pinpoints the exact lock types for which requests are waiting too long. With this information, administrators can easily troubleshoot long wait times.

Outputs of the test : One set of results for each lock type / lock mode.

The measures made by this test are as follows:

Measurement Description Measurement Unit Interpretation
Locks Indicates the rate at which a lock of this type was acquired. Locks/Sec  
Lock_waits Indicates the rate at which requests were waiting to acquire a lock of this type. Waits/Sec A consistent increase in the value for this measure could indicate that locks are not being released in a timely manner.
Lock_wait_time Indicates the time period for which requests were waiting to acquire a lock of this type, during the last measurement period. Seconds  
Avg_lockwait_time Indicates the average time for which requests were waiting to acquire a lock of this type. Seconds A high average lock wait time may mean sessions are having to wait for a long time to acquire locks on objects.

A high value may indicate one of the following:

  • Too many transactions happening

  • Locked resources not being released properly

  • Locks are being held by long-running operations