<?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"
	>

<channel>
	<title>Code by Martin</title>
	<atom:link href="http://martin.elwin.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://martin.elwin.com/blog</link>
	<description>Words about stuff...</description>
	<pubDate>Sun, 21 Sep 2008 22:11:57 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>Caching Filter Queries with Coherence</title>
		<link>http://martin.elwin.com/blog/2008/09/caching-filter-queries-with-coherence/</link>
		<comments>http://martin.elwin.com/blog/2008/09/caching-filter-queries-with-coherence/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 20:56:59 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[coherence]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://martin.elwin.com/blog/?p=28</guid>
		<description><![CDATA[A pretty nice thing that I recently tried with a customer was storing a query result in a query cache.
For instance, consider the following method:

public Collection getByFilter1&#40;String cacheName, Filter f&#41; &#123;
    NamedCache c = CacheFactory.getCache&#40;cacheName&#41;;
    return c.entrySet&#40;f&#41;;
&#125;

A query is executed across all nodes containing cache data in a cluster. [...]]]></description>
			<content:encoded><![CDATA[<p>A pretty nice thing that I recently tried with a customer was storing a query result in a query cache.</p>
<p>For instance, consider the following method:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Collection</span> getByFilter1<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> cacheName, Filter f<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    NamedCache c <span style="color: #339933;">=</span> CacheFactory.<span style="color: #006633;">getCache</span><span style="color: #009900;">&#40;</span>cacheName<span style="color: #009900;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">return</span> c.<span style="color: #006633;">entrySet</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A query is executed across all nodes containing cache data in a cluster. The filter acts on the <em>values</em> in the cache, not the keys. And as all values are usually not available locally on every node the query needs to execute on the separate nodes.</p>
<p>The query above is correct, but not very efficient. In the above case we query and retrieve the data as well from the separate nodes. A more efficient way would be to only get the keys for the matching values from the nodes and then retrieve the values from the local near cache:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Collection</span> getByFilter2<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> cacheName, Filter f<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    NamedCache c <span style="color: #339933;">=</span> CacheFactory.<span style="color: #006633;">getCache</span><span style="color: #009900;">&#40;</span>cacheName<span style="color: #009900;">&#41;</span>;
    <span style="color: #003399;">Set</span> keys <span style="color: #339933;">=</span> c.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span>;
    <span style="color: #000000; font-weight: bold;">return</span> c.<span style="color: #006633;">getAll</span><span style="color: #009900;">&#40;</span>keys<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">values</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>However, in this case we still perform the query every time.</p>
<p>Now - the clever part. Not so much on my side, but the Coherence engineers have thought things through and made the Filter implementations have good hashCode and equals implementations. This together with the fact that they are serializable makes them possible to use as keys in a cache! Sweet! Without changing our method&#8217;s interface we can add a query cache so that each query only is performed once.</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Collection</span> getByFilter3<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> cacheName, Filter f<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    NamedCache c <span style="color: #339933;">=</span> CacheFactory.<span style="color: #006633;">getCache</span><span style="color: #009900;">&#40;</span>cacheName<span style="color: #009900;">&#41;</span>;
&nbsp;
    NamedCache queryC <span style="color: #339933;">=</span> CacheFactory.<span style="color: #006633;">getCache</span><span style="color: #009900;">&#40;</span>cacheName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.querycache&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #003399;">Set</span> keys <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Set</span><span style="color: #009900;">&#41;</span>queryC.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>keys <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        keys <span style="color: #339933;">=</span> c.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span>;
        queryC.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>f, keys<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">return</span> c.<span style="color: #006633;">getAll</span><span style="color: #009900;">&#40;</span>keys<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">values</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that we only save the keys in the query cache. This to avoid having several caches with the same data. When near caching is used, getting the data for the keys can still be a local only operation. Compared to getting the data from the separate nodes it&#8217;s several orders of magnitude faster - depending on the usage patterns.</p>
<p>Of course if queries are different every time, the query cache will not help much. But in most high load applications the same data tends to be needed several times.</p>
<p>Additionally, the properties queried on should in most cases be indexed. This is important to avoid too much overhead when searching for an entry in a cache.</p>
<p>One thing to think about when adding query caches is: how is data updated?</p>
<p>As part of updating the value caches the query caches should preferably be emptied of the outdated cached queries. This could be done programmatically &#8220;manually&#8221; when the data is updated, or by hooking in code to clean the entries from the query cache using the map listener mechanism. The relevant code could do a query using the ContainsFilter.</p>
<p>To summarize: a little code can go a long way to improve performance without affecting the interface used by an application. Good when query heavy applications are adapted to use a distributed cache like Coherence.</p>
<p>/M</p>
]]></content:encoded>
			<wfw:commentRss>http://martin.elwin.com/blog/2008/09/caching-filter-queries-with-coherence/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Scala Syntax Highlighting for WP</title>
		<link>http://martin.elwin.com/blog/2008/06/scala-syntax-highlighting-for-wp/</link>
		<comments>http://martin.elwin.com/blog/2008/06/scala-syntax-highlighting-for-wp/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 07:56:52 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[geshi]]></category>

		<category><![CDATA[scala]]></category>

		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://martin.elwin.com/blog/?p=27</guid>
		<description><![CDATA[Writing the previous post I realized that the Wordpress plugin for syntax highligting I was using (Highlight Source Pro) didn&#8217;t support Scala.
Instead I tried the WP-Syntax plugin, but this didn&#8217;t support Symbol literals:

val sym = 'foobar
println(&#34;Symbol is: &#34; + sym)
val other = 'barfoo

This seems to be because of single quote being interpreted as a quotation [...]]]></description>
			<content:encoded><![CDATA[<p>Writing the previous post I realized that the Wordpress plugin for syntax highligting I was using (Highlight Source Pro) didn&#8217;t support Scala.</p>
<p>Instead I tried the WP-Syntax plugin, but this didn&#8217;t support Symbol literals:</p>

<div class="wp_syntax"><div class="code"><pre class="scala-old scala-old" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> sym <span style="color: #000080;">=</span> <span style="color: #6666FF;">'foobar
println(&quot;Symbol is: &quot; + sym)
val other = '</span>barfoo</pre></div></div>

<p>This seems to be because of single quote being interpreted as a quotation character. A fix seems to be removing the single quote from the Geshi language definition file <code>scala.php</code> in the <code>wp-syntax/geshi/geshi</code> directory. While there one can also specify a regex for symbols and give them their own color:</p>

<div class="wp_syntax"><div class="code"><pre class="scala scala" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">val</span> sym <span style="color: #FFAA00;">=</span> <span style="color: #008000;">'foobar</span>
println<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Symbol is: &quot;</span> + sym<span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">val</span> other <span style="color: #FFAA00;">=</span> <span style="color: #008000;">'barfoo</span></pre></div></div>

<p>Also check out the <a href="https://lampsvn.epfl.ch/trac/scala/browser/scala-tool-support/trunk/src/geshi/scala.php"><code>scala.php</code> in the LAMP repository</a> - it seems to add some additional keywords and other colors. I used this and made the changes described above.</p>
<p>Full new <code>scala.php</code> for Geshi after the break.</p>
<p><span id="more-27"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*************************************************************************************
 * scala.php
 * --------
 * Author: Geoffrey Washburn (washburn@acm.ogr)
 * Copyright: (c) 2004 Nigel McNie (http://qbnz.com/highlighter/)
 * Release Version: ???
 * Date Started: 2008/01/03
 *
 * Scala language file for GeSHi.
 *
 * CHANGES
 * -------
 * 2007/01/03
 *   -  Created by copying the Java highlighter
 *
 * TODO
 * -------------------------
 * * Finish
 *
 *************************************************************************************
 *
 *     This file is part of GeSHi.
 *
 *   GeSHi is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   GeSHi is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with GeSHi; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 ************************************************************************************/</span>
&nbsp;
<span style="color: #000088;">$language_data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span> <span style="color: #009900;">&#40;</span>
        <span style="">'LANG_NAME'</span> <span style="color: #339933;">=&gt;</span> <span style="">'Scala'</span><span style="color: #339933;">,</span>
        <span style="">'COMMENT_SINGLE'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="">'//'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>   <span style="color: #666666; font-style: italic;">/* import statements are not comments! */</span>
        <span style="">'COMMENT_MULTI'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'/*'</span> <span style="color: #339933;">=&gt;</span> <span style="">'*/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="">'CASE_KEYWORDS'</span> <span style="color: #339933;">=&gt;</span> GESHI_CAPS_NO_CHANGE<span style="color: #339933;">,</span>
        <span style="">'QUOTEMARKS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="">'&quot;'</span><span style="color: #339933;">,</span> <span style="">'&quot;&quot;&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="">'ESCAPE_CHAR'</span> <span style="color: #339933;">=&gt;</span> <span style="">'<span style="">\\</span>'</span><span style="color: #339933;">,</span>
        <span style="">'KEYWORDS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #666666; font-style: italic;">/* Scala keywords, part 1: control flow */</span>
                        <span style="">'case'</span><span style="color: #339933;">,</span> <span style="">'default'</span><span style="color: #339933;">,</span> <span style="">'do'</span><span style="color: #339933;">,</span> <span style="">'else'</span><span style="color: #339933;">,</span> <span style="">'for'</span><span style="color: #339933;">,</span>
                        <span style="">'if'</span><span style="color: #339933;">,</span> <span style="">'match'</span><span style="color: #339933;">,</span> <span style="">'while'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #666666; font-style: italic;">/* Scala keywords, part 2 */</span>
                        <span style="">'return'</span><span style="color: #339933;">,</span> <span style="">'throw'</span><span style="color: #339933;">,</span>
                        <span style="">'try'</span><span style="color: #339933;">,</span> <span style="">'catch'</span><span style="color: #339933;">,</span> <span style="">'finally'</span><span style="color: #339933;">,</span>
                        <span style="">'abstract'</span><span style="color: #339933;">,</span> <span style="">'class'</span><span style="color: #339933;">,</span> <span style="">'def'</span><span style="color: #339933;">,</span> <span style="">'extends'</span><span style="color: #339933;">,</span>
                        <span style="">'final'</span><span style="color: #339933;">,</span> <span style="">'forSome'</span><span style="color: #339933;">,</span> <span style="">'implicit'</span><span style="color: #339933;">,</span> <span style="">'import'</span><span style="color: #339933;">,</span>
                        <span style="">'lazy'</span><span style="color: #339933;">,</span> <span style="">'new'</span><span style="color: #339933;">,</span> <span style="">'object'</span><span style="color: #339933;">,</span> <span style="">'override'</span><span style="color: #339933;">,</span> <span style="">'package'</span><span style="color: #339933;">,</span>
                        <span style="">'private'</span><span style="color: #339933;">,</span> <span style="">'protected'</span><span style="color: #339933;">,</span>
                        <span style="">'requires'</span><span style="color: #339933;">,</span> <span style="">'sealed'</span><span style="color: #339933;">,</span> <span style="">'super'</span><span style="color: #339933;">,</span> <span style="">'this'</span><span style="color: #339933;">,</span> <span style="">'trait'</span><span style="color: #339933;">,</span> <span style="">'type'</span><span style="color: #339933;">,</span>
                        <span style="">'val'</span><span style="color: #339933;">,</span> <span style="">'var'</span><span style="color: #339933;">,</span> <span style="">'with'</span><span style="color: #339933;">,</span> <span style="">'yield'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #cc66cc;">3</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #666666; font-style: italic;">/* Scala keywords, part 3: standard value types */</span>
                        <span style="">'unit'</span><span style="color: #339933;">,</span> <span style="">'Unit'</span><span style="color: #339933;">,</span> <span style="">'boolean'</span><span style="color: #339933;">,</span> <span style="">'Boolean'</span><span style="color: #339933;">,</span> <span style="">'int'</span><span style="color: #339933;">,</span> <span style="">'Int'</span><span style="color: #339933;">,</span> <span style="">'Any'</span><span style="color: #339933;">,</span> <span style="">'AnyVal'</span><span style="color: #339933;">,</span> <span style="">'Nothing'</span><span style="color: #339933;">,</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #cc66cc;">4</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #666666; font-style: italic;">/* other reserved words in Scala: literals */</span>
                        <span style="color: #666666; font-style: italic;">/* should be styled to look similar to numbers and Strings */</span>
                        <span style="">'false'</span><span style="color: #339933;">,</span> <span style="">'null'</span><span style="color: #339933;">,</span> <span style="">'true'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #cc66cc;">5</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #666666; font-style: italic;">/* Scala reference types */</span>
                        <span style="">'AnyRef'</span><span style="color: #339933;">,</span> <span style="">'Null'</span><span style="color: #339933;">,</span> <span style="">'List'</span><span style="color: #339933;">,</span> <span style="">'String'</span><span style="color: #339933;">,</span> <span style="">'Integer'</span><span style="color: #339933;">,</span> <span style="">'Option'</span><span style="color: #339933;">,</span> <span style="">'Array'</span>
                        <span style="color: #009900;">&#41;</span>
&nbsp;
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="">'SYMBOLS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="">':'</span><span style="color: #339933;">,</span> <span style="">'*'</span><span style="color: #339933;">,</span> <span style="">'&amp;'</span><span style="color: #339933;">,</span> <span style="">'%'</span><span style="color: #339933;">,</span> <span style="">'!'</span><span style="color: #339933;">,</span> <span style="">';'</span><span style="color: #339933;">,</span> <span style="">'&lt;'</span><span style="color: #339933;">,</span> <span style="">'&gt;'</span><span style="color: #339933;">,</span> <span style="">'?'</span><span style="color: #339933;">,</span> <span style="">'_'</span><span style="color: #339933;">,</span> <span style="">'='</span><span style="color: #339933;">,</span> <span style="">'=&gt;'</span><span style="color: #339933;">,</span>
                <span style="">'&lt;-'</span><span style="color: #339933;">,</span> <span style="">'&lt;:'</span><span style="color: #339933;">,</span> <span style="">'&lt;%'</span><span style="color: #339933;">,</span> <span style="">'&gt;:'</span><span style="color: #339933;">,</span> <span style="">'#'</span><span style="color: #339933;">,</span> <span style="">'@'</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="">'CASE_SENSITIVE'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                GESHI_COMMENTS <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                <span style="color: #666666; font-style: italic;">/* all Scala keywords are case sensitive */</span>
                <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="">'STYLES'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="">'KEYWORDS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #b1b100;'</span><span style="color: #339933;">,</span>
                        <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #000000; font-weight: bold;'</span><span style="color: #339933;">,</span>
                        <span style="color: #cc66cc;">3</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #993333;'</span><span style="color: #339933;">,</span>
                        <span style="color: #cc66cc;">4</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #b13366;'</span><span style="color: #339933;">,</span>
                        <span style="color: #cc66cc;">5</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #aaaadd;'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="">'SYMBOLS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color:#800080;">0</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #FFAA00;'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="">'COMMENTS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #808080; font-style: italic;'</span><span style="color: #339933;">,</span>
                        <span style="">'MULTI'</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #808080; font-style: italic;'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="">'ESCAPE_CHAR'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color:#800080;">0</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #000099; font-weight: bold;'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="">'BRACKETS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color:#800080;">0</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #66cc66;'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="">'STRINGS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color:#800080;">0</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #ff0000;'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="">'NUMBERS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color:#800080;">0</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #cc66cc;'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="">'METHODS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #006600;'</span><span style="color: #339933;">,</span>
                        <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #006600;'</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="">'SCRIPT'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="">'REGEXPS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                        <span style="color:#800080;">0</span> <span style="color: #339933;">=&gt;</span> <span style="">'color: #008000;'</span>
                        <span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="">'URLS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="">''</span><span style="color: #339933;">,</span>
                <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="">''</span><span style="color: #339933;">,</span>
                <span style="color: #cc66cc;">3</span> <span style="color: #339933;">=&gt;</span> <span style="">''</span><span style="color: #339933;">,</span>
                <span style="color: #cc66cc;">4</span> <span style="color: #339933;">=&gt;</span> <span style="">''</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="">'OOLANG'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">,</span>
        <span style="">'OBJECT_SPLITTERS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="">'.'</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="">'REGEXPS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
               <span style="color:#800080;">0</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;'[a-zA-Z_][a-zA-Z0-9_]*&quot;</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="">'STRICT_MODE_APPLIES'</span> <span style="color: #339933;">=&gt;</span> GESHI_NEVER<span style="color: #339933;">,</span>
        <span style="">'SCRIPT_DELIMITERS'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="">'HIGHLIGHT_STRICT_BLOCK'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://martin.elwin.com/blog/2008/06/scala-syntax-highlighting-for-wp/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Clustering Scala Actors with Oracle Coherence for Fun and Profit</title>
		<link>http://martin.elwin.com/blog/2008/06/clustering-scala-actors-with-oracle-coherence/</link>
		<comments>http://martin.elwin.com/blog/2008/06/clustering-scala-actors-with-oracle-coherence/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 07:38:13 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[actors]]></category>

		<category><![CDATA[coherence]]></category>

		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://martin.elwin.com/blog/?p=26</guid>
		<description><![CDATA[[Disclaimer: I work for Oracle.]
Although I haven&#8217;t used it too much yet, Scala is definitely one of the languages I find most interesting right now. Many customers I work with are heavily Java focused, and getting a more flexible and powerful language with superb Java interoperability to use on the JVM feels very liberating. Now [...]]]></description>
			<content:encoded><![CDATA[<p>[Disclaimer: I work for Oracle.]</p>
<p>Although I haven&#8217;t used it too much yet, Scala is definitely one of the languages I find most interesting right now. Many customers I work with are heavily Java focused, and getting a more flexible and powerful language with superb Java interoperability to use on the JVM feels very liberating. Now I just need to convince the customers that Scala is the future&#8230; :] But if <a href="http://www.adam-bien.com/roller/abien/entry/java_net_javaone_which_programming">Gosling likes it</a> it must be good, right?</p>
<p>A few months ago Jonas Bonér wrote about how <a href="http://jonasboner.com/2008/01/25/clustering-scala-actors-with-terracotta/">Scala actors can be clustered with Terracotta</a>. I really enjoyed the article and I think the idea of distributed, redundant actors is very appealing. The actor paradigm is a nice way of developing concurrent applications (see the intro in Jonas&#8217; blog entry for more info) and if we can liberate the actors from the confines of a single JVM and easily distribute them over multiple hosts - all the better.</p>
<h3>Clustering with Coherence</h3>
<p>I&#8217;m not going to compare Coherence and Terracotta here. In short, <a href="http://www.oracle.com/technology/products/coherence/index.html">Coherence</a> provides, among other things, a distributed caching and code execution mechanism without single points of failure. Coherence can be downloaded for evaluation purposes from the Oracle web site.</p>
<p>The idea I wanted to try was to have Scala actors that store their state in the distributed Coherence cache and run as normal Scala actors on a single node in the cluster at a time. The node the actor runs on should be allocated by Coherence and if the node fails, the actor should be automatically started on another node with maintained state and without any lost messages.</p>
<p>Also, I wanted this to work as similarly to normal Scala actors as possible with compatibility between the two.</p>
<h3>The Result</h3>
<p>Before investigating the proof of concept solution, let&#8217;s look at the result and what it gives us.</p>
<p>Here&#8217;s a simple test application with a normal Scala actor. It uses the recommended way of creating actors with <code>actor { ... }</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="scala scala" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> coherencetest1
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<span style="color: #006600;">Date</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> scala.<span style="color: #006600;">actors</span>.<span style="color: #006600;">Actor</span>.<span style="color: #FFAA00;">_</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">object</span> ActorTest <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">def</span> main<span style="color: #66cc66;">&#40;</span>args <span style="color: #FFAA00;">:</span> <span style="color: #aaaadd;">Array</span><span style="color: #66cc66;">&#91;</span><span style="color: #aaaadd;">String</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #FFAA00;">:</span> <span style="color: #993333;">Unit</span> <span style="color: #FFAA00;">=</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    actor <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">var</span> pings <span style="color: #FFAA00;">:</span> <span style="color: #993333;">Int</span> <span style="color: #FFAA00;">=</span> 0
&nbsp;
      println<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Actor started.&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      self <span style="color: #FFAA00;">!</span> <span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'ping</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      loop <span style="color: #66cc66;">&#123;</span>
        react <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'ping</span>, i <span style="color: #FFAA00;">:</span> <span style="color: #993333;">Int</span><span style="color: #66cc66;">&#41;</span> <span style="color: #FFAA00;">=&gt;</span>
          pings <span style="color: #FFAA00;">=</span> pings + <span style="color: #cc66cc;">1</span>
          println<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Date + <span style="color: #ff0000;">&quot; - Got ping: &quot;</span> + i + <span style="color: #ff0000;">&quot; Total pings: &quot;</span> + pings<span style="color: #66cc66;">&#41;</span>
&nbsp;
          Thread.<span style="color: #006600;">sleep</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
          self <span style="color: #FFAA00;">!</span> <span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'ping</span>, i+<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>When this code is run, a simple actor that sends a message to itself is created and started. It sleeps for 1 second to pace the execution and to simulate a task that takes time to perform (of course, normally you shouldn&#8217;t sleep in real actors as you tie up the thread).</p>
<p>When the code is run, the following is displayed:</p>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">Actor started.
Sun Jun 29 15:57:16 CEST 2008 - Got ping: 1 Total pings: 1
Sun Jun 29 15:57:17 CEST 2008 - Got ping: 2 Total pings: 2
Sun Jun 29 15:57:18 CEST 2008 - Got ping: 3 Total pings: 3
Sun Jun 29 15:57:19 CEST 2008 - Got ping: 4 Total pings: 4
Sun Jun 29 15:57:20 CEST 2008 - Got ping: 5 Total pings: 5
Sun Jun 29 15:57:21 CEST 2008 - Got ping: 6 Total pings: 6
...</pre></div></div>

<p>Nothing too fancy, but a decent test case for our actor distribution. An important aspect of this actor is that it defines a local variable <code>pings</code> and prints a message in the initialization part, before the <code>loop</code> and <code>react</code>. The value of the local var must be maintained and the initialization code must only be run once, and not when an actor is started on a new node after a failure.</p>
<p>Let&#8217;s make it a distributed Actor:</p>

<div class="wp_syntax"><div class="code"><pre class="scala scala" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> coherencetest1
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<span style="color: #006600;">Date</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> scala.<span style="color: #006600;">actors</span>.<span style="color: #006600;">coherence</span>.<span style="color: #006600;">CoActor</span>.<span style="color: #FFAA00;">_</span>
&nbsp;
<span style="color: #FFAA00;">@</span>serializable
<span style="color: #000000; font-weight: bold;">object</span> DactorTest <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">def</span> main<span style="color: #66cc66;">&#40;</span>args <span style="color: #FFAA00;">:</span> <span style="color: #aaaadd;">Array</span><span style="color: #66cc66;">&#91;</span><span style="color: #aaaadd;">String</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #FFAA00;">:</span> <span style="color: #993333;">Unit</span> <span style="color: #FFAA00;">=</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    dactor <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">var</span> pings <span style="color: #FFAA00;">:</span> <span style="color: #993333;">Int</span> <span style="color: #FFAA00;">=</span> 0
&nbsp;
      println<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Actor started.&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      self <span style="color: #FFAA00;">!</span> <span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'ping</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
      loop <span style="color: #66cc66;">&#123;</span>
        react <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'ping</span>, i <span style="color: #FFAA00;">:</span> <span style="color: #993333;">Int</span><span style="color: #66cc66;">&#41;</span> <span style="color: #FFAA00;">=&gt;</span>
          pings <span style="color: #FFAA00;">=</span> pings + <span style="color: #cc66cc;">1</span>
          println<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Date + <span style="color: #ff0000;">&quot; - Got ping: &quot;</span> + i + <span style="color: #ff0000;">&quot; Total pings: &quot;</span> + pings<span style="color: #66cc66;">&#41;</span>
&nbsp;
          Thread.<span style="color: #006600;">sleep</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
          self <span style="color: #FFAA00;">!</span> <span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'ping</span>, i+<span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>What have we done here? Three things:</p>
<ol>
<li>Import <code>scala.actors.coherence.CoActor._</code> instead of <code>scala.actors.Actor._</code></li>
<li>Made the <em>application object</em> serializable</li>
<li>Create the actor using <code>dactor { ... }</code> instead of <code>actor { ... }</code></li>
</ol>
<p>The first point is simple - we need access to the new functionality, so we import the new CoActor object instead of the standard Actor object.</p>
<p>For number two - this is slightly nasty. If I interpret things correctly; as the code block created as a parameter to <code>react</code> needs to be serializable (so that the actor can be distributed over the network), all enclosing types needs to be serializable. I struggled with this for a while and the only option seems to be creating a proper named serializable type&#8230; But since I want to be able to create an actor in-line, we need to do it this way.</p>
<p>For the last point - <code>dactor { ... }</code> is simply the function used to create a distributed actor instead of a normal actor.</p>
<p>Let&#8217;s run it:</p>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">2008-06-29 16:11:18.779 Oracle Coherence 3.3.1/389 &lt;Info&gt; (thread=main, member=n/a): Loaded operational configuration from resource &quot;jar:file:/opt/coherence-3.3.1/lib/coherence.jar!/tangosol-coherence.xml&quot;
2008-06-29 16:11:18.785 Oracle Coherence 3.3.1/389 &lt;Info&gt; (thread=main, member=n/a): Loaded operational overrides from resource &quot;jar:file:/opt/coherence-3.3.1/lib/coherence.jar!/tangosol-coherence-override-dev.xml&quot;
2008-06-29 16:11:18.786 Oracle Coherence 3.3.1/389 &lt;D5&gt; (thread=main, member=n/a): Optional configuration override &quot;/tangosol-coherence-override.xml&quot; is not specified
&nbsp;
Oracle Coherence Version 3.3.1/389
 Grid Edition: Development mode
Copyright (c) 2000-2007 Oracle. All rights reserved.
&nbsp;
2008-06-29 16:11:19.042 Oracle Coherence GE 3.3.1/389 &lt;Info&gt; (thread=main, member=n/a): Loaded cache configuration from resource &quot;file:/crypt/dev/scala/CoherenceTest1/config/scalacoherence.xml&quot;
2008-06-29 16:11:19.331 Oracle Coherence GE 3.3.1/389 &lt;Warning&gt; (thread=main, member=n/a): UnicastUdpSocket failed to set receive buffer size to 1428 packets (2096304 bytes); actual size is 714 packets (1048576 bytes). Consult your OS documentation regarding increasing the maximum socket buffer size. Proceeding with the actual value may cause sub-optimal performance.
2008-06-29 16:11:19.459 Oracle Coherence GE 3.3.1/389 &lt;D5&gt; (thread=Cluster, member=n/a): Service Cluster joined the cluster with senior service member n/a
2008-06-29 16:11:22.662 Oracle Coherence GE 3.3.1/389 &lt;Info&gt; (thread=Cluster, member=n/a): Created a new cluster with Member(Id=1, Timestamp=2008-06-29 16:11:19.343, Address=192.168.54.1:8088, MachineId=24065, Location=process:31397@dellicious, Edition=Grid Edition, Mode=Development, CpuCount=2, SocketCount=1) UID=0xC0A836010000011AD4A9DCAF5E011F98
2008-06-29 16:11:22.834 Oracle Coherence GE 3.3.1/389 &lt;D5&gt; (thread=DistributedCache, member=1): Service DistributedCache joined the cluster with senior service member 1
Actor started.
Sun Jun 29 16:11:23 CEST 2008 - Got ping: 1 Total pings: 1
Sun Jun 29 16:11:24 CEST 2008 - Got ping: 2 Total pings: 2
Sun Jun 29 16:11:25 CEST 2008 - Got ping: 3 Total pings: 3
Sun Jun 29 16:11:26 CEST 2008 - Got ping: 4 Total pings: 4
Sun Jun 29 16:11:27 CEST 2008 - Got ping: 5 Total pings: 5
Sun Jun 29 16:11:28 CEST 2008 - Got ping: 6 Total pings: 6
...</pre></div></div>

<p>After the Coherence initialization (which happens automatically and which I&#8217;ve disabled in the outputs below) the actor starts up as expected. However, if we start this on two nodes - there will be two actors created, and no way for a new JVM to get hold of a reference to a specific existing actor. To handle this, let&#8217;s specify a name for the actor that we create using the <code>dactor(name : Symbol) { ... }</code> function:</p>

<div class="wp_syntax"><div class="code"><pre class="scala scala" style="font-family:monospace;">...
<span style="color: #000000; font-weight: bold;">object</span> DactorTest <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">def</span> main<span style="color: #66cc66;">&#40;</span>args <span style="color: #FFAA00;">:</span> <span style="color: #aaaadd;">Array</span><span style="color: #66cc66;">&#91;</span><span style="color: #aaaadd;">String</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #FFAA00;">:</span> <span style="color: #993333;">Unit</span> <span style="color: #FFAA00;">=</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    dactor<span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'pingActor</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">var</span> pings <span style="color: #FFAA00;">:</span> <span style="color: #993333;">Int</span> <span style="color: #FFAA00;">=</span> 0
...</pre></div></div>

<p>This simply means: Give me a reference to <code>pingActor</code>, but if it doesn&#8217;t exist - create it with the following body. This mechanism makes it easy to have a single instance of an actor even if the same application is running on multiple nodes, without having to explicitly check if an actor has already been created or not.</p>
<p>Now we can run the program on two different nodes. After the actor has started and is running on one node, I&#8217;ll kill that node:</p>
<table>
<tr valign="top">
<td>Node 1</td>
<td>Node 2</td>
</tr>
<tr valign="top">
<td>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">Actor started.
Sun Jun 29 16:30:41 CEST 2008 - Got ping: 1 Total pings: 1
Sun Jun 29 16:30:42 CEST 2008 - Got ping: 2 Total pings: 2
Sun Jun 29 16:30:43 CEST 2008 - Got ping: 3 Total pings: 3
Sun Jun 29 16:30:44 CEST 2008 - Got ping: 4 Total pings: 4
Sun Jun 29 16:30:45 CEST 2008 - Got ping: 5 Total pings: 5
Sun Jun 29 16:30:46 CEST 2008 - Got ping: 6 Total pings: 6
Sun Jun 29 16:31:02 CEST 2008 - Got ping: 19 Total pings: 19
Sun Jun 29 16:31:03 CEST 2008 - Got ping: 20 Total pings: 20
Sun Jun 29 16:31:04 CEST 2008 - Got ping: 21 Total pings: 21
Sun Jun 29 16:31:05 CEST 2008 - Got ping: 22 Total pings: 22
Sun Jun 29 16:31:06 CEST 2008 - Got ping: 23 Total pings: 23
Sun Jun 29 16:31:07 CEST 2008 - Got ping: 24 Total pings: 24
Sun Jun 29 16:31:08 CEST 2008 - Got ping: 25 Total pings: 25
Sun Jun 29 16:31:09 CEST 2008 - Got ping: 26 Total pings: 26
Sun Jun 29 16:31:10 CEST 2008 - Got ping: 27 Total pings: 27
Sun Jun 29 16:31:11 CEST 2008 - Got ping: 28 Total pings: 28
...</pre></div></div>

</td>
<td>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">Sun Jun 29 16:30:47 CEST 2008 - Got ping: 6 Total pings: 6
Sun Jun 29 16:30:48 CEST 2008 - Got ping: 7 Total pings: 7
Sun Jun 29 16:30:49 CEST 2008 - Got ping: 8 Total pings: 8
Sun Jun 29 16:30:50 CEST 2008 - Got ping: 9 Total pings: 9
Sun Jun 29 16:30:51 CEST 2008 - Got ping: 10 Total pings: 10
Sun Jun 29 16:30:53 CEST 2008 - Got ping: 11 Total pings: 11
Sun Jun 29 16:30:54 CEST 2008 - Got ping: 12 Total pings: 12
Sun Jun 29 16:30:55 CEST 2008 - Got ping: 13 Total pings: 13
Sun Jun 29 16:30:56 CEST 2008 - Got ping: 14 Total pings: 14
Sun Jun 29 16:30:57 CEST 2008 - Got ping: 15 Total pings: 15
Sun Jun 29 16:30:58 CEST 2008 - Got ping: 16 Total pings: 16
Sun Jun 29 16:30:59 CEST 2008 - Got ping: 17 Total pings: 17
Sun Jun 29 16:31:00 CEST 2008 - Got ping: 18 Total pings: 18
Sun Jun 29 16:31:01 CEST 2008 - Got ping: 19 Total pings: 19^C</pre></div></div>

</td>
</tr>
</table>
<p>First Node 1 started up and ran the actor until Node 2 started. At this point the actor was distributed to Node 2 (determined by the automatic cache partitioning done by Coherence) and started there. As can be seen, the local state (the total pings) was persisted and transferred over. When Node 2 was killed the actor was migrated back and started on Node 1. Note that the state of the actor is persisted for each message, so a sudden shutdown of a JVM is not a problem.</p>
<p>One might wonder why the message for ping number 6 and 19 can be seen in both outputs - this happens as the actor was migrated while the actor thread was sleeping - before the react body was complete. This causes the new node to rerun the message (since the processing of the message didn&#8217;t complete on the old node) and the support code in the old node makes sure all messages sent by the old actor are discarded as it&#8217;s been terminated. It&#8217;s a bit tricky coding actors to be fully idempotent as not everything is handled in a transaction, but limiting side effects to sending messages at the end of the processing makes it fairly reliable.</p>
<p>Here&#8217;s a slightly more complex example:</p>

<div class="wp_syntax"><div class="code"><pre class="scala scala" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> coherencetest1
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> java.<span style="color: #006600;">util</span>.<span style="color: #006600;">Date</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> scala.<span style="color: #006600;">actors</span>.<span style="color: #006600;">coherence</span>.<span style="color: #006600;">CoActor</span>.<span style="color: #FFAA00;">_</span>
&nbsp;
<span style="color: #FFAA00;">@</span>serializable
<span style="color: #000000; font-weight: bold;">object</span> DactorTest2 <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">def</span> main<span style="color: #66cc66;">&#40;</span>args <span style="color: #FFAA00;">:</span> <span style="color: #aaaadd;">Array</span><span style="color: #66cc66;">&#91;</span><span style="color: #aaaadd;">String</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #FFAA00;">:</span> <span style="color: #993333;">Unit</span> <span style="color: #FFAA00;">=</span> <span style="color: #66cc66;">&#123;</span>
    init
&nbsp;
    readLine
&nbsp;
    <span style="color: #000000; font-weight: bold;">val</span> numActors <span style="color: #FFAA00;">=</span> <span style="color: #cc66cc;">80</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">val</span> actors <span style="color: #FFAA00;">=</span> <span style="color: #b1b100;">for</span><span style="color: #66cc66;">&#40;</span>id <span style="color: #FFAA00;">&lt;</span>- <span style="color: #cc66cc;">1</span> to numActors<span style="color: #66cc66;">&#41;</span>
      <span style="color: #000000; font-weight: bold;">yield</span> dactor <span style="color: #66cc66;">&#123;</span>
        loop <span style="color: #66cc66;">&#123;</span>
          react <span style="color: #66cc66;">&#123;</span>
          <span style="color: #b1b100;">case</span> <span style="color: #008000;">'ping</span> <span style="color: #FFAA00;">=&gt;</span>
            println<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Date + <span style="color: #ff0000;">&quot; - Actor &quot;</span> + id + <span style="color: #ff0000;">&quot; got ping.&quot;</span><span style="color: #66cc66;">&#41;</span>
            reply<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'pong</span>, id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
          <span style="color: #66cc66;">&#125;</span>
        <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
&nbsp;
    actors.<span style="color: #006600;">map</span><span style="color: #66cc66;">&#40;</span><span style="color: #FFAA00;">_</span> <span style="color: #FFAA00;">!</span> <span style="color: #008000;">'ping</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">force</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">var</span> pongs <span style="color: #FFAA00;">=</span> 0
&nbsp;
    <span style="color: #b1b100;">while</span><span style="color: #66cc66;">&#40;</span>pongs <span style="color: #FFAA00;">&lt;</span> numActors<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      receive <span style="color: #66cc66;">&#123;</span>
      <span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'pong</span>, x <span style="color: #FFAA00;">:</span> <span style="color: #993333;">Int</span><span style="color: #66cc66;">&#41;</span> <span style="color: #FFAA00;">=&gt;</span>
        pongs <span style="color: #FFAA00;">=</span> pongs + <span style="color: #cc66cc;">1</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    println<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Got &quot;</span> + pongs + <span style="color: #ff0000;">&quot; pongs.&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
    readLine
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>In this example 80 distributed actors are created and sent a ping message. After that the main thread receives all the pong replies. The output of this, when run on 4 nodes look like so:</p>
<table>
<tr valign="top">
<td>Node 1</td>
<td>Node 2</td>
</tr>
<tr valign="top">
<td>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">Sun Jun 29 09:19:34 CEST 2008 - Actor 1 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 12 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 15 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 18 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 22 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 25 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 27 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 29 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 41 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 42 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 47 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 50 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 54 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 56 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 61 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 75 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 79 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 80 got ping.
Got 80 pongs.</pre></div></div>

</td>
<td>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">Sun Jun 29 15:19:34 CEST 2008 - Actor 9 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 13 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 19 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 20 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 21 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 23 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 24 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 28 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 33 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 36 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 46 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 52 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 57 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 58 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 62 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 67 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 68 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 71 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 77 got ping.</pre></div></div>

</td>
</tr>
<tr valign="top">
<td>Node 3</td>
<td>Node 4</td>
</tr>
<tr valign="top">
<td>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">Sun Jun 29 15:19:34 CEST 2008 - Actor 2 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 4 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 5 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 6 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 7 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 8 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 10 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 11 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 30 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 31 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 32 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 34 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 35 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 37 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 39 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 40 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 43 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 44 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 45 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 53 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 59 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 60 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 63 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 64 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 66 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 69 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 70 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 72 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 74 got ping.</pre></div></div>

</td>
<td>

<div class="wp_syntax"><div class="code"><pre class="text text" style="font-family:monospace;">Sun Jun 29 15:19:34 CEST 2008 - Actor 3 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 14 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 16 got ping.
Sun Jun 29 15:19:34 CEST 2008 - Actor 17 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 26 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 38 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 48 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 49 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 51 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 55 got ping.
Sun Jun 29 15:19:35 CEST 2008 - Actor 65 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 73 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 76 got ping.
Sun Jun 29 15:19:36 CEST 2008 - Actor 78 got ping.</pre></div></div>

</td>
</tr>
</table>
<p>The program was started on all 4 nodes, but only the first node passes the first readLine. The other nodes just init the distributed actor framework and wait. As can be seen, the actors were distributed over the running nodes as expected - however, with small numbers like this the distribution can be a bit uneven (compare Node 3 and Node 4).</p>
<h3>The Solution</h3>
<p>The solution to allow the distribution of serializable actors is based on the Coherence backing map listeners. These can be used to get notifications of which specific node is the master for a certain object. As there only is one master for an object at any point, and a new master is allocated automatically if a master fails, we can use this to determine where the actor should run.</p>
<p>The object returned from <code>dactor { ... }</code> is a Proxy object - very similar to the Proxy used in the standard Scala remote actors. In fact, this whole thing is built in a way similar to the standard Scala remote actors, with Proxy objects acting on behalf of the sender on the receiver side and receiver on the sender side.</p>
<p>Additionally, the Coherence grid invocation mechanism allows us to deliver messages to running actors directly to the node where it is running.</p>
<p>When a new dactor is created, the following happens:</p>
<ol>
<li><code>dactor { ... }</code> creates a new anonymous <code>CoActor</code> class with the dactor body set as the <code>init</code> method.</li>
<li>The CoActor.distribute method is called which in turn saves the anonymous class instance to the Coherence cache. The object gets serialized when this happens. The key to the object in the cache is either the passed in symbol name, or a name created from a <code>java.util.UUID</code></li>
<li>The <code>dactor</code> function returns a Proxy object with the name of the newly created distributed actor.</li>
<li>Meanwhile, in the node which Coherence designates the master of the object, the backing map listener gets an insert event and starts the actor.</li>
<li>The default <code>act()</code> method is called, which in a CoActor calls the <code>init</code> method first.</li>
<li>The <code>init</code> method contains the <code>dactor { ... }</code> body, which gets executed.</li>
<li>The body executes as normally up until the <code>loop</code> block. <code>loop</code> in the CoActor first saves the body of the loop into a variable in CoActor so that it can continue executing just this body after it&#8217;s started in a new node, without running the initialization code again. After this the loop body is run as normal.</li>
<li>When <code>react</code> is hit, the thread breaks and the actor waits for incoming message using the normal actor scheduling mechanism.</li>
</ol>
<p>When a message is sent to a distributed actor using a Proxy object the following happens:</p>
<ol>
<li>The message is sent using the Coherence invocation mechanism which transports the message to the master node where the cached actor runs.</li>
<li>In the master node, a Proxy actor representing the sender (which does not need to be a distributed actor) is created - this is because the actor framework always needs a sending actor which the receiver can reply to.</li>
<li>The sender Proxy is asked to send the actual message to the real distributed actor receiver.</li>
<li>The normal Scala actor framework handles the passing of the message and scheduling of the actors.</li>
<li>An overriden <code>send</code> in CoActor locks the actor and stores the message and a Save message in the mailbox.</li>
<li>The Save message gets the actor to first persist itself to the Coherence cache with the real message still in the mailbox. This is to ensure the message isn&#8217;t lost in case of a node failure.</li>
<li>After this the real message is processed by the actor and an overridden <code>react</code> saves the actor again after the message has been processed. This to update the cache with the new state of the actor.</li>
</ol>
<p>If the distributed actor does a <code>reply</code> or sends a message to the <code>sender</code>, the <code>Proxy</code> which represents the non-distributed actor gets called as follows:</p>
<ol>
<li>As the recevier (the original sender) isn&#8217;t a distributed actor handled by Coherence we cannot use the invocation mechanism. Instead the message is just put into a message cache.</li>
<li>A <code>MapListener</code> on every node checks to see if the newly inserted message in the message cache is intended for an actor running on that specific node.</li>
<li>If so, the message is deleted from the cache and delivered to the local actor through a Proxy representing the sender - just as in the previous case.</li>
</ol>
<h3>The Limitations</h3>
<p>The distributed actors are a bit limited in what they can do, as they always needs to be serializable and I didn&#8217;t want to change any of the standard Scala code. For instance - when a synchronous invocation is made the return channel is stored in the Scala actor. The return channel implementation used in Scala isn&#8217;t serializable, so I decided to not implement this feature for now.</p>
<p>Basically, only message sending (!), reply, sender, loop and react are allowed in the distributed actors. However, they can interoperate with normal actors as can be seen in this example:</p>

<div class="wp_syntax"><div class="code"><pre class="scala scala" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">val</span> distActor <span style="color: #FFAA00;">=</span> dactor<span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'distActor</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      loop <span style="color: #66cc66;">&#123;</span>
        react <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'ping</span><span style="color: #66cc66;">&#41;</span> <span style="color: #FFAA00;">=&gt;</span>
          reply<span style="color: #66cc66;">&#40;</span><span style="color: #008000;">'pong</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    actor <span style="color: #66cc66;">&#123;</span>
      distActor <span style="color: #FFAA00;">!</span> <span style="color: #008000;">'ping</span>
      loop <span style="color: #66cc66;">&#123;</span>
        react <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">case</span> x <span style="color: #FFAA00;">=&gt;</span>
          println<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Actor got message: &quot;</span> + x<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span>
      <span style="color: #66cc66;">&#125;</span>
    <span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The Proxy objects can be used by actors as they serialize correctly.</p>
<h3>The Conclusion</h3>
<p>Distributing (or GridEnabling(tm) or whatever the word <em>du jour</em> is) actors to easily use the processing power and resilience multiple computers give but at the same time hiding the complexity from the developer is a nice way to fairly easily scale up an actors based application. To add more processing power or redundancy - just fire up new nodes.</p>
<p>The proof of concept I made here just scratches the surface, but it&#8217;s interesting to see that it can be done with Coherence while maintaining the syntax and behavior expected by Scala developers.</p>
<h3>The Source</h3>
<p>The highly experimental and hackish source code for the proof of concept is available in the git repository at:</p>
<pre>
http://martin.elwin.com/git/scala-coherence.git
</pre>
<p>Dependencies are Scala (2.7.1 recommended) and Oracle Coherence. There are some scripts for Linux which are trivial to adapt to other operating environments.</p>
<p>/M</p>
]]></content:encoded>
			<wfw:commentRss>http://martin.elwin.com/blog/2008/06/clustering-scala-actors-with-oracle-coherence/feed/</wfw:commentRss>
		</item>
		<item>
		<title>DCOP and xmobar</title>
		<link>http://martin.elwin.com/blog/2008/05/dcop-and-xmobar/</link>
		<comments>http://martin.elwin.com/blog/2008/05/dcop-and-xmobar/#comments</comments>
		<pubDate>Thu, 22 May 2008 19:27:23 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[dcop]]></category>

		<category><![CDATA[kde]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[xmobar]]></category>

		<category><![CDATA[xmonad]]></category>

		<guid isPermaLink="false">http://martin.elwin.com/blog/?p=22</guid>
		<description><![CDATA[For me, the computer is a work tool that should get out of the way as much as possible to allow me to perform the task at hand. I spend many hours every day working with, primarily, my laptop, and seemingly insignificant improvements to the desktop environment and development tools add up over time to [...]]]></description>
			<content:encoded><![CDATA[<p>For me, the computer is a work tool that should get out of the way as much as possible to allow me to perform the task at hand. I spend many hours every day working with, primarily, my laptop, and seemingly insignificant improvements to the desktop environment and development tools add up over time to improve my overall efficiency. Because of this I like to spend time looking into how the tools I use can be tuned to better help in what I need to do.</p>
<p>One of the fairly recent chances I&#8217;ve made is to switch to a <a href="http://en.wikipedia.org/wiki/Tiling_window_manager">tiling window manager</a>. For those too lazy to read the Wikipedia entry: A tiling window manager automatically arranges the open windows to fill the available space of the screen - saving you the job of arranging the windows yourself. That sounds simple, but can be quite complex in practice.</p>
<p>I started out using <a href="http://modeemi.fi/~tuomov/ion/">Ion3</a> in mid 2007. Ion3 is nice and smooth and worked great and really got me hooked on tiled window managers. However, as a big fan of open source, I was a bit concerned by the author&#8217;s approach to controlling the development of Ion (see <a href="http://womble.decadent.org.uk/blog/renaming-of-ion3">this</a> or <a href="http://forums.gentoo.org/viewtopic-t-559010-start-0-postdays-0-postorder-asc-highlight-.html">this</a> for instance) so I switched to <a href="http://www.suckless.org/wiki/">wmii</a>. wmii was also quite nice, but I never got into it as well as Ion, for some reason.</p>
<p>That&#8217;s when I found <a href="http://xmonad.org/">xmonad</a> - a very nice window manager written in <a href="http://www.haskell.org/">Haskell</a>. Haskell is also used as the configuration language, which makes the configuration possibilities close to endless (actually, they probably are endless, as Haskell is Turing equivalent (although there are <a href="http://en.wikipedia.org/wiki/Talk:Type_polymorphism#Why_Haskell.3F">people who doubt it</a>!) :).</p>
<p>Today, some 6 months later, I use xmonad 0.7 together with KDE 3.5.9 (similar to the <a href="http://www.haskell.org/haskellwiki/Xmonad/Using_xmonad_in_KDE">setup described on the Haskell wiki</a>) on Kubuntu 8.04 and am very happy with it.</p>
<h3>Status Bar</h3>
<p>However, xmonad is just a tiling window manager, but I also want a status bar to allow me to see chosen pieces of information at a glance. I use KDE3 and have the KDE3 panel <code>kicker</code> running, but it&#8217;s hidden unless I put the mouse in the bottom left corner. I don&#8217;t use it normally and only keep it around to access the system tray (which I almost never need). Instead of the ugly and bulky <code>kicker</code> I want a smaller, slimmer alternative&#8230;</p>
<p>Enter <a href="http://code.haskell.org/~arossato/xmobar/">xmobar</a> - a text based status bar, also written in Haskell. xmobar comes with a number of plugins to show different aspects of the system on the status bar - date, cpu, memory, network activity, etc. A few things it doesn&#8217;t do out of the box, which I want to see, are number of new mails, current keyboard layout (I use US mainly, but switch to Swedish for the national chars) and speaker mute state. Luckily, xmobar provides a plugin to execute shell commands, and using this mechanism we can put anything on the status bar, as long as we can figure out a command to run to produce the wanted text.</p>
<h3>New Mails</h3>
<p>I use a <a href="http://en.wikipedia.org/wiki/Maildir">Maildir</a> mail storage (together with <a href="http://www.dovecot.org/">dovecot</a> to provide an IMAP interface to my mail), and since I don&#8217;t automatically split mails into groups/folders it&#8217;s enough to just check the number of mails in my Maildir&#8217;s <code>new</code> folder. The xmobar command for this ends up as:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell haskell" style="font-family:monospace;">Run Com <span style="background-color: #3cb371;">&quot;sh&quot;</span> <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;-c&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;<span style="background-color: #3cb371; font-weight: bold;">\&quot;</span>ls ~/Maildir/new | wc -l<span style="background-color: #3cb371; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: green;">&#93;</span> <span style="background-color: #3cb371;">&quot;mailcount&quot;</span> <span style="color: red;">600</span></pre></div></div>

<p>This command invokes the <code>sh</code> shell and passes a <em>command string</em> using the <code>-c</code> switch. The command string contains the actual shell command we want to execute: <code>ls ~/Maildir/new | wc -l</code></p>
<p>Executing <code>sh</code> and passing a command string allows us to use the pipe to pass the output of the <code>ls</code> to the <code>wc</code> to get the actual file count back.</p>
<p>The command is mapped to the <code>mailcount</code> alias, and the interval is set to 600 tenths of a second = 60 seconds.</p>
<h3>Keyboard Layout</h3>
<p>Since I use KDE3 the keyboard layout switching is handled by the <code>kxkb</code> application (shows up as a flag in the tray if multiple layouts are configured). KDE3 uses <a href="http://en.wikipedia.org/wiki/DCOP">DCOP</a> (unlike KDE4 which uses <a href="http://en.wikipedia.org/wiki/D-Bus">D-Bus</a>) for IPC. Available DCOP services can easily be interogated from the command line using the&#8230; you guessed it&#8230; <code>dcop</code> command.</p>
<p>Running <code>dcop</code> without parameters shows a list of available applications. On my system it shows:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">dcop</span>
konsole-<span style="color: #000000;">9099</span>
kdebluetooth
kicker
kxkb
guidance-<span style="color: #000000;">6223</span>
kded
adept_notifier
kmix
knotify
kio_uiserver
klauncher
khotkeys
kwalletmanager
digikam-<span style="color: #000000;">6484</span>
klipper
ksmserver
knetworkmanager</pre></div></div>

<p>Woohoo! kxkb is there, so let&#8217;s dig deeper. By a bit of trial and error we can probe the internals of the dcop-exposed application:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">dcop</span> kxkb
qt
MainApplication-Interface
kxkb
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">dcop</span> kxkb kxkb
QCStringList interfaces<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
QCStringList functions<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
bool setLayout<span style="color: #7a0874; font-weight: bold;">&#40;</span>QString layoutPair<span style="color: #7a0874; font-weight: bold;">&#41;</span>
QString getCurrentLayout<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
QStringList getLayoutsList<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
void forceSetXKBMap<span style="color: #7a0874; font-weight: bold;">&#40;</span>bool <span style="color: #000000; font-weight: bold;">set</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">dcop</span> kxkb kxkb getCurrentLayout
us</pre></div></div>

<p>Sweet! Now we have the dcop &#8220;path&#8221; to find the current keyboard layout. Let&#8217;s add it to the xmobar configuration:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell haskell" style="font-family:monospace;">Run Com <span style="background-color: #3cb371;">&quot;dcop&quot;</span> <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;kxkb&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;kxkb&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;getCurrentLayout&quot;</span><span style="color: green;">&#93;</span> <span style="background-color: #3cb371;">&quot;kbd&quot;</span> <span style="color: red;">20</span></pre></div></div>

<h3>Speaker Mute State</h3>
<p>Using <code>dcop</code> again we can find the mute state:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">dcop</span> kmix
qt
MainApplication-Interface
Mixer0
kmix
kmix-mainwindow<span style="color: #666666; font-style: italic;">#1</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">dcop</span> kmix Mixer0
QCStringList interfaces<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
QCStringList functions<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
void setVolume<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx,int percentage<span style="color: #7a0874; font-weight: bold;">&#41;</span>
void setMasterVolume<span style="color: #7a0874; font-weight: bold;">&#40;</span>int percentage<span style="color: #7a0874; font-weight: bold;">&#41;</span>
void increaseVolume<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx<span style="color: #7a0874; font-weight: bold;">&#41;</span>
void decreaseVolume<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx<span style="color: #7a0874; font-weight: bold;">&#41;</span>
int volume<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx<span style="color: #7a0874; font-weight: bold;">&#41;</span>
int masterVolume<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
void setAbsoluteVolume<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx,long int absoluteVolume<span style="color: #7a0874; font-weight: bold;">&#41;</span>
long int absoluteVolume<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx<span style="color: #7a0874; font-weight: bold;">&#41;</span>
long int absoluteVolumeMin<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx<span style="color: #7a0874; font-weight: bold;">&#41;</span>
long int absoluteVolumeMax<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx<span style="color: #7a0874; font-weight: bold;">&#41;</span>
void setMute<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx,bool on<span style="color: #7a0874; font-weight: bold;">&#41;</span>
void setMasterMute<span style="color: #7a0874; font-weight: bold;">&#40;</span>bool on<span style="color: #7a0874; font-weight: bold;">&#41;</span>
void toggleMute<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx<span style="color: #7a0874; font-weight: bold;">&#41;</span>
void toggleMasterMute<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
bool mute<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx<span style="color: #7a0874; font-weight: bold;">&#41;</span>
bool masterMute<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
int masterDeviceIndex<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
void setRecordSource<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx,bool on<span style="color: #7a0874; font-weight: bold;">&#41;</span>
bool isRecordSource<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx<span style="color: #7a0874; font-weight: bold;">&#41;</span>
void setBalance<span style="color: #7a0874; font-weight: bold;">&#40;</span>int balance<span style="color: #7a0874; font-weight: bold;">&#41;</span>
bool isAvailableDevice<span style="color: #7a0874; font-weight: bold;">&#40;</span>int deviceidx<span style="color: #7a0874; font-weight: bold;">&#41;</span>
QString mixerName<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
int open<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
int close<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">%</span> <span style="color: #c20cb9; font-weight: bold;">dcop</span> kmix Mixer0 masterMute
<span style="color: #c20cb9; font-weight: bold;">true</span></pre></div></div>

<p>Perfect - now we can create the last xmobar run command:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell haskell" style="font-family:monospace;">Run Com <span style="background-color: #3cb371;">&quot;dcop&quot;</span> <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;kmix&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;Mixer0&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;masterMute&quot;</span><span style="color: green;">&#93;</span> <span style="background-color: #3cb371;">&quot;mute&quot;</span> <span style="color: red;">20</span></pre></div></div>

<h3>Final Configuration</h3>
<p>So, here&#8217;s the final <code>.xmobarrc</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell haskell" style="font-family:monospace;">Config <span style="color: green;">&#123;</span> font <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;xft:Consolas-8&quot;</span>
       <span style="color: #339933; font-weight: bold;">,</span> bgColor <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;black&quot;</span>
       <span style="color: #339933; font-weight: bold;">,</span> fgColor <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;grey&quot;</span>
       <span style="color: #339933; font-weight: bold;">,</span> position <span style="color: #339933; font-weight: bold;">=</span> Bottom
       <span style="color: #339933; font-weight: bold;">,</span> commands <span style="color: #339933; font-weight: bold;">=</span> <span style="color: green;">&#91;</span> Run Network <span style="background-color: #3cb371;">&quot;eth0&quot;</span> <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;-L&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;0&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;-H&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;32&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;--normal&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;green&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;--high&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;red&quot;</span><span style="color: green;">&#93;</span> <span style="color: red;">50</span>
                    <span style="color: #339933; font-weight: bold;">,</span> Run Network <span style="background-color: #3cb371;">&quot;wlan0&quot;</span> <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;-L&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;0&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;-H&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;32&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;--normal&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;green&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;--high&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;red&quot;</span><span style="color: green;">&#93;</span> <span style="color: red;">51</span>
                    <span style="color: #339933; font-weight: bold;">,</span> Run Cpu <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;-L&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;3&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;-H&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;50&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;--normal&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;green&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;--high&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;red&quot;</span><span style="color: green;">&#93;</span> <span style="color: red;">52</span>
                    <span style="color: #339933; font-weight: bold;">,</span> Run Memory <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;-t&quot;</span><span style="color: #339933; font-weight: bold;">,</span><span style="background-color: #3cb371;">&quot;Mem: &lt;usedratio&gt;%&quot;</span><span style="color: green;">&#93;</span> <span style="color: red;">54</span>
                    <span style="color: #339933; font-weight: bold;">,</span> Run Date <span style="background-color: #3cb371;">&quot;%a %b %_d %H:%M:%S&quot;</span> <span style="background-color: #3cb371;">&quot;date&quot;</span> <span style="color: red;">10</span>
                    <span style="color: #339933; font-weight: bold;">,</span> Run StdinReader
                    <span style="color: #339933; font-weight: bold;">,</span> Run Com <span style="background-color: #3cb371;">&quot;dcop&quot;</span> <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;kxkb&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;kxkb&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;getCurrentLayout&quot;</span><span style="color: green;">&#93;</span> <span style="background-color: #3cb371;">&quot;kbd&quot;</span> <span style="color: red;">20</span>
                    <span style="color: #339933; font-weight: bold;">,</span> Run Com <span style="background-color: #3cb371;">&quot;sh&quot;</span> <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;-c&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;<span style="background-color: #3cb371; font-weight: bold;">\&quot;</span>ls ~/Maildir/new | wc -l<span style="background-color: #3cb371; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: green;">&#93;</span> <span style="background-color: #3cb371;">&quot;mailcount&quot;</span> <span style="color: red;">600</span>
                    <span style="color: #339933; font-weight: bold;">,</span> Run Com <span style="background-color: #3cb371;">&quot;dcop&quot;</span> <span style="color: green;">&#91;</span><span style="background-color: #3cb371;">&quot;kmix&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;Mixer0&quot;</span><span style="color: #339933; font-weight: bold;">,</span> <span style="background-color: #3cb371;">&quot;masterMute&quot;</span><span style="color: green;">&#93;</span> <span style="background-color: #3cb371;">&quot;mute&quot;</span> <span style="color: red;">20</span>
                    <span style="color: green;">&#93;</span>
       <span style="color: #339933; font-weight: bold;">,</span> sepChar <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;%&quot;</span>
       <span style="color: #339933; font-weight: bold;">,</span> alignSep <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;}{&quot;</span>
       <span style="color: #339933; font-weight: bold;">,</span> template <span style="color: #339933; font-weight: bold;">=</span> <span style="background-color: #3cb371;">&quot;&lt;fc=#ee9a00&gt;%date%&lt;/fc&gt; | %cpu% | %memory% | %eth0% - %wlan0% } %StdinReader% { Mail: %mailcount% | Kbd: %kbd% | Mute: %mute%&quot;</span>
       <span style="color: green;">&#125;</span></pre></div></div>

<p>And a small screenshot for your viewing pleasure:</p>
<p><a href='http://martin.elwin.com/blog/wp-content/uploads/2008/05/xmonad.png'><img src="http://martin.elwin.com/blog/wp-content/uploads/2008/05/xmonad.png" alt="" title="xmonad" width="300" height="187" class="alignnone size-medium wp-image-23" /></a><br />
(Yes, my laptop&#8217;s hostname is <em>dellicious</em>&#8230; ;)</p>
]]></content:encoded>
			<wfw:commentRss>http://martin.elwin.com/blog/2008/05/dcop-and-xmobar/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Scala XML and Java DOM</title>
		<link>http://martin.elwin.com/blog/2008/05/scala-xml-and-java-dom/</link>
		<comments>http://martin.elwin.com/blog/2008/05/scala-xml-and-java-dom/#comments</comments>
		<pubDate>Thu, 15 May 2008 21:49:05 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[scala]]></category>

		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://martin.elwin.com/blog/?p=21</guid>
		<description><![CDATA[How do we go from and DOM to Scala XML the most efficiently? Well&#8230; That&#8217;s what I asked myself the other day. The Scala API scala.xml.XML object has helper functions to create Scala XML structures from the usual suspects: Strings, InputStream, Reader, File, etc. But DOM Document or Element is missing.
Going via a Byte array, [...]]]></description>
			<content:encoded><![CDATA[<p>How do we go from and DOM to Scala XML the most efficiently? Well&#8230; That&#8217;s what I asked myself the other day. The Scala API <a href="http://www.scala-lang.org/docu/files/api/scala/xml/XML$object.html"><code>scala.xml.XML</code></a> object has helper functions to create Scala XML structures from the usual suspects: Strings, InputStream, Reader, File, etc. But DOM Document or Element is missing.</p>
<p>Going via a Byte array, Char array or String is simple. For instance, the following outputs the DOM to a char array, which is then used as the source of the Scala XML creation:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">Using Char array
<span style="color: #666666; font-style: italic;">//dom is the DOM Element</span>
val charWriter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">CharArrayWriter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
TransformerFactory.<span style="color: #006633;">newInstance</span>.<span style="color: #006633;">newTransformer</span>.<span style="color: #006633;">transform</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DOMSource<span style="color: #009900;">&#40;</span>dom<span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> StreamResult<span style="color: #009900;">&#40;</span>charWriter<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
val xml <span style="color: #339933;">=</span> XML.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">CharArrayReader</span><span style="color: #009900;">&#40;</span>charWriter.<span style="color: #006633;">toCharArray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>This works fine, and is reasonably fast. However, it does allocate some unnecessary memory (the char array) and performs some unnecessary parsing (of the char array) - both of which we&#8217;d really like to avoid.</p>
<p>How do we do this? Well, here&#8217;s one option that I came up with:</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">Using SAX
val saxHandler <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NoBindingFactoryAdapter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
saxHandler.<span style="color: #006633;">scopeStack</span>.<span style="color: #006633;">push</span><span style="color: #009900;">&#40;</span>TopScope<span style="color: #009900;">&#41;</span>
TransformerFactory.<span style="color: #006633;">newInstance</span>.<span style="color: #006633;">newTransformer</span>.<span style="color: #006633;">transform</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DOMSource<span style="color: #009900;">&#40;</span>dom<span style="color: #009900;">&#41;</span>, <span style="color: #000000; font-weight: bold;">new</span> SAXResult<span style="color: #009900;">&#40;</span>saxHandler<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
saxHandler.<span style="color: #006633;">scopeStack</span>.<span style="color: #006633;">pop</span>
val xml <span style="color: #339933;">=</span> factoryHandler.<span style="color: #006633;">rootElem</span></pre></div></div>

<p>What&#8217;s going on here? Well, the Scala XML library uses SAX to parse XML and create the XML structure. One way of generating SAX events is to walk a DOM tree, which is handled by the <code>javax.xml.transform.Transformer</code> with a <code>DOMSource</code> as input and a <code>SAXResult</code> as output. The extension of the <code>DefaultHandler</code> needed for handling the SAX events is implemented by the <code>scala.xml.parsing.FactoryAdapter</code>, which is extended by the <code>NoBindingFactoryAdapter</code> used to construct the XML structure. Because of this, we can do violence on the API and use the <code>NoBindingFactoryAdapter</code> directly as a SAX <code>DefaultHandler</code> - nice! The <code>scopeStack</code> calls are done to maintain the scope information, which I stole from the <code>loadXML</code> method in the <code>AdapterFactory</code> class.</p>
<p>However, let&#8217;s take a moment to reflect on this. Using the Scala XML library in this way is not really good. Even if it&#8217;s possible to do it this way, I&#8217;ve not seen it described as a supported way of using it and therefore it should be done only after considering that the next release of Scala might remove this possibility.</p>
<p>[<strong>Update 2008-07-02:</strong> <a href="http://burak.emir.googlepages.com/">Burak Emir</a> kindly added a comment; "Don't worry, the SAX factory adapter is not going to go away." - good to know!]</p>
<p>That said - let&#8217;s consider this an exploration of possibilities which could potentially lead to an update of the Scala XML API to allow a DOM to be used as a source instead&#8230;!</p>
<p>A quick test gives the following result.</p>
<p>Test data is a 6897 bytes XML file containing 118 elements with some 4 different namespaces.</p>
<p>I ran each test in 1000 iterations, with a full garbage collection before the first iteration. For every 100 iterations I printed the delta of the free memory and then timed the time for the complete 1000 iterations.</p>
<p>Char array: 100 iterations use around 28 MB, full test: 1414 ms<br />
SAX: 100 iterations use around 18 MB, full test: 970 ms</p>
<p>So, in conclusion, not overwhelming difference, but around 1/3 faster and 1/3 less memory consumed. Can we do better? I&#8217;m not sure. :)</p>
<p>The next step is to do Scala XML to DOM&#8230; This could be more interesting. I see two options:</p>
<ol>
<li>Implement the DOM API wrapping Scala XML</li>
<li>Generate SAX events based on the Scala XML and use that to build a DOM</li>
</ol>
<p>Option 1 would be more efficient - but the DOM API isn&#8217;t fun implementing. Option 2 would be much simpler, but probably would be less efficient and require more allocations. Gotta think about this one&#8230;</p>
<p>/M</p>
]]></content:encoded>
			<wfw:commentRss>http://martin.elwin.com/blog/2008/05/scala-xml-and-java-dom/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Compressed and Encrypted Backup with SquashFS and LUKS</title>
		<link>http://martin.elwin.com/blog/2008/05/backups-with-squashfs-and-luks/</link>
		<comments>http://martin.elwin.com/blog/2008/05/backups-with-squashfs-and-luks/#comments</comments>
		<pubDate>Sun, 11 May 2008 20:01:55 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[backup]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[luks]]></category>

		<category><![CDATA[squashfs]]></category>

		<guid isPermaLink="false">http://martin.elwin.com/blog/?p=20</guid>
		<description><![CDATA[Hardy Heron (KDE flavor) has been out for a while now and this weekend I finally decided to upgrade my (sweet sweet Dell M90) laptop from Gutsy. I used this as justification to get a new Hitachi SATA 200GB 7200RPM disk to replace the old 250GB 5200RPM in an effort to boost performance a little. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://kubuntu.org">Hardy Heron (KDE flavor)</a> has been out for a while now and this weekend I finally decided to upgrade my (sweet sweet Dell M90) laptop from Gutsy. I used this as justification to get a new Hitachi SATA 200GB 7200RPM disk to replace the old 250GB 5200RPM in an effort to boost performance a little. Getting a new disk also makes the upgrade a lot less risky - I can keep the old disk as it is while setting up the new system.</p>
<p>This time around I decided to get rid of the Windows partition previously used for dual booting (haven&#8217;t booted into Windows in 6 months). I also wanted to switch from the extremely useful and amazing <a href="http://www.truecrypt.org/">Truecrypt</a> to using Linux native <a href="http://luks.endorphin.org/">LUKS</a> encryption for my work and private data. I originally used Truecrypt on Linux with an NTFS file system to make the encrypted drive compatible with both Windows and Linux, but since then I&#8217;ve switched to an ext3 file system and don&#8217;t need the capability to mount it both under Linux and Windows. If you do, make sure to try - nay, use! - Truecrypt. It&#8217;s very very nice.</p>
<p>So, the 200GB disk ended up being partitioned like so:</p>
<pre>
$sudo fdisk -l /dev/sda

Disk /dev/sda: 200.0 GB, 200049647616 bytes
255 heads, 63 sectors/track, 24321 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0x000cbcfd

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        3647    29294496   83  Linux
/dev/sda2            3648        7294    29294527+  83  Linux
/dev/sda3            7295       23707   131837422+  83  Linux
/dev/sda4           23708       24321     4931955   82  Linux swap
</pre>
<p>That is, 30GB for root, 30GB to be encrypted, 135GB for data and the rest for swap.</p>
<p>Of course, with Hardy you can <a href="http://learninginlinux.wordpress.com/2008/04/23/installing-ubuntu-804-with-full-disk-encryption/">encrypt the root file system</a> just by selecting to do so in the alternate installer, but I don&#8217;t feel a need to go that way right now - maybe later.</p>
<h3>Backups</h3>
<p>Now, slowly approaching the main topic of this post, while playing around with the new disk and the old data I was thinking about putting in place a new backup strategy. At the same time (I can do anything while simultaneously going through my subscriptions in Google Reader:) I happened upon the <a href="http://jwz.livejournal.com/801607.html">JWZ post about backups</a>. JWZ, who obviously is a smart guy, is onto something. I&#8217;m already doing backups (of part of my data) using <code>rsync</code> to my server at home - not full disk image, admittedly, which was JWZ&#8217;s prescription. However, I wanted to try something different and achieve a few other things:</p>
<ul>
<li>Retain multiple snapshots of data at a time</li>
<li>Compress the data</li>
<li>Encrypt the data</li>
</ul>
<p>For a while I was considering ZFS (which I in a moment of temporary megalomania started porting to a native Linux kernel module, strictly for private use! - maybe something for another post) with its nice snapshot and compression support, but this idea was quickly discarded for being a bit too unnatural for most Linux setups. I added the additional requirement that the solution should work without having too many non-standard dependencies.</p>
<p><a href="http://bisqwit.iki.fi/source/cromfs.html">CromFS</a> has a nice feature set, but it&#8217;s a FUSE file system and it&#8217;s not available in Hardy. The comparison chart on the CromFS page however led me to <a href="http://squashfs.sourceforge.net/">SquashFS</a>. SquashFS is available out-of-the-box in Hardy and by <code>apt-get</code>ting the<code> squashfs-tools</code> package, everything needed is installed. The nice <a href="http://tldp.org/HOWTO/SquashFS-HOWTO/">HowTo</a> gives a good introduction to the commands required to use it. Note - SquashFS, as most compressed file systems, is read-only. This suites me perfectly as I want to make a snapshot for backup purposes, but it might not be what you want.</p>
<p>The idea I got was to use SquashFS as the compressed snapshot file system and wrap it in LUKS for encryption. By doing this I get a single compressed file which I can mount on a modern Linux distribution to access the data. It&#8217;s securely and (somewhat) efficiently stored.</p>
<p>Luckily, what I wanted to do is very similar to what the <a href="http://gentoo-wiki.com/HOWTO_Burn_Encrypted_Optical_Media_With_Luks">Gentoo guide for how to burn and encrypted CD image</a> describes - namely wrapping an already existing file system image in a LUKS encrypted container. Most of the following is based on the Gentoo guide.</p>
<p>There are a few steps to the process:</p>
<ol>
<li>Create SquashFS image from source data</li>
<li>Create LUKS container</li>
<li>Put SquashFS image inside LUKS container</li>
</ol>
<p>The trickiest (which isn&#8217;t really that tricky) part is figuring out how large to make the LUKS container. As we don&#8217;t know beforehand how large the SquashFS image is, we need to create it and then calculate how large the LUKS container should be.</p>
<h3>Step 1: SquashFS</h3>
<p>Let&#8217;s start with creating the SquashFS image:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$sudo mksquashfs /crypt /tmp/cryptbackup.sqsh -e /crypt/stuff
Parallel mksquashfs: Using 2 processors
Creating little endian 3.1 filesystem on /tmp/cryptbackup.sqsh, block size 131072.
[==========================================================================] 2297/2297 100%
Exportable Little endian filesystem, data block size 131072, compressed data, compressed metadata, compressed fragments, duplicates are removed
Filesystem size 13606.69 Kbytes (13.29 Mbytes)
        21.57% of uncompressed filesystem size (63077.89 Kbytes)
Inode table size 38419 bytes (37.52 Kbytes)
        32.62% of uncompressed inode table size (117780 bytes)
Directory table size 33629 bytes (32.84 Kbytes)
        58.10% of uncompressed directory table size (57880 bytes)
Number of duplicate files found 115
Number of inodes 3176
Number of files 1877
Number of fragments 45
Number of symbolic links  945
Number of device nodes 0
Number of fifo nodes 0
Number of socket nodes 0
Number of directories 354
Number of uids 5
        ....
Number of gids 12
        ....</pre></div></div>

<p>This creates a new compressed SquashFS image from the data in the <code>/crypt</code> directory (the contents of this directory becomes the contents of the image root). The <code>-e</code> flag excludes all files in the given directories - here, everything in <code>/crypt/stuff</code>. Note that it might be more efficient to store this image on a separate disk, if available. Even an external USB 2.0 mounted disk might be faster to write to while reading the data from the main disk.</p>
<p>The SquashFS image was compressed by about 50% in my case - 10GB of data stored in a 5GB image. Of course, the compression ratio achieved depends on the type data stored.</p>
<h3>Step 2: LUKS Container</h3>
<p>Step 1 done, we now need to create a LUKS container to store the SquashFS image in. How big should it be? Well&#8230; The Gentoo guide linked to above calculates the size of the LUKS overhead by checking the difference between a LUKS container and the mapped block device. It turns out that this overhead is 1032 blocks (each block being 512 bytes), no matter what the block size is. Googling this seems to confirm it, so for now I&#8217;m assuming that LUKS always adds 1032 blocks of overhead.</p>
<p>The size in 512 byte blocks of the SquashFS image can be found by doing:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$ls -l --block-size=512 /tmp/cryptbackup.sqsh
-rwx------ 1 root root 27216 2008-05-11 20:55 /data/cryptbackup.sqsh</pre></div></div>

<p>Which in the above case indicates that the file is 27216 512 byte blocks large (this is a test file&#8230;).</p>
<p>Adding 1032 blocks gives us the size needed for the LUKS container - 28248 blocks - let&#8217;s create it (while letting the shell handle the calculation for us):</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$sudo dd if=/dev/zero of=/tmp/cryptbackupluks.img bs=512 count=1 seek=$((27216+1032))</pre></div></div>

<p>Note that this creates a sparse file on most modern file systems, so it&#8217;s quite quick. We don&#8217;t need to fill it with random numbers or anything as the whole container will be updated when we write the SquashFS image to it.</p>
<p>Now, let&#8217;s map it. First locate an available loop device:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$sudo losetup -f
/dev/loop0</pre></div></div>

<p><code>loop0</code> is available - no loops on this system.</p>
<p>Set up the container file as a loop device:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$sudo losetup /dev/loop0 /tmp/cryptbackupluks.img</pre></div></div>

<p>Then make it a LUKS volume:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$sudo cryptsetup luksFormat /dev/loop0
&nbsp;
WARNING!
========
This will overwrite data on /dev/loop0 irrevocably.
&nbsp;
Are you sure? (Type uppercase yes): YES
Enter LUKS passphrase:
Verify passphrase:
Command successful.</pre></div></div>

<p>Make sure you remember the password&#8230; ;P</p>
<p>And open the device:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$sudo cryptsetup luksOpen /dev/loop0 cryptbackup
Enter LUKS passphrase:
key slot 0 unlocked.
Command successful.</pre></div></div>

<p>Now the device is available as <code>/dev/mapper/cryptbackup</code>, ready to accept our SquashFS image.</p>
<h3>Step 3: Put SquashFS Image into LUKS Container</h3>
<p>Let&#8217;s validate the overhead of LUKS:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$echo $((`sudo blockdev --getsize /dev/loop0`-`sudo blockdev --getsize /dev/mapper/cryptbackup`))
1032</pre></div></div>

<p>Sweet! So, the size of the mapped device should be the same as our SquashFS image:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$sudo blockdev --getsize /dev/mapper/cryptbackup
27217</pre></div></div>

<p>Hmmm&#8230; Close enough&#8230; ;P Perhaps there is some rounding to a full KB or something like that going on. Anywho, at least it is big enough for our 27216 block image. Let&#8217;s transfer it:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$sudo dd if=/tmp/cryptbackup.sqsh of=/dev/mapper/cryptbackup bs=512
27216+0 records in
27216+0 records out
13934592 bytes (14 MB) copied, 0.0544451 s, 256 MB/s</pre></div></div>

<p>Done and done. To verify that it works we can mount the file system:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$sudo mkdir /mnt/cryptbackup
$sudo mount /dev/mapper/cryptbackup /mnt/cryptbackup</pre></div></div>

<p>If all went well, <code>ls /mnt/cryptbackup</code> should now give the contents of the original directory.</p>
<p>To unmount, do:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$sudo umount /mnt/cryptbackup
$sudo cryptsetup luksClose cryptbackup
$sudo losetup -d /dev/loop0</pre></div></div>

<p>Now remove the old SquashFS image <code>/tmp/cryptbackup.sqsh</code> and store the LUKS container <code>/tmp/cryptbackupluks.img</code> in a safe location. I use a portable external hard disk and the server at home to save the backup images. To mount later, just run a few of the, slightly modified, above commands again:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$LOOP=`sudo losetup -s -f /tmp/cryptbackupluks.img`
$sudo cryptsetup luksOpen $LOOP cryptbackup
Enter LUKS passphrase:
key slot 0 unlocked.
Command successful.
$sudo mount /dev/mapper/cryptbackup /mnt/cryptbackup</pre></div></div>

<p>Now, all that remains is to create some helper scripts to avoid having to write all this every time I want to make a backup&#8230;</p>
<p>A drawback of this method is that it takes quite a while to perform the backups. It&#8217;s not incremental either, so the backups will presumably take longer and longer to make each time (as more data is accumulated). Next thing might be to try to create a base image with SquashFS and then do incremental backups with UnionFS or something&#8230; Hmmm&#8230;&#8230;</p>
<p>/M</p>
]]></content:encoded>
			<wfw:commentRss>http://martin.elwin.com/blog/2008/05/backups-with-squashfs-and-luks/feed/</wfw:commentRss>
		</item>
		<item>
		<title>UML Use Case Diagrams &#038; Graphviz</title>
		<link>http://martin.elwin.com/blog/2008/05/uml-use-case-diagrams-graphviz/</link>
		<comments>http://martin.elwin.com/blog/2008/05/uml-use-case-diagrams-graphviz/#comments</comments>
		<pubDate>Sun, 11 May 2008 11:06:59 +0000</pubDate>
		<dc:creator>Martin</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[dot]]></category>

		<category><![CDATA[graphviz]]></category>

		<category><![CDATA[uml]]></category>

		<category><![CDATA[usecase]]></category>

		<guid isPermaLink="false">http://martin.elwin.com/blog/?p=8</guid>
		<description><![CDATA[First post!
Oh, right, this isn&#8217;t Slashdot&#8230;
Anywho, here goes:
While at a customer, a few colleagues and I were joking about Use Case diagrams, which in their most basic form tend to be somewhat meaningless. However, in more complex requirements definitions, they certainly can help define actors and sections of functionality to go into certain releases, for [...]]]></description>
			<content:encoded><![CDATA[<p>First post!</p>
<p>Oh, right, this isn&#8217;t Slashdot&#8230;</p>
<p>Anywho, here goes:</p>
<p>While at a customer, a few colleagues and I were joking about <a href="http://en.wikipedia.org/wiki/Use_case_diagram">Use Case diagrams</a>, which in their most basic form tend to be somewhat meaningless. However, in more complex requirements definitions, they certainly can help define actors and sections of functionality to go into certain releases, for instance.</p>
<p>As I at the time was pondering how to best visualize the customer&#8217;s SOA architecture and service dependencies by generating <a href="http://www.graphviz.org">Graphviz</a> diagrams, I thought - hey, why not use Graphviz to quickly produce some snazzy Use Case Diagrams?</p>
<p>Well&#8230; the snazziness is debatable, and I didn&#8217;t manage it as quickly as I had hoped. But all considered, I think they turned out quite well.</p>
<p>To begin with, I have to figure out how to render a stick figure for a node. When using the postscript output, you can <a href="http://www.graphviz.org/Documentation/html/shapehowto.html">create custom shapes</a>, but since I want to create just a PNG image, I need to use an image as the custom shape file.</p>
<p>With <a href="http://www.inkscape.org/">Inkscape</a> I managed to produce the following:</p>
<p><a href='http://martin.elwin.com/blog/wp-content/uploads/2008/05/stick.png'><img src="http://martin.elwin.com/blog/wp-content/uploads/2008/05/stick.png" alt="Use Case Stick Figure" title="Use Case Stick Figure" class="size-full wp-image-13" /></a></p>
<p>Very nice.</p>
<p>Saving this as <code>stick.png</code>, we can create a simple diagram:</p>

<div class="wp_syntax"><div class="code"><pre class="dot dot" style="font-family:monospace;">1.<span style="color: #993333;">dot</span>
<span style="color: #000000; font-weight: bold;">digraph</span> G <span style="color: #66cc66;">&#123;</span>
    <span style="color: #ff0000;">&quot;User&quot;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">shapefile</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stick.png&quot;</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #ff0000;">&quot;Log In&quot;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">shape</span><span style="color: #66cc66;">=</span><span style="color: #993333;">ellipse</span><span style="color: #66cc66;">&#93;</span>;
    <span style="color: #ff0000;">&quot;User&quot;</span><span style="color: #66cc66;">-&gt;</span><span style="color: #ff0000;">&quot;Log In&quot;</span> <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">arrowhead</span><span style="color: #66cc66;">=</span><span style="color: #993333;">none</span><span style="color: #66cc66;">&#93;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This can be run through the Graphviz <code>dot</code> program thus:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">cat 1.dot | dot -Tpng &gt; 1.png</pre></div></div>

<p>Which produces:</p>
<p><a href='http://martin.elwin.com/blog/wp-content/uploads/2008/05/1.png'><img src="http://martin.elwin.com/blog/wp-content/uploads/2008/05/1.png" alt="Use Case 1" title="1.png" class="size-medium wp-image-16" /></a></p>
<p>Hmmm&#8230; Not quite what we wanted. Let&#8217;s make a few changes:</p>
<ul>
<li>Make the graph left-right.</li>
<li>Get rid of the box.</li>
<li>Move the actor label to below the stick figure.</li>
</ul>
<p>The most difficult of this is moving the label. This seems to require creating a cluster subgraph containing a label and the node with the custom shape. An example of this can be seen in <a href="http://www.karakas-online.de/forum/viewtopic.php?t=2647">this Graphviz tutorial</a>. Another alternative is using HTML in the label - but this looks ugly. So, doing this, and refactoring a bit to create nodes explicitly, we get:</p>

<div class="wp_syntax"><div class="code"><pre class="dot dot" style="font-family:monospace;">2.<span style="color: #993333;">dot</span>
<span style="color: #000000; font-weight: bold;">digraph</span> G <span style="color: #66cc66;">&#123;</span>
   <span style="color: #000066;">rankdir</span><span style="color: #66cc66;">=</span>LR;
&nbsp;
    <span style="color: #000000; font-weight: bold;">subgraph</span> clusterUser <span style="color: #66cc66;">&#123;</span><span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;User&quot;</span>; labelloc<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;b&quot;</span>; <span style="color: #000066;">peripheries</span><span style="color: #66cc66;">=</span>0; user<span style="color: #66cc66;">&#125;</span>;
    user <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">shapefile</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stick.png&quot;</span>, <span style="color: #000066;">peripheries</span><span style="color: #66cc66;">=</span>0, <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #993333;">invis</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    login <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Log In&quot;</span>, <span style="color: #000066;">shape</span><span style="color: #66cc66;">=</span><span style="color: #993333;">ellipse</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    user<span style="color: #66cc66;">-&gt;</span>login <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">arrowhead</span><span style="color: #66cc66;">=</span><span style="color: #993333;">none</span><span style="color: #66cc66;">&#93;</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>&#8230; which gives us:</p>
<p><a href='http://martin.elwin.com/blog/wp-content/uploads/2008/05/2.png'><img src="http://martin.elwin.com/blog/wp-content/uploads/2008/05/2.png" alt="Use Case 2" title="2.png"  class="size-full wp-image-17" /></a></p>
<p>Not too bad. Let&#8217;s add some more use cases:</p>

<div class="wp_syntax"><div class="code"><pre class="dot dot" style="font-family:monospace;">3.<span style="color: #993333;">dot</span>
<span style="color: #000000; font-weight: bold;">digraph</span> G <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066;">rankdir</span><span style="color: #66cc66;">=</span>LR;
    labelloc<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;b&quot;</span>;
    <span style="color: #000066;">peripheries</span><span style="color: #66cc66;">=</span>0;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Actor Nodes */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">node</span> <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">shape</span><span style="color: #66cc66;">=</span><span style="color: #993333;">plaintext</span>, <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #993333;">invis</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">subgraph</span> clusterUser <span style="color: #66cc66;">&#123;</span><span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;User&quot;</span>; user<span style="color: #66cc66;">&#125;</span>;
    user <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">shapefile</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stick.png&quot;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    <span style="color: #000000; font-weight: bold;">subgraph</span> clusterAdmin <span style="color: #66cc66;">&#123;</span><span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Administrator&quot;</span>; admin<span style="color: #66cc66;">&#125;</span>;
    admin <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">shapefile</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stick.png&quot;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Use Case Nodes */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">node</span> <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">shape</span><span style="color: #66cc66;">=</span><span style="color: #993333;">ellipse</span>, <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #993333;">solid</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    log_in <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Log In&quot;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    log_in_pwd <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Log In Password&quot;</span><span style="color: #66cc66;">&#93;</span>;
    log_in_cert <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Log In Certificate&quot;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    manage_user <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Manage User&quot;</span><span style="color: #66cc66;">&#93;</span>;
    change_email <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Change Email&quot;</span><span style="color: #66cc66;">&#93;</span>;
    change_pwd <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Change Password&quot;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">/* Edges */</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">edge</span>  <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">arrowhead</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;oarrow&quot;</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    admin<span style="color: #66cc66;">-&gt;</span>user;
&nbsp;
    <span style="color: #000000; font-weight: bold;">edge</span> <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">arrowhead</span><span style="color: #66cc66;">=</span><span style="color: #993333;">none</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    user<span style="color: #66cc66;">-&gt;</span>log_in;
    admin<span style="color: #66cc66;">-&gt;</span>manage_user;
&nbsp;
    <span style="color: #000000; font-weight: bold;">edge</span> <span style="color: #66cc66;">&#91;</span><span style="color: #000066;">arrowtail</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;vee&quot;</span>, <span style="color: #000066;">label</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;&lt;extend&gt;&gt;&quot;</span>, <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #993333;">dashed</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;
    log_in<span style="color: #66cc66;">-&gt;</span>manage_user;
    log_in<span style="color: #66cc66;">-&gt;</span>log_in_pwd;
    log_in<span style="color: #66cc66;">-&gt;</span>log_in_cert;
&nbsp;
    manage_user<span style="color: #66cc66;">-&gt;</span>change_email;
    manage_user<span style="color: #66cc66;">-&gt;</span>change_pwd;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Run through <code>dot</code> this produces the following image:</p>
<p><a href='http://martin.elwin.com/blog/wp-content/uploads/2008/05/3.png'><img src="http://martin.elwin.com/blog/wp-content/uploads/2008/05/3.png" alt="Use Case 3" title="3.png" class="size-full wp-image-18" /></a></p>
<p>The obvious problem with this is that the nodes aren&#8217;t placed were we really want them to be. Automatic node placement in a directed graph is one the strong points of dot, but the default placement doesn&#8217;t really correspond to how we want our Use Case to look. So, let&#8217;s add some hints to help dot place the 