PageRenderTime 71ms CodeModel.GetById 12ms app.highlight 54ms RepoModel.GetById 1ms app.codeStats 1ms

/hazelcast/src/test/java/com/hazelcast/impl/WanReplicationTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 309 lines | 269 code | 25 blank | 15 comment | 9 complexity | afdaf9d2dbb2f54c5c8192f7098ca1c2 MD5 | raw file
  1/*
  2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  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
 17package com.hazelcast.impl;
 18
 19import com.hazelcast.config.Config;
 20import com.hazelcast.config.WanReplicationConfig;
 21import com.hazelcast.config.WanReplicationRef;
 22import com.hazelcast.config.WanTargetClusterConfig;
 23import com.hazelcast.core.Hazelcast;
 24import com.hazelcast.core.HazelcastInstance;
 25import com.hazelcast.impl.wan.WanMergeListener;
 26import com.hazelcast.merge.PassThroughMergePolicy;
 27import org.junit.After;
 28import org.junit.Before;
 29import org.junit.BeforeClass;
 30import org.junit.Test;
 31import org.junit.runner.RunWith;
 32
 33import java.util.concurrent.CountDownLatch;
 34import java.util.concurrent.TimeUnit;
 35import java.util.concurrent.atomic.AtomicInteger;
 36import java.util.concurrent.atomic.AtomicReference;
 37
 38import static com.hazelcast.impl.TestUtil.getConcurrentMapManager;
 39import static junit.framework.Assert.assertEquals;
 40import static junit.framework.Assert.assertTrue;
 41
 42@RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
 43public class WanReplicationTest {
 44
 45    @BeforeClass
 46    public static void init() throws Exception {
 47        System.setProperty(GroupProperties.PROP_WAIT_SECONDS_BEFORE_JOIN, "1");
 48        System.setProperty(GroupProperties.PROP_VERSION_CHECK_ENABLED, "false");
 49        Hazelcast.shutdownAll();
 50    }
 51
 52    @After
 53    @Before
 54    public void cleanup() throws Exception {
 55        Hazelcast.shutdownAll();
 56    }
 57
 58    @Test
 59    public void testWANClustering() throws Exception {
 60        Config c1 = new Config();
 61        Config c2 = new Config();
 62        c1.getGroupConfig().setName("newyork");
 63        c1.addWanReplicationConfig(new WanReplicationConfig()
 64                .setName("my-wan")
 65                .addTargetClusterConfig(new WanTargetClusterConfig()
 66                        .addEndpoint("127.0.0.1:5702").setGroupName("london")));
 67        c1.getMapConfig("default").setWanReplicationRef(new WanReplicationRef()
 68                .setName("my-wan")
 69                .setMergePolicy(PassThroughMergePolicy.NAME));
 70        c2.getGroupConfig().setName("london");
 71        c2.addWanReplicationConfig(new WanReplicationConfig()
 72                .setName("my-wan")
 73                .addTargetClusterConfig(new WanTargetClusterConfig()
 74                        .addEndpoint("127.0.0.1:5701").setGroupName("newyork")));
 75        c2.getMapConfig("default").setWanReplicationRef(new WanReplicationRef()
 76                .setName("my-wan")
 77                .setMergePolicy(PassThroughMergePolicy.NAME));
 78        HazelcastInstance h1 = Hazelcast.newHazelcastInstance(c1);
 79        HazelcastInstance h2 = Hazelcast.newHazelcastInstance(c2);
 80        HazelcastInstance h12 = Hazelcast.newHazelcastInstance(c1);
 81        HazelcastInstance h13 = Hazelcast.newHazelcastInstance(c1);
 82        HazelcastInstance h22 = Hazelcast.newHazelcastInstance(c2);
 83        int size = 100;
 84        MergeLatch mergeLatch1 = new MergeLatch(2 * size);
 85        MergeLatch mergeLatch2 = new MergeLatch(size);
 86        getConcurrentMapManager(h1).addWanMergeListener(mergeLatch1);
 87        getConcurrentMapManager(h12).addWanMergeListener(mergeLatch1);
 88        getConcurrentMapManager(h13).addWanMergeListener(mergeLatch1);
 89        getConcurrentMapManager(h2).addWanMergeListener(mergeLatch2);
 90        getConcurrentMapManager(h22).addWanMergeListener(mergeLatch2);
 91        for (int i = 0; i < size; i++) {
 92            h2.getMap("default").put(i, "value" + i);
 93            h22.getMap("default").put(size + i, "value" + (size + i));
 94        }
 95        assertTrue("Latch state 1: " + mergeLatch1, mergeLatch1.await(60, TimeUnit.SECONDS));
 96        Thread.sleep(5000);
 97        assertEquals(0, mergeLatch2.totalOperations());
 98        assertEquals(2 * size, mergeLatch1.getUpdateCount());
 99        assertEquals(2 * size, mergeLatch1.totalOperations());
100        assertEquals(2 * size, h2.getMap("default").size());
101        assertEquals(2 * size, h1.getMap("default").size());
102        assertEquals(2 * size, h12.getMap("default").size());
103        assertEquals(2 * size, h13.getMap("default").size());
104        assertEquals(2 * size, h22.getMap("default").size());
105        mergeLatch1.reset();
106        for (int i = 0; i < size / 2; i++) {
107            h1.getMap("default").remove(i);
108            h13.getMap("default").remove(size + i);
109        }
110        assertTrue("Latch state 2: " + mergeLatch2, mergeLatch2.await(60, TimeUnit.SECONDS));
111        Thread.sleep(5000);
112        assertEquals(size, mergeLatch2.getRemoveCount());
113        assertEquals(size, mergeLatch2.totalOperations());
114        assertEquals(0, mergeLatch1.totalOperations());
115        assertEquals(size, h1.getMap("default").size());
116        assertEquals(size, h2.getMap("default").size());
117        assertEquals(size, h12.getMap("default").size());
118        assertEquals(size, h13.getMap("default").size());
119        assertEquals(size, h22.getMap("default").size());
120    }
121
122    @Test
123    public void testWANClustering2() throws Exception {
124        Config c1 = new Config();
125        Config c2 = new Config();
126        c1.getGroupConfig().setName("newyork");
127        c1.addWanReplicationConfig(new WanReplicationConfig()
128                .setName("my-wan")
129                .addTargetClusterConfig(new WanTargetClusterConfig()
130                        .addEndpoint("127.0.0.1:5703").setGroupName("london")));
131        c1.getMapConfig("default").setWanReplicationRef(new WanReplicationRef()
132                .setName("my-wan")
133                .setMergePolicy(PassThroughMergePolicy.NAME));
134        c2.getGroupConfig().setName("london");
135        c2.addWanReplicationConfig(new WanReplicationConfig()
136                .setName("my-wan")
137                .addTargetClusterConfig(new WanTargetClusterConfig()
138                        .addEndpoint("127.0.0.1:5701").setGroupName("newyork")));
139        c2.getMapConfig("default").setWanReplicationRef(new WanReplicationRef()
140                .setName("my-wan")
141                .setMergePolicy(PassThroughMergePolicy.NAME));
142        HazelcastInstance h10 = Hazelcast.newHazelcastInstance(c1);
143        HazelcastInstance h11 = Hazelcast.newHazelcastInstance(c1);
144        int size = 1000;
145        HazelcastInstance h20 = Hazelcast.newHazelcastInstance(c2);
146        HazelcastInstance h21 = Hazelcast.newHazelcastInstance(c2);
147        HazelcastInstance h12 = Hazelcast.newHazelcastInstance(c1);
148        MergeLatch mergeLatch1 = new MergeLatch(size);
149        getConcurrentMapManager(h10).addWanMergeListener(mergeLatch1);
150        getConcurrentMapManager(h11).addWanMergeListener(mergeLatch1);
151        getConcurrentMapManager(h12).addWanMergeListener(mergeLatch1);
152        MergeLatch mergeLatch2 = new MergeLatch(size);
153        getConcurrentMapManager(h20).addWanMergeListener(mergeLatch2);
154        getConcurrentMapManager(h21).addWanMergeListener(mergeLatch2);
155        for (int i = 0; i < size; i++) {
156            h11.getMap("default").put(i, "value" + i);
157        }
158        assertTrue("Latch state: " + mergeLatch2, mergeLatch2.await(60, TimeUnit.SECONDS));
159        Thread.sleep(5000);
160        assertEquals(size, mergeLatch2.totalOperations());
161        assertEquals(0, mergeLatch1.totalOperations());
162        assertEquals(size, h10.getMap("default").size());
163        assertEquals(size, h20.getMap("default").size());
164        assertEquals(size, h12.getMap("default").size());
165        assertEquals(size, h11.getMap("default").size());
166        assertEquals(size, h21.getMap("default").size());
167        mergeLatch2.reset();
168        for (int i = 0; i < size; i++) {
169            h21.getMap("default").put(size + i, "value" + (size + i));
170        }
171        assertTrue("Latch state: " + mergeLatch1, mergeLatch1.await(60, TimeUnit.SECONDS));
172        Thread.sleep(5000);
173        assertEquals(size, mergeLatch1.totalOperations());
174        assertEquals(0, mergeLatch2.totalOperations());
175        assertEquals(2 * size, h10.getMap("default").size());
176        assertEquals(2 * size, h20.getMap("default").size());
177        assertEquals(2 * size, h12.getMap("default").size());
178        assertEquals(2 * size, h11.getMap("default").size());
179        assertEquals(2 * size, h21.getMap("default").size());
180        mergeLatch1.reset(size / 2);
181        mergeLatch2.reset(size / 2);
182        for (int i = 0; i < size / 2; i++) {
183            h10.getMap("default").remove(i);
184            h21.getMap("default").remove(size + i);
185        }
186        assertTrue("Latch state: " + mergeLatch1, mergeLatch1.await(60, TimeUnit.SECONDS));
187        assertTrue("Latch state: " + mergeLatch2, mergeLatch2.await(60, TimeUnit.SECONDS));
188        Thread.sleep(5000);
189        assertEquals(size / 2, mergeLatch1.totalOperations());
190        assertEquals(size / 2, mergeLatch2.totalOperations());
191        assertEquals(size, h10.getMap("default").size());
192        assertEquals(size, h20.getMap("default").size());
193        assertEquals(size, h12.getMap("default").size());
194        assertEquals(size, h11.getMap("default").size());
195        assertEquals(size, h21.getMap("default").size());
196    }
197
198    @Test
199    public void testWANClusteringActivePassive() throws Exception {
200        Config c1 = new Config();
201        Config c2 = new Config();
202        c1.getGroupConfig().setName("newyork");
203        c1.addWanReplicationConfig(new WanReplicationConfig()
204                .setName("my-wan")
205                .addTargetClusterConfig(new WanTargetClusterConfig()
206                        .addEndpoint("127.0.0.1:5702").setGroupName("london")));
207        c1.getMapConfig("default").setWanReplicationRef(new WanReplicationRef()
208                .setName("my-wan")
209                .setMergePolicy(PassThroughMergePolicy.NAME));
210        c2.getGroupConfig().setName("london");
211        c2.getMapConfig("default").setWanReplicationRef(new WanReplicationRef()
212                .setName("my-wan")
213                .setMergePolicy(PassThroughMergePolicy.NAME));
214        HazelcastInstance h10 = Hazelcast.newHazelcastInstance(c1);
215        HazelcastInstance h20 = Hazelcast.newHazelcastInstance(c2);
216        int size = 1000;
217        MergeLatch mergeLatch2 = new MergeLatch(size);
218        getConcurrentMapManager(h20).addWanMergeListener(mergeLatch2);
219        for (int i = 0; i < size; i++) {
220            h10.getMap("default").put(i, "value" + i);
221        }
222        assertTrue("Latch state: " + mergeLatch2, mergeLatch2.await(60, TimeUnit.SECONDS));
223        Thread.sleep(5000);
224        assertEquals(size, mergeLatch2.totalOperations());
225        assertEquals(size, h10.getMap("default").size());
226        assertEquals(size, h20.getMap("default").size());
227        for (int i = 0; i < size; i++) {
228            assertEquals("value" + i, h20.getMap("default").get(i));
229        }
230    }
231
232    class MergeLatch implements WanMergeListener {
233        final AtomicInteger removeCount = new AtomicInteger();
234        final AtomicInteger updateCount = new AtomicInteger();
235        final AtomicInteger ignoreCount = new AtomicInteger();
236        final AtomicReference<CountDownLatch> latch;
237
238        MergeLatch(int count) {
239            latch = new AtomicReference<CountDownLatch>(new CountDownLatch(count));
240        }
241
242        public long getCount() {
243            return latch.get().getCount();
244        }
245
246        public void countDown() {
247            CountDownLatch l = latch.get();
248            if (l != null) {
249                l.countDown();
250            }
251        }
252
253        public void entryRemoved() {
254            removeCount.incrementAndGet();
255            countDown();
256        }
257
258        public void entryUpdated() {
259            updateCount.incrementAndGet();
260            countDown();
261        }
262
263        public void entryIgnored() {
264            ignoreCount.incrementAndGet();
265            countDown();
266        }
267
268        public int totalOperations() {
269            return getIgnoreCount() + getRemoveCount() + getUpdateCount();
270        }
271
272        public int getRemoveCount() {
273            return removeCount.get();
274        }
275
276        public int getUpdateCount() {
277            return updateCount.get();
278        }
279
280        public int getIgnoreCount() {
281            return ignoreCount.get();
282        }
283
284        public void reset() {
285            removeCount.set(0);
286            updateCount.set(0);
287            ignoreCount.set(0);
288        }
289
290        public void reset(int count) {
291            latch.set(new CountDownLatch(count));
292            reset();
293        }
294
295        public boolean await(int time, TimeUnit timeUnit) throws InterruptedException {
296            return latch.get().await(time, timeUnit);
297        }
298
299        @Override
300        public String toString() {
301            return "MergeLatch{" +
302                    "count=" + getCount() +
303                    ", removeCount=" + removeCount.get() +
304                    ", updateCount=" + updateCount.get() +
305                    ", ignoreCount=" + ignoreCount.get() +
306                    '}';
307        }
308    }
309}