@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()); } }
public AgentStatusRepresentationFactory(List<AgentStatus> agentStatuses, Repository repository) { this.shortIdPrefixSize = shortestUniquePrefix(transform(agentStatuses, idGetter()), MIN_PREFIX_SIZE); this.commonLocationParts = commonPrefixSegments('/', transform(agentStatuses, locationGetter()), MIN_LOCATION_SEGMENTS); this.repository = repository; }
public static String createAgentsVersion(Iterable<AgentStatus> agents) { Preconditions.checkNotNull(agents, "agents is null"); // canonicalize agent order Map<String, String> agentVersions = new TreeMap<String, String>(); for (AgentStatus agent : agents) { agentVersions.put(agent.getAgentId() + agent.getInstanceId(), agent.getVersion()); } return DigestUtils.md5Hex(agentVersions.values().toString()); }
@Override public void updateStatus() { URI internalUri = agentStatus.getInternalUri(); if (internalUri != null) { try { Request request = RequestBuilder.prepareGet() .setUri(uriBuilderFrom(internalUri).replacePath("/v1/agent/").build()) .build(); AgentStatusRepresentation agentStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(agentStatusCodec)); agentStatus = agentStatusRepresentation.toAgentStatus(agentStatus.getInstanceId(), agentStatus.getInstanceType()); return; } catch (Exception ignored) { } } // error talking to agent -- mark agent offline if (agentStatus.getState() != PROVISIONING) { agentStatus = agentStatus.changeState(OFFLINE); agentStatus = agentStatus.changeAllSlotsState(SlotLifecycleState.UNKNOWN); } }
public static void checkAgentVersion(AgentStatus agentStatus, String expectedAgentStatus) { Preconditions.checkNotNull(agentStatus, "agentStatus is null"); if (expectedAgentStatus == null) { return; } if (!expectedAgentStatus.equals(agentStatus.getVersion())) { throw new VersionConflictException(GALAXY_AGENT_VERSION_HEADER, agentStatus.getVersion()); } }
public AgentStatus changeState(AgentLifecycleState state) { return new AgentStatus(agentId, state, instanceId, internalUri, externalUri, location, instanceType, slots.values(), resources); }
@Override public SlotStatus install(Installation installation) { Preconditions.checkNotNull(installation, "installation is null"); URI internalUri = agentStatus.getInternalUri(); Preconditions.checkState(internalUri != null, "agent is down"); try { Request request = RequestBuilder.preparePost() .setUri(uriBuilderFrom(internalUri).replacePath("/v1/agent/slot/").build()) .setHeader(CONTENT_TYPE, APPLICATION_JSON) .setHeader(GALAXY_AGENT_VERSION_HEADER, status().getVersion()) .setBodyGenerator(jsonBodyGenerator(installationCodec, InstallationRepresentation.from(installation))) .build(); SlotStatusRepresentation slotStatusRepresentation = httpClient.execute(request, createJsonResponseHandler(slotStatusCodec, Status.CREATED.getStatusCode())); SlotStatus slotStatus = slotStatusRepresentation.toSlotStatus(agentStatus.getInstanceId()); agentStatus = agentStatus.changeSlotStatus(slotStatus); return slotStatus; } catch (Exception e) { throw Throwables.propagate(e); } } }
@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; } }
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; }
@Override public boolean apply(@Nullable AgentStatus agentStatus) { return agentStatus != null && (predicate.apply(agentStatus.getExternalUri()) || predicate.apply(agentStatus.getInternalUri())); } }
public AgentStatus terminateAgent(String agentId) { RemoteAgent agent = null; for (Iterator<Entry<String, RemoteAgent>> iterator = agents.entrySet().iterator(); iterator.hasNext(); ) { Entry<String, RemoteAgent> entry = iterator.next(); if (entry.getValue().status().getAgentId().equals(agentId)) { iterator.remove(); agent = entry.getValue(); break; } } if (agent == null) { return null; } if (!agent.getSlots().isEmpty()) { agents.putIfAbsent(agent.status().getInstanceId(), agent); throw new IllegalStateException("Cannot terminate agent that has slots: " + agentId); } provisioner.terminateAgents(ImmutableList.of(agentId)); return agent.status().changeState(AgentLifecycleState.TERMINATED); }
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; }
@VisibleForTesting public void updateAllAgents() { Set<String> instanceIds = newHashSet(); for (Instance instance : this.provisioner.listAgents()) { instanceIds.add(instance.getInstanceId()); RemoteAgent remoteAgent = remoteAgentFactory.createRemoteAgent(instance, instance.getInternalUri() != null ? AgentLifecycleState.ONLINE : AgentLifecycleState.OFFLINE); RemoteAgent existing = agents.putIfAbsent(instance.getInstanceId(), remoteAgent); if (existing != null) { existing.setInternalUri(instance.getInternalUri()); } } // add provisioning agents to provisioner list for (RemoteAgent remoteAgent : agents.values()) { if (remoteAgent.status().getState() == AgentLifecycleState.PROVISIONING) { instanceIds.add(remoteAgent.status().getAgentId()); } } // remove any agents not in the provisioner list agents.keySet().retainAll(instanceIds); List<ServiceDescriptor> serviceDescriptors = serviceInventory.getServiceInventory(transform(getAllSlots(), getSlotStatus())); for (RemoteAgent remoteAgent : agents.values()) { remoteAgent.updateStatus(); remoteAgent.setServiceInventory(serviceDescriptors); } }
@Override public void setServiceInventory(List<ServiceDescriptor> serviceInventory) { if (agentStatus.getState() == ONLINE) { Preconditions.checkNotNull(serviceInventory, "serviceInventory is null"); URI internalUri = agentStatus.getInternalUri(); try { Request request = RequestBuilder.preparePut() .setUri(uriBuilderFrom(internalUri).appendPath("/v1/serviceInventory").build()) .setHeader(CONTENT_TYPE, APPLICATION_JSON) .setBodyGenerator(jsonBodyGenerator(serviceDescriptorsCodec, new ServiceDescriptorsRepresentation(environment, serviceInventory))) .build(); httpClient.execute(request, createStatusResponseHandler()); if (serviceInventoryUp.compareAndSet(false, true)) { log.info("Service inventory put succeeded for agent at %s", internalUri); } } catch (Exception e) { if (serviceInventoryUp.compareAndSet(true, false) && !log.isDebugEnabled()) { log.error("Unable to post service inventory to agent at %s: %s", internalUri, e.getMessage()); } log.debug(e, "Unable to post service inventory to agent at %s: %s", internalUri, e.getMessage()); } } }
@GET @Path("/agent") @Produces(MediaType.APPLICATION_JSON) public Response getAllAgents(@Context UriInfo uriInfo) { List<SlotStatus> allSlotStatus = coordinator.getAllSlotStatus(); Predicate<AgentStatus> agentPredicate = AgentFilterBuilder.build(uriInfo, transform(coordinator.getAgents(), idGetter()), transform(allSlotStatus, SlotStatus.uuidGetter()), false, repository); List<AgentStatus> agents = coordinator.getAgents(agentPredicate); return Response.ok(transform(agents, fromAgentStatus(coordinator.getAgents(), repository))) .header(GALAXY_AGENTS_VERSION_HEADER, createAgentsVersion(agents)) .build(); }
public String apply(AgentStatus input) { return input.getAgentId(); } };
@Override public List<? extends RemoteSlot> getSlots() { return ImmutableList.copyOf(Iterables.transform(agentStatus.getSlotStatuses(), new Function<SlotStatus, HttpRemoteSlot>() { @Override public HttpRemoteSlot apply(SlotStatus slotStatus) { return new HttpRemoteSlot(slotStatus, httpClient, HttpRemoteAgent.this); } })); }
@Override public boolean apply(@Nullable AgentStatus agentStatus) { return agentStatus != null && agentStatus.getState() == state; } }
@Override public boolean apply(@Nullable AgentStatus agentStatus) { return agentStatus != null && predicate.apply(agentStatus.getInstanceId()); } }