1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| package org.jboss.cache.buddyreplication; |
8 |
| |
9 |
| import org.jboss.cache.Cache; |
10 |
| import org.jboss.cache.CacheImpl; |
11 |
| import org.jboss.cache.CacheSPI; |
12 |
| import org.jboss.cache.DefaultCacheFactory; |
13 |
| import org.jboss.cache.Fqn; |
14 |
| import org.jboss.cache.Region; |
15 |
| import org.jboss.cache.config.BuddyReplicationConfig; |
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.misc.TestingUtil; |
21 |
| import org.jboss.cache.util.CachePrinter; |
22 |
| import org.w3c.dom.Document; |
23 |
| import org.w3c.dom.Element; |
24 |
| |
25 |
| import javax.xml.parsers.DocumentBuilder; |
26 |
| import javax.xml.parsers.DocumentBuilderFactory; |
27 |
| import java.util.HashMap; |
28 |
| import java.util.Map; |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| public class BuddyBackupActivationInactivationTest extends BuddyReplicationTestsBase |
37 |
| { |
38 |
| public static final Fqn A = Fqn.fromString("/a"); |
39 |
| public static final Fqn A_B = Fqn.fromString("/a/b"); |
40 |
| public static final String JOE = "JOE"; |
41 |
| |
42 |
| protected Map<String, Cache> caches; |
43 |
| private ClassLoader orig_TCL; |
44 |
| |
45 |
2
| public void testBuddyBackupActivation() throws Exception
|
46 |
| { |
47 |
2
| CacheSPI cache1 = createCache("cache1", true, true, true);
|
48 |
2
| CacheSPI cache2 = createCache("cache2", true, true, true);
|
49 |
2
| Fqn A = Fqn.fromString("/a");
|
50 |
2
| TestingUtil.blockUntilViewsReceived(VIEW_BLOCK_TIMEOUT, cache1, cache2);
|
51 |
| |
52 |
| |
53 |
2
| Region c1 = cache1.getRegionManager().getRegion(A, Region.Type.MARSHALLING, true);
|
54 |
2
| Region c2 = cache2.getRegionManager().getRegion(A, Region.Type.MARSHALLING, true);
|
55 |
| |
56 |
2
| assertFalse(c1.isActive());
|
57 |
2
| assertFalse(c2.isActive());
|
58 |
| |
59 |
2
| c1.activate();
|
60 |
2
| cache1.put(A_B, "name", JOE);
|
61 |
| |
62 |
2
| TestingUtil.sleepThread(getSleepTimeout());
|
63 |
| |
64 |
2
| System.out.println("Cache dump BEFORE activation");
|
65 |
2
| System.out.println("cache1 " + CachePrinter.printCacheDetails(cache1));
|
66 |
2
| System.out.println("cache2 " + CachePrinter.printCacheDetails(cache2));
|
67 |
| |
68 |
2
| c2.activate();
|
69 |
| |
70 |
2
| System.out.println("Cache dump AFTER activation");
|
71 |
2
| System.out.println("cache1 " + CachePrinter.printCacheDetails(cache1));
|
72 |
2
| System.out.println("cache2 " + CachePrinter.printCacheDetails(cache2));
|
73 |
| |
74 |
2
| Fqn fqn = BuddyManager.getBackupFqn(cache1.getLocalAddress(), A_B);
|
75 |
| |
76 |
2
| assertEquals("State transferred with activation", JOE, cache2.get(fqn, "name"));
|
77 |
| } |
78 |
| |
79 |
2
| public void testReplToInactiveRegion() throws Exception
|
80 |
| { |
81 |
2
| CacheSPI cache1 = createCache("cache1", true, true, true);
|
82 |
2
| CacheSPI cache2 = createCache("cache2", true, true, true);
|
83 |
| |
84 |
2
| TestingUtil.blockUntilViewsReceived(VIEW_BLOCK_TIMEOUT, cache1, cache2);
|
85 |
2
| Fqn backupFqn = BuddyManager.getBackupFqn(cache1.getLocalAddress(), A_B);
|
86 |
2
| Fqn A = Fqn.fromString("/a");
|
87 |
| |
88 |
2
| Region regionA = cache1.getRegion(A, true);
|
89 |
2
| regionA.registerContextClassLoader(getClass().getClassLoader());
|
90 |
2
| regionA.activate();
|
91 |
| |
92 |
| |
93 |
| |
94 |
2
| cache2.getRegionManager().activate(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN);
|
95 |
2
| cache2.getRegionManager().deactivate(A);
|
96 |
| |
97 |
2
| cache1.put(A_B, "name", JOE);
|
98 |
| |
99 |
2
| TestingUtil.sleepThread(getSleepTimeout());
|
100 |
2
| assertNull("Should be no replication to inactive region", cache2.get(A_B, "name"));
|
101 |
| |
102 |
2
| assertNull("Should be no replication to inactive backup region", cache2.get(backupFqn, "name"));
|
103 |
| } |
104 |
| |
105 |
2
| public void testBuddyBackupInactivation() throws Exception
|
106 |
| { |
107 |
2
| CacheImpl cache1 = createCache("cache1", true, true, true);
|
108 |
2
| Fqn A = Fqn.fromString("/a");
|
109 |
2
| Region regionA = cache1.getRegion(A, true);
|
110 |
2
| regionA.registerContextClassLoader(getClass().getClassLoader());
|
111 |
2
| regionA.activate();
|
112 |
| |
113 |
2
| Fqn fqn = new Fqn(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN, "test");
|
114 |
2
| fqn = new Fqn(fqn, A_B);
|
115 |
2
| cache1.getInvocationContext().getOptionOverrides().setCacheModeLocal(true);
|
116 |
2
| cache1.put(fqn, "name", JOE);
|
117 |
| |
118 |
2
| assertEquals("Put should have been OK", JOE, cache1.get(fqn, "name"));
|
119 |
| |
120 |
2
| regionA.deactivate();
|
121 |
| |
122 |
2
| assertNull("Inactivation should have cleared region", cache1.get(fqn, "name"));
|
123 |
| } |
124 |
| |
125 |
10
| protected CacheImpl createCache(String cacheID,
|
126 |
| boolean sync, |
127 |
| boolean useMarshalling, |
128 |
| boolean startCache) |
129 |
| throws Exception |
130 |
| { |
131 |
10
| if (caches.get(cacheID) != null)
|
132 |
| { |
133 |
0
| throw new IllegalStateException(cacheID + " already created");
|
134 |
| } |
135 |
| |
136 |
10
| CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
137 |
| |
138 |
10
| CacheMode mode = sync ? CacheMode.REPL_SYNC : CacheMode.REPL_ASYNC;
|
139 |
10
| Configuration c = UnitTestCacheConfigurationFactory.createConfiguration(mode);
|
140 |
| |
141 |
10
| cache.setConfiguration(c);
|
142 |
10
| cache.getConfiguration().setClusterName("TestCluster");
|
143 |
10
| if (useMarshalling)
|
144 |
| { |
145 |
10
| cache.getConfiguration().setUseRegionBasedMarshalling(true);
|
146 |
10
| cache.getConfiguration().setInactiveOnStartup(true);
|
147 |
| } |
148 |
10
| cache.getConfiguration().setBuddyReplicationConfig(getBuddyConfig());
|
149 |
| |
150 |
| |
151 |
10
| caches.put(cacheID, cache);
|
152 |
| |
153 |
10
| if (startCache)
|
154 |
| { |
155 |
10
| cache.create();
|
156 |
10
| cache.start();
|
157 |
| } |
158 |
| |
159 |
10
| return cache;
|
160 |
| } |
161 |
| |
162 |
6
| protected void setUp() throws Exception
|
163 |
| { |
164 |
6
| super.setUp();
|
165 |
| |
166 |
6
| caches = new HashMap<String, Cache>();
|
167 |
| |
168 |
| |
169 |
6
| orig_TCL = Thread.currentThread().getContextClassLoader();
|
170 |
| } |
171 |
| |
172 |
6
| protected void tearDown() throws Exception
|
173 |
| { |
174 |
6
| super.tearDown();
|
175 |
| |
176 |
| |
177 |
6
| Thread.currentThread().setContextClassLoader(orig_TCL);
|
178 |
| |
179 |
6
| for (String cacheID : caches.keySet())
|
180 |
| { |
181 |
10
| stopCache(caches.get(cacheID));
|
182 |
| } |
183 |
| } |
184 |
| |
185 |
10
| protected void stopCache(Cache cache)
|
186 |
| { |
187 |
10
| if (cache != null)
|
188 |
| { |
189 |
10
| try
|
190 |
| { |
191 |
10
| cache.stop();
|
192 |
10
| cache.destroy();
|
193 |
| } |
194 |
| catch (Exception e) |
195 |
| { |
196 |
0
| System.out.println("Exception stopping cache " + e.getMessage());
|
197 |
0
| e.printStackTrace(System.out);
|
198 |
| } |
199 |
| } |
200 |
| } |
201 |
| |
202 |
10
| private BuddyReplicationConfig getBuddyConfig() throws Exception
|
203 |
| { |
204 |
| |
205 |
| |
206 |
10
| DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
|
207 |
10
| DocumentBuilder db = dbf.newDocumentBuilder();
|
208 |
10
| Document doc = db.newDocument();
|
209 |
10
| Element config = doc.createElement("config");
|
210 |
10
| doc.appendChild(config);
|
211 |
10
| Element replEnabled = doc.createElement("buddyReplicationEnabled");
|
212 |
10
| replEnabled.appendChild(doc.createTextNode("true"));
|
213 |
10
| config.appendChild(replEnabled);
|
214 |
10
| Element gravDisabled = doc.createElement("autoDataGravitation");
|
215 |
10
| gravDisabled.appendChild(doc.createTextNode("false"));
|
216 |
10
| config.appendChild(gravDisabled);
|
217 |
| |
218 |
10
| return XmlConfigurationParser.parseBuddyReplicationConfig(config);
|
219 |
| } |
220 |
| } |