Esempio n. 1
0
 protected void handleQTake(String[] args) {
   try {
     println(getQueue().take());
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
Esempio n. 2
0
 @SuppressWarnings("LockAcquiredButNotSafelyReleased")
 protected void handleLock(String[] args) {
   String lockStr = args[0];
   String key = args[1];
   Lock lock = hazelcast.getLock(key);
   if (lockStr.equalsIgnoreCase("lock")) {
     lock.lock();
     println("true");
   } else if (lockStr.equalsIgnoreCase("unlock")) {
     lock.unlock();
     println("true");
   } else if (lockStr.equalsIgnoreCase("trylock")) {
     String timeout = args.length > 2 ? args[2] : null;
     if (timeout == null) {
       println(lock.tryLock());
     } else {
       long time = Long.valueOf(timeout);
       try {
         println(lock.tryLock(time, TimeUnit.SECONDS));
       } catch (InterruptedException e) {
         e.printStackTrace();
       }
     }
   }
 }
Esempio n. 3
0
 public void run() {
   boolean live = true;
   Random rand = new Random();
   try {
     for (int j = 0; j < lockCountPerThread && live; j++) {
       final Lock lock = hz.getLock(key + rand.nextInt(locks));
       lock.lock();
       try {
         if (j % 100 == 0) {
           System.out.println(Thread.currentThread().getName() + " is at:" + j);
         }
         totalCount.incrementAndGet();
         Thread.sleep(1);
       } catch (InterruptedException e) {
         e.printStackTrace();
         break;
       } finally {
         try {
           lock.unlock();
         } catch (Exception e) {
           e.printStackTrace();
           live = false;
         }
       }
     }
   } finally {
     latch.countDown();
   }
 }
  /* github issue #183 */
  @Test
  public void testKeyBasedListeners() throws InterruptedException {
    try {
      Config config = new Config();
      HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
      IMap<String, String> map = instance.getMap("map");
      map.put("key1", "value1");
      map.put("key2", "value2");
      map.put("key3", "value3");

      ClientConfig clientConfig = new ClientConfig();
      HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

      final AtomicInteger count = new AtomicInteger(0);
      IMap<String, String> clientMap = client.getMap("map");

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryRemoved(EntryEvent<String, String> entryEvent) {}

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key1",
          true);

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryRemoved(EntryEvent<String, String> entryEvent) {}

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              System.out.println("event map");
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key2",
          true);

      map.put("key1", "new-value1");
      Thread.sleep(100);
      Assert.assertEquals(count.get(), 1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
Esempio n. 5
0
 protected void handleMapGetAsync(String[] args) {
   try {
     println(getMap().getAsync(args[1]).get());
   } catch (InterruptedException e) {
     e.printStackTrace();
   } catch (ExecutionException e) {
     e.printStackTrace();
   }
 }
Esempio n. 6
0
 protected void handleQPoll(String[] args) {
   long timeout = 0;
   if (args.length > 1) {
     timeout = Long.valueOf(args[1]);
   }
   try {
     println(getQueue().poll(timeout, TimeUnit.SECONDS));
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
Esempio n. 7
0
 protected void handleQOffer(String[] args) {
   long timeout = 0;
   if (args.length > 2) {
     timeout = Long.valueOf(args[2]);
   }
   try {
     boolean offered = getQueue().offer(args[1], timeout, TimeUnit.SECONDS);
     println(offered);
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
Esempio n. 8
0
  @Test
  public void testEvictionLFU() {
    try {
      final int k = 1;
      final int size = 10000;

      final String mapName = "testEvictionLFU";
      Config cfg = new Config();
      MapConfig mc = cfg.getMapConfig(mapName);
      mc.setEvictionPolicy(MapConfig.EvictionPolicy.LFU);
      mc.setEvictionPercentage(20);
      MaxSizeConfig msc = new MaxSizeConfig();
      msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE);
      msc.setSize(size);
      mc.setMaxSizeConfig(msc);

      TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
      final HazelcastInstance[] instances = factory.newInstances(cfg);
      IMap<Object, Object> map = instances[0].getMap(mapName);

      for (int i = 0; i < size / 2; i++) {
        map.put(i, i);
        map.get(i);
      }
      Thread.sleep(1000);
      for (int i = size / 2; i < size; i++) {
        map.put(i, i);
      }

      Thread.sleep(3000);

      Assert.assertFalse("No eviction!?!?!?", map.size() == size);
      boolean isFrequentlyUsedEvicted = false;
      for (int i = 0; i < size / 2; i++) {
        if (map.get(i) == null) {
          isFrequentlyUsedEvicted = true;
          break;
        }
      }
      Assert.assertFalse(isFrequentlyUsedEvicted);
      instances[0].getLifecycleService().shutdown();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
Esempio n. 9
0
  @Test
  public void testEvictionLRU() {
    final int k = 2;
    final int size = 10000;

    try {
      final String mapName = "testEvictionLRU";
      Config cfg = new Config();
      MapConfig mc = cfg.getMapConfig(mapName);
      mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU);
      mc.setEvictionPercentage(10);
      MaxSizeConfig msc = new MaxSizeConfig();
      msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE);
      msc.setSize(size);
      mc.setMaxSizeConfig(msc);

      TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
      final HazelcastInstance[] instances = factory.newInstances(cfg);
      IMap<Object, Object> map = instances[0].getMap(mapName);
      Thread.sleep(1000);

      for (int i = size / 2; i < size; i++) {
        map.put(i, i);
      }
      Thread.sleep(2000);
      for (int i = 0; i < size / 2; i++) {
        map.put(i, i);
      }
      Thread.sleep(1000);

      int recentlyUsedEvicted = 0;
      for (int i = 0; i < size / 2; i++) {
        if (map.get(i) == null) {
          recentlyUsedEvicted++;
        }
      }
      Assert.assertTrue(recentlyUsedEvicted == 0);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
Esempio n. 10
0
  @Test
  public void testEvictionSpeedTestPerPartition() {
    final int k = 2;
    final int size = 100;
    final CountDownLatch latch = new CountDownLatch(k);
    final String mapName = "testEvictionSpeedTestPerPartition";
    Config cfg = new Config();
    final MapConfig mc = cfg.getMapConfig(mapName);
    mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU);
    mc.setEvictionPercentage(25);
    final MaxSizeConfig msc = new MaxSizeConfig();
    msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_PARTITION);
    msc.setSize(size);
    mc.setMaxSizeConfig(msc);

    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(cfg);
    final int pnum = instances[0].getPartitionService().getPartitions().size();
    final AtomicInteger failCount = new AtomicInteger(0);

    new Thread() {
      final IMap map = instances[0].getMap(mapName);

      public void run() {
        try {
          Thread.sleep(1000);
          while (latch.getCount() != 0) {
            try {
              int msize = map.size();
              if (msize > (size * pnum * 1.2)) {
                failCount.incrementAndGet();
              }
              Thread.sleep(1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }.start();

    for (int i = 0; i < k; i++) {
      final IMap map = instances[i].getMap(mapName);
      new Thread() {
        public void run() {
          for (int j = 0; j < 100000; j++) {
            map.put(k + "-" + j, j);
          }
          latch.countDown();
        }
      }.start();
    }

    try {
      Assert.assertEquals(latch.await(10, TimeUnit.MINUTES), true);
      Assert.assertEquals(0, failCount.get());
    } catch (InterruptedException e) {
      e.printStackTrace();
      Assert.fail(e.getMessage());
    }
  }
Esempio n. 11
0
  // current eviction check period is 1 second.
  // about 30.000 records can be put in one second
  // so the size should be adapted
  @Test
  public void testEvictionSpeedTest() {
    final int k = 3;
    final int size = 10000;
    final CountDownLatch latch = new CountDownLatch(k);
    final String mapName = "testEvictionSpeedTest";
    Config cfg = new Config();
    final MapConfig mc = cfg.getMapConfig(mapName);
    mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU);
    mc.setEvictionPercentage(25);
    final MaxSizeConfig msc = new MaxSizeConfig();
    msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE);
    msc.setSize(size);
    mc.setMaxSizeConfig(msc);

    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(cfg);
    final AtomicBoolean success = new AtomicBoolean(true);

    new Thread() {
      final IMap map = instances[0].getMap(mapName);

      public void run() {
        try {
          Thread.sleep(1000);
          while (latch.getCount() != 0) {
            try {
              int msize = map.size();
              if (msize > (size * k + size * k * 10 / 100)) {
                success.set(false);
                break;
              }
              Thread.sleep(1000);
            } catch (InterruptedException e) {
              e.printStackTrace();
            }
          }
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }.start();

    for (int i = 0; i < k; i++) {
      final IMap map = instances[i].getMap(mapName);
      new Thread() {
        public void run() {
          for (int j = 0; j < size; j++) {
            map.put(k + "-" + j, j);
          }
          latch.countDown();
        }
      }.start();
    }

    try {
      Assert.assertTrue(latch.await(10, TimeUnit.MINUTES));
      Assert.assertTrue(success.get());
    } catch (InterruptedException e) {
      e.printStackTrace();
      Assert.fail(e.getMessage());
    }
  }
Esempio n. 12
0
  @Test(timeout = 1000 * 100)
  /** Test for issue 267 */
  public void testHighConcurrentLockAndUnlock() {
    Config config = new Config();
    final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(1);
    final HazelcastInstance hz = nodeFactory.newHazelcastInstance(config);
    final String key = "key";
    final int threadCount = 100;
    final int lockCountPerThread = 5000;
    final int locks = 50;
    final CountDownLatch latch = new CountDownLatch(threadCount);
    final AtomicInteger totalCount = new AtomicInteger();

    class InnerTest implements Runnable {
      public void run() {
        boolean live = true;
        Random rand = new Random();
        try {
          for (int j = 0; j < lockCountPerThread && live; j++) {
            final Lock lock = hz.getLock(key + rand.nextInt(locks));
            lock.lock();
            try {
              if (j % 100 == 0) {
                System.out.println(Thread.currentThread().getName() + " is at:" + j);
              }
              totalCount.incrementAndGet();
              Thread.sleep(1);
            } catch (InterruptedException e) {
              e.printStackTrace();
              break;
            } finally {
              try {
                lock.unlock();
              } catch (Exception e) {
                e.printStackTrace();
                live = false;
              }
            }
          }
        } finally {
          latch.countDown();
        }
      }
    }

    ExecutorService executorService = Executors.newCachedThreadPool();
    for (int i = 0; i < threadCount; i++) {
      executorService.execute(new InnerTest());
    }

    try {
      assertTrue("Lock tasks stuck!", latch.await(2, TimeUnit.MINUTES));
      assertEquals((threadCount * lockCountPerThread), totalCount.get());
    } catch (InterruptedException e) {
      e.printStackTrace();
    } finally {
      try {
        hz.getLifecycleService().terminate();
      } catch (Throwable ignored) {
      }
      executorService.shutdownNow();
    }
  }