private Object handleAuthenticated() {
    if (isOwnerConnection()) {
      final String uuid = getUuid();
      final String localMemberUUID = clientEngine.getLocalMember().getUuid();

      principal = new ClientPrincipal(uuid, localMemberUUID);
      reAuthLocal();
      Collection<Member> members = nodeEngine.getClusterService().getMembers();
      for (Member member : members) {
        if (!member.localMember()) {
          ClientReAuthOperation op = new ClientReAuthOperation(uuid);
          op.setCallerUuid(localMemberUUID);
          nodeEngine.getOperationService().send(op, member.getAddress());
        }
      }
    }

    boolean isNotMember =
        clientEngine.getClusterService().getMember(principal.getOwnerUuid()) == null;
    if (isNotMember) {
      throw new AuthenticationException(
          "Invalid owner-uuid: " + principal.getOwnerUuid() + ", it's not member of this cluster!");
    }

    endpoint.authenticated(principal, credentials, isOwnerConnection());
    setConnectionType();
    endpointManager.registerEndpoint(endpoint);
    clientEngine.bind(endpoint);

    final Address thisAddress = clientEngine.getThisAddress();
    return encodeAuth(thisAddress, principal.getUuid(), principal.getOwnerUuid());
  }
示例#2
0
  private void ensureConnectionToAllMembers() {
    boolean allConnected = false;
    if (node.joined()) {
      logger.finest("Waiting for all connections");
      int connectAllWaitSeconds =
          node.groupProperties.getSeconds(GroupProperty.CONNECT_ALL_WAIT_SECONDS);
      int checkCount = 0;
      while (checkCount++ < connectAllWaitSeconds && !allConnected) {
        try {
          //noinspection BusyWait
          TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException ignored) {
          EmptyStatement.ignore(ignored);
        }

        allConnected = true;
        Collection<Member> members = clusterService.getMembers();
        for (Member member : members) {
          if (!member.localMember()
              && node.connectionManager.getOrConnect(member.getAddress()) == null) {
            allConnected = false;
            if (logger.isFinestEnabled()) {
              logger.finest("Not-connected to " + member.getAddress());
            }
          }
        }
      }
    }
  }
 protected void updateCacheListenerConfigOnOtherNodes(
     CacheEntryListenerConfiguration<K, V> cacheEntryListenerConfiguration, boolean isRegister) {
   final Collection<Member> members = clientContext.getClusterService().getMemberList();
   final HazelcastClientInstanceImpl client =
       (HazelcastClientInstanceImpl) clientContext.getHazelcastInstance();
   final Collection<Future> futures = new ArrayList<Future>();
   for (Member member : members) {
     try {
       final Address address = member.getAddress();
       Data configData = toData(cacheEntryListenerConfiguration);
       final ClientMessage request =
           CacheListenerRegistrationCodec.encodeRequest(
               nameWithPrefix, configData, isRegister, address.getHost(), address.getPort());
       final ClientInvocation invocation = new ClientInvocation(client, request, address);
       final Future future = invocation.invoke();
       futures.add(future);
     } catch (Exception e) {
       ExceptionUtil.sneakyThrow(e);
     }
   }
   // make sure all configs are created
   // TODO do we need this ???s
   //        try {
   //            FutureUtil.waitWithDeadline(futures,
   // CacheProxyUtil.AWAIT_COMPLETION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
   //        } catch (TimeoutException e) {
   //            logger.warning(e);
   //        }
 }
示例#4
0
  private void shutdownNodes() {
    final Operation op = new ShutdownNodeOperation();

    logger.info("Sending shutting down operations to all members...");

    Collection<Member> members = getMembers(NON_LOCAL_MEMBER_SELECTOR);
    final long timeout =
        node.groupProperties.getNanos(GroupProperty.CLUSTER_SHUTDOWN_TIMEOUT_SECONDS);
    final long startTime = System.nanoTime();

    while ((System.nanoTime() - startTime) < timeout && !members.isEmpty()) {
      for (Member member : members) {
        nodeEngine.getOperationService().send(op, member.getAddress());
      }

      try {
        Thread.sleep(CLUSTER_SHUTDOWN_SLEEP_DURATION_IN_MILLIS);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        logger.warning("Shutdown sleep interrupted. ", e);
        break;
      }

      members = getMembers(NON_LOCAL_MEMBER_SELECTOR);
    }

    logger.info(
        "Number of other nodes remaining: "
            + getSize(NON_LOCAL_MEMBER_SELECTOR)
            + ". Shutting down itself.");
    node.shutdown(false);
  }
    public static void main(String[] args) throws Exception {
        HazelcastInstance hz = Hazelcast.newHazelcastInstance();
        Cluster cluster = hz.getCluster();

        cluster.addMembershipListener(new MembershipListener() {
            @Override
            public void memberAdded(MembershipEvent membershipEvent) {
                System.out.println( "********** MemberAdded " + membershipEvent );
            }

            @Override
            public void memberRemoved(MembershipEvent membershipEvent) {
                System.out.println( "********** MemberRemoved " + membershipEvent );
            }

            @Override
            public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
                System.out.println( "********** MemberAttributeChanged " + memberAttributeEvent );
            }
        });
Member localMember = cluster.getLocalMember();
        System.out.println ( "********** my inetAddress= " + localMember.getInetSocketAddress() );


    }
  @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());
  }
  @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());
  }
 @Override
 public boolean isClusterSafe() {
   final Node node = getNode();
   final Collection<Member> memberList = node.clusterService.getMembers();
   if (memberList == null || memberList.isEmpty() || memberList.size() < 2) {
     return true;
   }
   final Collection<Future> futures = new ArrayList<Future>(memberList.size());
   for (Member member : memberList) {
     final Address target = member.getAddress();
     final Operation operation = new SafeStateCheckOperation();
     final InternalCompletableFuture future =
         node.getNodeEngine()
             .getOperationService()
             .invokeOnTarget(InternalPartitionService.SERVICE_NAME, operation, target);
     futures.add(future);
   }
   // todo this max wait is appropriate?
   final int maxWaitTime = getMaxWaitTime(node);
   for (Future future : futures) {
     try {
       final Object result = future.get(maxWaitTime, TimeUnit.SECONDS);
       final boolean safe = (Boolean) result;
       if (!safe) {
         return false;
       }
     } catch (Exception e) {
       logger.warning("Error while querying cluster's safe state", e);
       return false;
     }
   }
   return true;
 }
示例#9
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;
 }
 HazelcastNodeStatus(final Member member_) {
   super(
       member_.getUuid(),
       member_.isLiteMember() ? "client" : "node",
       member_.getInetSocketAddress().toString());
   member = member_;
 }
示例#11
0
 @Override
 public Xid[] recover(int flag) throws XAException {
   NodeEngine nodeEngine = getNodeEngine();
   XAService xaService = getService();
   OperationService operationService = nodeEngine.getOperationService();
   ClusterService clusterService = nodeEngine.getClusterService();
   Collection<Member> memberList = clusterService.getMembers();
   List<InternalCompletableFuture<SerializableList>> futureList =
       new ArrayList<InternalCompletableFuture<SerializableList>>();
   for (Member member : memberList) {
     if (member.localMember()) {
       continue;
     }
     CollectRemoteTransactionsOperation op = new CollectRemoteTransactionsOperation();
     Address address = member.getAddress();
     InternalCompletableFuture<SerializableList> future =
         operationService.invokeOnTarget(SERVICE_NAME, op, address);
     futureList.add(future);
   }
   HashSet<SerializableXID> xids = new HashSet<SerializableXID>();
   xids.addAll(xaService.getPreparedXids());
   for (InternalCompletableFuture<SerializableList> future : futureList) {
     SerializableList xidSet = future.getSafely();
     for (Data xidData : xidSet) {
       SerializableXID xid = nodeEngine.toObject(xidData);
       xids.add(xid);
     }
   }
   return xids.toArray(new SerializableXID[xids.size()]);
 }
示例#12
0
  public void clearInternal() {
    try {
      Operation clearOperation = operationProvider.createClearOperation(name);
      clearOperation.setServiceName(SERVICE_NAME);
      BinaryOperationFactory factory = new BinaryOperationFactory(clearOperation, getNodeEngine());
      Map<Integer, Object> resultMap =
          operationService.invokeOnAllPartitions(SERVICE_NAME, factory);

      int numberOfAffectedEntries = 0;
      for (Object object : resultMap.values()) {
        numberOfAffectedEntries += (Integer) object;
      }

      MemberSelector selector =
          MemberSelectors.and(LITE_MEMBER_SELECTOR, NON_LOCAL_MEMBER_SELECTOR);
      for (Member member : getNodeEngine().getClusterService().getMembers(selector)) {
        operationService.invokeOnTarget(
            SERVICE_NAME, new ClearOperation(name), member.getAddress());
      }

      if (numberOfAffectedEntries > 0) {
        publishMapEvent(numberOfAffectedEntries, EntryEventType.CLEAR_ALL);
      }
    } catch (Throwable t) {
      throw rethrow(t);
    }
  }
  @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 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()));
  }
 private void updateMembersRef() {
   final Map<Address, Member> map = new LinkedHashMap<Address, Member>(members.size());
   for (Member member : members) {
     map.put(member.getAddress(), member);
   }
   clusterService.setMembersRef(Collections.unmodifiableMap(map));
 }
 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;
 }
示例#17
0
 private void invokeMemberRemoveOperation(Address deadAddress) {
   for (Member member : getMembers()) {
     Address address = member.getAddress();
     if (!thisAddress.equals(address) && !address.equals(deadAddress)) {
       nodeEngine.getOperationService().send(new MemberRemoveOperation(deadAddress), address);
     }
   }
 }
 @Override
 public Collection<Address> getTargets() {
   Collection<Member> memberList = nodeEngine.getClusterService().getMembers();
   Collection<Address> addresses = new HashSet<Address>();
   for (Member member : memberList) {
     addresses.add(member.getAddress());
   }
   return addresses;
 }
 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());
     }
   }
 }
示例#20
0
  @Override
  public Map<ClientType, Integer> getConnectedClientStats() {

    int numberOfCppClients = 0;
    int numberOfDotNetClients = 0;
    int numberOfJavaClients = 0;
    int numberOfOtherClients = 0;

    Operation clientInfoOperation = new GetConnectedClientsOperation();
    OperationService operationService = node.nodeEngine.getOperationService();
    Map<ClientType, Integer> resultMap = new HashMap<ClientType, Integer>();
    Map<String, ClientType> clientsMap = new HashMap<String, ClientType>();

    for (Member member : node.getClusterService().getMembers()) {
      Address target = member.getAddress();
      Future<Map<String, ClientType>> future =
          operationService.invokeOnTarget(SERVICE_NAME, clientInfoOperation, target);
      try {
        Map<String, ClientType> endpoints = future.get();
        if (endpoints == null) {
          continue;
        }
        // Merge connected clients according to their uuid.
        for (Map.Entry<String, ClientType> entry : endpoints.entrySet()) {
          clientsMap.put(entry.getKey(), entry.getValue());
        }
      } catch (Exception e) {
        logger.warning("Cannot get client information from: " + target.toString(), e);
      }
    }

    // Now we are regrouping according to the client type
    for (ClientType clientType : clientsMap.values()) {
      switch (clientType) {
        case JAVA:
          numberOfJavaClients++;
          break;
        case CSHARP:
          numberOfDotNetClients++;
          break;
        case CPP:
          numberOfCppClients++;
          break;
        default:
          numberOfOtherClients++;
      }
    }

    resultMap.put(ClientType.CPP, numberOfCppClients);
    resultMap.put(ClientType.CSHARP, numberOfDotNetClients);
    resultMap.put(ClientType.JAVA, numberOfJavaClients);
    resultMap.put(ClientType.OTHER, numberOfOtherClients);

    return resultMap;
  }
示例#21
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());
  }
 /**
  * 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 void memberRemoved(MembershipEvent membershipEvent) {
   synchronized (listenerRegLock) {
     Member member = membershipEvent.getMember();
     members.remove(member);
     for (Map<Address, ClientEventRegistration> registrationMap : registrations.values()) {
       ClientEventRegistration registration = registrationMap.remove(member.getAddress());
       removeEventHandler(registration.getCallId());
     }
   }
 }
示例#25
0
 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;
     }
   }
 }
 @Test
 public void testInstance() {
   assertNotNull(instance);
   final Set<Member> members = instance.getCluster().getMembers();
   assertEquals(1, members.size());
   final Member member = members.iterator().next();
   final InetSocketAddress inetSocketAddress = member.getSocketAddress();
   assertEquals(5700, inetSocketAddress.getPort());
   assertEquals("test-instance", config.getInstanceName());
   assertEquals("HAZELCAST_ENTERPRISE_LICENSE_KEY", config.getLicenseKey());
 }
  /**
   * 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();
  }
示例#28
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;
 }
示例#29
0
    private void callDisconnectionOperation(ClientEndpointImpl endpoint) {
      Collection<Member> memberList = nodeEngine.getClusterService().getMembers();
      OperationService operationService = nodeEngine.getOperationService();
      ClientDisconnectionOperation op = createClientDisconnectionOperation(endpoint.getUuid());
      operationService.runOperationOnCallingThread(op);

      for (Member member : memberList) {
        if (!member.localMember()) {
          op = createClientDisconnectionOperation(endpoint.getUuid());
          operationService.send(op, member.getAddress());
        }
      }
    }
示例#30
0
  private void assignNewMaster() {
    Address oldMasterAddress = node.getMasterAddress();
    if (node.joined()) {
      Collection<Member> members = getMembers();
      Member newMaster = null;
      int size = members.size();
      if (size > 1) {
        Iterator<Member> iterator = members.iterator();
        Member member = iterator.next();
        if (member.getAddress().equals(oldMasterAddress)) {
          newMaster = iterator.next();
        } else {
          logger.severe(
              format(
                  "Old master %s is dead, but the first of member list is a different member %s!",
                  oldMasterAddress, member));
          newMaster = member;
        }
      } else {
        logger.warning(
            format(
                "Old master %s is dead and this node is not master, "
                    + "but member list contains only %d members: %s",
                oldMasterAddress, size, members));
      }
      logger.info(
          format(
              "Old master %s left the cluster, assigning new master %s",
              oldMasterAddress, newMaster));
      if (newMaster != null) {
        node.setMasterAddress(newMaster.getAddress());
      } else {
        node.setMasterAddress(null);
      }
    } else {
      node.setMasterAddress(null);
    }

    if (logger.isFinestEnabled()) {
      logger.finest(
          format("Old master: %s, new master: %s ", oldMasterAddress, node.getMasterAddress()));
    }

    if (node.isMaster()) {
      clusterHeartbeatManager.resetMemberMasterConfirmations();
      clusterClock.reset();
    } else {
      clusterHeartbeatManager.sendMasterConfirmation();
    }
  }