private boolean hasSamePortAndStatus(
     AgentInfo agentInfo, AgentControllerIdentityImplementation agentIdentity) {
   if (agentInfo == null) {
     return false;
   }
   AgentManager agentManager = getAgentManager();
   return agentInfo.getPort() == agentManager.getAgentConnectingPort(agentIdentity)
       && agentInfo.getStatus() == agentManager.getAgentState(agentIdentity);
 }
 /**
  * Add the agent system data model share request on cache.
  *
  * @param id agent id in db.
  */
 @Override
 public void requestShareAgentSystemDataModel(Long id) {
   AgentInfo agent = getAgent(id, false);
   if (agent == null) {
     return;
   }
   agentRequestCache.put(
       extractRegionFromAgentRegion(agent.getRegion()) + "|" + createAgentKey(agent),
       new ClustedAgentRequest(agent.getIp(), agent.getName(), SHARE_AGENT_SYSTEM_DATA_MODEL));
 }
 /**
  * Stop agent. In cluster mode, it queues the agent stop request to agentRequestCache.
  *
  * @param id agent id in db
  */
 @Override
 public void stopAgent(Long id) {
   AgentInfo agent = getAgent(id, false);
   if (agent == null) {
     return;
   }
   agentRequestCache.put(
       extractRegionFromAgentRegion(agent.getRegion()) + "|" + createAgentKey(agent),
       new ClustedAgentRequest(agent.getIp(), agent.getName(), STOP_AGENT));
 }
 /**
  * Get all visible agents from DB.
  *
  * @return agent list
  */
 @Override
 public List<AgentInfo> getAllVisibleAgentInfoFromDB() {
   List<AgentInfo> result = Lists.newArrayList();
   Set<String> regions = getRegions();
   for (AgentInfo each : getAgentRepository().findAll(visible())) {
     if (regions.contains(extractRegionFromAgentRegion(each.getRegion()))) {
       result.add(each);
     }
   }
   return result;
 }
Пример #5
0
 /**
  * Approve/Unapprove the agent on given ip.
  *
  * @param ip ip
  * @param approve true/false
  */
 @CacheEvict(allEntries = true, value = "agents")
 public void approve(String ip, boolean approve) {
   List<AgentInfo> found = agentRepository.findAllByIp(ip);
   for (AgentInfo each : found) {
     each.setApproved(approve);
     agentRepository.save(each);
     agentRepository.findOne(each.getId());
     if (approve) {
       LOGGER.info("agent {} is approved", ip);
     } else {
       LOGGER.info("agent {} is not approved", ip);
     }
   }
 }
  /**
   * get the available agent count map in all regions of the user, including the free agents and
   * user specified agents.
   *
   * @param user current user
   * @return user available agent count map
   */
  @Override
  @Transactional
  public Map<String, MutableInt> getUserAvailableAgentCountMap(User user) {
    Set<String> regions = getRegions();
    Map<String, MutableInt> availShareAgents = newHashMap(regions);
    Map<String, MutableInt> availUserOwnAgent = newHashMap(regions);
    for (String region : regions) {
      availShareAgents.put(region, new MutableInt(0));
      availUserOwnAgent.put(region, new MutableInt(0));
    }
    String myAgentSuffix = "_owned_" + user.getUserId();

    for (AgentInfo agentInfo : getAllActiveAgentInfoFromDB()) {
      // Skip the all agents which doesn't approved, is inactive or
      // doesn't have region
      // prefix.
      if (!agentInfo.isApproved()) {
        continue;
      }

      String fullRegion = agentInfo.getRegion();
      String region = extractRegionFromAgentRegion(fullRegion);
      if (StringUtils.isBlank(region) || !regions.contains(region)) {
        continue;
      }
      // It's my own agent
      if (fullRegion.endsWith(myAgentSuffix)) {
        incrementAgentCount(availUserOwnAgent, region, user.getUserId());
      } else if (fullRegion.contains("_owned_")) {
        // If it's the others agent.. skip..
        continue;
      } else {
        incrementAgentCount(availShareAgents, region, user.getUserId());
      }
    }

    int maxAgentSizePerConsole = getMaxAgentSizePerConsole();

    for (String region : regions) {
      MutableInt mutableInt = availShareAgents.get(region);
      int shareAgentCount = mutableInt.intValue();
      mutableInt.setValue(Math.min(shareAgentCount, maxAgentSizePerConsole));
      mutableInt.add(availUserOwnAgent.get(region));
    }
    return availShareAgents;
  }
Пример #7
0
 @CacheEvict(allEntries = true, value = "agents")
 private AgentInfo creatAgentInfo(
     AgentControllerIdentityImplementation agentIdentity, List<AgentInfo> agents) {
   AgentInfo agentInfo = new AgentInfo();
   for (AgentInfo each : agents) {
     if (each != null && StringUtils.equals(each.getIp(), agentIdentity.getIp())) {
       agentInfo = each;
       break;
     }
   }
   if (!StringUtils.equals(agentInfo.getHostName(), agentIdentity.getName())
       || !StringUtils.equals(agentInfo.getRegion(), agentIdentity.getRegion())) {
     agentInfo.setHostName(agentIdentity.getName());
     agentInfo.setRegion(agentIdentity.getRegion());
     agentInfo.setIp(agentIdentity.getIp());
     agentInfo = agentRepository.save(agentInfo);
   }
   agentInfo.setPort(agentManager.getAgentConnectingPort(agentIdentity));
   agentInfo.setStatus(agentManager.getAgentState(agentIdentity));
   // need to save agent info into DB, like ip and port maybe changed.
   return agentInfo;
 }
Пример #8
0
 /**
  * Get a agent on given id.
  *
  * @param id agent id
  * @return agent
  */
 public AgentInfo getAgent(long id) {
   AgentInfo agentInfo = agentRepository.findOne(id);
   if (agentInfo == null) {
     return null;
   }
   AgentControllerIdentityImplementation agentIdentity =
       agentManager.getAgentIdentityByIp(agentInfo.getIp());
   if (agentIdentity != null) {
     agentInfo.setStatus(agentManager.getAgentState(agentIdentity));
     agentInfo.setPort(agentManager.getAgentConnectingPort(agentIdentity));
     agentInfo.setHostName(agentIdentity.getName());
     agentInfo.setRegion(agentIdentity.getRegion());
     agentInfo.setAgentIdentity(agentIdentity);
   }
   return agentInfo;
 }
  /**
   * Run a scheduled task to check the agent status.
   *
   * @since 3.1
   */
  public void checkAgentStatus() {
    List<AgentInfo> changeAgents = new ArrayList<AgentInfo>();
    String curRegion = getConfig().getRegion();

    Set<AgentIdentity> allAttachedAgents = getAgentManager().getAllAttachedAgents();
    Map<String, AgentControllerIdentityImplementation> attachedAgentMap =
        newHashMap(allAttachedAgents);
    for (AgentIdentity agentIdentity : allAttachedAgents) {
      AgentControllerIdentityImplementation existingAgent = convert(agentIdentity);
      attachedAgentMap.put(createAgentKey(existingAgent), existingAgent);
    }

    List<AgentInfo> agentsInDB = getAgentRepository().findAll(startWithRegion(curRegion));
    Map<String, AgentInfo> agentsInDBMap = Maps.newHashMap();
    // step1. check all agents in DB, whether they are attached to controller.
    for (AgentInfo eachAgentInDB : agentsInDB) {
      String keyOfAgentInDB = createAgentKey(eachAgentInDB);
      agentsInDBMap.put(keyOfAgentInDB, eachAgentInDB);
      AgentControllerIdentityImplementation agentIdentity = attachedAgentMap.remove(keyOfAgentInDB);

      if (agentIdentity != null) {
        // if the agent attached to current controller
        if (!hasSamePortAndStatus(eachAgentInDB, agentIdentity)) {
          fillUp(eachAgentInDB, agentIdentity);
          changeAgents.add(eachAgentInDB);
        } else if (!StringUtils.equals(eachAgentInDB.getRegion(), agentIdentity.getRegion())) {
          fillUp(eachAgentInDB, agentIdentity);
          eachAgentInDB.setStatus(WRONG_REGION);
          eachAgentInDB.setApproved(false);
          changeAgents.add(eachAgentInDB);
        }

      } else { // the agent in DB is not attached to current controller

        if (eachAgentInDB.getStatus() != INACTIVE) {
          eachAgentInDB.setStatus(INACTIVE);
          changeAgents.add(eachAgentInDB);
        }
      }
    }

    // step2. check all attached agents, whether they are new, and not saved in DB.
    for (AgentControllerIdentityImplementation agentIdentity : attachedAgentMap.values()) {
      AgentInfo agentInfo =
          getAgentRepository().findByIpAndHostName(agentIdentity.getIp(), agentIdentity.getName());
      if (agentInfo == null) {
        agentInfo = new AgentInfo();
      }
      if (StringUtils.equals(extractRegionFromAgentRegion(agentIdentity.getRegion()), curRegion)) {
        AgentInfo newAgentInfo = fillUp(agentInfo, agentIdentity);
        changeAgents.add(newAgentInfo);
      } else {
        if (agentInfo.getStatus() != WRONG_REGION) {
          AgentInfo newAgentInfo = fillUp(agentInfo, agentIdentity);
          agentInfo.setStatus(WRONG_REGION);
          agentInfo.setApproved(false);
          changeAgents.add(newAgentInfo);
        }
      }
    }

    // step3. update into DB
    getAgentRepository().save(changeAgents);
  }