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 org.junit.After;
27 import static org.junit.Assert.assertEquals;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 import java.util.Date;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 /***
36 * @author Greg Luck
37 * @version $Id: RMIBootstrapCacheLoaderTest.java 2154 2010-04-06 02:45:52Z cdennis $
38 */
39 public class RMIBootstrapCacheLoaderTest {
40
41
42 /***
43 * A value to represent replicate asynchronously
44 */
45 protected static final boolean ASYNCHRONOUS = true;
46
47 /***
48 * A value to represent replicate synchronously
49 */
50 protected static final boolean SYNCHRONOUS = false;
51
52 private static final Logger LOG = LoggerFactory.getLogger(RMIBootstrapCacheLoaderTest.class.getName());
53
54 /***
55 * CacheManager 1 in the cluster
56 */
57 protected CacheManager manager1;
58 /***
59 * CacheManager 2 in the cluster
60 */
61 protected CacheManager manager2;
62 /***
63 * CacheManager 3 in the cluster
64 */
65 protected CacheManager manager3;
66 /***
67 * CacheManager 4 in the cluster
68 */
69 protected CacheManager manager4;
70 /***
71 * CacheManager 5 in the cluster
72 */
73 protected CacheManager manager5;
74 /***
75 * CacheManager 6 in the cluster
76 */
77 protected CacheManager manager6;
78
79 /***
80 * The name of the cache under test
81 */
82 protected String cacheName = "sampleCache1";
83
84 /***
85 * {@inheritDoc}
86 * Sets up two caches: cache1 is local. cache2 is to be receive updates
87 *
88 * @throws Exception
89 */
90 @Before
91 public void setUp() throws Exception {
92
93 MulticastKeepaliveHeartbeatSender.setHeartBeatInterval(1000);
94
95 CountingCacheEventListener.resetCounters();
96 manager1 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed1.xml");
97 manager2 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed2.xml");
98
99
100 Thread.sleep(3000);
101 }
102
103 /***
104 * Force the VM to grow to its full size. This stops SoftReferences from being reclaimed in favour of
105 * Heap growth. Only an issue when a VM is cold.
106 */
107 protected void forceVMGrowth() {
108 byte[] forceVMGrowth = new byte[40000000];
109 }
110
111
112 /***
113 * {@inheritDoc}
114 *
115 * @throws Exception
116 */
117 @After
118 public void tearDown() throws Exception {
119
120 if (manager1 != null) {
121 manager1.shutdown();
122 }
123 if (manager2 != null) {
124 manager2.shutdown();
125 }
126 if (manager3 != null) {
127 manager3.shutdown();
128 }
129 if (manager4 != null) {
130 manager4.shutdown();
131 }
132 if (manager5 != null) {
133 manager5.shutdown();
134 }
135 if (manager6 != null) {
136 manager6.shutdown();
137 }
138 }
139
140 /***
141 * Tests loading from bootstrap
142 */
143 @Test
144 public void testBootstrapFromClusterWithAsyncLoader() throws CacheException, InterruptedException {
145
146 forceVMGrowth();
147
148
149 Integer index = null;
150 for (int i = 0; i < 2; i++) {
151 for (int j = 0; j < 1000; j++) {
152 index = Integer.valueOf(((1000 * i) + j));
153 manager2.getCache("sampleCache1").put(new Element(index,
154 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
155 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
156 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
157 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
158 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
159 }
160
161 }
162 assertEquals(2000, manager2.getCache("sampleCache1").getSize());
163
164 Thread.sleep(8000);
165 assertEquals(2000, manager1.getCache("sampleCache1").getSize());
166
167 manager3 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed3.xml");
168 Thread.sleep(5000);
169 assertEquals(2000, manager3.getCache("sampleCache1").getSize());
170
171
172 }
173
174 /***
175 * Tests loading from bootstrap
176 */
177 @Test
178 public void testBootstrapFromClusterWithSyncLoader() throws CacheException, InterruptedException {
179
180 forceVMGrowth();
181
182
183 Integer index = null;
184 for (int i = 0; i < 2; i++) {
185 for (int j = 0; j < 1000; j++) {
186 index = Integer.valueOf(((1000 * i) + j));
187 manager2.getCache("sampleCache2").put(new Element(index,
188 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
189 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
190 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
191 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
192 + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
193 }
194
195 }
196
197 assertEquals(2000, manager2.getCache("sampleCache2").getSize());
198
199 Thread.sleep(8000);
200 assertEquals(2000, manager1.getCache("sampleCache2").getSize());
201
202 manager3 = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "distribution/ehcache-distributed3.xml");
203
204
205 assertEquals(2000, manager3.getCache("sampleCache2").getSize());
206
207
208 }
209
210
211 /***
212 * Create the same named cache in two CacheManagers. Populate the first one. Check that the second one gets the
213 * entries.
214 */
215 @Test
216 public void testAddCacheAndBootstrapOccurs() throws InterruptedException {
217
218 manager1.addCache("testBootstrap1");
219 Cache testBootstrap1 = manager1.getCache("testBootstrap1");
220 for (int i = 0; i < 1000; i++) {
221 testBootstrap1.put(new Element("key" + i, new Date()));
222 }
223
224 manager2.addCache("testBootstrap1");
225 Cache testBootstrap2 = manager2.getCache("testBootstrap1");
226
227 Thread.sleep(3000);
228 assertEquals(1000, testBootstrap2.getSize());
229
230
231 }
232
233
234 }