| Author |
Message |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 09/29/2008 00:34:27
|
dhruv
journeyman
Joined: 09/29/2008 00:22:08
Messages: 10
Offline
|
Code:
Hi,
i am trying to simulate central caching system using terracotta
but facing few problems please help me out..
when i try to add refrence of ArrayList or Map in DSO object
and then try to update throws exception.
Platform Detail:
>TerraCotta 2.6.2
>JDK 1.5_012
>dso-boot-hotspot_win32_150_12.jar
>Winxp
Codes :
>TerraCottaCache.java
have DSO object "internalCache"
internalCache is a hashmap for central cache
>SingleCacheTest.java
commandline based cache control
which allows adding map,list...
//---------------------- TerraCottaCache.java ----------------------//
/**
*
*/
package com.zycus.utils.tc.cache;
import java.util.HashMap;
import java.util.Map;
/**
* @author dhruv.patel
*
*/
public class TerraCottaCache
{
private HashMap internalCache;
/**
*
*/
public TerraCottaCache()
{
internalCache = new HashMap();
}
/**
*
* <pre>
* </pre>
* @param key
* @param value
*/
public void put(Object key, Object value)
{
internalCache.put(key, value);
}
/**
*
* <pre>
* </pre>
* @param key
* @return Object
*/
public Object get(Object key)
{
return internalCache.get(key);
}
/**
*
* <pre>
* </pre>
* @param key
* @return object
*/
public Object remove(Object key)
{
return internalCache.remove(key);
}
/**
*
* <pre>
* </pre>
*/
public void clear()
{
internalCache.clear();
}
/**
*
* <pre>
* </pre>
* @return grt map
*/
public Map getMap()
{
return internalCache;
}
}
//---------------------- SingleCacheTest.java ----------------------//
/**
*
*/
package com.zycus.utils.tc.cache.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Vector;
import java.util.Map.Entry;
import com.zycus.utils.tc.cache.TerraCottaCache;
/**
* @author dhruv.patel
*
*/
public class SingleCacheTest
{
private TerraCottaCache cache;
/**
*
*/
public SingleCacheTest()
{
cache = new TerraCottaCache();
}
/**
* <pre>
* </pre>
*/
public void addData()
{
Random random = new Random();
for (int index = 0; index < 10; index++)
{
int randomNo = random.nextInt(1000);
cache.put(randomNo + "", randomNo + "");
}
System.out.println("added");
}
/**
* <pre>
* </pre>
*/
public void addList()
{
Vector list = (Vector) cache.get("list");
if (list == null)
{
list = new Vector();
cache.put("list", list);
}
Random random = new Random();
for (int index = 0; index < 10; index++)
{
int randomNo = random.nextInt(1000);
list.add(randomNo + "");
}
System.out.println("list added");
}
/**
* <pre>
* </pre>
*/
public void addMap()
{
Map map = (Map) cache.get("map");
if (map == null)
{
map = Collections.synchronizedMap(new HashMap());
cache.put("map", map);
}
Random random = new Random();
for (int index = 0; index < 10; index++)
{
int randomNo = random.nextInt(1000);
map.put(randomNo + "", randomNo + "");
}
System.out.println("map added");
}
/**
* <pre>
* </pre>
*/
public void clearData()
{
cache.clear();
System.out.println("cleared");
}
/**
* <pre>
* </pre>
*/
public void showData()
{
Map cacheMap = cache.getMap();
Iterator cacheIterator = cacheMap.entrySet().iterator();
Entry entry = null;
System.out.println("-----------------------------");
while (cacheIterator.hasNext())
{
entry = (Entry) cacheIterator.next();
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println("-----------------------------");
}
/**
* <pre>
* </pre>
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
SingleCacheTest cacheTest = new SingleCacheTest();
BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
String line = "";
String[] command = null;
while ((line = inputReader.readLine()) != null)
{
command = line.split(" ");
if (command[0].equalsIgnoreCase("insert"))
{
cacheTest.addData();
}
else if (command[0].equalsIgnoreCase("addmap"))
{
cacheTest.addMap();
}
else if (command[0].equalsIgnoreCase("addlist"))
{
cacheTest.addList();
}
else if (command[0].equalsIgnoreCase("clear"))
{
cacheTest.clearData();
}
else if (command[0].equalsIgnoreCase("show"))
{
cacheTest.showData();
}
else if (command[0].equalsIgnoreCase("exit"))
{
break;
}
}
}
}
///------------------------------tc-config.xml------------------------------//
<?xml version="1.0" encoding="UTF-8"?>
<con:tc-config xmlns:con="http://www.terracotta.org/config">
<servers>
<server host="%i" name="localhost">
<dso-port>9510</dso-port>
<jmx-port>9520</jmx-port>
<data>terracotta/server-data</data>
<logs>terracotta/server-logs</logs>
<statistics>terracotta/cluster-statistics</statistics>
</server>
<update-check>
<enabled>true</enabled>
</update-check>
</servers>
<clients>
<logs>terracotta/client-logs</logs>
<statistics>terracotta/client-statistics/%D</statistics>
</clients>
<application>
<dso>
<instrumented-classes>
<include>
<class-expression>*..*</class-expression>
</include>
</instrumented-classes>
<roots>
<root>
<field-name>com.zycus.utils.tc.cache.TerraCottaCache.internalCache</field-name>
</root>
</roots>
<locks>
<named-lock>
<method-expression>void com.zycus.utils.tc.cache.TerraCottaCache.clear()</method-expression>
<lock-level>write</lock-level>
<lock-name>clear</lock-name>
</named-lock>
<named-lock>
<method-expression>java.util.Map com.zycus.utils.tc.cache.TerraCottaCache.getMap()</method-expression>
<lock-level>write</lock-level>
<lock-name>getMap</lock-name>
</named-lock>
<named-lock>
<method-expression>void com.zycus.utils.tc.cache.TerraCottaCache.put(java.lang.Object, java.lang.Object)</method-expression>
<lock-level>write</lock-level>
<lock-name>put</lock-name>
</named-lock>
<named-lock>
<method-expression>java.lang.Object com.zycus.utils.tc.cache.TerraCottaCache.get(java.lang.Object)</method-expression>
<lock-level>write</lock-level>
<lock-name>get</lock-name>
</named-lock>
<named-lock>
<method-expression>java.lang.Object com.zycus.utils.tc.cache.TerraCottaCache.remove(java.lang.Object)</method-expression>
<lock-level>write</lock-level>
<lock-name>remove</lock-name>
</named-lock>
</locks>
</dso>
</application>
</con:tc-config>
///------------------------------STACK TRACE------------------------------//
com.tc.object.tx.UnlockedSharedObjectException:
*******************************************************************************
Attempt to access a shared object outside the scope of a shared lock.
All access to shared objects must be within the scope of one or more shared locks defined in your Terracotta configuration.
Please alter the locks section of your Terracotta configuration so that this access is auto-locked or protected by a named lock.
For more information on this issue, please visit our Troubleshooting Guide at:
http://terracotta.org/kit/troubleshooting
Caused by Thread: main in VM(16)
Shared Object Type: java.util.Vector
*******************************************************************************
at com.tc.object.tx.ClientTransactionManagerImpl.getTransaction(ClientTransactionManagerImpl.java:300)
at com.tc.object.tx.ClientTransactionManagerImpl.checkWriteAccess(ClientTransactionManagerImpl.java:313)
at com.tc.object.bytecode.ManagerImpl.checkWriteAccess(ManagerImpl.java:697)
at com.tc.object.bytecode.ManagerUtil.checkWriteAccess(ManagerUtil.java:378)
at java.util.Vector.add(Vector.java)
at com.zycus.utils.tc.cache.test.SingleCacheTest.addList(SingleCacheTest.java:67)
at com.zycus.utils.tc.cache.test.SingleCacheTest.main(SingleCacheTest.java:146)
Exception in thread "main" com.tc.object.tx.UnlockedSharedObjectException:
*******************************************************************************
Attempt to access a shared object outside the scope of a shared lock.
All access to shared objects must be within the scope of one or more shared locks defined in your Terracotta configuration.
Please alter the locks section of your Terracotta configuration so that this access is auto-locked or protected by a named lock.
For more information on this issue, please visit our Troubleshooting Guide at:
http://terracotta.org/kit/troubleshooting
Caused by Thread: main in VM(16)
Shared Object Type: java.util.Vector
*******************************************************************************
at com.tc.object.tx.ClientTransactionManagerImpl.getTransaction(ClientTransactionManagerImpl.java:300)
at com.tc.object.tx.ClientTransactionManagerImpl.checkWriteAccess(ClientTransactionManagerImpl.java:313)
at com.tc.object.bytecode.ManagerImpl.checkWriteAccess(ManagerImpl.java:697)
at com.tc.object.bytecode.ManagerUtil.checkWriteAccess(ManagerUtil.java:378)
at java.util.Vector.add(Vector.java)
at com.zycus.utils.tc.cache.test.SingleCacheTest.addList(SingleCacheTest.java:67)
at com.zycus.utils.tc.cache.test.SingleCacheTest.main(SingleCacheTest.java:146)
| Filename |
tc-config.xml |
Download
|
| Description |
|
| Filesize |
2 Kbytes
|
| Downloaded: |
115 time(s) |
| Filename |
SingleCacheTest.java |
Download
|
| Description |
|
| Filesize |
3 Kbytes
|
| Downloaded: |
103 time(s) |
| Filename |
TerraCottaCache.java |
Download
|
| Description |
|
| Filesize |
993 bytes
|
| Downloaded: |
101 time(s) |
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 09/29/2008 01:46:30
|
sanoujam
journeyman
Joined: 09/09/2008 10:30:25
Messages: 43
Offline
|
Hi Dhruv
The exception you are getting is because you are trying to modify an object that comes in your root's (com.zycus.utils.tc.cache.TerraCottaCache.internalCache) object graph without acquiring any locks.
You are trying to add a list/map as a value to the root map without acquiring locks in SingleCacheTest.addList and SingleCacheTest.addMap.
Any modifications in any shared object (a shared object is one which comes under the object graph of any root in Terracotta) needs to be done under the scope of a shared lock.
You missed out the following config in your tc-config:
Code:
<named-lock>
<method-expression>void com.zycus.utils.tc.cache.test.SingleCacheTest.addList()</method-expression>
<lock-level>write</lock-level>
<lock-name>addList</lock-name>
</named-lock>
<named-lock>
<method-expression>void com.zycus.utils.tc.cache.test.SingleCacheTest.addMap()</method-expression>
<lock-level>write</lock-level>
<lock-name>addMap</lock-name>
</named-lock>
Adding the above snippet in your tc-config should make it work.
You can find more information here
Hope you are finding Terracotta as exciting as we do.
Cheers
|
-- Abhishek Sanoujam |
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 09/29/2008 03:48:54
|
dhruv
journeyman
Joined: 09/29/2008 00:22:08
Messages: 10
Offline
|
Thank You Abhishek,
i was looking for terracotta because of transparency in configuration.
but as you mention my all classes which uses package need to be configured in TC xml.
is there any alternative to achieve configuration transparency for clients.
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 09/29/2008 08:32:28
|
tgautier
seraphim
Joined: 06/05/2006 12:19:26
Messages: 1781
Offline
|
You can use Annotations:
http://www.terracotta.org/web/display/docs/Annotations
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 09/29/2008 09:13:41
|
ari
seraphim
Joined: 05/24/2006 14:23:21
Messages: 1665
Location: San Francisco, CA
Offline
|
or TIMs. "Transparency for clients" is kind of unclear. But Annotations will give you easier / clearer config that the developers can see and know is therefore going to invoke Terracotta behavior. TIMs allow libraries (jars, or what have you) to completely hide all the Terracotta-ness from the end user developer. Your library configuration need not reside inside the regular tc-config.xml that developers have access to and rely on.
--Ari
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 09/30/2008 07:01:47
|
dhruv
journeyman
Joined: 09/29/2008 00:22:08
Messages: 10
Offline
|
Thanks Taylor Gautier & ari ,
but its really surprising to me tht
TerraCotta is able to check that
DSO is being used by some method which is not present in my config file.
but why it cant lock by itself ???
is there any way to automatically lock DSO without changing code.
my basic requirement for using DSO is for holding big object (application specific)
and there are many codes and many methods updating object.
i cant define those all in "xml" and even cant set "Annotations" for same.
please suggest any solution for real transparency to client.
i hope there must be some solution to it.
and one more thing i didnt find any "tim-get.bat"
using terracotta 2.6.2
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 09/30/2008 07:35:22
|
ari
seraphim
Joined: 05/24/2006 14:23:21
Messages: 1665
Location: San Francisco, CA
Offline
|
well,
when you say, "lock by itself" do you mean figure out where to place synchronized{} in your code? Or is your code pretty well synchronized and you want us to lock it all w/o any native Java locking going on in your code?
The first is easy...lock using a package expression and the autolock feature:
http://www.terracotta.org/web/display/docs/Concept+and+Architecture+Guide#ConceptandArchitectureGuide-locks
WARNING: it may not perform well since you will be doing lots of locking thru Terracotta that you might not need and you will have no choice but to make all locks write locks unless your development team uses some sort of naming convention for methods that get and objects that set values in shared objects (eg: getName() or setName()).
If you want the latter, I hate to say this but it doesn't exist. There is no product that infers concurrency model from the class files on disk and inserts the proper synchronization logic that I know of. But, yes, that would be an AWESOME feature.
--Ari
--Ari
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 10/01/2008 05:30:04
|
dhruv
journeyman
Joined: 09/29/2008 00:22:08
Messages: 10
Offline
|
My Root is HashMap(DSO)
i am removing object from HashMap
and when i try to update it ,
its throwing error
once the object is removed from ROOT
any updation to object in any method should not throw exception !!
but it is throing exception..
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 10/01/2008 07:19:53
|
tgautier
seraphim
Joined: 06/05/2006 12:19:26
Messages: 1781
Offline
|
ahh I see. this point is discussed in detail in the concept and architecture guide on locks. please read the whole section:
http://www.terracotta.org/web/display/docs/Concept+and+Architecture+Guide#ConceptandArchitectureGuide-Locks
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 10/01/2008 09:57:44
|
dhruv
journeyman
Joined: 09/29/2008 00:22:08
Messages: 10
Offline
|
hi,
any change in tc-config.xml require to recreate
boot.jar
or once boot.jar is created for specific JVM works fine with config file ??
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 10/01/2008 10:02:15
|
tgautier
seraphim
Joined: 06/05/2006 12:19:26
Messages: 1781
Offline
|
Most changes do not require a change in the boot-jar. Only things that affect the boot jar - e.g. core java classes.
For example:
1) updating the additional-boot-jar-classes entry
2) updating locking for java classes
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 10/01/2008 10:26:35
|
dhruv
journeyman
Joined: 09/29/2008 00:22:08
Messages: 10
Offline
|
where i can find what changes makes creating new boot.jar
what if i add many funcions entry for locking ??
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 10/01/2008 10:43:03
|
tgautier
seraphim
Joined: 06/05/2006 12:19:26
Messages: 1781
Offline
|
There's no doc that I am aware of that lists the changes explicitly. We will fix that.
I am updating my previous message to be more clear:
Most changes do not require a change in the boot-jar. Only things that affect the boot jar - e.g. locks that affect core java language classes (e.g. java.util.Hashtable).
For example:
1) updating the additional-boot-jar-classes entry
2) updating locking for java lang classes - e.g. java.util.Hashtable (not application classes)
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 10/01/2008 10:54:30
|
dhruv
journeyman
Joined: 09/29/2008 00:22:08
Messages: 10
Offline
|
so you mean to say that
if i configure tc-config.xml for my core caching API
and create boot.jar using eclipse tool for specific JVM
now when client code use this boot.jar
and add new methods entry in tc-config.xml
do i need to create boot.jar again in this case
my all new methods are application or client code specific.
|
|
|
 |
![[Post New]](/forums/templates/default/images/icon_minipost_new.gif) 10/01/2008 12:16:32
|
gkeim
ophanim
Joined: 12/05/2006 10:22:37
Messages: 685
Location: Terracotta, Inc.
Offline
|
No, the boot jar is for core Java classes, not your application classes.
|
Gary Keim (terracotta developer) Want to post to this forum? Join the Terracotta Community |
|
|
 |
|
|