Search in sources :

Example 1 with MasterModel

use of com.spotify.helios.master.MasterModel in project helios by spotify.

the class DeadAgentReaperTest method testDeadAgentReaper.

@Test
public void testDeadAgentReaper() throws Exception {
    final MasterModel masterModel = mock(MasterModel.class);
    final Clock clock = mock(Clock.class);
    when(clock.now()).thenReturn(new Instant(HOURS.toMillis(2000)));
    final List<Datapoint> datapoints = Lists.newArrayList(new Datapoint("host1", 0, TIMEOUT_HOURS - 1, HostStatus.Status.DOWN, true), new Datapoint("host2", 0, TIMEOUT_HOURS + 1, HostStatus.Status.DOWN, false), new Datapoint("host3", 1000, 1000, HostStatus.Status.UP, false), new Datapoint("host4", 500, 300, HostStatus.Status.DOWN, true), // Agents started in the future should not be reaped, even if they are reported as down
    new Datapoint("host5", 5000, 0, HostStatus.Status.DOWN, false), // they should
    new Datapoint("host6", 0, 0, HostStatus.Status.UP, false));
    when(masterModel.listHosts()).thenReturn(Lists.newArrayList(datapoints.stream().map(input -> input.host).collect(Collectors.toList())));
    for (final Datapoint datapoint : datapoints) {
        when(masterModel.isHostUp(datapoint.host)).thenReturn(HostStatus.Status.UP == datapoint.status);
        when(masterModel.getAgentInfo(datapoint.host)).thenReturn(AgentInfo.newBuilder().setStartTime(datapoint.startTime).setUptime(datapoint.uptime).build());
    }
    final DeadAgentReaper reaper = new DeadAgentReaper(masterModel, TIMEOUT_HOURS, clock, 100, 0);
    reaper.startAsync().awaitRunning();
    for (final Datapoint datapoint : datapoints) {
        if (datapoint.expectReap) {
            verify(masterModel, timeout(500)).deregisterHost(datapoint.host);
        } else {
            verify(masterModel, never()).deregisterHost(datapoint.host);
        }
    }
}
Also used : Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Mockito.timeout(org.mockito.Mockito.timeout) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Lists(com.google.common.collect.Lists) HostStatus(com.spotify.helios.common.descriptors.HostStatus) Instant(org.joda.time.Instant) HOURS(java.util.concurrent.TimeUnit.HOURS) AgentInfo(com.spotify.helios.common.descriptors.AgentInfo) Clock(com.spotify.helios.common.Clock) MasterModel(com.spotify.helios.master.MasterModel) Mockito.mock(org.mockito.Mockito.mock) MasterModel(com.spotify.helios.master.MasterModel) Instant(org.joda.time.Instant) Clock(com.spotify.helios.common.Clock) Test(org.junit.Test)

Example 2 with MasterModel

use of com.spotify.helios.master.MasterModel in project helios by spotify.

the class JobHistoryReaperTest method testJobHistoryReaper.

@Test
public void testJobHistoryReaper() throws Exception {
    final MasterModel masterModel = mock(MasterModel.class);
    final List<Datapoint> datapoints = Lists.newArrayList(// A job history with a corresponding job should NOT BE reaped.
    new Datapoint("job1", Job.newBuilder().setName("job1").build(), false), // A job history without a corresponding job should BE reaped.
    new Datapoint("job2", null, true));
    for (final Datapoint dp : datapoints) {
        when(masterModel.getJob(argThat(matchesName(dp.getJobName())))).thenReturn(dp.getJob());
    }
    final ZooKeeperClient client = mock(ZooKeeperClient.class);
    final List<String> jobHistories = ImmutableList.of("job1", "job2");
    when(client.getChildren(Paths.historyJobs())).thenReturn(jobHistories);
    final JobHistoryReaper reaper = new JobHistoryReaper(masterModel, client, 100, 0);
    reaper.startAsync().awaitRunning();
    for (final Datapoint datapoint : datapoints) {
        if (datapoint.expectReap) {
            verify(client, timeout(500)).deleteRecursive(Paths.historyJob(datapoint.getJobId()));
        } else {
            verify(client, never()).deleteRecursive(Paths.historyJob(datapoint.getJobId()));
        }
    }
}
Also used : MasterModel(com.spotify.helios.master.MasterModel) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) Test(org.junit.Test)

Example 3 with MasterModel

use of com.spotify.helios.master.MasterModel in project helios by spotify.

the class OldJobReaperTest method testOldJobReaper.

@Test
public void testOldJobReaper() throws Exception {
    final MasterModel masterModel = mock(MasterModel.class);
    final Clock clock = mock(Clock.class);
    when(clock.now()).thenReturn(new Instant(HOURS.toMillis(48)));
    final List<Datapoint> datapoints = Lists.newArrayList(// A job not deployed, with history, and last used too long ago should BE reaped
    new Datapoint("job1", emptyMap(), events(ImmutableList.of(HOURS.toMillis(20), HOURS.toMillis(22))), true), // A job not deployed, with history, and last used recently should NOT BE reaped
    new Datapoint("job2", emptyMap(), events(ImmutableList.of(HOURS.toMillis(20), HOURS.toMillis(40))), false), // A job not deployed, without history, and without a creation date should BE reaped
    new Datapoint("job3", emptyMap(), emptyList(), true), // A job not deployed, without history, and created before retention time should BE reaped
    new Datapoint("job4", emptyMap(), emptyList(), HOURS.toMillis(23), true), // A job not deployed, without history, created after retention time should NOT BE reaped
    new Datapoint("job5", emptyMap(), emptyList(), HOURS.toMillis(25), false), // A job deployed and without history should NOT BE reaped
    new Datapoint("job6", deployments(JobId.fromString("job6"), 2), emptyList(), false), // A job deployed, with history, and last used too long ago should NOT BE reaped
    new Datapoint("job7", deployments(JobId.fromString("job7"), 3), events(ImmutableList.of(HOURS.toMillis(20), HOURS.toMillis(22))), false), // A job deployed, with history, and last used recently should NOT BE reaped
    new Datapoint("job8", deployments(JobId.fromString("job8"), 3), events(ImmutableList.of(HOURS.toMillis(20), HOURS.toMillis(40))), false));
    when(masterModel.getJobs()).thenReturn(datapoints.stream().collect(Collectors.toMap(Datapoint::getJobId, Datapoint::getJob)));
    for (final Datapoint datapoint : datapoints) {
        when(masterModel.getJobHistory(datapoint.getJobId())).thenReturn(datapoint.getHistory());
        when(masterModel.getJobStatus(datapoint.getJobId())).thenReturn(datapoint.getJobStatus());
    }
    final OldJobReaper reaper = new OldJobReaper(masterModel, RETENTION_DAYS, clock, 100, 0);
    reaper.startAsync().awaitRunning();
    // Wait one second to give the reaper enough time to process all the jobs before verifying :(
    Thread.sleep(1000);
    for (final Datapoint datapoint : datapoints) {
        if (datapoint.expectReap) {
            verify(masterModel, timeout(500)).removeJob(datapoint.getJobId(), Job.EMPTY_TOKEN);
        } else {
            verify(masterModel, never()).removeJob(datapoint.getJobId(), Job.EMPTY_TOKEN);
        }
    }
}
Also used : MasterModel(com.spotify.helios.master.MasterModel) Instant(org.joda.time.Instant) Clock(com.spotify.helios.common.Clock) Test(org.junit.Test)

Aggregations

MasterModel (com.spotify.helios.master.MasterModel)3 Test (org.junit.Test)3 Clock (com.spotify.helios.common.Clock)2 Instant (org.joda.time.Instant)2 Lists (com.google.common.collect.Lists)1 AgentInfo (com.spotify.helios.common.descriptors.AgentInfo)1 HostStatus (com.spotify.helios.common.descriptors.HostStatus)1 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)1 List (java.util.List)1 HOURS (java.util.concurrent.TimeUnit.HOURS)1 Collectors (java.util.stream.Collectors)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.never (org.mockito.Mockito.never)1 Mockito.timeout (org.mockito.Mockito.timeout)1 Mockito.verify (org.mockito.Mockito.verify)1 Mockito.when (org.mockito.Mockito.when)1