Example #1
0
  @Test(timeout = 100000)
  public void testKeyOwnerDies() 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);

    warmUpPartitions(keyOwner, instance1, instance2);
    final String key = generateKeyOwnedBy(keyOwner);
    final ILock lock1 = instance1.getLock(key);
    lock1.lock();

    final CountDownLatch latch = new CountDownLatch(1);
    new Thread(
            new Runnable() {
              public void run() {
                final ILock lock = instance2.getLock(key);
                lock.lock();
                latch.countDown();
              }
            })
        .start();

    Thread.sleep(1000);
    keyOwner.getLifecycleService().shutdown();
    Assert.assertTrue(lock1.isLocked());
    Assert.assertTrue(lock1.isLockedByCurrentThread());
    Assert.assertTrue(lock1.tryLock());
    lock1.unlock();
    lock1.unlock();
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
  }
Example #2
0
  private void testLockEviction(boolean localKey) throws Exception {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final Config config = new Config();

    final HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    warmUpPartitions(instance2, instance1);

    final String key;
    if (localKey) {
      key = generateKeyOwnedBy(instance1);
    } else {
      key = generateKeyNotOwnedBy(instance1);
    }

    final ILock lock = instance1.getLock(key);
    lock.lock(10, TimeUnit.SECONDS);
    assertTrue(lock.getRemainingLeaseTime() > 0);
    Assert.assertTrue(lock.isLocked());

    final CountDownLatch latch = new CountDownLatch(1);
    Thread t =
        new Thread(
            new Runnable() {
              public void run() {
                final ILock lock = instance2.getLock(key);
                lock.lock();
                latch.countDown();
              }
            });
    t.start();
    Assert.assertTrue(latch.await(30, TimeUnit.SECONDS));
  }
Example #3
0
  @Test
  public void testMapPutTtlWithListener() throws InterruptedException {
    Config cfg = new Config();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance[] instances = factory.newInstances(cfg);
    warmUpPartitions(instances);

    final int k = 10;
    final int putCount = 10000;
    final CountDownLatch latch = new CountDownLatch(k * putCount);
    final IMap map = instances[0].getMap("testMapEvictionTtlWithListener");

    final AtomicBoolean error = new AtomicBoolean(false);
    final Set<Long> times = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>());

    map.addEntryListener(
        new EntryAdapter() {
          public void entryEvicted(final EntryEvent event) {
            final Long expectedEvictionTime = (Long) (event.getOldValue());
            long timeDifference = System.currentTimeMillis() - expectedEvictionTime;
            if (timeDifference > 5000) {
              error.set(true);
              times.add(timeDifference);
            }
            latch.countDown();
          }
        },
        true);

    for (int i = 0; i < k; i++) {
      final int threadId = i;
      int ttl = (int) (Math.random() * 5000 + 3000);
      for (int j = 0; j < putCount; j++) {
        final long expectedEvictionTime = ttl + System.currentTimeMillis();
        map.put(j + putCount * threadId, expectedEvictionTime, ttl, TimeUnit.MILLISECONDS);
      }
    }

    assertTrue(latch.await(1, TimeUnit.MINUTES));
    assertFalse("Some evictions took more than 3 seconds! -> " + times, error.get());
  }
Example #4
0
  private void testShutDownNodeWhenOtherWaitingOnLock(boolean localKey)
      throws InterruptedException {
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    final HazelcastInstance instance = nodeFactory.newHazelcastInstance(new Config());
    final HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(new Config());
    warmUpPartitions(instance2, instance);

    final String key;
    if (localKey) {
      key = generateKeyOwnedBy(instance);
    } else {
      key = generateKeyNotOwnedBy(instance);
    }

    final ILock lock = instance.getLock(key);
    final Thread thread =
        new Thread(
            new Runnable() {
              public void run() {
                lock.lock();
              }
            });
    thread.start();
    thread.join();
    new Thread(
            new Runnable() {
              public void run() {
                try {
                  Thread.sleep(3000);
                } catch (InterruptedException e) {
                  e.printStackTrace();
                }
                instance.getLifecycleService().shutdown();
              }
            })
        .start();
    lock.lock();
  }