<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>The Knut Hellan Blog</title>
	<atom:link href="http://knuthellan.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://knuthellan.com</link>
	<description>General geekyness and starting a company</description>
	<lastBuildDate>Sun, 20 May 2012 20:20:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='knuthellan.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>The Knut Hellan Blog</title>
		<link>http://knuthellan.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://knuthellan.com/osd.xml" title="The Knut Hellan Blog" />
	<atom:link rel='hub' href='http://knuthellan.com/?pushpress=hub'/>
		<item>
		<title>Crash course in Erlang</title>
		<link>http://knuthellan.com/2012/05/15/crash-course-in-erlang/</link>
		<comments>http://knuthellan.com/2012/05/15/crash-course-in-erlang/#comments</comments>
		<pubDate>Tue, 15 May 2012 08:36:31 +0000</pubDate>
		<dc:creator>Knut O. Hellan</dc:creator>
				<category><![CDATA[code example]]></category>

		<guid isPermaLink="false">http://knuthellan.com/?p=381</guid>
		<description><![CDATA[This is a summary of a talk I held Monday May 14 2012 at an XP Meetup in Trondheim. It is meant as a teaser for listeners to play with Erlang themselves. First, some basic concepts. Erlang has a form of constant called atom that is defined on first use. They are typically used as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=381&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This is a summary of a talk I held Monday May 14 2012 at an XP Meetup in Trondheim. It is meant as a teaser for listeners to play with Erlang themselves.</p>
<p>First, some basic concepts. Erlang has a form of constant called atom that is defined on first use. They are typically used as enums or symbols in other languages. Variables in Erlang are immutable so assigning a new value to an existing variable is not allowed</p>
<p><code><br />
1&gt; A = 1.<br />
1<br />
2&gt; A = 2.<br />
** exception error: no match of right hand side value 2<br />
3&gt; A = 1.<br />
1<br />
</code></p>
<p>The third statement shows that the assignment is actually a pattern match, but the first statement assigns a value to the variable.</p>
<p>Lists and property lists are central in Erlang. The lists behave much like lists in other functional languages or arrays in other types of languages. Property lists are a special type of list that is used as a hash or dict in other languages</p>
<p><code><br />
1&gt; A = [1,2,3,4,5,6,7].<br />
[1,2,3,4,5,6,7]<br />
2&gt; lists:nth(1, A).<br />
1<br />
3&gt; lists:nth(7, A).<br />
7<br />
4&gt; lists:last(A).<br />
7<br />
5&gt;<br />
5&gt; length(A).<br />
7<br />
6&gt; [Head|Tail] = A.<br />
[1,2,3,4,5,6,7]<br />
7&gt; Head.<br />
1<br />
8&gt; Tail.<br />
[2,3,4,5,6,7]<br />
</code></p>
<p>The Erlang shell is useful and when defining a module, it can be compiled and run directly from the shell. <a href="http://en.wikipedia.org/wiki/Fibonacci_number">Fibonacci numbers</a> are an easy demonstration of the capabilities of a functional language.</p>
<p><code><br />
-module(fib).</p>
<p>-export([fib/1]).</p>
<p>fib(0) -&gt; 0;<br />
fib(1) -&gt; 1;<br />
fib(N) -&gt; fib(N - 1) + fib(N - 2).<br />
</code></p>
<p>Compile and test</p>
<p><code><br />
1&gt; c(fib).<br />
{ok,fib}<br />
2&gt; lists:foreach(fun(N) -&gt; io:format("fib ~p = ~p~n", [N, fib:fib(N)]) end, [1,2,3,4,5,6,7]).<br />
fib 1 = 1<br />
fib 2 = 1<br />
fib 3 = 2<br />
fib 4 = 3<br />
fib 5 = 5<br />
fib 6 = 8<br />
fib 7 = 13<br />
ok</code></p>
<p>&nbsp;</p>
<p><strong>Messaging</strong></p>
<p>Erlang has a very powerful messaging system. This system supports distributed messaging.</p>
<p>First, a simple message loop that isn&#8217;t really a loop at all. Calling loop:init() will spawn a separate process waiting to receive messages.<br />
<script src="https://gist.github.com/2699609.js?file=loop.erl"></script></p>
<p><code><br />
1&gt; c(loop).<br />
{ok,loop}<br />
2&gt; Pid = loop:init().<br />
&lt;0.39.0&gt;<br />
3&gt; loop:ping(Pid).<br />
{ping,&lt;0.32.0&gt;}<br />
4&gt; flush().<br />
Shell got {pong,&lt;0.39.0&gt;}<br />
ok<br />
5&gt; loop:ping(Pid).<br />
{ping,&lt;0.32.0&gt;}<br />
6&gt; flush().<br />
ok<br />
</code></p>
<p>The first time the loop is pinged, it replies pong, but the second time, nothing happens. When a message is received, the loop function will finish.</p>
<p><strong>Timeouts</strong></p>
<p>Message receive statements in Erlang may time out:<br />
<script src="https://gist.github.com/2699609.js?file=loop1.erl"></script><br />
<code><br />
 c(loop1).<br />
{ok,loop1}<br />
2&gt;  Pid = loop1:init().<br />
&lt;0.39.0&gt;<br />
I have decided to die<br />
</code><br />
The message loop times out after 1000 millisecond and exits the function.</p>
<p><strong>An actual message loop</strong></p>
<p>Let&#8217;s convert the message handling function loop into a real loop through tail recursion. Tail recursion means this loop can run forever without the growing stack otherwise caused by recursion.<br />
<script src="https://gist.github.com/2699609.js?file=loop2.erl"></script></p>
<p><code><br />
1&gt; c(loop2).<br />
{ok,loop2}<br />
2&gt; Pid = loop2:init().<br />
&lt;0.39.0&gt;<br />
3&gt; loop2:ping(Pid).<br />
{ping,&lt;0.32.0&gt;}<br />
4&gt; loop2:ping(Pid).<br />
{ping,&lt;0.32.0&gt;}<br />
5&gt; flush().<br />
Shell got {pong,&lt;0.39.0&gt;}<br />
Shell got {pong,&lt;0.39.0&gt;}<br />
ok<br />
6&gt; loop2:ping(Pid).<br />
{ping,&lt;0.32.0&gt;}<br />
7&gt; flush().<br />
Shell got {pong,&lt;0.39.0&gt;}<br />
ok<br />
</code><br />
Calling ping multiple times means we get multiple replies as we should from a message loop.<br />
<code></p>
<p></code><br />
Calling stop terminates the message loop.<br />
<code><br />
8&gt; loop2:stop(Pid).<br />
stop<br />
9&gt; loop2:ping(Pid).<br />
{ping,&lt;0.32.0&gt;}<br />
10&gt; flush(). &nbsp; &nbsp; &nbsp; &nbsp;<br />
ok<br />
</code><br />
<strong>State</strong></p>
<p>While Erlang is a functional language and should be stateless, we may insert state into our message loop. Note that the variable State in the example never change value within a context.<br />
<script src="https://gist.github.com/2699609.js?file=loop3.erl"></script><br />
<code><br />
1&gt; c(loop3).<br />
{ok,loop3}<br />
2&gt; Pid = loop3:init().<br />
&lt;0.39.0&gt;<br />
3&gt; loop3:ping(Pid).<br />
{ping,&lt;0.32.0&gt;}<br />
4&gt; loop3:ping(Pid).<br />
{ping,&lt;0.32.0&gt;}<br />
5&gt; loop3:ping(Pid).<br />
{ping,&lt;0.32.0&gt;}<br />
6&gt; flush().<br />
Shell got {pong,&lt;0.39.0&gt;,0}<br />
Shell got {pong,&lt;0.39.0&gt;,1}<br />
Shell got {pong,&lt;0.39.0&gt;,2}<br />
ok<br />
7&gt; loop3:stop(Pid).<br />
Final state = 3<br />
stop<br />
</code><br />
Every ping leads to an increment of State and stop prints the final value.</p>
<p><strong>Distributed messaging</strong></p>
<p><script src="https://gist.github.com/2699609.js?file=loop4.erl"></script><br />
First, we start an Erlang shell with the short name left and a cookie meetup. The cookie is used by the shells to find each other.</p>
<p><code>erl -sname left -setcookie meetup</code><br />
Then start the loop in this shell.<br />
<code><br />
(left@localhost)1&gt; c(loop4).<br />
{ok,loop4}<br />
(left@localhost)2&gt; loop4:init().<br />
true<br />
</code></p>
<p>Observe that the prompt includes the shortname of the node.</p>
<p>Start a new shell called right with the same cookie:<br />
<code>erl -sname right -setcookie meetup</code></p>
<p>Send messages to the loop running in the other shell and observe the response<br />
<code><br />
(right@localhost)1&gt; loop4:ping(left@localhost).<br />
Got 0 from &lt;6032.45.0&gt;<br />
ok<br />
(right@localhost)2&gt; loop4:ping(left@localhost).<br />
Got 1 from &lt;6032.45.0&gt;<br />
ok<br />
</code></p>
<p>Sending stop terminates the loop<br />
<code><br />
(right@localhost)3&gt; loop4:stop(left@localhost).<br />
stop<br />
</code></p>
<p>This causes the following output in the left shell<br />
<code><br />
Final state = 2<br />
(left@localhost)3&gt;<br />
</code></p>
<p><strong>OTP or at least generic servers</strong></p>
<p>It&#8217;s hard to talk about Erlang and messaging without at least touching OTP and generic servers. A module defining a generic server needs to specify -behaviour(gen_server) and define some functions used by the generic server framework. This file also introduces Erlang macros as a built-in macro ?MODULE is used here. ?MODULE contains the name of the module as an atom.</p>
<script src="https://gist.github.com/2699609.js?file=gobbler.erl"></script>
<p>The functions representing an API to this server are count, increment, start_link and stop. We start the server by calling start_link and then call count and increment to see what happens.</p>
<p><code><br />
1&gt; c(gobbler).<br />
{ok,gobbler}<br />
2&gt; gobbler:start_link().<br />
{ok,}<br />
3&gt; gobbler:count().<br />
0<br />
4&gt; gobbler:count().<br />
0<br />
5&gt; gobbler:increment().<br />
ok<br />
6&gt; gobbler:increment().<br />
ok<br />
7&gt; gobbler:count().<br />
2<br />
</code></p>
<p>No surprises there. When gobbler:count is called, gen_sever:call(?MODULE, count) sends the message count to the message loop of the server. The message loop calls handle_call(count, From, State) with From identifying the caller and State containing the State variable which is set to 0 in init called by start_link. handle_call(count, From, State) returns {reply, State, State} with reply being mandatory in a call to indicate that the loop should send a reply to the caller, the first State is the message returned to the caller and the last State is the new value of the State variable to send into the next iteration of the loop.</p>
<p>gobbler:increment uses gen_server:cast(?MODULE, increment) to send an increment message to the server. Cast sends the message and forgets it, meaning the caller will not expect a reply or even wait for one.</p>
<p>Selection of the right handle_cast is a result of pattern matching. increment matches the first handle_cast so it is executed. A cast of stop would have matched the second one.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/knuthellan.wordpress.com/381/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/knuthellan.wordpress.com/381/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/knuthellan.wordpress.com/381/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/knuthellan.wordpress.com/381/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/knuthellan.wordpress.com/381/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/knuthellan.wordpress.com/381/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/knuthellan.wordpress.com/381/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/knuthellan.wordpress.com/381/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/knuthellan.wordpress.com/381/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/knuthellan.wordpress.com/381/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/knuthellan.wordpress.com/381/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/knuthellan.wordpress.com/381/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/knuthellan.wordpress.com/381/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/knuthellan.wordpress.com/381/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=381&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://knuthellan.com/2012/05/15/crash-course-in-erlang/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">knuthellan</media:title>
		</media:content>
	</item>
		<item>
		<title>Iterating over joins in Pig</title>
		<link>http://knuthellan.com/2012/04/20/iterating-over-joins-in-pig/</link>
		<comments>http://knuthellan.com/2012/04/20/iterating-over-joins-in-pig/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 14:22:45 +0000</pubDate>
		<dc:creator>Knut O. Hellan</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://knuthellan.com/?p=372</guid>
		<description><![CDATA[Apache Pig is a fantastic language for processing data. It is sometimes incredibly annoying, but it beats the hell out of writing a ton of map reduces and chaining them together. When iterating over joins, an issue that I know that I&#8217;m not the only one having ran into is referencing data after a join [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=372&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Apache Pig is a fantastic language for processing data. It is sometimes incredibly annoying, but it beats the hell out of writing a ton of map reduces and chaining them together. When iterating over joins, an issue that I know that I&#8217;m not the only one having ran into is referencing data after a join in pig.</p>
<p>Normally, you access fields using the dereference operators . or # depending on the data type. The period symbol, . is used for tuples and bags, i.e. tuple.field0, tuple.field1, bag.field0, bag.field1. Maps are dereference with a hash, #, i.e. map#&#8217;field0&#8242;, map#&#8217;field1&#8242;.</p>
<p>This does not work after a join. The expected iteration after a JOIN:</p>
<p><code><br />
joined = JOIN list0 BY key, list1 BY key;<br />
purified = FOREACH joined GENERATE list0.key;<br />
</code></p>
<p>This will fail with the obscure error: &#8220;scalar has more than one row in the output&#8221;. This error message is a known problem is and there is a <a href="https://issues.apache.org/jira/browse/PIG-2134" title="ticket for this in Pig's Jira"></a>. As can be seen from the ticket, the correct way to iterate over the join is by using the relation operator, :: instead of the dereferencing operators like this:</p>
<p><code><br />
joined = JOIN list0 BY key, list1 BY key;<br />
purified = FOREACH joined GENERATE list0::key;<br />
</code></p>
<p>If you fall for the temptation of skipping the name of the list to get the field from like this:<br />
<code><br />
joined = JOIN list0 BY key, list1 BY key;<br />
purified = FOREACH joined GENERATE key;<br />
</code></p>
<p>You will get the more informative message: &#8220;Found more than one match: list0::key, list1::key&#8221;.</p>
<p>What you are really doing after a join is addressing columns in relations. For users, addressing columns in a relation with a period would be easier, but using :: might make the underlying code easier to understand.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/knuthellan.wordpress.com/372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/knuthellan.wordpress.com/372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/knuthellan.wordpress.com/372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/knuthellan.wordpress.com/372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/knuthellan.wordpress.com/372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/knuthellan.wordpress.com/372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/knuthellan.wordpress.com/372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/knuthellan.wordpress.com/372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/knuthellan.wordpress.com/372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/knuthellan.wordpress.com/372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/knuthellan.wordpress.com/372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/knuthellan.wordpress.com/372/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/knuthellan.wordpress.com/372/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/knuthellan.wordpress.com/372/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=372&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://knuthellan.com/2012/04/20/iterating-over-joins-in-pig/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">knuthellan</media:title>
		</media:content>
	</item>
		<item>
		<title>Hadoop Status Reporting from Ruby</title>
		<link>http://knuthellan.com/2012/02/21/hadoop-status-reporting-from-ruby/</link>
		<comments>http://knuthellan.com/2012/02/21/hadoop-status-reporting-from-ruby/#comments</comments>
		<pubDate>Tue, 21 Feb 2012 14:32:09 +0000</pubDate>
		<dc:creator>Knut O. Hellan</dc:creator>
				<category><![CDATA[hadoop]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://knuthellan.com/?p=363</guid>
		<description><![CDATA[Hadoop Map-Reduce is a great tool for analyzing and processing large amount of data. There are a few things one needs to keep in mind when working with Hadoop. This is the simple solution to one possibly annoying problem. Hadoop expects reducers to emit something regularly. If a reducer runs for a long time without [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=363&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Hadoop Map-Reduce is a great tool for analyzing and processing large amount of data. There are a few things one needs to keep in mind when working with Hadoop. This is the simple solution to one possibly annoying problem.</p>
<p style="text-align:center;"><a href="http://hadoop.apache.org/"><img class="aligncenter" src="http://hadoop.apache.org/images/hadoop-logo.jpg" alt="Hadoop Logo" /></a></p>
<p>Hadoop expects reducers to emit something regularly. If a reducer runs for a long time without output, it will be terminated and retried. The error message in this case is something like &#8220;Task attempt X failed to report status for Y seconds&#8221;.</p>
<p>I bet some of you are thinking that this should not be a problem since the mappers should do all the work and not the reducers. This is mostly true, but if the job of the reducer is to feed a lot of data to a database that is not write-optimized, things may take a little time.</p>
<p>The trick is to regularly write to STDERR to let Hadoop know that your reducer is healthy and progressing.</p>
<p>Add this line to the input processing loop of your reducer:<br />
<code><br />
STDERR.puts("reporter:status:things_ok") unless (count += 1) % 1000 &gt; 0<br />
</code></p>
<p>This will emit <em>reporter:status:things_ok</em> every 1000 items which is a fine magical number. Substitute your favorite magic number as long as it&#8217;s not too big.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/knuthellan.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/knuthellan.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/knuthellan.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/knuthellan.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/knuthellan.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/knuthellan.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/knuthellan.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/knuthellan.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/knuthellan.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/knuthellan.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/knuthellan.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/knuthellan.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/knuthellan.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/knuthellan.wordpress.com/363/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=363&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://knuthellan.com/2012/02/21/hadoop-status-reporting-from-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">knuthellan</media:title>
		</media:content>

		<media:content url="http://hadoop.apache.org/images/hadoop-logo.jpg" medium="image">
			<media:title type="html">Hadoop Logo</media:title>
		</media:content>
	</item>
		<item>
		<title>Case statement pitfall when migrating to Ruby 1.9</title>
		<link>http://knuthellan.com/2011/08/01/case-statement-pitfall-when-migrating-to-ruby-1-9/</link>
		<comments>http://knuthellan.com/2011/08/01/case-statement-pitfall-when-migrating-to-ruby-1-9/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 14:34:19 +0000</pubDate>
		<dc:creator>Knut O. Hellan</dc:creator>
				<category><![CDATA[code example]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://knuthellan.com/?p=329</guid>
		<description><![CDATA[I have been using Rubinius 2.0 to run machine learning experiments with libsvm lately. When running in Ruby 1.9.2, I noticed that my classifier always classified all samples as negative. I though this was caused by issues with libsvm-ruby-swig so I recompiled libsvm-ruby-swig from scratch including rerunning swig, but nothing changed. Next, I changed to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=329&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have been using Rubinius 2.0 to run machine learning experiments with libsvm lately. When running in Ruby 1.9.2, I noticed that my classifier always classified all samples as negative. I though this was caused by issues with <a href="https://github.com/tomz/libsvm-ruby-swig">libsvm-ruby-swig</a> so I recompiled libsvm-ruby-swig from scratch including rerunning swig, but nothing changed. Next, I changed to use <a href="https://github.com/bdigital/libsvmffi">libsvmffi</a> instead, but the result was the same. Realizing that I actually had some tests running av very simple classifier and that these tests passed on 1.9.2 made me look closer at the code. What I found was that the behavior of the Ruby case statement has changed from 1.8.7 to 1.9.2.</p>
<p>For if statements, 1 is equal to 1.0 in both 1.8.7 and 1.9, but while 1 matches 1.0 in 1.8.7 case statements, it does not in 1.9.2.</p>
<p>Code snippet that shows the difference:</p>
<blockquote>
<pre>
#!/usr/bin/env ruby

puts case 1.0
when 1
  "yay"
else
  "nay"
end
</pre>
</blockquote>
<p>First the output of irb when running 1.8.7:</p>
<blockquote>
<pre>
$ rvm use ruby-1.8.7
Using /usr/local/rvm/gems/ruby-1.8.7-p334
$ ./case.rb 
<b>yay</b>
</pre>
</blockquote>
<p>And the same in 1.9.2:</p>
<blockquote>
<pre>
$ rvm use ruby-1.9.2
Using /usr/local/rvm/gems/ruby-1.9.2-p180
$ ./case.rb 
<b>nay</b>
</pre>
</blockquote>
<p>Needless to say, I was puzzled by this result, but I was more surprised by the 1.8.7 behavior than 1.9.2. My assumption when I wrote the code was that I was dealing with integer values and since it worked, I forgot about it. Next time you see different behavior between 1.8.7 and 1.9.2 it might be worth reviewing case statements.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/knuthellan.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/knuthellan.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/knuthellan.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/knuthellan.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/knuthellan.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/knuthellan.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/knuthellan.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/knuthellan.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/knuthellan.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/knuthellan.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/knuthellan.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/knuthellan.wordpress.com/329/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/knuthellan.wordpress.com/329/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/knuthellan.wordpress.com/329/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=329&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://knuthellan.com/2011/08/01/case-statement-pitfall-when-migrating-to-ruby-1-9/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">knuthellan</media:title>
		</media:content>
	</item>
		<item>
		<title>CouchDB and the web</title>
		<link>http://knuthellan.com/2010/09/09/couchdb-and-the-web/</link>
		<comments>http://knuthellan.com/2010/09/09/couchdb-and-the-web/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 12:59:52 +0000</pubDate>
		<dc:creator>Knut O. Hellan</dc:creator>
				<category><![CDATA[code example]]></category>
		<category><![CDATA[couchdb]]></category>

		<guid isPermaLink="false">http://knuthellan.com/?p=316</guid>
		<description><![CDATA[This is my presentation from JavaZone 2010 Note that during my presentation, I showed the view section and basic replication directly in Futon instead of showing the fallback in the slides. What I did show was mostly the same, but naturally I showed some variations on the mappers as well.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=316&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="https://docs.google.com/present/view?id=ddjngdz7_60cgznwj85">This is my presentation from JavaZone 2010</a></p>
<p>Note that during my presentation, I showed the view section and basic replication directly in Futon instead of showing the fallback in the slides. What I did show was mostly the same, but naturally I showed some variations on the mappers as well.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/knuthellan.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/knuthellan.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/knuthellan.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/knuthellan.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/knuthellan.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/knuthellan.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/knuthellan.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/knuthellan.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/knuthellan.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/knuthellan.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/knuthellan.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/knuthellan.wordpress.com/316/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/knuthellan.wordpress.com/316/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/knuthellan.wordpress.com/316/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=316&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://knuthellan.com/2010/09/09/couchdb-and-the-web/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">knuthellan</media:title>
		</media:content>
	</item>
		<item>
		<title>Feature prioritization for Pillow the CouchDB shard manager</title>
		<link>http://knuthellan.com/2010/07/14/feature-prioritization-for-pillow-the-couchdb-shard-manager/</link>
		<comments>http://knuthellan.com/2010/07/14/feature-prioritization-for-pillow-the-couchdb-shard-manager/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 11:31:59 +0000</pubDate>
		<dc:creator>Knut O. Hellan</dc:creator>
				<category><![CDATA[couchdb]]></category>
		<category><![CDATA[scaling]]></category>
		<category><![CDATA[Storage]]></category>

		<guid isPermaLink="false">http://knuthellan.com/?p=303</guid>
		<description><![CDATA[I have now reached the end of my todo list for Pillow. That doesn&#8217;t mean it&#8217;s finished and ready to be stamped version 1.0. In it&#8217;s current incarnation it is fully usable and production ready, but in order to earn a 1.0 it needs to do a bit more. The current resharding always doubles the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=303&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have now reached the end of my todo list for <a href="http://github.com/khellan/Pillow">Pillow</a>. That doesn&#8217;t mean it&#8217;s finished and ready to be stamped version 1.0. In it&#8217;s current incarnation it is fully usable and production ready, but in order to earn a 1.0 it needs to do a bit more.</p>
<p>The current resharding always doubles the number of servers required. Since you may overshard, that doesn&#8217;t necessarily mean you have to double the number of physical servers, but you need to organize more <a href="http://couchdb.apache.org/">CouchDB</a> instances than you might otherwise need. Smoother sharding algorithms that enable addition of single additional servers exist (<a href="http://en.wikipedia.org/wiki/Consistent_hashing">consistent hashing</a>) so Pillow should support this.</p>
<p>Pillow currently only supports rereducers written in Erlang. It would really be nice to support JavaScript for rereducers. A summing rereducer exists and mappers without reducers works just like in CouchDB. However when you have more complex reduction needs, copying the reducer code from your CouchDB into Pillow beats writing (and maintaining) them again in a new language.</p>
<p>Pillow should really support the bulk document API of CouchDB. I haven&#8217;t used this one myself, but adding support should be pretty straightforward.</p>
<p><a href="http://wiki.github.com/couchapp/couchapp/">CouchApp</a> support is harder since it requires JavaScript support and then some. I probably need to play around with a CouchApp or two to find out more, but since I haven&#8217;t done so, it&#8217;s hard to determine how much work it would take.</p>
<p>While I do hope that there are no non-replicated CouchDB servers in production out there, reality is that there probably are lots. I like the three-way replication minimum myself and with CouchDB&#8217;s master-master scheme, it works really well. Pillow however is currently happily ignorant of any replication you have set up. I would really like to have Pillow manage such replication. In addition to managing replication, sets of Pillow servers should be controllable from a random server in the same master-master way ensuring full control of your cluster from any single Pillow node.</p>
<p>There is no clear prioritized list right now, all features listed above (and probably more) would be beneficial. However, as I am currently the only one developing Pillow and the time I can spend on Pillow is limited, I have to prioritize. The five features can be grouped:</p>
<ul>
<li>CouchDB API compatibility: JavaScript views, bulk documents, CouchApp</li>
<li>Production flexibility and scaling: Consistent Hashing and Replication management</li>
</ul>
<p>It is not hard to admit that API compatibility is important, but the core of the API is supported. Production flexibility and scaling is more important for me at the moment and I will probably focus on that. I also think that replication management is slightly more useful than consistent hashing. Choosing between the API features is harder since I don&#8217;t need them myself, but JavaScript views is a prerequisite of CouchApp and bulk document support is straightforward in comparison to CouchApp leading to this priority list:</p>
<ol>
<li>Replication management</li>
<li>Consistent hashing</li>
<li>JavaScript views</li>
<li>Bulk documents</li>
<li>CouchApp</li>
</ol>
<p>This list is the result of my needs at the time of writing. Others may convince me to adjust the priorities. Better yet, others may jump in and add support for the features they need.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/knuthellan.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/knuthellan.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/knuthellan.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/knuthellan.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/knuthellan.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/knuthellan.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/knuthellan.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/knuthellan.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/knuthellan.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/knuthellan.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/knuthellan.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/knuthellan.wordpress.com/303/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/knuthellan.wordpress.com/303/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/knuthellan.wordpress.com/303/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=303&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://knuthellan.com/2010/07/14/feature-prioritization-for-pillow-the-couchdb-shard-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">knuthellan</media:title>
		</media:content>
	</item>
		<item>
		<title>A functional approach to Ruby</title>
		<link>http://knuthellan.com/2010/06/22/a-functional-approach-to-ruby/</link>
		<comments>http://knuthellan.com/2010/06/22/a-functional-approach-to-ruby/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 09:51:02 +0000</pubDate>
		<dc:creator>Knut O. Hellan</dc:creator>
				<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://knuthellan.com/?p=276</guid>
		<description><![CDATA[Several articles and blog posts have been written about functional Ruby. They tend to focus either on whether Ruby is a functional language or how to do functional programming in Ruby. I am not planning to do either. This post will look into the benefits of a functional approach to Ruby and the transition from [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=276&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Several articles and blog posts have been written about functional Ruby. They tend to focus either on whether Ruby is a functional language or how to do functional programming in Ruby. I am not planning to do either. This post will look into the benefits of a functional approach to Ruby and the transition from thinking classic object-oriented to functional.</p>
<p>I consider the discussion around Ruby being a functional language or not academic with no effect on my use of the language. There is no doubt that functional programming is possible in Ruby, but bear in mind that it does not enforce pure functions that do not have side-effects. As for good overviews of functional programming in Ruby, I suggest <a href="http://www.khelll.com/">Khaled alHabache’s</a> post <a href="http://www.khelll.com/blog/ruby/ruby-and-functional-programming/">Ruby and Functional Programming</a>. </p>
<p>When I originally started developing in Ruby, I was used to object-oriented programming. I tended to make classes for all kinds of data objects and the result looked like a nicer, more readable version of Java code. While this works, it is not the most effective way of developing in Ruby (or other dynamic programming languages). </p>
<p>One of the benefits of Ruby (and many other dynamic programming languages), is their lack of static typing. In classic object-oriented development, you would define member variables and methods to operate on the variables. You don&#8217;t have to do that in Ruby since you have a flexible hash class that can store most of what you need. Once you have replaced all member variables with a hash, the hash is your object and the methods of the old class are just functions that could operate on your hash.</p>
<p>There are situations where a hash doesn&#8217;t make sense. If you build an abstraction class, i.e. a storage system abstraction class, you might want to keep some information internally. Connection parameters for the storage system could be kept in a hash, but that doesn&#8217;t feel right. Interestingly, since there is normally only a single storage system of a particular type in use, you could make the storage abstraction class a singleton and keep all the connection parameters internally in traditional member variables.</p>
<p>Some information that you would stored in a database or on a different server might be used often enough to keep a memory cache. Let me stress that I don&#8217;t like caches and I try to avoid them whenever possible since they add complexity and the potential for inconsistent data among different servers. That being said, I do add caches when they are needed and once again, caches can also be implemented as a hash. Some code needs to control the caches, but since you normally cache data coming either from storage or a different server or service, you could put them in the abstraction class for that. If you need to write and use common code to manage the caches, you can easily build a mixin module.</p>
<p>Interestingly, the model you end up with if you only have hashes you send around and keep consistent data in singletons is similar to the Erlang gen_server behavior. This behavior is a general server template for Erlang, a pure functional language with immutable variables. The state variable is given as a parameter to all the general server functions. This allows the gen_server to maintain information in a pure functional setting.</p>
<p>When you get used to keeping abstractions in singletons and your data in hashes, you can also use modules instead of classes to modularize your code to keep related functionality together. If you also use blocks to define exact behavior inside function you end up with flexible code that is very easy to reuse.</p>
<p>Keeping your object data in a hash and implementing functions without side-effects make testing easy. What you get back from a function call is only a result of the function parameters and there is no need to test combinations of operations. In an object-oriented setting, member variables might not be observable and even if the returned value of a method call is correct the object might be in an undesired state that you cannot test without modifying the class to make internal variables accessible in your test system. Clearly the functional approach is cleaner and requires less test code.</p>
<p>The Sincerial system being a request handling system is built mostly functionally. There are singletons guarding the storage system and other cached data. The system also uses classic objects where that makes sense. Ruby is an object-oriented language and I believe in using the language features available when appropriate. This might sound like a contradiction to the whole post, but it&#8217;s not. My point is that you should avoid creating traditional classes when a hash can do the job and use functional programming techniques actively to improve maintainability, testability and readability of your code.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/knuthellan.wordpress.com/276/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/knuthellan.wordpress.com/276/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/knuthellan.wordpress.com/276/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/knuthellan.wordpress.com/276/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/knuthellan.wordpress.com/276/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/knuthellan.wordpress.com/276/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/knuthellan.wordpress.com/276/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/knuthellan.wordpress.com/276/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/knuthellan.wordpress.com/276/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/knuthellan.wordpress.com/276/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/knuthellan.wordpress.com/276/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/knuthellan.wordpress.com/276/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/knuthellan.wordpress.com/276/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/knuthellan.wordpress.com/276/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=276&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://knuthellan.com/2010/06/22/a-functional-approach-to-ruby/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">knuthellan</media:title>
		</media:content>
	</item>
		<item>
		<title>Pillow, the CouchDB shard manager</title>
		<link>http://knuthellan.com/2010/06/10/pillow-the-couchdb-shard-manager/</link>
		<comments>http://knuthellan.com/2010/06/10/pillow-the-couchdb-shard-manager/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 11:12:39 +0000</pubDate>
		<dc:creator>Knut O. Hellan</dc:creator>
				<category><![CDATA[couchdb]]></category>

		<guid isPermaLink="false">http://knuthellan.com/?p=268</guid>
		<description><![CDATA[If there is one thing that has bothered me about my choice of CouchDB as the main storage system for Sincerial, it&#8217;s the lack of an automatic system for shard management. In the early days of a startup, a single server is probably capable of handling all the necessary data. However, a successful service that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=268&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If there is one thing that has bothered me about my choice of <a href="http://couchdb.apache.org/">CouchDB</a> as the main storage system for <a href="http://www.sincerial.com/">Sincerial</a>, it&#8217;s the lack of an automatic system for shard management. In the early days of a startup, a single server is probably capable of handling all the necessary data. However, a successful service that is built around the harvesting and analysis of data will sooner or later have to shard the dataset across multiple servers. And for a new service, the sooner you have to start sharding, the better. Several good distributed storage systems exist. <a href="http://labs.google.com/papers/bigtable.html">Google&#8217;s original bigtable</a>, <a href="http://hbase.apache.org/">HBase (Hadoop&#8217;s bigtable equivalent)</a>, <a href="http://cassandra.apache.org/">Cassandra</a> and more solve this particular problem, but there is more to choosing and running a storage system than just data volume scaling. CouchDB has other strengths that made it a good choice for our application, but that is not the topic of this post.</p>
<p><a href="http://tilgovi.github.com/couchdb-lounge/">CouchDB-lounge</a> originally written by Kevin Ferguson (macfergus), Vijay Ragunathan (lukatmyshu) and Shaun Lindsay (srlindsay) at Meebo.com will handle distribution of requests to the right servers in a cluster of servers, but you still have to handle resharding or repartitioning manually. CouchDB-lounge consists of two components, dumbproxy handling reading and writing of documents and smartproxy handling views. These require Nginx and Twisted (a Python framework) respectively. If you overshard appropriately, you can scale your data volume a long way before you have to start resharding.</p>
<p>While manually repartitioning a CouchDB database is doable, I&#8217;d rather have an automatic way of doing it since I don&#8217;t want to make mistakes. In addition, the Sincerial system uses Ruby running with<a href="http://www.modrails.com/"> Phusion Passenger</a> in <a href="http://httpd.apache.org/">Apache</a> and I didn&#8217;t want to add two more frameworks on top of that. This might sound like a not-invented here excuse, but it isn&#8217;t or at least I don&#8217;t think it is.</p>
<p>When I started developing <a href="http://github.com/khellan/Pillow">Pillow</a>, I chose to do so in erlang to match couchdb. The reason was two-fold. First of all I was curious about erlang and I like functional programming. Secondly, CouchDB was written in erlang and there had to be a reason for that. Now I&#8217;ve released version 0.3 of Pillow. This version supports automatic resharding, routing of requests to the right shard and views. Reducers need to be written in erlang, but a summing reducer is in place and mappers without reducers are supported out of the box. As such, this version of Pillow has all the functionality I set out to develop, but it does not support the full CouchDB API.</p>
<p>The bulk document API is not supported. And I haven&#8217;t tried running standalone CouchApps. The reason being that I am focusing on our needs. I intend to have full support of the CouchDB API eventually, but it might be that I integrate more tightly with CouchDB and use CouchDB as a library to make this happen. This will make supporting javascript reducers easier as well. It would of course be interesting to have Pillow become an integral part of CouchDB as well providing one can still access each CouchDB server directly for maintenance purposes. The latter being one of the reasons I&#8217;m in general a bit sceptical to distributed systems that hide the inner workings since often hard to fix problems that may occur.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/knuthellan.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/knuthellan.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/knuthellan.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/knuthellan.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/knuthellan.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/knuthellan.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/knuthellan.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/knuthellan.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/knuthellan.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/knuthellan.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/knuthellan.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/knuthellan.wordpress.com/268/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/knuthellan.wordpress.com/268/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/knuthellan.wordpress.com/268/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=268&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://knuthellan.com/2010/06/10/pillow-the-couchdb-shard-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">knuthellan</media:title>
		</media:content>
	</item>
		<item>
		<title>CouchDB Replication Monitor</title>
		<link>http://knuthellan.com/2009/11/24/couchdb-replication-monitor/</link>
		<comments>http://knuthellan.com/2009/11/24/couchdb-replication-monitor/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 11:52:44 +0000</pubDate>
		<dc:creator>Knut O. Hellan</dc:creator>
				<category><![CDATA[couchdb]]></category>

		<guid isPermaLink="false">http://knuthellan.com/?p=247</guid>
		<description><![CDATA[CouchDB does replication, but replication needs to be set up after each server restart. This means you need to ensure that replication is restarted whenever the daemon restarts CouchDB. I have never seen replication stop working without a restart, but I prefer being safe to being sorry about replication. To be perfectly honest, I do [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=247&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>CouchDB does replication, but replication needs to be set up after each server restart. This means you need to ensure that replication is restarted whenever the daemon restarts CouchDB. I have never seen replication stop working without a restart, but I prefer being safe to being sorry about replication. To be perfectly honest, I do not trust that my replication initiation after a soft CouchDB restart works properly either so I prefer to monitor the replication and have a safety mechanism in place to restart replication if needed.</p>
<p>There are several ways to monitor replication. You could fetch the status page of all servers and restart replication on servers with an empty page, but that is a kind of brute force approach in my world. A better solution is to use the replication itself to monitor that it works. </p>
<p>Each server updates their timestamp in CouchDB and this is again replicated to the other servers. This gets us a bit of the way, but not all the way. The server you are checking might have received updates from all the other servers, but you don&#8217;t know if it&#8217;s pushed out anything to the other servers. To solve this, you can add information about the other servers to the local server as well. This will give you a matrix of server replication status.</p>
<p>For each server, you will see the timestamp replicated from the server and a list of timestamps replicated to that server. The latter often being a generation older than the former. Cron can be used to update this data. The cronjob reads all the server timestamps and updates this servers timestamp followed by a list of the other servers timestamp.</p>
<p>A mapper to get a server id to server status out of the db.<br />
<code></p>
<pre>
map: function(doc) {
  emit(doc._id, doc);
}
</pre>
<p></code></p>
<p>Our monitroing database is called server_status. The design containing the mapper is called collections and the view server_list.</p>
<p>A Ruby database checker that can run on cron.<br />
<code></p>
<pre>
require 'rubygems'
require 'couchrest'
require 'json'
require 'open-uri'

STATUS_DB = 'http://localhost:5984/server_status'
COLLECTIONS = 'collections'
SERVER_LIST = 'server_list'

hostname = ARGV[0]

status_db = CouchRest.database!(STATUS_DB)
status_view = "#{STATUS_DB}/_design/#{COLLECTIONS}/_view/#{SERVER_LIST}"

# Get the current information about this server if available
server_status = begin
  status_db.get(hostname)
rescue RestClient::ResourceNotFound
  {'_id' =&gt; hostname}
end

server_status['time'] = Time.new.to_i
# Get the current times of the other servers and update this server's
# view of them
JSON(open(status_view).read)['rows'].map do |row|
  {'server' =&gt; row['id'], 'status' =&gt; row['value']}
end.each do |status|
  unless status['server'] == hostname
    server_status['servers'][status['server']] = status['status']['time'] 
  end
end
status_db.save_doc(server_status)
</pre>
<p></code></p>
<p>Now you need to determine when to trigger replication restart. This can be handled in the watchdog cronjob. If the highest timestamp seen for this server at other servers is above a threshold, restart replication.</p>
<p>The final loop triggering when the age is above a threshold. The init_replication method just posts a continuous replication trigger to the db:<br />
<code></p>
<pre>
JSON(open(status_view).read)['rows'].map do |row|
  {'server' =&gt; row['id'], 'status' =&gt; row['value']}
end.each do |status|
  if server_status['time'] - status['status']['time'] &gt; THRESHOLD 
    init_replication(status['server']) 
  end
  unless status['server'] == hostname
    server_status['servers'][status['server']] = status['status']['time'] 
  end
end
</pre>
<p></code></p>
<p>Rudimentary init_replication method.<br />
<code></p>
<pre>
def init_replication(server)
  target = "http://#{server}:5984"
  databases = ['server_status']
  databases.each do |db|
    config = {
            'source' =&gt; "#{db}",
            'target' =&gt; "#{target}/#{db}",
            'continuous' =&gt; true
    }
    payload = JSON.generate(config)
    result = Net::HTTP.new('127.0.0.1', '5984').post(
      '/_replicate', payload, {'content-type' =&gt; 'text/x-json'})
    unless result.code == 200
      p "replication to #{target}/#{db} failed with #{result.code}" 
    end
  end
end
</pre>
<p></code></p>
<p>We have a monitoring view of replication ages in our system. It shows the matrix of timestamps as age in seconds rather than the actual timestamp since the age is the important metric.<br />
<a href="http://knuthellan.files.wordpress.com/2009/11/server_status.jpg"><img src="http://knuthellan.files.wordpress.com/2009/11/server_status.jpg?w=300&h=104" alt="Server Status" title="Server Status" width="300" height="104" class="alignnone size-medium wp-image-250" /></a></p>
<p>A bonus of this replication monitoring system is that we can access the status page from a mobil phone and get an accurate picture of the replication status. This doesn&#8217;t worry me now, but it did when we first set it up. Now it&#8217;s just a part of our general monitoring view.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/knuthellan.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/knuthellan.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/knuthellan.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/knuthellan.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/knuthellan.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/knuthellan.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/knuthellan.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/knuthellan.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/knuthellan.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/knuthellan.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/knuthellan.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/knuthellan.wordpress.com/247/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/knuthellan.wordpress.com/247/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/knuthellan.wordpress.com/247/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=247&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://knuthellan.com/2009/11/24/couchdb-replication-monitor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">knuthellan</media:title>
		</media:content>

		<media:content url="http://knuthellan.files.wordpress.com/2009/11/server_status.jpg?w=300" medium="image">
			<media:title type="html">Server Status</media:title>
		</media:content>
	</item>
		<item>
		<title>Sincerial Launched</title>
		<link>http://knuthellan.com/2009/11/02/sincerial-launched/</link>
		<comments>http://knuthellan.com/2009/11/02/sincerial-launched/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 14:25:56 +0000</pubDate>
		<dc:creator>Knut O. Hellan</dc:creator>
				<category><![CDATA[couchdb]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[startup]]></category>

		<guid isPermaLink="false">http://knuthellan.com/?p=235</guid>
		<description><![CDATA[Since we launched Sincerial with one connected online store, fundies.no on Thursday, I feel it&#8217;s time to go through the system and the choices we made on our way to launch. Hosting The obvious and easy choice was Amazon Web Services (AWS) Elastic Compute Cloud (EC2). A hosting service such as EC2 allows a lot [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=235&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Since we launched <a href="http://www.sincerial.com/">Sincerial</a> with one connected online store, <a href="http://www.fundies.no/">fundies.no</a> on Thursday, I feel it&#8217;s time to go through the system and the choices we made on our way to launch.</p>
<p><strong>Hosting</strong><br />
The obvious and easy choice was <em>Amazon Web Services (AWS) Elastic Compute Cloud (EC2)</em>. A hosting service such as <em>EC2</em> allows a lot of flexibility in server solutions including quick ramp up if the luxury problem of needing more servers occurs. Freedom to choose operating system and providing a virtual server resembling what you get from a traditional hosting services were also important for us. There were alternatives, traditional hosting services where you book physical servers would incur a larger fixed cost than we wanted and at the same time, we would have lost the flexibility. We currently fire up servers and test things in no time just to shut those servers down after the test. With a traditional hosting provider, we would need spare servers for testing or used the live servers.<em> Windows Azure</em> could have been an alternative, but it&#8217;s just a <em>.NET Windows</em> hosting environment and not flexible enough. I feel I should mention Google App Engine as well, but we didn&#8217;t really consider it since it&#8217;s too restricted and even more locked in than <em>Windows Azure</em>.</p>
<p><strong>Operating System</strong><br />
We decided to use <em>CentOS</em> for the servers. The reason for this was favorable experience with <em>Fedora</em> over the last year and <em>Ubuntu</em> getting more painful at the same time.<em> Red Hat Enterprise Linux (RHEL)</em> was a contender, but <em>CentOS</em> provides us with the part of <em>RHEL</em> that we need without all the parts that we would pay for, but not use. <em>Debian</em> would have been our choice a few years back, but the <em>Fedora</em> and <em>Ubuntu</em> experiences over the last year brought us down on the <em>Fedora</em>, <em>RHEL</em>, <em>CentOS</em> side and as mentioned, <em>CentOS</em> was the best fit among those three. <em>Windows</em> wasn&#8217;t considered. We had absolutely no need for anything <em>Windows</em> in there and remote management and configuration of <em>Linux</em> systems is so much easier while being extremely stable. The <em>Amazon Machine Image (AMI)</em> we use was created by <a href="http://www.rightscale.com/">RightScale</a>.</p>
<p><strong>Web Serving Environment</strong><br />
We chose <em>Apache</em> because that&#8217;s what we&#8217;ve used in the past and <em>Apache</em> is solid and dependable. The downside of <em>Apache</em> is that it&#8217;s a big beast that might be overkill for our need. An alternative that we looked a bit at is <em>nginx</em>, but we didn&#8217;t want to spend time on that before launch. We will however look more closely at <em>nginx</em> in the future.</p>
<p><strong>Progamming Language</strong><br />
I am a rubyist and we chose <em>Ruby</em>. The first time I tried <em>Ruby</em>, I wrote a few scripts that I would normally have written in <em>Perl</em>. These weren&#8217;t big scripts, but far from one-liners. They downloaded some content and analyzed it. Writing the scripts in <em>Ruby</em> took me a bit longer than it would have taken writing them in <em>Perl</em>, but they worked right away. In <em>Perl</em> there is always something wrong unless you have a one-liner. The strongest alternative was <em>Python</em> and between <em>Ruby</em> and <em>Python</em> it comes down to taste. The first readability I got as a Googler was in <em>Python</em> and I did write a lot of <em>Python</em>, but it still doesn&#8217;t feel right. I was very happy when I got a chance to sneak myself to a <em>Ruby</em> readability. <em>Java</em> was sort of not considered, but would have been the choice if the lightweight short time-to-market alternatives hadn&#8217;t been available. We use<em> Phusion Passenger</em> to serve a combination of pure <em>Ruby</em> racks and <em>Sinatra</em> apps in <em>Apache</em>. </p>
<p><strong>Storage</strong><br />
<em>CouchDB</em> won this one. Key value storage is what we need so relational databases are a complication that we can happily forget about. Selling points of <em>CouchDB</em> was that it was easy to get started, it has an <em>HTTP RESTful API</em> and views are written as <em>Javascript</em> map-reduce. Map reduces is well suited to our calculation needs so we get a lot more done inside the database than we would do with i.e. <em>SQL</em>. The strongest alternative was definitely <em>MongoDB</em> which is very similar to <em>CouchDB</em>, but uses a more classical query language. We also briefly looked at <em>Voldemort</em>, but our calculation need is not suited to <em>Voldemort</em>&#8216;s simple key lookup scheme. <em>SimpleDB</em> ties us to <em>Amazon</em> and <em>Hadoop</em> is a bit too heavy for our needs. We didn&#8217;t consider <em>MySQL</em>, but that would have been our choice had we needed a relational database.</p>
<p><strong>Conclusion</strong><br />
Our <em>LARC</em> stack (<em>Linux</em> <em>Apache</em>, <em>Ruby</em>, <em>CouchDB</em>) works very well for us and that combination has enabled us to develop our service very quickly with the result being an easy system to maintain. We haven&#8217;t really looked back at those choices except for a few moments after upgrading to <em>CouchDB</em> 0.10.0 and seeing that our map reduces that put a lot of work in the reducers had to be rewritten. One look at <em>MongoDB</em>&#8216;s query language stopped those regrets. As mentioned when discussing web serving environments, we do consider switching from <em>Apache</em> to <em>nginx</em>, but it&#8217;s not a high priority thing and we are happy with <em>Apache</em> and the consideration comes more from curiosity than need. As for <em>CentOS</em> and <em>EC2</em>, they just work and gets out of the way which is exactly what they should do.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/knuthellan.wordpress.com/235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/knuthellan.wordpress.com/235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/knuthellan.wordpress.com/235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/knuthellan.wordpress.com/235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/knuthellan.wordpress.com/235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/knuthellan.wordpress.com/235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/knuthellan.wordpress.com/235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/knuthellan.wordpress.com/235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/knuthellan.wordpress.com/235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/knuthellan.wordpress.com/235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/knuthellan.wordpress.com/235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/knuthellan.wordpress.com/235/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/knuthellan.wordpress.com/235/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/knuthellan.wordpress.com/235/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=knuthellan.com&#038;blog=6371883&#038;post=235&#038;subd=knuthellan&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://knuthellan.com/2009/11/02/sincerial-launched/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">knuthellan</media:title>
		</media:content>
	</item>
	</channel>
</rss>
