1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.distribution;
18
19
20 import net.sf.ehcache.AbstractCacheTest;
21 import net.sf.ehcache.Cache;
22 import net.sf.ehcache.CacheException;
23 import net.sf.ehcache.CacheManager;
24 import net.sf.ehcache.Element;
25 import net.sf.ehcache.event.CountingCacheEventListener;
26 import net.sf.ehcache.util.RetryAssert;
27
28 import org.hamcrest.collection.IsEmptyCollection;
29 import org.junit.After;
30
31 import static org.junit.Assert.assertEquals;
32
33 import org.junit.Before;
34 import org.junit.Test;
35
36 import java.util.Collections;
37 import java.util.Date;
38 import java.util.Set;
39 import java.util.concurrent.Callable;
40 import java.util.concurrent.TimeUnit;
41
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /***
46 * @author Greg Luck
47 * @version $Id: RMIBootstrapCacheLoaderTest.html 13146 2011-08-01 17:12:39Z oletizi $
48 */
49 public class RMIBootstrapCacheLoaderTest extends AbstractRMITest {
50
51
52 /***
53 * A value to represent replicate asynchronously
54 */
55 protected static final boolean ASYNCHRONOUS = true;
56
57 /***
58 * A value to represent replicate synchronously
59 */
60 protected static final boolean SYNCHRONOUS = false;
61
62 private static final Logger LOG = LoggerFactory.getLogger(RMIBootstrapCacheLoaderTest.class.getName());
63
64 /***
65 * CacheManager 1 in the cluster
66 */
67 protected CacheManager manager1;
68 /***
69 * CacheManager 2 in the cluster
70 */
71 protected CacheManager manager2;
72 /***
73 * CacheManager 3 in the cluster
74 */
75 protected CacheManager manager3;
76 /***
77 * CacheManager 4 in the cluster
78 */
79 protected CacheManager manager4;
80 /***
81 * CacheManager 5 in the cluster
82 */
83 protected CacheManager manager5;
84 /***
85 * CacheManager 6 in the cluster
86 */
87 protected CacheManager manager6;
88
89 /***
90 * The name of the cache under test
91 */
92 protected String cacheName = "sampleCache1";
93
94 /***
95 * {@inheritDoc}
96 * Sets up two caches: cache1 is local. cache2 is to be receive updates
97 *
98 * @throws Exception
99 */
100 @Before
101 public void setUp() throws Exception {
102
103 MulticastKeepaliveHeartbeatSender.setHeartBeatInterval(1000);
104
105 CountingCacheEventListener.resetCounters();
106 manager1 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed1.xml");
107 manager2 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed2.xml");
108
109
110 waitForClusterMembership(10, TimeUnit.SECONDS, Collections.singleton("sampleCache1"), manager1, manager2);
111 }
112
113 /***
114 * Force the VM to grow to its full size. This stops SoftReferences from being reclaimed in favour of
115 * Heap growth. Only an issue when a VM is cold.
116 */
117 protected void forceVMGrowth() {
118 try {
119 Object[] arrays = new Object[40];
120 for (int i = 0; i < arrays.length; i++) {
121 arrays[i] = new byte[1024 * 1024];
122 }
123 } catch (OutOfMemoryError e) {
124
125 }
126 }
127
128
129 /***
130 * {@inheritDoc}
131 *
132 * @throws Exception
133 */
134 @After
135 public void tearDown() throws Exception {
136
137 if (manager1 != null) {
138 manager1.shutdown();
139 }
140 if (manager2 != null) {
141 manager2.shutdown();
142 }
143 if (manager3 != null) {
144 manager3.shutdown();
145 }
146 if (manager4 != null) {
147 manager4.shutdown();
148 }
149 if (manager5 != null) {
150 manager5.shutdown();
151 }
152 if (manager6 != null) {
153 manager6.shutdown();
154 }
155
156 RetryAssert.assertBy(30, TimeUnit.SECONDS, new Callable<Set<Thread>>() {
157 public Set<Thread> call() throws Exception {
158 return getActiveReplicationThreads();
159 }
160 }, IsEmptyCollection.<Thread>empty());
161 }
162
163 /***
164 * Tests loading from bootstrap
165 */
166 @Test
167 public void testBootstrapFromClusterWithAsyncLoader() throws CacheException, InterruptedException {
168
169 forceVMGrowth();
170
171
172 Integer index = null;
173 for (int i = 0; i < 2; i++) {
174 for (int j = 0; j < 1000; j++) {
175 index = Integer.valueOf(((1000 * i) + j));
176 manager2.getCache("sampleCache1").put(new Element(index,
177 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
178 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
179 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
180 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
181 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
182 }
183
184 }
185 assertEquals(2000, manager2.getCache("sampleCache1").getSize());
186
187 Thread.sleep(8000);
188 assertEquals(2000, manager1.getCache("sampleCache1").getSize());
189
190 manager3 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed3.xml");
191 Thread.sleep(5000);
192 assertEquals(2000, manager3.getCache("sampleCache1").getSize());
193
194
195 }
196
197 /***
198 * Tests loading from bootstrap
199 */
200 @Test
201 public void testBootstrapFromClusterWithSyncLoader() throws CacheException, InterruptedException {
202
203 forceVMGrowth();
204
205
206 Integer index = null;
207 for (int i = 0; i < 2; i++) {
208 for (int j = 0; j < 1000; j++) {
209 index = Integer.valueOf(((1000 * i) + j));
210 manager2.getCache("sampleCache2").put(new Element(index,
211 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
212 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
213 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
214 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
215 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
216 }
217
218 }
219
220 assertEquals(2000, manager2.getCache("sampleCache2").getSize());
221
222 Thread.sleep(8000);
223 assertEquals(2000, manager1.getCache("sampleCache2").getSize());
224
225 manager3 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed3.xml");
226
227
228 assertEquals(2000, manager3.getCache("sampleCache2").getSize());
229
230
231 }
232
233
234 /***
235 * Create the same named cache in two CacheManagers. Populate the first one. Check that the second one gets the
236 * entries.
237 */
238 @Test
239 public void testAddCacheAndBootstrapOccurs() throws InterruptedException {
240
241 manager1.addCache("testBootstrap1");
242 Cache testBootstrap1 = manager1.getCache("testBootstrap1");
243 for (int i = 0; i < 1000; i++) {
244 testBootstrap1.put(new Element("key" + i, new Date()));
245 }
246
247 manager2.addCache("testBootstrap1");
248 Cache testBootstrap2 = manager2.getCache("testBootstrap1");
249
250 Thread.sleep(3000);
251 assertEquals(1000, testBootstrap2.getSize());
252
253
254 }
255
256
257 }