Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The MongoDB Magical Mystery Tour: The Server Internals - Paul Pederson, 10gen

mongodb
January 03, 2012

The MongoDB Magical Mystery Tour: The Server Internals - Paul Pederson, 10gen

MongoSV 2011

This is an overview of the MongoDB architecture: the storage engine for bson and btrees, the query evaluator, the replication design, and the sharding design. The talk is more broad than deep, and it is intended to give an effective overview of the mongodb design decisions and implementation strategies.

mongodb

January 03, 2012
Tweet

More Decks by mongodb

Other Decks in Technology

Transcript

  1. Paul Pedersen, Deputy CTO [email protected] Mongo SV (M3, 10am) December

    9, 2011 Magical Mystery Tour (anatomy of mongodb) Tuesday, January 3, 2012
  2. mongodb design goals • rich data model • general-purpose DBMS

    • eventual consistency • web scale • durability • availability Tuesday, January 3, 2012
  3. Codebase structure bson client db tools shell s util rich

    data model web scale query, index, durability, availability C++ language driver networking support, misc Tuesday, January 3, 2012
  4. BSON • Stands for “Binary(J)SON” • rich data model •

    more compact • suitable for serialization and storage • roadmap: further compression (keyname dictionary) Tuesday, January 3, 2012
  5. bson client db tools shell s util enum BSONType {

    MinKey=-1, // smaller than all other types EOO=0, // end of object NumberDouble=1, // double precision floating point (53 bit int) String=2, // character string, stored in utf8 Object=3, // an embedded object Array=4, // an embedded array BinData=5, // binary data Undefined=6, // Undefined type jstOID=7, // ObjectId Bool=8, // boolean type Date=9, // date type jstNULL=10, // null type RegEx=11, // regular expression, a pattern with options Symbol=14, // a programming language symbol CodeWScope=15, // javascript code NumberInt = 16, // 32 bit signed integer Timestamp = 17, // updated to Date value next OpTime on insert NumberLong = 18, // 64 bit integer JSTypeMax=18, // max type that is not MaxKey MaxKey=127 // larger than all other types }; see: http://bsonspec.org Tuesday, January 3, 2012
  6. bson client db tools shell s util bson-inl.h - BSONObj

    inline methods, (e.g.) operator<<, toString bson.h - standalone bson header for when not using MongoDB bson_db.h - BSON-related methods required by MongoDB server bsonelement.h - BSONElement declarations and implementations bsonmisc.h - BSON{SizeTracker,ObjBuilderValueStream,..} bsonobj.h - JSON-style object in a binary representation bsonobjbuilder - Utility for creating a BSONObj bsonobjiterator - iterator for a BSONObj (next() element of obj) bsontypes - BSON type enum oid - bson object id, 12 bytes, hopefully unique ordering - BSON key pattern (from query) Source file inventory Tuesday, January 3, 2012
  7. C++ client • http client used internally • includes the

    C++ language driver • gridfs - for storing large files • distributed lock Tuesday, January 3, 2012
  8. bson client db tools shell s util connpool - mongod

    connection pool constants - result flag enum dbclient - mongodb interface dbclient_rs - connect to a replica set of servers distlock - configdb backed synchronizing of system-wide tasks gridfs - for storing large file objects in mongodb (chunk them) model - base class for mongodb serializable objects mongo_client_lib - mongo C++ driver parallel - runs a query in parallel across N mongod instances syncclusterconnection - connection to cluster of servers (as one) Source file inventory Tuesday, January 3, 2012
  9. Database server core • database main() • file management, extent

    allocation • query processing / query optimization • BTree indexes • journaling • oplog replication • capped collections Tuesday, January 3, 2012
  10. bson client db tools shell s util cap - capped

    collection class: three extents, circular buffer cloner - database copier compact - defragmentation algorithm: accumulate runs, extern sort database - allocExtent(), mongod file manager: test.0, test.1,etc db - mongod main(), all command line options diskloc - fileno:offset Source file inventory Tuesday, January 3, 2012
  11. bson client db tools shell s util dbcommands - class

    CmdResetError class CmdGetLastError class CmdGetPrevError class CmdDropDatabase class CmdRepairDatabase class CmdProfile class CmdServerStatus class CmdGetOpTime class Cmd class CmdDiagLogging class CmdDrop class CmdCount class CmdCreate class CmdDropIndexes class CmdReIndex class CmdListDatabases class CmdCloseAllDatabases class CmdFileMD5 class CmdDatasize class CollectionStats class DBStats class CmdCloneCollectionAsCapped class CmdConvertToCapped class CmdWhatsMyUri class DBHashCmd class CapTrunc Source file inventory Tuesday, January 3, 2012
  12. bson client db tools shell s util dur - ‘write

    intensions’, journal before write dur_commitjob - journal group commit object dur_journal - manage rotating set of files j._0,j._1,j._2 dur_journalformat - header, sectheader,entry,footer dur_journalimpl - journal inplementation class dur_preplogbuffer - journal output buffer, write O_DIRECT dur_recover - reboot / recovery class from journal dur_stats - journaledBytes, commitsInWriteLock, etc. dur_writedatatofiles - once journaled, copy to unprotected mmf durop - journaled operations, not basic writes Source file inventory Tuesday, January 3, 2012
  13. bson client db tools shell s util index - memory

    mapped (btree) index interface indexkey - index plugin design instance - diagnostic logging, dbResponse, assembleResponse matcher - match BSON objects against query pattern db.c.find( {x:{$gte:4,$lte:7} ) db.c.find( {x:/^a/} ) db.c.find( {x:{$in:[3,6]}} ) db.c.find( {x:6,y:’c’} ) matcher_covered - match covered index items mongommf - memory mapped file abstraction layer mongomutex - singleton, (i.e.) dbMutex, uses RWLock Source file inventory Tuesday, January 3, 2012
  14. bson client db tools shell s util namespace - NamespaceDetails:

    collection header nonce - for security (not expect to recur) oplog - capped collection (i,u,d,cmd,nop) replication log oplogreader - tailing the oplog collection pdfile - data file / memory manager, ‘Extent’ declaration projection - (indoc, projection spec) => outdoc queryoptimizer - query evaluation logic queryutil - struct FieldBound struct FieldInterval class FieldRange class FieldRangeSet class FieldRangeSetPair class FieldRangeVector class FieldRangeVectorIterator class OrRangeGenerator Source file inventory Tuesday, January 3, 2012
  15. bson client db tools shell s util repl - replication

    support: ReplSource uses OplogReader repl_block - bool opReplicatedEnough(op, int w) scanandorder - ScanAndOrder(int startFrom, int limit, BSONObj order, const FieldRangeSet &frs) security - none/read/write client authorizations taskqueue - producers/consumers queue, we expect consumers more often outside critical sections Source file inventory Tuesday, January 3, 2012
  16. Web scale • select a shard key, get key range

    partitioning • logical, not physical chunks • chunk migration / rebalancing • multi-datacenter partitioning Tuesday, January 3, 2012
  17. bson client db tools shell s util balance - background

    chunk balancer thread, uses ‘rounds’, one per mongos, but only one active at a time balancer_policy - compute a recommended chunk to move chunk - {min, max, shard}, manages split, move client - server side of connection to mongos Source file inventory Tuesday, January 3, 2012
  18. bson client db tools shell s util commands_admin - class

    GridAdminCmd class NetStatCmd class FlushRouterConfigCmd class ServerStatusCmd class FsyncCommand class MoveDatabasePrimaryCmd class EnableShardingCmd class ShardCollectionCmd class GetShardVersion class SplitCollectionCmd class MoveChunkCmd class ListShardsCmd class AddShard class RemoveShardCmd class IsDbGridCmd class CmdIsMaster class CmdWhatsMyUri class CmdShardingGetPrevError class CmdShardingGetLastError class CmdShardingResetError class CmdListDatabases class CmdCloseAllDatabases class CmdReplSetGetStatus Source file inventory Tuesday, January 3, 2012
  19. bson client db tools shell s util commands_public - class

    PublicGridCommand class RunOnAllShardsCommand class AllShardsCollectionCommand class DropIndexesCmd class ReIndexCmd class ProfileCmd class ValidateCmd class RepairDatabaseCmd class DBStatsCmd class DropCmd class DropDBCmd class RenameCollectionCmd class CopyDBCmd class CountCmd class CollectionStats class FindAndModifyCmd class DataSizeCmd class ConvertToCappedCmd class GroupCmd class DistinctCmd class FileMD5Cmd class Geo2dFindNearCmd class MRCmd class ApplyOpsCmd class CompactCmd class PipelineCommand Source file inventory Tuesday, January 3, 2012
  20. bson client db tools shell s util d_chunk_manager - controls

    boundaries of all chunks for a given collection on one shard d_logic - shard version, maps ns to chunk manager d_migrate - chunk migration d_split - chunk splitting d_state - chunk state d_writeback - queue of returned ops, due to chunk migration request - mongos request object server - processRequest(Message& m, MessagingPort& p) Source file inventory Tuesday, January 3, 2012
  21. bson client db tools shell s util shard - (mongod)*

    replica set: may contain multiple chunks shard_version - shard versioning shardconnection - shardkey - key pattern for shard key (like index key pattern) strategy - shard query evaluation strategy base class: strategy_shard - query across shards strategy_single - query one shard writeback_listener - reapply writes to chunks that moved Source file inventory Tuesday, January 3, 2012