예제 #1
0
  public Runnable prepareMergeRunnable() {
    Map<MapContainer, Collection<Record>> recordMap =
        new HashMap<MapContainer, Collection<Record>>(mapContainers.size());
    InternalPartitionService partitionService = nodeEngine.getPartitionService();
    int partitionCount = partitionService.getPartitionCount();
    Address thisAddress = nodeEngine.getClusterService().getThisAddress();

    for (MapContainer mapContainer : mapContainers.values()) {
      for (int i = 0; i < partitionCount; i++) {
        RecordStore recordStore = getPartitionContainer(i).getRecordStore(mapContainer.getName());
        // add your owned entries to the map so they will be merged
        if (thisAddress.equals(partitionService.getPartitionOwner(i))) {
          Collection<Record> records = recordMap.get(mapContainer);
          if (records == null) {
            records = new ArrayList<Record>();
            recordMap.put(mapContainer, records);
          }
          records.addAll(recordStore.getReadonlyRecordMap().values());
        }
        // clear all records either owned or backup
        recordStore.reset();
      }
    }
    return new Merger(recordMap);
  }
  @Override
  public void shutdown() {
    for (Address address : nodes.keySet()) {
      if (address.equals(node.getThisAddress())) {
        continue;
      }

      final NodeEngineImpl nodeEngine = nodes.get(address);
      if (nodeEngine != null && nodeEngine.isRunning()) {
        nodeEngine
            .getExecutionService()
            .execute(
                ExecutionService.SYSTEM_EXECUTOR,
                new Runnable() {
                  public void run() {
                    ClusterServiceImpl clusterService =
                        (ClusterServiceImpl) nodeEngine.getClusterService();
                    clusterService.removeAddress(node.getThisAddress());
                  }
                });
      }
    }
    for (MockConnection connection : mapConnections.values()) {
      connection.close();
    }
  }
 /**
  * Binding completes the connection and makes it available to be used with the ConnectionManager.
  */
 private boolean bind(
     TcpIpConnection connection, Address remoteEndPoint, Address localEndpoint, boolean reply) {
   if (logger.isFinestEnabled()) {
     logger.finest("Binding " + connection + " to " + remoteEndPoint + ", reply is " + reply);
   }
   final Address thisAddress = ioService.getThisAddress();
   if (ioService.isSocketBindAny()
       && !connection.isClient()
       && !thisAddress.equals(localEndpoint)) {
     logger.warning(
         "Wrong bind request from "
             + remoteEndPoint
             + "! This node is not requested endpoint: "
             + localEndpoint);
     connection.close();
     return false;
   }
   connection.setEndPoint(remoteEndPoint);
   ioService.onSuccessfulConnection(remoteEndPoint);
   if (reply) {
     sendBindRequest(connection, remoteEndPoint, false);
   }
   if (checkAlreadyConnected(connection, remoteEndPoint)) {
     return false;
   }
   return registerConnection(remoteEndPoint, connection);
 }
예제 #4
0
  // CHECKSTYLE:OFF
  // This equals method is to complex for our rules due to many internal object type members
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    MigrationInfo that = (MigrationInfo) o;

    if (partitionId != that.partitionId) {
      return false;
    }
    if (destination != null ? !destination.equals(that.destination) : that.destination != null) {
      return false;
    }
    if (masterUuid != null ? !masterUuid.equals(that.masterUuid) : that.masterUuid != null) {
      return false;
    }
    if (source != null ? !source.equals(that.source) : that.source != null) {
      return false;
    }

    return true;
  }
예제 #5
0
 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);
   //        }
 }
 private static ClientConfig getClientConfig(HazelcastInstance instance) {
   ClientConfig clientConfig = new ClientConfig();
   Address address = getNode(instance).address;
   clientConfig.getNetworkConfig().addAddress(address.getHost() + ":" + address.getPort());
   clientConfig.getGroupConfig().setName(instance.getConfig().getGroupConfig().getName());
   return clientConfig;
 }
예제 #7
0
 private boolean isLocalAddress(final Address address) throws UnknownHostException {
   final Address thisAddress = node.getThisAddress();
   final boolean local = thisAddress.getInetSocketAddress().equals(address.getInetSocketAddress());
   if (logger.isFinestEnabled()) {
     logger.finest(address + " is local? " + local);
   }
   return local;
 }
예제 #8
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);
     }
   }
 }
예제 #9
0
 @Override
 public int hashCode() {
   int result = partitionId;
   result = 31 * result + (source != null ? source.hashCode() : 0);
   result = 31 * result + (destination != null ? destination.hashCode() : 0);
   result = 31 * result + (masterUuid != null ? masterUuid.hashCode() : 0);
   return result;
 }
예제 #10
0
 private void sendRemoveMemberToOthers(final Address deadAddress) {
   for (MemberImpl member : lsMembers) {
     Address address = member.getAddress();
     if (!thisAddress.equals(address) && !address.equals(deadAddress)) {
       sendProcessableTo(new MemberRemover(deadAddress), address);
     }
   }
 }
 protected final boolean isValid() {
   final ClusterServiceImpl clusterService = getService();
   final Connection conn = getConnection();
   final Address masterAddress = conn != null ? conn.getEndPoint() : null;
   return conn == null
       || // which means this is a local call.
       (masterAddress != null && masterAddress.equals(clusterService.getMasterAddress()));
 }
예제 #12
0
  public LocalMapStats createStats(String name) {
    LocalMultiMapStatsImpl stats = getLocalMultiMapStatsImpl(name);
    long ownedEntryCount = 0;
    long backupEntryCount = 0;
    long hits = 0;
    long lockedEntryCount = 0;
    ClusterServiceImpl clusterService = (ClusterServiceImpl) nodeEngine.getClusterService();

    Address thisAddress = clusterService.getThisAddress();
    for (int i = 0; i < nodeEngine.getPartitionService().getPartitionCount(); i++) {
      InternalPartition partition = nodeEngine.getPartitionService().getPartition(i);
      MultiMapPartitionContainer partitionContainer = getPartitionContainer(i);
      MultiMapContainer multiMapContainer = partitionContainer.getCollectionContainer(name);
      if (multiMapContainer == null) {
        continue;
      }
      Address owner = partition.getOwnerOrNull();
      if (owner != null) {
        if (owner.equals(thisAddress)) {
          lockedEntryCount += multiMapContainer.getLockedCount();
          for (MultiMapWrapper wrapper : multiMapContainer.multiMapWrappers.values()) {
            hits += wrapper.getHits();
            ownedEntryCount += wrapper.getCollection(false).size();
          }
        } else {
          int backupCount = multiMapContainer.config.getTotalBackupCount();
          for (int j = 1; j <= backupCount; j++) {
            Address replicaAddress = partition.getReplicaAddress(j);
            int memberSize = nodeEngine.getClusterService().getMembers().size();

            int tryCount = REPLICA_ADDRESS_TRY_COUNT;
            // wait if the partition table is not updated yet
            while (memberSize > backupCount && replicaAddress == null && tryCount-- > 0) {
              try {
                Thread.sleep(REPLICA_ADDRESS_SLEEP_WAIT_MILLIS);
              } catch (InterruptedException e) {
                throw ExceptionUtil.rethrow(e);
              }
              replicaAddress = partition.getReplicaAddress(j);
            }

            if (replicaAddress != null && replicaAddress.equals(thisAddress)) {
              for (MultiMapWrapper wrapper : multiMapContainer.multiMapWrappers.values()) {
                backupEntryCount += wrapper.getCollection(false).size();
              }
            }
          }
        }
      }
    }
    stats.setOwnedEntryCount(ownedEntryCount);
    stats.setBackupEntryCount(backupEntryCount);
    stats.setHits(hits);
    stats.setLockedEntryCount(lockedEntryCount);
    return stats;
  }
예제 #13
0
 public void writeData(DataOutput out) throws IOException {
   out.writeUTF(script);
   out.writeUTF(engine);
   out.writeBoolean(targetAllMembers);
   out.writeInt(targets.size());
   for (Address target : targets) {
     target.writeData(out);
   }
   writeMap(out, bindings);
 }
예제 #14
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;
  }
예제 #15
0
 private MemberImpl createMember(MemberInfo memberInfo, String ipV6ScopeId) {
   Address address = memberInfo.getAddress();
   address.setScopeId(ipV6ScopeId);
   return new MemberImpl(
       address,
       thisAddress.equals(address),
       memberInfo.getUuid(),
       (HazelcastInstanceImpl) nodeEngine.getHazelcastInstance(),
       memberInfo.getAttributes(),
       memberInfo.isLiteMember());
 }
예제 #16
0
 @Override
 public void writeData(ObjectDataOutput out) throws IOException {
   out.writeInt(members.length);
   for (Address member : members) {
     member.writeData(out);
   }
   out.writeInt(ownerIndexes.length);
   for (int index : ownerIndexes) {
     out.writeInt(index);
   }
 }
예제 #17
0
 @Override
 public void connectionRemoved(Connection connection) {
   if (logger.isFinestEnabled()) {
     logger.finest("Removed connection " + connection.getEndPoint());
   }
   if (!node.joined()) {
     Address masterAddress = node.getMasterAddress();
     if (masterAddress != null && masterAddress.equals(connection.getEndPoint())) {
       node.setMasterAddress(null);
     }
   }
 }
예제 #18
0
 public void readData(DataInput in) throws IOException {
   script = in.readUTF();
   engine = in.readUTF();
   targetAllMembers = in.readBoolean();
   int size = in.readInt();
   targets = new HashSet<Address>(size);
   for (int i = 0; i < size; i++) {
     Address target = new Address();
     target.readData(in);
     targets.add(target);
   }
   bindings = readMap(in);
 }
예제 #19
0
 @Override
 public void run() {
   final long now = Clock.currentTimeMillis();
   final String mapName = this.mapName;
   final MapServiceContext mapServiceContext = this.mapServiceContext;
   final NodeEngine nodeEngine = mapServiceContext.getNodeEngine();
   final ClusterService clusterService = nodeEngine.getClusterService();
   final InternalPartitionService partitionService = nodeEngine.getPartitionService();
   final Address thisAddress = clusterService.getThisAddress();
   final int partitionCount = partitionService.getPartitionCount();
   Map<Integer, Integer> partitionToEntryCountHolder = Collections.emptyMap();
   List<DelayedEntry> entries = Collections.emptyList();
   boolean createLazy = true;
   for (int partitionId = 0; partitionId < partitionCount; partitionId++) {
     final InternalPartition partition = partitionService.getPartition(partitionId, false);
     final Address owner = partition.getOwnerOrNull();
     final RecordStore recordStore = getRecordStoreOrNull(mapName, partitionId);
     if (owner == null || recordStore == null) {
       // no-op because no owner is set yet.
       // Therefore we don't know anything about the map
       continue;
     }
     final WriteBehindQueue<DelayedEntry> queue = getWriteBehindQueue(recordStore);
     final List<DelayedEntry> delayedEntries = filterItemsLessThanOrEqualToTime(queue, now);
     if (delayedEntries.isEmpty()) {
       continue;
     }
     if (!owner.equals(thisAddress)) {
       if (now > lastRunTime + backupRunIntervalTime) {
         doInBackup(queue, delayedEntries, partitionId);
       }
       continue;
     }
     // initialize when needed, we do not want
     // to create these on backups for every second.
     if (createLazy) {
       partitionToEntryCountHolder = new HashMap<Integer, Integer>();
       entries = new ArrayList<DelayedEntry>();
       createLazy = false;
     }
     partitionToEntryCountHolder.put(partitionId, delayedEntries.size());
     entries.addAll(delayedEntries);
   }
   if (!entries.isEmpty()) {
     final Map<Integer, List<DelayedEntry>> failsPerPartition =
         writeBehindProcessor.process(entries);
     removeProcessed(mapName, getEntryPerPartitionMap(entries));
     addFailsToQueue(mapName, failsPerPartition);
     lastRunTime = now;
   }
 }
예제 #20
0
 private boolean isThisNodeMasterCandidate(Collection<Address> possibleAddresses) {
   int thisHashCode = node.getThisAddress().hashCode();
   for (Address address : possibleAddresses) {
     if (isBlacklisted(address)) {
       continue;
     }
     if (node.connectionManager.getConnection(address) != null) {
       if (thisHashCode > address.hashCode()) {
         return false;
       }
     }
   }
   return true;
 }
예제 #21
0
 protected Collection<Address> getMemberAddresses() {
   Address thisAddress = getNodeEngine().getThisAddress();
   Collection<Member> members =
       getNodeEngine().getClusterService().getMembers(MemberSelectors.DATA_MEMBER_SELECTOR);
   Collection<Address> addresses = new ArrayList<Address>();
   for (Member member : members) {
     Address address = member.getAddress();
     if (address.equals(thisAddress)) {
       continue;
     }
     addresses.add(address);
   }
   return addresses;
 }
예제 #22
0
 @Override
 public void readData(ObjectDataInput in) throws IOException {
   int len = in.readInt();
   members = new Address[len];
   for (int i = 0; i < len; i++) {
     Address a = new Address();
     a.readData(in);
     members[i] = a;
   }
   len = in.readInt();
   ownerIndexes = new int[len];
   for (int i = 0; i < len; i++) {
     ownerIndexes[i] = in.readInt();
   }
 }
예제 #23
0
  void doRemoveAddress(Address deadAddress, boolean destroyConnection) {
    if (!ensureMemberIsRemovable(deadAddress)) {
      return;
    }

    lock.lock();
    try {
      if (deadAddress.equals(node.getMasterAddress())) {
        assignNewMaster();
      }
      if (node.isMaster()) {
        clusterJoinManager.removeJoin(new MemberInfo(deadAddress));
      }
      Connection conn = node.connectionManager.getConnection(deadAddress);
      if (destroyConnection && conn != null) {
        node.connectionManager.destroyConnection(conn);
      }
      MemberImpl deadMember = getMember(deadAddress);
      if (deadMember != null) {
        removeMember(deadMember);
        logger.info(membersString());
      }
    } finally {
      lock.unlock();
    }
  }
예제 #24
0
 /**
  * @param queue write behind queue.
  * @param delayedEntries entries to be processed.
  * @param partitionId corresponding partition id.
  */
 private void doInBackup(
     final WriteBehindQueue queue,
     final List<DelayedEntry> delayedEntries,
     final int partitionId) {
   final NodeEngine nodeEngine = mapServiceContext.getNodeEngine();
   final ClusterService clusterService = nodeEngine.getClusterService();
   final InternalPartitionService partitionService = nodeEngine.getPartitionService();
   final Address thisAddress = clusterService.getThisAddress();
   final InternalPartition partition = partitionService.getPartition(partitionId, false);
   final Address owner = partition.getOwnerOrNull();
   if (owner != null && !owner.equals(thisAddress)) {
     writeBehindProcessor.callBeforeStoreListeners(delayedEntries);
     removeProcessed(mapName, getEntryPerPartitionMap(delayedEntries));
     writeBehindProcessor.callAfterStoreListeners(delayedEntries);
   }
 }
 @Override
 protected void writeInternal(ObjectDataOutput out) throws IOException {
   super.writeInternal(out);
   out.writeUTF(newState.toString());
   initiator.writeData(out);
   out.writeUTF(txnId);
 }
예제 #26
0
  // CHECKSTYLE:OFF
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    Registration that = (Registration) o;

    if (id != null ? !id.equals(that.id) : that.id != null) {
      return false;
    }
    if (serviceName != null ? !serviceName.equals(that.serviceName) : that.serviceName != null) {
      return false;
    }
    if (topic != null ? !topic.equals(that.topic) : that.topic != null) {
      return false;
    }
    if (filter != null ? !filter.equals(that.filter) : that.filter != null) {
      return false;
    }
    if (subscriber != null ? !subscriber.equals(that.subscriber) : that.subscriber != null) {
      return false;
    }

    return true;
  }
예제 #27
0
 @Override
 public void onDisconnect(final Address dead) {
   if (dead.equals(target)) {
     removeRemoteCall(getCallId());
     setResult(Boolean.FALSE);
   }
 }
예제 #28
0
  private void joinViaTargetMember(Address targetAddress, long maxJoinMillis) {
    try {
      if (targetAddress == null) {
        throw new IllegalArgumentException("Invalid target address -> NULL");
      }
      if (logger.isFinestEnabled()) {
        logger.finest("Joining over target member " + targetAddress);
      }
      if (targetAddress.equals(node.getThisAddress()) || isLocalAddress(targetAddress)) {
        node.setAsMaster();
        return;
      }
      long joinStartTime = Clock.currentTimeMillis();
      Connection connection;
      while (node.isRunning()
          && !node.joined()
          && (Clock.currentTimeMillis() - joinStartTime < maxJoinMillis)) {

        connection = node.connectionManager.getOrConnect(targetAddress);
        if (connection == null) {
          //noinspection BusyWait
          Thread.sleep(JOIN_RETRY_WAIT_TIME);
          continue;
        }
        if (logger.isFinestEnabled()) {
          logger.finest("Sending joinRequest " + targetAddress);
        }
        clusterJoinManager.sendJoinRequest(targetAddress, true);
        //noinspection BusyWait
        Thread.sleep(JOIN_RETRY_WAIT_TIME);
      }
    } catch (final Exception e) {
      logger.warning(e);
    }
  }
예제 #29
0
 public InetAddress getInetAddress() {
   try {
     return localEndpoint.getInetAddress();
   } catch (UnknownHostException e) {
     throw ExceptionUtil.rethrow(e);
   }
 }
예제 #30
0
 @Override
 public void writeData(ObjectDataOutput out) throws IOException {
   out.writeUTF(id);
   out.writeUTF(serviceName);
   out.writeUTF(topic);
   subscriber.writeData(out);
   out.writeObject(filter);
 }