<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	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/"
	>

<channel>
	<title>schmichael&#039;s blog &#187; Technology</title>
	<atom:link href="http://blog.schmichael.com/category/technology/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.schmichael.com</link>
	<description>good good study, day day up</description>
	<lastBuildDate>Wed, 16 May 2012 23:59:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Failing with MongoDB</title>
		<link>http://blog.schmichael.com/2011/11/05/failing-with-mongodb/</link>
		<comments>http://blog.schmichael.com/2011/11/05/failing-with-mongodb/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 19:06:03 +0000</pubDate>
		<dc:creator>Michael Schurter</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[mongodb]]></category>

		<guid isPermaLink="false">http://blog.schmichael.com/?p=979</guid>
		<description><![CDATA[Update: Sorry this isn&#8217;t my best piece of writing and there seems to be some confusion. The dataset in question was first in a 1.8 master/slave pair and then migrated to sharded replica sets and 2.0.0. For a bit of &#8230; <a href="http://blog.schmichael.com/2011/11/05/failing-with-mongodb/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><b>Update:</b> Sorry this isn&#8217;t my best piece of writing and there seems to be some confusion. The dataset in question was first in a 1.8 master/slave pair and then migrated to sharded replica sets and 2.0.0.</p>
<p>For a bit of history of my dealings with MongoDB at Urban Airship, I gave a couple versions of a Scaling with MongoDB talk:</p>
<ul>
<li>
<a href="http://opensourcebridge.org/2011/wiki/Scaling_with_MongoDB">at Open Source Bridge (latest &#038; greatest)</a>
</li>
<li><a href="http://blog.schmichael.com/2011/02/02/schmongodb-slides-from-update-portland/">at Update Portland (original, less polished version)</a></li>
</ul>
<p>My coworker Adam Lowry even gave a follow-up talk of sorts at <a href="http://postgresopen.org/2011/schedule/presentations/98/">Postgres Open 2011</a> (<a href="http://wiki.postgresql.org/images/7/7f/Adam-lowry-postgresopen2011.pdf">slides</a>) about migrating one of our datasets off of MongoDB and (back) on to PostgreSQL.</p>
<p>After reading through those slides you&#8217;re probably wondering why we&#8217;re still dealing with MongoDB at all. We fully intended to migrate our data out of it by now, but priorities change, deadlines slip, and we never expected one of our last uses of MongoDB to experience a surge in growth.</p>
<p>The dataset in question seemed ideal for MongoDB:</p>
<ul>
<li>Ephemeral &#8211; if we lose it we experience service degradation for a short while, but nothing catastrophic</li>
<li>Small &#8211; easily fits into memory (~15 GB)</li>
<li>Secondary index &#8211; In a key/value store we would have had to manage a secondary index manually</li>
</ul>
<p>So this dataset dodged a lot of the previous problems we had with MongoDB and seemed safe to migrate at our leisure.</p>
<p><a name="global_write_lock"><b>Global Write Lock</b></a></p>
<p><a href="http://www.mongodb.org/display/DOCS/How+does+concurrency+work#Howdoesconcurrencywork-Read%2FWriteLock">MongoDB has a global write lock.</a> This means that while applying an insert or update, a single mongod instance can&#8217;t respond to other queries.</p>
<p>Our dataset may be small but it has a heavy read and write load. When the service it backed experienced a surge in usage, MongoDB quickly became CPU bound. This was especially frustrating considering mongod was running in a simple master/slave setup on two servers: each with 16 cores and enough memory to hold all the data a few times over again.</p>
<p>Because of the global write lock and heavy write load, operations are effectively serialized and executed on a single core. Meaning our servers didn&#8217;t even look loaded, as just 1 core would be 100% utilized by mongod.</p>
<p><a name="sharding"><b>Let the Sharding Begin</b></a></p>
<p>So we need to utilize multiple cores&#8230;<br />
To do that we need multiple write locks&#8230;<br />
There&#8217;s 1 write lock per mongod. So&#8230;<br />
&#8230;multiple mongods per server?</p>
<p>We&#8217;d been avoiding sharding after having no luck getting it working in the 1.5.x dev series, but it&#8217;s our only choice now to get multiple mongods. I ran some tests and it seemed like we could turn our master/slave setup into a 2 shard setup with 2 mongods and 1 arbiter per shard with downtime in the seconds or low minutes.</p>
<p>The operational complexity of configuring a MongoDB cluster is daunting with each component bringing its own caveats:</p>
<p><b>mongod config servers</b></p>
<ul>
<li>You need <i>exactly</i> 3 config mongods (1 is fine for testing, which makes things appear simpler than they really are).</li>
<li>There are lots of caveats with the config servers, so read <a href="http://www.mongodb.org/display/DOCS/Changing+Config+Servers">Changing Config Servers</a> carefully before configuring your cluster.</li>
<li>Otherwise these mongod instances are fairly blackboxish to me. Despite being mongod processes you administer them completely differently.</li>
</ul>
<p><b>mongos routers</b></p>
<ul>
<li>1 per app server. This wouldn&#8217;t be a big deal <a href="http://www.mongodb.org/display/DOCS/flushRouterConfig+command">except that our mongoses often start failing and require flushRouterConfig to be run on them</a>. <a href="https://jira.mongodb.org/browse/SERVER-3739">2.0.1 supposedly fixes this</a>, but we haven&#8217;t tested that yet (and trading known problems for new unknown ones is always scary).</li>
<li>mongos instances can use a lot of CPU and seem to have random spikes where they fully utilize every core very briefly. Keep this in mind if your application servers are already CPU bound.</li>
<li>On the bright side mongos balanced our data rather quickly. Our shard key is a uuid, and it properly setup reasonable ranges in very short order without having to preconfigure them.</li>
<li>&#8220;mongos&#8221; is a terribly confusing name. It sounds like multiple mongo instances. We&#8217;ve taken to calling them mongooses internally due to frequent typos and confusion.</li>
</ul>
<p><b>arbiters</b></p>
<ul>
<li>You need at least 3 members in a replica set in order to complete an election if 1 member goes down.</li>
<li>We haven&#8217;t had any issues with arbiters&#8230; not sure what we&#8217;d do if one broke somehow but since they have no persistent data they&#8217;re safe to restart at any time.</li>
</ul>
<p><b>mongods</b></p>
<ul>
<li>Early on we ran into a problem where changing replica set member entries wasn&#8217;t propagated to the config servers&#8217; shard configuration. Restarting every mongos fixed it.</li>
<li>As far as I can tell a new replica set member will never leave the initial RECOVERING state until all operations to that set are stopped. Even 40 updates per second was enough of a trickle to prevent a new set member from leaving RECOVERING to becoming a SECONDARY. We had to shutdown mongoses to cut off all traffic to bring up a new member. (The replication log gave every indication of being caught up and our usual update load is thousands per second.)</li>
<li>Setting rest in the config file doesn&#8217;t seem to work. Put &#8211;rest in your command line options.</li>
<li>Sending an HTTP request to a mongod&#8217;s main port (instead of the HTTP endpoint) seems to be capable of crashing the mongod.</li>
</ul>
<p><b>Client Drivers</b></p>
<p>While a single replica set member was in a RECOVERING state our Java services couldn&#8217;t complete <i>any</i> operations while our Python service was happily working away.</p>
<p><b>Summary</b></p>
<p>Right now we&#8217;re getting by with 2 shards on 2 dedicated servers and then mongoses and config servers spread throughout other servers. There appears to be some data loss occurring, though due to the ephemeral fast changing nature of this dataset it&#8217;s very difficult to determine definitively or reproduce independently.</p>
<p>So we&#8217;re trying to migrate off of MongoDB to a custom service better suited for this dataset ASAP.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schmichael.com/2011/11/05/failing-with-mongodb/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
		<item>
		<title>MemoryMapFile Convenience Class for Python</title>
		<link>http://blog.schmichael.com/2011/09/05/memorymapfile-convenience-class-for-python/</link>
		<comments>http://blog.schmichael.com/2011/09/05/memorymapfile-convenience-class-for-python/#comments</comments>
		<pubDate>Tue, 06 Sep 2011 05:54:25 +0000</pubDate>
		<dc:creator>Michael Schurter</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[mmap]]></category>

		<guid isPermaLink="false">http://blog.schmichael.com/?p=968</guid>
		<description><![CDATA[My obsession with mmap hasn&#8217;t died, but while Python&#8217;s mmap module is a wonderful low level library it&#8217;s a bit hard for a newcomer to use properly. I&#8217;ve started toying with a convenience wrapper class for mmap.mmap (at least the &#8230; <a href="http://blog.schmichael.com/2011/09/05/memorymapfile-convenience-class-for-python/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>My <a href="http://blog.schmichael.com/2011/05/15/sharing-python-data-between-processes-using-mmap/">obsession</a> with <a href="http://en.wikipedia.org/wiki/Mmap">mmap</a> hasn&#8217;t died, but while <a href="http://docs.python.org/library/mmap">Python&#8217;s mmap module</a> is a wonderful low level library it&#8217;s a bit hard for a newcomer to use properly.</p>
<p>I&#8217;ve started toying with a convenience wrapper class for <code>mmap.mmap</code> (at least the Unix version):<br />
<script src="https://gist.github.com/1196686.js?file=memorymap.py"></script></p>
<p>My original goal was to automatically <a href="http://docs.python.org/library/mmap#mmap.resize">grow</a> the mmap whenever the user attempts to write beyond the current size of the mmap file, but that&#8217;s going to take carefully wrapping quite a few methods (<code>write</code>, <code>__setitem__</code>, and maybe get/read methods too).</p>
<p>If it becomes useful, I may use it in <a href="https://github.com/schmichael/mmstats">mmstats</a>.</p>
<p>Feedback welcome!</p>
<p><strong>Update:</strong> Discovered the hard way (segfaults) that resizing mmaps is tricky: the region can be moved but data will be copied. However, any existing pointers (from ctypes.<type>.from_buffer in my case) will now point to freed memory and segfault upon use.</p>
<p>tl;dr &#8211; If at all possible, precompute the size of your mmap before using it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schmichael.com/2011/09/05/memorymapfile-convenience-class-for-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sharing Python data between processes using mmap</title>
		<link>http://blog.schmichael.com/2011/05/15/sharing-python-data-between-processes-using-mmap/</link>
		<comments>http://blog.schmichael.com/2011/05/15/sharing-python-data-between-processes-using-mmap/#comments</comments>
		<pubDate>Mon, 16 May 2011 04:28:29 +0000</pubDate>
		<dc:creator>Michael Schurter</dc:creator>
				<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[ctypes]]></category>
		<category><![CDATA[mmap]]></category>
		<category><![CDATA[struct]]></category>

		<guid isPermaLink="false">http://blog.schmichael.com/?p=947</guid>
		<description><![CDATA[I&#8217;ve been toying with an idea of exposing statistics for a Python application via shared memory to keep the performance impact on the application as low as possible. The goal being an application could passively expose a number of metrics &#8230; <a href="http://blog.schmichael.com/2011/05/15/sharing-python-data-between-processes-using-mmap/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been toying with an idea of exposing statistics for a Python application via shared memory to keep the performance impact on the application as low as possible. The goal being an application could passively expose a number of metrics that could either be periodically polled via <a href="http://munin-monitoring.org/">munin</a>/<a href="http://www.icinga.org/">Icinga</a>/etc plugins or interactive tools when diagnosing issues on a system.</p>
<p>But first things first: I need to put data into <a href="http://en.wikipedia.org/wiki/Shared_memory">shared memory</a> from Python. <a href="http://en.wikipedia.org/wiki/Mmap">mmap</a> is an excellent widely-implemented POSIX system call for creating a shared memory space backed by an on-disk file.</p>
<p>Usually in the UNIX world you have 2 ways of accessing/manipulating data: memory addresses or streams (files). Manipulating data via memory addresses means <a href="http://en.wikipedia.org/wiki/Pointer_%28computing%29">pointers</a>, offsets, <a href="http://en.wikipedia.org/wiki/Malloc">malloc/free</a>, etc. Stream interfaces manipulate data via <a href="http://en.wikipedia.org/wiki/System_call">read/write/seek system calls</a> for files and <a href="http://en.wikipedia.org/wiki/Berkeley_sockets">send/recv/etc for sockets</a>.</p>
<p>mmap gives you both interfaces. A memory mapped file can be manipulated via read/write/seek or by directly accessing its mapped memory region. The advantage of the latter is that this memory region is in userspace &#8212; meaning you can manipulate a file without incurring the overhead of write system calls for every manipulation.</p>
<p>Anyway, enough exposition, let&#8217;s see some code. <small>(Despite mmap&#8217;s nice featureset, I&#8217;m only using it as a simple memory sharing mechanism anyway.)</small> The following code shares a tiny bit of data between 2 Python processes using the excellent <a href="http://docs.python.org/library/mmap">mmap module in the stdlib</a>. <code>a.py</code> writes to the memory mapped region, and <code>b.py</code> reads the data out. <a href="http://docs.python.org/library/ctypes">ctypes</a> allows for an easy way to create values in a memory mapped region and manipulate them like &#8220;normal&#8221; Python objects.</p>
<p><em>These code samples were written using Python 2.7 on Linux. They should work fine on any POSIX system, but Windows users will have to change the mmap calls to match the Windows API.</em></p>
<p><strong>a.py</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/env python</span>
<span style="color: #ff7700;font-weight:bold;">import</span> ctypes
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">mmap</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">struct</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Create new empty file to back memory map on disk</span>
    fd = <span style="color: #dc143c;">os</span>.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/tmp/mmaptest'</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">O_CREAT</span> | <span style="color: #dc143c;">os</span>.<span style="color: black;">O_TRUNC</span> | <span style="color: #dc143c;">os</span>.<span style="color: black;">O_RDWR</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Zero out the file to insure it's the right size</span>
    <span style="color: #ff7700;font-weight:bold;">assert</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span>fd, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>00'</span> <span style="color: #66cc66;">*</span> <span style="color: #dc143c;">mmap</span>.<span style="color: black;">PAGESIZE</span><span style="color: black;">&#41;</span> == <span style="color: #dc143c;">mmap</span>.<span style="color: black;">PAGESIZE</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Create the mmap instace with the following params:</span>
    <span style="color: #808080; font-style: italic;"># fd: File descriptor which backs the mapping or -1 for anonymous mapping</span>
    <span style="color: #808080; font-style: italic;"># length: Must in multiples of PAGESIZE (usually 4 KB)</span>
    <span style="color: #808080; font-style: italic;"># flags: MAP_SHARED means other processes can share this mmap</span>
    <span style="color: #808080; font-style: italic;"># prot: PROT_WRITE means this process can write to this mmap</span>
    buf = <span style="color: #dc143c;">mmap</span>.<span style="color: #dc143c;">mmap</span><span style="color: black;">&#40;</span>fd, <span style="color: #dc143c;">mmap</span>.<span style="color: black;">PAGESIZE</span>, <span style="color: #dc143c;">mmap</span>.<span style="color: black;">MAP_SHARED</span>, <span style="color: #dc143c;">mmap</span>.<span style="color: black;">PROT_WRITE</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Now create an int in the memory mapping</span>
    i = ctypes.<span style="color: black;">c_int</span>.<span style="color: black;">from_buffer</span><span style="color: black;">&#40;</span>buf<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Set a value</span>
    i.<span style="color: black;">value</span> = <span style="color: #ff4500;">10</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># And manipulate it for kicks</span>
    i.<span style="color: black;">value</span> += <span style="color: #ff4500;">1</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">assert</span> i.<span style="color: black;">value</span> == <span style="color: #ff4500;">11</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Before we create a new value, we need to find the offset of the next free</span>
    <span style="color: #808080; font-style: italic;"># memory address within the mmap</span>
    offset = <span style="color: #dc143c;">struct</span>.<span style="color: black;">calcsize</span><span style="color: black;">&#40;</span>i._type_<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># The offset should be uninitialized ('\x00')</span>
    <span style="color: #ff7700;font-weight:bold;">assert</span> buf<span style="color: black;">&#91;</span>offset<span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\x</span>00'</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Now ceate a string containing 'foo' by first creating a c_char array</span>
    s_type = ctypes.<span style="color: black;">c_char</span> <span style="color: #66cc66;">*</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'foo'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Now create the ctypes instance</span>
    s = s_type.<span style="color: black;">from_buffer</span><span style="color: black;">&#40;</span>buf, offset<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># And finally set it</span>
    s.<span style="color: black;">raw</span> = <span style="color: #483d8b;">'foo'</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'First 10 bytes of memory mapping: %r'</span> <span style="color: #66cc66;">%</span> buf<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">10</span><span style="color: black;">&#93;</span>
    <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Now run b.py and press ENTER'</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Changing i'</span>
    i.<span style="color: black;">value</span> <span style="color: #66cc66;">*</span>= i.<span style="color: black;">value</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Changing s'</span>
    s.<span style="color: black;">raw</span> = <span style="color: #483d8b;">'bar'</span>
&nbsp;
    new_i = <span style="color: #008000;">raw_input</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Enter a new value for i: '</span><span style="color: black;">&#41;</span>
    i.<span style="color: black;">value</span> = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>new_i<span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p><strong>b.py</strong></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">mmap</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">struct</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">time</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Open the file for reading</span>
    fd = <span style="color: #dc143c;">os</span>.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'/tmp/mmaptest'</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">O_RDONLY</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;"># Memory map the file</span>
    buf = <span style="color: #dc143c;">mmap</span>.<span style="color: #dc143c;">mmap</span><span style="color: black;">&#40;</span>fd, <span style="color: #dc143c;">mmap</span>.<span style="color: black;">PAGESIZE</span>, <span style="color: #dc143c;">mmap</span>.<span style="color: black;">MAP_SHARED</span>, <span style="color: #dc143c;">mmap</span>.<span style="color: black;">PROT_READ</span><span style="color: black;">&#41;</span>
&nbsp;
    i = <span style="color: #008000;">None</span>
    s = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
        new_i, = <span style="color: #dc143c;">struct</span>.<span style="color: black;">unpack</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'i'</span>, buf<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        new_s, = <span style="color: #dc143c;">struct</span>.<span style="color: black;">unpack</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'3s'</span>, buf<span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span>:<span style="color: #ff4500;">7</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> i <span style="color: #66cc66;">!</span>= new_i <span style="color: #ff7700;font-weight:bold;">or</span> s <span style="color: #66cc66;">!</span>= new_s:
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'i: %s =&gt; %d'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>i, new_i<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'s: %s =&gt; %s'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>s, new_s<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Press Ctrl-C to exit'</span>
            i = new_i
            s = new_s
&nbsp;
        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    main<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p><small>(Note that I cruelly don&#8217;t clean up /tmp/mmaptest after the scripts finished. Consider it a 4KB tax for anyone who runs arbitrary code they found on the Internet without reading it first.)</small></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schmichael.com/2011/05/15/sharing-python-data-between-processes-using-mmap/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>signalfd</title>
		<link>http://blog.schmichael.com/2011/02/20/signalfd/</link>
		<comments>http://blog.schmichael.com/2011/02/20/signalfd/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 06:50:07 +0000</pubDate>
		<dc:creator>Michael Schurter</dc:creator>
				<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.schmichael.com/?p=920</guid>
		<description><![CDATA[This article covers signalfd, a system call only available on Linux. If anyone knows of an equivalent for OSX or BSDs,* please let me know. It&#8217;d be great to create a compatibility layer. Writing asynchronous IO code is fun; handling &#8230; <a href="http://blog.schmichael.com/2011/02/20/signalfd/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>This article covers </em>signalfd<em>, a system call only available on Linux. If anyone knows of an equivalent for OSX or BSDs,<abbrev title="If there's something like signalfd for Windows, I'm sorry but I really couldn't care less.">*</abbrev> please <a href="http://blog.schmichael.com/about-me/">let me know</a>. It&#8217;d be great to create a compatibility layer.</em></p>
<p>Writing asynchronous IO code is fun; handling signals is not. <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/signalfd.2.html">signalfd</a> allows you to move your signal handling code into your main event loop instead of hooking up global handlers and using the featureless <a href="http://docs.python.org/library/signal#signal.set_wakeup_fd">set_wakeup_fd</a> function to break the main loop.</p>
<p>Luckily <a href="https://launchpad.net/python-signalfd">Jean-Paul Calderone had already created a great Python wrapper for the signalfd and sigprocmask system calls</a>. Unfortunately it doesn&#8217;t include a way to parse the siginfo_t structure which contains all the useful information about the signal you&#8217;re handling.</p>
<p>I&#8217;ve added a helper to do just that in a branch:</p>
<p><a href="https://code.launchpad.net/~schmichael/python-signalfd/helpers">https://code.launchpad.net/~schmichael/python-signalfd/helpers</a></p>
<p>A sample program would look like:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">select</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">signal</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> signalfd
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> sigfdtest<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    <span style="color: #808080; font-style: italic;"># Catch them all!</span>
    sigs = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> attr <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">dir</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">signal</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> attr.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SIG'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #ff7700;font-weight:bold;">not</span> attr.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'SIG_'</span><span style="color: black;">&#41;</span>:
            sigs.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">signal</span>, attr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    sfd = signalfd.<span style="color: black;">create_signalfd</span><span style="color: black;">&#40;</span>sigs<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Capturing: %r'</span> <span style="color: #66cc66;">%</span> <span style="color: #008000;">sorted</span><span style="color: black;">&#40;</span>sigs<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'selecting - pid: %d'</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">getpid</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        r = <span style="color: #dc143c;">select</span>.<span style="color: #dc143c;">select</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>sfd<span style="color: black;">&#93;</span>, <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>, <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> s <span style="color: #ff7700;font-weight:bold;">in</span> r:
            <span style="color: #ff7700;font-weight:bold;">assert</span> s <span style="color: #ff7700;font-weight:bold;">is</span> sfd, <span style="color: #483d8b;">'Python nicely re-uses the fd instance'</span>
            sig = signalfd.<span style="color: black;">read_signalfd</span><span style="color: black;">&#40;</span>sfd<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">print</span> sig
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    sigfdtest<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>When run you can throw some signals at it:</p>
<pre>
Capturing: [6, 14, 7, 17, 17, 18, 8, 1, 4, 2, 29, 6, 9, 13, 29, 27, 30, 3, 64, 34, 11, 19, 31, 15, 5, 20, 21, 22, 23, 10, 12, 26, 28, 24, 25]
selecting - pid: 6523
^CSIGINT
selecting - pid: 6523
^CSIGINT
selecting - pid: 6523
SIGHUP
selecting - pid: 6523
Killed
</pre>
<p>Of course you&#8217;ll need to use an uninterpretable signal like KILL to exit.</p>
<p><a href="http://bugs.python.org/issue8407">Jean-Paul attempted to get signalfd included in Python 2.7&#8242;s signal module, and it was slated for inclusion in 3.2</a>. However, given that <a href="http://www.python.org/download/releases/3.2/">3.2</a> was just <a href="http://docs.python.org/release/3.2/library/signal">released without it</a>, I&#8217;m guessing the attempt to get this functionality into Python&#8217;s stdlib has been forgotten.</p>
<p>Up next: <a href="http://www.kernel.org/doc/man-pages/online/pages/man2/eventfd.2.html">eventfd</a> perhaps?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schmichael.com/2011/02/20/signalfd/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>schmongodb slides from Update Portland</title>
		<link>http://blog.schmichael.com/2011/02/02/schmongodb-slides-from-update-portland/</link>
		<comments>http://blog.schmichael.com/2011/02/02/schmongodb-slides-from-update-portland/#comments</comments>
		<pubDate>Thu, 03 Feb 2011 06:37:29 +0000</pubDate>
		<dc:creator>Michael Schurter</dc:creator>
				<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[mongodb]]></category>
		<category><![CDATA[updatepdx]]></category>
		<category><![CDATA[urbanairship]]></category>

		<guid isPermaLink="false">http://blog.schmichael.com/?p=895</guid>
		<description><![CDATA[A few months ago someone in #pdxwebdev on Freenode asked an innocent MongoDB question. In response I ranted seemingly endlessly about our experience with MongoDB at Urban Airship. After a few moments somebody (perhaps sarcastically? who can know on IRC) &#8230; <a href="http://blog.schmichael.com/2011/02/02/schmongodb-slides-from-update-portland/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>A few months ago someone in #pdxwebdev on Freenode asked an innocent <a href="http://www.mongodb.org">MongoDB</a> question. In response I ranted seemingly endlessly about our experience with MongoDB at <a href="http://urbanairship.com">Urban Airship</a>. After a few moments somebody (perhaps sarcastically? who can know on IRC) suggested I give a talk on my experiences with MongoDB. That led me to realize despite <a href="http://calagator.org/">Portland&#8217;s amazing meetup culture</a> there were no tech-meetups that focused on either:</p>
<ol>
<li>Narrative talks based on experiences in production <small>(<em>not</em> how-tos)</small></li>
<li>Database-agnostic backend systems focused groups <small>(<em>not</em> just a NoSQL meetup)</small></li>
</ol>
<p>So I started one: <a href="http://www.meetup.com/updatepdx/">Update Portland</a>.</p>
<p>And I gave my promised MongoDB talk: <a href="https://docs.google.com/present/view?id=ddzswzbr_104f2sgp8dq">schmongodb</a>.</p>
<p>And <a href="http://twitter.com/meghanpgill/status/23455639216848896">10gen sent swag</a>! (Thanks to <a href="http://twitter.com/meghanpgill">Meghan</a>! It was a big hit.)</p>
<p><em>And</em> my brilliant coworker <a href="http://twitter.com/eonnen">Erik Onnen</a> gave a short talk on how he&#8217;s beginning to use <a href="http://sna-projects.com/kafka/">Kafka</a> at Urban Airship. (Expect a long form talk on that in the future!)</p>
<p>Thanks to everyone who showed up. I had a great time and have high hopes for the upcoming meetings. (Sign up for the <a href="http://groups.google.com/group/update-pdx">mailing list</a>!)</p>
<p>The slides may come across as overly negative. After all Urban Airship is actively moving away from MongoDB for our largest and busiest pieces of data. So I want to make 2 things very clear:</p>
<ol>
<li>I like MongoDB and would like to use it again in the future. There&#8217;s a lot I don&#8217;t like about it, but I can&#8217;t think of any &#8220;perfect&#8221; piece of software.</li>
<li>The IO situation in EC2, particularly EBS&#8217;s poor performance (RAIDing really doesn&#8217;t help) made life with MongoDB miserable. This story may have been very different if we were running MongoDB on bare metal with fast disks.</li>
</ol>
<p><a href="http://www.mikeherrick.com/">Mike Herrick, the VP of Engineering at Urban Airship</a>, put me on the spot at the end of my talk by asking me by asking me: <strong>&#8220;Knowing what you know now, what would you have done differently?&#8221;</strong></p>
<p>I didn&#8217;t have a good answer, and I still don&#8217;t. Despite all of the misadventures, MongoDB wasn&#8217;t the wrong choice. Scaling systems is just hard, and if you want something to work under load, you&#8217;re going to have to learn all of its ins and outs. We initially started moving to Cassandra, and while it has tons of wonderful attributes, we&#8217;re running into plenty of problems with it as well.</p>
<p>So I think the answer is <em>knowing then what I know now</em>. In other words: <strong>Do your homework</strong>. That way we could have avoided these shortcomings and perhaps still be happy with MongoDB today. Hopefully these slides will help others in how they plan to use MongoDB so they can use it properly and happily.</p>
<p><strong>Note:</strong> I added lots of comments to the speaker notes, so you&#8217;ll probably want to view those while looking at the slides.<br />
<iframe src="https://docs.google.com/present/embed?id=ddzswzbr_104f2sgp8dq&#038;size=m" frameborder="0" width="555" height="451"></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schmichael.com/2011/02/02/schmongodb-slides-from-update-portland/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Deploying Python behind Nginx Talk Slides</title>
		<link>http://blog.schmichael.com/2011/01/25/deploying-python-behind-nginx-talk-slides/</link>
		<comments>http://blog.schmichael.com/2011/01/25/deploying-python-behind-nginx-talk-slides/#comments</comments>
		<pubDate>Tue, 25 Jan 2011 18:52:13 +0000</pubDate>
		<dc:creator>Michael Schurter</dc:creator>
				<category><![CDATA[GNU/Linux]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[pdxpython]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[slides]]></category>
		<category><![CDATA[wsgi]]></category>

		<guid isPermaLink="false">http://blog.schmichael.com/?p=889</guid>
		<description><![CDATA[I gave a talk on deploying Python WSGI apps behind nginx at the Portland Python User Group meeting on January 11th and finally got around to publishing the slides: schmingx. I should mention Jason Kirtland informed me after the meeting &#8230; <a href="http://blog.schmichael.com/2011/01/25/deploying-python-behind-nginx-talk-slides/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I gave a talk on deploying Python WSGI apps behind <a href="http://wiki.nginx.org/">nginx</a> at the <a href="http://wiki.python.org/moin/PortlandPythonUserGroup">Portland Python User Group</a> meeting on January 11th and finally got around to publishing the slides: <a href="https://docs.google.com/present/edit?id=0Ab7GDIugV1qCZGR6c3d6YnJfMTA1aGRtZmJxYzI&#038;hl=en">schmingx</a>.</p>
<p>I should mention <a href="http://discorporate.us/jek/">Jason Kirtland</a> informed me after the meeting that <a href="http://www.fastcgi.com/devkit/doc/fcgi-spec.html">FastCGI</a> supports <a href="http://www.fastcgi.com/devkit/doc/fcgi-spec.html#S3.5">persistent connections (and a host of other features)</a> between a load balancer and backend app servers.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.schmichael.com/2011/01/25/deploying-python-behind-nginx-talk-slides/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

