Kristina Chodorow's Blog
Kristina Chodorow
Kristina is a software engineer at 10gen. She is a maintainer for the MongoDB Perl and Java drivers, wrote and maintains the PHP driver, and has given talks worldwide on MongoDB.
Homepage: http://www.snailinaturtleneck.com
Posts by Kristina Chodorow
The Comments Conundrum
Feb 2nd

One of the most common questions we get is:
I have a collection of blog posts and each post has an array of comments. How do I get…
…all comments by a given author
…the most recent comments
…the most popular commenters?
And so on. The answer to this has always been “Well, you can’t do that on the server side…” You can either do it on the client side or store comments in their own collection. What you really want is the ability to treat embedded documents like a “real” collection.
The aggregation pipeline gives you this ability by letting you “unwind” arrays into separate documents, then doing whatever else you need to do in subsequent pipeline operators.
For example…

Getting all comments by Serious Cat
Serious Cat’s comments are scattered between post documents, so there wasn’t a good way of querying for just those embedded documents. Now there is.
Let’s assume we want each comment by Serious Cat, along with the title and url of the post Serious Cat was commenting on. So, the steps we need to take are:
- Extract the fields we want (title, url, comments)
- Unwind the comments field: make each comment into a “real” document
- Query our new “comments collection” for “Serious Cat”
Using the aggregation pipeline, this looks like:
> db.runCommand({aggregate: "posts", pipeline: [ { // extract the fields $project: { title : 1, url : 1, comments : 1 } }, { // explode the "comments" array into separate documents $unwind: "$comments" }, { // query like a boss $match: {comments.author : "Serious Cat"} }]})
Now, this works well for something like a blog, where you have human-generated (small) data. If you’ve got gigs of comments to go through, you probably want to filter out as many as possible (e.g., with $match or $limit) before sending it to the “everything-in-memory” parts of the pipeline.
Getting the most recent comments
Let’s assume our site lists the 10 most recent comments across all posts, with links back to the posts they appeared on, e.g.,
- Great post! -Jerry (February 2nd, 2012) from This is a Great Post
- What does batrachophagous mean? -Fred (February 2nd, 2012) from Fun with Crosswords
- Where can I get discount Prada shoes? -Tom (February 1st, 2012) from Rant about Spam
…
To extract these comments from a collection of posts, you could do something like:
> db.runCommand({aggregate: "posts", pipeline: [ { // extract the fields $project: { title : 1, url : 1, comments : 1 } { // explode "comments" array into separate documents $unwind: "$comments" }, { // sort newest first $sort: { "comments.date" : -1 } }, { // get the 10 newest $limit: 10 }]})
Let’s take a moment to look at what $unwind does to a sample document.
Suppose you have a document that looks like this after the $project:
{ "url" : "/blog/spam", "title" : "Rant about Spam", "comments" : [ {text : "Where can I get discount Prada shoes?", ...}, {text : "First!", ...}, {text : "I hate spam, too!", ...}, {text : "I love spam.", ...} ] }
Then, after unwinding the comments field, you’d have:
{ "url" : "/blog/spam", "title" : "Rant about Spam", "comments" : [ {text : "Where can I get discount Prada shoes?", ...}, ] } { "url" : "/blog/spam", "title" : "Rant about Spam", "comments" : [ {text : "First!", ...} ] } { "url" : "/blog/spam", "title" : "Rant about Spam", "comments" : [ {text : "I hate spam, too!", ...} ] }, { "url" : "/blog/spam", "title" : "Rant about Spam", "comments" : [ {text : "I love spam.", ...} ] }
Then we $sort, $limit, and Bob’s your uncle.
![]()
Rank commenters by popularity
Suppose we allow users to upvote comments and we want to see who the most popular commenters are.
The steps we want to take are:
- Project out the fields we need (similar to above)
- Unwind the comments array (similar to above)
- Group by author, taking a count of votes (this will sum up all of the votes for each comment)
- Sort authors to find the most popular commenters
Using the pipeline, this would look like:
> db.runCommand({aggregate: "posts", pipeline: [ { // extract the fields we'll need $project: { title : 1, url : 1, comments : 1 } }, { // explode "comments" array into separate documents $unwind: "$comments" }, { // count up votes by author $group : { _id : "$comments.author", popularity : {$sum : "$comments.votes"} } }, { // sort by the new popular field $sort: { "popularity" : -1 } }]})
As I mentioned before, there are a couple downsides to using the aggregation pipeline: a lot of the pipeline is done in-memory and can be very CPU- and memory-intensive. However, used judiciously, it give you a lot more freedom to mush around your embedded documents.
Hacking Chess: Data Munging
Jan 27th
This is a supplement to the Hacking Chess with the MongoDB Pipeline. This post has instructions for rolling your own data sets from chess games.
Download a collection of chess games you like. I’m using 1132 wins in less than 10 moves, but any of them should work.
These files are in a format called portable game notation (.PGN), which is a human-readable notation for chess games. For example, the first game in TEN.PGN (helloooo 80s filenames) looks like:
[Event "?"] [Site "?"] [Date "????.??.??"] [Round "?"] [White "Gedult D"] [Black "Kohn V"] [Result "1-0"] [ECO "B33/09"] 1.e4 c5 2.Nf3 Nc6 3.d4 cxd4 4.Nxd4 Nf6 5.Nc3 e5 6.Ndb5 d6 7.Nd5 Nxd5 8.exd5 Ne7 9.c4 a6 10.Qa4 1-0
This represents a 10-turn win at an unknown event. The “ECO” field shows which opening was used (a Sicilian in the game above).
Unfortunately for us, MongoDB doesn’t import PGNs in their native format, so we’ll need to convert them to JSON. I found a PGN->JSON converter in PHP that did the job here. Scroll down to the “download” section to get the .zip.
It’s one of those zips that vomits its contents into whatever directory you unzip it in, so create a new directory for it.
So far, we have:
$ mkdir chess $ cd chess $ $ ftp ftp://ftp.pitt.edu/group/student-activities/chess/PGN/Collections/ten-pg.zip ./ $ unzip ten-pg.zip $ $ wget http://www.dhtmlgoodies.com/scripts/dhtml-chess/dhtml-chess.zip $ unzip dhtml-chess.zip
Now, create a simple script, say parse.php, to run through the chess matches and output them in JSON, one per line:
<?php require("PgnParser.class.php"); $parser = new PgnParser("/path/to/chess/TEN.PGN"); $total = $parser->getNumberOfGames(); for ($i=0; $i<$total; $i++) { echo $parser->getGameDetailsAsJson($i)."\n"; } ?>
Run parse.php and dump the results into a file:
$ php parse.php > games.jsonNow you’re ready to import games.json.
Hacking Chess with the MongoDB Pipeline
Jan 26th

MongoDB’s new aggegation framework is now available in the nightly build! This post demonstrates some of its capabilities by using it to analyze chess games.
Make sure you have a the “Development Release (Unstable)” nightly running before trying out the stuff in this post. The aggregation framework will be in 2.1.0, but as of this writing it’s only in the nightly build.
First, we need some chess games to analyze. Download games.json, which contains 1132 games that were won in 10 moves or less (crush their soul and do it quick).
You can use mongoimport to import games.json into MongoDB:
$ mongoimport --db chess --collection fast_win games.json connected to: 127.0.0.1 imported 1132 objects
We can take a look at our chess games in the Mongo shell:
> use chess switched to db chess > db.fast_win.count() 1132 > db.fast_win.findOne() { "_id" : ObjectId("4ed3965bf86479436d6f1cd7"), "event" : "?", "site" : "?", "date" : "????.??.??", "round" : "?", "white" : "Gedult D", "black" : "Kohn V", "result" : "1-0", "eco" : "B33/09", "moves" : { "1" : { "white" : { "move" : "e4" }, "black" : { "move" : "c5" } }, "2" : { "white" : { "move" : "Nf3" }, "black" : { "move" : "Nc6" } }, ... "10" : { "white" : { "move" : "Qa4" } } } }
Not exactly the greatest schema, but that’s how the chess format exporter munged it. Regardless, now we can use aggregation pipelines to analyze these games.
Experiment #1: First Mover Advantage
White has a slight advantage in chess because you move first (Wikipedia says it’s a 52%-56% chance of winning). I’d hypothesize that, in a short game, going first matters even more.
Let’s find out.
The “result” field in these docs is “1-0″ if white wins and “0-1″ if black wins. So, we want to divide our docs into two groups based on the “result” field and count how many docs are in each group. Using the aggregation pipeline, this looks like:
> db.runCommand({aggregate : "fast_win", pipeline : [ ... { ... $group : { ... _id : "$result", // group by 'result' field ... numGames : {$sum : 1} // add 1 for every document in the group ... } ... }]}) { "result" : [ { "_id" : "0-1", "numGames" : 435 }, { "_id" : "1-0", "numGames" : 697 } ], "ok" : 1 }
That gives a 62% chance white will win (697 wins/1132 total games). Pretty good (although, of course, this isn’t a very large sample set).

In case you're not familiar with it, a reference chessboard with 1-8, a-h marked.
Experiment #2: Best Starting Move
Given a starting move, what percent of the time will that move lead to victory? This probably depends on whether you’re playing white or black, so we’ll just focus on white’s opening move.
First, we’ll just determine what starting moves white uses with this series of steps:
- project all of white’s first moves (the
moves.1.white.movefield) - group all docs with the same starting move together
- and count how many documents (games) used that move.
These steps look like:
> db.runCommand({aggregate: "fast_win", pipeline: [ ... // '$project' is used to extract all of white's opening moves ... { ... $project : { ... // extract moves.1.white.move into a new field, firstMove ... firstMove : "$moves.1.white.move" ... } ... }, ... // use '$group' to calculate the number of times each move occurred ... { ... $group : { ... _id : "$firstMove", ... numGames : {$sum : 1} ... } ... }]}) { "result" : [ { "_id" : "d3", "numGames" : 2 }, { "_id" : "e4", "numGames" : 696 }, { "_id" : "b4", "numGames" : 17 }, { "_id" : "g3", "numGames" : 3 }, { "_id" : "e3", "numGames" : 2 }, { "_id" : "c4", "numGames" : 36 }, { "_id" : "b3", "numGames" : 4 }, { "_id" : "g4", "numGames" : 11 }, { "_id" : "h4", "numGames" : 1 }, { "_id" : "Nf3", "numGames" : 37 }, { "_id" : "f3", "numGames" : 1 }, { "_id" : "f4", "numGames" : 25 }, { "_id" : "Nc3", "numGames" : 14 }, { "_id" : "d4", "numGames" : 283 } ], "ok" : 1 }
Now let’s compare those numbers with whether white won or lost.
> db.runCommand({aggregate: "fast_win", pipeline: [ ... // extract the first move ... { ... $project : { ... firstMove : "$moves.1.white.move", ... // create a new field, "win", which is 1 if white won and 0 if black won ... win : {$cond : [ ... {$eq : ["$result", "1-0"]}, 1, 0 ... ]} ... } ... }, ... // group by the move and count up how many winning games used it ... { ... $group : { ... _id : "$firstMove", ... numGames : {$sum : 1}, ... numWins : {$sum : "$win"} ... } ... }, ... // calculate the percent of games won with this starting move ... { ... $project : { ... _id : 1, ... numGames : 1, ... percentWins : { ... $multiply : [100, { ... $divide : ["$numWins","$numGames"] ... }] ... } ... } ... }, ... // discard moves that were used in less than 10 games (probably not representative) ... { ... $match : { ... numGames : {$gte : 10} ... } ... }, ... // order from worst to best ... { ... $sort : { ... percentWins : 1 ... } ... }]}) { "result" : [ { "_id" : "f4", "numGames" : 25, "percentWins" : 24 }, { "_id" : "b4", "numGames" : 17, "percentWins" : 35.294117647058826 }, { "_id" : "c4", "numGames" : 36, "percentWins" : 50 }, { "_id" : "d4", "numGames" : 283, "percentWins" : 50.53003533568905 }, { "_id" : "g4", "numGames" : 11, "percentWins" : 63.63636363636363 }, { "_id" : "Nf3", "numGames" : 37, "percentWins" : 67.56756756756756 }, { "_id" : "e4", "numGames" : 696, "percentWins" : 68.24712643678161 }, { "_id" : "Nc3", "numGames" : 14, "percentWins" : 78.57142857142857 } ], "ok" : 1 }
Pawn to e4 seems like the most dependable winner here. Knight to c3 also seems like a good choice (at a nearly 80% win rate), but it was only used in 14 winning games.
Experiment #3: Best and Worst Moves for Black
We basically want to do a similar pipeline to Experiment 2, but for black. At the end, we want to find the best and worst percent.
> db.runCommand({aggregate: "fast_win", pipeline: [ ... // extract the first move ... { ... $project : { ... firstMove : "$moves.1.black.move", ... win : {$cond : [ ... {$eq : ["$result", "0-1"]}, 1, 0 ... ]} ... } ... }, ... // group by the move and count up how many winning games used it ... { ... $group : { ... _id : "$firstMove", ... numGames : {$sum : 1}, ... numWins : {$sum : "$win"} ... } ... }, ... // calculate the percent of games won with this starting move ... { ... $project : { ... _id : 1, ... numGames : 1, ... percentWins : { ... $multiply : [100, { ... $divide : ["$numWins","$numGames"] ... }] ... } ... } ... }, ... // discard moves that were used in less than 10 games (probably not representative) ... { ... $match : { ... numGames : {$gte : 10} ... } ... }, ... // get the best and worst ... { ... $group : { ... _id : 1, ... best : {$max : "$_id"}, ... worst : {$min : "$_id"} ... } ... }]}) { "result" : [ { "_id" : 1, "best" : "g6", "worst" : "Nc6" } ], "ok" : 1 }
“Nc6″ means “move the knight to c6.” Or, rather, don’t, because it doesn’t tend to work out that well.
I like this new aggregation functionality because it’s feels simpler than MapReduce. You can start with a one-operation pipeline and build it up, step-by-step, seeing exactly what a given operation does to your output. (And no Javascript required, which is always a plus.)
There’s lots more documentation on aggregation pipelines in the docs and I’ll be doing a couple more posts on it.
And now, for something completely different
Jan 17th
Probably only relevant to a limited portion of my audience, but Silicon Valley Ryan Gosling is awesome. I have never seen anything like and I’m not sure what the point is, but I know I’m a fan.
Go forth and be sexy and supportive for the female programmers you know.
Replica Set Internals Bootcamp: Part I – Elections
Jan 4th
I’ve been doing replica set “bootcamps” for new hires. It’s mainly focused on applying this to debug replica set issues and being able to talk fluently about what’s happening, but it occurred to me that you (blog readers) might be interested in it, too.
There are 8 subjects I cover in my bootcamp:

- Elections
- Creating a set
- Reconfiguring
- Syncing
- Initial Sync
- Rollback
- Authentication
- Debugging
I’m going to do one subject per post, we’ll see how many I can get through.
Prerequisites: I’m assuming you know what replica sets are and you’ve configured a set, written data to it, read from a secondary, etc. You understand the terms primary and secondary.
The most obvious feature of replica sets is their ability to elect a new primary, so the first thing we’ll cover is this election process.
Replica Set Elections

Let’s say we have a replica set with 3 members: X, Y, and Z. Every two seconds, each server sends out a heartbeat request to the other members of the set. So, if we wait a few seconds, X sends out heartbeats to Y and Z. They respond with information about their current situation: the state they’re in (primary/secondary), if they are eligible to become primary, their current clock time, etc.
X receives this info and updates its “map” of the set: if members have come up or gone down, changed state, and how long the roundtrip took.
At this point, if X map changed, X will check a couple of things: if X is primary and a member went down, it will make sure it can still reach a majority of the set. If it cannot, it’ll demote itself to a secondary.
Demotions
There is one wrinkle with X demoting itself: in MongoDB, writes default to fire-and-forget. Thus, if people are doing fire-and-forget writes on the primary and it steps down, they might not realize X is no longer primary and keep sending writes to it. The secondary-formerly-known-as-primary will be like, “I’m a secondary, I can’t write that!” But because the writes don’t get a response on the client, the client wouldn’t know.
Technically, we could say, “well, they should use safe writes if they care,” but that seems dickish. So, when a primary is demoted, it also closes all connections to clients so that they will get a socket error when they send the next message. All of the client libraries know to re-check who is primary if they get an error. Thus, they’ll be able to find who the new primary is and not accidentally send an endless stream of writes to a secondary.
Elections
Anyway, getting back to the heartbeats: if X is a secondary, it’ll occasionally check if it should elect itself, even if its map hasn’t changed. First, it’ll do a sanity check: does another member think it’s primary? Does X think it’s already primary? Is X ineligible for election? If it fails any of the basic questions, it’ll continue puttering along as is.
If it seems as though a new primary is needed, X will proceed to the first step in election: it sends a message to Y and Z, telling them “I am considering running for primary, can you advise me on this matter?”
When Y and Z get this message, they quickly check their world view. Do they already know of a primary? Do they have more recent data than X? Does anyone they know of have more recent data than X? They run through a huge list of sanity checks and, if everything seems satisfactory, they tentatively reply “go ahead.” If they find a reason that X cannot be elected, they’ll reply “stop the election!”
If X receives any “stop the election!” messages, it cancels the election and goes back to life as a secondary.
If everyone says “go ahead,” X continues with the second (and final) phase of the election process.
For the second phase, X sends out a second message that is basically, “I am formally announcing my candidacy.” At this point, Y and Z make a final check: do all of the conditions that held true before still hold? If so, they allow X to take their election lock and send back a vote. The election lock prevents them from voting for another candidate for 30 seconds.
If one of the checks doesn’t pass the second time around (fairly unusual, at least in 2.0), they send back a veto. If anyone vetos, the election fails.

Suppose that Y votes for X and Z vetos X. At that point, Y‘s election lock is taken, it cannot vote in another election for 30 seconds. That means that, if Z wants to run for primary, it had better be able to get X‘s vote. That said, it should be able to if Z is a viable candidate: it’s not like the members hold grudges (except for Y, for 30 seconds).
If no one vetos and the candidate member receives votes from a majority of the set, the candidate becomes primary.
Confused?
Feel free to ask questions in the comments below. This is a loving, caring bootcamp (as bootcamps go).
SQL to MongoDB: An Updated Mapping
Dec 9th
The aggregation pipeline code has finally been merged into the main development branch and is scheduled for release in 2.2. It lets you combine simple operations (like finding the max or min, projecting out fields, taking counts or averages) into a pipeline of operations, making a lot of things that were only possible by using MapReduce doable with a “normal” query.
In celebration of this, I thought I’d re-do the very popular MySQL to MongoDB mapping using the aggregation pipeline, instead of MapReduce.
Here is the original SQL:
SELECT Dim1, Dim2, SUM(Measure1) AS MSum, COUNT(*) AS RecordCount, AVG(Measure2) AS MAvg, MIN(Measure1) AS MMin MAX(CASE WHEN Measure2 < 100 THEN Measure2 END) AS MMax FROM DenormAggTable WHERE (Filter1 IN (’A’,’B’)) AND (Filter2 = ‘C’) AND (Filter3 > 123) GROUP BY Dim1, Dim2 HAVING (MMin > 0) ORDER BY RecordCount DESC LIMIT 4, 8
We can break up this statement and replace each piece of SQL with the new aggregation pipeline syntax:
| MongoDB Pipeline | MySQL |
|---|---|
aggregate: "DenormAggTable" |
FROM DenormAggTable |
{ $match : { Filter1 : {$in : ['A','B']}, Filter2 : 'C', Filter3 : {$gt : 123} } } |
WHERE (Filter1 IN (’A’,’B’)) AND (Filter2 = ‘C’) AND (Filter3 > 123) |
{ $project : { Dim1 : 1, Dim2 : 1, Measure1 : 1, Measure2 : 1, lessThanAHundred : { $cond: [ {$lt: ["$Measure2", 100] }, "$Measure2", // if 0] // else } } } |
CASE WHEN Measure2 < 100 THEN Measure2 END |
{ $group : { _id : {Dim1 : 1, Dim2 : 1}, MSum : {$sum : "$Measure1"}, RecordCount : {$sum : 1}, MAvg : {$avg : "$Measure2"}, MMin : {$min : "$Measure1"}, MMax : {$max : "$lessThanAHundred"} } } |
SELECT Dim1, Dim2, SUM(Measure1) AS MSum, COUNT(*) AS RecordCount, AVG(Measure2) AS MAvg, MIN(Measure1) AS MMin MAX(CASE WHEN Measure2 < 100 THEN Measure2 END) AS MMax GROUP BY Dim1, Dim2 |
{ $match : {MMin : {$gt : 0}} } |
HAVING (MMin > 0) |
{ $sort : {RecordCount : -1} } |
ORDER BY RecordCount DESC |
{ $limit : 8 }, { $skip : 4 } |
LIMIT 4, 8 |
Putting all of these together gives you your pipeline:
> db.runCommand({aggregate: "DenormAggTable", pipeline: [ { $match : { Filter1 : {$in : ['A','B']}, Filter2 : 'C', Filter3 : {$gt : 123} } }, { $project : { Dim1 : 1, Dim2 : 1, Measure1 : 1, Measure2 : 1, lessThanAHundred : {$cond: [{$lt: ["$Measure2", 100]}, { "$Measure2", 0] } } }, { $group : { _id : {Dim1 : 1, Dim2 : 1}, MSum : {$sum : "$Measure1"}, RecordCount : {$sum : 1}, MAvg : {$avg : "$Measure2"}, MMin : {$min : "$Measure1"}, MMax : {$max : "$lessThanAHundred"} } }, { $match : {MMin : {$gt : 0}} }, { $sort : {RecordCount : -1} }, { $limit : 8 }, { $skip : 4 } ]})
As you can see, the SQL matches the pipeline operations pretty clearly. If you want to play with it, it’ll be available soon to a the development nightly build.
If you’re at MongoSV today (December 9th, 2011), check out Chris Westin’s talk on the new aggregation framework at 3:45 in room B4.
On working at 10gen
Oct 18th
10gen is trying to hire a gazillion people, so I’m averaging two interviews a day (bleh). A lot of people have asked what it’s like to work on MongoDB, so I thought I’d write a bit about it.
A Usual Day

Coffee: the lynchpin of my day.
- Get in around 10am.
- Check if there are any commercial support questions that need to be answered right now.
- Have a cup of coffee and code until lunch.
- Eat lunch.
- If nothing dire has happened, go out for coffee+writing. This refuels my brain and is a creative outlet: that’s where I am now. My coffee does not look nearly as awesome as the coffee on the right.
- Go back to the office, code all afternoon.
- Depending on the day, usually between 5:30 and 6:30 the programmers will naturally start discussing problems we had over the day, interviews, support, the latest geek news, etc. Often beers are broken out.
- Wrap up, go home.
There are some variations on this: as I mentioned, a lot of time lately is taken up by interviewing. Other coworkers spend a lot more time than I do at consults, trainings, speaking at conferences, etc.
Other General Workday Stuff
On Fridays, we have lunch as a team. After lunch, we have a tech talk where someone presents on what they’re working on (e.g., the inspiration for my geospatial post) or general info that’s good to know (e.g., the inspiration for my virtual memory post). This is a nice way to end the week, especially since Fridays often wrap up earlier than other days.
A couple people use OS X or Windows for development, most people use Linux. You can use whatever you want. I’d like to encourage emacs users, in particular, to apply, as we’re falling slightly behind vi in numbers.
We sit in an open office plan, everyone at tables in a big room (including the CEO and CTO, who are both programmers). The only people in separate rooms are the people who have to be on the phone all day (sales, marketers, basketweavers… I’m not really clear on what non-technical people do).
And speaking of what people actually do, here are three examples of my job (that are more specific than “coding”):
Fixing Other People’s Bugs

Recently, a developer was using MongoDB and IBM’s DB2 with PHP. After he installed the MongoDB driver, PHP started segfaulting all over the place. I downloaded the ibm_db2 PHP extension to take a look.
PHP keeps a “storage unit” for extensions’ long-term memory use. Every extension shares the space and can store things there.
The DB2 extension was basically fire-bombing the storage unit.
It went through the storage, object by object, casting the objects into DB2 types and then freeing them. This worked fine when DB2 was the only PHP extension being used, but broke down when anyone else tried to use that storage. I gave the user a small patch that stopped the DB2 extension from destroying objects it didn’t create, and everything worked fine for them, after that.
The Game is Afoot

A user reported that they couldn’t initialize their replica set: a member wasn’t coming online. The trick with this type of bug is to get enough evidence before the user wants to beat you over the head with the 800th log you’ve requested.
I asked them to send the first round of logs. It was weird, nothing was wrong from server1‘s point of view: it initialized properly and could connect to everyone in the set. I puzzled over the messages, figuring out that once server1 had created the set, server2 had accepted the connection from server1 but then somehow failed to connect back to server1 and so couldn’t pick up the set config. However, according to server1, it could connect fine to server2 and thought it was perfectly healthy!
I finally realized what must be happening: “It looks like server2 couldn’t connect to any of the others, but all of them could connect to it. Could you check your firewall?”
“Oh, that server was blocking all outgoing connections! Now its working fine.”
Elementary, my dear Watson.
You know you’re not at a big company when…

At least it had "handles."
Someone on Sparc complained that the Perl driver wasn’t working at all for them. My first thought was that Sparc is big-endian, so maybe the Perl driver wasn’t flipping memory correctly. I asked Eliot where our Power PC was, and he said we must have forgotten it when we moved: it was still in our old office around the corner.
“Bring someone to help carry it,” he told me. “It’s heavy.”
Pshaw, I thought. How heavy could an old desktop be?
I went around the corner and the other company graciously let me walk into their server room, choose a server, and walk out with it. Unfortunately, it weighed about 50 pounds, and I have a traditional geek physique (no muscles). The trip back to our office involved me staggering a couple steps, putting it down, shaking out my arms, and repeat.
When I got to our office, I just dragged it down the hallway to our server closet. Eliot saw me tugboating the thing down the hallway.
“You didn’t bring someone to help?”
“It’s *oof* fine!”
Unfortunately, once it was all set up, the Perl driver worked perfectly on it. So it wasn’t big-endian specific.
I was now pretty sure it was Sparc-specific (another person had reported the same problem on a Sparc), so I bought an elderly Sparc server for a couple hundred bucks off eBay. When it arrived a couple days later, Eliot showed me how to rack it and I spent a day fighting with the Solaris/Oracle package manager. However, it was all worth it: I tried running the Perl driver and it instantly failed (success!).
After some debugging, I realized that Sparc was much more persnickety than Intel about byte alignment. The Perl driver was playing fast and loose with a byte buffer, casting pieces of it into other types (which Sparc didn’t like). I changed some casts to memcpys and the Perl driver started working beautifully.
But every day is different
The episodes above are a very small sample of what I do: there are hundreds of other things I’ve worked on over the last few years from speaking to working on the database to writing a freakin Facebook app.
So, if this sounded interesting, please go to our jobs website and submit an application!
Getting Started with MMS
Sep 28th
Edit: since this was written, Sam has written some excellent documentation on using MMS. I recommend reading through it as you explore MMS.
Telling someone “You should set up monitoring” is kind of like telling someone “You should exercise 20 minutes three times a week.” Yes, you know you should, but your chair is so comfortable and you haven’t keeled over dead yet.
For years*, 10gen has been planning to do monitoring “right,” making it painless to monitor your database. Today, we released the MongoDB Monitoring Service: MMS.
MMS is free hosted monitoring for MongoDB. I’ve been using it to help out paying customers for a while, so I thought I’d do a quick post on useful stuff I’ve discovered (documentation is… uh… a little light, so far).
So, first: you sign up.

There are two options: register a company and register another account for an existing company. For example, let’s say I wanted to monitor the servers for Snail in a Turtleneck Enterprises. I’ll create a new account and company group. Then Andrew, sys admin of my heart, can create an account with Snail in a Turtleneck Enterprises and have access to all the same monitoring info.

Once you’re registered, you’ll see a page encouraging you to download the MMS agent. Click on the “download the agent” link.

This is a little Python program that collects stats from MongoDB, so you need to have pymongo installed, too. Starting from scratch on Ubuntu, do:
$ # prereqs $ sudo apt-get install python python-setuptools $ sudo easy_install pymongo $ $ # set up agent $ unzip name-of-agent.zip $ cd name-of-agent $ mkdir logs $ $ # start agent $ nohup python agent.py > logs/agent.log 2>&1 &
Last step! Back to the website: see that “+” button next to the “Hosts” title?

Designed by programmers, for Vulcans
Click on that and type a hostname. If you have a sharded cluster, add a mongos. If you have a replica set, add any member.
Now go have a nice cup of coffee. This is an important part of the process.
When you get back, tada, you’ll have buttloads of graphs. They probably won’t have much on them, since MMS will have been monitoring them for all of a few minutes.
Cool stuff to poke
This is the top bar of buttons:
![]()
Of immediate interest: click “Hosts” to see a list of hosts.
You’ll see hostname, role, and the last time the MMS agent was able to reach this host. Hosts that it hasn’t reached recently will have a red ping time.

Now click on a server’s name to see all of the info about it. Let’s look at a single graph.

You can click & drag to see a smaller bit of time on the graph. See those icons in the top right? Those give you:
- +
- Add to dashboard: you can create a custom dashboard with any charts you’re interested in. Click on the “Dashboard” link next to “Hosts” to see your dashboard.
- Link
- Link to a private URL for this chart. You’ll have to be logged in to see it.
- Email a jpg of this chart to someone.
- i
- This is maybe the most important one: a description of what this chart represents.
That’s the basics. Some other points of interest:
- You can set up alerts by clicking on “Alerts” in the top bar
- “Events” shows you when hosts went down or came up, because primary or secondary, or were upgraded.
- Arbiters don’t have their own chart, since they don’t have data. However, there is an “Arbiters” tab that lists them if you have some.
- The “Last Ping” tab contains all of the info sent by MMS on the last ping, which I find interesting.
- If you are confused, there is an “FAQ” link in the top bar that answers some common questions.
If you have any problems with MMS, there’s a little form at the bottom to let you complain:

This will file a bug report for you. This is a “private” bug tracker, only 10gen and people in your group will be able to see the bugs you file.
* If you ran mongod --help using MongoDB version 1.0.0 or higher, you might have noticed some options that started with --mms. In other words, we’ve been planning this for a little while.
More PHP Internals: References
Sep 7th
By request, a quick post on using PHP references in extensions.
To start, here’s an example of references in PHP we’ll be translating into C:
<?php // just for displaying output function display($x) { echo "x is $x\n"; } // pass in an argument by making a copy of it function not_by_ref($arg) { echo "called not_by_ref($arg)\n"; $arg = 2; } // pass in an argument by reference function by_ref(&$arg) { echo "called by_ref($arg)\n"; $arg = 3; } $x = 1; display($x); not_by_ref($x); display($x); // when x is passed by reference, the function can change the value by_ref($x); display($x); ?>
This will print:
x is 1 called not_by_ref(1) x is 1 called by_ref(1) x is 3
If you want your C extension’s function to officially have a signature with ampersands in it, you have to declare to PHP that you want to pass in refs as arguments. Remember how we declared functions in this struct?
zend_function_entry rlyeh_functions[] = { PHP_FE(cthulhu, NULL) { NULL, NULL, NULL } };
The second argument to PHP_FE, NULL, can optional be the argument spec. For example, let’s say we’re implementing by_ref() in C. We would add this to php_rlyeh.c:
// the 1 indicates pass-by-reference ZEND_BEGIN_ARG_INFO(arginfo_by_ref, 1) ZEND_END_ARG_INFO(); zend_function_entry rlyeh_functions[] = { PHP_FE(cthulhu, NULL) PHP_FE(by_ref, arginfo_by_ref) { NULL, NULL, NULL } }; PHP_FUNCTION(by_ref) { zval *zptr = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zptr) == FAILURE) { return; } php_printf("called (the c version of) by_ref(%d)\n", (int)Z_LVAL_P(zptr)); ZVAL_LONG(zptr, 3); }
Suppose we also add not_by_ref(). This might look something like:
ZEND_BEGIN_ARG_INFO(arginfo_not_by_ref, 0) ZEND_END_ARG_INFO(); zend_function_entry rlyeh_functions[] = { PHP_FE(cthulhu, NULL) PHP_FE(by_ref, arginfo_by_ref) PHP_FE(not_by_ref, arginfo_not_by_ref) { NULL, NULL, NULL } }; PHP_FUNCTION(not_by_ref) { zval *zptr = 0, *copy = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zptr) == FAILURE) { return; } php_printf("called (the c version of) not_by_ref(%d)\n", (int)Z_LVAL_P(zptr)); ZVAL_LONG(zptr, 2); }
However, if we try running this, we’ll get:
x is 1 called (the c version of) not_by_ref(1) x is 2 called (the c version of) by_ref(2) x is 3
What happened? not_by_ref used our variable like a reference!
This is really weird and annoying behavior (if anyone knows why PHP does this, please comment below).
To work around it, if you want non-reference behavior, you have to manually make a copy of the argument.
Our not_by_ref() function becomes:
PHP_FUNCTION(not_by_ref) { zval *zptr = 0, *copy = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zptr) == FAILURE) { return; } // make a copy MAKE_STD_ZVAL(copy); memcpy(copy, zptr, sizeof(zval)); // set refcount to 1, as we're only using "copy" in this function Z_SET_REFCOUNT_P(copy, 1); php_printf("called (the c version of) not_by_ref(%d)\n", (int)Z_LVAL_P(copy)); ZVAL_LONG(copy, 2); zval_ptr_dtor(©); }
Note that we set the refcount of copy to 1. This is because the refcount for zptr is 2: 1 ref from the calling function + 1 ref from the not_by_ref function. However, we don’t want the copy of zptr to have a refcount of 2, because it’s only being used by the current function.
Also note that memcpy-ing the zval only works because this is a scalar: if this were an array or object, we’d have to use PHP API functions to make a deep copy of the original.
If we run our PHP program again, it gives us:
x is 1 called (the c version of) not_by_ref(1) x is 1 called (the c version of) by_ref(1) x is 3
Okay, this is pretty good… but we’re actually missing a case. What happens if we pass in a reference to not_by_ref()? In PHP, this looks like:
function not_by_ref($arg) { $arg = 2; } $x = 1; not_by_ref(&$x); display($x);
…which displays “x is 2″. Unfortunately, we’ve overridden this behavior in our not_by_ref() C function, so we have to special case: if this is a reference, change its value, otherwise make a copy and change the copy’s value.
PHP_FUNCTION(not_by_ref) { zval *zptr = 0, *copy = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zptr) == FAILURE) { return; } // NEW CODE if (Z_ISREF_P(zptr)) { // if this is a reference, make copy point to zptr copy = zptr; // adding a reference so we can indiscriminately delete copy later zval_add_ref(&zptr); } // OLD CODE else { // make a copy MAKE_STD_ZVAL(copy); memcpy(copy, zptr, sizeof(zval)); // set refcount to 1, as we're only using "copy" in this function Z_SET_REFCOUNT_P(copy, 1); } php_printf("called (the c version of) not_by_ref(%d)\n", (int)Z_LVAL_P(copy)); ZVAL_LONG(copy, 2); zval_ptr_dtor(©); }
Now it’ll behave “properly.”
There may be a better way to do this, please leave a comment if you know of one. However, as far as I know, this is the only way to emulate the PHP reference behavior.
If you would like to read more about PHP references, Derick Rethans wrote a great article on it for PHP Architect.



Subscribe to all posts