Ejemplo n.º 1
0
 private void restoreClusterNode(ClusterNode clusterNode) {
   clusterNode.hostNode = topologyCache.getNode(clusterNode.getHost());
   // This will reset the lastHeartbeatTime
   clusterNode.heartbeat(clusterNode.getClusterNodeInfo());
   clusterNode.initResourceTypeToMaxCpuMap(cpuToResourcePartitioning);
   updateRunnability(clusterNode);
 }
Ejemplo n.º 2
0
  /**
   * return true if a new node has been added - else return false
   *
   * @param clusterNodeInfo the node that is heartbeating
   * @return true if this is a new node that has been added, false otherwise
   */
  public boolean heartbeat(ClusterNodeInfo clusterNodeInfo) throws DisallowedNode {
    ClusterNode node = nameToNode.get(clusterNodeInfo.name);
    if (!canAllowNode(clusterNodeInfo.getAddress().getHost())) {
      if (node != null) {
        node.heartbeat(clusterNodeInfo);
      } else {
        throw new DisallowedNode(clusterNodeInfo.getAddress().getHost());
      }
      return false;
    }
    boolean newNode = false;
    Map<ResourceType, String> currentResources = clusterNodeInfo.getResourceInfos();
    if (currentResources == null) {
      currentResources = new EnumMap<ResourceType, String>(ResourceType.class);
    }

    if (node == null) {
      LOG.info("Adding node with heartbeat: " + clusterNodeInfo.toString());
      node =
          new ClusterNode(
              clusterNodeInfo,
              topologyCache.getNode(clusterNodeInfo.address.host),
              cpuToResourcePartitioning);
      addNode(node, currentResources);
      newNode = true;
    }

    node.heartbeat(clusterNodeInfo);

    boolean appsChanged = false;
    Map<ResourceType, String> prevResources = nameToApps.get(clusterNodeInfo.name);
    Set<ResourceType> deletedApps = null;
    for (Map.Entry<ResourceType, String> entry : prevResources.entrySet()) {
      String newAppInfo = currentResources.get(entry.getKey());
      String oldAppInfo = entry.getValue();
      if (newAppInfo == null || !newAppInfo.equals(oldAppInfo)) {
        if (deletedApps == null) {
          deletedApps = EnumSet.noneOf(ResourceType.class);
        }
        deletedApps.add(entry.getKey());
        appsChanged = true;
      }
    }
    Map<ResourceType, String> addedApps = null;
    for (Map.Entry<ResourceType, String> entry : currentResources.entrySet()) {
      String newAppInfo = entry.getValue();
      String oldAppInfo = prevResources.get(entry.getKey());
      if (oldAppInfo == null || !oldAppInfo.equals(newAppInfo)) {
        if (addedApps == null) {
          addedApps = new EnumMap<ResourceType, String>(ResourceType.class);
        }
        addedApps.put(entry.getKey(), entry.getValue());
        appsChanged = true;
      }
    }
    if (deletedApps != null) {
      for (ResourceType deleted : deletedApps) {
        clusterManager.nodeAppRemoved(clusterNodeInfo.name, deleted);
      }
    }
    if (addedApps != null) {
      for (Map.Entry<ResourceType, String> added : addedApps.entrySet()) {
        addAppToNode(node, added.getKey(), added.getValue());
      }
    }

    updateRunnability(node);
    return newNode || appsChanged;
  }