Пример #1
0
  @Test
  public void testLockConditionSignalAllShutDownKeyOwner() throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final Config config = new Config();
    final String name = "testLockConditionSignalAllShutDownKeyOwner";
    final HazelcastInstance instance = nodeFactory.newHazelcastInstance(config);
    final AtomicInteger count = new AtomicInteger(0);
    final int size = 50;
    int k = 0;
    final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance(config);
    while (!keyOwner
        .getCluster()
        .getLocalMember()
        .equals(instance.getPartitionService().getPartition(++k).getOwner())) {
      Thread.sleep(10);
    }

    final ILock lock = instance.getLock(k);
    final ICondition condition = lock.newCondition(name);

    final CountDownLatch awaitLatch = new CountDownLatch(size);
    final CountDownLatch finalLatch = new CountDownLatch(size);
    for (int i = 0; i < size; i++) {
      new Thread(
              new Runnable() {
                public void run() {
                  lock.lock();
                  try {
                    awaitLatch.countDown();
                    condition.await();
                    Thread.sleep(5);
                    if (lock.isLockedByCurrentThread()) {
                      count.incrementAndGet();
                    }
                  } catch (InterruptedException ignored) {
                  } finally {
                    lock.unlock();
                    finalLatch.countDown();
                  }
                }
              })
          .start();
    }

    final ILock lock1 = keyOwner.getLock(k);
    final ICondition condition1 = lock1.newCondition(name);
    awaitLatch.await(1, TimeUnit.MINUTES);
    lock1.lock();
    condition1.signalAll();
    lock1.unlock();
    keyOwner.getLifecycleService().shutdown();

    finalLatch.await(2, TimeUnit.MINUTES);
    Assert.assertEquals(size, count.get());
  }
 @Test
 public void testInstance() {
   assertNotNull(instance);
   final Set<Member> members = instance.getCluster().getMembers();
   assertEquals(1, members.size());
   final Member member = members.iterator().next();
   final InetSocketAddress inetSocketAddress = member.getInetSocketAddress();
   assertEquals(5700, inetSocketAddress.getPort());
   assertEquals("test-instance", config.getInstanceName());
   assertEquals("HAZELCAST_ENTERPRISE_LICENSE_KEY", config.getLicenseKey());
 }
Пример #3
0
  @Test(timeout = 100000)
  public void testKeyOwnerDiesOnCondition() throws Exception {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
    final Config config = new Config();
    final HazelcastInstance keyOwner = nodeFactory.newHazelcastInstance(config);
    final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
    int k = 0;
    final AtomicInteger atomicInteger = new AtomicInteger(0);
    while (keyOwner
        .getCluster()
        .getLocalMember()
        .equals(instance1.getPartitionService().getPartition(k++).getOwner())) {
      Thread.sleep(10);
    }

    final int key = k;
    final ILock lock1 = instance1.getLock(key);
    final String name = "testKeyOwnerDiesOnCondition";
    final ICondition condition1 = lock1.newCondition(name);

    Thread t =
        new Thread(
            new Runnable() {
              public void run() {
                final ILock lock = instance2.getLock(key);
                final ICondition condition = lock.newCondition(name);
                lock.lock();
                try {
                  condition.await();
                } catch (InterruptedException e) {
                  e.printStackTrace();
                } finally {
                  lock.unlock();
                }
                atomicInteger.incrementAndGet();
              }
            });
    t.start();
    Thread.sleep(1000);
    lock1.lock();
    keyOwner.getLifecycleService().shutdown();

    condition1.signal();

    lock1.unlock();
    Thread.sleep(1000);
    t.join();
    Assert.assertEquals(1, atomicInteger.get());
  }
Пример #4
0
 @Test
 public void testPutWithTwoMember() throws Exception {
   Config config = new Config();
   HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
   HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
   assertEquals(2, h1.getCluster().getMembers().size());
   assertEquals(2, h2.getCluster().getMembers().size());
   IMap imap1 = h1.getMap("default");
   IMap imap2 = h2.getMap("default");
   assertEquals(0, imap1.size());
   assertEquals(0, imap2.size());
   CMap cmap1 = getCMap(h1, "default");
   CMap cmap2 = getCMap(h2, "default");
   assertNotNull(cmap1);
   assertNotNull(cmap2);
   Object key = "1";
   Object value = "value";
   Data dKey = toData(key);
   Data dValue = toData(value);
   imap1.put(key, value, 5, TimeUnit.SECONDS);
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(
       1,
       cmap1.getMapIndexService().getOwnedRecords().size()
           + cmap2.getMapIndexService().getOwnedRecords().size());
   Record record1 = cmap1.getRecord(dKey);
   Record record2 = cmap2.getRecord(dKey);
   long now = System.currentTimeMillis();
   long millisLeft1 = record1.getExpirationTime() - now;
   long millisLeft2 = record2.getExpirationTime() - now;
   assertTrue(millisLeft1 <= 5000 && millisLeft1 > 0);
   assertTrue(millisLeft2 <= 5000 && millisLeft2 > 0);
   assertTrue(record1.isActive());
   assertTrue(record2.isActive());
   assertEquals(1, record1.valueCount());
   assertEquals(1, record2.valueCount());
   assertEquals(dValue, record1.getValueData());
   assertEquals(dValue, record2.getValueData());
   imap1.set("2", "value2", 5, TimeUnit.SECONDS);
   assertEquals("value2", imap1.get("2"));
   assertEquals("value2", imap2.get("2"));
   Thread.sleep(6000);
   assertNull(imap1.get("2"));
   assertNull(imap2.get("2"));
   now = System.currentTimeMillis();
   assertFalse(record1.isValid(now));
   assertFalse(record2.isValid(now));
   Thread.sleep(23000);
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size());
   assertEquals(0, cmap1.mapRecords.size());
   assertEquals(0, cmap2.mapRecords.size());
   imap1.put(key, value, 10, TimeUnit.SECONDS);
   assertTrue(migrateKey(key, h1, h1, 0));
   assertTrue(migrateKey(key, h1, h2, 1));
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(
       1,
       cmap1.getMapIndexService().getOwnedRecords().size()
           + cmap2.getMapIndexService().getOwnedRecords().size());
   record1 = cmap1.getRecord(dKey);
   record2 = cmap2.getRecord(dKey);
   now = System.currentTimeMillis();
   millisLeft1 = record1.getExpirationTime() - now;
   millisLeft2 = record2.getExpirationTime() - now;
   assertTrue(millisLeft1 <= 11000 && millisLeft1 > 0);
   assertTrue(millisLeft2 <= 11000 && millisLeft2 > 0);
   assertTrue(record1.isActive());
   assertTrue(record2.isActive());
   assertTrue(record1.isValid(now));
   assertTrue(record2.isValid(now));
   assertEquals(1, record1.valueCount());
   assertEquals(1, record2.valueCount());
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(
       1,
       cmap1.getMapIndexService().getOwnedRecords().size()
           + cmap2.getMapIndexService().getOwnedRecords().size());
   assertTrue(migrateKey(key, h1, h2, 0));
   assertTrue(migrateKey(key, h1, h1, 1));
   cmap1.startCleanup(true);
   cmap2.startCleanup(true);
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size());
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   now = System.currentTimeMillis();
   millisLeft1 = record1.getExpirationTime() - now;
   millisLeft2 = record2.getExpirationTime() - now;
   assertTrue(millisLeft1 <= 10000 && millisLeft1 > 0);
   assertTrue(millisLeft2 <= 10000 && millisLeft2 > 0);
   assertTrue(record1.isActive());
   assertTrue(record2.isActive());
   assertTrue(record1.isValid(now));
   assertTrue(record2.isValid(now));
   assertEquals(1, record1.valueCount());
   assertEquals(1, record2.valueCount());
   Thread.sleep(11000);
   now = System.currentTimeMillis();
   assertFalse(record1.isValid(now));
   assertFalse(record2.isValid(now));
   Thread.sleep(20000);
   assertEquals(0, cmap1.mapRecords.size());
   assertEquals(0, cmap2.mapRecords.size());
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size());
   imap1.put("1", "value1");
   record1 = cmap1.getRecord(dKey);
   record2 = cmap2.getRecord(dKey);
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size());
   now = System.currentTimeMillis();
   assertEquals(Long.MAX_VALUE, record1.getExpirationTime());
   assertEquals(Long.MAX_VALUE, record2.getExpirationTime());
   assertTrue(record1.isActive());
   assertTrue(record2.isActive());
   assertTrue(record1.isValid(now));
   assertTrue(record2.isValid(now));
   assertEquals(1, record1.valueCount());
   assertEquals(1, record2.valueCount());
   imap1.remove("1");
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size());
   Thread.sleep(20000);
   assertEquals(0, cmap1.mapRecords.size());
   assertEquals(0, cmap2.mapRecords.size());
   assertEquals(0, cmap1.mapIndexService.size());
   assertEquals(0, cmap2.mapIndexService.size());
 }