KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > cache > buddyreplication > BuddyReplicationFailoverTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.cache.buddyreplication;
8
9 import org.jboss.cache.CacheImpl;
10 import org.jboss.cache.Fqn;
11 import org.jboss.cache.misc.TestingUtil;
12
13 /**
14  * Tests behaviour when data owners fail - essentially this tests data gravitation
15  *
16  * @author <a HREF="mailto:manik@jboss.org">Manik Surtani (manik@jboss.org)</a>
17  */

18 public class BuddyReplicationFailoverTest extends BuddyReplicationTestsBase
19 {
20    protected boolean optimisticLocks = false;
21    private String JavaDoc key = "key";
22    private String JavaDoc value = "value";
23    private CacheImpl[] caches;
24
25    protected void tearDown() throws Exception JavaDoc
26    {
27       if (caches != null)
28       {
29          cleanup(caches);
30          caches = null;
31       }
32    }
33
34
35    public void testDataGravitationKillOwner() throws Exception JavaDoc
36    {
37       testDataGravitation(true);
38    }
39
40    public void testDataGravitationDontKillOwner() throws Exception JavaDoc
41    {
42       testDataGravitation(false);
43    }
44
45    private void testDataGravitation(boolean killOwner) throws Exception JavaDoc
46    {
47       caches = createCaches(3, false, true, optimisticLocks);
48
49       String JavaDoc fqn = "/test";
50       String JavaDoc backupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + fqn;
51
52       dumpCacheContents(caches);
53
54       caches[0].put(fqn, key, value);
55
56       dumpCacheContents(caches);
57
58       assertEquals("Value should exist", value, caches[0].get(fqn, key));
59
60       dumpCacheContents(caches);
61
62       // use exists instead of get() to prevent going up the interceptor stack
63
assertTrue("Should be false", !caches[1].exists(fqn));
64       assertTrue("Should be false", !caches[2].exists(fqn));
65
66       assertNull("Should be null", caches[0].get(backupFqn, key));
67       assertEquals("Value should exist", value, caches[1].get(backupFqn, key));
68       assertNull("Should be null", caches[2].get(backupFqn, key));
69
70       if (killOwner)
71       {
72          caches[0].stop();
73          caches[0] = null;
74          TestingUtil.sleepThread(500);
75       }
76
77       System.out.println("***** Killed original data owner, about to call a get on a different cache instance. *****");
78
79       // according to data gravitation, a call to *any* cache should retrieve the data, and move the data to the new cache.
80
assertEquals("Value should have gravitated", value, caches[2].get(fqn, key));
81
82       TestingUtil.sleepThread(500);
83
84       dumpCacheContents(caches);
85
86       // now lets test the eviction part of gravitation
87
String JavaDoc newBackupFqn = "/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress()) + fqn;
88
89       // use exists instead of get() to prevent going up the interceptor stack
90
if (!killOwner) assertTrue("Should be false", !caches[0].exists(fqn));
91       assertTrue("Should be false", !caches[1].exists(fqn));
92
93       // the old backup should no longer exist
94
if (!killOwner) assertNull("Should be null", caches[0].get(backupFqn, key));
95       assertNull("Should be null", caches[1].get(backupFqn, key));
96       assertNull("Should be null", caches[2].get(backupFqn, key));
97
98       // and the backup should now exist in caches[2]'s buddy which is caches[0]
99
if (killOwner)
100       {
101          assertEquals("Value should exist", value, caches[1].get(newBackupFqn, key));
102       }
103       else
104       {
105          assertEquals("Value should exist", value, caches[0].get(newBackupFqn, key));
106          assertNull("Should be null", caches[1].get(newBackupFqn, key));
107       }
108       assertNull("Should be null", caches[2].get(newBackupFqn, key));
109    }
110
111    public void testDataReplicationSuppression() throws Exception JavaDoc
112    {
113       caches = createCaches(3, false, false, optimisticLocks);
114
115       Fqn fqn = Fqn.fromString("/test");
116       Fqn backupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress())
117               + "/test");
118       caches[0].put(fqn, key, value);
119
120       dumpCacheContents(caches);
121
122       assertEquals("value", caches[0].get(fqn, key));
123       assertNull(caches[0].get(backupFqn, key));
124       assertEquals("value", caches[1].get(backupFqn, key));
125       assertNull(caches[1].get(fqn, key));
126       assertNull(caches[2].get(fqn, key));
127       assertNull(caches[2].get(backupFqn, key));
128
129       assertNoLocks(caches);
130
131       backupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[1].getLocalAddress())
132               + "/test");
133
134       System.out.println("*** Calling get() on cache[1] with force option");
135
136       caches[1].getInvocationContext().getOptionOverrides().setForceDataGravitation(true);
137       assertEquals("value", caches[1].get(fqn, key));
138
139       dumpCacheContents(caches);
140
141       assertNull(caches[1].get(backupFqn, key));
142       assertEquals("value", caches[2].get(backupFqn, key));
143       assertNull(caches[2].get(fqn, key));
144       assertNull(caches[0].get(fqn, key));
145       assertNull(caches[0].get(backupFqn, key));
146    }
147
148    public void testSubtreeRetrieval() throws Exception JavaDoc
149    {
150       caches = createCaches(3, false, true, optimisticLocks);
151
152       Fqn fqn = Fqn.fromString("/test");
153       Fqn fqn2 = Fqn.fromString("/test/subtree");
154
155       Fqn backupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + "/test");
156       Fqn backupFqn2 = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[0].getLocalAddress()) + "/test/subtree");
157
158
159       caches[0].put(fqn, key, value);
160       caches[0].put(fqn2, key, value);
161
162       // test backup replication to buddy
163
assertEquals(value, caches[0].get(fqn, key));
164       assertEquals(value, caches[0].get(fqn2, key));
165       assertEquals(value, caches[1].get(backupFqn, key));
166       assertEquals(value, caches[1].get(backupFqn2, key));
167
168       assertTrue(!caches[0].exists(backupFqn));
169       assertTrue(!caches[0].exists(backupFqn2));
170       assertTrue(!caches[1].exists(fqn));
171       assertTrue(!caches[1].exists(fqn2));
172       assertTrue(!caches[2].exists(fqn));
173       assertTrue(!caches[2].exists(fqn2));
174       assertTrue(!caches[2].exists(backupFqn));
175       assertTrue(!caches[2].exists(backupFqn2));
176
177       assertNoLocks(caches);
178
179       // gravitate to 2:
180
caches[2].get(fqn); // expect entire subtree to gravitate.
181

182       Fqn newBackupFqn = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress()) + "/test");
183       Fqn newBackupFqn2 = Fqn.fromString("/" + BuddyManager.BUDDY_BACKUP_SUBTREE + "/" + BuddyManager.getGroupNameFromAddress(caches[2].getLocalAddress()) + "/test/subtree");
184
185       assertEquals(value, caches[2].get(fqn, key));
186       assertTrue(caches[2].exists(fqn2));
187       assertEquals(value, caches[0].get(newBackupFqn, key));
188       assertTrue(caches[0].exists(newBackupFqn2));
189
190       assertTrue(!caches[2].exists(newBackupFqn));
191       assertTrue(!caches[2].exists(newBackupFqn2));
192       assertTrue(!caches[0].exists(fqn));
193       assertTrue(!caches[0].exists(fqn2));
194       assertTrue(!caches[1].exists(fqn));
195       assertTrue(!caches[1].exists(fqn2));
196       assertTrue(!caches[1].exists(newBackupFqn));
197       assertTrue(!caches[1].exists(newBackupFqn2));
198
199       for (int i = 0; i < caches.length; i++)
200       {
201          assertTrue(!caches[i].exists(backupFqn));
202          assertTrue(!caches[i].exists(backupFqn2));
203       }
204
205       assertNoLocks(caches);
206    }
207 }
208
Popular Tags