public static Map<String, Integer> getAvailableResources(AgentStatus agentStatus) { Map<String, Integer> availableResources = new TreeMap<String, Integer>(agentStatus.getResources()); for (SlotStatus slotStatus : agentStatus.getSlotStatuses()) { for (Entry<String, Integer> entry : slotStatus.getResources().entrySet()) { int value = Objects.firstNonNull(availableResources.get(entry.getKey()), 0); availableResources.put(entry.getKey(), value - entry.getValue()); } } return availableResources; }
private List<RemoteAgent> selectAgents(Predicate<AgentStatus> filter, Installation installation) { // randomize agents so all processes don't end up on the same node // todo sort agents by number of process already installed on them List<RemoteAgent> targetAgents = newArrayList(); List<RemoteAgent> allAgents = newArrayList(filter(this.agents.values(), filterAgentsBy(filter))); if (!allowDuplicateInstallationsOnAnAgent) { allAgents = newArrayList(filter(this.agents.values(), filterAgentsWithAssignment(installation))); } Collections.shuffle(allAgents); for (RemoteAgent agent : allAgents) { // verify agent state AgentStatus status = agent.status(); if (status.getState() != AgentLifecycleState.ONLINE) { continue; } // agents without declared resources are considered to have unlimited resources if (!status.getResources().isEmpty()) { // verify that required resources are available Map<String, Integer> availableResources = InstallationUtils.getAvailableResources(status); if (!InstallationUtils.resourcesAreAvailable(availableResources, installation.getResources())) { continue; } } targetAgents.add(agent); } return targetAgents; }
@Override public AgentStatus status() { AgentStatus agentStatus = agent.getAgentStatus(); return new AgentStatus(agentStatus.getAgentId(), agentStatus.getState(), instanceId, agentStatus.getInternalUri(), agentStatus.getExternalUri(), agentStatus.getLocation(), instanceType, agentStatus.getSlotStatuses(), agentStatus.getResources()); }
public AgentStatusRepresentation create(AgentStatus status) { Builder<SlotStatusRepresentation> builder = ImmutableList.builder(); for (SlotStatus slot : status.getSlotStatuses()) { builder.add(SlotStatusRepresentation.from(slot, Integer.MAX_VALUE, repository)); } return new AgentStatusRepresentation( status.getAgentId(), safeTruncate(status.getAgentId(), shortIdPrefixSize), status.getInstanceId(), status.getState(), status.getInternalUri(), status.getExternalUri(), status.getLocation(), trimLeadingSegments(status.getLocation(), '/', commonLocationParts), status.getInstanceType(), builder.build(), status.getResources(), status.getVersion()); } }
@Override public boolean apply(@Nullable AgentStatus status) { // We can only install on online agents if (status.getState() != ONLINE) { return false; } // Constraints: normally we only allow only instance of a binary+config on each agent if (!allowDuplicateInstallationsOnAnAgent) { for (SlotStatus slot : status.getSlotStatuses()) { if (repository.binaryEqualsIgnoreVersion(assignment.getBinary(), slot.getAssignment().getBinary()) && repository.configEqualsIgnoreVersion(assignment.getConfig(), slot.getAssignment().getConfig())) { return false; } } } // agents without declared resources are considered to have unlimited resources if (!status.getResources().isEmpty()) { // verify that required resources are available Installation installation = toInstallation(repository, assignment); Map<String, Integer> availableResources = getAvailableResources(status); if (!resourcesAreAvailable(availableResources, installation.getResources())) { return false; } } return true; } }