KEMBAR78
Concurrency Control in MongoDB 3.0 | PPTX
Concurrency Control in MongoDB 3.0
Kaloian Manassiev
Senior Engineer at MongoDB
kaloianm@mongodb.com
@kaloianm
2
Audience
• Operations engineers
• Application developers
• Third-party storage engine developers
• Anybody who is curious
3
What is concurrency control?
4
What is concurrency control?
• Locking
• Data consistency
$100 + $100 = $200 (not $100 )
• MVCC
5
Collection Collection
6
Collection Collection
Storage Engine API
7
What you will learn
• Top level concurrency control
–Instance, database and collection
• How top level cooperates with the
storage engine
8
What you will NOT learn
• WiredTiger internals
–Separate session on this
9
Talk outline
• MongoDB concurrency control
• Multiple-granularity locking
• WiredTiger vs MMAP V1
• Questions
10
MongoDB 2.0 (and before)
THE
TOP LOCK
R W
R OK NO
W NO NO
11
MongoDB 2.2
Lock per database
db1.coll.insert({a:1}) db4.coll.find({c:5})
12
MongoDB 2.2
THE
TOP LOCK
+ intents
13
TOP
db1.coll.insert({a:1}) db4.coll.find({c:5})
IX IS
X S
14
TOP
db.coll.insert({a:1}) db.coll.find({c:5})
IX IS
X S
15
Why intents?
• Get rid of the global lock?
• Use read (shared) lock?
16
TOP
db1.coll.insert({a:1}) db4.coll.find({c:5})
S S
X S
17
TOP
db1.coll.insert({a:1}) db.fsyncLock({lock:1})
S S
X
18
TOP
db1.coll.insert({a:1}) db.fsyncLock({lock:1})
IX S
X
19
TOP
db1.coll.insert({a:1}) db.fsyncLock({lock:1})
IX S
XS S S S
20
TOP
db.fsyncLock({lock:1})
S
S S S S
db1.coll.find({a:1})
IS
S
21
Intents
• “Intention” to access one or more
children of an item
–Compatible with other intents
–Need to acquire lock further down
–No overhead
22
Locks
• “Lock” an item for particular access
–Item being locked
–All items below it
23
Intents compatibility
IS IX S X
IS OK OK OK NO
IX OK OK NO NO
S OK NO OK NO
X NO NO NO NO
24
MongoDB 3.0
TOP
25
Multiple-granularity locking
TOP
26
Lock Manager
• Ensures the locking protocol is
obeyed
• Very low overhead
• Testable
27
Lock Manager
• Improved fairness
• Support for locking policies
• Lock statistics gathering
28
TOP
db.coll1.update({a:1}) db.coll1.update({a:4})
IX IX
IX IX
IX IX
X X
29
Storage Engine API
TOP
30
Storage Engine API
TOP
Storage Engine API
31
TOP
db.coll1.update({a:1}) db.coll1.update({a:4})
IX IX
IX IX
IX IX
WiredTiger MVCC
32
TOP
db.coll1.update({a:1}) db.coll1.drop()
IX IX
IX X
IX
WiredTiger MVCC
33
How does MMAP V1 work?
• Supports collection concurrency
• Takes lock on the collection
–Instead of intent
34
TOP
db.coll1.update({a:1}) db.coll2.update({a:4})
IX IX
IX IX
X X
MMAP V1
35
TOP
db.coll1.update({a:1}) db.coll1.find({a:4})
IX IS
IX IS
X S
MMAP V1
36
WiredTiger
• Intents: Global, Database, Collection
• Document-level concurrency left to the
storage engine
37
MMAP V1
• Intents: Global, Database
• Locks: Collection
38
Conclusion
• Storage engines have direct control
over concurrency
–Completely decoupled from top-level
locking
–Enabled document-level control
39
Conclusion
• Retrofitted MMAP V1 to use multi-
granularity locks
–Support for collection-level locking
–Got rid of the global lock usage for
journaling
QUESTIONS?
EXTRA SLIDES
42
43
44
45
46
47
48
49

Concurrency Control in MongoDB 3.0

Editor's Notes

  • #2 Thank for coming to MongoDB World Introduce myself and what team I work on Say about what I did before MongoDB (Microsoft SQL Server and then AWS) Say what I worked on in MongoDB 3.0 Explain what I will be talking about
  • #3 Explain for whom is this talk intended and why is it useful for that particular audience. Leave the questions for the end. I will also be available in the Ask the Experts area.
  • #4 What is concurrency control? PAUSE Locks Consistent data (ensuring the correct amount of money is in a bank account) High throughput
  • #5 In database systems, concurrency control is all of those things, but also more. Before I tell you what more means, let’s do a quick recap of what’s the logical composition of mongodb.
  • #6 You all know what are the logical parts of the database, but let’s recap.
  • #7 In MongoDB 3.0: Call out what is the Top part – responsibility of MongoDB Call out what is the Bottom part (actual documents) – responsibility of the storage engine Top part and bottom part talk through the storage engine API PAUSE
  • #8 Now that I have highlighted the parts I’ll be talking about…
  • #10 Let’s get started.
  • #11 CHANGE THE PACE OF TALKING Single “top” lock Protected both the data and the metadata
  • #12 Allowed two operations for two different databases to run in parallel (this is for 2.2, 2.4 and 2.6) Synchronized access to the same database Question: How is the instance protected?
  • #13 Before I formally define what intents are, let me give a couple of examples of how they work.
  • #16 WAIT A FEW SECONDS BEFOR SHOWING THE FIRST BULLET
  • #17 Let’s look at this hypothetical scenario. It kind of looks like it works.
  • #18 Now, consider an operation which wants to have the entire server in read-only mode. PAUSE BEFORE WALKING THROUGH THE ANIMATION QUESTION: Has it made the server read-only? PAUSE
  • #19 Now let’s see how intents help make this correct.
  • #20 At some point the insert operation will complete and the S lock will be granted. PAUSE BEFORE CONTINUING
  • #24 For completeness, this is the compatibility matrix between locks and intents. PAUSE
  • #25 CHANGE PACE OF TALKING This was the world up until MongoDB version 2.6. Now, in 3.0 we wanted to add document-level locking. QUESTION AND PAUSE BEFORE CLICKING: Can you guess what is the first thing we did?
  • #26 This started to look like multi-granularity locking, which is a very well studied and tested concept from database systems. Multiple granularity locking requires locks to be acquired in top to bottom fashion.
  • #27 EXPLAIN THE BULLETS PAUSE A LITTLE BIT: Now let me show you how multi-granularity locking works in the document world. CHANGE OF PACE
  • #28 EXPLAIN THE BULLETS PAUSE A LITTLE BIT: Now let me show you how multi-granularity locking works in the document world. CHANGE OF PACE
  • #29 PAUSE A LITTLE BIT AND WALK THROUGH A STORY STORY: Turned out adapting a data structure which was never intended to be concurrent is extremely difficult. Documents are not as independent as we thought (indexes).
  • #30 Instead we did something even better. We scrapped this document-level locking…
  • #31 … and implemented a storage engine API. PAUSE A BIT TO LET THAT SINK IN
  • #32 Let’s see an example.
  • #33 Many things can go wrong - such as WT accessing a null pointer.
  • #34 This showed how top-level concurrency control works with WiredTiger (and any other storage engine, which supports document-level locking). How does MMAP V1 work? Wait a few seconds.
  • #35 Operation 1 and Operation 2 both access completely different parts of the storage engine. QUESTION AND PAUSE: Protection against the collection being dropped?
  • #36 AT THE END – protection against the database being dropped remains exactly the same.
  • #39 Separated responsibilities for concurrency control between MongoDB and the pluggable storage engine.
  • #41 Now I’ll be happy to take questions.
  • #43 This slide zooms in on what the Locker API is and what is the state of a particular locker object at runtime. This locker is for an operation, which does some kind of write action (insert/update/delete) for a storage engine, which supports document-level locking. The locker has intent on the GLOBAL resource, intent on the ‘sales’ database resource and intent on the ‘orders’ collection resource. If there were another thread doing a read for example, it would locally have similar state, but instead of IX, it would have IS instead. So these two threads never need to synchronize at the catalog level and all the concurrency control is left to the storage engine instead.
  • #44 The lock manager, since it uses named resources and we do not know the names of these resources in advance, is essentially a chained hash table with an entry for each resource containing the resource’s type and name like shown in this diagram. Like any hash table, it contains a fixed (non-growing array) of lock buckets and each bucket is either empty or contains a linked-list of something we call LockHead. There is a single LockBuckets array for the entire instance and there is one LockHead per each lockable resource.
  • #45  ----- Meeting Notes (5/21/15 15:35) ----- REMOVE THESE SLIDES ABOUT IMPLEMENTATION
  • #48 Let’s recap how memory mapped files work.