internal anatomy of an update

26
Update Anatomy The guide to how an update works

Upload: mongodb

Post on 15-Jan-2015

501 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Internal Anatomy of an Update

Update AnatomyThe guide to how an update works

Page 2: Internal Anatomy of an Update

Me = Scott Hernandez● Worked on Morphia (+java), community● Over 3 years with MongoDB● Worked on update re-write in 2.6● Working on replication team now● Lots of experience with many parts

Page 3: Internal Anatomy of an Update

User Components

update({f:1}, {$set: {a: 2}}, {multi:true})● Query● Update Modifiers (or replacement doc)● Options

o multi, upserto writeConcern

Page 4: Internal Anatomy of an Update

New Code (in 2.6)● New update and query code● Replaced all modifier implementations● New validation and error messages● Using new “MutableBSON” document class● New oplog entry generation code

Page 5: Internal Anatomy of an Update

Internal Components● Update Command (-> request)● Update Executor: Runner + Driver● *Modifiers (Inc, Set, Unset, Bit, …)● Validation, Modification, and Replication

Page 6: Internal Anatomy of an Update

Two Paths To Update

Wire Protocol Op

User

Write Commands

Internal Update Request

Page 7: Internal Anatomy of an Update

… don’t forgetFindAndModify ;)

Page 8: Internal Anatomy of an Update

Update Request1. Create

o Validate argso Prepare what we can (reduce lock time)o Record system state (repl, sharding, etc)

2. Send for execution

Page 9: Internal Anatomy of an Update

What is a modifier{$set: {a: 1} }, {$min: {c: “b”} }

● Modifier: “set”, “min”● Field Path: “a”, “c” (+ “b”)● Value: 1, “b”

Page 10: Internal Anatomy of an Update

Implementing a modifierdb/ops/modifier_interface.h:● init ( modExpr, opts ) // validation● prepare ( doc, fieldName, execInfo ) // work● apply ( ) // update document● log ( ) // oplog entry created

Page 11: Internal Anatomy of an Update

Modifier::init● Validate form of update, and args● Parse path● Indicate if positional, and supported

Page 12: Internal Anatomy of an Update

Modifier::prepare● Work with doc, but don’t change it● Validate field, type, operation● Substitute positional path element, if appl.● Do work, stored to the side● Return ExecInfo (no-op, fields affected)

Page 13: Internal Anatomy of an Update

Modifier::apply● Apply prepared work to doc

Page 14: Internal Anatomy of an Update

Modifier::log● Calculate transformation logic for oplog entry● Update idempotent oplog entry

o Seto Unseto Full Replacement

Page 15: Internal Anatomy of an Update

db/ops/modifier_* + tests● add_to_set.h/cpp● bit.h/cpp● compare.h/cpp● current_date.h/cpp● inc.h/cpp● obj_replace.h/cpp● pop.h/cpp

● pull.h/cpp● pull_all.h/cpp● push.h/cpp● rename.h/cpp● set.h/cpp● unset.h/cpp

Page 16: Internal Anatomy of an Update

Update Executor● Finish prep● If no docs + upsert, prepare insert doc● Run driver (mods) against each doc● Ensure, validate, log for replication● Return UpdateResult

Page 17: Internal Anatomy of an Update

Update FlowQuery + Update

Cur

sor ● Init

● Prepare● Apply

{ $inc: {a:1}, { $inc: {b:1}}

● Init● Prepare● Apply

{ $set: {c: {e:1} }}

● Log

● Log

Entry

Oplog

No results, upsert:true

Inse

rt

Entry

Page 18: Internal Anatomy of an Update

Update Flow Notes● Yields based on timing/in-memory● Each document update is atomic● Periodically allows journal to flush, if needed

Page 19: Internal Anatomy of an Update

Questions?Round 1: No, then we have more slides

Page 20: Internal Anatomy of an Update

2.6 Stuff● New modifiers● Improvements● Behavioral Changes

Page 21: Internal Anatomy of an Update

New Modifiers● Mul(tiply) existing field by constant: $mul{a:1} + {$mul:{a:10}} -> {a:10}● Min/Max of existing field w/constant: $min/$max{a:1} + {$min:{a:100}} -> {a:1} (no-op)● Store current date/ts in a field: $currentDate{} + {$currentDate:{ dt: true}} -> {dt: new Date()}

Page 22: Internal Anatomy of an Update

Improvements● $push

o $sort elements (works with numbers/strings/etc)o $sort and/or $slice (doesn’t require $sort for $slice)o $slice off either end (no middle yet)o $position lets you specify where in the arrayo $each, $sort, $slice no longer order dep.

● $bito xor support

Page 23: Internal Anatomy of an Update

Improvements● Error messages with context

o The field 'l' must be an array but is of type NumberLong in document {_id: ObjectId('52a0a61672a5f2f771099b54')}

o Cannot apply $inc to a value of non-integral type. {_id: ObjectId('52a0a61672a5f2f771099b54')} has the field 'd' of non-integral type Date

Page 24: Internal Anatomy of an Update

Improvements● No reordering of fields (except: _id is first)● Better building of doc during insert

o update({a:1, b:1}, {$inc:{c:1}}, 0,1) -> {a:1, b:1, c:1} o now uses parsed query tree to get equalities

● Will now return new _id for all types● Accurate tracking of no-ops, modified docs

Page 25: Internal Anatomy of an Update

Improvements● Validation

o Immutable Fields (_id, shard keys)o Storage restrictions (no $field, “.”, _id field types)o DBRef validation

● Can now query + set _id/shard-key fieldso update({_id:1}, {$set:{_id:1, ...}}) // oko update({_id:1}, {_id:2, ...}) // error

● Cleanup bad data ($unset/$rename etc)

Page 26: Internal Anatomy of an Update

Questions?No, well thanks for coming!