Exemplo n.º 1
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;
  }
Exemplo n.º 2
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);
     }
   }
 }
Exemplo n.º 3
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;
  }
 /**
  * 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);
 }
Exemplo n.º 5
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);
    }
  }
Exemplo n.º 6
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;
  }
  @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();
    }
  }
Exemplo n.º 8
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();
    }
  }
Exemplo n.º 9
0
 @Override
 public void onDisconnect(final Address dead) {
   if (dead.equals(target)) {
     removeRemoteCall(getCallId());
     setResult(Boolean.FALSE);
   }
 }
Exemplo n.º 10
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);
  }
Exemplo n.º 11
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()));
 }
Exemplo n.º 13
0
 private HazelcastInstance getInstanceForAddress(HazelcastInstance[] instances, Address address) {
   for (HazelcastInstance instance : instances) {
     Address instanceAddress = instance.getCluster().getLocalMember().getAddress();
     if (address.equals(instanceAddress)) {
       return instance;
     }
   }
   throw new IllegalArgumentException();
 }
Exemplo n.º 14
0
 private boolean ensureMemberIsRemovable(Address deadAddress) {
   if (!node.joined()) {
     return false;
   }
   if (deadAddress.equals(thisAddress)) {
     return false;
   }
   return true;
 }
Exemplo n.º 15
0
 private void closeIfAddressMatches(Address address) {
   final ClientConnection currentOwnerConnection = ownerConnection;
   if (currentOwnerConnection == null || !currentOwnerConnection.live()) {
     return;
   }
   if (address.equals(currentOwnerConnection.getRemoteEndpoint())) {
     close();
   }
 }
Exemplo n.º 16
0
 void doRemoveAddress(Address deadAddress, boolean destroyConnection) {
   checkServiceThread();
   logger.log(Level.INFO, "Removing Address " + deadAddress);
   if (!node.joined()) {
     node.failedConnection(deadAddress);
     return;
   }
   if (deadAddress.equals(thisAddress)) return;
   if (deadAddress.equals(getMasterAddress())) {
     if (node.joined()) {
       MemberImpl newMaster = getNextMemberAfter(deadAddress, false, 1);
       if (newMaster != null) node.setMasterAddress(newMaster.getAddress());
       else node.setMasterAddress(null);
     } else {
       node.setMasterAddress(null);
     }
     logger.log(Level.FINEST, "Now Master " + node.getMasterAddress());
   }
   if (isMaster()) {
     setJoins.remove(new MemberInfo(deadAddress));
     resetMemberMasterConfirmations();
   }
   final Connection conn = node.connectionManager.getConnection(deadAddress);
   if (destroyConnection && conn != null) {
     node.connectionManager.destroyConnection(conn);
   }
   MemberImpl deadMember = getMember(deadAddress);
   if (deadMember != null) {
     removeMember(deadMember);
     node.getClusterImpl().setMembers(lsMembers);
     node.concurrentMapManager.syncForDead(deadMember);
     node.blockingQueueManager.syncForDead(deadAddress);
     node.listenerManager.syncForDead(deadAddress);
     node.topicManager.syncForDead(deadAddress);
     // node.getClusterImpl().setMembers(lsMembers); // shifted up to get members in syncForDead
     // methods
     disconnectExistingCalls(deadAddress);
     if (isMaster()) {
       logger.log(Level.FINEST, deadAddress + " is dead. Sending remove to all other members.");
       sendRemoveMemberToOthers(deadAddress);
     }
     logger.log(Level.INFO, this.toString());
   }
 }
 Object call(Address address, Callable callable) {
   Set<Member> members = factory.getCluster().getMembers();
   for (Member member : members) {
     if (address.equals(((MemberImpl) member).getAddress())) {
       DistributedTask task = new DistributedTask(callable, member);
       return executeTaskAndGet(task);
     }
   }
   return null;
 }
Exemplo n.º 18
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());
 }
Exemplo n.º 19
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);
     }
   }
 }
Exemplo n.º 20
0
    private void sendHeartbeats(Address address, long[] callIds) {
      heartbeatPacketsSend.inc();

      if (address.equals(thisAddress)) {
        scheduler.execute(new ProcessOperationHeartbeatsTask(callIds));
      } else {
        Packet packet =
            new Packet(serializationService.toBytes(callIds))
                .setAllFlags(FLAG_OP | FLAG_OP_CONTROL | FLAG_URGENT);
        nodeEngine.getNode().getConnectionManager().transmit(packet, address);
      }
    }
  @Override
  public boolean registerConnection(final Address remoteEndPoint, final Connection connection) {
    if (remoteEndPoint.equals(ioService.getThisAddress())) {
      return false;
    }

    if (connection instanceof TcpIpConnection) {
      TcpIpConnection tcpConnection = (TcpIpConnection) connection;
      Address currentEndPoint = tcpConnection.getEndPoint();
      if (currentEndPoint != null && !currentEndPoint.equals(remoteEndPoint)) {
        throw new IllegalArgumentException(
            connection + " has already a different endpoint than: " + remoteEndPoint);
      }
      tcpConnection.setEndPoint(remoteEndPoint);

      if (!connection.isClient()) {
        TcpIpConnectionMonitor connectionMonitor = getConnectionMonitor(remoteEndPoint, true);
        tcpConnection.setMonitor(connectionMonitor);
      }
    }
    connectionsMap.put(remoteEndPoint, connection);
    connectionsInProgress.remove(remoteEndPoint);
    ioService
        .getEventService()
        .executeEventCallback(
            new StripedRunnable() {
              @Override
              public void run() {
                for (ConnectionListener listener : connectionListeners) {
                  listener.connectionAdded(connection);
                }
              }

              @Override
              public int getKey() {
                return remoteEndPoint.hashCode();
              }
            });
    return true;
  }
Exemplo n.º 22
0
 public void sendMemberListToMember(Address target) {
   if (!isMaster()) {
     return;
   }
   if (thisAddress.equals(target)) {
     return;
   }
   Collection<MemberImpl> members = getMemberImpls();
   MemberInfoUpdateOperation op =
       new MemberInfoUpdateOperation(
           createMemberInfoList(members), clusterClock.getClusterTime(), false);
   nodeEngine.getOperationService().send(op, target);
 }
Exemplo n.º 23
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;
   }
 }
Exemplo n.º 24
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;
 }
Exemplo n.º 25
0
 public void invalidateScheduledActionsFor(Address endpoint, Set<Integer> threadIds) {
   if (!node.joined() || !node.isActive()) return;
   if (setScheduledActions.size() > 0) {
     Iterator<ScheduledAction> it = setScheduledActions.iterator();
     while (it.hasNext()) {
       ScheduledAction sa = it.next();
       Request request = sa.getRequest();
       if (endpoint.equals(request.caller) && threadIds.contains(request.lockThreadId)) {
         sa.setValid(false);
         it.remove();
       }
     }
   }
 }
 private boolean connectAndSendJoinRequest(Address masterAddress) {
   if (masterAddress == null || masterAddress.equals(node.address)) {
     throw new IllegalArgumentException();
   }
   Connection conn = node.connectionManager.getOrConnect(masterAddress);
   logger.log(Level.FINEST, "Master connection " + conn);
   systemLogService.logJoin("Master connection " + conn);
   if (conn != null) {
     return node.clusterManager.sendJoinRequest(masterAddress, true);
   } else {
     logger.log(Level.INFO, "Connecting to master node: " + masterAddress);
     return false;
   }
 }
Exemplo n.º 27
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);
   }
 }
Exemplo n.º 28
0
 public Set<Data> localKeySet(String name) {
   Set<Data> keySet = new HashSet<Data>();
   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;
     }
     if (thisAddress.equals(partition.getOwnerOrNull())) {
       keySet.addAll(multiMapContainer.keySet());
     }
   }
   getLocalMultiMapStatsImpl(name).incrementOtherOperations();
   return keySet;
 }
Exemplo n.º 29
0
  void addMembersRemovedInNotActiveState(Collection<Address> addresses) {
    lock.lock();
    try {
      Map<Address, MemberImpl> membersRemovedInNotActiveState =
          new LinkedHashMap<Address, MemberImpl>(membersRemovedInNotActiveStateRef.get());

      for (Address address : addresses) {
        if (thisAddress.equals(address)) {
          continue;
        }
        membersRemovedInNotActiveState.put(address, new MemberImpl(address, false));
      }

      membersRemovedInNotActiveStateRef.set(
          Collections.unmodifiableMap(membersRemovedInNotActiveState));
    } finally {
      lock.unlock();
    }
  }
Exemplo n.º 30
0
  public boolean send(Response response, Address target) {
    checkNotNull(target, "Target is required!");

    if (thisAddress.equals(target)) {
      throw new IllegalArgumentException(
          "Target is this node! -> " + target + ", response: " + response);
    }

    byte[] bytes = serializationService.toBytes(response);
    Packet packet = new Packet(bytes, -1).setAllFlags(FLAG_OP | FLAG_RESPONSE);

    if (response.isUrgent()) {
      packet.setFlag(FLAG_URGENT);
    }

    ConnectionManager connectionManager = node.getConnectionManager();
    Connection connection = connectionManager.getOrConnect(target);
    return connectionManager.transmit(packet, connection);
  }