@Test
  public void testSubmitToKeyOwnerCallable() throws Exception {
    final int k = simpleTestNodeCount;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(new Config());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch latch = new CountDownLatch(k / 2);
    final ExecutionCallback callback =
        new ExecutionCallback() {
          public void onResponse(Object response) {
            if ((Boolean) response) count.incrementAndGet();
            latch.countDown();
          }

          public void onFailure(Throwable t) {}
        };
    for (int i = 0; i < k; i++) {
      final HazelcastInstance instance = instances[i];
      final IExecutorService service = instance.getExecutorService("testSubmitToKeyOwnerCallable");
      final String script = "hazelcast.getCluster().getLocalMember().equals(member)";
      final HashMap map = new HashMap();
      final Member localMember = instance.getCluster().getLocalMember();
      map.put("member", localMember);
      int key = 0;
      while (!localMember.equals(instance.getPartitionService().getPartition(++key).getOwner())) ;
      if (i % 2 == 0) {
        final Future f = service.submitToKeyOwner(new ScriptCallable(script, map), key);
        assertTrue((Boolean) f.get(5, TimeUnit.SECONDS));
      } else {
        service.submitToKeyOwner(new ScriptCallable(script, map), key, callback);
      }
    }
    assertTrue(latch.await(30, TimeUnit.SECONDS));
    assertEquals(k / 2, count.get());
  }
 public static String generateKeyNotOwnedBy(HazelcastInstance instance) {
   final Member localMember = instance.getCluster().getLocalMember();
   final PartitionService partitionService = instance.getPartitionService();
   for (; ; ) {
     String id = UUID.randomUUID().toString();
     Partition partition = partitionService.getPartition(id);
     if (!localMember.equals(partition.getOwner())) {
       return id;
     }
   }
 }
 /**
  * Returns the next {@code long} key owned by the given Hazelcast instance.
  *
  * @param instance Hazelcast instance to search next key for
  * @param lastKey last key to start search from
  * @return next key owned by given Hazelcast instance
  */
 public static long nextKeyOwnedBy(HazelcastInstance instance, long lastKey) {
   Member localMember = instance.getCluster().getLocalMember();
   PartitionService partitionService = instance.getPartitionService();
   while (true) {
     Partition partition = partitionService.getPartition(lastKey);
     if (localMember.equals(partition.getOwner())) {
       return lastKey;
     }
     lastKey++;
   }
 }
    @Override
    public boolean equals(Object o) {
      if (this == o) return true;
      if (o == null || getClass() != o.getClass()) return false;

      TestMessage that = (TestMessage) o;

      if (data != null ? !data.equals(that.data) : that.data != null) return false;
      if (publisher != null ? !publisher.equals(that.publisher) : that.publisher != null)
        return false;

      return true;
    }
  private HazelcastInstance getOwnerNode(String key, HazelcastInstance[] nodes) {
    PartitionService partitionService = nodes[0].getPartitionService();
    Partition partition = partitionService.getPartition(key);
    Member owner = partition.getOwner();

    for (int i = 0; i < nodes.length; i++) {
      HazelcastInstance node = nodes[i];
      Member localMember = node.getCluster().getLocalMember();
      if (localMember.equals(owner)) {
        return node;
      }
    }

    throw new IllegalStateException("This should not be happen...");
  }
 private static void load(boolean load, ExecutorService es, final MultiMap<String, byte[]> map) {
   if (load) {
     final Member thisMember = instance.getCluster().getLocalMember();
     for (int i = 0; i < entryCount; i++) {
       final String key = String.valueOf(i);
       Partition partition = instance.getPartitionService().getPartition(key);
       if (thisMember.equals(partition.getOwner())) {
         es.execute(
             new Runnable() {
               public void run() {
                 map.put(key, new byte[valueSize]);
               }
             });
       }
     }
   }
 }
 private static void load(boolean load, ExecutorService es, final MultiMap<String, byte[]> map) {
   if (load) {
     final Member thisMember = Hazelcast.getCluster().getLocalMember();
     for (int i = 0; i < ENTRY_COUNT; i++) {
       final String key = String.valueOf(i);
       Partition partition = Hazelcast.getPartitionService().getPartition(key);
       if (thisMember.equals(partition.getOwner())) {
         es.execute(
             new Runnable() {
               public void run() {
                 map.put(key, new byte[VALUE_SIZE]);
               }
             });
       }
     }
   }
 }
  @Setup
  public void setup(TestContext testContext) throws Exception {
    this.testContext = testContext;
    targetInstance = testContext.getTargetInstance();
    testId = testContext.getTestId();

    integrityMap = targetInstance.getMap(basename + "Integrity");
    stressMap = targetInstance.getMap(basename + "Stress");

    integrityThreads = new MapIntegrityThread[mapIntegrityThreadCount];
    value = new byte[valueSize];

    Random random = new Random();
    random.nextBytes(value);

    if (mapLoad && isMemberNode(targetInstance)) {
      PartitionService partitionService = targetInstance.getPartitionService();
      final Set<Partition> partitionSet = partitionService.getPartitions();
      for (Partition partition : partitionSet) {
        while (partition.getOwner() == null) {
          Thread.sleep(1000);
        }
      }
      LOGGER.info(testId + ": " + partitionSet.size() + " partitions");

      Member localMember = targetInstance.getCluster().getLocalMember();
      for (int i = 0; i < totalIntegrityKeys; i++) {
        Partition partition = partitionService.getPartition(i);
        if (localMember.equals(partition.getOwner())) {
          integrityMap.put(i, value);
        }
      }
      LOGGER.info(
          testId + ": integrityMap=" + integrityMap.getName() + " size=" + integrityMap.size());

      Config config = targetInstance.getConfig();
      MapConfig mapConfig = config.getMapConfig(integrityMap.getName());
      LOGGER.info(testId + ": " + mapConfig);
    }
  }