Search in sources :

Example 16 with SingularityAgent

use of com.hubspot.singularity.SingularityAgent in project Singularity by HubSpot.

the class SingularityAgentAndRackManager method agentLost.

void agentLost(AgentID agentIdObj) {
    final String agentId = agentIdObj.getValue();
    Optional<SingularityAgent> agent = agentManager.getObject(agentId);
    if (agent.isPresent()) {
        MachineState previousState = agent.get().getCurrentState().getState();
        agentManager.changeState(agent.get(), MachineState.DEAD, Optional.empty(), Optional.empty());
        if (configuration.getDisasterDetection().isEnabled()) {
            updateDisasterCounter(previousState);
        }
        checkRackAfterAgentLoss(agent.get());
    } else {
        LOG.warn("Lost a agent {}, but didn't know about it", agentId);
    }
}
Also used : SingularityAgent(com.hubspot.singularity.SingularityAgent) MachineState(com.hubspot.singularity.MachineState)

Example 17 with SingularityAgent

use of com.hubspot.singularity.SingularityAgent in project Singularity by HubSpot.

the class SingularityAgentAndRackManager method loadAgentsAndRacksFromMaster.

public void loadAgentsAndRacksFromMaster(MesosMasterStateObject state, boolean isStartup) {
    Map<String, SingularityAgent> activeAgentsById = agentManager.getObjectsByIdForState(MachineState.ACTIVE);
    Map<String, SingularityRack> activeRacksById = rackManager.getObjectsByIdForState(MachineState.ACTIVE);
    Map<String, SingularityRack> remainingActiveRacks = Maps.newHashMap(activeRacksById);
    int agents = 0;
    int racks = 0;
    for (MesosMasterAgentObject agentJsonObject : state.getAgents()) {
        String agentId = agentJsonObject.getId();
        String rackId = agentAndRackHelper.getRackId(agentJsonObject.getAttributes());
        Map<String, String> textAttributes = agentAndRackHelper.getTextAttributes(agentJsonObject.getAttributes());
        String host = agentAndRackHelper.getMaybeTruncatedHost(agentJsonObject.getHostname());
        if (activeAgentsById.containsKey(agentId)) {
            SingularityAgent agent = activeAgentsById.get(agentId);
            if (agent != null && (!agent.getResources().isPresent() || !agent.getResources().get().equals(agentJsonObject.getResources()))) {
                LOG.trace("Found updated resources ({}) for agent {}", agentJsonObject.getResources(), agent);
                agentManager.saveObject(agent.withResources(agentJsonObject.getResources()));
            }
            activeAgentsById.remove(agentId);
        } else {
            SingularityAgent newAgent = new SingularityAgent(agentId, host, rackId, textAttributes, Optional.of(agentJsonObject.getResources()));
            if (check(newAgent, agentManager) == CheckResult.NEW) {
                agents++;
            }
        }
        if (activeRacksById.containsKey(rackId)) {
            remainingActiveRacks.remove(rackId);
        } else {
            SingularityRack rack = new SingularityRack(rackId);
            if (check(rack, rackManager) == CheckResult.NEW) {
                racks++;
            }
        }
    }
    for (SingularityAgent leftOverAgent : activeAgentsById.values()) {
        agentManager.changeState(leftOverAgent, isStartup ? MachineState.MISSING_ON_STARTUP : MachineState.DEAD, Optional.empty(), Optional.empty());
    }
    for (SingularityRack leftOverRack : remainingActiveRacks.values()) {
        rackManager.changeState(leftOverRack, isStartup ? MachineState.MISSING_ON_STARTUP : MachineState.DEAD, Optional.empty(), Optional.empty());
    }
    LOG.info("Found {} new racks ({} missing) and {} new agents ({} missing)", racks, remainingActiveRacks.size(), agents, activeAgentsById.size());
}
Also used : SingularityRack(com.hubspot.singularity.SingularityRack) SingularityAgent(com.hubspot.singularity.SingularityAgent) MesosMasterAgentObject(com.hubspot.mesos.json.MesosMasterAgentObject)

Example 18 with SingularityAgent

use of com.hubspot.singularity.SingularityAgent in project Singularity by HubSpot.

the class SingularityAgentAndRackManager method checkOffer.

public CheckResult checkOffer(Offer offer) {
    final String agentId = offer.getAgentId().getValue();
    final String rackId = agentAndRackHelper.getRackIdOrDefault(offer);
    final String host = agentAndRackHelper.getMaybeTruncatedHost(offer);
    final Map<String, String> textAttributes = agentAndRackHelper.getTextAttributes(offer);
    final SingularityAgent agent = new SingularityAgent(agentId, host, rackId, textAttributes, Optional.empty());
    CheckResult result = check(agent, agentManager);
    if (result == CheckResult.NEW) {
        if (inactiveAgentManager.isInactive(agent.getHost())) {
            LOG.info("Agent {} on inactive host {} attempted to rejoin. Marking as decommissioned.", agent, host);
            agentManager.changeState(agent, MachineState.STARTING_DECOMMISSION, Optional.of(String.format("Agent %s on inactive host %s attempted to rejoin cluster.", agentId, host)), Optional.empty());
        } else {
            LOG.info("Offer revealed a new agent {}", agent);
        }
    }
    final SingularityRack rack = new SingularityRack(rackId);
    if (check(rack, rackManager) == CheckResult.NEW) {
        LOG.info("Offer revealed a new rack {}", rack);
    }
    return result;
}
Also used : SingularityRack(com.hubspot.singularity.SingularityRack) SingularityAgent(com.hubspot.singularity.SingularityAgent)

Example 19 with SingularityAgent

use of com.hubspot.singularity.SingularityAgent in project Singularity by HubSpot.

the class SingularityMachinesTest method testReconcileSlavesOnStartup.

@Test
public void testReconcileSlavesOnStartup() {
    // Load 3 agents on startup
    MesosMasterStateObject state = getMasterState(3);
    singularityAgentAndRackManager.loadAgentsAndRacksFromMaster(state, true);
    // Load 2 agents on startup
    // 2 agents, third has died
    MesosMasterStateObject newState = getMasterState(2);
    singularityAgentAndRackManager.loadAgentsAndRacksFromMaster(newState, true);
    List<SingularityAgent> agents = agentManager.getObjects();
    Assertions.assertEquals(3, agents.size());
    for (SingularityAgent agent : agents) {
        if (agent.getId().equals("2")) {
            Assertions.assertEquals(MachineState.MISSING_ON_STARTUP, agent.getCurrentState().getState());
        } else {
            Assertions.assertEquals(MachineState.ACTIVE, agent.getCurrentState().getState());
        }
    }
}
Also used : MesosMasterStateObject(com.hubspot.mesos.json.MesosMasterStateObject) SingularityAgent(com.hubspot.singularity.SingularityAgent) Test(org.junit.jupiter.api.Test)

Example 20 with SingularityAgent

use of com.hubspot.singularity.SingularityAgent in project Singularity by HubSpot.

the class SingularityMachinesTest method testBadExpiringStateTransition.

@Test
public void testBadExpiringStateTransition() {
    SingularityAgent agent = getSingleSlave();
    Assertions.assertThrows(WebApplicationException.class, () -> agentResource.decommissionAgent(singularityUser, agent.getId(), new SingularityMachineChangeRequest(Optional.of(1L), Optional.empty(), Optional.empty(), Optional.of(MachineState.FROZEN), Optional.empty())));
}
Also used : SingularityMachineChangeRequest(com.hubspot.singularity.api.SingularityMachineChangeRequest) SingularityAgent(com.hubspot.singularity.SingularityAgent) Test(org.junit.jupiter.api.Test)

Aggregations

SingularityAgent (com.hubspot.singularity.SingularityAgent)26 Test (org.junit.jupiter.api.Test)13 SingularityMachineChangeRequest (com.hubspot.singularity.api.SingularityMachineChangeRequest)6 MesosMasterStateObject (com.hubspot.mesos.json.MesosMasterStateObject)4 MachineState (com.hubspot.singularity.MachineState)4 SingularityRack (com.hubspot.singularity.SingularityRack)4 ArrayList (java.util.ArrayList)4 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)3 MesosMasterAgentObject (com.hubspot.mesos.json.MesosMasterAgentObject)2 SingularityMachineStateHistoryUpdate (com.hubspot.singularity.SingularityMachineStateHistoryUpdate)2 SingularityPendingTaskId (com.hubspot.singularity.SingularityPendingTaskId)2 SingularityTask (com.hubspot.singularity.SingularityTask)2 HashSet (java.util.HashSet)2 Optional (java.util.Optional)2 Timed (com.codahale.metrics.annotation.Timed)1 HashMultiset (com.google.common.collect.HashMultiset)1 Multiset (com.google.common.collect.Multiset)1 Inject (com.google.inject.Inject)1 Singleton (com.google.inject.Singleton)1 AgentPlacement (com.hubspot.singularity.AgentPlacement)1