Problem with cluster under same VM
gustavo.ambrozio May 12, 2005 9:57 AMI'm new to JBossCache and I can't see what I'm doing wrong.....
Basically, I'm testing JBossCache to verify if it's behaviour can solve my problem. I wrote the following class (simplified):
public class TreeCacheTest implements Runnable { private static boolean testEnds = false; private String id; public static void main(String[] args) { new Thread(new TreeCacheTest("1")).start(); new Thread(new TreeCacheTest("2")).start(); new Thread(new TreeCacheTest("3")).start(); } public TreeCacheTest(String id) { this.id = id; } public void run() { Util.sleep((long) (Math.random()*5000)); try { TreeCache tree = new TreeCache(); tree.setFetchStateOnStartup(true); tree.setClusterName("Test"); tree.setClusterProperties("state_transfer.xml"); tree.setCacheMode(TreeCache.REPL_SYNC); tree.createService(); tree.startService(); long end = System.currentTimeMillis() + 10000 + (long) (Math.random()*5000); while (end > System.currentTimeMillis()) { long wait = (long) (Math.random()*1000); Util.sleep(wait); synchronized(TreeCacheTest.class) { if (testEnds) break; } tree.put("/a", id+"_"+wait, new Long(wait)); } synchronized(TreeCacheTest.class) { testEnds = true; } Set set = tree.getKeys("/a"); System.out.println("ended "+id+" "+set.size()); } catch (Exception e) { e.printStackTrace(); } } }
This code starts 3 threads with different ids so I can track what everyone is doing. Each thread adds nodes to the cache for about 10 seconds. When the first one stops, all threads stop (using the static variable testEnds). So, when these threads stop, I think they should all have the same data, right? Well, that's not the case.... I get the following lines at the end of the test:
ended 1 42
ended 3 44
ended 2 42
Well, it varies from test to test, but they never have the same number of members in their cache..... What am I missing here?? Maybe my cluster configuration (state_transfer.xml):
<config> <UDP mcast_recv_buf_size="64000" mcast_send_buf_size="32000" mcast_port="45566" ucast_recv_buf_size="64000" use_incoming_packet_handler="false" mcast_addr="228.8.8.8" use_outgoing_packet_handler="false" loopback="true" ucast_send_buf_size="32000" ip_ttl="32"/> <AUTOCONF /> <PING timeout="2000" num_initial_members="3"/> <MERGE2 max_interval="10000" min_interval="5000"/> <FD_SOCK /> <VERIFY_SUSPECT timeout="1500"/> <pbcast.NAKACK max_xmit_size="8192" gc_lag="50" retransmit_timeout="600,1200,2400,4000"/> <UNICAST timeout="1000,1500,2000"/> <pbcast.STABLE stability_delay="1000" desired_avg_gossip="20000" max_bytes="0"/> <FRAG frag_size="8192" down_thread="false" up_thread="false"/> <pbcast.GMS print_local_addr="true" join_timeout="3000" join_retry_timeout="2000" shun="true"/> <pbcast.STATE_TRANSFER /> </config>
Thanks for the help.