<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Snail in a Turtleneck</title>
	
	<link>http://www.snailinaturtleneck.com/blog</link>
	<description>Kristina Chodorow's Blog</description>
	<lastBuildDate>Tue, 15 May 2012 15:48:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/kchodorow" /><feedburner:info uri="kchodorow" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>10 Kindle Apps for the Non-Existent Developer API</title>
		<link>http://feedproxy.google.com/~r/kchodorow/~3/vlm9qL_BjXo/</link>
		<comments>http://www.snailinaturtleneck.com/blog/2012/05/15/10-kindle-apps-for-the-non-existent-developer-api/#comments</comments>
		<pubDate>Tue, 15 May 2012 15:39:48 +0000</pubDate>
		<dc:creator>Kristina Chodorow</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[developer API]]></category>
		<category><![CDATA[kindle]]></category>

		<guid isPermaLink="false">http://www.snailinaturtleneck.com/blog/?p=1992</guid>
		<description><![CDATA[The Kindle should have a developer API. Ereaders could be revolutionizing the way people read, but right now they&#8217;re like paperbacks without the nice book smell. I&#8217;ve heard a lot of people say, &#8220;the Kindle isn&#8217;t powerful enough for apps.&#8221; Poppycock. I&#8217;m not talking about using it to play Angry Birds, I&#8217;m talking about stuff&#8230;]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p><img src="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/05/kindle1-300x289.jpg" alt="" title="kindle" width="300" height="289" class="alignright size-medium wp-image-1997" /></p>
<p>The Kindle should have a developer API.  Ereaders could be revolutionizing the way people read, but right now they&#8217;re like paperbacks without the nice book smell.</p>
<p>I&#8217;ve heard a lot of people say, &#8220;the Kindle isn&#8217;t powerful enough for apps.&#8221;  Poppycock.  I&#8217;m not talking about using it to play Angry Birds, I&#8217;m talking about stuff a calculator could zoom though and would actually improve the reading experience.</p>
<p>So I present 10 apps that would be super-useful, require few resources, and (in some cases) increase profits:</p>
<ol>
<li>A &#8220;more content&#8221; button for magazines.  If I&#8217;m reading a good magazine, I&#8217;d love to be able to get $10 more of content when I&#8217;m done. It&#8217;d be like giving a rat a lever that dispenses pellets.  Yum, reading pellets.
<li>Renaming books.  Apparently my workflow is defective, because I&#8217;ll often end up with 6 titles named &#8220;Imported PDF&#8221;, and there is no way to distinguish the one I want other than opening each PDF until I find it.  If I could just rename the damn things&#8230;
<li>Support for other organizational schemes.  Some people like tags (like whoever wrote Gmail, apparently) and everyone else likes hierarchical folders.  I hate tags, I want things neatly tucked away in <a href="http://www.sfwa.org/2012/02/2011-nebula-awards-nominees-announced/">Sci Fi/Nebula Awards/Short Stories</a>, not a franken-tag like &#8220;Sci Fi &#8211; Nebula Awards &#8211; Short Stories&#8221; (okay, it&#8217;s equivalent, but I hate tags).
<li>In technical books, how often is there a diagram that you keep flipping back and forth to for the next 10 to 15 pages?  It would be nice to be able to &#8220;pin&#8221; it to the top of the screen as you read all of the text related to the diagram.
<li><a href="http://www.goodreads.com/">Goodreads</a> integration.  When I finish a book, I want to rate it and have it automatically added to my &#8220;read&#8221; shelf in at Goodreads.
<li>Related to above: recommendations when I finish a book and rate it.  If I just rated it five stars, show me other books people who loved this book liked.  If I rated it one star, show me books people who hated this book liked.
<li>Related to above (again): list my Amazon recommendations inline with my list of books.  This would be a money-spinner for them, I think, because Amazon&#8217;s recommendation engine is freakishly accurate (except when it gets thrown out of whack by holiday shopping).  If I was looking at my Kindle and saw a list of books I really wanted to read a click away&#8230; well, I&#8217;d be much poorer.
<li>Make looking up words plugable to different search engines (Wikipedia, Urban Dictionary, D&#038;D Compendium, etc).  I was recently reading &#8220;Crime and Punishment&#8221; and came across the term &#8220;yellow ticket.&#8221;  The built-in dictionary knew what &#8220;yellow&#8221; was, and it knew what &#8220;ticket&#8221; was, but that didn&#8217;t help a whole lot (answer: <a href="http://en.wikipedia.org/wiki/Prostitution_in_Russia">an id given to Russian prostitutes</a>).
<li>Update support.  Technical books especially can benefit from this: O&#8217;Reilly has been working to do multiple quick releases of ebooks so that they can be updated as the technology changes.  Imagine if you&#8217;re opening up your well-thumbed copy of <a href="http://shop.oreilly.com/product/0636920018308.do"><em>Scaling MongoDB</em></a> and a dialog pops up: &#8220;Version 1.2 of <em>Scaling MongoDB</em> is available, covering changes in MongoDB 2.2.  Would you like to download? [Yes/No]&#8220;.  However, the support just isn&#8217;t there on the device side.  (And a new version of <em>Scaling MongoDB</em> isn&#8217;t available yet, sorry.)
<li>Metrics.  As an author, I would <em>love</em> to know how long it took someone to read a page, how many times they came back to it, and when they put the book down and went to do something else.  Authors have never been able to get this level of feedback before and I think it would revolutionize writing.  Basic user tracking would be <em>amazing</em>.
</ol>
<p>I&#8217;m not sure why Amazon doesn&#8217;t have a dev API, but I&#8217;d imagine that part of the reason is that most publishers would not like it.  However, I think Amazon is big enough to crush them into submission.  I hope that they will hurry up and do so.</p>
<p>If anyone has any ideas on how to get Amazon to implement a developer API, please comment!</p>
<p><em>P.S. I know about the <a href="https://kdk-javadocs.s3.amazonaws.com/2.0/index.html">API here</a>, but that&#8217;s essentially for Angry-Birds-type apps.  I&#8217;m looking for an API that lets you mess with the reader.</em></p>
<div class="shr-publisher-1992"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.snailinaturtleneck.com%2Fblog%2F2012%2F05%2F15%2F10-kindle-apps-for-the-non-existent-developer-api%2F' data-shr_title='10+Kindle+Apps+for+the+Non-Existent+Developer+API'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom --><img src="http://feeds.feedburner.com/~r/kchodorow/~4/vlm9qL_BjXo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.snailinaturtleneck.com/blog/2012/05/15/10-kindle-apps-for-the-non-existent-developer-api/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://www.snailinaturtleneck.com/blog/2012/05/15/10-kindle-apps-for-the-non-existent-developer-api/</feedburner:origLink></item>
		<item>
		<title>––thursday #5: diagnosing high readahead</title>
		<link>http://feedproxy.google.com/~r/kchodorow/~3/k412e8EUBQo/</link>
		<comments>http://www.snailinaturtleneck.com/blog/2012/05/10/thursday-5-diagnosing-high-readahead/#comments</comments>
		<pubDate>Thu, 10 May 2012 21:28:22 +0000</pubDate>
		<dc:creator>Kristina Chodorow</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[--thursday]]></category>

		<guid isPermaLink="false">http://www.snailinaturtleneck.com/blog/?p=1881</guid>
		<description><![CDATA[Having readahead set too high can slow your database to a crawl. This post discusses why that is and how you can diagnose it. The #1 sign that readahead is too high is that MongoDB isn&#8217;t using as much RAM as it should be. If you&#8217;re running Mongo Monitoring Service (MMS), take a look at&#8230;]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>Having readahead set too high can slow your database to a crawl.  This post discusses why that is and how you can diagnose it.  </p>
<p>The #1 sign that readahead is too high is that MongoDB isn&#8217;t using as much RAM as it should be.  If you&#8217;re running <a href="http://mms.10gen.com">Mongo Monitoring Service (MMS)</a>, take a look at the &#8220;resident&#8221; size on the &#8220;memory&#8221; chart.  Resident memory can be thought of as &#8220;the amount of space MongoDB &#8216;owns&#8217; in RAM.&#8221;  Therefore, if MongoDB is the only thing running on a machine, we want resident size to be as high as possible.  On the chart below, resident is ~3GB:  </p>
<p><img src="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/04/Screen-Shot-2012-04-25-at-4.48.35-PM-1024x440.png" alt="" title="MMS" width="1024" height="440" class="aligncenter size-large wp-image-1970" /></p>
<p>Is 3GB good or bad?  Well, it depends on the machine.  If the machine only has 3.5GB of RAM, I&#8217;d be pretty happy with 3GB resident.  However, if the machine has, say, 15GB of RAM, then we&#8217;d like at least 15GB of the data to be in there (the &#8220;mapped&#8221; field is (sort of) data size, so I&#8217;m assuming we have 60GB of data).</p>
<p>Assuming we&#8217;re accessing a lot of this data, we&#8217;d expect MongoDB&#8217;s resident set size to be 15GB, but it&#8217;s only 3GB.  If we try turning down readahead and the resident size jumps to 15GB and our app starts going faster.  But why is this?</p>
<p>Let&#8217;s take an example: suppose all of our docs are 512 bytes in size (readahead is set in 512-byte increments, called <em>sectors</em>, so 1 doc = 1 sector makes the math easier).  If we have 60GB of data then we have ~120 million documents (60GB of data/(512 bytes/doc)).  The 15GB of RAM on this machine should be able to hold ~30 million documents.  </p>
<p>Our application accesses documents randomly across our data set, so we&#8217;d expect MongoDB to eventually &#8220;own&#8221; (have resident) all 15GB of RAM, as 1) it&#8217;s the only thing running and 2) it&#8217;ll eventually fetch at least 15GB of the data.</p>
<p>Now, let&#8217;s set our readahead to 100 (100 512-byte sectors, aka 100 documents): <code>blockdev --set-ra 100</code>.  What happens when we run our application?</p>
<p>Picture our disk as looking like this, where each o is a document:</p>
<pre>
...
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
... // keep going for millions more o's
</pre>
<p>Let&#8217;s say our app requests a document.  We&#8217;ll mark it with &#8220;x&#8221; to show that the OS has pulled it into memory:</p>
<pre>
...
ooooooooooooooooooooooooo
ooooxoooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
...
</pre>
<p>See it on the third line there?  But that&#8217;s not the only doc that&#8217;s pulled into memory: readahead is set to 100 so the next 99 documents are pulled into memory, too:</p>
<pre>
...
ooooooooooooooooooooooooo
ooooxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxooooooooooooooooooooo
ooooooooooooooooooooooooo
ooooooooooooooooooooooooo
...
</pre>
<div id="attachment_1983" class="wp-caption alignright" style="width: 297px"><img src="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/05/shoppingcart.jpg" alt="" title="Hobo shopping cart" width="287" height="300" class="size-full wp-image-1983" /><p class="wp-caption-text">Is your OS returning this with every document?</p></div>
<p>Now we have 100 docs in memory, but remember that our application is accessing documents randomly: the likelihood of the next document we access is in that block of 100 docs is almost nil.  At this point, there&#8217;s 50KB of data in RAM (512 bytes * 100 docs = 51,200 bytes) and MongoDB&#8217;s resident size has only increase by 512 bytes (1 doc).</p>
<p>Our app will keep bouncing around the disk, reading docs from here and there and filing up memory with docs MongoDB never asked for until RAM is completely full of junk that&#8217;s never been used.  Then, it&#8217;ll start evicting things to make room for new junk as our app continues to make requests.</p>
<p>Working this out, there&#8217;s a 25% chance of our app requesting a doc that&#8217;s already in memory, so 75% of the requests are going to go to disk.  Say we&#8217;re doing 2 requests a sec.  Then 1 hour of requests is 2 requests * 3600 seconds/hour = 7200 requests, 4800 of which are going to disk (.75 * 7200). If each request pulls back 50KB, that&#8217;s 240MB read from disk/hour.  If we set readahead to 0, we&#8217;ll have 2MB read from disk/hour.</p>
<p>Which brings us to the next symptom of a too-high readahead: unexpectedly high disk IO. Because most of the data we want isn&#8217;t in memory, we keep having to go to disk, dragging shopping-carts full of junk into RAM, perpetuating the high disk io/low resident mem cycle.</p>
<p>The general takeaway is that a DB is not a &#8220;normal&#8221; workload for an OS.  The default settings may screw you over.</p>
<div class="shr-publisher-1881"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.snailinaturtleneck.com%2Fblog%2F2012%2F05%2F10%2Fthursday-5-diagnosing-high-readahead%2F' data-shr_title='%E2%80%93%E2%80%93thursday+%235%3A+diagnosing+high+readahead'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom --><img src="http://feeds.feedburner.com/~r/kchodorow/~4/k412e8EUBQo" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.snailinaturtleneck.com/blog/2012/05/10/thursday-5-diagnosing-high-readahead/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		<feedburner:origLink>http://www.snailinaturtleneck.com/blog/2012/05/10/thursday-5-diagnosing-high-readahead/</feedburner:origLink></item>
		<item>
		<title>Replica Set Internals Bootcamp Part IV: Syncing</title>
		<link>http://feedproxy.google.com/~r/kchodorow/~3/wwgEBeelw4I/</link>
		<comments>http://www.snailinaturtleneck.com/blog/2012/05/07/replica-set-internals-bootcamp-part-iv-syncing/#comments</comments>
		<pubDate>Mon, 07 May 2012 21:48:51 +0000</pubDate>
		<dc:creator>Kristina Chodorow</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.snailinaturtleneck.com/blog/?p=1863</guid>
		<description><![CDATA[I&#8217;ve been doing replica set &#8220;bootcamps&#8221; for new hires. It&#8217;s mainly focused on applying this to debug replica set issues and being able to talk fluently about what&#8217;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&#8230;]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>I&#8217;ve been doing replica set &#8220;bootcamps&#8221; for new hires. It&#8217;s mainly focused on applying this to debug replica set issues and being able to talk fluently about what&#8217;s happening, but it occurred to me that you (blog readers) might be interested in it, too.  </p>
<p><img src="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/01/dog-boot-camp-2.jpg" alt="" title="dog-boot-camp" width="265" height="286" class="alignright size-full wp-image-1760" /></p>
<p>There are 8 subjects I cover in my bootcamp: </p>
<ol>
<li><a href="http://www.snailinaturtleneck.com/blog/?p=1752">Elections</a>
<li><a href="http://www.snailinaturtleneck.com/blog/?p=1767">Creating a set</a>
<li>Reconfiguring
<li><a href="http://www.snailinaturtleneck.com/blog/?p=1863">Syncing</a>
<li>Initial Sync
<li>Rollback
<li>Authentication
<li>Debugging
</ol>
<p>I&#8217;m going to do one subject per post, we&#8217;ll see how many I can get through. I&#8217;m punting on reconfig for now because it&#8217;s finicky to write about.</p>
<p><em>Prerequisites: I&#8217;m assuming you know what replica sets are and you&#8217;ve configured a set, written data to it, read from a secondary, etc.  You understand the terms primary and secondary.</em></p>
<h4>Syncing</h4>
<p>When a secondary is operating normally, it chooses a member to sync from (more on that below) and starts pulling operations from the source&#8217;s <em>local.oplog.rs</em> collection.  When it gets an op (call it W, for &#8220;write&#8221;), it does three things:</p>
<ol>
<li>Applies the op</li>
<li>Writes the op to its own oplog (also <em>local.oplog.rs</em>)</li>
<li>Requests the next op</li>
</ol>
<p>If the db crashes between 1 &#038; 2 and then comes back up, it&#8217;ll think it hasn&#8217;t applied W yet, so it&#8217;ll re-apply it.  Luckily (i.e., due to massive amounts of hard work), oplog ops are idempotent: you can apply W once, twice, or a thousand times and you&#8217;ll end up with the same document.</p>
<p>For example, if you have a doc that looks like <code>{counter:1}</code> and you do an update like <code>{$inc:{counter:1}}</code> on the primary, you&#8217;ll end up with <code>{counter:2}</code> and the oplog will store <code>{$set:{counter:2}}</code>.  The secondaries will replicate that instead of the <code>$inc</code>.</p>
<h4>w</h4>
<p>To ensure a write is present on, say, two members, you can do:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">foo</span>.<span style="color: #660066;">runCommand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>getLastError<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> w<span style="color: #339933;">:</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Syntax varies based on language, consult your API docs, but it&#8217;s always an option for writes.  The way this works is kind of cool.</p>
<p>Suppose you have a member called <em>primary</em> and another member syncing from it, called <em>secondary</em>.  How does <em>primary</em> know where <em>secondary</em> is synced to?  Well, <em>secondary</em> is querying <em>primary</em>&#8216;s oplog for more results.  So, if <em>secondary</em> requests an op written at 3pm, <em>primary</em> knows <em>seconday</em> has replicated all ops written before 3pm.</p>
<p>So, it goes like:</p>
<ol>
<li>Do a write on <em>primary</em>.</li>
<li>Write is written to the oplog on <em>primary</em>, with a field &#8220;ts&#8221; saying the write occurred at time <em>t</em>.</li>
<li><code>{getLastError:1,w:2}</code> is called on <em>primary</em>.  <em>primary</em> has done the write, so it is just waiting for one more server to get the write (w:2).
<li><em>secondary</em> queries the oplog on <em>primary</em> and gets the op
<li><em>secondary</em> applies the op from time <em>t</em>
<li><em>secondary</em> requests ops with <em>{ts:{$gt:t}}</em> from <em>primary</em>&#8216;s oplog
<li><em>primary</em> updates that <em>secondary</em> has applied up to <em>t</em> because it is requesting ops > <em>t</em>.
<li>getLastError notices that <em>primary</em> and <em>secondary</em> both have the write, so w:2 is satisfied and it returns.
</ol>
<h4>Starting up</h4>
<p>When you start up a node, it takes a look at its <em>local.oplog.rs</em> collection and finds the latest entry in there.  This is called the <em>lastOpTimeWritten</em> and it&#8217;s the latest op this secondary has applied.</p>
<p>You can always use this shell helper to get the current last op in the shell:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> rs.<span style="color: #660066;">debug</span>.<span style="color: #660066;">getLastOpWritten</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>The &#8220;ts&#8221; field is the last optime written.</p>
<p>If a member starts up and there are no entries in the oplog, it will begin the initial sync process, which is beyond the scope of this post.</p>
<p>Once it has the last op time, it will chose a target to sync from.</p>
<h4>Who to sync from</h4>
<p>As of 2.0, servers automatically sync from whoever is &#8220;nearest&#8221; based on average ping time.  So, if you bring up a new member it starts sending out heartbeats to all the other members and averaging how long it takes to get a response.  Once it has a decent picture of the world, it&#8217;ll decide who to sync from using the following algorithm:</p>
<pre>
for each member that is healthy:
    if member[state] == PRIMARY
        add to set of possible sync targets

    if member[lastOpTimeWritten] > our[lastOpTimeWritten]
        add to set of possible sync targets

sync target = member with the min ping time from the possible sync targets
</pre>
<p>The definition of &#8220;member that is healthy&#8221; has changed somewhat over the versions, but generally you can think of it as a &#8220;normal&#8221; member: a primary or secondary.  In 2.0, &#8220;healthy&#8221; debatably includes slave delayed nodes. </p>
<p>You can see who a server is syncing from by running <code>db.adminCommand({replSetGetStatus:1})</code> and looking at the &#8220;syncingTo&#8221; field (only present on secondaries).  Yes, yes, it probably should have been syncingFrom.  Backwards compatibility sucks.</p>
<h4>Chaining Slaves</h4>
<p><img src="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/05/Chain.jpg" alt="" title="Chain" width="300" height="300" class="alignright size-full wp-image-1974" /></p>
<p>The algorithm for chosing a sync target means that slave chaining is semi-automatic: start up a server in a data center and it&#8217;ll (probably) sync from a server in the same data center, minimizing WAN traffic.  (Note that you can&#8217;t end up with a sync loop, i.e., A syncing from B and B syncing from A, because a secondary can only sync from another secondary with a strictly higher optime.)</p>
<p>One cool thing to implement was making w work with slave chaining.  If <em>A</em> is syncing from <em>B</em> and <em>B</em> is syncing from <em>C</em>, how does <em>C</em> know where <em>A</em> is synced to?  The way this works is that it builds on the existing oplog-reading protocol.</p>
<p>When <em>A</em> starts syncing from <em>B</em> (or any server starts syncing from another server), it sends a special &#8220;handshake&#8221; message that basically says, &#8220;Hi, I&#8217;m <em>A</em> and I&#8217;ll be syncing from your oplog.  Please track this connection for w purposes.&#8221;  </p>
<p>When <em>B</em> gets this message, it says, &#8220;Hmm, I&#8217;m not primary, so let me forward that along to the member <em>I&#8217;m</em> syncing from.&#8221;  So it opens a new connection to <em>C</em> and says &#8220;Pretend I&#8217;m &#8216;<em>A</em>&#8216;, I&#8217;ll be syncing from your oplog on <em>A</em>&#8216;s behalf.&#8221;  Note that <em>B</em> now has two connections open to <em>C</em>, one for itself and one for <em>A</em>.  </p>
<p>Whenever <em>A</em> requests more ops from <em>B</em>, <em>B</em> sends the ops from its oplog and then forwards a dummy request to <em>C</em> along &#8220;<em>A</em>&#8216;s&#8221; connection to <em>C</em>.  <em>A</em> doesn&#8217;t even need to be able to connect directly to <em>C</em>.</p>
<pre>
A        B        C
           <====>
  <====>   <---->
</pre>
<p><code>&lt;====&gt;</code> is a &#8220;real&#8221; sync connection.  The connection between B and C on A&#8217;s behalf is called a &#8220;ghost&#8221; connection (<code>&lt;----&gt;</code>).</p>
<p>On the plus side, this minimizes network traffic.  On the negative side, the absolute time it takes a write to get to all members is higher.</p>
<h4>Coming soon to a replica set near you&#8230;</h4>
<p>In 2.2, there will be a new command, <code>replSetSyncFrom</code>, that lets you change who a member is syncing from, bypassing the &#8220;choosing a sync target&#8221; logic.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">adminCommand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>replSetSyncFrom<span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;otherHost:27017&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<div class="shr-publisher-1863"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.snailinaturtleneck.com%2Fblog%2F2012%2F05%2F07%2Freplica-set-internals-bootcamp-part-iv-syncing%2F' data-shr_title='Replica+Set+Internals+Bootcamp+Part+IV%3A+Syncing'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom --><img src="http://feeds.feedburner.com/~r/kchodorow/~4/wwgEBeelw4I" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.snailinaturtleneck.com/blog/2012/05/07/replica-set-internals-bootcamp-part-iv-syncing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.snailinaturtleneck.com/blog/2012/05/07/replica-set-internals-bootcamp-part-iv-syncing/</feedburner:origLink></item>
		<item>
		<title>Night of the Living Dead Ops</title>
		<link>http://feedproxy.google.com/~r/kchodorow/~3/3mE5gn5mOrc/</link>
		<comments>http://www.snailinaturtleneck.com/blog/2012/04/20/night-of-the-living-dead-ops/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 21:03:32 +0000</pubDate>
		<dc:creator>Kristina Chodorow</dc:creator>
				<category><![CDATA[MongoDB]]></category>

		<guid isPermaLink="false">http://www.snailinaturtleneck.com/blog/?p=1956</guid>
		<description><![CDATA[MongoDB users often ask about the &#8220;killed&#8221; field in db.currentOp() output. For example, if you&#8217;ve run db.killOp(), you might see something like: &#62; db.currentOp&#40;&#41; &#123; &#34;inprog&#34; : &#91; &#123; &#34;opid&#34; : 3062962, &#34;active&#34; : true, &#34;lockType&#34; : &#34;write&#34;, &#34;waitingForLock&#34; : false, &#34;secs_running&#34; : 32267, &#34;op&#34; : &#34;update&#34;, &#34;ns&#34; : &#34;httpdb.servers&#34;, &#34;query&#34; : &#123; &#34;_id&#34; :&#8230;]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>MongoDB users often ask about the &#8220;killed&#8221; field in <code>db.currentOp()</code> output.  For example, if you&#8217;ve run <code>db.killOp()</code>, you might see something like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">currentOp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #3366CC;">&quot;inprog&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #3366CC;">&quot;opid&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">3062962</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;active&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;lockType&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;write&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;waitingForLock&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;secs_running&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">32267</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;op&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;update&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;ns&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;httpdb.servers&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;query&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #3366CC;">&quot;_id&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;150.237.88.189&quot;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;client&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;127.0.0.1:50416&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;desc&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;conn&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;threadId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0x2900c400&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;connectionId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">74</span><span style="color: #339933;">,</span>
<span style="display:block;background-color: #ffc;">			<span style="color: #3366CC;">&quot;killed&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span></span>			<span style="color: #3366CC;">&quot;numYields&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #3366CC;">&quot;opid&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">3063051</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;active&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;lockType&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;read&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;waitingForLock&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;op&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;query&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;ns&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;query&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #3366CC;">&quot;count&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;servers&quot;</span><span style="color: #339933;">,</span>
				<span style="color: #3366CC;">&quot;query&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #3366CC;">&quot;code&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #3366CC;">&quot;$gte&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;client&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;127.0.0.1:30736&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;desc&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;conn&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;threadId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0x29113700&quot;</span><span style="color: #339933;">,</span>
			<span style="color: #3366CC;">&quot;connectionId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">191</span><span style="color: #339933;">,</span>
<span style="display:block;background-color: #ffc;">			<span style="color: #3366CC;">&quot;killed&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span></span>			<span style="color: #3366CC;">&quot;numYields&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span>
		<span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The operation looks dead&#8230; it has <code>killed:true</code>, right?  But you can run <code>db.currentOp()</code> again and again and the op doesn&#8217;t go away, even though it&#8217;s been &#8220;killed.&#8221;  So what&#8217;s up with that?</p>
<div id="attachment_1957" class="wp-caption alignright" style="width: 360px"><a href="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/04/chainsaw.jpeg"><img src="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/04/chainsaw.jpeg" alt="" title="chainsaw" width="350" height="210" class="size-full wp-image-1957" /></a><p class="wp-caption-text">Chainsaws: the kill -9 of living dead</p></div>
<p>It has to do with how MongoDB handles killed operations.  When you run <code>db.killOp(3062962)</code>, MongoDB looks up operation 3062962 in a hashtable and sets its <code>killed</code> field to <code>true</code>.  However, the code running that op gets to decide whether to even check that field and how deal with it appropriately.  </p>
<p>There are basically three ways MongoDB ops handle getting killed:</p>
<ul>
<li>Ones that die when they yield whatever lock they&#8217;re holding.  This means that if the op never yields (note that numYields is 0 in the example above), it will never be killed.
<li>Ones that can be killed at certain checkpoints.  For example, index builds happen in multiple stages and check <code>killed</code> between stages.  (Many commands do this, too.)
<li>Ones cannot be killed at all.  For example, rsSync, the name for the op applying replication, falls into this category.  There are some sharding commands that can&#8217;t be killed, too.
</ul>
<p>There is no <code>kill -9</code> equivalent in MongoDB (other than <code>kill -9</code>-ing the server itself): if an op doesn&#8217;t know how to safely kill itself, it won&#8217;t die until it&#8217;s good and ready.  Which means that you can have a &#8220;killed&#8221; op in <code>db.currentOp()</code> output for a long time.  <code>killed</code> might be better named <code>killRequested</code>.</p>
<p>Also, if you kill an operation before it acquires a lock, it&#8217;ll generally start executing anyway (e.g., op 3063051 above).  For example, try opening up a shell and make the db hold the writelock for 10 minutes:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> db.<span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sleep(10*60*1000)&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>While that&#8217;s running, in another shell, try doing an insert (which will block, as the db cannot do any writes while the <code>db.eval()</code> is holding the writelock).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">foo</span>.<span style="color: #660066;">insert</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>x<span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Now, in a third shell, kill the insert we just did (before the 10 minutes elapse):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">currentOp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">&quot;inprog&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                <span style="color: #009900;">&#123;</span>
                        <span style="color: #3366CC;">&quot;opid&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">455937</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;active&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;lockType&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;W&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;waitingForLock&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;secs_running&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">25</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;op&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;query&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;ns&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;query&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #3366CC;">&quot;$eval&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sleep(10*60*1000)&quot;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;client&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;127.0.0.1:51797&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;desc&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;conn&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;threadId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0x7f241c0bf700&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;connectionId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">14477</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;locks&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #3366CC;">&quot;.&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;W&quot;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;numYields&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                <span style="color: #009900;">&#123;</span>
<span style="display:block;background-color: #ffc;">                        <span style="color: #3366CC;">&quot;opid&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">455949</span><span style="color: #339933;">,</span></span>                        <span style="color: #3366CC;">&quot;active&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;lockType&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;w&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;waitingForLock&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;op&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;insert&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;ns&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;query&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;client&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;127.0.0.1:51799&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;desc&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;conn&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;threadId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0x7f24147f8700&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;connectionId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">14478</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;locks&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #3366CC;">&quot;.&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;w&quot;</span><span style="color: #339933;">,</span>
                                <span style="color: #3366CC;">&quot;.test&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;W&quot;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;numYields&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&gt;</span> <span style="color: #006600; font-style: italic;">// get the opId of the insert from currentOp</span>
<span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">killOp</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">455949</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;info&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;attempting to kill op&quot;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&gt;</span> <span style="color: #006600; font-style: italic;">// run currentOp again to see that killed:true</span>
<span style="color: #339933;">&gt;</span> db.<span style="color: #660066;">currentOp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">&quot;inprog&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>
                <span style="color: #009900;">&#123;</span>
                        <span style="color: #3366CC;">&quot;opid&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">455937</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;active&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;lockType&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;W&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;waitingForLock&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;secs_running&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">221</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;op&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;query&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;ns&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;test&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;query&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #3366CC;">&quot;$eval&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sleep(10*60*1000)&quot;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;client&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;127.0.0.1:51797&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;desc&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;conn&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;threadId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0x7f241c0bf700&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;connectionId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">14477</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;locks&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #3366CC;">&quot;.&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;W&quot;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;numYields&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                <span style="color: #009900;">&#123;</span>
                        <span style="color: #3366CC;">&quot;opid&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">455949</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;active&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;lockType&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;w&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;waitingForLock&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;op&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;insert&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;ns&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;query&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
&nbsp;
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;client&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;127.0.0.1:51799&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;desc&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;conn&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;threadId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;0x7f24147f8700&quot;</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;connectionId&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">14478</span><span style="color: #339933;">,</span>
                        <span style="color: #3366CC;">&quot;locks&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #3366CC;">&quot;.&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;w&quot;</span><span style="color: #339933;">,</span>
                                <span style="color: #3366CC;">&quot;.test&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;W&quot;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="display:block;background-color: #ffc;">                        <span style="color: #3366CC;">&quot;killed&quot;</span> <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span></span>                        <span style="color: #3366CC;">&quot;numYields&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you wait 10 minutes for the <code>db.eval()</code> to finish, then do a find on <em>db.foo</em>, you&#8217;ll see that <code>{x:1}</code> was actually inserted anyway.  This is because the op&#8217;s lifecycle looks something like:</p>
<ul>
<li>Wait for lock
<li>Acquire lock!
<li>Start running
<li>Yield lock
<li>Check for killed
</ul>
<p>So it&#8217;ll run a bit before dying (if it can be killed at all), which may produce unintuitive results.</p>
<div class="shr-publisher-1956"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.snailinaturtleneck.com%2Fblog%2F2012%2F04%2F20%2Fnight-of-the-living-dead-ops%2F' data-shr_title='Night+of+the+Living+Dead+Ops'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom --><img src="http://feeds.feedburner.com/~r/kchodorow/~4/3mE5gn5mOrc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.snailinaturtleneck.com/blog/2012/04/20/night-of-the-living-dead-ops/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.snailinaturtleneck.com/blog/2012/04/20/night-of-the-living-dead-ops/</feedburner:origLink></item>
		<item>
		<title>––thursday #4: blockdev</title>
		<link>http://feedproxy.google.com/~r/kchodorow/~3/JpKMUgZltjc/</link>
		<comments>http://www.snailinaturtleneck.com/blog/2012/04/05/thursday-4-blockdev/#comments</comments>
		<pubDate>Thu, 05 Apr 2012 17:50:19 +0000</pubDate>
		<dc:creator>Kristina Chodorow</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[--thursday]]></category>
		<category><![CDATA[blockdev]]></category>
		<category><![CDATA[readahead]]></category>

		<guid isPermaLink="false">http://www.snailinaturtleneck.com/blog/?p=1927</guid>
		<description><![CDATA[Disk IO is slow. You just won&#8217;t believe how vastly, hugely, mind-bogglingly slow it is. I mean, you may think your network is slow, but that&#8217;s just peanuts to disk IO. The image below helps visualize how slow (post continues below). (Originally found on Hacker News and inspired by Gustavo Duarte&#8217;s blog.) The kernel knows&#8230;]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>Disk IO is slow.  You just won&#8217;t believe how vastly, hugely, mind-bogglingly slow it is. I mean, you may think your network is slow, but that&#8217;s just peanuts to disk IO.</p>
<p>The image below helps visualize how slow (post continues below). </p>
<p><a href="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/03/latency.gif"><img src="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/03/latency.gif" alt="" title="Access latency" width="1120" height="13800" class="size-full wp-image-1898" /></a></p>
<p>(Originally found on <a href="http://news.ycombinator.com/item?id=702713">Hacker News</a> and inspired by <a href="http://duartes.org/gustavo/blog/post/what-your-computer-does-while-you-wait">Gustavo Duarte&#8217;s blog</a>.)</p>
<p>The kernel knows how slow the disk is and tries to be smart about accessing it.  It not only reads the data you requested, it also returns a bit more.  This way, if you&#8217;re reading through a file or watching a movie (sequential access), your system doesn&#8217;t have to go to disk as frequently because you&#8217;re pulling more data back than you strictly requested each time.</p>
<p>You can see how far the kernel reads ahead using the <em>blockdev</em> tool:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">sudo</span> blockdev <span style="color: #660033;">--report</span>
RO    RA   SSZ   BSZ   StartSec            Size   Device
rw   <span style="color: #000000;">256</span>   <span style="color: #000000;">512</span>  <span style="color: #000000;">4096</span>          <span style="color: #000000;">0</span>     <span style="color: #000000;">80026361856</span>   <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda
rw   <span style="color: #000000;">256</span>   <span style="color: #000000;">512</span>  <span style="color: #000000;">4096</span>       <span style="color: #000000;">2048</span>     <span style="color: #000000;">80025223168</span>   <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sda1
rw   <span style="color: #000000;">256</span>   <span style="color: #000000;">512</span>  <span style="color: #000000;">4096</span>          <span style="color: #000000;">0</span>   <span style="color: #000000;">2000398934016</span>   <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb
rw   <span style="color: #000000;">256</span>   <span style="color: #000000;">512</span>  <span style="color: #000000;">1024</span>       <span style="color: #000000;">2048</span>        <span style="color: #000000;">98566144</span>   <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb1
rw   <span style="color: #000000;">256</span>   <span style="color: #000000;">512</span>  <span style="color: #000000;">4096</span>     <span style="color: #000000;">194560</span>      <span style="color: #000000;">7999586304</span>   <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb2
rw   <span style="color: #000000;">256</span>   <span style="color: #000000;">512</span>  <span style="color: #000000;">4096</span>   <span style="color: #000000;">15818752</span>     <span style="color: #000000;">19999490048</span>   <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb3
rw   <span style="color: #000000;">256</span>   <span style="color: #000000;">512</span>  <span style="color: #000000;">4096</span>   <span style="color: #000000;">54880256</span>   <span style="color: #000000;">1972300152832</span>   <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>sdb4</pre></div></div>

<p>Readahead is listed in the &#8220;RA&#8221; column.  As you can see, I have two disks (sda and sdb) with readahead set to 256 on each.  But what unit is that 256? Bytes?  Kilobytes?  Dolphins?  If we look at the man page for <em>blockdev</em>, it says:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">man</span> blockdev
...
       <span style="color: #660033;">--setra</span> N
              Set readahead to N <span style="color: #000000;">512</span>-byte sectors.
...</pre></div></div>

<p>This means that my readahead is 512 bytes*256=131072 or 128KB.  That means that, whenever I read from disk, the disk is actually reading at least 128KB of data, even if I only requested a few bytes.</p>
<p>So what value should you set your readahead to?  Please don&#8217;t set it to a number you find online without understanding the consequences.  If you Google for &#8220;blockdev setra&#8221;, the first result uses <em>blockdev &#8211;setra 65536</em>, which translates to 32MB of readahead.  That means that, whenever you read from disk, the disk is actually doing 32MB worth of work.  Please <em>do not</em> set your readahead this high if you&#8217;re doing a lot of random-access reads and writes, as all of the extra IO can slow things down a lot (and if your low on memory, you&#8217;ll be forcing the kernel to fill up your RAM with data you won&#8217;t need).  </p>
<p>Getting a good readahead value can help disk IO issues to some extent, but if you are using MongoDB (in particular), please consider your typical document size and access patterns before changing your <em>blockdev</em> settings.  I&#8217;m not recommending any particular value because what&#8217;s perfect for one application/machine can be death for another.</p>
<p>I&#8217;m really enjoying these &#8211;thursday posts because every week people have commented with different/better/interesting ways of doing what I talked about (or ways of telling the difference between <a href="http://www.snailinaturtleneck.com/blog/2012/03/15/thursday-2-diff-n-patch/#comments">stalagmites and stalactites</a>), which is really cool.  So I&#8217;m throwing this out there: how would you figure out what a good readahead setting is?  Next week I&#8217;m planning to do <em>iostat</em> for &#8211;thursday which should cover this a bit, but please leave a comment if you have any ideas.</p>
<div class="shr-publisher-1927"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.snailinaturtleneck.com%2Fblog%2F2012%2F04%2F05%2Fthursday-4-blockdev%2F' data-shr_title='%E2%80%93%E2%80%93thursday+%234%3A+blockdev'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom --><img src="http://feeds.feedburner.com/~r/kchodorow/~4/JpKMUgZltjc" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.snailinaturtleneck.com/blog/2012/04/05/thursday-4-blockdev/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		<feedburner:origLink>http://www.snailinaturtleneck.com/blog/2012/04/05/thursday-4-blockdev/</feedburner:origLink></item>
		<item>
		<title>––thursday #3: a handy git prompt</title>
		<link>http://feedproxy.google.com/~r/kchodorow/~3/jr9yJlx7fwk/</link>
		<comments>http://www.snailinaturtleneck.com/blog/2012/03/29/thursday-3-a-handy-git-prompt/#comments</comments>
		<pubDate>Thu, 29 Mar 2012 18:15:18 +0000</pubDate>
		<dc:creator>Kristina Chodorow</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[--thursday]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[prompt]]></category>

		<guid isPermaLink="false">http://www.snailinaturtleneck.com/blog/?p=1891</guid>
		<description><![CDATA[Everyone says to use git branches early and often, but I inevitably lose track of what branch I&#8217;m on. My workflow generally goes something like: Check out a branch Lunch! Get back to my desk and make an emergency bug fix Commit emergency fix Suddenly realize I&#8217;m not on the branch I meant to be&#8230;]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>Everyone says to use git branches early and often, but I inevitably lose track of what branch I&#8217;m on.  My workflow generally goes something like:</p>
<ol>
<li>Check out a branch
<li>Lunch!
<li>Get back to my desk and make an emergency bug fix
<li>Commit emergency fix
<li>Suddenly realize I&#8217;m not on the branch I meant to be on, optimally (but not always) before I try to push
</ol>
<p>To keep track, I&#8217;ve modified my command prompt to display what I&#8217;m working on using <em>__git_ps1</em>, which prints a nice &#8220;you are here&#8221; string for your prompt:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ __git_ps1
 <span style="color: #7a0874; font-weight: bold;">&#40;</span>master<span style="color: #7a0874; font-weight: bold;">&#41;</span>
$ <span style="color: #c20cb9; font-weight: bold;">git</span> checkout myTag123
$ __git_ps1
 <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>myTag123<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>(Newlines added for clarity, it actually displays as &#8221; (master)$ git checkout&#8230;&#8221;, which is generally what you want so your prompt doesn&#8217;t contain a newline.)  I&#8217;m not sure what&#8217;s up with all the &#8216;()&#8217;s, but it does let you distinguish between branches (&#8216;(branchname)&#8217;) and tags (&#8216;((tagname))&#8217;).</p>
<p><em>__git_ps1</em> will even show you if you&#8217;re in the middle of a bisect or a merge, which is another common workflow for me: </p>
<ol>
<li>Merge something and there are a ton of conflicts
<li>Boldly decide to deal with it tomorrow and go home for the day
<li>The next day, write and emergency patch for something
<li>Check <em>git status</em>
<li>Suddenly I remember that I&#8217;m in the middle of a merge
<li>Do unpleasant surgery to get my git repo back to a sane state
</ol>
<p>If you tend to fall into a similar workflow, I highly recommend modifying your prompt to something like:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #007800;">PS1</span>=<span style="color: #ff0000;">'\w$(__git_ps1)&gt; '</span>
~<span style="color: #000000; font-weight: bold;">/</span>gitroot<span style="color: #000000; font-weight: bold;">/</span>mongo <span style="color: #7a0874; font-weight: bold;">&#40;</span>v2.0<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>And suddenly life is better, which is what Linux is all about.</p>
<p>You can run <code>PS1=...</code> on a per-shell basis (or <code>export PS1</code> for all shells this session) or add that line to your <em>~/.bashrc</em> file to get that prompt in all future shells.</p>
<p>Finally, <em>__git_ps1</em> isn&#8217;t a binary in your path, it&#8217;s a function (so it won&#8217;t show up if you run <em>which __git_ps1</em>).  You can see its source by running <em>type __git_ps1</em>.</p>
<p><em>Note: unfortunately this doesn&#8217;t work for me on OS X, so I think it might be Linux-only.</em></p>
<div class="shr-publisher-1891"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.snailinaturtleneck.com%2Fblog%2F2012%2F03%2F29%2Fthursday-3-a-handy-git-prompt%2F' data-shr_title='%E2%80%93%E2%80%93thursday+%233%3A+a+handy+git+prompt'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom --><img src="http://feeds.feedburner.com/~r/kchodorow/~4/jr9yJlx7fwk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.snailinaturtleneck.com/blog/2012/03/29/thursday-3-a-handy-git-prompt/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.snailinaturtleneck.com/blog/2012/03/29/thursday-3-a-handy-git-prompt/</feedburner:origLink></item>
		<item>
		<title>Debugging Sexism</title>
		<link>http://feedproxy.google.com/~r/kchodorow/~3/L6G4gk1ocTE/</link>
		<comments>http://www.snailinaturtleneck.com/blog/2012/03/22/debugging-sexism/#comments</comments>
		<pubDate>Thu, 22 Mar 2012 21:07:37 +0000</pubDate>
		<dc:creator>Kristina Chodorow</dc:creator>
				<category><![CDATA[Personal]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://www.snailinaturtleneck.com/blog/?p=1899</guid>
		<description><![CDATA[The word of the day is sexism: Shanley Kane tweeted the CTO of Geekli.st, asking them to take down a video of a woman in a Geekli.st top and underwear dancing around. The subsequent tweets were captured in a long Storify and Geekli.st subsequently issued a public apology. The interesting thing about this, to me,&#8230;]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><div id="attachment_1910" class="wp-caption alignright" style="width: 330px"><a href="http://www.flickr.com/photos/stevenfettig/1390275600/sizes/m/in/photostream/"><img src="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/03/rodin.jpg" alt="" title="rodin" width="320" height="213" class="size-full wp-image-1910" /></a><p class="wp-caption-text">Photo by  Steven Fettig, some rights reserved</p></div>
<p>The word of the day is sexism: <a href="https://twitter.com/#!/shanley">Shanley Kane</a> tweeted the CTO of Geekli.st, asking them to take down a <a href="http://vimeo.com/27758336">video</a> of a woman in a Geekli.st top and underwear dancing around.  The subsequent tweets were captured in a long <a href="http://storify.com/charlesarthur/oh-hai-sexism">Storify</a> and Geekli.st subsequently issued a <a href="http://gklst.tumblr.com/post/19734620901/geeklist-and-a-public-apology">public apology</a>.</p>
<p>The interesting thing about this, to me, is how often well-meaning geeks react badly when someone says that they did something sexist, racist, homophobic, etc. </p>
<p>Let&#8217;s say someone says to you, in a public forum, &#8220;This thing you did is sexist.&#8221;  What should you do?</p>
<p>First, if your immediate reaction is to say, &#8220;No it&#8217;s not!&#8221; don&#8217;t give in to your immediate reaction. That seems to be what most people start off with and it doesn&#8217;t help anything.  </p>
<p>The reason you&#8217;re probably so eager to say <em>it&#8217;s</em> not sexist is because <em>you&#8217;re</em> not sexist.  (Let&#8217;s assume, if you&#8217;re reading this blog, that you are correct: you are not sexist.)</p>
<p>So, if you aren&#8217;t sexist, why can&#8217;t you flat-out say &#8220;You are wrong&#8221;?  Well, for starters, just because you&#8217;re not sexist doesn&#8217;t mean you never do anything sexist.  Besides, they&#8217;re saying &#8220;This thing you did is sexist&#8221; but what they <em>mean</em> is &#8220;<b>I feel</b> this thing you did is sexist.&#8221;  And even if you&#8217;re Simone de Beauvoir and Margaret Atwood and Buffy the Vampire Slayer rolled into one, you cannot tell them their feelings are wrong (well, you <em>can</em>, but you&#8217;ll look like an ass). What should you do, then?  </p>
<p>You should try to make them feel better and try to avoid hurting them in the future.</p>
<ul>
<li>Step 1: <em>try to make them feel better.</em>  The best way to do this is through an apology.  A real one, not an &#8220;I&#8217;m sorry this offended you&#8221;-type apology.  A better choice: &#8220;I&#8217;m sorry that I did that.&#8221;  Now follow up with, &#8220;What can I do to make this better?&#8221;
<li>Step 2: <em>try to avoid hurting them in the future.</em> Obviously, everyone makes mistakes. You can&#8217;t never make one again, even if you and your company were publicly humiliated the first time around.  The important thing is to try to learn from it.  Google around and read about the issues women and minorities are concerned about in geek culture.  Even if you don&#8217;t agree with everything everyone writes (I certainly don&#8217;t), you will at least know what the issues are.
</ul>
<p>If someone tells you they&#8217;re offended, you should take that seriously.  If they&#8217;re speaking up, there are probably many others who are silently offended.</p>
<div class="shr-publisher-1899"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.snailinaturtleneck.com%2Fblog%2F2012%2F03%2F22%2Fdebugging-sexism%2F' data-shr_title='Debugging+Sexism'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom --><img src="http://feeds.feedburner.com/~r/kchodorow/~4/L6G4gk1ocTE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.snailinaturtleneck.com/blog/2012/03/22/debugging-sexism/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.snailinaturtleneck.com/blog/2012/03/22/debugging-sexism/</feedburner:origLink></item>
		<item>
		<title>––thursday #2: diff ‘n patch</title>
		<link>http://feedproxy.google.com/~r/kchodorow/~3/GjovCwJY9JE/</link>
		<comments>http://www.snailinaturtleneck.com/blog/2012/03/15/thursday-2-diff-n-patch/#comments</comments>
		<pubDate>Thu, 15 Mar 2012 14:44:15 +0000</pubDate>
		<dc:creator>Kristina Chodorow</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[--thursday]]></category>
		<category><![CDATA[diff]]></category>
		<category><![CDATA[patch]]></category>

		<guid isPermaLink="false">http://www.snailinaturtleneck.com/blog/?p=1882</guid>
		<description><![CDATA[I’m trying something new: every Thursday I’ll do a short post on how to do something with the command line. I always seem to either create or apply patches in the wrong direction. It&#8217;s like stalagmites vs. stalactites, which I struggled with until I heard the nemonic: &#8220;Stalagmites might hang from the ceiling&#8230; but they&#8230;]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p><em>I’m trying something new: every Thursday I’ll do a short post on how to do something with the command line.</em></p>
<p>I always seem to either create or apply patches in the wrong direction.  It&#8217;s like stalagmites vs. stalactites, which I struggled with until I heard the nemonic: &#8220;Stalag<em>mite</em>s <em>might</em> hang from the ceiling&#8230; but they don&#8217;t.&#8221; </p>
<p>Moving right along, you can use diff to get line-by-line changes between any two files.  Generally I use <em>git diff</em> because I&#8217;m dealing with a git repo, so that&#8217;s what I&#8217;ll use here. </p>
<p>Let&#8217;s get a diff of MongoDB between version 2.0.2 and 2.0.3.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #c20cb9; font-weight: bold;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>mongodb<span style="color: #000000; font-weight: bold;">/</span>mongo.git
$ <span style="color: #7a0874; font-weight: bold;">cd</span> mongo
$ <span style="color: #c20cb9; font-weight: bold;">git</span> <span style="color: #c20cb9; font-weight: bold;">diff</span> r2.0.2..r2.0.3 <span style="color: #000000; font-weight: bold;">&gt;</span> mongo.patch</pre></div></div>

<p>This takes all of the changes between 2.0.2 and 2.0.3 (<em>r2.0.2..r2.0.3</em>) and dumps them into a file called <em>mongo.patch</em> (that&#8217;s the <em>&gt; mongo.patch</em> part).</p>
<p>Now, let&#8217;s get the code from 2.0.2 and apply <em>mongo.patch</em>, effectively making it 2.0.3 (this is kind of a silly example but if you&#8217;re still with me after the stalagmite thing, I assume you don&#8217;t mind silly examples):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">git</span> checkout r2.0.2
Note: checking out <span style="color: #ff0000;">'r2.0.2'</span>.
&nbsp;
You are <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #ff0000;">'detached HEAD'</span> state. You can look around, <span style="color: #c20cb9; font-weight: bold;">make</span> experimental
changes and commit them, and you can discard any commits you <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #000000; font-weight: bold;">in</span> this
state without impacting any branches by performing another checkout.
&nbsp;
If you want to create a new branch to retain commits you create, you may
<span style="color: #000000; font-weight: bold;">do</span> so <span style="color: #7a0874; font-weight: bold;">&#40;</span>now or later<span style="color: #7a0874; font-weight: bold;">&#41;</span> by using <span style="color: #660033;">-b</span> with the checkout <span style="color: #7a0874; font-weight: bold;">command</span> again. Example:
&nbsp;
  <span style="color: #c20cb9; font-weight: bold;">git</span> checkout <span style="color: #660033;">-b</span> new_branch_name
&nbsp;
HEAD is now at 514b122... BUMP 2.0.2
$ 
$ <span style="color: #c20cb9; font-weight: bold;">patch</span> <span style="color: #660033;">-p1</span> <span style="color: #000000; font-weight: bold;">&lt;</span> mongo.patch</pre></div></div>

<p>What intuitive syntax!</p>
<p>What does the <em>-p1</em> mean?  How many forward slashes to remove from the path in the patch, of course.  </p>
<p>To take an example, if you look at the last 11 lines of the patch, you can see that it is the diff for the file that changes the version number.  It looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">$ tail -n 11 mongo.patch
--- a/util/version.cpp
+++ b/util/version.cpp
@@ -38,7 +38,7 @@ namespace mongo {
      *      1.2.3-rc4-pre-
      * If you really need to do something else you'll need to fix _versionArray()
      */
-    const char versionString[] = &quot;2.0.2&quot;;
+    const char versionString[] = &quot;2.0.3&quot;;
&nbsp;
     // See unit test for example outputs
     static BSONArray _versionArray(const char* version){</pre></div></div>

<p>Note the <em>a/util/version.cpp</em> and <em>b/util/version.cpp</em>.  These indicate the file the patch should be applied to, but there are no <em>a</em> or <em>b</em> directories in the MongoDB repository.  The <em>a</em> and <em>b</em> prefixes indicate that one is the previous version and one is the new version.  And <em>-p</em> says how many slashes to strip from this path.  An example may make this clearer:</p>
<ul>
<li><em>-p0</em> (equivalent to not specifying <em>-p</em>): &#8220;apply this patch to <em>a/util/version.cpp</em>&#8221; (which doesn&#8217;t exist)
<li><em>-p1</em>: &#8220;apply this patch to <em>util/version.cpp</em>&#8221; ← bingo, that&#8217;s what we want
<li><em>-p2</em>: &#8220;apply this patch to <em>version.cpp</em>&#8221; (which doesn&#8217;t exist)
</ul>
<p>So, we use <em>-p1</em>, because that makes the patch&#8217;s paths match the actually directory structure.  If someone sent you a patch and the path is something like <em>/home/bob/bobsStuff/foo.txt</em> and your name is not Bob, you&#8217;re just trying to patch <em>foo.txt</em>, you&#8217;d probably want to use <em>-p4</em>.</p>
<p>On the plus side, if you&#8217;re using patches generated by git, they&#8217;re super-easy to apply. Git chose the intuitive verb &#8220;apply&#8221; to patch a file.  If you have a patch generated by <em>git diff</em>, you can patch your current tree with:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">git</span> apply mongo.patch</pre></div></div>

<p>So, aside from the stupid choice of verbiage, that is generally easier.</p>
<p>Did I miss anything? Get anything wrong? Got a suggestion for next week? Leave a comment below and let me know!</p>
<div class="shr-publisher-1882"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.snailinaturtleneck.com%2Fblog%2F2012%2F03%2F15%2Fthursday-2-diff-n-patch%2F' data-shr_title='%E2%80%93%E2%80%93thursday+%232%3A+diff+%27n+patch'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom --><img src="http://feeds.feedburner.com/~r/kchodorow/~4/GjovCwJY9JE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.snailinaturtleneck.com/blog/2012/03/15/thursday-2-diff-n-patch/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://www.snailinaturtleneck.com/blog/2012/03/15/thursday-2-diff-n-patch/</feedburner:origLink></item>
		<item>
		<title>––thursday #1: screen</title>
		<link>http://feedproxy.google.com/~r/kchodorow/~3/EN92Kve5U-o/</link>
		<comments>http://www.snailinaturtleneck.com/blog/2012/03/08/thursday-1-screen/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 20:43:37 +0000</pubDate>
		<dc:creator>Kristina Chodorow</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[--thursday]]></category>
		<category><![CDATA[screen]]></category>

		<guid isPermaLink="false">http://www.snailinaturtleneck.com/blog/?p=1864</guid>
		<description><![CDATA[I&#8217;m trying something new: every Thursday I&#8217;ll go over how to do something with the command line. Let me know what you think. If you are using a modern-ish browser, you probably use tabs to keep multiple things open at once: your email, your calendar, whatever you&#8217;re actually doing, etc. You can do the same&#8230;]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p><em>I&#8217;m trying something new: every Thursday I&#8217;ll go over how to do something with the command line. Let me know what you think.</em></p>
<p>If you are using a modern-ish browser, you probably use tabs to keep multiple things open at once: your email, your calendar, whatever you&#8217;re actually doing, etc.  You can do the same thing with the shell using <em>screen</em>: in a single terminal, you can compile a program while you&#8217;re editing a file and watching another process out of the corner of your eye.</p>
<p>Note that <em>screen</em> is super handy when SSH&#8217;d into a box.  SSH in once, then start screen and open up all of the windows you need.</p>
<h4>Using <em>screen</em></h4>
<p>To start up <em>screen</em>, run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">screen</span></pre></div></div>

<p>Now your shell will clear and <em>screen</em> will give you a welcome message.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&nbsp;
Screen version 4.00.03jw4 (FAU) 2-May-06
&nbsp;
Copyright (c) 1993-2002 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann
&nbsp;
...
&nbsp;
&nbsp;
&nbsp;
&nbsp;
                          [Press Space or Return to end.]</pre></div></div>

<p>As it says at the bottom, just hit Return to clear the welcome message.  Now you&#8217;ll see an empty prompt and you can start working normally.  </p>
<p>Let&#8217;s say we have three things we want to do:</p>
<ol>
<li>Run <em>top</em>
<li>Edit a file
<li>Tail a log
</ol>
<p>Go ahead and start up <em>top</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ top</pre></div></div>

<p>Well, now we need to edit a file but <em>top</em>&#8216;s using the shell.  What to do now?  Just create a new window.   While <em>top</em> is still running, hit <code>^A c</code> (I&#8217;m using ^A as shorthand for Control-a, so this means &#8220;hit Control-a, then hit c&#8221;) to create a new window.  The new window gets put right on top of the old one, so you&#8217;ll see a fresh shell and be at the prompt again.  But where did <em>top</em> go?  Not to worry, it&#8217;s still there.  We can switch back to it with <code>^A n</code> or <code>^A p</code> (next or previous window). </p>
<p>Now we can start up our editor and begin editing a file.  But now we want to tail a file, so we create another new window with <code>^A c</code> and run our <code>tail -f <em>filename</em></code>.  We can continue to use <code>^A n</code> and <code>^A p</code> to switch between the three things we&#8217;re doing (and open more windows as necessary).</p>
<h4>Availability</h4>
<p><em>screen</em> seems pretty ubiquitous, it has been on every Linux machine I&#8217;ve ever tried running it on and even OS X (although it may be part of XCode, I haven&#8217;t checked).</p>
<h4>Note for Emacs Users</h4>
<p><code>^A</code> is an annoying escape key, as it is also go-to-beginning-of-line shortcut in Emacs (and the shell).  To fix this, create a <em>.screenrc</em> file and add one line to change this to something else:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;"># use ^T
escape ^Tt
# or ^Y
escape ^Yy</pre></div></div>

<p>The escape sequence is 3 characters: carat, T, and t.  (It is not using the single special character &#8220;^T&#8221;.)  The traditional escape key is actually Ctrl-^, as the carat is the one character Emacs doesn&#8217;t use for anything.  In a <em>.screenrc</em> file, this results in the rather bizarre string:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">escape ^^^^</pre></div></div>

<p>&#8230;which makes sense when you think about it, but looks a bit weird. </p>
<h4>Odds and Ends</h4>
<p>As long as you&#8217;re poking at the <em>.screenrc</em> file, you might want to turn off the welcome message, too:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">startup_message off</pre></div></div>

<p>Run <code>^A ?</code> anytime for help, or check out the manual&#8217;s list of <a href="http://www.gnu.org/software/screen/manual/html_node/Default-Key-Bindings.html#Default-Key-Bindings">default bindings</a>.</p>
<p>Did I miss anything?  Get anything wrong?  Got a suggestion for next week?  Leave a comment below and let me know!</p>
<div class="shr-publisher-1864"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.snailinaturtleneck.com%2Fblog%2F2012%2F03%2F08%2Fthursday-1-screen%2F' data-shr_title='%E2%80%93%E2%80%93thursday+%231%3A+screen'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom --><img src="http://feeds.feedburner.com/~r/kchodorow/~4/EN92Kve5U-o" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.snailinaturtleneck.com/blog/2012/03/08/thursday-1-screen/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		<feedburner:origLink>http://www.snailinaturtleneck.com/blog/2012/03/08/thursday-1-screen/</feedburner:origLink></item>
		<item>
		<title>Replica Set Internals Bootcamp: Part II – Creating a Set</title>
		<link>http://feedproxy.google.com/~r/kchodorow/~3/-6EdwbJ16WQ/</link>
		<comments>http://www.snailinaturtleneck.com/blog/2012/03/06/replica-set-internals-bootcamp-part-ii-creating-a-set/#comments</comments>
		<pubDate>Tue, 06 Mar 2012 21:02:07 +0000</pubDate>
		<dc:creator>Kristina Chodorow</dc:creator>
				<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[bootcamp]]></category>
		<category><![CDATA[internals]]></category>

		<guid isPermaLink="false">http://www.snailinaturtleneck.com/blog/?p=1767</guid>
		<description><![CDATA[I&#8217;ve been doing replica set &#8220;bootcamps&#8221; for new hires. It&#8217;s mainly focused on applying this to debug replica set issues and being able to talk fluently about what&#8217;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&#8230;]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop --><!-- End Shareaholic LikeButtonSetTop --><p>I&#8217;ve been doing replica set &#8220;bootcamps&#8221; for new hires. It&#8217;s mainly focused on applying this to debug replica set issues and being able to talk fluently about what&#8217;s happening, but it occurred to me that you (blog readers) might be interested in it, too.  </p>
<p><img src="http://www.snailinaturtleneck.com/blog/wp-content/uploads/2012/01/dog-boot-camp-2.jpg" alt="" title="dog-boot-camp" width="265" height="286" class="alignright size-full wp-image-1760" /></p>
<p>There are 8 subjects I cover in my bootcamp: </p>
<ol>
<li><a href="http://www.snailinaturtleneck.com/blog/?p=1752">Elections</a>
<li><a href="http://www.snailinaturtleneck.com/blog/?p=1767">Creating a set</a>
<li>Reconfiguring
<li>Syncing
<li>Initial Sync
<li>Rollback
<li>Authentication
<li>Debugging
</ol>
<p>I&#8217;m going to do one subject per post, we&#8217;ll see how many I can get through.</p>
<p><em>Prerequisites: I&#8217;m assuming you know what replica sets are and you&#8217;ve configured a set, written data to it, read from a secondary, etc.  You understand the terms primary and secondary.</em>  </p>
<h4>Initializing a Set</h4>
<p>Suppose you are creating a new set.  You start up some <em>mongod</em>s with no data and the <em>replSet</em> option.  When a server starts up with the <em>replSet</em> option, the first thing it does is check its <em>local.system.replset</em> collection for a replica set config it can load, but it won&#8217;t find one (because there&#8217;s no data).  Since it cannot find a config, it goes into the <em>EMPTYCONFIG</em> state. If you look in the logs, you&#8217;ll see messages about <em>EMPTYCONFIG</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:24:35 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Tue Mar  6 12:24:35 [rsStart] replSet info you may need to run replSetInitiate -- rs.initiate() in the shell -- if that is not already done
Tue Mar  6 12:24:45 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Tue Mar  6 12:24:55 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Tue Mar  6 12:25:05 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Tue Mar  6 12:25:15 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Tue Mar  6 12:25:25 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)</pre></div></div>

<p>Now suppose you send the <em>replSetInitiate</em> command to one of the members.  <em>replSetInitiate</em> can either take a custom configuration or generate a config automatically. If you do not pass in a config, the server will try to figure out what its hostname is and generate a config using that.  </p>
<p><em>Note: on EC2, the server <b>always</b> chooses the wrong hostname, so you have to pass in the config you want.</em></p>
<p>Once the server has a config document, either passed in or generated, it&#8217;ll make sure it can reach every node specified (all members must be up and reachable to initiate), then store the config in the <em>local.system.replset</em> collection.  Finally, it begins sending heartbeat requests to the other members of the set.  </p>
<h4>The Log: Step-by-Step</h4>
<p>Being able to interpret log files is critical to knowing what&#8217;s going on, so let&#8217;s walk through a sample log from running <code>rs.initiate()</code>.  Here&#8217;s the whole log, feel free to skip over this for now.  We&#8217;ll take it piece-by-piece below.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:00 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)
Tue Mar  6 12:45:02 [initandlisten] connection accepted from 127.0.0.1:38839 #2 (2 connections now open)
Tue Mar  6 12:45:08 [conn2] replSet replSetInitiate admin command received from client
Tue Mar  6 12:45:08 [conn2] replSet info initiate : no configuration specified.  Using a default configuration for the set
Tue Mar  6 12:45:08 [conn2] replSet created this configuration for initiation : { _id: &quot;foo&quot;, members: [ { _id: 0, host: &quot;ubuntu:30000&quot; } ] }
Tue Mar  6 12:45:08 [conn2] replSet replSetInitiate config object parses ok, 1 members specified
Tue Mar  6 12:45:08 [conn2] replSet replSetInitiate all members seem up
Tue Mar  6 12:45:08 [conn2] ******
Tue Mar  6 12:45:08 [conn2] creating replication oplog of size: 49085MB...
Tue Mar  6 12:45:08 [FileAllocator] allocating new datafile /datadir/local.ns, filling with zeroes...
Tue Mar  6 12:45:08 [FileAllocator] creating directory /datadir/_tmp
Tue Mar  6 12:45:08 [FileAllocator] done allocating datafile /datadir/local.ns, size: 16MB,  took 0.016 secs
Tue Mar  6 12:45:08 [FileAllocator] allocating new datafile /datadir/local.0, filling with zeroes...
Tue Mar  6 12:45:08 [FileAllocator] done allocating datafile /datadir/local.0, size: 2047MB,  took 0.016 secs
Tue Mar  6 12:45:08 [conn2] datafileheader::init initializing /datadir/local.0 n:0
Tue Mar  6 12:45:08 [FileAllocator] allocating new datafile /datadir/local.1, filling with zeroes...
...
Tue Mar  6 12:45:09 [conn2] datafileheader::init initializing /datadir/local.22 n:22
Tue Mar  6 12:45:09 [FileAllocator] allocating new datafile /datadir/local.23, filling with zeroes...
Tue Mar  6 12:45:09 [FileAllocator] done allocating datafile /datadir/local.23, size: 2047MB,  took 0.042 secs
Tue Mar  6 12:45:09 [conn2] datafileheader::init initializing /datadir/local.23 n:23
Tue Mar  6 12:45:10 [conn2] ******
Tue Mar  6 12:45:10 [conn2] replSet info saving a newer config version to local.system.replset
Tue Mar  6 12:45:10 [conn2] replSet saveConfigLocally done
Tue Mar  6 12:45:10 [conn2] replSet replSetInitiate config now saved locally.  Should come online in about a minute.
Tue Mar  6 12:45:10 [conn2] command admin.$cmd command: { replSetInitiate: undefined } ntoreturn:1 keyUpdates:0 reslen:196 2802ms
Tue Mar  6 12:45:10 [rsStart] replSet load config ok from self
Tue Mar  6 12:45:10 [rsStart] replSet I am ubuntu:30000
Tue Mar  6 12:45:10 [rsStart] replSet STARTUP2
Tue Mar  6 12:45:10 [rsSync] replSet SECONDARY
Tue Mar  6 12:45:25 [rsSync] waiting for 2 pings from other members before syncing
Tue Mar  6 12:45:26 [rsMgr] replSet info electSelf 0
Tue Mar  6 12:45:26 [rsMgr] replSet PRIMARY</pre></div></div>

<p>Now, to understand this line-by-line. First, we start out in the <em>EMPTYCONFIG</em> state, as described above.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:00 [rsStart] replSet can't get local.system.replset config from self or any seed (EMPTYCONFIG)</pre></div></div>

<p>Now the shell connects to the server and runs <code>rs.initiate()</code> (<em>replSetInitiate</em>), which apparently takes me 6 seconds to type (take that, Mavis Beacon).</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:02 [initandlisten] connection accepted from 127.0.0.1:38839 #2 (2 connections now open)
Tue Mar  6 12:45:08 [conn2] replSet replSetInitiate admin command received from client</pre></div></div>

<p>Because we didn&#8217;t give a config, the server tries to generate one:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:08 [conn2] replSet info initiate : no configuration specified.  Using a default configuration for the set
Tue Mar  6 12:45:08 [conn2] replSet created this configuration for initiation : { _id: &quot;foo&quot;, members: [ { _id: 0, host: &quot;ubuntu:30000&quot; } ] }</pre></div></div>

<p>Now it does some sanity checks on this config (e.g., making sure all members are up and reachable).  This matters more if we gave it a config, but sometimes things are so screwed up you can&#8217;t reach yourself.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:08 [conn2] replSet replSetInitiate config object parses ok, 1 members specified
Tue Mar  6 12:45:08 [conn2] replSet replSetInitiate all members seem up</pre></div></div>

<p>Now it calculates the size of the oplog and allocates it between two lines of ******.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:08 [conn2] ******
Tue Mar  6 12:45:08 [conn2] creating replication oplog of size: 49085MB...
Tue Mar  6 12:45:08 [FileAllocator] allocating new datafile /datadir/local.ns, filling with zeroes...
Tue Mar  6 12:45:08 [FileAllocator] creating directory /datadir/_tmp
Tue Mar  6 12:45:08 [FileAllocator] done allocating datafile /datadir/local.ns, size: 16MB,  took 0.016 secs
Tue Mar  6 12:45:08 [FileAllocator] allocating new datafile /datadir/local.0, filling with zeroes...
Tue Mar  6 12:45:08 [FileAllocator] done allocating datafile /datadir/local.0, size: 2047MB,  took 0.016 secs
Tue Mar  6 12:45:08 [conn2] datafileheader::init initializing /datadir/local.0 n:0
Tue Mar  6 12:45:08 [FileAllocator] allocating new datafile /datadir/local.1, filling with zeroes...
...
Tue Mar  6 12:45:09 [conn2] datafileheader::init initializing /datadir/local.22 n:22
Tue Mar  6 12:45:09 [FileAllocator] allocating new datafile /datadir/local.23, filling with zeroes...
Tue Mar  6 12:45:09 [FileAllocator] done allocating datafile /datadir/local.23, size: 2047MB,  took 0.042 secs
Tue Mar  6 12:45:09 [conn2] datafileheader::init initializing /datadir/local.23 n:23
Tue Mar  6 12:45:10 [conn2] ******</pre></div></div>

<p>It doesn&#8217;t actually print the &#8220;&#8230;&#8221; part, I just omitted 21 files being preallocated.</p>
<p>Now it stores the configuration in the <em>local.system.replset</em> collection.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:10 [conn2] replSet info saving a newer config version to local.system.replset
Tue Mar  6 12:45:10 [conn2] replSet saveConfigLocally done
Tue Mar  6 12:45:10 [conn2] replSet replSetInitiate config now saved locally.  Should come online in about a minute.</pre></div></div>

<p>It seldom takes a minute for it to come online, but this way you&#8217;ll be pleasantly surprised.</p>
<p><em>replSetInitiate</em> is now done running.  It will often be logged, because slow operations are logged and allocating the oplog usually takes a while.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:10 [conn2] command admin.$cmd command: { replSetInitiate: undefined } ntoreturn:1 keyUpdates:0 reslen:196 2802ms</pre></div></div>

<p>Now it actually loads this configuration as the replica set config we want to use:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:10 [rsStart] replSet load config ok from self</pre></div></div>

<p>You won&#8217;t see this next line if you&#8217;re running 2.0, I added it for 2.2 because I was sick of having to decipher which server a log was from:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:10 [rsStart] replSet I am ubuntu:30000</pre></div></div>

<p>Yay, now config has been loaded, making our state <em>STARTUP2</em> (the &#8220;config has been loaded, starting up RS threads&#8221; state):</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:10 [rsStart] replSet STARTUP2</pre></div></div>

<p>At this point, it can go straight into <em>PRIMARY</em> state, but it doesn&#8217;t.  We could fix this, but it hasn&#8217;t been a priority, so it goes into <em>SECONDARY</em> state briefly before becoming <em>PRIMARY</em>:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:10 [rsSync] replSet SECONDARY</pre></div></div>

<p>A &#8220;real&#8221; secondary (we&#8217;re about to become primary, so we don&#8217;t count) will gather some stats about who&#8217;s best to sync from before it chooses a target:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:25 [rsSync] waiting for 2 pings from other members before syncing</pre></div></div>

<p>Good luck with that, buddy, you&#8217;re the only one in the config.</p>
<p>Aha, we&#8217;ve noticed that we can become primary:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Tue Mar  6 12:45:26 [rsMgr] replSet info electSelf 0
Tue Mar  6 12:45:26 [rsMgr] replSet PRIMARY</pre></div></div>

<p>And now we&#8217;re all set.</p>
<h4>Propegating the Config</h4>
<p>If we add members or provide an initial config with multiple members, the initial member&#8217;s heartbeat requests are sent to a bunch of servers in <em>EMPTYCONFIG</em> state. When these other servers receive the heartbeat request, they notice &#8220;Hey, I don&#8217;t have any config and this guy seems to think I&#8217;m part of his set, so I should check in with him.&#8221;  </p>
<p>These <em>EMPTYCONFIG</em> members will then request a config from the member that requested the heartbeat.  Seeing themselves in the config, they&#8217;ll save it to their own <em>local.system.replset</em> collection, then start sending out their own heartbeats.  At that point, they&#8217;ll move into <em>RECOVERING</em> state and, once they&#8217;re synced to the original member, they&#8217;ll turn into secondaries (unless they&#8217;re arbiters, of course, but same general idea).</p>
<p>One quirk of initiating a set is that every member of the set must not have any data, other than the one you&#8217;re sending <em>replSetInitiate</em> to.  After the set is initialized you can add members containing data, but <em>at most one</em> member can have data when you initialize the set.</p>
<h4>Confused?</h4>
<p>Feel free to ask questions in the comments below. This is a loving, caring bootcamp (as bootcamps go).</p>
<div class="shr-publisher-1767"></div><!-- Start Shareaholic LikeButtonSetBottom --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.snailinaturtleneck.com%2Fblog%2F2012%2F03%2F06%2Freplica-set-internals-bootcamp-part-ii-creating-a-set%2F' data-shr_title='Replica+Set+Internals+Bootcamp%3A+Part+II+-+Creating+a+Set'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom --><img src="http://feeds.feedburner.com/~r/kchodorow/~4/-6EdwbJ16WQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.snailinaturtleneck.com/blog/2012/03/06/replica-set-internals-bootcamp-part-ii-creating-a-set/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://www.snailinaturtleneck.com/blog/2012/03/06/replica-set-internals-bootcamp-part-ii-creating-a-set/</feedburner:origLink></item>
	</channel>
</rss><!-- Dynamic page generated in 8.094 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-15 08:58:59 -->

