Search in sources :

Example 1 with SingularityTaskCurrentUsageWithId

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

the class SingularityUsageTest method testUsagePollerComplex.

@Test
public void testUsagePollerComplex() throws InterruptedException {
    initRequest();
    initFirstDeploy();
    saveAndSchedule(request.toBuilder().setInstances(Optional.of(2)));
    resourceOffers(1);
    configuration.setNumUsageToKeep(2);
    configuration.setUsageIntervalSeconds(1);
    configuration.setCheckUsageEveryMillis(1);
    List<SingularityTaskId> taskIds = taskManager.getActiveTaskIds();
    String t1 = taskIds.get(0).getId();
    String t2 = taskIds.get(1).getId();
    String slaveId = slaveManager.getObjectIds().get(0);
    String host = slaveManager.getObjects().get(0).getHost();
    MesosTaskMonitorObject t1u1 = getTaskMonitor(t1, 2, 5, 100);
    MesosTaskMonitorObject t2u1 = getTaskMonitor(t2, 10, 5, 1024);
    mesosClient.setSlaveResourceUsage(host, Arrays.asList(t1u1, t2u1));
    usagePoller.runActionOnPoll();
    cleaner.runActionOnPoll();
    Thread.sleep(2);
    // 5 seconds have elapsed, t1 has used 1 CPU the whole time = 5 + 2
    // t2 has used 2.5 CPUs the whole time =
    MesosTaskMonitorObject t1u2 = getTaskMonitor(t1, 7, 10, 125);
    MesosTaskMonitorObject t2u2 = getTaskMonitor(t2, 22.5, 10, 750);
    mesosClient.setSlaveResourceUsage(host, Arrays.asList(t1u2, t2u2));
    usagePoller.runActionOnPoll();
    cleaner.runActionOnPoll();
    // check usage now
    Assert.assertEquals(3.5, usageManager.getSlaveUsage(slaveId).get(1).getCpusUsed(), 0);
    Assert.assertEquals(875, usageManager.getSlaveUsage(slaveId).get(1).getMemoryBytesUsed(), 0);
    Assert.assertEquals(2, usageManager.getSlaveUsage(slaveId).get(1).getNumTasks(), 0);
    // check task usage
    Assert.assertEquals(22.5, usageManager.getTaskUsage(t2).get(1).getCpuSeconds(), 0);
    Assert.assertEquals(10, usageManager.getTaskUsage(t2).get(0).getCpuSeconds(), 0);
    Thread.sleep(2);
    MesosTaskMonitorObject t1u3 = getTaskMonitor(t1, 8, 11, 125);
    MesosTaskMonitorObject t2u3 = getTaskMonitor(t2, 23.5, 11, 1024);
    mesosClient.setSlaveResourceUsage(host, Arrays.asList(t1u3, t2u3));
    usagePoller.runActionOnPoll();
    cleaner.runActionOnPoll();
    // check that there is only 2 usages
    Assert.assertEquals(2, usageManager.getSlaveUsage(slaveId).size());
    Assert.assertEquals(2, usageManager.getTaskUsage(t1).size());
    Assert.assertEquals(2, usageManager.getTaskUsage(t2).size());
    Assert.assertEquals(22.5, usageManager.getTaskUsage(t2).get(0).getCpuSeconds(), 0);
    Assert.assertEquals(23.5, usageManager.getTaskUsage(t2).get(1).getCpuSeconds(), 0);
    Assert.assertEquals(875, usageManager.getSlaveUsage(slaveId).get(0).getMemoryBytesUsed(), 0);
    Assert.assertEquals(1149, usageManager.getSlaveUsage(slaveId).get(1).getMemoryBytesUsed(), 0);
    Assert.assertEquals(slaveId, usageManager.getAllCurrentSlaveUsage().get(0).getSlaveId());
    Assert.assertEquals(1149, usageManager.getAllCurrentSlaveUsage().get(0).getMemoryBytesUsed());
    List<SingularityTaskCurrentUsageWithId> taskCurrentUsages = usageManager.getTaskCurrentUsages(taskManager.getActiveTaskIds());
    Assert.assertEquals(2, taskCurrentUsages.size());
    List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIds();
    for (SingularityTaskCurrentUsageWithId taskCurrentUsage : taskCurrentUsages) {
        activeTaskIds.remove(taskCurrentUsage.getTaskId());
    }
    Assert.assertTrue(activeTaskIds.isEmpty());
}
Also used : SingularityTaskCurrentUsageWithId(com.hubspot.singularity.SingularityTaskCurrentUsageWithId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) MesosTaskMonitorObject(com.hubspot.mesos.json.MesosTaskMonitorObject) Test(org.junit.Test)

Example 2 with SingularityTaskCurrentUsageWithId

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

the class UsageManager method getTaskCurrentUsages.

public List<SingularityTaskCurrentUsageWithId> getTaskCurrentUsages(List<SingularityTaskId> taskIds) {
    List<String> paths = new ArrayList<>(taskIds.size());
    for (SingularityTaskId taskId : taskIds) {
        paths.add(getCurrentTaskUsagePath(taskId.getId()));
    }
    Map<String, SingularityTaskCurrentUsage> currentTaskUsages = getAsyncWithPath("getTaskCurrentUsages", paths, taskCurrentUsageTranscoder);
    List<SingularityTaskCurrentUsageWithId> currentTaskUsagesWithIds = new ArrayList<>(paths.size());
    for (Entry<String, SingularityTaskCurrentUsage> entry : currentTaskUsages.entrySet()) {
        currentTaskUsagesWithIds.add(new SingularityTaskCurrentUsageWithId(SingularityTaskId.valueOf(getTaskIdFromCurrentUsagePath(entry.getKey())), entry.getValue()));
    }
    return currentTaskUsagesWithIds;
}
Also used : SingularityTaskCurrentUsageWithId(com.hubspot.singularity.SingularityTaskCurrentUsageWithId) SingularityTaskCurrentUsage(com.hubspot.singularity.SingularityTaskCurrentUsage) ArrayList(java.util.ArrayList) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Aggregations

SingularityTaskCurrentUsageWithId (com.hubspot.singularity.SingularityTaskCurrentUsageWithId)2 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)2 MesosTaskMonitorObject (com.hubspot.mesos.json.MesosTaskMonitorObject)1 SingularityTaskCurrentUsage (com.hubspot.singularity.SingularityTaskCurrentUsage)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1