<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Symphonious &#187; Code and Geek Stuff</title> <atom:link href="http://www.symphonious.net/category/code-and-geek-stuff/feed/" rel="self" type="application/rss+xml" /><link>http://www.symphonious.net</link> <description>Living in a state of accord.</description> <lastBuildDate>Wed, 25 Jan 2012 21:25:34 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Development Mode &#8211; Concatenating Scripts and CSS</title><link>http://www.symphonious.net/2012/01/11/development-mode-concatenating-scripts-and-css/</link> <comments>http://www.symphonious.net/2012/01/11/development-mode-concatenating-scripts-and-css/#comments</comments> <pubDate>Wed, 11 Jan 2012 21:37:30 +0000</pubDate> <dc:creator>Adrian Sutton</dc:creator> <category><![CDATA[Code and Geek Stuff]]></category> <guid
isPermaLink="false">http://www.symphonious.net/?p=1623</guid> <description><![CDATA[HTML 5 Boilerplate reminded me of an old-school tool which is extremely useful for concatenating JS and CSS files on the fly &#8211; server side includes: &#60;FilesMatch &#34;.*\.combined\.(js&#124;css)$&#34;&#62; Options +Includes SetOutputFilter INCLUDES &#60;/FilesMatch&#62; Then you have a main scripts.combined.js (or css) which contains: &#60;!--#include file=&#34;libs/backbone-min.js&#34; --&#62; &#60;!--#include file=&#34;libs/underscore-min.js&#34; --&#62; &#60;!--#include file=&#34;libs/jquery-1.7.1.min.js&#34; --&#62; Plus any other [...]]]></description> <content:encoded><![CDATA[<p> HTML 5 Boilerplate reminded me of an old-school tool which is extremely useful for concatenating JS and CSS files on the fly &#8211; server side includes:</p><pre>
&#60;FilesMatch &quot;.*\.combined\.(js|css)$&quot;&#62;
  Options +Includes
  SetOutputFilter INCLUDES
&#60;/FilesMatch&#62;
</pre><p> Then you have a main scripts.combined.js (or css) which contains:</p><pre>
&#60;!--#include file=&quot;libs/backbone-min.js&quot; --&#62;
&#60;!--#include file=&quot;libs/underscore-min.js&quot; --&#62;
&#60;!--#include file=&quot;libs/jquery-1.7.1.min.js&quot; --&#62;
</pre><p> Plus any other files you need, in the order you specify. This works great for development mode so you can change a file on the fly and just refresh the browser without running any kind of build script. When it comes time to push to production, it’s easy for a build script to process the file ahead of time and be confident that you’ll get exactly the same result.</p>]]></content:encoded> <wfw:commentRss>http://www.symphonious.net/2012/01/11/development-mode-concatenating-scripts-and-css/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Cross Pairing</title><link>http://www.symphonious.net/2012/01/03/cross-pairing/</link> <comments>http://www.symphonious.net/2012/01/03/cross-pairing/#comments</comments> <pubDate>Tue, 03 Jan 2012 21:42:38 +0000</pubDate> <dc:creator>Adrian Sutton</dc:creator> <category><![CDATA[Code and Geek Stuff]]></category> <category><![CDATA[XP]]></category> <guid
isPermaLink="false">http://www.symphonious.net/?p=1613</guid> <description><![CDATA[This evening I stumbled across two interesting posts about alternate layouts for pairing rather than sitting side by side. Firstly Tom Dale talking about Tilde&#8217;s corner desk pairing setup (and some of the software setup they use) and that was inspired by Josh Susser&#8217;s face to face pairing setup at Pivotal Labs. Both approaches require [...]]]></description> <content:encoded><![CDATA[<p> This evening I stumbled across two interesting posts about alternate layouts for pairing rather than sitting side by side.  Firstly <a
href="http://tomdale.net/2012/01/tildes-pairing-setup/">Tom Dale talking about Tilde&#8217;s corner desk pairing setup</a> (and some of the software setup they use) and that was inspired by <a
href="http://pivotallabs.com/users/jsusser/blog/articles/1505-pairing-tete-a-tete">Josh Susser&#8217;s face to face pairing setup at Pivotal Labs</a>.</p><p>Both approaches require more floor space which makes them difficult to setup but I would expect the face to face pairing to be a heck of a lot better if done well. I&#8217;ve always preferred having separate screens in mirror configuration as well as separate keyboards and mice to allow the developers to sit a little further apart to be comfortable and to be able to look straight ahead at the screen. That said, I quite like having a second monitor for spreading out windows as we have at LMAX so it&#8217;s not clear cut which is better.</p><p>It&#8217;s also interesting to note the popularity of the flat screen iMacs as opposed to Mac Pros or laptops. The former being too expensive for extra power and extensibility that generally isn&#8217;t required and the latter often being a bit too individualised to be good as pairing machines. Plus laptops, while amazingly powerful these days still have less bang for the buck and the reduction in performance is just enough to matter for development.</p>]]></content:encoded> <wfw:commentRss>http://www.symphonious.net/2012/01/03/cross-pairing/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Bottlenecks in Programmer Productivity</title><link>http://www.symphonious.net/2012/01/02/bottlenecks-in-programmer-productivity/</link> <comments>http://www.symphonious.net/2012/01/02/bottlenecks-in-programmer-productivity/#comments</comments> <pubDate>Mon, 02 Jan 2012 14:10:53 +0000</pubDate> <dc:creator>Adrian Sutton</dc:creator> <category><![CDATA[Code and Geek Stuff]]></category> <guid
isPermaLink="false">http://www.symphonious.net/?p=1618</guid> <description><![CDATA[Yan Pritzker recently posted “5 ways to get insane productivity boosts for coders” which provides some good tips on how to improve your usage of tools when doing technical work. To summarise: Never look when you can search Don’t repeat yourself (by setting up shortcuts and command line aliases) Learn a scripting language Learn an [...]]]></description> <content:encoded><![CDATA[<p> Yan Pritzker recently posted “<a
href="http://yanpritzker.com/2006/10/03/five-ways-to-get-insane-productivity-boosts/">5 ways to get insane productivity boosts for coders</a>” which provides some good tips on how to improve your usage of tools when doing technical work. To summarise:</p><ul><li> Never look when you can search</li><li> Don’t repeat yourself (by setting up shortcuts and command line aliases)</li><li> Learn a scripting language</li><li> Learn an editor (and use that editor everywhere)</li><li> Learn regular expressions</li></ul><p> However, nearly all of these tips really boil down to how to be more productive at writing text and the mechanics of writing code – editing actual source code files, jumping to different places in those files, executing commands more efficiently etc. Are these really the tasks that consume the vast majority of a developers time?</p><p> While often it feels like they are, after all we spend all day working with a text editor or the command line, are we really spending all that time struggling to keep up with the stream of code our brains are trying to output? Personally, I spend a lot more time thinking about what the best algorithm is, what direction we should be pushing the design in and how to do that and so on. Rarely do I need to type at full speed for extended periods of time.</p><p> Given that, I suspect the real bottlenecks in developer productivity are more along the lines of:</p><ul><li> Comprehending an area of code and its design</li><li> Identifying, evaluating and selecting potential solutions to a problem (be that choice of algorithm, design choices or choice of classes/libraries etc)</li><li> Understanding and sharing a design vision within the team</li><li> Understanding and sharing requirements within the team</li></ul><p> This isn’t an exhaustive list but it should give an idea for the types of things that really limit programmer productivity. In other words, someone who types with two fingers can be much more productive than someone who knows every shortcut in their editor if they are able to identify a simpler solution to the problem.</p><p> For example, recently LMAX needed to make a large and potentially quite risky change to how our system worked. The change affected a core concept in the system and so could have knock-on effects to a wide range of components and be very hard to test effectively. At the start of our two week iteration we already had a potential solution planned out but the developers were uncomfortable with the amount of risk involved with that plan. So we spent a full week discussing options, exploring the code and experimenting, plus talking with business experts to get a better understanding of the domain model. All that discussion led us to find a much simpler solution which, if something was missed, would be sure to cause failures in our existing acceptance tests. Even counting that initial week of discussion, finding the right solution meant we could deliver the change in about half the time we had expected.</p><p> So by all means, spend time learning to use your tools better, it avoids a lot of tedious boring work, but if you really want a step change improvement in productivity, work to improve you communication, design and thinking skills.</p>]]></content:encoded> <wfw:commentRss>http://www.symphonious.net/2012/01/02/bottlenecks-in-programmer-productivity/feed/</wfw:commentRss> <slash:comments>1</slash:comments> </item> <item><title>Simple Long Poll Patterns in JavaScript</title><link>http://www.symphonious.net/2011/11/13/simple-long-poll-patterns-in-javascript/</link> <comments>http://www.symphonious.net/2011/11/13/simple-long-poll-patterns-in-javascript/#comments</comments> <pubDate>Sun, 13 Nov 2011 20:10:11 +0000</pubDate> <dc:creator>Adrian Sutton</dc:creator> <category><![CDATA[Code and Geek Stuff]]></category> <category><![CDATA[JavaScript]]></category> <guid
isPermaLink="false">http://www.symphonious.net/?p=1576</guid> <description><![CDATA[A couple of little patterns for writing long-poll based JavaScript applications have evolved in some internal apps I’ve been working on lately so I thought I’d document them for posterity. Simple Long Poll Loop There’s very little special about this &#8211; it basically just uses jQuery to do a standard long poll. The pattern that [...]]]></description> <content:encoded><![CDATA[<p> A couple of little patterns for writing long-poll based JavaScript applications have evolved in some internal apps I’ve been working on lately so I thought I’d document them for posterity.</p><h2> Simple Long Poll Loop</h2><div
id="gist-1362590" class="gist"><div
class="gist-file"><div
class="gist-data gist-syntax"><div
class="highlight"><pre><div class='line' id='LC1'><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">$</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">longPollUrl</span> <span class="o">=</span> <span class="s1">&#39;/longpoll&#39;</span><span class="p">;</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">lastReceivedSequence</span><span class="p">;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">function</span> <span class="nx">poll</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">(</span><span class="nx">longPollUrl</span><span class="p">,</span> <span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">data</span><span class="o">:</span> <span class="p">{</span> <span class="s1">&#39;lastSequence&#39;</span><span class="o">:</span> <span class="nx">lastReceivedSequence</span> <span class="p">},</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">dataType</span><span class="o">:</span> <span class="s1">&#39;json&#39;</span><span class="p">,</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">timeout</span><span class="o">:</span> <span class="mi">15000</span><span class="p">,</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">success</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">data</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">lastReceivedSequence</span> <span class="o">=</span> <span class="nx">data</span><span class="p">.</span><span class="nx">sequenceNumber</span><span class="p">;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="nb">window</span><span class="p">).</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">&#39;onLongPollEvent&#39;</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">complete</span><span class="o">:</span> <span class="nx">poll</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC19'><span class="p">})(</span><span class="nx">jQuery</span><span class="p">);</span></div></pre></div></div><div
class="gist-meta"> <a
href="https://gist.github.com/raw/1362590/7782cc645aaddefa59ec1502223dc5c59f62e31b/basic.js" style="float:right;">view raw</a> <a
href="https://gist.github.com/1362590#file_basic.js" style="float:right;margin-right:10px;color:#666">basic.js</a> <a
href="https://gist.github.com/1362590">This Gist</a> brought to you by <a
href="http://github.com">GitHub</a>.</div></div></div><p> There’s very little special about this &#8211; it basically just uses jQuery to do a standard long poll.  The pattern that I like is that instead of calling a particular function directly to handle events it just uses the jQuery trigger function to fire an event that can be handled by any interested code, without introducing tight coupling between the two bits of code.  Listeners would receive events with:</p><div
id="gist-1362590" class="gist"><div
class="gist-file"><div
class="gist-data gist-syntax"><div
class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">window</span><span class="p">).</span><span class="nx">bind</span><span class="p">(</span><span class="s1">&#39;onLongPollEvent&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">eve</span><span class="p">,</span> <span class="nx">longPollData</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="p">});</span></div></pre></div></div><div
class="gist-meta"> <a
href="https://gist.github.com/raw/1362590/e43c38b579ee776419bcd57f4c3a2b2ba3475ab9/listener.js" style="float:right;">view raw</a> <a
href="https://gist.github.com/1362590#file_listener.js" style="float:right;margin-right:10px;color:#666">listener.js</a> <a
href="https://gist.github.com/1362590">This Gist</a> brought to you by <a
href="http://github.com">GitHub</a>.</div></div></div><h2> Automatically Reload JavaScript Changes</h2><p> One of the internal tools I’ve been working on is our “big feedback” page for reporting build results. This page is displayed on a large monitor the whole team can see, so refreshing the page to load JavaScript changes requires actually getting up and walking over to it. Fortunately, there’s a simple pattern to get the JavaScript to reload itself:</p><div
id="gist-1362590" class="gist"><div
class="gist-file"><div
class="gist-data gist-syntax"><div
class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="nx">serverVersion</span><span class="p">;</span></div><div class='line' id='LC2'><span class="p">...</span></div><div class='line' id='LC3'><span class="k">if</span> <span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">serverVersion</span> <span class="o">!=</span> <span class="nx">serverVersion</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">reload</span><span class="p">();</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span><span class="p">;</span></div><div class='line' id='LC6'><span class="p">}</span></div><div class='line' id='LC7'><span class="p">...</span></div></pre></div></div><div
class="gist-meta"> <a
href="https://gist.github.com/raw/1362590/3a6e0ffa1e0e78cc24dc0732c4c1dfd4e5a2ae52/reload.js" style="float:right;">view raw</a> <a
href="https://gist.github.com/1362590#file_reload.js" style="float:right;margin-right:10px;color:#666">reload.js</a> <a
href="https://gist.github.com/1362590">This Gist</a> brought to you by <a
href="http://github.com">GitHub</a>.</div></div></div><p> Each long poll event sent down to the browser includes the server version &#8211; when it changes, the JavaScript reloads the page.  There are two nice little details here:</p><ol><li> Deploying a new version actually requires some downtime while the server restarts. Naive implementations will reload the page when the long poll request returns an error but that usually results in the page attempting to reload and also getting an error or no response from the server and now there’s no JavaScript running to recover. Instead, this will wait for the server to come back up and be answering requests again before reloading.</li><li> So I don’t have to remember to update the server version, the server actually sends the UNIX time stamp from when it started up. Anytime the server restarts, that value changes and the JavaScript will reload. This can lead to some false positives but it’s extremely simple and effective. Another option would be for the build scripts to insert the SVN revision but that doesn’t work as well for development.</li></ol><p> Usually I insert this reload code directly into the long poll loop so that it is guaranteed to run first and avoid any potential problems with data formats changing. Even the serverVersion data format can change since it doesn’t try to parse it in any way &#8211; any change, including data.serverVersion no longer being present, will cause the page to reload.</p><p> Put together the code comes out something like:</p><div
id="gist-1362590" class="gist"><div
class="gist-file"><div
class="gist-data gist-syntax"><div
class="highlight"><pre><div class='line' id='LC1'><span class="p">(</span><span class="kd">function</span><span class="p">(</span><span class="nx">$</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">longPollUrl</span> <span class="o">=</span> <span class="s1">&#39;/longpoll&#39;</span><span class="p">;</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">lastReceivedSequence</span><span class="p">;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">serverVersion</span><span class="p">;</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">function</span> <span class="nx">poll</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">.</span><span class="nx">ajax</span><span class="p">(</span><span class="nx">longPollUrl</span><span class="p">,</span> <span class="p">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">data</span><span class="o">:</span> <span class="p">{</span> <span class="s1">&#39;lastSequence&#39;</span><span class="o">:</span> <span class="nx">lastReceivedSequence</span> <span class="p">},</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">dataType</span><span class="o">:</span> <span class="s1">&#39;json&#39;</span><span class="p">,</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">timeout</span><span class="o">:</span> <span class="mi">15000</span><span class="p">,</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">success</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">data</span><span class="p">)</span> <span class="k">return</span><span class="p">;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">lastReceivedSequence</span> <span class="o">=</span> <span class="nx">data</span><span class="p">.</span><span class="nx">sequenceNumber</span><span class="p">;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">serverVersion</span> <span class="o">!=</span> <span class="nx">serverVersion</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">reload</span><span class="p">();</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span><span class="p">;</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="nb">window</span><span class="p">).</span><span class="nx">trigger</span><span class="p">(</span><span class="s1">&#39;onLongPollEvent&#39;</span><span class="p">,</span> <span class="nx">data</span><span class="p">);</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">},</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">complete</span><span class="o">:</span> <span class="nx">poll</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">});</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC24'><span class="p">})(</span><span class="nx">jQuery</span><span class="p">);</span></div></pre></div></div><div
class="gist-meta"> <a
href="https://gist.github.com/raw/1362590/62216c01d6f248c435e699d430da647c9e392c19/full.js" style="float:right;">view raw</a> <a
href="https://gist.github.com/1362590#file_full.js" style="float:right;margin-right:10px;color:#666">full.js</a> <a
href="https://gist.github.com/1362590">This Gist</a> brought to you by <a
href="http://github.com">GitHub</a>.</div></div></div><p> Nothing earth-shatteringly new, but handy little tools to have up your sleeve.</p>]]></content:encoded> <wfw:commentRss>http://www.symphonious.net/2011/11/13/simple-long-poll-patterns-in-javascript/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Background Logging with the Disruptor</title><link>http://www.symphonious.net/2011/09/26/background-logging-with-the-disruptor/</link> <comments>http://www.symphonious.net/2011/09/26/background-logging-with-the-disruptor/#comments</comments> <pubDate>Mon, 26 Sep 2011 20:27:26 +0000</pubDate> <dc:creator>Adrian Sutton</dc:creator> <category><![CDATA[Code and Geek Stuff]]></category> <category><![CDATA[Disruptor]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[LMAX]]></category> <category><![CDATA[Performance]]></category> <guid
isPermaLink="false">http://www.symphonious.net/?p=1568</guid> <description><![CDATA[Peter Lawrey posted an example of using the Exchanger class from core Java to implement a background logging implementation. He briefly compared it to the LMAX disruptor and since someone requested it, I thought it might be interesting to show a similar implementation using the disruptor. Firstly, let’s revisit the very high level differences between [...]]]></description> <content:encoded><![CDATA[<p> <a
href="http://vanillajava.blogspot.com/2011/09/exchange-and-gc-less-java.html">Peter Lawrey posted an example of using the Exchanger class from core Java to implement a background logging implementation</a>. He briefly compared it to the <a
href="http://code.google.com/p/disruptor/">LMAX disruptor</a> and since someone requested it, I thought it might be interesting to show a similar implementation using the disruptor.</p><p> Firstly, let’s revisit the very high level differences between the exchanger and the disruptor. Peter notes:</p><blockquote><p> This approach has similar principles to the <a
href="http://code.google.com/p/disruptor/">Disruptor</a>. No GC using recycled, pre-allocated buffers and lock free operations (The Exchanger not completely lock free and doesn&#39;t busy wait, but it could)</p><p> Two keys difference are:</p><ul><li> there is only one producer/consumer in this case, the disruptor supports multiple consumers.</li><li> this approach re-uses a much smaller buffer efficiently. If you are using ByteBuffer (as I have in the past) an optimal size might be 32 KB. The disruptor library was designed to exploit large amounts of memory on the assumption it is relative cheap and can use medium sized (MBs) to very large buffers (GBs). e.g. it was design for servers with 144 GB. I am sure it works well on much smaller servers. ;)</li></ul></blockquote><p> Actually, there’s nothing about the Disruptor that requires large amounts of memory. If you know that your producers and consumers are going to keep pace with each other well and you don’t have a requirement to replay old events, you can use quite a small ring buffer with the Disruptor. There are a lot of advantages to having a large ring buffer, but it’s by no means a requirement.</p><p> It’s also worth noting that the Disruptor does not require consumers to busy-spin, you can choose to use a blocking wait strategy, or strategies that combine busy-spin and blocking to handle both spikes and lulls in event rates efficiently.</p><p> There is also an important advantage to the Disruptor that wasn’t mentioned: it will process events immediately if the consumer is keeping up. If the consumer falls behind however, it can process events in a batch to catch up. This significantly reduces latency while still handling spikes in load efficiently.</p><h2> The Code</h2><p> First let’s start with the LogEntry class. This is a simple value object that is used as our entries on the ring buffer and passed from the producer thread over to the consumer thread.</p><p> Peter’s Exchanger based implementation &#8211; the use of StringBuilder in the LogEntry class is actually a race condition and not thread safe. Both the publishing side and the consumer side are attempting to modify it and depending on how long it takes the publishing side to write the log message to the StringBuilder, it will potentially be processed and then reset by the consumer side before the publisher is complete. In this implementation I’m instead using a simple String to avoid that problem.</p><div
id="gist-1243275" class="gist"><div
class="gist-file"><div
class="gist-data gist-syntax"><div
class="highlight"><pre><div class='line' id='LC1'><span class="kn">import</span> <span class="nn">com.lmax.disruptor.EventFactory</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kd">class</span> <span class="nc">LogEntry</span></div><div class='line' id='LC4'><span class="o">{</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kd">static</span> <span class="kd">final</span> <span class="n">EventFactory</span><span class="o">&lt;</span><span class="n">LogEntry</span><span class="o">&gt;</span> <span class="n">FACTORY</span> <span class="o">=</span> <span class="k">new</span> <span class="n">EventFactory</span><span class="o">&lt;</span><span class="n">LogEntry</span><span class="o">&gt;()</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">{</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="n">LogEntry</span> <span class="nf">newInstance</span><span class="o">()</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="k">new</span> <span class="nf">LogEntry</span><span class="o">();</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">};</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">long</span> <span class="n">time</span><span class="o">;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">level</span><span class="o">;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">String</span> <span class="n">text</span><span class="o">;</span></div><div class='line' id='LC16'><span class="o">}</span></div><div class='line' id='LC17'><br/></div></pre></div></div><div
class="gist-meta"> <a
href="https://gist.github.com/raw/1243275/025846f5c293bcf6dbe2a20c73bad514e5317ad0/LogEntry.java" style="float:right;">view raw</a> <a
href="https://gist.github.com/1243275#file_log_entry.java" style="float:right;margin-right:10px;color:#666">LogEntry.java</a> <a
href="https://gist.github.com/1243275">This Gist</a> brought to you by <a
href="http://github.com">GitHub</a>.</div></div></div><p> The one Disruptor-specific addition is that we create an EventFactory instance which the Disruptor uses to pre-populate the ring buffer entries.</p><p> Next, let’s look at the BackgroundLogger class that sets up the process and acts as the producer.</p><div
id="gist-1243275" class="gist"><div
class="gist-file"><div
class="gist-data gist-syntax"><div
class="highlight"><pre><div class='line' id='LC1'><span class="kn">import</span> <span class="nn">com.lmax.disruptor.RingBuffer</span><span class="o">;</span></div><div class='line' id='LC2'><span class="kn">import</span> <span class="nn">com.lmax.disruptor.dsl.Disruptor</span><span class="o">;</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="kn">import</span> <span class="nn">java.util.concurrent.ExecutorService</span><span class="o">;</span></div><div class='line' id='LC5'><span class="kn">import</span> <span class="nn">java.util.concurrent.Executors</span><span class="o">;</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">BackgroundLogger</span></div><div class='line' id='LC8'><span class="o">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">int</span> <span class="n">ENTRIES</span> <span class="o">=</span> <span class="mi">64</span><span class="o">;</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">private</span> <span class="kd">final</span> <span class="n">ExecutorService</span> <span class="n">executorService</span><span class="o">;</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">private</span> <span class="kd">final</span> <span class="n">Disruptor</span><span class="o">&lt;</span><span class="n">LogEntry</span><span class="o">&gt;</span> <span class="n">disruptor</span><span class="o">;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">private</span> <span class="kd">final</span> <span class="n">RingBuffer</span><span class="o">&lt;</span><span class="n">LogEntry</span><span class="o">&gt;</span> <span class="n">ringBuffer</span><span class="o">;</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">BackgroundLogger</span><span class="o">()</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">{</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">executorService</span> <span class="o">=</span> <span class="n">Executors</span><span class="o">.</span><span class="na">newCachedThreadPool</span><span class="o">();</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">disruptor</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Disruptor</span><span class="o">&lt;</span><span class="n">LogEntry</span><span class="o">&gt;(</span><span class="n">LogEntry</span><span class="o">.</span><span class="na">FACTORY</span><span class="o">,</span> <span class="n">ENTRIES</span><span class="o">,</span> <span class="n">executorService</span><span class="o">);</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">disruptor</span><span class="o">.</span><span class="na">handleEventsWith</span><span class="o">(</span><span class="k">new</span> <span class="n">LogEntryHandler</span><span class="o">());</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">disruptor</span><span class="o">.</span><span class="na">start</span><span class="o">();</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ringBuffer</span> <span class="o">=</span> <span class="n">disruptor</span><span class="o">.</span><span class="na">getRingBuffer</span><span class="o">();</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC23'><br/></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">log</span><span class="o">(</span><span class="n">String</span> <span class="n">text</span><span class="o">)</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">{</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">final</span> <span class="kt">long</span> <span class="n">sequence</span> <span class="o">=</span> <span class="n">ringBuffer</span><span class="o">.</span><span class="na">next</span><span class="o">();</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">final</span> <span class="n">LogEntry</span> <span class="n">logEntry</span> <span class="o">=</span> <span class="n">ringBuffer</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">sequence</span><span class="o">);</span></div><div class='line' id='LC28'><br/></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">logEntry</span><span class="o">.</span><span class="na">time</span> <span class="o">=</span> <span class="n">System</span><span class="o">.</span><span class="na">currentTimeMillis</span><span class="o">();</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">logEntry</span><span class="o">.</span><span class="na">level</span> <span class="o">=</span> <span class="n">level</span><span class="o">;</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">logEntry</span><span class="o">.</span><span class="na">text</span> <span class="o">=</span> <span class="n">text</span><span class="o">;</span></div><div class='line' id='LC32'><br/></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">ringBuffer</span><span class="o">.</span><span class="na">publish</span><span class="o">(</span><span class="n">sequence</span><span class="o">);</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC35'><br/></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">stop</span><span class="o">()</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">{</span></div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">disruptor</span><span class="o">.</span><span class="na">shutdown</span><span class="o">();</span></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">executorService</span><span class="o">.</span><span class="na">shutdownNow</span><span class="o">();</span></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC41'><span class="o">}</span></div><div class='line' id='LC42'><br/></div><div class='line' id='LC43'><br/></div><div class='line' id='LC44'><br/></div></pre></div></div><div
class="gist-meta"> <a
href="https://gist.github.com/raw/1243275/d0fdee872f13a0a2c75b4b0e1c00355e0216eec6/BackgroundLogger.java" style="float:right;">view raw</a> <a
href="https://gist.github.com/1243275#file_background_logger.java" style="float:right;margin-right:10px;color:#666">BackgroundLogger.java</a> <a
href="https://gist.github.com/1243275">This Gist</a> brought to you by <a
href="http://github.com">GitHub</a>.</div></div></div><p> In the constructor we create an ExecutorService which the Disruptor will use to execute the consumer threads (a single thread in this case), then the disruptor itself. We pass in the LogEntry.FACTORY instance for it to use to create the entries and a size for the ring buffer.</p><p> The log method is our producer method. Note the use of two-phase commit. First claim a slot with the ringBuffer.next() method, then copy our values into that slot’s entry and finally publish the slot, ready for the consumer to process. We could have also used the Disruptor.publish method which can make this simpler for many use cases by rolling the two phase commit into call.</p><p> The producer doesn’t need to do any batching as the Disruptor will do that automatically if the consumer is falling behind, though there are also APIs that allow batching the producer which can improve the performance if it fits into your design (here it’s probably better to publish each log entry as it comes in).</p><p> The stop method uses the new shutdown method on the Disruptor which takes care of waiting until all consumers have processed all available entries for you, <a
href="http://groups.google.com/group/lmax-disruptor/browse_thread/thread/511cadc6383260aa/35e537304fc6439f?lnk=gst&#38;q=stop+disruptor#35e537304fc6439f">though the code for doing it yourself is quite straight-forward</a>. Finally we shut down the executor.</p><p> Note that we don’t need a flush method since the Disruptor is always consuming log events as quickly as the consumer can.</p><p> Last of all, the consumer which is almost entirely implementation logic:</p><div
id="gist-1243275" class="gist"><div
class="gist-file"><div
class="gist-data gist-syntax"><div
class="highlight"><pre><div class='line' id='LC1'><br/></div><div class='line' id='LC2'><span class="kn">import</span> <span class="nn">com.lmax.disruptor.EventHandler</span><span class="o">;</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">LogEntryHandler</span> <span class="kd">implements</span> <span class="n">EventHandler</span><span class="o">&lt;</span><span class="n">LogEntry</span><span class="o">&gt;</span></div><div class='line' id='LC5'><span class="o">{</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="nf">LogEntryHandler</span><span class="o">()</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">{</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">public</span> <span class="kt">void</span> <span class="nf">onEvent</span><span class="o">(</span><span class="kd">final</span> <span class="n">LogEntry</span> <span class="n">logEntry</span><span class="o">,</span> <span class="kd">final</span> <span class="kt">long</span> <span class="n">sequence</span><span class="o">,</span> <span class="kd">final</span> <span class="kt">boolean</span> <span class="n">endOfBatch</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">{</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// Write</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC15'><br/></div><div class='line' id='LC16'><span class="o">}</span></div><div class='line' id='LC17'><br/></div></pre></div></div><div
class="gist-meta"> <a
href="https://gist.github.com/raw/1243275/f353a5c78c4bc6c75121b1870d37608e36935d31/LogEntryHandler.java" style="float:right;">view raw</a> <a
href="https://gist.github.com/1243275#file_log_entry_handler.java" style="float:right;margin-right:10px;color:#666">LogEntryHandler.java</a> <a
href="https://gist.github.com/1243275">This Gist</a> brought to you by <a
href="http://github.com">GitHub</a>.</div></div></div><p> The consumer’s onEvent method is called for each LogEntry put into the Disruptor. The endOfBatch flag can be used as a signal to flush written content to disk, allowing very large buffer sizes to be used causing writes to disk to be batched when the consumer is running behind, yet also ensure that our valuable log messages get to disk as quickly as possible.</p><p> <a
href="https://gist.github.com/1243275">The full code is available as a Gist</a>.</p>]]></content:encoded> <wfw:commentRss>http://www.symphonious.net/2011/09/26/background-logging-with-the-disruptor/feed/</wfw:commentRss> <slash:comments>4</slash:comments> </item> <item><title>OS X Lion: iCal Repeatedly Asks for Google Calendar Password</title><link>http://www.symphonious.net/2011/07/22/os-x-lion-ical-repeatedly-asks-for-google-calendar-password/</link> <comments>http://www.symphonious.net/2011/07/22/os-x-lion-ical-repeatedly-asks-for-google-calendar-password/#comments</comments> <pubDate>Fri, 22 Jul 2011 18:16:36 +0000</pubDate> <dc:creator>Adrian Sutton</dc:creator> <category><![CDATA[Apple]]></category> <guid
isPermaLink="false">http://www.symphonious.net/?p=1555</guid> <description><![CDATA[The one problem I’ve found when upgrading to Lion is that suddenly iCal couldn’t sync to my Google Apps Calendar account &#8211; instead it repeatedly asked for the password. I’m still not really sure what caused this, but my solution was to simply delete both ~/Library/Preferences/*iCal* and ~/Library/Calendars. You really only want to do that [...]]]></description> <content:encoded><![CDATA[<p> The one problem I’ve found when upgrading to Lion is that suddenly iCal couldn’t sync to my Google Apps Calendar account &#8211; instead it repeatedly asked for the password. I’m still not really sure what caused this, but my solution was to simply delete both ~/Library/Preferences/*iCal* and ~/Library/Calendars.  You <em>really</em> only want to do that if you exclusively use Google Calendar. If you have local calendars deleting ~/Library/Calendars will delete them.</p><p> Once I’d done that, I re-enabled the Calendars option for the account in System Preferences, “Mail, Contacts and Calendars”, entered my password one last time and it worked perfectly.</p>]]></content:encoded> <wfw:commentRss>http://www.symphonious.net/2011/07/22/os-x-lion-ical-repeatedly-asks-for-google-calendar-password/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Martin Fowler on the LMAX Architecture</title><link>http://www.symphonious.net/2011/07/12/martin-fowler-on-the-lmax-architecture/</link> <comments>http://www.symphonious.net/2011/07/12/martin-fowler-on-the-lmax-architecture/#comments</comments> <pubDate>Tue, 12 Jul 2011 21:01:05 +0000</pubDate> <dc:creator>Adrian Sutton</dc:creator> <category><![CDATA[Code and Geek Stuff]]></category> <category><![CDATA[Disruptor]]></category> <category><![CDATA[LMAX]]></category> <guid
isPermaLink="false">http://www.symphonious.net/?p=1552</guid> <description><![CDATA[Martin Fowler has posted an excellent overview of the LMAX architecture which helps put the use-cases and key considerations that led to the LMAX disruptor pattern in context. Good to see some focus being put on the programming model that LMAX uses as well. While convention suggests that to get the best performance you have [...]]]></description> <content:encoded><![CDATA[<p> <a
href="http://martinfowler.com/articles/lmax.html">Martin Fowler has posted an excellent overview of the LMAX architecture</a> which helps put the use-cases and key considerations that led to the <a
href="http://code.google.com/p/disruptor/">LMAX disruptor pattern</a> in context.</p><p> Good to see some focus being put on the programming model that LMAX uses as well. While convention suggests that to get the best performance you have to make heavy use of concurrency and multithreading, the measurements LMAX did showed that the cost of contention and communication between threads was too great. As such, the programming model at LMAX is largely single-threaded, achieving the best possible performance while avoiding the complexities of multithreaded programming.</p>]]></content:encoded> <wfw:commentRss>http://www.symphonious.net/2011/07/12/martin-fowler-on-the-lmax-architecture/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>LMAX Disruptor &#8211; High Performance, Low Latency and Simple Too</title><link>http://www.symphonious.net/2011/07/11/lmax-disruptor-high-performance-low-latency-and-simple-too/</link> <comments>http://www.symphonious.net/2011/07/11/lmax-disruptor-high-performance-low-latency-and-simple-too/#comments</comments> <pubDate>Mon, 11 Jul 2011 08:24:45 +0000</pubDate> <dc:creator>Adrian Sutton</dc:creator> <category><![CDATA[Code and Geek Stuff]]></category> <category><![CDATA[Disruptor]]></category> <category><![CDATA[LMAX]]></category> <guid
isPermaLink="false">http://www.symphonious.net/?p=1541</guid> <description><![CDATA[The LMAX disruptor is an ultra-high performance, low-latency message exchange between threads. It&#39;s a bit like a queue on steroids (but quite a lot of steroids) and is one of the key innovations used to make the LMAX exchange run so fast. There is a rapidly growing set of information about what the disruptor is, [...]]]></description> <content:encoded><![CDATA[<p> The <a
href="http://code.google.com/p/disruptor/">LMAX disruptor</a> is an ultra-high performance, low-latency message exchange between threads. It&#39;s a bit like a queue on steroids (but quite a lot of steroids) and is one of the key innovations used to make the <a
href="http://www.lmaxtrader.co.uk/">LMAX exchange</a> run so fast. There is a rapidly growing set of information about what the disruptor is, why it&#39;s important and how it works &#8211; a good place to start is the <a
href="http://code.google.com/p/disruptor/wiki/BlogsAndArticles">list of articles</a> and for the on-going stuff, follow <a
href="http://blogs.lmax.com/">LMAX Blogs</a>. For really detailed stuff, there&#39;s also the <a
href="http://disruptor.googlecode.com/files/Disruptor-1.0.pdf">white paper</a> (PDF).</p><p> While the disruptor pattern is ultimately very simple to work with, setting up multiple consumers with the dependencies between them can require a bit too much boilerplate code for my liking. To make it quick and easy for 99% of cases, I&#39;ve whipped up a <a
href="http://github.com/ajsutton/disruptorWizard">simple DSL for the disruptor pattern</a>. For example, to wire up a &quot;diamond pattern&quot; of consumers:</p><p> <img
src="http://www.symphonious.net/wp-content/uploads/2011/07/1P3C-Diamond.png" alt="Diamond pattern of consumers. C3 depends on both C1 and C2 completing." width="400" height="265" /></p><p> (Image blatantly stolen from <a
href="http://mechanitis.blogspot.com/2011/07/dissecting-disruptor-wiring-up.html">Trisha Gee&#39;s excellent series explaining the disruptor pattern</a>)</p><p> In this scenario, consumers C1 and C2 can process entries as soon as the producer (P1) puts them on the ring buffer (in parallel). However, consumer C3 has to wait for <em>both</em> C1 and C2 to complete before it processes the entries. In real life this might be because we need to both journal the data to disk (C1) and validate the data (C2) before we do the actual business logic (C3).</p><p> With the raw disruptor syntax, these consumers would be created with the following code:</p><pre>
Executor executor = Executors.newCachedThreadPool();
BatchHandler handler1 = new MyBatchHandler1();
BatchHandler handler2 = new MyBatchHandler2();
BatchHandler handler3 = new MyBatchHandler3()
RingBuffer ringBuffer = new RingBuffer(ENTRY_FACTORY, RING_BUFFER_SIZE);
ConsumerBarrier consumerBarrier1 = ringBuffer.createConsumerBarrier();
BatchConsumer consumer1 = new BatchConsumer(consumerBarrier1, handler1);
BatchConsumer consumer2 = new BatchConsumer(consumerBarrier1, handler2);
ConsumerBarrier consumerBarrier2 =
ringBuffer.createConsumerBarrier(consumer1, consumer2);
BatchConsumer consumer3 = new BatchConsumer(consumerBarrier2, handler3);
executor.execute(consumer1);
executor.execute(consumer2);
executor.execute(consumer3);
ProducerBarrier producerBarrier =
ringBuffer.createProducerBarrier(consumer3);
</pre><p> We have to create our actual handlers (the two instances of MyBatchHandler), plus consumer barriers, BatchConsumer instances and actually execute the consumers on their own threads. The DSL can handle pretty much all of that setup work for us with the end result being:</p><pre>
Executor executor = Executors.newCachedThreadPool();
BatchHandler handler1 = new MyBatchHandler1();
BatchHandler handler2 = new MyBatchHandler2();
BatchHandler handler3 = new MyBatchHandler3();
DisruptorWizard dw = new DisruptorWizard(ENTRY_FACTORY, RING_BUFFER_SIZE, executor);
dw.consumeWith(handler1, handler2).then(handler3);
ProducerBarrier producerBarrier = dw.createProducerBarrier();
</pre><p> We can even build parallel chains of consumers in a diamond pattern: <img
title="Disruptor Dependency Chains" width="764" height="378" src="http://www.symphonious.net/wp-content/uploads/2011/07/disruptorChains-300x225.png" alt="Two chains of consumers running in parallel with a final consumer dependent on both." /></p><p> (Thanks to Trish for using her fancy graphics tablet to create a decent version of this image instead of my original finger painting on an iPad&#8230;)</p><pre>
dw.consumeWith(handler1a, handler2a);
dw.after(handler1a).consumeWith(handler1b);
dw.after(handler2a).consumeWith(handler2b);
dw.after(handler1b, handler2b).consumeWith(handler3);
ProducerBarrier producerBarrier = dw.createProducerBarrier();
</pre><p> The DSL is quite new so any feedback on it would be greatly appreciated and of course feel free to <a
href="http://github.com/ajsutton/disruptorWizard">fork it on GitHub</a> and improve it.</p>]]></content:encoded> <wfw:commentRss>http://www.symphonious.net/2011/07/11/lmax-disruptor-high-performance-low-latency-and-simple-too/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>The Single Implementation Fallacy</title><link>http://www.symphonious.net/2011/06/18/the-single-implementation-fallacy/</link> <comments>http://www.symphonious.net/2011/06/18/the-single-implementation-fallacy/#comments</comments> <pubDate>Sat, 18 Jun 2011 17:42:53 +0000</pubDate> <dc:creator>Adrian Sutton</dc:creator> <category><![CDATA[Code and Geek Stuff]]></category> <category><![CDATA[Java]]></category> <guid
isPermaLink="false">http://www.symphonious.net/?p=1536</guid> <description><![CDATA[As my colleague and favorite debating opponent Danny Yates noted: We got into a bit of a debate at work recently. It went a bit like this: “Gah! Why do we have this interface when there is only a single implementation?” (The stock answer to this goes:) “Because we need the interface in order to [...]]]></description> <content:encoded><![CDATA[<p> As my colleague and favorite debating opponent <a
href="http://blog.codeaholics.org/2011/the-single-implementation-paradox/">Danny Yates noted</a>:</p><blockquote><p> We got into a bit of a debate at work recently. It went a bit like this:</p><p> “Gah! Why do we have this interface when there is only a single implementation?”</p><p> (The stock answer to this goes:) “Because we need the interface in order to mock this class in our tests.”</p><p> “Oh no you don’t, you can use the FingleWidget [insert appropriate technology of your mocking framework of choice here - e.g. JMock ClassImposteriser]! I’m smarter than you!”</p><p> “Well, yes, you can. But if you’ve correctly followed Design for Extension principles, you’ve made the class final, right? And you definitely can’t mock that! Hah! I’m smarter than you!”</p><p> “Ah ha! But you could always use the <a
href="http://jdave.org/documentation.html#mocking">JDave Unfinaliser Agent</a>! I’m so smart it hurts!”</p></blockquote><p> I tend to side with Danny that using the unfinaliser agent is a bad idea, but I also have to question the benefit of declaring a class final in the first place. However, let’s first cover why I think <a
href="http://www.symphonious.net/2011/06/02/enterprisey-interfaces/">single implementation interfaces are an “enterprisey” anti-pattern</a> in a little more detail.</p><h2> Why Single Implementation Interfaces Are Evil</h2><h3> Interface Separation or Interface Duplication</h3><p> The main argument people raise in favour of having interfaces for everything, even if there’s only one implementation is that it separates the API from the implementation. However in practice with languages like Java, this is simply not true. The interface has to be entirely duplicated in the implementation and the two are tightly coupled. Take the code:</p><pre>
public interface A {
  String doSomething(int p1, Object p2);
}
public class AImpl implements A {
  public String doSomething(int p1, Object p2) { ... }
}
</pre><p> This is a pretty clear violation of Don’t Repeat Yourself (DRY). The fact that the implementation name is essentially the same as the interface is a clear indication that there’s actually only one concept here. If there had been a vision of multiple implementations that work in different ways the class name would have reflected this (e.g. LinkedList vs ArrayList or FileReader vs StringReader).</p><p> As a general rule, if you can’t think of a good name for your class (or method, variable, etc) you’ve probably broken things down in the wrong way and you should rethink it.</p><h3> Extra Layers == Extra Work</h3><p> The net result of duplicating the API is that each time you want to add or change a method on the interface you have to duplicate that work and add it to the class as well. It’s a small amount of time but distracts from the real task at hand and amounts to a lot of unnecessary “busy work” if you force every class to have a duplicate interface. Plus if you subscribe to the idea of <a
href="http://www.infoq.com/news/2011/05/less-code-is-better">code as inventory</a>, those duplicated method declarations are costing you money.</p><p> <a
href="http://radar.oreilly.com/2011/06/devwir-ios-lawsuits-openoffice-apache-java.html">Also, as James Turner pointed out</a>:</p><blockquote> Unneeded interfaces are not only wasted code, they make reading and debugging the code much more difficult, because they break the link between the call and the implementation.</blockquote><p> This is probably the biggest problem I have with single implementation interfaces. When you’re tracking down a difficult bug you have to load up a lot of stuff into your head all at once – the call stack, variable values, expected control flow vs actual etc etc. Having to make the extra jump through a pointless interface on each call can be the straw that breaks the camel’s back and cause the developer to loose track of the vital context information. It’s doubly bad if you have to jump through a factory as well.</p><h3> Library Code</h3><p> Many people argue that in library code, providing separate interfaces is essential to define the API and ensure nothing leaks out accidentally. This is the one case where I think it makes sense to use an interface as it frees up your internal classes to use method visibility to let classes collaborate “behind the scenes” and have a clean implementation, without that leaking out to the API.</p><p> A word of warning however: one of the fatal mistakes you can make in a Java library is to provide interfaces that you expect the code using the library to implement. Doing this makes it extremely difficult to maintain backwards compatibility – if you ever need to add a method to that interface compatibility is immediately and unavoidably broken. On the other hand, providing an abstract class that you expect to be extended allows new methods to be added more easily since they can often provide a no-op implementation and maintain backwards compatibility. Abstract classes do limit the choices the code using the library can make though so neither option is a clear cut winner.</p><h2> Why Declaring Classes Final is Pointless</h2><p> So at last we come back around to the original problem of needing to mock out classes during testing but being unable to because they’re marked final. There seem to be two main reasons that people like to make classes final:</p><ol><li> Classes should be marked final unless they are explicitly designed for inheritance.</li><li> Marking a class final provides hints to HotSpot that can improve performance either by method inlining or using faster method call dispatch algorithms (direct instead of dynamic).</li></ol><h3> Designing for Extension</h3><p> I have a fair bit of sympathy for the argument that classes should be final unless designed for inheritance, but for shared code within a development team it has a very critical flaw &#8211; it’s trivial to just remove the word final and carry on, so people will. Let’s face it, if you look at a class and think “I can best solve my problem by extending this class” then a silly little keyword which may have just been put their by habit is not going to stop you. You’d need to also provide a clear comment about why the class isn’t suitable for extension but in most cases such a reason doesn’t exist &#8211; extension just hadn’t been thought about yet so the class is inherently not designed for extension. Besides which, if you have the concept of shared code ownership then whoever extends the class is responsible for making any design changes required to make it suitable for extending when they use it as a base class. Most likely though, they have already looked at the class and decided it’s suitable for extension as is which is why they are trying to do just that.</p><p> Perhaps what would be better is to require any class that is designed for extension to have a @DesignedForExtension annotation, then use code analysis tools (like <a
href="http://code.google.com/p/freud/">Freud</a>) to fail the build if a class without that annotation is extended. That makes the default not-extendable which is more likely to be correct and still lets you mock the object for testing. You would however want an IDE plugin to make the error message show up immediately but it does seem like a nice way to get the best of all worlds.</p><h3> Final Classes Go Faster</h3><p> I found myself immediately suspicious of this claim &#8211; it may have been true once but HotSpot is a seriously clever little JIT engine and advances at an amazing pace. Many people claim that HotSpot can inline final methods and it can, but it can also inline non-final methods. That’s right, <a
href="http://java.sun.com/developer/technicalArticles/Networking/HotSpot/inlining.html">it will automatically work out that there is only one possible version of this method that exists and go right ahead and inline it for you</a>.</p><p> There is also a slight variant of this that claims that since dynamic method dispatch is more expensive, marking a method as final means the JVM can avoid the dynamic dispatch for that method. Marking a class final effectively makes all it’s methods final so that every method would get the benefit.</p><p> My reasoning is such that if HotSpot can work out that it can safely inline a method, it clearly has all the information required to avoid the dynamic dispatch as well. I can’t however find any reference to definitively show it does that. Fortunately, I don’t need to. Remember back at the start we said we had to introduce an interface to make things testable? That means changing our code from:</p><pre>
final class A { public void doSomething(); }
…
A a = new A();
a.doSomething();
</pre><p> To:</p><pre>
interface A { void doSomething(); }
final class AImpl implements A { public void doSomething(); }
…
A a = new AImpl();
a.doSomething();
</pre><p> Since we’ve duplicated the method declaration, there is no guarantee that the only version of doSomething is in AImpl, since any class could implement interface A and provide a version of doSomething. We’re right back to relying on HotSpot doing clever tricks to enable method inlining and avoiding dynamic method dispatch.</p><p> There simply can be no performance benefit to declaring a class final if you then refer to it via an interface rather than the concrete class. And if you refer to it as the concrete class you can’t test it.</p><h2> Conclusion</h2><p> There shouldn’t be anything too surprising here – less code is better, simpler architectures work better and never underestimate how clever HotSpot is. Slavishly following the rule of separating the interface from the code doesn’t make code any more testable, it doesn’t reduce coupling between classes (since they still call the same methods) and it does create extra work. So why does everyone keep doing it?</p><p> Oh and, nyah, nyah, I’m so smart it hurts…</p>]]></content:encoded> <wfw:commentRss>http://www.symphonious.net/2011/06/18/the-single-implementation-fallacy/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> <item><title>contentEditable in Mobile WebKit Update</title><link>http://www.symphonious.net/2011/06/10/contenteditable-in-mobile-webkit-update/</link> <comments>http://www.symphonious.net/2011/06/10/contenteditable-in-mobile-webkit-update/#comments</comments> <pubDate>Fri, 10 Jun 2011 18:30:03 +0000</pubDate> <dc:creator>Adrian Sutton</dc:creator> <category><![CDATA[Code and Geek Stuff]]></category> <category><![CDATA[Editors]]></category> <guid
isPermaLink="false">http://www.symphonious.net/?p=1534</guid> <description><![CDATA[Joyously, it looks like iOS 5 fixes the issues with contentEditable not working correctly in Mobile WebKit. A patch has landed in TinyMCE to enable the editor to work there. Great news. Hopefully the changes to WebKit can flow through to Android as well.]]></description> <content:encoded><![CDATA[<p> Joyously, it looks like iOS 5 fixes the <a
href="http://www.symphonious.net/2010/09/13/contenteditable-in-mobile-webkit/">issues with contentEditable not working correctly in Mobile WebKit</a>. A <a
href="https://github.com/tinymce/tinymce/commit/77d9e5577d6ad13bcb0512608359837f31e1e7b2">patch has landed in TinyMCE</a> to enable the editor to work there. Great news.</p><p> Hopefully the changes to WebKit can flow through to Android as well.</p>]]></content:encoded> <wfw:commentRss>http://www.symphonious.net/2011/06/10/contenteditable-in-mobile-webkit-update/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
