<?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>Labix Blog</title>
	<atom:link href="http://blog.labix.org/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.labix.org</link>
	<description>by Gustavo Niemeyer</description>
	<lastBuildDate>Tue, 30 Apr 2013 16:13:48 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Porting 6700 lines of C to Go</title>
		<link>http://blog.labix.org/2013/04/28/porting-6700-lines-of-c-to-go</link>
		<comments>http://blog.labix.org/2013/04/28/porting-6700-lines-of-c-to-go#comments</comments>
		<pubDate>Sun, 28 Apr 2013 13:50:49 +0000</pubDate>
		<dc:creator>niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Article]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[Project]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=1418</guid>
		<description><![CDATA[A few years ago, when I started pondering about the possibility of porting juju to the Go language, one of the first pieces of the puzzle that were put in place was goyaml: a Go package to parse and serialize &#8230; <a href="http://blog.labix.org/2013/04/28/porting-6700-lines-of-c-to-go">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>A few years ago, when I started pondering about the possibility of porting <a href="https://juju.ubuntu.com">juju</a> to the Go language, one of the first pieces of the puzzle that were put in place was <a href="https://wiki.ubuntu.com/goyaml">goyaml</a>: a Go package to parse and serialize a <a href="yaml.org/spec/1.1">yaml</a> document. This was just an experiment and, as a sane route to get started, a Go layer that does all the language-specific handling was written on top of the <a href="http://pyyaml.org/wiki/LibYAML">libyaml</a> C scanner, parser, and serializer library.</p>
<p>This was a good initial plan, but for a number of reasons the end goal was always to have a pure Go implementation. Having a C layer in a Go program slows down builds significantly due to the time taken to build the C code, makes compiling in other platforms and cross-compiling harder, has certain runtime penalties, and also forces the application to drop the memory safety guarantees offered by Go.</p>
<p><span id="more-1418"></span>For these reasons, over the last couple of weeks I took a few hours a day to port the C backend to Go. The total time, considering full time work days, would be equivalent to about a week worth of work.</p>
<p>The work started on the scanner and parser side of the library. This took most of the time, not only because it encompassed more than half of the code base, but also because the shared logic had to be ported too, and there was a need to understand which patterns were used in the old code and how they would be converted across in a reasonable way.</p>
<p>The whole scanner and parser plus header files, or around 5000 code lines of C, were ported over in a single shot without intermediate runs. To steer the process in a sane direction, <a href="http://golang.org/cmd/gofmt">gofmt</a> was called often to reformat the converted code, and then the project was compiled every once in a while to make sure that the pieces were hanging together properly enough.</p>
<p>It&#8217;s worth highlighting how useful gofmt was in that process. The C code was converted in the most convenient way to type it, and then gofmt would quickly put it all together in a familiar form for analysis. Not rarely, it would also point out trivial syntactic issues. A double win.</p>
<p>After the scanner and parser were finally converted completely, the pre-existing Go unmarshaling logic was shifted to the new pure implementation, and the reading side of the test suite could run as-is. Naturally, though, it didn&#8217;t work out of the box.</p>
<p>To quickly pick up the errors in the new implementation, the C logic and the Go port were put side-by-side to run the same tests, and tracing was introduced in strategic points of the scanner and parser. With that, it was easy to spot where they diverged and pinpoint the human errors.</p>
<p>It took about two hours to get the full suite to run successfully, with a handful of bugs uncovered. Out of curiosity, the issues were:</p>
<ul>
<li> An improperly dropped parenthesis affected the precedence of an expression
<li> A slice was being iterated with copying semantics where a reference was necessary
<li> A pointer arithmetic conversion missed the base where there was base+offset addressing
<li> An inner scoped variable improperly shadowed the outer scope
</ul>
<p>The same process of porting and test-fixing was then repeated on the the serializing side of the project, in a much shorter time frame for the reasons cited.</p>
<p>The resulting code isn&#8217;t yet idiomatic Go. There are several signs in it that it was ported over from C: the name conventions, the use of custom solutions for buffering and reader/writer abstractions, the excessive copying of data due to the need of tracking data ownership so the simple deallocating destructors don&#8217;t double-free, etc. It&#8217;s also been <i>deoptimized</i>, due to changes such as the removal of macros and in many cases its inlining, and the direct expansion of large unions which causes some core objects to grow significantly.</p>
<p>At this point, though, it&#8217;s easy to gradually move the code base towards the common idiom in small increments and as time permits, and cleaning up those artifacts that were left behind.</p>
<p>This code will be made public over the next few days via a new goyaml release.  Meanwhile, some quick facts about the process and outcome follows.</p>
<p><b>Lines of code</b></p>
<p>According to cloc, there was a total of 7070 lines of C code in .c and .h files.  Of those, 6727 were ported, and 342 were 12 functions that were left unconverted as being unnecessary right now. Those 6727 lines of C became 5039 lines of Go code in a mostly one-to-one dumb translation.</p>
<p>That difference comes mainly from garbage collection, lack of forward declarations, standard helpers such as append, range-based for loops, first class slice type with length and capacity, internal OOM handling, and so on.</p>
<p>Future work code can easily increase the difference further by replacing some of the logic ported with more sensible options available in Go, such as standard abstractions for readers and writers, buffered writing support as availalbe in the standard library, etc.</p>
<p><b>Code clarity and safety</b></p>
<p>In the specific context of the work done, which is of a scanner, parser and serializer, the slice abstraction is responsible for noticeable clarity gains in the code, when compared to the equivalent logic based on pointer arithmetic. It also gives a much more comforting guarantee of correctness of the written code due to bound-checking.</p>
<p><b>Performance</b></p>
<p>While curious, this shouldn&#8217;t be taken as a performance comparison between the two languages, as it is comparing a fine tuned C implementation with something that is worse than a direct one-to-one port: not only it hasn&#8217;t seen any time at all on preventing waste, but the original logic was deoptimized due to changes such as the removal of inlining macros and the expansion of large unions. There are many obvious changes to be done for improving performance.</p>
<p>With that out of the way, in a simple decoding benchmark the C-backed decoder runs on about 37% of the time taken by the out-of-the-box deoptimized Go port.</p>
<p><b>Output size</b></p>
<p>The previous goyaml.a Go package file had 1463kb.  The new one has 1016kb.  This difference includes glue code generated for the integration.</p>
<p>Considering only the .c and .h files involved in the port, the C object code generated with the standard flags used by the go build tool (-g -O2) sums up to 789kb. The equivalent Go code with the standard settings compiles to 664kb. The 12 functions not ported are also part of that difference, so the difference is pretty much negligible.</p>
<p><b>Build time</b></p>
<p>Building the 8 .c files alone takes 3.6 seconds with the standard flags used by the go build tool (-g -O2). After the port, building the entire Go project with the standard settings takes 0.3 seconds.</p>
<p><b>Mechanical changes</b></p>
<p>Many of the mechanical changes were done using regular expressions. Excluding the trivial ones, about a dozen regular expressions were used to swap variable and type names, drop parenthesis, place brackets in the right locations, convert function declarations, and so on.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2013/04/28/porting-6700-lines-of-c-to-go/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exceptional crashes</title>
		<link>http://blog.labix.org/2013/04/23/exceptional-crashes</link>
		<comments>http://blog.labix.org/2013/04/23/exceptional-crashes#comments</comments>
		<pubDate>Tue, 23 Apr 2013 08:35:07 +0000</pubDate>
		<dc:creator>niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Article]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=1330</guid>
		<description><![CDATA[Last week I was part of a rant with a couple of coworkers around the fact Go handles errors for expected scenarios by returning an error value instead of using exceptions or a similar mechanism. This is a rather controversial &#8230; <a href="http://blog.labix.org/2013/04/23/exceptional-crashes">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Last week I was part of a rant with a couple of coworkers around the fact Go handles errors for expected scenarios by returning an error value instead of using exceptions or a similar mechanism. This is a rather controversial topic because people have grown used to having errors out of their way via exceptions, and Go brings back <a href="http://golang.org/doc/articles/error_handling.html">an improved version</a> of a well known pattern previously adopted by a number of languages &mdash; including C &mdash; where errors are communicated via return values. This means that errors are in the programmer&#8217;s face and have to be dealt with <i>all the time</i>. In addition, the controversy extends towards the fact that, in languages with exceptions, every unadorned error comes with a full traceback of what happened and where, which in some cases is convenient.</p>
<p><span id="more-1330"></span>All this convenience has a cost, though, which is rather simple to summarize:</p>
<blockquote style="font-size: 120%"><p>
Exceptions teach developers to not care about errors.
</p></blockquote>
<p>A sad corollary is that this is relevant even if you are a brilliant developer, as you&#8217;ll be affected by the world around you being lenient towards error handling. The problem will show up in the libraries that you import, in the applications that are sitting in your desktop, and in the <i>servers that back your data</i> as well.</p>
<p>Raymond Chen described the issue <a href="http://blogs.msdn.com/b/oldnewthing/archive/2004/04/22/118161.aspx">back in 2004</a> as:</p>
<blockquote><p>
Writing correct code in the exception-throwing model is in a sense harder than in an error-code model, since anything can fail, and you have to be ready for it. In an error-code model, it&#8217;s obvious when you have to check for errors: When you get an error code. In an exception model, you just have to know that errors can occur anywhere.</p>
<p>In other words, in an error-code model, it is obvious when somebody failed to handle an error: They didn&#8217;t check the error code. But in an exception-throwing model, it is not obvious from looking at the code whether somebody handled the error, since the error is not explicit.<br />
(&#8230;)<br />
When you&#8217;re writing code, do you think about what the consequences of an exception would be if it were raised by each line of code? You have to do this if you intend to write correct code.
</p></blockquote>
<p>That&#8217;s exactly right. Every line that may raise an exception holds a hidden &#8220;else&#8221; branch for the error scenario that is very easy to forget about. Even if it sounds like a pointless repetitive task to be entering that error handling code, the exercise of writing it down forces developers to keep the alternative scenario in mind, and pretty often it doesn&#8217;t end up empty.</p>
<p>It isn&#8217;t the first time I write about that, and given the controversy that surrounds these claims, I generally try to find one or two examples that bring the issue home. So here is the best example I could find today, within the <a href="http://docs.python.org/3/library/pty.html">pty</a> module of Python&#8217;s 3.3 standard library:</p>
<p><code>
<pre>
def spawn(argv, master_read=_read, stdin_read=_read):
    """Create a spawned process."""
    if type(argv) == type(''):
        argv = (argv,)
    pid, master_fd = fork()
    if pid == CHILD:
        os.execlp(argv[0], *argv)
    (...)
</pre>
<p></code></p>
<p>Every time someone calls this logic with an improper executable in argv there will be a new Python process lying around, uncollected, and unknown to the application, because execlp will fail, and the process just forked will be disregarded. It doesn&#8217;t matter if a client of that module catches that exception or not. It&#8217;s too late. The local duty wasn&#8217;t done. Of course, the bug is trivial to fix by adding a try/except within the spawn function itself. The problem, though, is that <i>this logic looked fine for everybody that ever looked at that function <a href="http://hg.python.org/cpython/rev/8c9cc054c5ed#l1.99">since 1994</a> when Guido van Rossum first committed it!</i></p>
<p>Here is another interesting one:</p>
<p><code>
<pre>
$ make clean
Sorry, command-not-found has crashed! Please file a bug report at:

https://bugs.launchpad.net/command-not-found/+filebug

Please include the following information with the report:

command-not-found version: 0.3
Python version: 3.2.3 final 0
Distributor ID: Ubuntu
Description:    Ubuntu 13.04
Release:        13.04
Codename:       raring
Exception information:

unsupported locale setting
Traceback (most recent call last):
  File "/.../CommandNotFound/util.py", line 24, in crash_guard
    callback()
  File "/usr/lib/command-not-found", line 69, in main
    enable_i18n()
  File "/usr/lib/command-not-found", line 40, in enable_i18n
    locale.setlocale(locale.LC_ALL, '')
  File "/usr/lib/python3.2/locale.py", line 541, in setlocale
    return _setlocale(category, locale)
locale.Error: unsupported locale setting
</pre>
<p></code></p>
<p>That&#8217;s a pretty harsh crash for the lack of locale data in a system-level application that is, ironically, supposed to tell users what packages to install when commands are missing. Note that at the top of the stack there&#8217;s a reference to <i>crash_guard</i>. This function has the intent of catching all exceptions right at the edge of the call stack, and displaying a detailed system specification and traceback to aid in fixing the problem.</p>
<p>Such &#8220;parachute catching&#8221; is a fairly common pattern in exception-oriented programming and tends to give developers the false sense of having good error handling within the application. Rather than actually <i>guarding</i> the application, though, it&#8217;s just a useful way to crash. The proper thing to have done in the case above would be to print a warning, if at all, and then let the program run as usual. This would have been achieved by simply wrapping that one line as in:</p>
<p><code>
<pre>
try:
    locale.setlocale(locale.LC_ALL, '')
except Exception as e:
    print("Cannot change locale:", e)
</pre>
<p></code></p>
<p>Clearly, it was easy to handle that one. The problem, again, is that it was very natural to not do it in the first place. In fact, it&#8217;s more than natural: it actually <i>feels good</i> to not be looking at the error path. It&#8217;s less code, more linear, and what&#8217;s left is the most desired outcome.</p>
<p>The consequence, unfortunately, is that we&#8217;re immersing ourselves in a world of brittle software and pretty whales. Although more verbose, the error result style builds the correct mindset: does that function or method have a possible error outcome? How is it being handled? Is that system-interacting function not returning an error? What is being done with the problem that, of course, can happen?</p>
<p>A surprising number of crashes and plain misbehavior is a result of such unconscious negligence.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2013/04/23/exceptional-crashes/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Unix-like pipelines for Go</title>
		<link>http://blog.labix.org/2013/04/15/unix-like-pipelines-for-go</link>
		<comments>http://blog.labix.org/2013/04/15/unix-like-pipelines-for-go#comments</comments>
		<pubDate>Mon, 15 Apr 2013 08:52:53 +0000</pubDate>
		<dc:creator>niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=1271</guid>
		<description><![CDATA[This weekend the proper environment settled out for sorting a pet peeve that shows up every once in a while when coding: writing logic that interacts with other applications in the system via their stdin and stdout streams is often &#8230; <a href="http://blog.labix.org/2013/04/15/unix-like-pipelines-for-go">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>This weekend the proper environment settled out for sorting a pet peeve that shows up every once in a while when coding: writing logic that interacts with other applications in the system via their stdin and stdout streams is often more involved than it should be, which seems pretty ironic when sitting in front of a Unix-like system.</p>
<p><span id="more-1271"></span>Rather than going over the trouble of setting up pipes and hooking them up in a custom way, often applications end up just delegating the job to /bin/sh, which is not ideal for a number of reasons: argument formatting isn&#8217;t straightforward, injecting custom application-defined logic is hard, which means even simple tasks that might be easily achieved by the language end up shelling out to further external applications, and so on.</p>
<p>In an attempt to address that, I&#8217;ve spent some time working on an experimental <a href="http://golang.org">Go</a> package that is being released today: <a href="http://labix.org/pipe">pipe</a>.</p>
<p>I hope you like it as well, and please drop me a note if you find any issues.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2013/04/15/unix-like-pipelines-for-go/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Synchronicity: threads vs. events</title>
		<link>http://blog.labix.org/2013/03/28/synchronicity-threads-vs-events</link>
		<comments>http://blog.labix.org/2013/03/28/synchronicity-threads-vs-events#comments</comments>
		<pubDate>Thu, 28 Mar 2013 10:56:36 +0000</pubDate>
		<dc:creator>niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=1248</guid>
		<description><![CDATA[There are a number of common misconceptions in software development surrounding the idea of concurrency. This has been coming for decades, and some of the issues have just been reinforced one more time in an otherwise interesting post in LinkedIn&#8217;s &#8230; <a href="http://blog.labix.org/2013/03/28/synchronicity-threads-vs-events">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>There are a number of common misconceptions in software development surrounding the idea of concurrency. This has been coming for decades, and some of the issues have just been reinforced one more time in an otherwise interesting <a href="http://engineering.linkedin.com/play/play-framework-async-io-without-thread-pool-and-callback-hell">post</a> in LinkedIn&#8217;s engineering blog that recommends their development framework.</p>
<p>Such issues may be observed throughout the post, but can be elucidated via this short paragraph:<span id="more-1248"></span></p>
<blockquote><p>
As we saw with the Scala and JavaScript examples above, for very simple cases, the Evented (asynchronous) code is generally more complicated than Threaded (synchronous) code. However, in most real-world scenarios, you’ll have to make several I/O calls, and to make them fast, you’ll need to do them in parallel.
</p></blockquote>
<p>At a glance, this may look like a sane proposition. There&#8217;s agreement that an <i>asynchronous</i> API or framework is one that does not block the flow of execution when faced with a task that has a long or non-predictable deadline, and this coding style is harder for human beings to get right. For example, if you see code such as:</p>
<p><code>
<pre>
data = read(filename)
</pre>
<p></code></p>
<p>There&#8217;s less brain work to process and build on it than so called asynchronous logic such as:</p>
<p><code>
<pre>
read(filename, callback)
</pre>
<p></code></p>
<p>It&#8217;s also true that there are important interfaces that follow the asynchronous style to prevent resource waste. Some of these <a href="http://en.wikipedia.org/wiki/Epoll">exist</a> in the kernel I/O API.</p>
<p>So what&#8217;s the issue, then?</p>
<p>There are a few. The first one is the statement that to make I/O scale you have to do it in parallel. That&#8217;s clearly not true. Scalable I/O requires your program to not waste an irresponsible amount of memory and CPU per operation. This may be achieved with simple concurrent techniques, and <a href="http://vimeo.com/49718712">concurrency is not parallelism</a>.</p>
<p>This drives to the next point, which is the strong association between synchronous programming and threads. You can have synchronous programming, and its simplified mental model, <i>without operating system threads</i>. This can be done by having a compiler and runtime that is mindful about performance and resource consumption, building on the efficient interfaces to implement its abstractions.</p>
<p>These ideas have also been covered in this <a href="http://static.usenix.org/events/hotos03/tech/full_papers/vonbehren/vonbehren_html/">paper from 2003</a>, including benchmark results that debunk the performance myth. What seems most interesting about this paper is that it theorizes such a compiler and runtime that would allow &#8220;overcom[ing] limitations in current threads packages and improv[ing] safety, programmer productivity, and performance&#8221;, by using techniques such as dynamic stack growth, stack moving, cheaper synchronization, and compile-time data race detection.</p>
<p>That exact mix, including all of the properties described in the paper, are available today in the <a href="http://golang.org">Go language</a>. You can have synchronous programming, concurrency, parallelism, and performance. We live in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2013/03/28/synchronicity-threads-vs-events/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>12 years ago</title>
		<link>http://blog.labix.org/2013/03/01/12-years-ago</link>
		<comments>http://blog.labix.org/2013/03/01/12-years-ago#comments</comments>
		<pubDate>Fri, 01 Mar 2013 01:40:18 +0000</pubDate>
		<dc:creator>niemeyer</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=1236</guid>
		<description><![CDATA[These ancient entries were taken from my old Advogato diary, written in my early twenties, a year after I joined the development team of Conectiva Linux. I&#8217;m copying them for historic purposes, with the content untouched. It&#8217;s curious to look &#8230; <a href="http://blog.labix.org/2013/03/01/12-years-ago">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>These ancient entries were taken from my old Advogato diary, written in my early twenties, a year after I joined the development team of Conectiva Linux. I&#8217;m copying them for historic purposes, with the content untouched. It&#8217;s curious to look back and have such details of what was going on at the time, things that feel good, and things that feel awkward such as the <i>&#8220;Dear Diary, &#8230;&#8221;</i> style of writing, and the amount of exclamations!!</p>
<p><span id="more-1236"></span><br />
<b>6 Feb 2002</b></p>
<p>Wow!! It has been a long time since my last diary entry.</p>
<p>I&#8217;ve left Linuxconf development team coordination in favor of the Conectiva Linux port to the S390 platform coordination (ok, I&#8217;m mostly coordinating myself now <img src='http://blog.labix.org/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> . Most of the work is done. I have developed an acceptable installer (in Python!) and most of the packages are ported. We had some problems with IBM OCO modules (ick!), but we are already workarounding them (we gave up on some of our kernel patches, and I patched insmod to recognize OCO modules). Anyway, more information about this later (if I don&#8217;t disappear for another year.. <img src='http://blog.labix.org/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> &#8230;</p>
<p>In the process of porting Conectiva Linux to S390 and PPC (Harald Welve started the PPC port, and I&#8217;m keeping it up to date and working on missing stuff) we are learning some lessons. We are trying to use those lessons to build a defacto package building system using the Python language. Unfortunately, we don&#8217;t have enough people here to develop it quickly, so we are trying a more realistic and evolutive approach this time. The first part of it is almost done. While devloping it, I&#8217;ve studied a little bit about process groups and extended python with a missing killpg() system call. I&#8217;ve also discovered that when python spawns a new thread, it blocks all signals. With this information in mind, I have also extended it with a new execv() syscall, that besides doing the usual work, unblocks every signal before the real call to execv(). I hope this project becomes real someday.</p>
<p>I&#8217;ve also been playing with Python optimization lately. There&#8217;s a big opportunity for somebody wanting to study and implement some concepts there. I&#8217;ve read some documentation about Stack Machine Optimization and made some tries (basically, optimizations around the inner loop and the Big Switch, stack caching, and other flavors of this joy). Today I found a paper from Skip Montanaro documenting some of the tries I&#8217;ve made (reading it first would save me a lot of time, but this knowledge will be useful anyway). You should have a look at <a href="http://manatee.mojam.com/~skip/python/spam7/optimizer.html">his paper</a> if you have any interest in the topic. Oh, don&#8217;t forget to get yourself a copy of Lemburg&#8217;s <a href="http://www.lemburg.com/files/python/">pybench</a> to have a general idea of what you&#8217;re doing (don&#8217;t trust too much on it, it&#8217;s just a benchmark). I&#8217;ve written Skip a mail to discuss a little bit about what could be integrated into the interpreter. Let&#8217;s see where we get.</p>
<p>Oh&#8230; I must not forget to update the people I&#8217;ve certified in the past to reflect what they&#8217;ve been doing.</p>
<p><b>27 Jan 2001</b></p>
<p>I&#8217;ve just tested the patch floppy_cs on kernel 2.2.17 and it works just fine!!! I had no problems applying it. Now my Libretto 50CT has a floppy drive. Maybe Conectiva Linux can ship with this patch. The only drawback is that floppy support must be modular.</p>
<p>In the few last days, I&#8217;ve implemented support for Inputgrid into gnome-linuxconf. It allows one to define sensitive areas into a drawing area. Gurus are already working with it.</p>
<p>I&#8217;ve also created two new commands for Linuxconf&#8217;s gui protocol: Splash and Hidesplash. Linuxconf will send them while it is starting, and the graphic frontend is suposed to show a nice splash screen. Support in gnome-linuxconf has also been implemented with an image designed by Everaldo (thanks!!!).</p>
<p>Btw, yesterday I&#8217;ve fixed a bug in Python&#8217;s bsddb module. It was handling DB_RECNO databases with string keys. As the documentation says, these databases must use, in key&#8217;s &#8220;data&#8221; field, a pointer to a memory location holding a recno_t type.</p>
<p>My parents have arrived yesterday!! They&#8217;ll stay here until monday and then will go to Minas Gerais, visiting Raul and Lulude.</p>
<p><b>22 Jan 2001</b></p>
<p>Yesterday I&#8217;ve implemented a message signing module for Mailman. Darian (aka dmalloc), from openprojects.net has asked if I could do such module to use on lists.openprojects.net. (I said yes&#8230; <img src='http://blog.labix.org/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> . I&#8217;m going to ask the people here at Conectiva if they want to use this module in some of our lists.</p>
<p>Pybot has a few new features: CTCP handling/sending, timer module, unhandled messages hook, and something else I probably forgot.</p>
<p>About Linuxconf, I have spent the last days fixing a few simple bugs introduced in 1.24r2 and in the last modules developed by Conectiva. I hope to release a Linuxconf update to Conectiva 6.0 tomorrow.</p>
<p>gnome-linuxconf has won a home with screenshots and everything else at <a href="http://distro.conectiva.com/projetos/45">http://distro.conectiva.com/projetos/45</a>. I&#8217;ve also published it at <a href="http://freshmeat.net/projects/gnome-linuxconf/">freshmeat</a> and put files to download at <a href="http://sourceforge.net/projects/gnome-linuxconf">SourceForge</a>.</p>
<p>Oh, good news I forgot to tell: for those of you that are using wxxt-linuxconf, Jack (Jacques Gelinas) has implemented the Treemenu icons into this frontend as well.</p>
<p><b>16 Jan 2001</b></p>
<p>Today I&#8217;ve fixed a bug in the Pythonmod module of Linuxconf. Linuxconf has a default handler for the SIGCHLD signal that controls all of its child termination. This method has a few disadvantages. Before calling any external processes without using default Linuxconf methods, you must block this handler, otherwise Linuxconf will get on your way. Because of this, If a Pythonmod module tried to fork external processes, they were failing. Now Pythonmod is setting the SIGCHLD signal to SIG_DFL (POSIX doesn&#8217;t allow us to SIG_IGN it) before calling python code, and after returning from a few Linuxconf API functions that set the handler back. When the python code returns, popen_initsignal() is called, putting the Linuxconf handler back in place.</p>
<p>On the gnome-linuxconf side, I&#8217;ve implemented the drawing context command Defpen. Now we have colored lines and primitives!! (ok&#8230; not that good&#8230; <img src='http://blog.labix.org/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>I&#8217;ve also spent a few hours in the last two days backing up and restoring data in my colocated machine. Now my personal emails are back online and the server has an updated kernel. I hope it doesn&#8217;t bother me for a long time.</p>
<p>Unfortunately, the server stuff didn&#8217;t let me work on Pybot, but I had time to implement dynamically loading, unloading and reloading of modules, before I started on the server. This will help a lot in the development, since I don&#8217;t have to reboot the bot everytime something is wrong. Anyway, now that the server is ok (I hope so), I&#8217;m planning to spend some of my spare time on the bot (yes, I still have some&#8230; <img src='http://blog.labix.org/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> .</p>
<p><b>11 Jan 2001</b></p>
<p>Today I&#8217;ve added the ability of using icons while in the Treemenu mode of Linuxconf. I have just changed some functions to pass the icon name around until it got into the treemenu module and then sent it to the GUI front-end. A little hack on gnome-linuxconf did the work at the front-end side. Following this line of improvements, I&#8217;m planning to add a splash screen or something like that soon. Icons would also be welcome in the web interface.</p>
<p>Besides that, I&#8217;m also playing with a Python IRC bot. It&#8217;s not meant to be a war or a channel control bot. I&#8217;m planning to implement useful modules to help making IRC even more useful as an information media (no it won&#8217;t be just another infobot clone). The core and a few modules are ready. I&#8217;ll post more information later&#8230; for now, I&#8217;ll just tell that it is a multi-channel, multi-server bot, and that I&#8217;m trying to make its commands with natural language (eg. forward messages from #blah on servername to #bloh on servername).</p>
<p>Happy birthday Diogo!!</p>
<p><b>16 Aug 2000</b></p>
<p>Created advogato account.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2013/03/01/12-years-ago/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing the Ubuntu Finder</title>
		<link>http://blog.labix.org/2013/02/20/introducing-the-ubuntu-finder</link>
		<comments>http://blog.labix.org/2013/02/20/introducing-the-ubuntu-finder#comments</comments>
		<pubDate>Wed, 20 Feb 2013 23:18:57 +0000</pubDate>
		<dc:creator>niemeyer</dc:creator>
				<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Project]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=1228</guid>
		<description><![CDATA[A small and fun experiment is out:]]></description>
				<content:encoded><![CDATA[<p>A small and fun experiment is out:</p>
<p><iframe width="584" height="329" src="http://www.youtube.com/embed/LP_oBftMK7E?feature=oembed" frameborder="0" allowfullscreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2013/02/20/introducing-the-ubuntu-finder/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ethics for code reviewers</title>
		<link>http://blog.labix.org/2013/02/06/ethics-for-code-reviewers</link>
		<comments>http://blog.labix.org/2013/02/06/ethics-for-code-reviewers#comments</comments>
		<pubDate>Wed, 06 Feb 2013 04:26:02 +0000</pubDate>
		<dc:creator>niemeyer</dc:creator>
				<category><![CDATA[Article]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=1185</guid>
		<description><![CDATA[In the previous post, I explored a bit how ephemeral most of the artifacts of software development processes are. One of these processes is code reviewing, which is arguably a major player in code quality, knowledge acquisition, and even team &#8230; <a href="http://blog.labix.org/2013/02/06/ethics-for-code-reviewers">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>In the <a href="http://blog.labix.org/2013/02/04/the-ephemeral-life-of-software-development">previous post</a>, I explored a bit how ephemeral most of the artifacts of software development processes are. One of these processes is code reviewing, which is arguably a major player in code quality, knowledge acquisition, and even team dynamics.</p>
<p>Even being so important, the outcome of the code review process &mdash; the review itself &mdash; tends to reach a very limited audience and have a short life time. It&#8217;ll be hard to change that picture given the nature of reviews: they are conversational, and address specific issues for the integration of a change in the project. At the same time, even if code reviews are not generally useful as permanent documentation, we can increase their value as reference material by improving the quality of those conversations. Having a good conversation has many other great side effects, of course.</p>
<p>As a small step in that direction, what follows are personal guidelines that I have been evolving empirically over the years as a software developer and code reviewer. They may not bring you fortune and fame, and are not always easy to apply, but hopefully they will help improving your experience as a member of your team and the value of those reviews.</p>
<p><span id="more-1185"></span><b>Explain why</b></p>
<p>Unless the change is about an extremely obvious mistake, explain why you&#8217;re suggesting it. If the reasoning was natural to the author, he&#8217;d have done it in the first place. Good explanations also help avoiding the same mistake over and over again, and are much more rewarding to the listener. They also become a target for future references.</p>
<p>If you don&#8217;t have enough time to justify it and would rather provide the review sooner than later, one approach is to just recommend the change and invite the author for a conversation later if that would be helpful. That said, try to have that conversation over a media that may be shared with the rest of the team, or recollected whenever necessary.</p>
<p><b>Be respectful</b></p>
<p>Always keep in mind that there&#8217;s a person on the other side of the wire, not a machine, and that it&#8217;s hard to understand written words with little context. Avoid letting anger and frustration leak into the review, even if you feel it is justified. There&#8217;s no good outcome in those situations.</p>
<p>It doesn&#8217;t matter who broke it, or who coded that silly piece of code. If there is broken code, and the project has reviews, multiple people were in the pipeline for that result, and they were trying to get it right. Take shared ownership of the problem, and look for the solution and for how to avoid such issues in the future.</p>
<p><b>Praise the good work</b></p>
<p>Reviews carry some low energy feel by their very nature. No matter how positive you are about them, and how much the whole team understands and agrees it is for the best, you are in fact looking for places to put your finger in someone else&#8217;s work. For that reason, it is very helpful to take every chance you can of praising logic, design, code organization, or whatever else that you honestly felt was well done. It won&#8217;t ever balance it out, but it will at least remind the author that the contributions are welcome.</p>
<p><b>Suggestions are appreciated</b></p>
<p>Perhaps a longer variable name would be helpful, or that constant could have a more descriptive name?  In many circumstances, the change is indeed subjective, and the gain is pretty marginal. In those cases, if you really can&#8217;t resist the urge to say something, a good approach is a suggestion that may be exercised or not at the author&#8217;s discretion. Ideally, suggest several options that would feel better to you, so that your point is better understood and agreement is easier. That said, read on.</p>
<p><b>Avoid trivialities</b></p>
<p>When reviewing that very simple point, think to yourself: all things considered, does it actually matter?  Is the cost of the author&#8217;s time, and the potential debate, really worth it?  You surely have your opinion about whether to spell &#8220;<i>min &lt;= count</i>&#8221; or &#8220;<i>count &gt;= min</i>&#8220;, but so does everybody else. When it&#8217;s purely a matter of preference, the author is entitled to have one after all.</p>
<p><b>Small branches win</b></p>
<p>Code reviews are useful for a number of secondary reasons, but the primary goal of the code review is to analyze a proposed change, to fix it for inclusion, or to reject it. It&#8217;s often tempting to recommend further changes to be bundled onto the same review, but it&#8217;s important to keep some focus. Are these additional changes tightly related to the original idea, or would they rather be more appropriate on a future branch?</p>
<p>Also keep an eye on large review submissions. It&#8217;s quite rare to see changes of a thousand lines or more that are really an indivisible unit. More often, it ends up like that organically, as a result of the workflow followed by the author. These branches may be very frustrating, both for the author and for reviewers. For the reviewer, it&#8217;s hard to keep the necessary level of attention and enthusiasm for the problem over expanded periods of time. For the author, it will be equally problematic to run over a large review. In some extreme cases, it may be worth going back and breaking down the change into more change sets.</p>
<p>Overall, fast iterations on small branches are much more rewarding to work with.</p>
<p><b>Work with inline comments</b></p>
<p>This is about tooling, but doing anything else should be considered unethical really. If you don&#8217;t have a system that allows the change diff to be seen within the rest of the content, and comments to be made inlined right where you see the issue, implement one right now. Moving to such a system was the most dramatic change in productivity I had as a reviewer in the past several years, and makes the whole experience a lot more bearable for everyone.</p>
<p><b>Enjoy!</b></p>
<p>Make sure you&#8217;re enjoying what you do, and appreciate what your code reviews are achieving. There&#8217;s little point in playing the role of an intelligent computer over extended periods of time if you are unhappy about it. Get yourself your preferred slow-drinking beverage (<a href="http://en.wikipedia.org/wiki/Mate_(beverage)">chimarrão?</a>), perhaps some snacks, a comfortable chair, and relax.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2013/02/06/ethics-for-code-reviewers/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The ephemeral life of software development</title>
		<link>http://blog.labix.org/2013/02/04/the-ephemeral-life-of-software-development</link>
		<comments>http://blog.labix.org/2013/02/04/the-ephemeral-life-of-software-development#comments</comments>
		<pubDate>Mon, 04 Feb 2013 18:29:09 +0000</pubDate>
		<dc:creator>niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Article]]></category>
		<category><![CDATA[Design]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=1178</guid>
		<description><![CDATA[Lately I&#8217;ve been considering the amount of waste we produce during software development, and how to increase the amount of recycled content. I&#8217;m not talking about actual trash, though, but rather about software development artifacts. Over the years, we&#8217;ve learned &#8230; <a href="http://blog.labix.org/2013/02/04/the-ephemeral-life-of-software-development">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Lately I&#8217;ve been considering the amount of waste we produce during software development, and how to increase the amount of recycled content. I&#8217;m not talking about actual trash, though, but rather about software development artifacts.</p>
<p>Over the years, we&#8217;ve learned about and put in practice several means for improving the quality and success rate of projects we create or contribute to. We have practices such as sprints to get people together with high communication bandwidth; we have code reviews for sharing knowledge and improving project quality; we&#8217;ve got technical leadership roles to mentor developers and guide the progress of projects; we&#8217;ve created kanban boards and burndown charts to help people visualize what they&#8217;re going through; and so on.</p>
<p><span id="more-1178"></span>While all of that seems to have helped tremendously, there&#8217;s a sad fact about where we stand: the artifacts of most of these processes are local to their context, and very sensitive to time. That burndown chart is meaningless after it&#8217;s burned, and a kanban has no relevant history. Our technical leads indeed guide their teams, but their wisdom stays with the few people that had the chance to interact with them, and subjectively so. That brilliant code review from our best developers has a very limited audience, and rarely carries any meaning just days after it has been accomplished.</p>
<p>That last one is specially interesting. The process of reviewing code is an intense task, very expensive, and that takes a significant portion of the life of an active developer, and even then very little is carried forward as the outcome of that process. We have no effective means or even culture of sharing the generated wisdom to other teams. In fact, we rarely share these details even within the team itself.  Why was that line changed like this?  Why an interface like that is a bad idea?  Who will instruct the new guy next week, and where did we record a bit of the wisdom of the brilliant guy that has left the company recently?</p>
<p>Unfortunately there&#8217;s probably no easy solution for this problem. At this point, I mainly recognize that most of the efforts I&#8217;ve lead to improve software development for the past several years had a very limited scope. The software itself became immediately better as a result of my efforts, its design became more sensible, and hopefully I contributed a bit to the growth of people around me, but at a company or even community-wide scope, all of these code reviews, sprints, and IRC conversations are buried for very rare revives.</p>
<p>I want to start doing something about this, though. There must be a way to shape these conversations in a more reusable format; in a way that knowledge and agreement can be more proactively preserved and scattered. Perhaps it&#8217;s more about how than it is about what. Perhaps we just need to write more posts like this, and cover more topics related to daily development findings. Not sure. I&#8217;ll be thinking&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2013/02/04/the-ephemeral-life-of-software-development/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Baby feeding statistics with R</title>
		<link>http://blog.labix.org/2013/01/25/baby-feeding-statistics-with-r</link>
		<comments>http://blog.labix.org/2013/01/25/baby-feeding-statistics-with-r#comments</comments>
		<pubDate>Fri, 25 Jan 2013 11:46:08 +0000</pubDate>
		<dc:creator>niemeyer</dc:creator>
				<category><![CDATA[Article]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[R]]></category>
		<category><![CDATA[Snippet]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=1112</guid>
		<description><![CDATA[Our son Otávio was born recently. Right in the first few days, we decided to keep tight control on the feeding times for a while, as it is an intense routine pretty unlike anything else, and obviously critical for the &#8230; <a href="http://blog.labix.org/2013/01/25/baby-feeding-statistics-with-r">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Our son Otávio was born recently. Right in the first few days, we decided to keep tight control on the feeding times for a while, as it is an intense routine pretty unlike anything else, and obviously critical for the health of the baby. I imagined that it wouldn&#8217;t be hard to find an Android app that would do that in a reasonable way, and indeed there are quite a few. We went with <a href="https://play.google.com/store/apps/details?id=com.luckyxmobile.babycare">Baby Care</a>, as it has a polished interface and more features than we&#8217;ll ever use. The app also includes some basic statistics, but not enough for our needs. Luckily, though, it is able to export the data as a CSV file, and post-processing that file with the <a href="http://r-project.org">R language</a> is easy, and allows extracting some fun facts about what the routine of a healthy baby can look like in the first month, as shown below.</p>
<p><span id="more-1112"></span><img src="http://blog.labix.org/wp-content/uploads/2013/01/IMG_20130121_154012-e1359113788882.jpg" alt="Otávio" width="400" height="300" class="aligncenter size-full wp-image-1156" style="border: 0" /></p>
<p>The first thing to do is to import the raw data from the CSV file. It is a one-liner in R:<br />
<code>
<pre>
&gt; info = read.csv("baby-care.csv", header=TRUE)
</pre>
<p></code></p>
<p>Then, this file actually comes with other events that won&#8217;t be processed now, so we&#8217;ll slice it and grab only the rows and columns of interest:<br />
<code>
<pre>
&gt; feeding &lt;- info[info$Event.type == "Breast",
        c("Event.subType", "Start.Time", "End.Time", "Duration")]
</pre>
<p></code></p>
<p>This is how it looks like:<br />
<code>
<pre>
&gt; feeding[100:103,]
    Event.subType       Start.Time         End.Time Duration
129          Left 2013/01/04 13:45 2013/01/04 14:01    00:16
132          Left 2013/01/04 16:21 2013/01/04 16:30    00:09
134         Right 2013/01/04 17:46 2013/01/04 17:54    00:08
</pre>
<p></code></p>
<p>Now things get more interesting. Let&#8217;s extract that duration column into a more useful vector, and do some basic analysis:</p>
<p><code>
<pre>
&gt; duration &lt;- as.difftime(as.vector(feeding$Duration), &quot;%H:%M&quot;)

&gt; length(duration)
[1] 365

&gt; total = sum(duration)
&gt; units(total) = &quot;hours&quot;
&gt; total
Time difference of 63.71667 hours

&gt; mean(duration)
Time difference of 10.47397 mins
&gt; sd(duration)
[1] 5.937172

</pre>
<p></code></p>
<p>A total of 63 hours surprised me, but the mean time of around 10 minutes per feeding is within the recommendation, and the standard deviation looks reasonable. It may be more conveniently pictured as a histogram:<br />
<code>
<pre>
&gt; hist(as.numeric(duration), breaks="FD",
    col="blue", main="", xlab="Minutes")
</pre>
<p></code></p>
<p><a href="http://blog.labix.org/wp-content/uploads/2013/01/duration-hist.png"><img src="http://blog.labix.org/wp-content/uploads/2013/01/duration-hist.png" alt="Duration histogram" width="480" height="480" class="aligncenter size-full wp-image-1126" /></a></p>
<p>Another point we were interested on is if both sides are properly balanced:<br />
<code>
<pre>
&gt; sides &lt;- c("  Right", "  Left")
&gt; tapply(duration, feeding$Event.subType, mean)[sides]
   Right     Left 
10.72283 10.22099
</pre>
<p></code></p>
<p>Looks good.</p>
<p>All of the analysis so far goes over the whole period, but how has the daily intake changed over time? We&#8217;ll need an additional vector to compute this and visualize in a chart:<br />
<code>
<pre>
&gt; day &lt;- format(strptime(feeding$Start.Time, &quot;%Y/%m/%d %H:%M&quot;),
                &quot;%Y/%m/%d&quot;)
&gt; perday &lt;- tapply(duration, day, sum)
&gt; mean(perday)
[1] 136.5357
&gt; sd(perday)
[1] 53.72735
&gt; sd(perday[8:length(perday)])
[1] 17.49735

&gt; plot(perday, type="h", col="blue", xlab="Day", ylab="Minutes")
</pre>
<p></code></p>
<p><a href="http://blog.labix.org/wp-content/uploads/2013/01/duration-perday.png"><img src="http://blog.labix.org/wp-content/uploads/2013/01/duration-perday.png" alt="Daily duration" width="480" height="480" class="aligncenter size-full wp-image-1136" /></a></p>
<p>The mean looks good, with about two hours every day. The standard deviation looks high on a first look, but it&#8217;s actually not that bad if we take off the first few days. Looking at the graph shows why: the slope on the left-hand side, which is expected as there&#8217;s less milk and the baby has more trouble right after birth.</p>
<p>The chart shows a red flag, though: one day seems well below the mean. This is something to be careful about, as babies can get into a loop where they sleep too much and miss being hungry, the lack of feeding causes hypoglycemia, which causes more sleep, and it doesn&#8217;t end up well. A rule of thumb is to wake the baby up every two hours in the first few days, and at most every four hours once he stabilizes for the following weeks.</p>
<p>So, this was another point of interest: what are the intervals between feedings?</p>
<p><code>
<pre>
&gt; start = strptime(feeding$Start.Time, "%Y/%m/%d %H:%M")
&gt; end = strptime(feeding$End.Time, "%Y/%m/%d %H:%M")
&gt; interval &lt;- start[-1]-end[-length(end)]

&gt; hist(as.numeric(interval), breaks="FD", col="blue",
       main="", xlab="Minutes")
</code></pre>
<p><a href="http://blog.labix.org/wp-content/uploads/2013/01/interval-hist.png"><img src="http://blog.labix.org/wp-content/uploads/2013/01/interval-hist.png" alt="Interval histogram" width="480" height="480" class="aligncenter size-full wp-image-1146" /></a></p>
<p>Seems great, with most feedings well under two hours. There's a worrying outlier, though, of more than 6 hours. Unsurprisingly, it happened over night:</p>
<p><code>
<pre>
&gt; feeding$End.Time[interval &gt; 300]
[1] 2013/01/07 00:52
</pre>
<p></code></p>
<p>It wasn't a significant issue, but we don't want that happening often while his body isn't yet ready to hold enough energy for a full night of sleep. That's the kind of reason we've been monitoring him, and is important because <i>our</i> bodies are eager to get full nights of sleep, which opens the door for unintended slack. As a reward for that kind of control, we've got the chance to enjoy not only his health, but also an admirable mood.</p>
<p>Love, Dad.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2013/01/25/baby-feeding-statistics-with-r/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Multi-doc transactions for MongoDB</title>
		<link>http://blog.labix.org/2012/08/22/multi-doc-transactions-for-mongodb</link>
		<comments>http://blog.labix.org/2012/08/22/multi-doc-transactions-for-mongodb#comments</comments>
		<pubDate>Wed, 22 Aug 2012 19:55:30 +0000</pubDate>
		<dc:creator>niemeyer</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Article]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Go]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[Project]]></category>
		<category><![CDATA[Test]]></category>

		<guid isPermaLink="false">http://blog.labix.org/?p=1030</guid>
		<description><![CDATA[I&#8217;m glad to announce experimental support for multi-document transactions in the mgo driver that integrates MongoDB with the Go language. The support is done via a driver extension, so it works with any MongoDB release supported by the driver (&#62;= &#8230; <a href="http://blog.labix.org/2012/08/22/multi-doc-transactions-for-mongodb">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I&#8217;m glad to announce experimental support for multi-document transactions in the <a href="http://labix.org/mgo">mgo driver</a> that integrates <a href="http://mongodb.org">MongoDB</a> with the <a href="http://golang.org">Go language</a>. The support is done via a driver extension, so it works with any MongoDB release supported by the driver (&gt;= 1.8).</p>
<h2>Features</h2>
<p>Here is a quick highlight list to get your brain ticking before the details:</p>
<ul>
<li>Supports sharding</li>
<li>Operations may span multiple collections</li>
<li>Handles changes, inserts and removes</li>
<li>Supports pre-conditions</li>
<li>Self-healing</li>
<li>No additional locks or leases</li>
<li>Works with existing data</li>
</ul>
<p>Let&#8217;s see what these actually mean and how the goodness is done.</p>
<p><span id="more-1030"></span><br />
<h2>The problem being addressed</h2>
<p>The typical example is a bank transaction: imagine you have two documents representing accounts for different people, and you want to transfer 100 bucks from Aram to Ben. Despite the apparent simplicity in that description, there are a number of edge cases that turn it into a non-trivial change.</p>
<p>Imagine an agent processing the change following these steps:</p>
<ol>
<li>Is Ben&#8217;s account valid?</li>
<li>Take 100 bucks out of Aram&#8217;s account if its balance is above 100</li>
<li>Insert 100 bucks into Ben&#8217;s account</li>
</ol>
<p>Note that this description already assumes the availability of some single-document atomic operations as supported by MongoDB. Even then, how many race conditions and crash-related problems can you count? Here are some spoilers that hint at the problem complexity:</p>
<ul>
<li>What if Ben cancels his account after (1)?</li>
<li>What if the agent crashes after (2)?</li>
</ul>
<h2>How it works</h2>
<p>Thanks to the availability of single-document atomic operations, it is be possible to craft a sequence of changes that manipulate documents in a way that supports multi-document transactional behavior. This works as long as the clients agree to use the same conventions.</p>
<p>This isn&#8217;t exactly news, though, and there&#8217;s even documentation describing <a href="http://cookbook.mongodb.org/patterns/perform-two-phase-commits/">how one can explore these ideas</a>. The challenge is in crafting a generic mechanism that not only does the basics but goes beyond by supporting inserts and removes, being workload agnostic, behaving correctly on crashes (!), and yet remaining pleasant to use. That&#8217;s the territory being explored.</p>
<p>The implemented semantics offers an isolation level that allows <a href="http://en.wikipedia.org/wiki/Isolation_(database_systems)">non-repeatable reads</a> to occur (a partially committed transaction is visible), but the changes are guaranteed to only be visible in the order specified in the transaction, and once any change is done the transaction is guaranteed to be applied completely without intervening changes in the affected documents (no <a href="http://en.wikipedia.org/wiki/Isolation_(database_systems)">dirty reads</a>). Among other things, this means one can use any existing mechanism at read time.</p>
<p>When writing documents that are affected by the transaction mechanism, one must necessarily use <a href="http://labix.org/v2/mgo/txn">the API of the new <em>mgo/txn</em> package</a>, which ended up surprisingly thin and straightforward. In other words for emphasis: if you modify fields that are affected by the transaction mechanism both with and without <i>mgo/txn</i>, it will <b>misbehave</b> arbitrarily. Fields that are <i>read or written</i> by <i>mgo/txn</i> must only be changed using <i>mgo/txn</i>.</p>
<p>Using the example described above, the bank account transfer might be done as:</p>
<pre>
runner := txn.NewRunner(tcollection)
ops := []txn.Op{{
        C:      "accounts", 
        Id:     "aram",
        Assert: M{"balance": M{"$gte": 100}},
        Update: M{"$inc": M{"balance": -100}},
}, {
        C:      "accounts",
        Id:     "ben",
        Assert: M{"valid": true},
        Update: M{"$inc": M{"balance": 100}},
}}
id := bson.NewObjectId() // Optional
err := runner.Run(ops, id, nil)
</pre>
<p>The assert and update values are usual MongoDB <a href="http://www.mongodb.org/display/DOCS/Querying">querying</a> and <a href="http://www.mongodb.org/display/DOCS/Updating">updating</a> documents. The tcollection is a MongoDB collection that is used to atomically insert the transaction details into the database. As long as that document makes it into the database, the transaction is guaranteed to be eventually entirely applied or entirely aborted. The exact moment when this happens is defined by whether there are other transactions in progress and whether a communication problem occurs and when it occurs, as described below.</p>
<h2>Concurrency and crash-proofness</h2>
<p>Perhaps the most interesting piece of the puzzle when coming up with a nice transaction mechanism is defining what happens when an agent misbehaves, even more in a world where there are multiple distributed transaction runners. If there are locks, someone must unlock when a runner crashes, and must know the difference between running slowly and crashing. If there are leases, the lease boundary becomes an issue. In both cases, the speed of the overall system would become bounded by the speed of the slowest runner.</p>
<p>Instead of falling onto those issues, the implemented mechanism observes the transactions being attempted on the affected documents, orders them in a globally agreed way, and pushes all of their operations <i>concurrently</i>.</p>
<p>To illustrate the behavior, imagine again the described scenario of bank transferences:</p>
<p><a href="http://blog.labix.org/wp-content/uploads/2012/08/txn-diagram.png"><img src="http://blog.labix.org/wp-content/uploads/2012/08/txn-diagram.png" alt="" width="500" height="225" class="aligncenter size-full wp-image-1068" /></a></p>
<p>In this diagram there are two transactions being attempted, T1 and T2. The first is a transference from Aram to Ben, and the second is a transference from Ben to Carl. If a runner starts executing T2 while T1 is still being applied by a different runner, the first runner will <i>pick T1 up and complete it</i> before starting to work on T2 which is its real goal. This works even if the original runner of T1 died while it was in progress. In reality, there&#8217;s little difference between the original runner of T1 and another runner that observes T1 on its way.</p>
<p>There&#8217;s a chance that T1&#8242;s runner died too soon, though, and it hasn&#8217;t had a chance to even start the transaction by tagging Ben&#8217;s account document as participating in it. In that case, T2 will be pushed forward by its own runner independently, since there&#8217;s nothing on its way. T1 isn&#8217;t lost, though, and it may be resumed at any point by calling the runner&#8217;s <a href="http://labix.org/v2/mgo/txn#Runner.Resume">Resume</a> or <a href="http://labix.org/v2/mgo/txn#Runner.ResumeAll">ResumeAll</a> methods.</p>
<p>The whole logic is implemented without introducing any new globally shared point of coordination. It works if documents are in different collections, different shards, and it works even if the transaction collection itself is sharded across multiple backends for scalability purposes.</p>
<h2>The testing approach</h2>
<p>While a lot of thinking was put onto the way the mechanism works, this is of course non-trivial and bug-inviting logic. In an attempt to nail down bugs early on, a testing environment was put in place to simulate multiple runners in a conflicting workload. To make matters more realistic, this simulation happens in a harsh scenario with faults and artificial slowdowns being randomly injected into the system. At the end, the result is evaluated to see if the changes performed respected the invariants established.</p>
<p>While hundreds of thousands of transactions have been successfully run in this fashion, the package should still be considered experimental at this point, and its API is still prone to change.</p>
<h2>There&#8217;s one race</h2>
<p>There&#8217;s one known race that&#8217;s worth mentioning, and it was consciously left there for the moment as a tradeoff. The race shows itself when inserting a new document, at the point in time when the decision has been made that the insert was genuinely good. At this exact moment, if that runner is <i>frozen</i> for long enough that would allow for a different runner to insert the document and remove it again, and then the original runner is <i>unfrozen</i> without any errors or timeouts, it will naturally go on and insert the new document.</p>
<p>There are multiple solutions for this problem, but they present their own disadvantages. One solution would be to manipulate the document instead of removing it, but that would leave the collection with ghost content that has to be cared for, and that&#8217;s an unwanted side effect. A second solution would be to use the internal <i>applyOps</i> machinery that MongoDB uses in its sharding implementation, but that would mean that collections affected by transactions couldn&#8217;t be sharded, which is another unwanted side effect (please vote for <a href="https://jira.mongodb.org/browse/SERVER-1439">SERVER-1439</a> so we can use it).</p>
<h2>Have fun!</h2>
<p>I hope the package serves you well, and if you would like to talk further about it, please join the <a href="https://groups.google.com/forum/?fromgroups#!forum/mgo-users">mgo-users</a> mailing list and drop a message.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.labix.org/2012/08/22/multi-doc-transactions-for-mongodb/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
