<?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>Ashish's Tech Blog &#187; Terracotta</title>
	<atom:link href="http://www.ashishpaliwal.com/blog/tag/terracotta/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ashishpaliwal.com/blog</link>
	<description>From Programmer, For Programmers</description>
	<lastBuildDate>Tue, 17 Aug 2010 12:04:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Getting Started with Terracotta Toolkit &#8211; Part 3 (Clustered Map)</title>
		<link>http://www.ashishpaliwal.com/blog/2010/08/getting-started-with-terracotta-toolkit-part-3-clustered-map/</link>
		<comments>http://www.ashishpaliwal.com/blog/2010/08/getting-started-with-terracotta-toolkit-part-3-clustered-map/#comments</comments>
		<pubDate>Tue, 17 Aug 2010 11:40:29 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Terracotta]]></category>
		<category><![CDATA[Clustered Map]]></category>
		<category><![CDATA[Terracotta Toolkit]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=532</guid>
		<description><![CDATA[Did you ever wanted to have a Map which can be clustered across JVM, without doing much work for replicating data, ensuring coherence of data? Terracotta Toolkit allows you to fullfill your dream, Use the same Map API's you have been using for long and see your data being clustered transparently with Terracotta. The fun [...]]]></description>
			<content:encoded><![CDATA[<p>Did you ever wanted to have a Map which can be clustered across JVM, without doing much work for replicating data, ensuring coherence of data? Terracotta Toolkit allows you to fullfill your dream, Use the same Map API's you have been using for long and see your data being clustered transparently with Terracotta. The fun begins, when you add new clients and they have the data with them without any additional programming.</p>
<p>Lets start with some general information about Toolkit. Refer Toolkit javadoc for more details <a href="http://www.terracotta.org/documentation" target="_blank">here</a><br />
<script type="text/javascript">// <![CDATA[
  google_ad_client = "pub-6961884887741817"; /* 468x15, created 8/2/10 */ google_ad_slot = "9956401459"; google_ad_width = 468; google_ad_height = 15;
// ]]&gt;</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script></p>
<h2>Pre-requisite</h2>
<li>Download Terracotta 3.3.0 from <a href="http://www.terracotta.org/dl/" target="_blank">Download page</a></li>
<li>Include following <em>terracotta-toolkit-1.0-runtime-1.0.0.jar</em> into your classpath for using Toolkit. The jar is present inside Terracotta_Install_dir/common folder.</li>
<p>You can spend some time reading the following posts on Toolkit, although they can be read in any order</p>
<ul>
<li><a href="http://www.ashishpaliwal.com/blog/2010/08/getting-started-with-terracotta-toolkit-part-1/" target="_blank">Getting Started with Terracotta Toolkit – Part 1</a></li>
<li><a href="http://www.ashishpaliwal.com/blog/2010/08/getting-started-with-terracotta-toolkit-%E2%80%93-part-2-cluster-events/" target="_blank">Getting Started with Terracotta Toolkit – Part 2 (Cluster Events)</a></li>
</ul>
<p>Lets explore a bit what do we achieve when we say Clustered Map</p>
<div id="attachment_536" class="wp-caption aligncenter" style="width: 414px"><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2010/08/Toolkit3-Map1.png"><img class="size-full wp-image-536" title="Unclustered Map" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2010/08/Toolkit3-Map1.png" alt="Unclustered Map" width="404" height="130" /></a><p class="wp-caption-text">Unclustered Map</p></div>
<p>As you can see in the figure above, we have different JVM's, each having a Map instance used by Application. Now if we were to share the same instance across app's running in different nodes, we would have to write a lot of infrastructure code to replicate data, keep data coherent etc.</p>
<div id="attachment_537" class="wp-caption aligncenter" style="width: 394px"><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2010/08/Toolkit3-Map2.png"><img class="size-full wp-image-537" title="Clustered Map" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2010/08/Toolkit3-Map2.png" alt="Clustered Map" width="384" height="137" /></a><p class="wp-caption-text">Clustered Map</p></div>
<p>The figure above depicts a Clustered Map. The Map instance is visible to App's across JVM's as if they are accessing a local instance. You can achieve this in a few lines, without worrying about the infrastructure code. Lets see how we can achieve this in few simple steps.<br />
<script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x60ImageOnly */
google_ad_slot = "0221726572";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<h4>Steps to creare Clustered Map</h4>
<li>Initialize Toolkit</li>
<li>Get a Map reference</li>
<li>Use the Map</li>
<p><br></p>
<h3>Initializing the Toolkit</h3>
<p>Before any operations can be performed using Toolkit, it needs to be initialized first. The initialization code is very simple, just single line</p>
<pre class="brush: java">
ClusteringToolkit clustering = new TerracottaClient(&quot;localhost:9510&quot;).getToolkit();
</pre>
<p>This line initializes the Terracotta Client and gets an instance of Toolkit to work with. The argument passed to the TerracottaClient is the IP Address and Port of the Terracotta Server.</p>
<p>Have created a simple function for initialization of the Toolkit.</p>
<pre class="brush: java">
ClusteringToolkit clusterToolkit = null;

public void initializeToolKit(String serverAdd) {
clusterToolkit = new TerracottaClient(serverAdd).getToolkit();
}
</pre>
<h3>Get/Create the Map</h3>
<pre class="brush: java">
public void initializeMap() {
    clusteredMap = clusterToolkit.getMap(MAP_NAME);
}
</pre>
<p>The code above creates a Clustered Map. There is no more infrastructure code that you have to write. This gives us a Clustered Map reference. The best part is, there are no new API's to be learned, we continue to use the same old Map API's.</p>
<p>Let's see the data class that we would share across the Map.</p>
<pre class="brush: java">
public class SharedData implements Serializable {

    private String data;

    public SharedData(String data) {
        this.data = data;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}
</pre>
<p>Its a no-brainer class. Just a simple class for demonstration purpose.</p>
<p>Just to keep life simple, we are going to create a function which populates the Map, and the Nodes can then consume the populated data. Consume here would mean just call a get() and print. In real life scenario, you can mutate the data, and the updated data shall be visible across the cluster.</p>
<p>Lets see the populate function</p>
<pre class="brush: java">
protected void populateMap(int objectCount) {
        for(int i = 0; i &lt; objectCount; i++) {
            SharedData data = new SharedData(&quot;&quot;+i);
            clusteredMap.put(i, data);
        }
    }
</pre>
<p>Its a simple function which just puts the data in the Map.</p>
<p>Lets look at the consume function</p>
<pre class="brush: java">
 protected void consumeMapData(int maxObjectId) {
        Range range = new Range(1, maxObjectId);
        while(true) {
            long key = range.getRandomKey();
            byte[] dataBytes = clusteredMap.get((int)key);
            System.out.println(&quot;Key = &quot;+key);
            if(dataBytes == null) {
                System.out.println(&quot;OOPS!.. somethings wrong dude...&quot;);
                return;
            }
            SharedData data = (SharedData)deserializeObject(dataBytes);
            if(data != null) {
                System.out.println(&quot;Data = &quot;+data.getData());
            }
            try {
                // Sleep for 5 secs
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
</pre>
<p>The function just takes a random key and call a get on the map.</p>
<p>To run this implementation, we would have 3 Nodes running, 1 producer and 2 consumer. The picture below gives an idea of the topology.<br />
<div id="attachment_540" class="wp-caption aligncenter" style="width: 411px"><a href="http://www.ashishpaliwal.com/blog/wp-content/uploads/2010/08/Toolkit3-Map3.png"><img src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2010/08/Toolkit3-Map3.png" alt="Runtime Topology" title="Runtime Topology" width="401" height="130" class="size-full wp-image-540" /></a><p class="wp-caption-text">Runtime Topology</p></div></p>
<p><em>To run the implementation</em></p>
<li>Start Terracotta Server</li>
<li>Start Producer</li>
<li>Start one or more Consumer</li>
<p>You can get the complete source of the sample <a href="http://code.google.com/p/terracotta-samples/">here</a></p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x60ImageOnly */
google_ad_slot = "0221726572";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2010/08/getting-started-with-terracotta-toolkit-part-3-clustered-map/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Started with Terracotta Toolkit – Part 2 (Cluster Events)</title>
		<link>http://www.ashishpaliwal.com/blog/2010/08/getting-started-with-terracotta-toolkit-%e2%80%93-part-2-cluster-events/</link>
		<comments>http://www.ashishpaliwal.com/blog/2010/08/getting-started-with-terracotta-toolkit-%e2%80%93-part-2-cluster-events/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 07:52:58 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Terracotta]]></category>
		<category><![CDATA[Terracotta Toolkit]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=513</guid>
		<description><![CDATA[In the last post we saw Terracotta toolkit Queue. In this Part, lets explore Cluster Events. Cluster Events are propagated across the Terracotta Cluster, whenever specific event happens inside the Cluster. In this post we shall explore what are the different types of Events available. Cluster Events As of 3.3.0, following are the four Events [...]]]></description>
			<content:encoded><![CDATA[<p>In the last post we saw Terracotta toolkit Queue. In this Part, lets explore Cluster Events. Cluster Events are propagated across the Terracotta Cluster, whenever specific event happens inside the Cluster. In this post we shall explore what are the different types of Events available.</p>
<h3>Cluster Events</h3>
<p>As of 3.3.0, following are the four Events that are available</p>
<ul>
<li>Node Joined</li>
<li>Node Left</li>
<li>Operations Enabled</li>
<li>Operations Disabled</li>
</ul>
<p>So what do we need to do, in order to receive these events? Just two simple steps</p>
<ul>
<li>Implement ClusterListener</li>
<li>Register the Listener with toolkit</li>
</ul>
<h2>Pre-requisite</h2>
<li>Download Terracotta 3.3.0 from <a href="http://www.terracotta.org/dl/" target="_blank">Download page</a></li>
<li>Include following <em>terracotta-toolkit-1.0-runtime-1.0.0.jar</em> into your classpath for using Toolkit. The jar is present inside Terracotta_Install_dir/common folder.</li>
<h4>Implementing ClusterListener</h4>
<p>Here is the code</p>
<pre class="brush: java">
class MyClusterListener implements ClusterListener {

        public void nodeJoined(ClusterEvent clusterEvent) {
            try {
                System.out.println(&quot;Event Type = &quot;+clusterEvent.getType());
                System.out.println(&quot;Node Joined &quot;+clusterEvent.getNode().getId()
                        + &quot;, IP=&quot;+clusterEvent.getNode().getAddress());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

        public void nodeLeft(ClusterEvent clusterEvent) {
            try {
                System.out.println(&quot;Node Left &quot;+clusterEvent.getNode().getId()
                        + &quot;, IP=&quot;+clusterEvent.getNode().getAddress());
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
        }

        public void operationsEnabled(ClusterEvent clusterEvent) {
            System.out.println(&quot;Operations Enabled&quot;);
        }

        public void operationsDisabled(ClusterEvent clusterEvent) {
            System.out.println(&quot;Operations Disabled&quot;);
        }
    }
</pre>
<p>As you can see, the implementation is pretty simple. Now its time to register the listener</p>
<pre class="brush: java">
ClusterInfo clusterInfo = clusterToolkit.getClusterInfo();
clusterInfo.addClusterListener(new MyClusterListener());
</pre>
<p>Get the complete code <a href=" http://code.google.com/p/terracotta-samples/">here</a></p>
<p>Time to see the implementation in Action</p>
<ul>
<li>Start Terracotta Server</li>
<li>Start the Listener</li>
<li>Start/Stop Terracotta Clients and see the events being received</li>
</ul>
<p>There can be some innovative things that can be done inside the ClusterListener. For ex, you can send SNMP Traps to your management system for nodeJoined or nodeLeft events. </p>
<p>Stay tuned for more on Terracotta Toolkit...</p>
<p>Looking for more details, you find them here</p>
<li><a href="http://www.terracotta.org/documentation/ga/product-documentation">Terracotta Documentation</a></li>
<li><a href="http://www.terracotta.org/documentation/javadocs/terracotta-toolkit/">Terracotta Toolkit Javadoc </a></li>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2010/08/getting-started-with-terracotta-toolkit-%e2%80%93-part-2-cluster-events/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>So you want Distributed, Scalable and Highly Available Cache?</title>
		<link>http://www.ashishpaliwal.com/blog/2010/02/so-you-want-distributed-scalable-and-highly-available-cache/</link>
		<comments>http://www.ashishpaliwal.com/blog/2010/02/so-you-want-distributed-scalable-and-highly-available-cache/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 09:22:53 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Ehcache]]></category>
		<category><![CDATA[Terracotta]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=402</guid>
		<description><![CDATA[Abstract So you want Distributed, Scalable and Highly Available Cache? If yes, then this is the right place for you. The Terracotta Ehcache release has these features inbuilt. The Express Installation mode has simplified Terracotta integration in your application. How this post is organized First we shall start with simple standalone cache and then see [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Abstract</strong></p>
<p><em><strong>So you want Distributed, Scalable and Highly Available Cache?</strong></em> If yes, then this is the right place for you. The Terracotta Ehcache release has these features inbuilt. The <a href="http://www.terracotta.org/documentation/ga/product-documentation-2.html#382351408_pgfId-11318">Express Installation</a> mode has simplified Terracotta integration in your application. </p>
<p><strong>How this post is organized</strong><br />
First we shall start with simple standalone cache and then see how Terracotta can easily help us create a Distributed, Scalable and Highly Available cache, with a few minor configurations</p>
<p><strong>What all do you need run the example?</strong></p>
<ul>
<li>Terracotta 3.2.0 - Download it from <a href="http://www.terracotta.org/dl/">here</a></li>
</ul>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x15LinkUnit */
google_ad_slot = "4400881690";
google_ad_width = 468;
google_ad_height = 15;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>Sample Application</strong><br />
Lets design a sample application (a real simple one), which we shall use to demonstrate the features. Device Monitoring is a common requirement in OSS System, and caching the Device information reduces the Database hits. The essential components of our application are<br />
- DevideInfo - A POJO that stores device information<br />
- CacheHandler - Class that handles cache initialization and other ops</p>
<p>Lets see the CacheHandler class, which is the heart of our implementation</p>
<pre class="brush: java">
public class DeviceInfo  implements Serializable {

    String deviceId;

    String name;

    // .... other device information

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }
}
</pre>
<p>The class is simple and contains information related to the device.</p>
<p>Let see our CacheHandler class</p>
<pre class="brush: java">
public class CacheHandler {

    public Cache deviceCache;

    public void initCache() {
        // Initialize the CacheManager
        CacheManager cacheManager = CacheManager.create();
        // Get the Cache to store device information
        deviceCache = cacheManager.getCache(&quot;deviceCache&quot;);
    }

    public void addToCache(DeviceInfo device) {
        Element el = new Element(device.getDeviceId(), device);
        deviceCache.put(el);
    }

    protected int getDeviceCacheSize() {
        return deviceCache.getSize();
    }

    public static void main(String[] args) {
        CacheHandler handler = new CacheHandler();
        handler.initCache();

        // Lets add 10 devices
        // just to keep life simple
        for(int i = 0; i &lt; 10; i++) {
            DeviceInfo device = new DeviceInfo();
            device.setDeviceId(&quot;Device-&quot;+i);
            handler.addToCache(device);
        }

        // Not recommended in production
        System.out.println(&quot;Cache Size = &quot;+handler.getDeviceCacheSize());

    }
}
</pre>
<p>The class has two main functions<br />
- initCache - The API initializes the cache<br />
- addToCache - The API adds the device information to the device cache</p>
<p>Lets see our ehcache.xml</p>
<pre class="brush: xml">
&lt;ehcache xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:noNamespaceSchemaLocation=&quot;ehcache.xsd&quot;
             updateCheck=&quot;true&quot; monitoring=&quot;autodetect&quot;&gt;

   &lt;defaultCache
            maxElementsInMemory=&quot;10000&quot;
            eternal=&quot;false&quot;
            timeToIdleSeconds=&quot;120&quot;
            timeToLiveSeconds=&quot;120&quot;
            overflowToDisk=&quot;true&quot;
            diskSpoolBufferSizeMB=&quot;30&quot;
            maxElementsOnDisk=&quot;10000000&quot;
            diskPersistent=&quot;false&quot;
            diskExpiryThreadIntervalSeconds=&quot;120&quot;
            memoryStoreEvictionPolicy=&quot;LRU&quot;
            /&gt;

    &lt;cache name=&quot;deviceCache&quot;
           maxElementsInMemory=&quot;1000000&quot;
           maxElementsOnDisk=&quot;1000&quot;
           eternal=&quot;false&quot;
           overflowToDisk=&quot;false&quot;
           diskSpoolBufferSizeMB=&quot;20&quot;
           timeToIdleSeconds=&quot;100&quot;
           timeToLiveSeconds=&quot;100&quot;
           memoryStoreEvictionPolicy=&quot;LFU&quot;&gt;
     &lt;/cache&gt;

&lt;/ehcache&gt;
</pre>
<p>In the configuration we have defined a deviceCache, that we shall use to store device information.</p>
<p>Following is the list of jars you would need to run this app </p>
<ul>
<li>ehcache-core-1.7.2.jar</li>
<li>slf4j-api-1.5.8.jar</li>
<li>slf4j-jdk14-1.5.8.jar</li>
</ul>
<p>All these jars are available as part of Terracotta installation under <TC Install Dir>/distributed-cache directory</p>
<p>You can run this example and see the same in action.</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 468x60ImageOnly */
google_ad_slot = "0221726572";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
<p><strong>Making it Distributed....</strong></p>
<p>Now lets assume that we want to make the Cache fault tolerant, survive JVM crashes, as well as have it available on different JVM's. It should be able to Scale as we add more JVM's to it and other stuff. With Terracotta, we can just do it in a few steps without changing the code. the magic lies with Express Installation mode, introduced in Terracotta version 3.2.0 and above</p>
<p>Lets see the 3 steps that we need to perform</p>
<p>1. Updates to the ehcache.xml<br />
Add following element to ehcache.xml</p>
<p>&lt;terracottaConfig url=&quot;localhost:9510&quot; /&gt;</p>
<p>and for the deviceCache, we add &lt;terracotta /&gt; element</p>
<p>here is the updated ehcache.xml</p>
<pre class="brush: xml">
&lt;ehcache xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:noNamespaceSchemaLocation=&quot;ehcache.xsd&quot;
             updateCheck=&quot;true&quot; monitoring=&quot;autodetect&quot;&gt;

   &lt;terracottaConfig url=&quot;localhost:9510&quot; /&gt; 

   &lt;defaultCache
            maxElementsInMemory=&quot;10000&quot;
            eternal=&quot;false&quot;
            timeToIdleSeconds=&quot;120&quot;
            timeToLiveSeconds=&quot;120&quot;
            overflowToDisk=&quot;true&quot;
            diskSpoolBufferSizeMB=&quot;30&quot;
            maxElementsOnDisk=&quot;10000000&quot;
            diskPersistent=&quot;false&quot;
            diskExpiryThreadIntervalSeconds=&quot;120&quot;
            memoryStoreEvictionPolicy=&quot;LRU&quot;
            /&gt;

    &lt;cache name=&quot;deviceCache&quot;
           maxElementsInMemory=&quot;1000000&quot;
           maxElementsOnDisk=&quot;1000&quot;
           eternal=&quot;false&quot;
           timeToIdleSeconds=&quot;100&quot;
           timeToLiveSeconds=&quot;100&quot;
           memoryStoreEvictionPolicy=&quot;LFU&quot;&gt;

        &lt;terracotta /&gt;
     &lt;/cache&gt;

&lt;/ehcache&gt;
</pre>
<p>There is no need to change the code <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>2. Add ehcache-terracotta-1.8.0.jar to the classpath</p>
<p>Lets start the Terracotta Server</p>
<p>3. Goto Terracotta_Install_dir/bin and execute </p>
<blockquote><p>$ ./start-tc-server.sh</p></blockquote>
<p>This shall start the Terracotta server</p>
<p>Now run the application on multiple JVM's. The device information is available on all client JVM's <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>To monitor Cache, you can use <a href="http://www.terracotta.org/confluence/display/docs/Terracotta+Developer+Console">Terracotta Dev Console</a></p>
<p>If you have further question/queries, please visit <a href="http://forums.terracotta.org/forums/forums/list.page">Terracotta Forums</a>.</p>
<p><strong>What's coming up Next</strong>?<br />
The next post shall touch on write-behind feature in the latest <a href="http://www.terracotta.org/beta/darwin">Terracotta Darwin</a> release. Its essentially to get best of both worlds - Caching and Database Offloading <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  Stay tuned..</p>
<p><script type="text/javascript"><!--
google_ad_client = "pub-6961884887741817";
/* 300x250, created 2/9/10 */
google_ad_slot = "1177095982";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2010/02/so-you-want-distributed-scalable-and-highly-available-cache/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Started with Terracotta</title>
		<link>http://www.ashishpaliwal.com/blog/2009/11/getting-started-with-terracotta/</link>
		<comments>http://www.ashishpaliwal.com/blog/2009/11/getting-started-with-terracotta/#comments</comments>
		<pubDate>Mon, 23 Nov 2009 10:51:40 +0000</pubDate>
		<dc:creator>ashish</dc:creator>
				<category><![CDATA[Application Programming]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[Terracotta]]></category>

		<guid isPermaLink="false">http://www.ashishpaliwal.com/blog/?p=340</guid>
		<description><![CDATA[Abstract In this small post we shall explore Terracotta, a leading pure Java Scalability platform. The discussion is based on the AtomicInteger example from Terracotta site, which shows how to implement a Cluster wide id generator (Actually it’s the Sequencer example, but to keep my steps simple had used AtomicInteger). The reason why I choose [...]]]></description>
			<content:encoded><![CDATA[<h2><strong>Abstract</strong></h2>
<p>In this small post we shall explore Terracotta, a leading pure Java Scalability platform. The discussion is based on the AtomicInteger example from Terracotta site, which shows how to implement a Cluster wide id generator (Actually it’s the Sequencer example, but to keep my steps simple had used AtomicInteger). The reason why I choose this example was coz of a very similar requirement that I had to implement in Clustered J2EE application.<br />
Terracotta is well known and needs no introduction <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<h2><strong>About problem Statement</strong></h2>
<p>Well, I needed a simple solution to have<br />
•	Cluster wide unique id’s<br />
•	Less frequent access to these id’s<br />
•	Optional Persistence</p>
<p><strong>NOTE:</strong> Please note that the current example is slightly modified version of example from terracotta.org.</p>
<h2>Pre-requisite</h2>
<p>To run this example, you need to have following installed<br />
•	Terracotta<br />
•	And JDK offcourse <img src='http://www.ashishpaliwal.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
Let’s take a look at the sample code first</p>
<pre class="brush: java">
import java.util.concurrent.atomic.*;

public class IdGenerator
{

private AtomicInteger masterCounter = new AtomicInteger(0);

private AtomicInteger slaveCounter = new AtomicInteger(1024);

public int getNewMasterId()
{
int newId = masterCounter.getAndIncrement();
System.out.println(&quot;Master Id is: &quot; + newId);
return newId;
}

public int getNewSlaveId()
{
int slaveId = slaveCounter.getAndIncrement();
System.out.println(&quot;Slave Id is: &quot; + slaveId);
return slaveId;
}

public static void main(String[] args)
{
new IdGenerator().getNewMasterId();
}
}
</pre>
<p>The code has two fields ids master and slave, of which master id need to be unique cluster wide. The code is pretty straight forward, and there is nothing to explain. The id’s are generated by simple getAndIncrement() API.</p>
<p>Lets see the tc-config.xml</p>
<pre class="brush: xml">

&lt;tc:tc-config xmlns:tc=&quot;http://www.terracotta.org/config&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://www.terracotta.org/schema/terracotta-4.xsd&quot;&gt;

&lt;application&gt;
&lt;dso&gt;
&lt;roots&gt;
&lt;root&gt;
&lt;field-name&gt;IdGenerator.masterCounter&lt;/field-name&gt;
&lt;/root&gt;
&lt;/roots&gt;
&lt;/dso&gt;
&lt;/application&gt;
&lt;/tc:tc-config&gt;
</pre>
<p>Since only master Id needed to be unique in this case, hence the configuration</p>
<p>Let’s look at the deployment architecture</p>
<div id="attachment_343" class="wp-caption aligncenter" style="width: 310px"><img class="size-medium wp-image-343" title="nodes" src="http://www.ashishpaliwal.com/blog/wp-content/uploads/2009/11/nodees-300x134.png" alt="Deployment Architecture" width="300" height="134" /><p class="wp-caption-text">Deployment Architecture</p></div>
<p>The application was to run on 3 nodes (3 JVM’s on same machine) and one Terracotta server running.<br />
Before we get into running this application, lets take a look at some useful scripts provided by Terracotta<br />
•	<strong>dso-java (bat|sh)</strong> – startup script that bootstraps Terracotta libraries to your application<br />
•	<strong>start-tc-server (bat|sh)</strong> – Script to start Terracotta Server. It is mandatory to start Terracotta Server before running Clients</p>
<p>Let’s run the application<br />
1.	Compile the application. There is no dependency on TC libraries.<br />
2.	First Start the terracotta Server using script start-tc-server<br />
3.	Run 3 separate JVM with command “dso-java IdGenerator”<br />
That’s it. Our first application is running. If we observe the console output, you can see that we have unique id’s for each invocation.</p>
<h2>References:</h2>
<p>Source code for Terracotta examples - http://svn.terracotta.org/svn/forge/cookbook</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ashishpaliwal.com/blog/2009/11/getting-started-with-terracotta/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
