@Test
  public void submitCallablePartitionAware_WithExecutionCallback() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String mapName = randomString();
    IMap map = client.getMap(mapName);
    String key = HazelcastTestSupport.generateKeyOwnedBy(server);
    Member member = server.getCluster().getLocalMember();

    Callable<String> runnable = new MapPutPartitionAwareCallable<String, String>(mapName, key);

    final AtomicReference<String> result = new AtomicReference<String>();
    final CountDownLatch responseLatch = new CountDownLatch(1);

    service.submit(
        runnable,
        new ExecutionCallback<String>() {
          public void onResponse(String response) {
            result.set(response);
            responseLatch.countDown();
          }

          public void onFailure(Throwable t) {}
        });

    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(member.getUuid(), result.get());
    assertTrue(map.containsKey(member.getUuid()));
  }
  @Test
  public void submitCallablePartitionAware() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String mapName = randomString();
    IMap map = client.getMap(mapName);
    String key = HazelcastTestSupport.generateKeyOwnedBy(server);
    Member member = server.getCluster().getLocalMember();

    Callable<String> callable = new MapPutPartitionAwareCallable<String, String>(mapName, key);
    Future<String> result = service.submit(callable);

    assertEquals(member.getUuid(), result.get());
    assertTrue(map.containsKey(member.getUuid()));
  }
  @Test
  public void submitRunnablePartitionAware_withExecutionCallback() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    String mapName = randomString();
    String key = HazelcastTestSupport.generateKeyOwnedBy(server);
    Member member = server.getCluster().getLocalMember();
    Runnable runnable = new MapPutPartitionAwareRunnable<String>(mapName, key);
    final CountDownLatch responseLatch = new CountDownLatch(1);

    service.submit(
        runnable,
        new ExecutionCallback() {
          @Override
          public void onResponse(Object response) {
            responseLatch.countDown();
          }

          @Override
          public void onFailure(Throwable t) {}
        });
    IMap map = client.getMap(mapName);

    assertOpenEventually("responseLatch", responseLatch);
    assertTrue(map.containsKey(member.getUuid()));
  }
  @Test
  public void testSubmitCallableToMember_withExecutionCallback() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    Callable getUuidCallable = new GetMemberUuidTask();
    Member member = server.getCluster().getLocalMember();
    final CountDownLatch responseLatch = new CountDownLatch(1);
    final AtomicReference<Object> result = new AtomicReference<Object>();

    service.submitToMember(
        getUuidCallable,
        member,
        new ExecutionCallback() {
          @Override
          public void onResponse(Object response) {
            result.set(response);
            responseLatch.countDown();
          }

          @Override
          public void onFailure(Throwable t) {}
        });

    assertOpenEventually("responseLatch", responseLatch);
    assertEquals(member.getUuid(), result.get());
  }
 private List<MembershipEvent> detectMembershipEvents(Map<String, Member> prevMembers) {
   final List<MembershipEvent> events = new LinkedList<MembershipEvent>();
   final Set<Member> eventMembers =
       Collections.unmodifiableSet(new LinkedHashSet<Member>(members));
   for (Member member : members) {
     final Member former = prevMembers.remove(member.getUuid());
     if (former == null) {
       events.add(
           new MembershipEvent(
               client.getCluster(), member, MembershipEvent.MEMBER_ADDED, eventMembers));
     }
   }
   for (Member member : prevMembers.values()) {
     events.add(
         new MembershipEvent(
             client.getCluster(), member, MembershipEvent.MEMBER_REMOVED, eventMembers));
     Address address = member.getAddress();
     if (clusterService.getMember(address) == null) {
       final Connection connection = connectionManager.getConnection(address);
       if (connection != null) {
         connectionManager.destroyConnection(connection);
       }
     }
   }
   return events;
 }
 HazelcastNodeStatus(final Member member_) {
   super(
       member_.getUuid(),
       member_.isLiteMember() ? "client" : "node",
       member_.getInetSocketAddress().toString());
   member = member_;
 }
Ejemplo n.º 7
0
 @Override
 public List<PartitionQuery<K, T>> getPartitions(Query<K, T> query) throws IOException {
   List<PartitionQuery<K, T>> partitions = new ArrayList<>();
   try {
     Member[] clusterMembers = new Member[hazelcastInstance.getCluster().getMembers().size()];
     this.hazelcastInstance.getCluster().getMembers().toArray(clusterMembers);
     for (Member member : clusterMembers) {
       JCacheResult<K, T> result = ((JCacheResult<K, T>) query.execute());
       ConcurrentSkipListSet<K> memberOwnedCacheEntries = new ConcurrentSkipListSet<>();
       while (result.next()) {
         K key = result.getKey();
         Partition partition = hazelcastInstance.getPartitionService().getPartition(key);
         if (partition.getOwner().getUuid().equals(member.getUuid())) {
           memberOwnedCacheEntries.add(key);
         }
       }
       PartitionQueryImpl<K, T> partition =
           new PartitionQueryImpl<>(
               query,
               memberOwnedCacheEntries.first(),
               memberOwnedCacheEntries.last(),
               member.getSocketAddress().getHostString());
       partition.setConf(this.getConf());
       partitions.add(partition);
     }
   } catch (java.lang.Exception ex) {
     LOG.error(
         "Exception occurred while partitioning the query based on Hazelcast partitions.", ex);
     return null;
   }
   LOG.info("Query is partitioned to {} number of partitions.", partitions.size());
   return partitions;
 }
  public Member startMember(String clusterId) throws ServerException {
    LOG.info("Starting a Member on cluster : " + clusterId);
    HzCluster hzCluster = clusterMap.get(clusterId);
    if (hzCluster != null) {
      Config config = hzCluster.getConfig();

      LOG.info(config.getNetworkConfig().getJoin().getTcpIpConfig());

      HazelcastInstance hzInstance = Hazelcast.newHazelcastInstance(config);
      com.hazelcast.core.Member member = hzInstance.getCluster().getLocalMember();
      if (hzCluster.addInstance(member.getUuid(), hzInstance)) {
        return new Member(
            member.getUuid(), member.getAddress().getHost(), member.getAddress().getPort());
      }
    }
    throw new ServerException("Cannot find Cluster with id:" + clusterId);
  }
 protected void destroyCacheOnAllMembers(String name, String callerUuid) {
   final OperationService operationService = nodeEngine.getOperationService();
   final Collection<Member> members = nodeEngine.getClusterService().getMembers();
   for (Member member : members) {
     if (!member.localMember() && !member.getUuid().equals(callerUuid)) {
       final CacheDestroyOperation op = new CacheDestroyOperation(name, true);
       operationService.invokeOnTarget(AbstractCacheService.SERVICE_NAME, op, member.getAddress());
     }
   }
 }
Ejemplo n.º 10
0
  /**
   * This is triggered when a new member joined to the cluster.
   *
   * @param membershipEvent contains the information about the added node.
   */
  @Override
  public void memberAdded(MembershipEvent membershipEvent) {
    Member member = membershipEvent.getMember();
    log.info(
        "Handling cluster gossip: New member joined to the cluster. Member Socket Address:"
            + member.getSocketAddress()
            + " UUID:"
            + member.getUuid());

    ClusterResourceHolder.getInstance().getClusterManager().memberAdded();
  }
Ejemplo n.º 11
0
 public static void encode(Member member, ClientMessage clientMessage) {
   AddressCodec.encode(member.getAddress(), clientMessage);
   clientMessage.set(member.getUuid());
   Map<String, Object> attributes = new HashMap<String, Object>(member.getAttributes());
   clientMessage.set(attributes.size());
   for (Map.Entry<String, Object> entry : attributes.entrySet()) {
     clientMessage.set(entry.getKey());
     Object value = entry.getValue();
     clientMessage.set(value.toString());
   }
 }
  @Test
  public void testSubmitCallableToMember() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    Callable<String> getUuidCallable = new GetMemberUuidTask();
    Member member = server.getCluster().getLocalMember();

    Future<String> result = service.submitToMember(getUuidCallable, member);

    assertEquals(member.getUuid(), result.get());
  }
Ejemplo n.º 13
0
 public static int calculateDataSize(Member member) {
   int dataSize = AddressCodec.calculateDataSize(member.getAddress());
   dataSize += ParameterUtil.calculateDataSize(member.getUuid());
   dataSize += Bits.INT_SIZE_IN_BYTES;
   Map<String, Object> attributes = member.getAttributes();
   for (Map.Entry<String, Object> entry : attributes.entrySet()) {
     dataSize += ParameterUtil.calculateDataSize(entry.getKey());
     Object value = entry.getValue();
     // TODO: this is costly to use toString
     dataSize += ParameterUtil.calculateDataSize(value.toString());
   }
   return dataSize;
 }
Ejemplo n.º 14
0
 /**
  * This is triggered when a node left the cluster.
  *
  * @param membershipEvent contains the information about the removed node.
  */
 @Override
 public void memberRemoved(MembershipEvent membershipEvent) {
   Member member = membershipEvent.getMember();
   log.info(
       "Handling cluster gossip: A member left the cluster. Member Socket Address:"
           + member.getSocketAddress()
           + " UUID:"
           + member.getUuid());
   ClusterManager clusterManager = ClusterResourceHolder.getInstance().getClusterManager();
   try {
     clusterManager.memberRemoved(member);
   } catch (Exception e) {
     log.error("Error while handling node removal, NodeID:" + clusterManager.getNodeId(member), e);
   }
 }
  @Test
  public void testSubmitCallableToMembers_withMemberSelector() throws Exception {
    IExecutorService service = client.getExecutorService(randomString());

    Callable<String> getUuidCallable = new GetMemberUuidTask();
    MemberSelector selectAll = new SelectAllMembers();

    Map<Member, Future<String>> map = service.submitToMembers(getUuidCallable, selectAll);
    for (Member member : map.keySet()) {
      Future<String> result = map.get(member);
      String uuid = result.get();

      assertEquals(member.getUuid(), uuid);
    }
  }
 @Override
 public void handle(String uuid, String key, int opType, String value) {
   Map<Address, Member> memberMap = clusterService.getMembersRef();
   if (memberMap == null) {
     return;
   }
   for (Member target : memberMap.values()) {
     if (target.getUuid().equals(uuid)) {
       final MemberAttributeOperationType operationType =
           MemberAttributeOperationType.getValue(opType);
       ((AbstractMember) target).updateAttribute(operationType, key, value);
       MemberAttributeEvent memberAttributeEvent =
           new MemberAttributeEvent(client.getCluster(), target, operationType, key, value);
       clusterService.fireMemberAttributeEvent(memberAttributeEvent);
       break;
     }
   }
 }
  @Test
  public void testExecuteOnMember() {
    final IExecutorService service = client.getExecutorService(randomString());
    final String mapName = randomString();

    final Member member = instance1.getCluster().getLocalMember();
    final String targetUuid = member.getUuid();

    service.executeOnMember(new MapPutRunnable(mapName), member);

    final IMap map = client.getMap(mapName);
    assertTrueEventually(
        new AssertTask() {
          public void run() throws Exception {
            assertTrue(map.containsKey(targetUuid));
          }
        });
  }
  @Override
  public void handle(Set<Member> initialMembers) {
    Map<String, Member> prevMembers = Collections.emptyMap();
    if (!members.isEmpty()) {
      prevMembers = new HashMap<String, Member>(members.size());
      for (Member member : members) {
        prevMembers.put(member.getUuid(), member);
      }
      members.clear();
    }

    for (Member initialMember : initialMembers) {
      members.add(initialMember);
    }

    final List<MembershipEvent> events = detectMembershipEvents(prevMembers);
    if (events.size() != 0) {
      applyMemberListChanges();
    }
    fireMembershipEvent(events);
    initialListFetchedLatch.countDown();
  }
 private String getIdFromMember(Member member) {
   return member.getUuid();
 }