<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>10K-LOC</title>
	<atom:link href="http://10kloc.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://10kloc.wordpress.com</link>
	<description>Tales from the Coding Nights; Notes to Self</description>
	<lastBuildDate>Mon, 03 Jun 2013 23:53:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='10kloc.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>10K-LOC</title>
		<link>http://10kloc.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://10kloc.wordpress.com/osd.xml" title="10K-LOC" />
	<atom:link rel='hub' href='http://10kloc.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Command Line Flags for the JVM</title>
		<link>http://10kloc.wordpress.com/2013/05/10/command-line-options-in-java-virtual-machine/</link>
		<comments>http://10kloc.wordpress.com/2013/05/10/command-line-options-in-java-virtual-machine/#comments</comments>
		<pubDate>Sat, 11 May 2013 00:33:54 +0000</pubDate>
		<dc:creator>10kloc</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[-X]]></category>
		<category><![CDATA[-XX:]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://10kloc.wordpress.com/?p=1542</guid>
		<description><![CDATA[This post attempts to demystify command-line options of the Java Virtual Machine (JVM). I&#8217;m talking about those strange characters you often have to type when starting Java to run a program. The options are often used to specify environment variables (class<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://10kloc.wordpress.com/2013/05/10/command-line-options-in-java-virtual-machine/">Read more &#8250;</a></div><!-- end of .read-more --><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1542&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This post attempts to demystify <em>command-line </em><strong>options</strong> of the Java Virtual Machine (JVM). I&#8217;m talking about those strange characters you often have to type when starting Java to run a program. The options are often used to specify environment variables (class path), configure  performance characteristics of the JVM (garbage collection frequency), amongst many other things.</p>
<p>For example, a sample of valid command line options are shown below in bold.</p>
<pre><code>java <strong>-Xmx6g</strong> AClass</code>
<code>java <strong>-version</strong></code>
<code></code><code>java <strong>-Xms4g -Xmx6g</strong> SomeClass
</code><code>java <strong>-DSomeVal="foo"</strong> MyProgram
</code><code>java <strong>-DSomeVal="foo" -cp MyProgram.jar -Xmx6g -XX:+UseCompressedOops -XX:+UseG1GC</strong> com.foo.Bar</code><span style="font-family:Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;font-size:13px;line-height:19px;"> </span></pre>
<p>JVM also support many command-line options which allow users to specify:</p>
<ul>
<li><span style="font-size:13px;line-height:19px;">Minimum and Maximum Size of the Heap Memory</span></li>
<li><span style="font-size:13px;line-height:19px;">Type of Garbage Collector</span></li>
<li>Type of JIT Compiler (Client or Server), or,</li>
<li>To display JVM version,</li>
<li>etc.</li>
</ul>
<p>Java Language Specification (JLS) breaks the command-line options into three categories based on their maturity level. The most mature options belong to a category called &#8220;<strong>Standard Options</strong>&#8221; and must be supported by all JVM&#8217;s. Less mature options are called &#8220;<strong>Non-Standard</strong>&#8220;. These are specific to JVM (e.g. HotSpot) and are subject to change between releases. There is also a third category of  options called &#8220;<strong>Developer Options</strong>&#8220;. Developer options are a shade of Non-Standard.</p>
<p>Quick Recap: JVM command-line options are used to specify configuration settings to control execution of the Virtual Machine. These options are broken into three categories as shown below:</p>
<ol>
<li><span style="line-height:12.997159004211px;">Standard Options</span></li>
<li>Non-Standard Options</li>
<li>Developer Options (Experimental)</li>
</ol>
<p>Let&#8217;s look at the categories in detail.</p>
<h2>1. Standard Options</h2>
<p>The Standard Options are regulated by the Java Virtual Machine Specification and must be supported by all implementations of Java Virtual Machines. For example, OpenJDK, HotSpot, etc. Standard Options are stable and do not change between releases. Standard Options begin with a <strong>-</strong> followed by the <em>name</em> of option, e.g. <strong>-</strong><em>version</em></p>
<p>Some standard options are shown below:</p>
<p>-version:  <code>java -version</code></p>
<p>-cp:  <code>java -cp &lt;PATH&gt;</code></p>
<p>-jar:  <code>java -jar MyProgram.jar</code></p>
<p>-Dproperty=value:  <code>java -DSomeVal="foo"</code></p>
<h2>2. Non-Standard Options</h2>
<p>Non-Standard Options always begin with <strong>-X</strong>.  The fact that they are not guaranteed to be supported in all implementations of the JVM or even between versions, did little to hurt their popularity. Non-Standard Options remain popular and are widely used. These options often specify an integer value with a suffix of <em>k</em>,<em>m</em>, or <em>g</em> to specify <em>kilo</em>, <em>mega</em> or <em>giga. </em>To get a list of all Non-Standard Options supported by your JVM, you can invoke the launcher with -X, e.g. `java -X`. Examples:</p>
<p>-Xms:  <code>java -Xms2g </code></p>
<p>-X:  <code>java -X</code></p>
<h2>3. Developer Options</h2>
<p>Developers Options always begin with <strong>-XX</strong>.  They are also Non-Standard. They follow the following format for setting boolean Options: -XX: followed by either <span style="color:#008000;"><strong>+</strong></span> or <span style="color:#800000;"><strong>-</strong></span> to indicate <span style="color:#008000;"><strong>true</strong></span> or <span style="color:#800000;"><strong>false</strong></span>, followed by the name of option.</p>
<p>-XX:+UseCompressedOops (Indicates that the Option UseCompressedOops must be used)</p>
<p><span style="font-size:13px;line-height:19px;">From, </span><a style="font-size:13px;line-height:19px;" href="http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html">Java Documentation</a><span style="font-size:13px;line-height:19px;">:</span></p>
<ul>
<li>
<blockquote><p>Boolean options are turned on with <code>-XX:+&lt;option&gt;</code> and turned off with <code>-XX:-&lt;option&gt;</code>.</p></blockquote>
</li>
<li>
<blockquote><p>Numeric options are set with <code>-XX:&lt;option&gt;=&lt;number&gt;</code>. Numbers can include &#8216;m&#8217; or &#8216;M&#8217; for megabytes, &#8216;k&#8217; or &#8216;K&#8217; for kilobytes, and &#8216;g&#8217; or &#8216;G&#8217; for gigabytes (for example, 32k is the same as 32768).</p></blockquote>
</li>
<li>
<blockquote><p>String options are set with <code>-XX:&lt;option&gt;=&lt;string&gt;</code>, are usually used to specify a file, a path, or a list of commands</p></blockquote>
</li>
</ul>
<p>Examples:</p>
<p><code>java -XX:+PrintCompilation</code></p>
<p><code>java -XX:+ParallelGCThreads=10</code></p>
<p>Click <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html">here</a> for a complete list of Options from Oracle.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/10kloc.wordpress.com/1542/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/10kloc.wordpress.com/1542/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1542&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://10kloc.wordpress.com/2013/05/10/command-line-options-in-java-virtual-machine/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cf62f9525150a638d6a92adb5d122b00?s=96&#38;d=retro&#38;r=G" medium="image">
			<media:title type="html">10kloc</media:title>
		</media:content>
	</item>
		<item>
		<title>Object Naming Conventions in JMX</title>
		<link>http://10kloc.wordpress.com/2013/05/10/object-naming-conventions-in-jmx/</link>
		<comments>http://10kloc.wordpress.com/2013/05/10/object-naming-conventions-in-jmx/#comments</comments>
		<pubDate>Sat, 11 May 2013 00:30:52 +0000</pubDate>
		<dc:creator>10kloc</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[convention]]></category>
		<category><![CDATA[domain]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[jmx]]></category>
		<category><![CDATA[mbean]]></category>
		<category><![CDATA[naming]]></category>

		<guid isPermaLink="false">http://10kloc.wordpress.com/?p=1552</guid>
		<description><![CDATA[Every MBean must have a name, more accurately, an ObjectName. Although, MBean could be named anything, E.g. &#8220;DogBean&#8221; or &#8220;SunnyDay&#8221;, it is important to choose consistent and well defined names to avoid inflicting mental torture on the poor soul who is<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://10kloc.wordpress.com/2013/05/10/object-naming-conventions-in-jmx/">Read more &#8250;</a></div><!-- end of .read-more --><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1552&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Every <a href="http://docs.oracle.com/javase/tutorial/jmx/mbeans/standard.html">MBean</a> must have a name, more accurately, an <a href="http://docs.oracle.com/javase/7/docs/api/javax/management/ObjectName.html">ObjectName</a>. Although, MBean could be named anything, E.g. &#8220;DogBean&#8221; or &#8220;SunnyDay&#8221;, it is important to choose consistent and well defined names to avoid inflicting mental torture on the poor soul who is interacting with your application via JMX.</p>
<p>Fortunately, MBean names follow some standard conventions and names can determine how Clients display MBeans. MBean names look like this:</p>
<p><strong><span style="color:#339966;"><em>domain</em></span>:<span style="color:#808000;"><em>key=property</em></span></strong></p>
<p>Remember this convention. Read the last line carefully. Notice that there are two parts with a <strong>: </strong>separating them. The first part is called domain and the second part is called key-properties-list.</p>
<p>Here&#8217;s an example MBean name with both domain and properties: <span style="color:#339966;">com.somecompany.app</span>:<span style="color:#808000;">type=ThreadPool</span></p>
<h3>Domain Naming Conventions</h3>
<p>The domain could be any arbitrary string, but it cannot contain a : since it is used as a separator. Slash (/) isn&#8217;t allowed as well. If the domain name is not provided, then the MBean shows up under &#8220;<strong>DefaultDomain</strong>&#8220;. As mentioned earlier, domain names should be predictable. According to <a href="http://www.oracle.com/technetwork/java/javase/tech/best-practices-jsp-136021.html#mozTocId434075">Oracle Technet</a>:</p>
<blockquote><p>&#8230;.if you know that there is going to be an MBean representing a certain object, then you should be able to know what its name will be.</p></blockquote>
<p>And then add further:</p>
<blockquote><p>The domain part of an Object Name should start with a Java package name. This prevents collisions between MBeans coming from different subsystems. There might be additional text after the package name. Examples:</p>
<p><tt>com.sun.someapp:type=Whatsit,name=5<br />
com.sun.appserv.Domain1:type=Whatever</tt></p></blockquote>
<h3>Key=Property Naming Conventions</h3>
<p>The property list is optional and is in the following format:</p>
<p><span style="color:#808000;">name=property</span></p>
<p>If you wish to specify multiple properties, separate each of them by comma(,). For example:</p>
<p><span style="color:#339966;">com.somecompany.app</span>:<span style="color:#808000;"><em>type=ThreadPool</em></span><strong>,</strong><span style="color:#808000;"><em>poolname=Parser</em></span><strong>,</strong><span style="color:#808000;"><em>scope=internal</em></span></p>
<p>They Key=Property should be used to uniquely identify MBean Objects, such as, each Object in the same domain may have different properties. For example:</p>
<p>com.somecompany.app:<em>type=ThreadPool,name=Parser</em><em style="font-size:13px;line-height:19px;"><br />
</em></p>
<p>com.somecompany.app:<em>type=<em>ThreadPool,name=Generator</em></em><em></em></p>
<p>That&#8217;s MBean naming convention from  1000 feet. If you want to get more information, please read <a href="http://www.oracle.com/technetwork/java/javase/tech/best-practices-jsp-136021.html#mozTocId434075">here</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/10kloc.wordpress.com/1552/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/10kloc.wordpress.com/1552/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1552&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://10kloc.wordpress.com/2013/05/10/object-naming-conventions-in-jmx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cf62f9525150a638d6a92adb5d122b00?s=96&#38;d=retro&#38;r=G" medium="image">
			<media:title type="html">10kloc</media:title>
		</media:content>
	</item>
		<item>
		<title>Finite State Machine in Java using Executor Framework</title>
		<link>http://10kloc.wordpress.com/2013/03/10/finite-state-machine-in-java-using-executor-framework/</link>
		<comments>http://10kloc.wordpress.com/2013/03/10/finite-state-machine-in-java-using-executor-framework/#comments</comments>
		<pubDate>Mon, 11 Mar 2013 05:02:42 +0000</pubDate>
		<dc:creator>10kloc</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[callable returning callable]]></category>
		<category><![CDATA[executor service]]></category>
		<category><![CDATA[finite state machine]]></category>
		<category><![CDATA[fsm]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[inception]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://10kloc.wordpress.com/?p=1539</guid>
		<description><![CDATA[&#8230;<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1539&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>&#8230;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/10kloc.wordpress.com/1539/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/10kloc.wordpress.com/1539/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1539&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://10kloc.wordpress.com/2013/03/10/finite-state-machine-in-java-using-executor-framework/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cf62f9525150a638d6a92adb5d122b00?s=96&#38;d=retro&#38;r=G" medium="image">
			<media:title type="html">10kloc</media:title>
		</media:content>
	</item>
		<item>
		<title>RuntimeExceptions &#8211; To try-catch or not to catch?</title>
		<link>http://10kloc.wordpress.com/2013/03/09/runtimeexceptions-try-catch-or-not-to-catch/</link>
		<comments>http://10kloc.wordpress.com/2013/03/09/runtimeexceptions-try-catch-or-not-to-catch/#comments</comments>
		<pubDate>Sat, 09 Mar 2013 20:28:56 +0000</pubDate>
		<dc:creator>10kloc</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[runtimeexceptions]]></category>
		<category><![CDATA[when to catch runtime exceptions]]></category>

		<guid isPermaLink="false">http://10kloc.wordpress.com/?p=1511</guid>
		<description><![CDATA[The general consensus in the Java community seems to be that RuntimeExceptions should not be caught. Before we delve into the subject, let us review exceptions in Java. The language supports three kinds of Exceptions to signal &#8220;exceptional conditions&#8221;, all of which<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://10kloc.wordpress.com/2013/03/09/runtimeexceptions-try-catch-or-not-to-catch/">Read more &#8250;</a></div><!-- end of .read-more --><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1511&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The general consensus in the Java community seems to be that <a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html"><code>RuntimeException</code></a>s should not be caught. Before we delve into the subject, let us review exceptions in Java. The language supports three kinds of Exceptions to signal &#8220;exceptional conditions&#8221;, all of which are direct descendants of the <a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html">Throwable</a> class:</p>
<ol>
<li><code><a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/Error.html">Error</a> </code></li>
<li><a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html"><code>Exception</code></a></li>
<li><a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html"><code>RuntimeException</code></a></li>
</ol>
<h2>Error</h2>
<p>These exceptional circumstances are like &#8220;act-of-god&#8221; events. For example, car manufacturers don&#8217;t design cars to prevent damage from tsunamis, asteroids or alien invasions. If an alien invasion happens, there is (usually) nothing anyone can do.</p>
<blockquote><p>These are exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. For example, suppose that an application successfully opens a file for input, but is unable to read the file because of a hardware or system malfunction.</p>
<p>Source: <a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html">The Java Tutorials</a></p></blockquote>
<h2>Exception</h2>
<p>These are most widely known as checked exceptions. Back to our car manufacturer analogy, they are like not wearing passenger seat belts. We don&#8217;t always know our passengers. This can de detected by seat-belt sensors and the guilty passenger could be warned or even buzzed with harmless 12 volts.</p>
<p>According to official documentation:</p>
<blockquote><p>These are exceptional conditions that a well-written application should anticipate and recover from. For example, suppose an application prompts a user for an input file name,  [..] But sometimes the user supplies the name of a nonexistent file, and the constructor throws <code>java.io.FileNotFoundException</code>. A well-written program will catch this exception and notify the user of the mistake, possibly prompting for a corrected file name.</p>
<p>Source: <a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html">The Java Tutorials</a></p></blockquote>
<h2>RuntimeException</h2>
<p>Fitting in our analogy, this is like pumping diesel into a gasoline car. This can be prevented (by being smarter)  in the first place.</p>
<blockquote><p>These are exceptional conditions that are internal to the application, and that the application usually cannot anticipate or recover from. These usually indicate programming bugs, such as logic errors or improper use of an API.</p>
<p>Source: <a href="http://docs.oracle.com/javase/tutorial/essential/exceptions/catchOrDeclare.html">The Java Tutorials</a></p></blockquote>
<h2>Try/Catch</h2>
<p>In Java, exceptional conditions must be caught by <strong>try-catch</strong> block. However, <a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/Error.html"><code>Error</code></a>s and  <a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html"><code>RuntimeException</code></a>s (and their derivatives)  are not subject to this requirement, which means code which throws them, need not be wrapped in a try-catch block. Whereas code that throws checked <code><a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html">Exception</a>s or their derivates, must be wrapped in try-catch block.</code></p>
<h2>Back to the point&#8230;</h2>
<p>Dividing a number by 0 will generate a<strong> run time</strong> exception, <a title="class in java.lang" href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ArithmeticException.html">ArithmeticException</a>, which according to our previous arguments, is not required to be caught. Java official take on the subject of catch <code><a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html">RuntimeException</a>s</code> is:</p>
<blockquote><p>The application can catch this exception, but it probably makes more sense to eliminate the bug that caused the exception to occur.</p></blockquote>
<p>If you follow this official recommendation, you should not catch the <a title="class in java.lang" href="http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/ArithmeticException.html">ArithmeticException</a>, which will be thrown in the denominator == 0.  Instead, before division, we can check if (denominator &gt; 0). If not, we can halt further execution or even dare to throw a exception of our own <b><a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/IllegalArgumentException.html#IllegalArgumentException()">IllegalArgumentException</a></b>.</p>
<p>OK, so far it is looks like we can have a well designed program that checks inputs and can get away by not catching RuntimeExceptions as per Oracle&#8217;s recomendations.</p>
<p>So when is it OK for an application to catch <code><a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html">RuntimeException</a>s</code>? Never?</p>
<p>A while back, I architected a high-performance traffic director with the goal of operating in the proximity of <span style="text-decoration:underline;">10,000 transactions per seconds</span> (TPS). The project had a very high availability criteria and one of the requirement was that it &#8220;must-never-exit&#8221;.</p>
<p>The director performs minimum amount of processing on each transaction before passing it further. Transactions came in two flavours, call them: A and B. We were only interested in transactions of type A. We had a transactions handler to process type A. Naturally, it &#8220;choked&#8221; run time exceptions when we passed in transactions of type B. The solution? Create a function and pass it every single transaction. If it returned true, we continued to further processing. Otherwise, we simply ignored the transaction, and continued onto the next one.</p>
<pre class="brush: java; title: ; notranslate">
boolean checkFormat(Transaction t) {
//return true if the t is of type A. false otherwise.
}
</pre>
<p>This worked well, except&#8230;..</p>
<p>&#8230; the analysis showed that this function returned <span style="text-decoration:underline;">false only once a year.</span> The reason, 99.99999999999999% transactions were of type A. Yet, we were subjecting every single transaction to be checked. This does not sound so bad, but due to the nature of transactions, the only way to differentiate was by doing <span style="text-decoration:underline;">expensive String comparison</span> on various fields.</p>
<p>When this finding was brought to my knowledge, I immediately had the `checkFormat(&#8230;)` function removed and instead let the handler do it&#8217;s course and throw <a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html"><code>RuntimeException</code></a> upon encountering transaction of type, B. When the exception gets thrown once a year, we catch it, log it and move onto the next transaction. The result: improvement in performance, and room to squeeze in additional calculations.</p>
<h2>Summary</h2>
<p>Although, we can get away by not catching <a title="class in java.lang" href="http://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html"><code>RuntimeException</code></a>s most of the time, there are definitely some circumstances which justify catching these types of exceptions.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/10kloc.wordpress.com/1511/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/10kloc.wordpress.com/1511/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1511&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://10kloc.wordpress.com/2013/03/09/runtimeexceptions-try-catch-or-not-to-catch/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cf62f9525150a638d6a92adb5d122b00?s=96&#38;d=retro&#38;r=G" medium="image">
			<media:title type="html">10kloc</media:title>
		</media:content>
	</item>
		<item>
		<title>Java Multithreading Steeplechase: Executors</title>
		<link>http://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-executors/</link>
		<comments>http://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-executors/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 04:40:20 +0000</pubDate>
		<dc:creator>10kloc</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[benefits]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[executor]]></category>
		<category><![CDATA[executorservice]]></category>
		<category><![CDATA[future]]></category>
		<category><![CDATA[futuretask]]></category>
		<category><![CDATA[history]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[thread pool]]></category>

		<guid isPermaLink="false">http://10kloc.wordpress.com/?p=1459</guid>
		<description><![CDATA[Historical Perspective on Tasks &#38; Threads Tasks are activities that perform some action or do calculations. For example a task could calculate prime numbers up to some upper limit. Good tasks do not depend on other tasks: they are independent.<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-executors/">Read more &#8250;</a></div><!-- end of .read-more --><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1459&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h2>Historical Perspective on Tasks &amp; Threads</h2>
<p>Tasks are activities that perform some action or do calculations. For example a task could calculate prime numbers up to some upper limit. Good tasks do not depend on other tasks: they are independent. In this post, when I refer to tasks, I would mean tasks that are independent.</p>
<p>Tasks in Java can be represented by a very simple interface called <em>Runnable</em> that has only one method: <em>run</em>(). The singular function <em>neither returns a value nor can throw checked exceptions</em>.</p>
<pre class="brush: java; title: ; notranslate">
public interface Runnable {
    void run();
}
</pre>
<p>Many new comers to Java presume <em>Thread</em>s to be the primary abstraction for running tasks. This means that a task can be submitted to a thread which then runs the task. In fact, the Thread class has constructors which take a Runnable for execution:</p>
<pre class="brush: java; title: ; notranslate">
Thread(Runnable target)
Thread(Runnable target, String name)
Thread(ThreadGroup group, Runnable target)
...
</pre>
<p>There are obvious benefits in segregating tasks and threads.</p>
<blockquote><p>A Task, defined by implementing Runnables, is submitted to Thread for execution. The Thread doesn&#8217;t know anything about the task and the same thread could run several different tasks.</p></blockquote>
<h2>Enter Executor:</h2>
<p><em>Executor</em> was introduced in Java 1.5 as a clean abstraction for executing tasks. Mantle was passed to <em>Executor</em> from <em>Thread</em>. According to the Java API, an <em>Executor</em>:</p>
<p>&#8220;&#8230; executes submitted <a title="interface in java.lang" href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Runnable.html"><code>Runnable</code></a> tasks.  This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc.&#8221; In essence, Executor is an interface, whose simplicity rivals that of Runnable:</p>
<pre class="brush: java; title: ; notranslate">
public interface Executor {
    void execute(Runnable command);
}
</pre>
<p>The &#8216;very simple&#8217; <em>Executor</em> interface forms basis for a <strong>very powerful asynchronous task execution framework</strong>. It is based on a <strong>Producer-Consumer</strong> pattern: Producers produce tasks and Consumer threads execute these tasks.</p>
<h2>ExecutorService</h2>
<p>There is little chance you will use ever use <em>Executor</em> directly. It is very powerful, yet feature starved interface with a lone method for executing tasks. Fortunately for us, it has a famous child called <em>ExecutorService</em>, which provides <strong>lifecycle support</strong> such as shutdown, <strong>task tracking</strong> and the ability to <strong>retrieve results</strong>.</p>
<h2>Tracking Task Progress via Future</h2>
<p><em>ExecutorService</em> defines a method called `<em>submit(Runnable task)</em>` which returns a `<em>Future</em>` that can be <span style="text-decoration:underline;">used to track task&#8217;s progress and cancel it</span> (if desired). Future is an interface. From its javadocs:</p>
<p>&#8220;A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation. The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready. Cancellation is performed by the cancel method. Additional methods are provided to determine if the task completed normally or was cancelled. Once a computation has completed, the computation cannot be cancelled.&#8221;</p>
<h3>RunnableFuture</h3>
<p>Earlier on, I said that the interface <em>Runnable</em> doesn&#8217;t return a value. <em>Runnable</em> tasks can indicate completion by modifying a shared data structure. <em>RunnableFuture</em> implements both <em>Future</em> and <em>Runnable</em> interfaces. It can be submitted to any method which expects <em>Runnable</em> and the <em>Future</em> allows to access its result.</p>
<p>So far we have only discussed interfaces (<em>Executor</em>, <em>ExecutorService</em> and <em>Future</em>). Before we look into concrete classes, let us consider one very important concept.</p>
<h2>Thread Pool</h2>
<p>A  design pattern: <a href="http://en.wikipedia.org/wiki/Thread_pool_pattern" rel="nofollow">http://en.wikipedia.org/wiki/Thread_pool_pattern</a>. It has a task queue which holds incoming tasks and has a pool of thread which takes tasks from the queue and execute them.</p>
<div class="wp-caption alignnone" style="width: 410px"><img alt="thread pool" src="http://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Thread_pool.svg/400px-Thread_pool.svg.png" width="400" height="207" /><p class="wp-caption-text">A sample thread pool (green boxes) with waiting tasks (blue) and completed tasks (yellow)</p></div>
<p>Benefits of Thread Pools are thread re-use (creating new threads is a significant CPU overhead) and improved responsiveness (there may already be a waiting thread when a task arrives).</p>
<p>Now let us discuss concrete classes.</p>
<h2>AbstractExecutorService</h2>
<p>This is a <a title="Skeletal Implementation" href="http://10kloc.wordpress.com/2012/12/03/abstract-interfaces-the-mystery-revealed/">skeletal implementation</a> for <em>ExecutorService</em>, providing default implementations for some of it&#8217;s methods.</p>
<pre class="brush: java; title: ; notranslate">
public abstract class AbstractExecutorService
implements ExecutorService
</pre>
<h2>ThreadPoolExecutor</h2>
<p>This is an <em>ExecutorService</em> that applies the<strong> Thread Pool pattern</strong> to execute tasks. From its javadocs:</p>
<p>&#8220;An ExecutorService that executes each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods.&#8221; It provides several methods for setting pool and task queue sizes. For more information:</p>
<pre class="brush: java; title: ; notranslate">
public class ThreadPoolExecutor extends AbstractExecutorService
implements Executor, ExecutorService
</pre>
<h2>FutureTask</h2>
<p>Provides an implementation of <em>Future </em>and<em> RunnableFuture</em>. From its javadoc:</p>
<p>&#8220;&#8230;provides a base implementation of <a title="interface in java.util.concurrent" href="http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html"><code>Future</code></a>, with methods to start and cancel a computation, query to see if the computation is complete, and retrieve the result of the computation.&#8221;</p>
<p>Since a <em>FutureTask</em> implements <em>RunnableFuture</em>, you can <em>submit</em> it directly to an <em>ExecutorService</em>.</p>
<h2>Callable:</h2>
<p><em>Callable</em>&#8216;s were introduced in Java 5 as the next version of <em>Runnable</em>. Just like <em>Thread</em> passed mantle to <em>Executor</em> for task execution, <em>Runnable</em> passed mantle to <em>Callable</em> for representing tasks.</p>
<blockquote><p>Callable for used to represent tasks. Unlike Runnable&#8217;s, they can return a value and even throw Exceptions. They even support generics.</p></blockquote>
<h2>Summary:</h2>
<p><em>Executor</em> and <em>ExecutorService</em> form a very powerful framework for asynchronous task execution. <em>Future</em> is a wrapper that provides a way to track a task&#8217;s progress and could be used to cancel it. <em>Callable</em> represents a task and allows the task to return a value and throw exceptions.</p>
<p>So you might ask why do we still have <em>Thread</em>s and <em>Runnable</em>s if we have better choices available, in the form of <em>Executor</em> and <em>Callable</em>. As far as <em>Callable</em> Vs <em>Runnable</em> is concerned, the reason is purely b<strong>ackwards compatibility</strong>. <em>Threads</em> are not languishing in Java. <em>ExecutorService</em> simply provides a cleaner abstraction for executing tasks. They still rely on <em>Thread</em>s to execute these tasks.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/10kloc.wordpress.com/1459/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/10kloc.wordpress.com/1459/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1459&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-executors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cf62f9525150a638d6a92adb5d122b00?s=96&#38;d=retro&#38;r=G" medium="image">
			<media:title type="html">10kloc</media:title>
		</media:content>

		<media:content url="http://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/Thread_pool.svg/400px-Thread_pool.svg.png" medium="image">
			<media:title type="html">thread pool</media:title>
		</media:content>
	</item>
		<item>
		<title>Java Multithreading Steeplechase: Stopping Threads</title>
		<link>http://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-stopping-threads/</link>
		<comments>http://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-stopping-threads/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 01:15:37 +0000</pubDate>
		<dc:creator>10kloc</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[accept]]></category>
		<category><![CDATA[blocking method]]></category>
		<category><![CDATA[interrupt]]></category>
		<category><![CDATA[interruption]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[non-standard cancellation]]></category>
		<category><![CDATA[Socket]]></category>
		<category><![CDATA[stop]]></category>
		<category><![CDATA[stopping threads]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://10kloc.wordpress.com/?p=1111</guid>
		<description><![CDATA[Let us cut to the chase: In Java, there is no way to quickly and reliably stop a thread. Java language designers got drunk once and attempted to support forced thread termination by releasing the following methods: `Thread.stop()`, `Thread.suspend()` and `Thread.resume()`.<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-stopping-threads/">Read more &#8250;</a></div><!-- end of .read-more --><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1111&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Let us cut to the chase: In Java, there is no way to quickly and reliably stop a thread.</p>
<p>Java language designers got drunk once and attempted to support <em>forced</em> thread termination by releasing the following methods: `<span style="color:#808080;"><em>Thread.stop()</em></span>`, `<span style="color:#808080;"><em>Thread.suspend()</em></span>` and `<span style="color:#808080;"><em>Thread.resume()</em></span>`. However, when they become sober, they quickly realized their mistake and deprecated them. Abrupt thread termination is not so straight forward. A running thread, often called by many writers as a light-weight process, has its own stack and is the master of its own destiny (well daemons are). It may own files and sockets. It may hold locks. Termination is not always easy: Unpredictable consequences may arise if the thread is in the middle of writing to a file and is killed before it can finish writing. Or what about the monitor locks held by the thread when it is shot in the head? For more information on why `<em>Thread.stop()</em>` was deprecated, follow this link: <a title="Why Thread.stop() was deprecated" href="http://docs.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html">http://docs.oracle.com/javase/6/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html</a></p>
<p>Anyways, back to the point.</p>
<blockquote><p>In Java, there is no way to quickly and reliably stop a thread.</p></blockquote>
<p>To stop threads in Java, we rely on a <strong>co-operative</strong> mechanism called <strong>Interruption</strong>. The concept is very simple. To stop a thread, all we can do is deliver it a signal, aka <em>interrupt</em> it, requesting that the thread stops itself at the next available opportunity. That&#8217;s all. There is no telling what the receiver thread might do with the signal: it may not even bother to check the signal; or even worse ignore it.</p>
<blockquote><p>Once you start a thread, nothing can (safely) stop it, except the thread itself. At most, the thread could be simply asked &#8211; or interrupted &#8211; to stop itself.</p></blockquote>
<p>Hence in Java, stopping threads is a two step procedure:</p>
<ul>
<li>Sending stop signal to thread &#8211; aka interrupting it</li>
<li>Designing threads to act on interruption</li>
</ul>
<p>A thread in Java could be interrupted by calling `<span style="color:#808080;"><em>Thread.interrupt()</em></span>` method. Threads can check for interruption by calling `<span style="color:#808080;"><em>Thread.isInterrupted()</em></span>` method. A good thread must check for interruption at regular intervals. The following code fragment illustrates this:</p>
<pre class="brush: java; title: ; notranslate">
public static void main(String[] args) throws Exception {

        /**
         * A Thread which is responsive to Interruption.
         */
        class ResponsiveToInterruption extends Thread {
            @Override public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                    System.out.println(&quot;[Interruption Responsive Thread] Alive&quot;);
                }
                System.out.println(&quot;[Interruption Responsive Thread] bye**&quot;);

            }
        }

        /**
         * Thread that is oblivious to Interruption. It does not even check it's
         * interruption status and doesn't even know it was interrupted.
         */
        class ObliviousToInterruption extends Thread {
            @Override public void run() {
                while (true) {
                    System.out.println(&quot;[Interruption Oblivious Thread] Alive&quot;);
                }
                // The statement below will never be reached.
                //System.out.println(&quot;[Interruption Oblivious Thread] bye&quot;);
            }
        }

        Thread theGood = new ResponsiveToInterruption();
        Thread theUgly = new ObliviousToInterruption();

        theGood.start();
        theUgly.start();

        theGood.interrupt(); // The thread will stop itself
        theUgly.interrupt(); // Will do nothing
}
</pre>
<pre class="brush: bash; title: ; notranslate">
[Interruption Oblivious Thread] Alive
[Interruption Responsive Thread] Alive
[Interruption Responsive Thread] Alive
[Interruption Oblivious Thread] Alive
[Interruption Responsive Thread] bye**
[Interruption Oblivious Thread] Alive
[Interruption Oblivious Thread] Alive
[Interruption Oblivious Thread] Alive
[Interruption Oblivious Thread] Alive
....
</pre>
<blockquote><p>A well designed thread checks its interrupt status at regular intervals and take action when interrupted, usually by cleaning and stopping itself.</p></blockquote>
<h2>Blocking Methods and Interruption:</h2>
<p>A thread can check for interruption at regular intervals &#8211; e.g. as a loop condition &#8211; and take action when it is interrupted. Life would have been easy if it weren&#8217;t for those pesky blocking methods: these methods may &#8220;block&#8221; and take a long time to return, effectively delaying the calling thread&#8217;s ability to check for interruption in a timely manner. Methods like `<span style="color:#808080;"><em>Thread.sleep</em></span>()`, `<span style="color:#808080;"><em>BlockingQueue.put</em></span>()`, `<span style="color:#808080;"><em>ServerSocket.accept</em></span>()` are some examples of blocking methods.</p>
<blockquote><p>If the code is waiting on a blocked method, it may not check the interrupt status until the blocking method returns.</p></blockquote>
<p>Blocking methods which support interruption <em>usually</em> throw an Exception when they detect interruption, transferring the control back to the caller. Blocking methods either throw <em>InterruptedException</em> or <em>ClosedByInterruptionException</em> to signal interruption to the caller. Let us consider an example. the code below calls `<em>Thread.sleep()</em>`. When it detects interruption, `<em>Thread.sleep()</em>` throws <em>InterruptedException</em> and the caller exits the loop. All blocking methods which throw <em>InterruptedException</em> also clear the interrupted status. You must either act on interruption when you catch this exception or at the very least, set the interrupted status again to allow the code higher up the stack to act on interruption.</p>
<pre class="brush: java; title: ; notranslate">
   @Override
   public void run() {
        while(true) {
            try {
                Thread.sleep(Long.MAX_VALUE);
            } catch (InterruptedException exit) {
                break; //Break out of the loop; ending thread
            }
        }
    }
</pre>
<blockquote><p>This may sound preposterous, but the code that does nothing on InterruptedException is &#8220;swallowing&#8221; the interruption, denying other code to take action on interruption.</p></blockquote>
<h2>Interruption Oblivious Blocking Methods:</h2>
<p>In the first code example in this post, we have two threads, <em>ResponsiveToInterruption</em> and <em>ObliviousToInterruption</em>. The former checked for interruption regularly &#8211; as loop condition &#8211; whereas the later didn&#8217;t even bother to check. Blocking methods in Java library fall in the same two categories. The Good ones throw Exceptions when they detect interruption whereas the Ugly one&#8217;s don&#8217;t do anything. Blocking methods in the <em>java.net.Socket</em> don&#8217;t respond to Interruption. For example, the thread below cannot be stopped by interruption when it is waiting for clients. When a client is connected, accept() returns Socket allowing the caller to check for interruption:</p>
<pre class="brush: java; title: ; notranslate">
        /**
         * Thread that checks for interruption, but calls a blocking method
         * that doesn't detect Interruptions.
         */
        class InterruptibleShesNot extends Thread {

            @Override
            public void run() {
                while(!Thread.currentThread().isInterrupted()) {
                    try {
                        ServerSocket server = new ServerSocket(8080);
                        Socket client = server.accept(); // This method will not
                                                         // return or 'unblock'
                                                         // until a client connects
                    } catch (IOException ignored) { }
                }

            }

        }
</pre>
<p>So how do you deal with blocking methods that do not respond to Interruption? You will have to think outside the box and find ways to cancel the operation by other means. For example, <em>Socket</em> operations throw <em>SocketException</em> when the underlying socket is <strong>closed </strong>(by `<em>Socket.close()`</em>). The code below takes advantage of this fact and closes the underlying socket, forcing all blocking methods such as <em>ServerSocket.accept()</em> to throw <em>SocketException</em>.</p>
<pre class="brush: java; title: ; notranslate">
package kitchensink;

import java.net.*;
import java.io.*;

/**
 * Demonstrates non-standard thread cancellation.
 *
 * @author umermansoor
 */
public class SocketCancellation {

    /**
     * ServerSocket.accept() doesn't detect or respond to interruption. The
     * class below overrides the interrupt() method to support non-standard
     * cancellation by canceling the underlying ServerSocket forcing the
     * accept() method to throw Exception, on which we act by breaking the while
     * loop.
     *
     * @author umermansoor
     */
    static class CancelleableSocketThread extends Thread {

        private final ServerSocket server;

        public CancelleableSocketThread(int port) throws IOException {
            server = new ServerSocket(port);
        }

        @Override
        public void interrupt() {
            try {
                server.close();
            } catch (IOException ignored) {
            } finally {
                super.interrupt();
            }
        }

        @Override
        public void run() {
            while (true) {
                try {
                    Socket client = server.accept();
                } catch (Exception se) {
                    break;
                }
            }
        }
    }

    /**
     * Main entry point.
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        CancelleableSocketThread cst = new CancelleableSocketThread(8080);
        cst.start();
        Thread.sleep(3000);
        cst.interrupt();
    }
}
</pre>
<h2>Summary:</h2>
<ul>
<li>Threads cannot be stopped externally; they can only be delivered a signal to stop</li>
<li>It is up to the Thread to:<strong> i)</strong> check the interruption flag regularly, and<strong> ii)</strong> to act upon it</li>
<li>Sometimes checking interruption is not possible if the thread is blocked on a blocking method, such as `<em>BlockingQueue.put()</em>`. Luckily, most blocking methods detect interruption and throw <em>InterruptedException</em> or <em>ClosedByInterruptionException</em></li>
<li>To support blocking methods that do not act on interruptions, non-standard cancellation mechanisms must be used, as illustrated in the last example</li>
</ul>
<h2>Extra:</h2>
<p>The thread class also has a method called `<em>interrupted</em>()`. This is what is does: it clears the interrupted status and returns its previous value. Use this method only when you know what you are doing or when you want to clear the interrupt status.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/10kloc.wordpress.com/1111/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/10kloc.wordpress.com/1111/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1111&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://10kloc.wordpress.com/2013/03/03/java-multithreading-steeplechase-stopping-threads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cf62f9525150a638d6a92adb5d122b00?s=96&#38;d=retro&#38;r=G" medium="image">
			<media:title type="html">10kloc</media:title>
		</media:content>
	</item>
		<item>
		<title>Introduction To Apache Hadoop &#8211; HDFS &amp; MapReduce</title>
		<link>http://10kloc.wordpress.com/2013/01/20/an-introduction-to-apache-hadoop-hdfs-mapreduce/</link>
		<comments>http://10kloc.wordpress.com/2013/01/20/an-introduction-to-apache-hadoop-hdfs-mapreduce/#comments</comments>
		<pubDate>Sun, 20 Jan 2013 18:01:03 +0000</pubDate>
		<dc:creator>10kloc</dc:creator>
				<category><![CDATA[Hadoop]]></category>
		<category><![CDATA[apache hadoop]]></category>
		<category><![CDATA[cloud data processing]]></category>
		<category><![CDATA[distributed batch processing]]></category>
		<category><![CDATA[hadoop]]></category>

		<guid isPermaLink="false">http://10kloc.wordpress.com/?p=1046</guid>
		<description><![CDATA[Let&#8217;s get something out of the way quickly: Hadoop is NOT a database. It is NOT a library. In reality, there is NO single product called Hadoop. Hadoop is made up of stand-alone modules such as a distributed file system<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://10kloc.wordpress.com/2013/01/20/an-introduction-to-apache-hadoop-hdfs-mapreduce/">Read more &#8250;</a></div><!-- end of .read-more --><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1046&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Let&#8217;s get something out of the way quickly: Hadoop is NOT a database. It is NOT a library. In reality, there is NO single product called Hadoop. Hadoop is made up of <em>stand-alone</em> modules such as a distributed file system called HDFS, a distributed database named HBASE, a library for parallel processing of large distributed datasets called MapReduce, and the list goes on. An analogy would be Microsoft Office. There is no application called Microsoft Office. It&#8217;s the name given to a suite of desktop applications like Word, Excel, etc.</p>
<p>In this post we will focus on the Hadoop Distributed File System (HDFS) and MapReduce. These two are core Hadoop modules and are widely used. Together (HDFS + MapReduce) form a very powerful framework for distributed batch processing &#8211; I want you to remember this statement by heart.</p>
<blockquote><p>HDFS and MapReduce form a framework for distributed batch processing.</p></blockquote>
<p>Almost anything you can accomplish with Hadoop&#8217;s modules  (HDFS + MapReduce) could be done with built-in Linux utilities like <em>grep</em>, <em>awk</em>, <em>bash</em>, etc. Hadoop excels in large scale, distributed processing of data, where data to be processed is distributed on hundreds of nodes. With the advent of Cloud Computing, this is quickly becoming the norm. Distributed servers running on multiple nodes producing decentralized logs make it difficult to analyze data in one central place. Consider <em>Google </em>- it runs on thousands of web servers in multiple data centers around the world. Each web server generates a log file which is stored on its local disk, ending up with thousands of log files stored on as many servers. Analytics program should be able to view these dispersed logs as a single logical unit. For example, the following hypothetical queries require processing every single log file to find the total for that server and then add up results from all servers to get the final aggregate sum:</p>
<ul>
<li><span style="line-height:13px;">Number of unique users between 12:00-1:00am.</span></li>
<li>Number of users in a day from Chile.</li>
</ul>
<blockquote><p>Hadoop&#8217;s true power lies in its ability to scale to hundreds or thousands of nodes to distribute large amounts of work across a set of machines to be performed in parallel.</p></blockquote>
<p><span style="font-size:1.5em;">HDFS</span></p>
<p>Hadoop modules are built upon a Distributed File System appropriately named Hadoop Distributed File System (HDFS). The most famous `distributed` file system in existence today is the Network File System or NFS. HDFS is different from NFS on many levels, especially with regards to scalability.</p>
<blockquote><p>Note: The design of HDFS is based on Google File System (GFS) described in <a href="http://research.google.com/archive/gfs.html">this</a> paper.</p></blockquote>
<p>HDFS is based on a master/slave architecture and the design requires that a <strong>single master node</strong> keeps track of all the files in the file system. This is called <strong>NameNode</strong>. A NameNode stores only the meta-data about the files present on the file system: it doesn&#8217;t store the actual data. The data is stored on <strong>DataNodes</strong>. Files are stored on the HDFS in <strong>blocks which are typically 64 MB in size</strong>.</p>
<p><em>NameNode versus DataNode:</em> A NameNode manages the HDFS&#8217;s namespace and regulates access to files and directories. It distributes data blocks to DataNodes and stores this mapping information. DataNodes are responsible for storing data and serving read and write requests from clients.</p>
<p>Let us consider an example, suppose we are storing a 131 MB file on the HDFS. The file will be stored in three blocks on the HDFS  (64 + 64 + 3). NameNode will distribute the three blocks to DataNodes and keep track of the mapping. To read a file stored on the HDFS, the client must have HDFS installed. The client HDFS will obtain the file information from <strong>NameNode</strong> such as the number of file blocks and their location and then<strong> download these blocks directly from DataNodes.</strong></p>
<p>Fore more information on the HDFS, I recommend the following link: http://hadoop.apache.org/docs/hdfs/current/hdfs_design.html</p>
<h2>MapReduce</h2>
<p>MapReduce (MR) is a framework or a library for writing applications to process large amounts of distributed data in parallel. Like HDFS, it&#8217;s architecture is also based on master/slave model. The master is a special node which coordinates activity between several worker nodes.</p>
<p>Here&#8217;s how it works: The master receives the input data which is to be processed. The input data is split into smaller chunks and all these chunks are distributed to and processed in parallel on multiple worker nodes. This is called the <strong>Map</strong> Phase. The workers send their results back to the master node which aggregates these results to produce the sum total. This is called the <strong>Reduce</strong> phase.</p>
<p>Note: I&#8217;ve oversimplified the inner workings of MapReduce. The Map phase output is written to the local disk of workers which is partitioned in as many regions as there are Reduce workers available. This locations is then passed to the master which passes it onto Reduce workers. I recommend <a href="http://research.google.com/archive/mapreduce.html">this</a> paper on MapReduce by Google, which actually introduced it.</p>
<p>MR applications at least must provide the following three input parameters:</p>
<ol>
<li>Location of the input data (e.g. a directory consisting of a single (rare) or multiple input files).</li>
<li>Programming implementations of Map, Reduce functions and their configuration (e.g. a Java JAR file which is distributed to workers)</li>
<li>Location of the output data (e.g. `/tmp/hadoop-output/`)</li>
</ol>
<p>You must remember this always:<strong> All input and output in MR is based on &lt;key, value&gt; pairs</strong>. It is everywhere, the input to the Map function, its output, the input to the Reduce function and its output are all in &lt;key, value&gt; pairs.</p>
<h2>MapReduce Example in Java</h2>
<p>To wrap our minds around MapReduce, let us consider an example. Suppose you have just become the Development Lead for a company which specializes in reading seismic data which measure earthquake magnitudes around the world. There are thousands of such sensors deployed around the world recording earthquake data in log files, the following format:</p>
<h5 id="LC2" style="text-align:center;">nc,71920701,1,&#8221;Saturday, January 12, 2013 19:43:18 UTC&#8221;,38.7865,-122.7630,<span style="color:#800000;">1.5</span>,1.10,27,<span style="color:#800000;">&#8220;Northern California&#8221;</span></h5>
<p>Each entry consists of lot of details. The items in <span style="color:#800000;">red</span> are the magnitude of the earthquake and the name of region where the reading was taken, respectively.</p>
<p>There are millions of such log files available. In addition, logs also contain erroneous entries such as when the sensor became faulty and went in an infinite loop dumping thousands of lines a second. The input data is stored on 50 machines and all the log files combined are about 10 Terabytes in size.  Your Director of Software asks you to perform a simple task: <em>for every region where sensors were deployed, find out the highest magnitude of the earthquake recorded</em>.</p>
<p>Now, let&#8217;s think about this for a moment. This tasks sounds rather simple. You could use your trusted linux tools like `grep`, `sort`, or even `awk` to accomplish this if the logfiles were available on a single computer. But they are not &#8211; they are scattered across 50 computers. Processing data on each computer manually and combing results will be too inefficient (for a Lead Developer, that is).</p>
<p>This is the kind of problem where you can use Hadoop. Let us see how you would do it:</p>
<ol>
<li>First you will deploy HDFS on the 50 machines where the input data is stored so that all data could be seen by all machines. Let us say you put all logfiles of data in a folder on HDFS called <em>input/</em>.</li>
<li>Next, you will write a Java application providing implementations of Map &amp; Reduce functions.
<pre class="brush: java; title: ; notranslate">
    /**
     * The `Mapper` function. It receives a line of input from the file,
     * extracts `region name` and `earthquake magnitude` from it, and outputs
     * the `region name` and `magnitude` in &lt;key, value&gt; manner.
     * @param key - The line offset in the file - ignored.
     * @param value - This is the line itself.
     * @param context - Provides access to the OutputCollector and Reporter.
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public void map(LongWritable key, Text value, Context context) throws
            IOException, InterruptedException {

        String[] line = value.toString().split(&quot;,&quot;, 12);

        // Ignore invalid lines
        if (line.length != 12) {
            System.out.println(&quot;- &quot; + line.length);
            return;
        }

        // The output `key` is the name of the region
        String outputKey = line[11];

        // The output `value` is the magnitude of the earthquake
        double outputValue = Double.parseDouble(line[8]);

        // Record the output in the Context object
        context.write(new Text(outputKey), new DoubleWritable(outputValue));
    }
</pre>
<pre class="brush: java; title: ; notranslate">
    /**
     * The `Reducer` function. Iterates through all earthquake magnitudes for a
     * region to find the maximum value. The output is the `region name` and the
     * `maximum value of the magnitude`.
     * @param key - The name of the region
     * @param values - Iterator over earthquake magnitudes in the region
     * @param context - Used for collecting output
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public void reduce(Text key, Iterable values,
            Context context) throws IOException, InterruptedException {

        // Standard algorithm for finding the max value
        double maxMagnitude = Double.MIN_VALUE;
        for (DoubleWritable value : values) {
            maxMagnitude = Math.max(maxMagnitude, value.get());
        }

        context.write(key, new DoubleWritable(maxMagnitude));
    }
</pre>
</li>
<li>Next you will configure MapReduce to be run processing on all 50 computers. This achieves <strong>data locality</strong> &#8211; the logfiles are processed on the same computer where they are located and only the results are sent back to be reduced, saving bandwidth.</li>
<li>Run `hadoop` passing it the location of the input folder on the HDFS <em>(input/</em>), MapReduce program, and the location where the output is to be produced (<em>output/</em>). And that&#8217;s all you need to do.</li>
</ol>
<h2>Example Project on GitHub</h2>
<p>You can find the complete source code for the above example on <a href="https://github.com/umermansoor/hadoop-java-example">GitHub repository</a> I have created.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/10kloc.wordpress.com/1046/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/10kloc.wordpress.com/1046/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=1046&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://10kloc.wordpress.com/2013/01/20/an-introduction-to-apache-hadoop-hdfs-mapreduce/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cf62f9525150a638d6a92adb5d122b00?s=96&#38;d=retro&#38;r=G" medium="image">
			<media:title type="html">10kloc</media:title>
		</media:content>
	</item>
		<item>
		<title>3 Effective Techniques For Software Versioning</title>
		<link>http://10kloc.wordpress.com/2013/01/05/3-effective-techniques-for-software-versioning/</link>
		<comments>http://10kloc.wordpress.com/2013/01/05/3-effective-techniques-for-software-versioning/#comments</comments>
		<pubDate>Sat, 05 Jan 2013 07:08:12 +0000</pubDate>
		<dc:creator>10kloc</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java version class]]></category>
		<category><![CDATA[semantic versioning]]></category>
		<category><![CDATA[version numbers]]></category>
		<category><![CDATA[versioning scheme]]></category>
		<category><![CDATA[versioning system]]></category>

		<guid isPermaLink="false">http://10kloc.wordpress.com/?p=761</guid>
		<description><![CDATA[Software version numbers are all over the map. At present, my system reports the following versions of various installed software: 17.0 1.2.167.19 11.0.0 1.5.11 (1635) 1.6.0_37  23.0.1271.101 6.0.0.2968 Mayan Apocalypse (96) 0.6.9 3.7.1 (M20110909-1335) 7.0 (Build 201104080000)   As you<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://10kloc.wordpress.com/2013/01/05/3-effective-techniques-for-software-versioning/">Read more &#8250;</a></div><!-- end of .read-more --><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=761&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Software version numbers are all over the map. At present, my system reports the following versions of various installed software:</p>
<p><span style="color:#800000;"> 17.0</span><br />
<span style="color:#800000;"> 1.2.167.19</span><br />
<span style="color:#800000;"> 11.0.0</span><br />
<span style="color:#800000;"> 1.5.11 (1635)</span><br />
<span style="color:#800000;"> 1.6.0_37 </span><br />
<span style="color:#800000;"> 23.0.1271.101</span><br />
<span style="color:#800000;"> 6.0.0.2968</span><br />
<span style="color:#800000;"> Mayan Apocalypse (96)</span><br />
<span style="color:#800000;"> 0.6.9</span><br />
<span style="color:#800000;"> 3.7.1 (M20110909-1335)</span><br />
<span style="color:#800000;"> 7.0 (Build 201104080000)  </span></p>
<p>As you can see, there is no concrete pattern here. The most common theme seems to be:</p>
<pre>&lt;major&gt;.&lt;minor&gt;.&lt;patch&gt;.&lt;build&gt;</pre>
<p>To be fair, there have been some attempts to standardize version numbers such as <a href="http://semver.org/">Semantic Versioning</a>, <a href="http://apr.apache.org/versioning.html">Apache APR</a>, even the older <a href="http://www.fenestrated.net/~macman/mirrors/Apple%20Technotes%20%28As%20of%202002%29/tn/tn1132.html"><span style="text-decoration:underline;">Apple Standards</span></a>. However,…..</p>
<h2>Not all software is created equal:</h2>
<p>We have a wide variety of software from libraries and frameworks, to complex server software to simple mobile applications. Some software is characterized by a high build rate (e.g. few times a day) whereas on the other end of the spectrum, it’s easy to find cases where a release every few months or even years is common practice. Some software is released to the public, other stays in-house. It is hard to define a single universal scheme that works for all kinds of software. So let us divide software into the following three broad categories:</p>
<ol>
<li>Libraries and Frameworks</li>
<li>Public Software</li>
<li>In-house or Hosted Software</li>
</ol>
<h2>Libraries and Frameworks</h2>
<p>For library components and frameworks, which define their own API, Semantic Versioning (<a href="http://semver.org/">SemVer</a>) looks promising. It requires that the software must define a public API to be able to use it. From their <a href="http://semver.org/">webpage</a>:</p>
<blockquote>
<p style="text-align:left;">“A normal version number MUST take the form X.Y.Z where X, Y, and Z are non-negative integers. X is the major version, Y is the minor version, and Z is the patch version. Each element MUST increase numerically by increments of one. For instance: 1.9.0 -&gt; 1.10.0 -&gt; 1.11.0.”</p>
</blockquote>
<p>Web based frameworks like Express (for Node.js), Ruby On Rails, can benefit from it.</p>
<h2>Public Software</h2>
<p>When it comes to public software, things get complicated. Let’s take a moment to ask yourself why your users would ever use the version information? The only answer that comes to my mind is that’s how they decide if there’s a newer version available for purchase or not. Back in the day when I was using Windows 95 and heard about this fancy new thing called Windows 98, I knew it was time to upgrade.</p>
<p>Here’s the deal: your <b>users don’t really care about cryptic version numbers</b>. The only time detailed version information such as <i>4.1.2222A</i> would come handy is when the user is experiencing issues and is on the phone, the support guy would want to know exactly what version of the software they are using right down to the build number.</p>
<p style="text-align:left;">Therefore, it is a <b>good idea for public software to have a</b> <b>dual versioning scheme</b>: The first version is a user-friendly name that is easy to remember and use in casual discussions, e.g. <i>Lion</i>.  The second is the detailed version to be used in times of crisis, e.g. <i>10.7.5</i> <i>(11G63), </i>such as when browsing the Internet for known vulnerabilities or calling Apple for support.</p>
<p style="text-align:left;">Microsoft seemed to have invented or at least popularized the dual versioning system with Windows 95. Windows 95, the immensely popular successor of Windows 3.1x, was actually called <i>Windows 4.0</i>. Windows 98 was <i>4.1</i>. Apple picked this idea up starting from OS X 10.0 and started naming major releases after cats (Cheetah, Jaguar, Panther, to the latest Mountain Lion). You can apply your creative juices when coming up with a user-friendly versioning scheme and even involve the marketing group in the process.</p>
<p>For detailed versioning scheme, I personally prefer &lt;major&gt;.&lt;minor&gt;.&lt;revision&gt;.&lt;build&gt;. You could use &lt;major&gt;.&lt;minor&gt;.&lt;revision&gt;.&lt;date&gt;. For example:</p>
<p>2.15.45.30098312345<br />
1.0.3 (December 21, 2012)</p>
<p><b>Embedding the date in the release is actually a very good idea</b>. It conveys time the build was generated. Others prefer appending build Ids generated by build management systems such as SVN, Git or Mercurial. At my work, I use the following rules:</p>
<ul>
<li><b>Major:</b> This is usually done at the end of full release cycle; the resulting product is a major upgrade. All stakeholders (Managers, VPs, Marketing Directors) are informed of the major increment and is usually followed by press releases and marketing.</li>
<li><b>Minor:</b> When additional functionality or new features are introduced. Software Development managers or Senior Developers typically approve such increments.</li>
<li><b>Revision:</b> Bug fixes, ad-hoc patches, any minor change. Developers increment this number each time they make a minor change or fix a bug.</li>
<li><b>Build:</b> This is the only component of the version that is automatically generated. There is a python script that generates this using the Git commit SHA every time the software is built.</li>
</ul>
<p>This <a href="http://www.javacodegeeks.com/2012/09/an-unambiguous-software-version-scheme.html">post</a> from Alex Collins explain the rules are incrementing each component:</p>
<blockquote><p>“You zero digits to the right of any you increment, so if you fix a bug and introduce a new feature after version 5.3.6 then the new version is 5.4.0. Unstated digits are assumed to be zero, so 5.4.0 is the same as 5.4.0.0 and 5.4.0.0.0.0.0.0.0…”</p></blockquote>
<p><strong>Bonus:</strong> I have written a <a href="https://github.com/umermansoor/Version.java">Java class</a> to hold Version information. It is available under the MIT license so you can freely use it. .NET users, have <a href="http://msdn.microsoft.com/en-us/library/system.version.aspx">Version</a> class which comes with the framework.</p>
<h2>In-house or Hosted Software</h2>
<p>For high rate builds using automated systems like Jenkins, it is a good idea to have versions automatically time stamped. Something like &lt;year&gt;.&lt;month&gt;.&lt;day&gt;.&lt;time&gt; is good for providing detailed information to support desk staff and for referencing build information to developers. E.g. <i>2012.12.29.0059</i> is the version of build generated on December 29, 2012 at 12:59 a.m.</p>
<h2>Be Consistent</h2>
<p>Whatever you do, please be consistent and stick to the system you pick. Sun Microsystems (acquired by Oracle), confused developers everywhere by changing their versioning scheme along the way.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/10kloc.wordpress.com/761/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/10kloc.wordpress.com/761/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=761&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://10kloc.wordpress.com/2013/01/05/3-effective-techniques-for-software-versioning/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cf62f9525150a638d6a92adb5d122b00?s=96&#38;d=retro&#38;r=G" medium="image">
			<media:title type="html">10kloc</media:title>
		</media:content>
	</item>
		<item>
		<title>Understanding JavaScript Prototypes.</title>
		<link>http://10kloc.wordpress.com/2012/12/30/756/</link>
		<comments>http://10kloc.wordpress.com/2012/12/30/756/#comments</comments>
		<pubDate>Sun, 30 Dec 2012 20:11:40 +0000</pubDate>
		<dc:creator>10kloc</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[__proto__]]></category>

		<guid isPermaLink="false">http://10kloc.wordpress.com/2012/12/30/756/</guid>
		<description><![CDATA[Reblogged from JavaScript, JavaScript...: (en Español) JavaScript's prototype object generates confusion wherever it goes. Seasoned JavaScript professionals, even authors frequently exhibit a limited understanding of the concept. I believe a lot of the trouble stems from our earliest encounters with<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://10kloc.wordpress.com/2012/12/30/756/">Read more &#8250;</a></div><!-- end of .read-more --><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=756&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<div class="reblog-post"><p class="reblog-from"><img alt='' src='http://2.gravatar.com/avatar/52c6174ba60557536f93809b4e95d97c?s=25&amp;d=retro&amp;r=G' class='avatar avatar-25' height='25' width='25' /> <a href="http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/">Reblogged from JavaScript, JavaScript...:</a></p><div class="wpcom-enhanced-excerpt"><div class="wpcom-enhanced-excerpt-content">
<p>(<a href="http://www.programania.net/diseno-de-software/entendiendo-los-prototipos-en-javascript/">en Español</a>)</p>
<p>JavaScript's prototype object generates confusion wherever it goes. Seasoned JavaScript professionals, even authors frequently exhibit a limited understanding of the concept. I believe a lot of the trouble stems from our earliest encounters with prototypes, which almost always relate to <em>new</em>, <em>constructor</em> and the very misleading <em>prototype</em> property attached to functions. In fact prototype is a remarkably simple concept.</p>
</div> <p class="read-more"><a href="http://javascriptweblog.wordpress.com/2010/06/07/understanding-javascript-prototypes/" target="_self"><span>Read more&hellip;</span> 1,340 more words</a></p></div></div><div class="reblogger-note"><div class='reblogger-note-content'>
An excellent post explaining JavaScript's Object creation mechanism via prototype.
</div></div>]]></content:encoded>
			<wfw:commentRss>http://10kloc.wordpress.com/2012/12/30/756/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cf62f9525150a638d6a92adb5d122b00?s=96&#38;d=retro&#38;r=G" medium="image">
			<media:title type="html">10kloc</media:title>
		</media:content>
	</item>
		<item>
		<title>Cassandra Chapter 5: Data Replication Strategies</title>
		<link>http://10kloc.wordpress.com/2012/12/27/cassandra-chapter-5-data-replication-strategies/</link>
		<comments>http://10kloc.wordpress.com/2012/12/27/cassandra-chapter-5-data-replication-strategies/#comments</comments>
		<pubDate>Thu, 27 Dec 2012 19:25:32 +0000</pubDate>
		<dc:creator>10kloc</dc:creator>
				<category><![CDATA[Cassandra]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[apache cassandra]]></category>
		<category><![CDATA[network topology strategy]]></category>
		<category><![CDATA[replication]]></category>
		<category><![CDATA[replication factor]]></category>
		<category><![CDATA[simple strategy]]></category>

		<guid isPermaLink="false">http://10kloc.wordpress.com/?p=621</guid>
		<description><![CDATA[Where as Data Partitioning (discussed in last chapter) is concerned with picking a node to store the first copy of data (row) on, Replication is all about storing additional copies of data on multiple nodes so it can deal with<span class="ellipsis">&#8230;</span><div class="read-more"><a href="http://10kloc.wordpress.com/2012/12/27/cassandra-chapter-5-data-replication-strategies/">Read more &#8250;</a></div><!-- end of .read-more --><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=621&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Where as Data Partitioning (discussed in l<a href="http://10kloc.wordpress.com/2012/12/27/cassandra-chapter-4-data-partitioning/">ast chapter</a>) is concerned with picking a node to store the first copy of data (row) on, Replication is all about storing additional copies of data on multiple nodes so it can deal with node failures without data loss.</p>
<p>In Cassandra terminology, these copies of data or rows are called <strong>replicas</strong>.</p>
<h2>Replication Factor:</h2>
<p>This parameter determine how many nodes in your cluster store copies of data. For example, if Replication Factor (RF) is set to 2, there will be two copies of every data stored on different nodes. As common sense dictates, the RF cannot be greater than the number of nodes in the cluster. You cannot say store 10 replicas of data (RF=10) when you only have 8 nodes available. If you try to do this, your writes will fail.</p>
<h2>Replication Strategies:</h2>
<p>Cassandra has two replication protocols or strategies which I will discuss below. The main difference between the two is whether you have a single data center or your Cassandra cluster spans multiple data centers.</p>
<ul>
<li><span style="line-height:14px;"><strong>Simple Strategy</strong>: As the name indicates, this is the simplest strategy. Data Partitioner protocol (e.g. RandomPartitioner) picks out the first node for the first replica of data. SimpleStrategy places the <strong>second replica on the very next node in the ring</strong>.</span></li>
<li><strong>Network Topology Strategy</strong>: As the name indicates, this strategy is aware of the network topology (location of nodes in racks, data centers etc.) and is much intelligent than Simple Strategy. This strategy is a must if your Cassandra cluster spans multiple data centers and lets you specify<strong> how many replicas you want per data center</strong>. It tries to distribute data among racks to minimize failures. That is, when choosing nodes to store replicas, it will try to find a node on a different rack.Let&#8217;s talk about a few real-world scenarios from my experience: My team and I were designing a Cassandra cluster spanning 4 data centers (we called them sites). Please note that I&#8217;m changing several details about the actual scenario, however, the concepts are the same. Each site also had own application servers. See figure below.<br />
<img class="alignnone size-large wp-image-747" alt="Real World Cassandra Deployment" src="http://10kloc.files.wordpress.com/2012/12/cass-replication.jpg?w=580&#038;h=580" width="580" height="580" /></p>
<p>We had two concerns:<br />
1. Allow local reads in a data center to avoid cross data center latency. For example, the Application Server in Site #1 should read from the Cassandra cluster in the same site.<br />
2. Application should continue to work albeit with limited functionality if connectivity between data centers is disrupted.<br />
3. Tolerate <strong>entire cluster failures</strong> in two data centers and some node failures throughout all clusters in all data centers.</p>
<p>To meet all the requirements, we used NetworkTopologyStrategy with 2 replicas per data center. That is every single row of data will be stored in 8 nodes in total ( 2 nodes per site x 4 sites). Hence we can tolerate failure a single node per site while still allowing local reads. We can also tolerate failure of an entire cluster but will have to incur the extra cost of cross data center latency in the Application server of the site where the cluster failed. For example, if the cluster in Site #2 dies completely, the Application server in that site can have to retrieve data from any of Site # 1, 2 or 3 with extra cost of inter site communication. In this example, we can still have our application working (with decreased performance) even if Cassandra clusters in 3 sites fail completely.</li>
</ul>
<h2>SimpleStrategy Versus NetworkTopologyStrategy &#8211; Use Cases:</h2>
<p>I have read in several posts people suggesting that if you have a single data center, you should use Simple Strategy. However, it&#8217;s not so simple. Simple Strategy is completely oblivious to network topology, including rack configuration. If the replicas happen to be on the same rack, and the rack fail, you will suffer from data loss. Racks are susceptible to failures due to power, heat or network issues. So, I would recommend using Simple Strategy only when your cluster is in a single data center and on a single rack (or you don&#8217;t care about rack configuration).</p>
<h2>How Cassandra knows your Network Topology?</h2>
<p>There is no magic here. Cassandra doesn&#8217;t learn your topology automatically. You must define the topology such as assigning nodes to racks and data centers and give this information to Cassandra which then uses it. This mapping of nodes to racks and data center is done using Snitch, which I will discuss later.</p>
<p>~~~ Ignore below this line ~~~<br />
PlanetCassandra</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/10kloc.wordpress.com/621/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/10kloc.wordpress.com/621/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=10kloc.wordpress.com&#038;blog=42359662&#038;post=621&#038;subd=10kloc&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://10kloc.wordpress.com/2012/12/27/cassandra-chapter-5-data-replication-strategies/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/cf62f9525150a638d6a92adb5d122b00?s=96&#38;d=retro&#38;r=G" medium="image">
			<media:title type="html">10kloc</media:title>
		</media:content>

		<media:content url="http://10kloc.files.wordpress.com/2012/12/cass-replication.jpg?w=580" medium="image">
			<media:title type="html">Real World Cassandra Deployment</media:title>
		</media:content>
	</item>
	</channel>
</rss>
