Пример #1
0
  @Test(timeout = 300000)
  public void testJoinWhenMemberClosedInBetween() throws InterruptedException {
    // Test is expecting to all can join safely.
    // On the failed case the last opened instance throws java.lang.IllegalStateException: Node
    // failed to start!
    Config config = new Config();
    HazelcastInstance i1 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance i2 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance i3 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance i4 = Hazelcast.newHazelcastInstance(config);

    final IMap<Integer, Integer> map = i4.getMap("a");
    int numThreads = 40;
    final int loop = 5000;

    Thread[] threads = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
      threads[i] =
          new Thread(
              new Runnable() {
                public void run() {
                  Random random = new Random();
                  for (int j = 0; j < loop; j++) {
                    int op = random.nextInt(3);
                    if (op == 0) {
                      map.put(j, j);
                    } else if (op == 1) {
                      Integer val = map.remove(j);
                      assert val == null || val.equals(j);
                    } else {
                      Integer val = map.get(j);
                      assert val == null || val.equals(j);
                    }
                  }
                }
              });
      threads[i].start();
    }

    i1.shutdown();
    i2.shutdown();
    i3.shutdown();

    // Should not throw java.lang.IllegalStateException: Node failed to start!
    Hazelcast.newHazelcastInstance(config);

    for (int i = 0; i < numThreads; i++) {
      threads[i].join();
    }
  }
 @Override
 public void run() {
   try {
     start.await();
     while (isRunning.get()) {
       instance1.shutdown();
       instance2.shutdown();
       Thread.sleep(10l);
       instance1 = newInstance();
       instance2 = newInstance();
     }
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
  @Test
  public void test() {
    List<Object> expectedItems = new LinkedList<Object>();
    for (int k = 0; k < 100; k++) {
      queue.add(k);
      expectedItems.add(k);
    }

    remote1.shutdown();
    remote2.shutdown();

    assertEquals(expectedItems.size(), queue.size());
    List actualItems = Arrays.asList(queue.toArray());
    assertEquals(expectedItems, actualItems);
  }
Пример #4
0
  @Repeat(25)
  @Test
  public void testEachConnectionUseDifferentSelectorEventually() {
    Config config = new Config();
    config.setProperty(GroupProperties.PROP_IO_BALANCER_INTERVAL_SECONDS, "1");
    config.setProperty(GroupProperties.PROP_IO_THREAD_COUNT, "2");

    HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance instance3 = Hazelcast.newHazelcastInstance(config);

    instance2.shutdown();
    instance2 = Hazelcast.newHazelcastInstance(config);

    IMap<Integer, Integer> map = instance1.getMap(randomMapName());

    long deadLine = System.currentTimeMillis() + TEST_DURATION_SECONDS * 1000;
    for (int i = 0; System.currentTimeMillis() < deadLine; i++) {
      map.put(i % 1000, i);
    }

    assertBalanced(instance1);
    assertBalanced(instance2);
    assertBalanced(instance3);
  }
  /** Destroys currently allocated instance. */
  public void destroy() {

    log.info("Shutting down Hazelcast instance [{}]..", instanceName);
    final HazelcastInstance instance = Hazelcast.getHazelcastInstanceByName(instanceName);
    if (instance != null) {
      instance.shutdown();
    }
  }
  @Override
  public void close() {
    if (instance != null) {
      instance.shutdown();

      logger.success("Shut down hazelcast");
    }
  }
 public boolean shutdownMember(String clusterId, String memberId) {
   LOG.info("Shutting down the Member " + memberId + "on cluster : " + clusterId);
   HzCluster hzCluster = clusterMap.get(clusterId);
   HazelcastInstance hazelcastInstance = hzCluster.getInstanceById(memberId);
   hazelcastInstance.shutdown();
   hzCluster.removeInstance(memberId);
   return true;
 }
  @After
  public void tearDown() {
    super.tearDown();

    if (client != null) {
      client.shutdown();
    }
  }
 @Override
 public void run() {
   while (!stop) {
     try {
       sleepSeconds(10);
       HazelcastInstance hz = queue.take();
       hz.shutdown();
       queue.add(createHazelcastInstance());
     } catch (InterruptedException ignored) {
     }
   }
 }
Пример #10
0
 @Override
 public void close() {
   flush();
   cacheEntryList.clear();
   if (!cache.isDestroyed() && !manager.isClosed()) {
     cache.close();
   }
   if (!manager.isClosed()) {
     manager.close();
   }
   hazelcastInstance.shutdown();
   persistentDataStore.close();
   LOG.info("JCache Gora datastore destroyed successfully.");
 }
Пример #11
0
  @Test(timeout = 120000)
  public void testGetAllKeys() throws Exception {
    TestEventBasedMapStore testMapStore = new TestEventBasedMapStore();
    Map store = testMapStore.getStore();
    Set keys = new HashSet();
    int size = 1000;
    for (int i = 0; i < size; i++) {
      store.put(i, "value" + i);
      keys.add(i);
    }
    Config config = newConfig(testMapStore, 2);

    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
    HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance h2 = nodeFactory.newHazelcastInstance(config);

    IMap map1 = h1.getMap("default");
    IMap map2 = h2.getMap("default");

    // checkIfMapLoaded("default", h1);
    // checkIfMapLoaded("default", h2);

    assertEquals("value1", map1.get(1));
    assertEquals("value1", map2.get(1));

    assertEquals(1000, map1.size());
    assertEquals(1000, map2.size());

    HazelcastInstance h3 = nodeFactory.newHazelcastInstance(config);
    IMap map3 = h3.getMap("default");
    // checkIfMapLoaded("default", h3);

    assertEquals("value1", map1.get(1));
    assertEquals("value1", map2.get(1));
    assertEquals("value1", map3.get(1));

    assertEquals(1000, map1.size());
    assertEquals(1000, map2.size());
    assertEquals(1000, map3.size());

    h3.shutdown();

    assertEquals("value1", map1.get(1));
    assertEquals("value1", map2.get(1));

    assertEquals(1000, map1.size());
    assertEquals(1000, map2.size());
  }
Пример #12
0
  @Test
  public void testClearBackup() {
    HazelcastInstance[] instances = createHazelcastInstances();
    HazelcastInstance instance1 = instances[0];
    HazelcastInstance instance2 = instances[1];
    String name = generateKeyOwnedBy(instance1);
    IQueue<Object> queue1 = instance1.getQueue(name);
    IQueue<Object> queue2 = instance2.getQueue(name);
    for (int i = 0; i < 4; i++) {
      queue1.offer("item" + i);
    }
    assertSizeEventually(4, queue2);
    assertIterableEquals(queue2, "item0", "item1", "item2", "item3");
    queue1.clear();

    instance1.shutdown();

    assertSizeEventually(0, queue2);
  }
Пример #13
0
  @Test
  public void testAddAllBackup() {
    HazelcastInstance[] instances = createHazelcastInstances();
    HazelcastInstance instance1 = instances[0];
    HazelcastInstance instance2 = instances[1];
    String name = generateKeyOwnedBy(instance1);
    IQueue<Object> queue1 = instance1.getQueue(name);
    IQueue<Object> queue2 = instance2.getQueue(name);
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < 4; i++) {
      list.add("item" + i);
    }
    assertTrue(queue1.addAll(list));

    instance1.shutdown();

    assertSizeEventually(4, queue2);
    assertIterableEquals(queue2, "item0", "item1", "item2", "item3");
  }
Пример #14
0
 @Test
 public void shouldCreateQueue() throws InterruptedException {
   // Given
   int numberOfEvents = 5;
   Config config = new Config();
   QueueStoreConfig jdbcBackedQueueConfig =
       QueueStoreConfigFactory.getJdbcBackedQueueConfig(dataSource(), queueName);
   QueueConfig messageQueue = config.getQueueConfig(queueName);
   messageQueue.setQueueStoreConfig(jdbcBackedQueueConfig);
   HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(config);
   // When
   QueueStore storeImplementation = jdbcBackedQueueConfig.getStoreImplementation();
   for (long i = 0; i < numberOfEvents; i++) {
     storeImplementation.store(i, String.valueOf(i));
   }
   IQueue<String> iQueue = hazelcastInstance.getQueue(queueName);
   MatcherAssert.assertThat(iQueue.size(), CoreMatchers.equalTo(numberOfEvents));
   String actual = iQueue.take();
   MatcherAssert.assertThat(actual, CoreMatchers.equalTo("0"));
   hazelcastInstance.shutdown();
 }
Пример #15
0
  /** Testing if topic can properly listen messages and if topic has any issue after a shutdown. */
  @Test
  public void testTopicCluster() throws InterruptedException {
    String topicName = "TestMessages" + generateRandomString(5);
    Config cfg = new Config();

    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances(cfg);
    HazelcastInstance instance1 = instances[0];
    HazelcastInstance instance2 = instances[1];

    ITopic<String> topic1 = instance1.getTopic(topicName);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final String message = "Test" + randomString();

    topic1.addMessageListener(
        new MessageListener<String>() {
          public void onMessage(Message msg) {
            assertEquals(message, msg.getMessageObject());
            latch1.countDown();
          }
        });

    ITopic<String> topic2 = instance2.getTopic(topicName);
    final CountDownLatch latch2 = new CountDownLatch(2);
    topic2.addMessageListener(
        new MessageListener<String>() {
          public void onMessage(Message msg) {
            assertEquals(message, msg.getMessageObject());
            latch2.countDown();
          }
        });

    topic1.publish(message);
    assertTrue(latch1.await(5, TimeUnit.SECONDS));

    instance1.shutdown();
    topic2.publish(message);
    assertTrue(latch2.await(5, TimeUnit.SECONDS));
  }
Пример #16
0
  @Test
  public void whenTargetMemberDiesThenOperationAbortedWithMembersLeftException() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance local = factory.newHazelcastInstance();
    HazelcastInstance remote = factory.newHazelcastInstance();
    warmUpPartitions(local, remote);

    OperationService service = getNode(local).nodeEngine.getOperationService();
    Operation op = new TargetOperation();
    Address address = new Address(remote.getCluster().getLocalMember().getSocketAddress());
    Future f = service.createInvocationBuilder(null, op, address).invoke();
    sleepSeconds(1);

    remote.shutdown();

    try {
      f.get();
      fail();
    } catch (MemberLeftException expected) {

    }
  }
Пример #17
0
  @Test
  public void whenPartitionTargetMemberDiesThenOperationSendToNewPartitionOwner() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance local = factory.newHazelcastInstance();
    HazelcastInstance remote = factory.newHazelcastInstance();
    warmUpPartitions(local, remote);

    Node localNode = getNode(local);
    OperationService service = localNode.nodeEngine.getOperationService();
    Operation op = new PartitionTargetOperation();
    String partitionKey = generateKeyOwnedBy(remote);
    int partitionId = localNode.nodeEngine.getPartitionService().getPartitionId(partitionKey);
    Future f =
        service.createInvocationBuilder(null, op, partitionId).setCallTimeout(30000).invoke();
    sleepSeconds(1);

    remote.shutdown();

    // the get should work without a problem because the operation should be re-targeted at the
    // newest owner
    // for that given partition
    f.get();
  }
Пример #18
0
  @Test
  public void testConnectedClientsWithReAuth() throws InterruptedException {
    final ClientConfig clientConfig = new ClientConfig();
    clientConfig.getNetworkConfig().setConnectionAttemptPeriod(1000 * 10);

    final CountDownLatch countDownLatch = new CountDownLatch(2);

    clientConfig.addListenerConfig(
        new ListenerConfig(
            new LifecycleListener() {
              @Override
              public void stateChanged(LifecycleEvent event) {
                if (event.getState() == LifecycleEvent.LifecycleState.CLIENT_CONNECTED) {
                  countDownLatch.countDown();
                }
              }
            }));
    HazelcastInstance instance = Hazelcast.newHazelcastInstance();
    final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

    // restart the node
    instance.shutdown();
    Thread.sleep(1000);
    final HazelcastInstance restartedInstance = Hazelcast.newHazelcastInstance();

    client.getMap(randomMapName()).size(); // do any operation

    assertOpenEventually(countDownLatch); // wait for clients to reconnect & reAuth
    assertTrueEventually(
        new AssertTask() {
          @Override
          public void run() throws Exception {
            assertEquals(1, restartedInstance.getClientService().getConnectedClients().size());
          }
        });
  }
 private void killKeyOwner(String key, TestMapUsingMapStoreBuilder<String, Object> storeBuilder) {
   HazelcastInstance[] nodes = storeBuilder.getNodes();
   HazelcastInstance ownerNode = getOwnerNode(key, nodes);
   ownerNode.shutdown();
 }
 @After
 public void tearDown() throws IOException {
   elasticsearchServer.shutdown();
   hazelcastInstance.shutdown();
 }
 @Override
 public void shutdown() {
   if (h != null) h.shutdown();
 }
 @After
 public void after() {
   if (instance != null) {
     instance.shutdown();
   }
 }
Пример #23
0
 @AfterClass
 public static void destroy() {
   hz.shutdown();
   Hazelcast.shutdownAll();
 }
Пример #24
0
 @Stop
 public void stopHazel() {
   hazelInstance.shutdown();
   hazelInstance = null;
 }