1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.buddyreplication; |
8 |
| |
9 |
| import junit.framework.TestCase; |
10 |
| import org.jboss.cache.Cache; |
11 |
| import org.jboss.cache.CacheImpl; |
12 |
| import org.jboss.cache.DefaultCacheFactory; |
13 |
| import org.jboss.cache.Fqn; |
14 |
| import org.jboss.cache.config.BuddyReplicationConfig; |
15 |
| import org.jboss.cache.config.CacheLoaderConfig; |
16 |
| import org.jboss.cache.config.Configuration; |
17 |
| import org.jboss.cache.config.Configuration.CacheMode; |
18 |
| import org.jboss.cache.factories.UnitTestCacheConfigurationFactory; |
19 |
| import org.jboss.cache.factories.XmlConfigurationParser; |
20 |
| import org.jboss.cache.loader.CacheLoader; |
21 |
| import org.jboss.cache.loader.CacheLoaderManager; |
22 |
| import org.jboss.cache.misc.TestingUtil; |
23 |
| import org.jboss.cache.xml.XmlHelper; |
24 |
| import org.jgroups.Address; |
25 |
| import org.w3c.dom.Element; |
26 |
| |
27 |
| import javax.transaction.TransactionManager; |
28 |
| import java.io.File; |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public abstract class BuddyReplicationTestsBase extends TestCase |
36 |
| { |
37 |
| protected CacheImpl[] caches; |
38 |
| |
39 |
50
| protected void tearDown() throws Exception
|
40 |
| { |
41 |
50
| System.out.println("***** TEARING DOWN *****");
|
42 |
50
| System.setProperty("org.jboss.cache.shutdown.force", "true");
|
43 |
50
| if (caches != null)
|
44 |
| { |
45 |
| |
46 |
| |
47 |
44
| for (CacheImpl c : caches)
|
48 |
| { |
49 |
128
| if (c != null && c.getBuddyManager() != null) c.getBuddyManager().stop();
|
50 |
| } |
51 |
| |
52 |
44
| for (CacheImpl c : caches)
|
53 |
| { |
54 |
136
| if (c != null)
|
55 |
| { |
56 |
128
| TransactionManager tm = c.getTransactionManager();
|
57 |
128
| if (tm != null)
|
58 |
| { |
59 |
126
| try
|
60 |
| { |
61 |
0
| if (tm.getTransaction() != null) tm.rollback();
|
62 |
| } |
63 |
| catch (Exception e) |
64 |
| { |
65 |
| |
66 |
0
| e.printStackTrace();
|
67 |
| } |
68 |
| } |
69 |
| |
70 |
128
| CacheLoaderManager clm = c.getCacheLoaderManager();
|
71 |
128
| if (clm != null)
|
72 |
| { |
73 |
28
| CacheLoader cl = c.getCacheLoaderManager().getCacheLoader();
|
74 |
28
| try
|
75 |
| { |
76 |
28
| if (cl != null) cl.remove(Fqn.ROOT);
|
77 |
| } |
78 |
| catch (Exception e) |
79 |
| { |
80 |
| |
81 |
0
| e.printStackTrace();
|
82 |
| } |
83 |
| } |
84 |
| |
85 |
128
| c.killChannel();
|
86 |
128
| c.stop();
|
87 |
128
| c = null;
|
88 |
| } |
89 |
| } |
90 |
| } |
91 |
50
| caches = null;
|
92 |
50
| System.gc();
|
93 |
| } |
94 |
| |
95 |
| protected static int VIEW_BLOCK_TIMEOUT = 5000; |
96 |
| |
97 |
2
| protected CacheImpl createCache(int numBuddies, String buddyPoolName) throws Exception
|
98 |
| { |
99 |
2
| return createCache(numBuddies, buddyPoolName, false, true);
|
100 |
| } |
101 |
| |
102 |
0
| protected CacheImpl createCache(int numBuddies, String buddyPoolName, boolean useDataGravitation) throws Exception
|
103 |
| { |
104 |
0
| return createCache(numBuddies, buddyPoolName, useDataGravitation, true);
|
105 |
| } |
106 |
| |
107 |
16
| protected CacheImpl createCache(int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean start) throws Exception
|
108 |
| { |
109 |
16
| return createCache(false, numBuddies, buddyPoolName, useDataGravitation, true, start);
|
110 |
| } |
111 |
| |
112 |
94
| protected CacheImpl createCache(boolean optimisticLocks, int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean start) throws Exception
|
113 |
| { |
114 |
94
| return createCache(optimisticLocks, numBuddies, buddyPoolName, useDataGravitation, true, start);
|
115 |
| } |
116 |
| |
117 |
28
| protected CacheImpl createCache(int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean removeOnFind, boolean start) throws Exception
|
118 |
| { |
119 |
28
| return createCache(false, numBuddies, buddyPoolName, useDataGravitation, removeOnFind, start);
|
120 |
| } |
121 |
| |
122 |
138
| protected CacheImpl createCache(boolean optimisticLocks, int numBuddies, String buddyPoolName, boolean useDataGravitation, boolean removeOnFind, boolean start) throws Exception
|
123 |
| { |
124 |
138
| CacheImpl c = (CacheImpl) DefaultCacheFactory.getInstance().createCache(UnitTestCacheConfigurationFactory.createConfiguration(CacheMode.REPL_SYNC), false);
|
125 |
138
| c.getConfiguration().setClusterName("BuddyReplicationTest");
|
126 |
| |
127 |
138
| String xmlString = "<config><buddyReplicationEnabled>true</buddyReplicationEnabled>\n" +
|
128 |
| "<buddyCommunicationTimeout>500</buddyCommunicationTimeout>\n" + |
129 |
| " <buddyLocatorClass>org.jboss.cache.buddyreplication.NextMemberBuddyLocator</buddyLocatorClass>\n" + |
130 |
| " <autoDataGravitation>" + useDataGravitation + "</autoDataGravitation>\n" + |
131 |
| " <dataGravitationRemoveOnFind>" + removeOnFind + "</dataGravitationRemoveOnFind>\n" + |
132 |
| " <buddyLocatorProperties>numBuddies = " + numBuddies + "</buddyLocatorProperties>\n"; |
133 |
| |
134 |
26
| if (buddyPoolName != null) xmlString += "<buddyPoolName>" + buddyPoolName + "</buddyPoolName>";
|
135 |
138
| xmlString += "</config>";
|
136 |
138
| Element element = XmlHelper.stringToElement(xmlString);
|
137 |
138
| BuddyReplicationConfig config = XmlConfigurationParser.parseBuddyReplicationConfig(element);
|
138 |
138
| c.getConfiguration().setBuddyReplicationConfig(config);
|
139 |
| |
140 |
138
| c.getConfiguration().setFetchInMemoryState(true);
|
141 |
138
| if (optimisticLocks)
|
142 |
| { |
143 |
12
| c.getConfiguration().setNodeLockingScheme(Configuration.NodeLockingScheme.OPTIMISTIC);
|
144 |
| } |
145 |
138
| c.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
|
146 |
138
| c.getConfiguration().setSyncCommitPhase(true);
|
147 |
| |
148 |
| |
149 |
138
| configureMultiplexer(c);
|
150 |
| |
151 |
138
| if (start)
|
152 |
| { |
153 |
102
| c.start();
|
154 |
102
| validateMultiplexer(c);
|
155 |
| } |
156 |
138
| return c;
|
157 |
| } |
158 |
| |
159 |
| |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
108
| protected void configureMultiplexer(Cache cache) throws Exception
|
167 |
| { |
168 |
| |
169 |
| } |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
74
| protected void validateMultiplexer(Cache cache)
|
179 |
| { |
180 |
74
| assertFalse("Cache is not using multiplexer", cache.getConfiguration().isUsingMultiplexer());
|
181 |
| } |
182 |
| |
183 |
11
| protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool) throws Exception
|
184 |
| { |
185 |
11
| return createCaches(1, numCaches, useBuddyPool, false);
|
186 |
| } |
187 |
| |
188 |
14
| protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool, boolean useDataGravitation, boolean optimisticLocks) throws Exception
|
189 |
| { |
190 |
14
| return createCaches(1, numCaches, useBuddyPool, useDataGravitation, optimisticLocks);
|
191 |
| } |
192 |
| |
193 |
1
| protected CacheImpl[] createCaches(int numCaches, boolean useBuddyPool, boolean useDataGravitation) throws Exception
|
194 |
| { |
195 |
1
| return createCaches(1, numCaches, useBuddyPool, useDataGravitation);
|
196 |
| } |
197 |
| |
198 |
8
| protected CacheImpl[] createCachesWithCacheLoader(int numCaches, boolean useDataGravitation, boolean removeOnFind, boolean passivation) throws Exception
|
199 |
| { |
200 |
8
| return this.createCachesWithCacheLoader(numCaches, useDataGravitation, removeOnFind, passivation, false);
|
201 |
| } |
202 |
| |
203 |
8
| protected CacheImpl[] createCachesWithCacheLoader(int numCaches, boolean useDataGravitation, boolean removeOnFind, boolean passivation, boolean fetchPersistent) throws Exception
|
204 |
| { |
205 |
8
| String tmpLoc = System.getProperty("java.io.tmpdir", "/tmp");
|
206 |
8
| tmpLoc += File.separator + "BuddyReplicationTestsBase-";
|
207 |
| |
208 |
8
| CacheImpl[] caches = new CacheImpl[numCaches];
|
209 |
8
| for (int i = 0; i < numCaches; i++)
|
210 |
| { |
211 |
24
| caches[i] = createCacheWithCacheLoader(useDataGravitation, removeOnFind, passivation, fetchPersistent, true);
|
212 |
| } |
213 |
| |
214 |
| |
215 |
8
| TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
|
216 |
8
| TestingUtil.sleepThread(getSleepTimeout());
|
217 |
8
| return caches;
|
218 |
| } |
219 |
| |
220 |
28
| protected CacheImpl createCacheWithCacheLoader(boolean useDataGravitation, boolean removeOnFind, boolean passivation, boolean fetchPersistent, boolean start) throws Exception
|
221 |
| { |
222 |
28
| CacheImpl cache = createCache(1, null, useDataGravitation, removeOnFind, false);
|
223 |
| |
224 |
28
| String cloader = "<config>\n" +
|
225 |
| "<passivation>" + passivation + "</passivation>\n" + |
226 |
| "<preload></preload>\n" + |
227 |
| |
228 |
| "<cacheloader>\n" + |
229 |
| "<class>org.jboss.cache.loader.DummyInMemoryCacheLoader</class>\n" + |
230 |
| |
231 |
| "<async>false</async>\n" + |
232 |
| "<shared>false</shared>\n" + |
233 |
| "<fetchPersistentState>" + fetchPersistent + "</fetchPersistentState>\n" + |
234 |
| "</cacheloader>\n" + |
235 |
| "</config>"; |
236 |
| |
237 |
28
| Element element = XmlHelper.stringToElement(cloader);
|
238 |
28
| CacheLoaderConfig cfg = XmlConfigurationParser.parseCacheLoaderConfig(element);
|
239 |
28
| cache.getConfiguration().setCacheLoaderConfig(cfg);
|
240 |
28
| if (start)
|
241 |
| { |
242 |
24
| cache.start();
|
243 |
| } |
244 |
| |
245 |
28
| return cache;
|
246 |
| } |
247 |
| |
248 |
5
| protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool) throws Exception
|
249 |
| { |
250 |
5
| return createCaches(numBuddies, numCaches, useBuddyPool, false);
|
251 |
| } |
252 |
| |
253 |
17
| protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool, boolean useDataGravitation) throws Exception
|
254 |
| { |
255 |
17
| return createCaches(numBuddies, numCaches, useBuddyPool, useDataGravitation, false);
|
256 |
| } |
257 |
| |
258 |
31
| protected CacheImpl[] createCaches(int numBuddies, int numCaches, boolean useBuddyPool, boolean useDataGravitation, boolean optimisticLocks) throws Exception
|
259 |
| { |
260 |
31
| CacheImpl[] caches = new CacheImpl[numCaches];
|
261 |
31
| for (int i = 0; i < numCaches; i++)
|
262 |
| { |
263 |
94
| caches[i] = createCache(optimisticLocks, numBuddies, useBuddyPool ? Character.toString((char) ('A' + i)) : null, useDataGravitation, true);
|
264 |
| } |
265 |
| |
266 |
| |
267 |
31
| TestingUtil.blockUntilViewsReceived(caches, VIEW_BLOCK_TIMEOUT);
|
268 |
31
| TestingUtil.sleepThread(getSleepTimeout());
|
269 |
31
| return caches;
|
270 |
| } |
271 |
| |
272 |
56
| protected void printBuddyGroup(CacheImpl cache)
|
273 |
| { |
274 |
56
| BuddyGroup bg = cache.getBuddyManager().buddyGroup;
|
275 |
56
| System.out.println("*** " + bg);
|
276 |
56
| System.out.println(" Groups I participate in: " + cache.getBuddyManager().buddyGroupsIParticipateIn.keySet());
|
277 |
| } |
278 |
| |
279 |
| |
280 |
| |
281 |
| |
282 |
50
| protected int getSleepTimeout()
|
283 |
| { |
284 |
50
| return 1000;
|
285 |
| } |
286 |
| |
287 |
50
| protected void assertIsBuddy(CacheImpl dataOwner, CacheImpl buddy, boolean onlyBuddy)
|
288 |
| { |
289 |
50
| Address dataOwnerLocalAddress = dataOwner.getLocalAddress();
|
290 |
50
| Address buddyLocalAddress = buddy.getLocalAddress();
|
291 |
| |
292 |
50
| System.out.println("*** assert with groups. Testing that " + buddyLocalAddress + " is a buddy for owner " + dataOwnerLocalAddress + " only buddy? " + onlyBuddy);
|
293 |
50
| printBuddyGroup(dataOwner);
|
294 |
| |
295 |
50
| BuddyManager dataOwnerBuddyManager = dataOwner.getBuddyManager();
|
296 |
50
| BuddyManager buddyBuddyManager = buddy.getBuddyManager();
|
297 |
| |
298 |
| |
299 |
19
| if (onlyBuddy) assertEquals("Should only have one buddy", 1, dataOwnerBuddyManager.getBuddyAddresses().size());
|
300 |
| |
301 |
50
| assertTrue(buddyLocalAddress + " should be a buddy to " + dataOwnerLocalAddress, dataOwnerBuddyManager.getBuddyAddresses().contains(buddyLocalAddress));
|
302 |
| |
303 |
| |
304 |
50
| BuddyGroup group = buddyBuddyManager.buddyGroupsIParticipateIn.get(BuddyManager.getGroupNameFromAddress(dataOwnerLocalAddress));
|
305 |
50
| System.out.println("*** Buddy's version of dataOwner's group " + group);
|
306 |
| |
307 |
50
| assertTrue("buddy's list of groups it participates in should contain data owner's group name", buddyBuddyManager.buddyGroupsIParticipateIn.containsKey(BuddyManager.getGroupNameFromAddress(dataOwnerLocalAddress)));
|
308 |
19
| if (onlyBuddy) assertEquals(1, group.getBuddies().size());
|
309 |
49
| assertTrue(buddyLocalAddress + " should be a buddy to " + group.getGroupName(), group.getBuddies().contains(buddyLocalAddress));
|
310 |
| } |
311 |
| |
312 |
48
| protected void dumpCacheContents(CacheImpl... caches)
|
313 |
| { |
314 |
48
| System.out.println("**** START: Cache Contents ****");
|
315 |
48
| for (int i = 0; i < caches.length; i++)
|
316 |
| { |
317 |
144
| if (caches[i] == null)
|
318 |
| { |
319 |
3
| System.out.println(" ** Cache " + i + " is null!");
|
320 |
| } |
321 |
| else |
322 |
| { |
323 |
141
| System.out.println(" ** Cache " + i + " is " + caches[i].getLocalAddress());
|
324 |
141
| System.out.println(" " + caches[i].printLockInfo());
|
325 |
| } |
326 |
| } |
327 |
48
| System.out.println("**** END: Cache Contents ****");
|
328 |
| } |
329 |
| |
330 |
24
| protected void assertNoLocks(CacheImpl[] caches)
|
331 |
| { |
332 |
24
| for (CacheImpl cache : caches)
|
333 |
| { |
334 |
72
| if (cache != null) assertEquals(0, cache.getNumberOfLocksHeld());
|
335 |
| } |
336 |
| } |
337 |
| |
338 |
| } |