[Logo] Terracotta Discussion Forums (LEGACY READ-ONLY ARCHIVE)
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
Messages posted by: apostle8  XML
Profile for apostle8 -> Messages posted by apostle8 [6]
Author Message

rajoshi wrote:
HI ,

I am not sure if it's because of Thread.sleep () . Have you checked it , does it works if we don't make the Thread sleep.I don't think it's because of that . Actually the cache which is created programmatically works for the Cachemanager instance that created it . In your code in Test2.java , even if you change.
cacheNew = CacheManager.getInstance().getCache("CACHE_NEW"); to
cacheNew = CacheManager.getInstance().getCache("CACHE_MY");(where CACHE_MY is not defined anywhere ) it will not throw any error and say CACHE_MY is null , it means its not even picking up the "CACHE_NEW" created by the Cachemanager in Test1. 


I tested, Thread.sleep don't effect anything, it still doesn't work if i removed it.

the main issue is: when create a new Cache CACHE_NEW in Test1.java, CacheManager cannot save the cache into Terracotta server -- that's why Test2.java cannot get CACHE_NEW.

do you know how create a new cache programmatically via CacheManager and store into TC server?

rajoshi wrote:
Hi ,

In your code actually in Test1.java you are creating the cache cacheNew dynamically which is handled by the cachemanger instance you have created there and at the end you do the shutdown . However in the other class Test2.java you are again trying to use the cacheNew with a new instance of cachemanager hence it is giving you null value. 


Thank you very much for you reply, actually not the issue, even we removed
Code:
 CacheManager.getInstance().shutdown();
 


in Test2.java we still cannot got the cache and values, did you saw:

Code:
 for (int i=0;i<=100;i++) {
     ........
     try {
           Thread.sleep(3000);
     } catch (InterruptedException e) {
            e.printStackTrace();
     }
 }
 


that's mean Test1.java will execute about 3 seconds * 100 times = 300 seconds, so if you start Test1.java first and start Test2.java immediately, in the 300 seconds you will see cacheNew is null in test2.java.
I have an issue here confused me many days, wish can got help, thank you very much!

below as my test files, the issue is:
I host 2 TC service as cache server(the second one is for hot standby), and multiple EHCache clients connect to these 2 TC services.

I created 2 class file -- Test1.java and Test2.java, each class as an EHCache client. Both they read ehcache-tc.xml.

The logic is: In ehcache-tc.xml i predefined a cache named "CACHE_TEST", multiple ehcache clients use the config file and connect to TC server.

Test case 1. Start Test1.java and then Test2.java, when ehcache client 1(Test1.java) get cache "CACHE_TEST" and put values in it, ehcache client 2(Test2.java) can got these values.

Test case 2. When Test1.java create a new cache and set a new name "TEST_NEW", and then pus some values in the cache, in Test2.java, we cannot get the cache from TC server even values in it. -- I know that's because TEST_NEW not be submitted into TC server, but i don't know how to add the cache into cluster, anybody can help on this?


Test steps:
1. start TC services.
2. Run Test1.java
3. Run Test2.java



i host 2 Terracotta service as cache cluster, config file as:

Code:
 
 <?xml version="1.0" encoding="UTF-8"?>
 <con:tc-config xmlns:con="http://www.terracotta.org/config">
   <servers>
     <server host="10.0.1.113" name="tc1">
       <dso-port bind="0.0.0.0">9510</dso-port>
       <jmx-port bind="0.0.0.0">9520</jmx-port>
       <data>/Users/colky/java/terracotta/server-data1</data>
       <logs>/Users/colky/java/terracotta/server-logs1</logs>
       <statistics>/Users/colky/java/terracotta/cluster-statistics1</statistics>
       <dso>
         <persistence>
           <mode>permanent-store</mode>
         </persistence>
       </dso>
     </server>
     <server host="10.0.1.113" name="tc2">
       <dso-port bind="0.0.0.0">9610</dso-port>
       <jmx-port bind="0.0.0.0">9620</jmx-port>
       <data>/Users/colky/java/terracotta/server-data2</data>
       <logs>/Users/colky/java/terracotta/server-logs2</logs>
       <statistics>/Users/colky/java/terracotta/cluster-statistics2</statistics>
       <dso>
         <persistence>
           <mode>permanent-store</mode>
         </persistence>
       </dso>
     </server>
     <!--If using more than one server, add an <ha> section.-->
     <ha>
       <mode>networked-active-passive</mode>
       <networked-active-passive>
         <election-time>5</election-time>
       </networked-active-passive>
     </ha>
   </servers>
   <clients>
     <logs>/Users/colky/java/terracotta/client-logs</logs>
   </clients>
   <application>
     <dso>
       <instrumented-classes/>
     </dso>
   </application>
 </con:tc-config>
 
 



Step 2 is start these 2 Terracotta service.

And then in my project, i created a ehcache configuration file ehcache-tc.xml:

Code:
 
 <?xml version="1.0" encoding="UTF-8"?>
 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xsi:noNamespaceSchemaLocation="ehcache.xsd">  
 
 
      
      
     <terracottaConfig url="10.0.1.113:9510, 10.0.1.113:9610" /> 
 
 
     <defaultCache maxElementsInMemory="100000" eternal="false"  memoryStoreEvictionPolicy="LRU"
         timeToIdleSeconds="100000" timeToLiveSeconds="100000" maxElementsOnDisk="0">  
         <terracotta clustered="true" coherent="true" valueMode="serialization"/>
     </defaultCache>  
 
 
     <cache name="CACHE_TEST" maxElementsInMemory="100000" eternal="false"  memoryStoreEvictionPolicy="LRU"
         timeToIdleSeconds="100000" timeToLiveSeconds="100000" maxElementsOnDisk="0">  
         <terracotta clustered="true" coherent="true" valueMode="serialization"/>
     </cache>  
          
 
       
 </ehcache>  
 
 



Here is my test source code base on ehcache-tc.xml:


Test1.java

Code:
 package ehcache;   
 
 import net.sf.ehcache.Cache;
 import net.sf.ehcache.CacheManager;
 import net.sf.ehcache.Element;
 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
   
 
 
 public class Test1 {
     public static void main(String[] args) {
         
         CacheManager.create(Test1.class.getResource("/ehcache-tc.xml"));
         
         //get a cache defined in ehcache xml
         Cache cacheTest = CacheManager.getInstance().getCache("CACHE_TEST"); 
         
         //define a new cache, how add into cluster?
         
         Cache cacheNew = null;
         if (!CacheManager.getInstance().cacheExists("CACHE_NEW")) {
             //i tried multiple method to create a new cache and both cannot work in cluster
             
             //method 1: create new one and add into cache manager.
             cacheNew = new Cache("CACHE_NEW", 10000, MemoryStoreEvictionPolicy.LFU, false, null, true, 
                     120, 120, false, 1000, null, null, 10000000, 30, true, true, "serialization", true);
             CacheManager.getInstance().addCache(cacheNew);
             
             //method 2: create new one via CacheManager
             //CacheManager.getInstance().addCache("CACHE_NEW");
             
             //method 3: get CACHE_TEST's configuration from ehcache-tc.xml and rename to CACHE_NEW, and create base on the configuration
             //skip here, don't know how to write.
             
             System.out.println("Created cache: CACHE_NEW");
             
         } else {
             cacheNew = CacheManager.getInstance().getCache("CACHE_NEW");
         }
         
         
         for (int i=0;i<=100;i++) {
             
             cacheTest.put(new Element("test_key1", "value " + i));
             
             System.out.println(cacheTest.get("test_key1")); // can got value here
             
             
             cacheNew.put(new Element("new_key1", "value " + i));
             
             System.out.println(cacheNew.get("new_key1")); // also can got value here
 
             try {
                 Thread.sleep(3000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         
         }
         CacheManager.getInstance().shutdown();
     }
 }
  
 
 
 




Test2.java


Code:
 package ehcache;   
 
 import net.sf.ehcache.Cache;
 import net.sf.ehcache.CacheManager;
   
 
 
 public class Test2 {
     public static void main(String[] args) {
         
         CacheManager.create(Test2.class.getResource("/ehcache-tc.xml"));
         
         //get a cache defined in ehcache xml
         Cache cacheTest = CacheManager.getInstance().getCache("CACHE_TEST"); 
         
 
         Cache cacheNew = CacheManager.getInstance().getCache("CACHE_NEW");
         System.out.println("cacheNew is null: " + (cacheNew == null)); 
         
         for (int i=0;i<=100;i++) {
 
             System.out.println(cacheTest.get("test_key1")); // can got value here
 
             // cannot got value here, because cacheNew is null
             if (cacheNew != null)
                 System.out.println(cacheNew.get("new_key1")); 
 
             try {
                 Thread.sleep(3000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         
         }
         
         CacheManager.getInstance().shutdown();
         
     }
 }
  
 
 


老马在吗??请问Terracotta是不是没有办法解决这种问题啊?
顶一下,希望高手解答
大家好,这里有个非常棘手的问题想请教,已经困扰了我好几天了,关于详细的代码请参见下方。

我的问题是:
我启动了2个Terracotta服务,多个ehcache实例同时连接到这2个TC服务,起到Cluster缓存的作用。当然,其中一个TC服务只是热备份的作用,为了便于理解,你认为只有一个TC服务就好了。

在Test1.java里面, 当我获取一个ehcache-tc.xml里面预先定义的cache并赋值给它的时候,我再启动Test2.java,这个时候在Test2.java里面,可以读取到这个值,说明cluster是工作的。

但是,当我在Test1.java动态创建一个新的cache时(没有在ehcache-tc.xml里面定义),Test2.java无法读取到该cache或者该cache里面的数据。我知道是因为Test1.java没有把这个新的cache放到cluster里面,请问如何用代码动态的放到cluster里面呢?

代码见下面:


2个Terracotta服务的配置文件:

Code:
 
 <?xml version="1.0" encoding="UTF-8"?>
 <con:tc-config xmlns:con="http://www.terracotta.org/config">
   <servers>
     <server host="10.0.1.113" name="tc1">
       <dso-port bind="0.0.0.0">9510</dso-port>
       <jmx-port bind="0.0.0.0">9520</jmx-port>
       <data>/Users/colky/java/terracotta/server-data1</data>
       <logs>/Users/colky/java/terracotta/server-logs1</logs>
       <statistics>/Users/colky/java/terracotta/cluster-statistics1</statistics>
       <dso>
         <persistence>
           <mode>permanent-store</mode>
         </persistence>
       </dso>
     </server>
     <server host="10.0.1.113" name="tc2">
       <dso-port bind="0.0.0.0">9610</dso-port>
       <jmx-port bind="0.0.0.0">9620</jmx-port>
       <data>/Users/colky/java/terracotta/server-data2</data>
       <logs>/Users/colky/java/terracotta/server-logs2</logs>
       <statistics>/Users/colky/java/terracotta/cluster-statistics2</statistics>
       <dso>
         <persistence>
           <mode>permanent-store</mode>
         </persistence>
       </dso>
     </server>
     <!--If using more than one server, add an <ha> section.-->
     <ha>
       <mode>networked-active-passive</mode>
       <networked-active-passive>
         <election-time>5</election-time>
       </networked-active-passive>
     </ha>
   </servers>
   <clients>
     <logs>/Users/colky/java/terracotta/client-logs</logs>
   </clients>
   <application>
     <dso>
       <instrumented-classes/>
     </dso>
   </application>
 </con:tc-config>
 
 


配置好之后,就是启动这2个tc服务,当然你也可以启动一个。

在Java Project里面,配置ehcache-tc.xml:

Code:
 
 <?xml version="1.0" encoding="UTF-8"?>
 <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     xsi:noNamespaceSchemaLocation="ehcache.xsd">  
 
 
      
      
     <terracottaConfig url="10.0.1.113:9510, 10.0.1.113:9610" /> 
 
 
     <defaultCache maxElementsInMemory="100000" eternal="false"  memoryStoreEvictionPolicy="LRU"
         timeToIdleSeconds="100000" timeToLiveSeconds="100000" maxElementsOnDisk="0">  
         <terracotta clustered="true" coherent="true" valueMode="serialization"/>
     </defaultCache>  
 
 
     <cache name="CACHE_TEST" maxElementsInMemory="100000" eternal="false"  memoryStoreEvictionPolicy="LRU"
         timeToIdleSeconds="100000" timeToLiveSeconds="100000" maxElementsOnDisk="0">  
         <terracotta clustered="true" coherent="true" valueMode="serialization"/>
     </cache>  
          
 
       
 </ehcache>  
 
 



测试代码:

Test1.java

Code:
 package ehcache;   
 
 import net.sf.ehcache.Cache;
 import net.sf.ehcache.CacheManager;
 import net.sf.ehcache.Element;
 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
   
 
 
 public class Test1 {
     public static void main(String[] args) {
         
         CacheManager.create(Test1.class.getResource("/ehcache-tc.xml"));
         
         //get a cache defined in ehcache xml
         Cache cacheTest = CacheManager.getInstance().getCache("CACHE_TEST"); 
         
         //define a new cache, how add into cluster?
         
         Cache cacheNew = null;
         if (!CacheManager.getInstance().cacheExists("CACHE_NEW")) {
             //i tried multiple method to create a new cache and both cannot work in cluster
             
             //method 1: create new one and add into cache manager.
             cacheNew = new Cache("CACHE_NEW", 10000, MemoryStoreEvictionPolicy.LFU, false, null, true, 
                     120, 120, false, 1000, null, null, 10000000, 30, true, true, "serialization", true);
             CacheManager.getInstance().addCache(cacheNew);
             
             //method 2: create new one via CacheManager
             //CacheManager.getInstance().addCache("CACHE_NEW");
             
             //method 3: get CACHE_TEST's configuration from ehcache-tc.xml and rename to CACHE_NEW, and create base on the configuration
             //skip here, don't know how to write.
             
             System.out.println("Created cache: CACHE_NEW");
             
         } else {
             cacheNew = CacheManager.getInstance().getCache("CACHE_NEW");
         }
         
         
         for (int i=0;i<=100;i++) {
             
             cacheTest.put(new Element("test_key1", "value " + i));
             
             System.out.println(cacheTest.get("test_key1")); // can got value here
             
             
             cacheNew.put(new Element("new_key1", "value " + i));
             
             System.out.println(cacheNew.get("new_key1")); // also can got value here
 
             try {
                 Thread.sleep(3000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         
         }
         CacheManager.getInstance().shutdown();
     }
 }
  
 
 
 




Test2.java


Code:
 package ehcache;   
 
 import net.sf.ehcache.Cache;
 import net.sf.ehcache.CacheManager;
   
 
 
 public class Test2 {
     public static void main(String[] args) {
         
         CacheManager.create(Test2.class.getResource("/ehcache-tc.xml"));
         
         //get a cache defined in ehcache xml
         Cache cacheTest = CacheManager.getInstance().getCache("CACHE_TEST"); 
         
 
         Cache cacheNew = CacheManager.getInstance().getCache("CACHE_NEW");
         System.out.println("cacheNew is null: " + (cacheNew == null)); 
         
         for (int i=0;i<=100;i++) {
 
             System.out.println(cacheTest.get("test_key1")); // can got value here
 
             // cannot got value here, because cacheNew is null
             if (cacheNew != null)
                 System.out.println(cacheNew.get("new_key1")); 
 
             try {
                 Thread.sleep(3000);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         
         }
         
         CacheManager.getInstance().shutdown();
         
     }
 }
  
 
 



测试方法:启动 test1.java,然后再启动Test2.java,你会发现当2个class读写CACHE_TEST这个cache的时候,都没有问题,但是当读写一个代码生成的新的cache的时候,test2.java就读取不到了,请教怎么样才能做到代码动态生成cache并cluster!?
 
Profile for apostle8 -> Messages posted by apostle8 [6]
Go to:   
Powered by JForum 2.1.7 © JForum Team