Search in sources :

Example 1 with ContainerStatsImpl

use of com.yahoo.vespa.hosted.dockerapi.ContainerStatsImpl in project vespa by vespa-engine.

the class NodeAgentImplTest method testGetRelevantMetrics.

@Test
@SuppressWarnings("unchecked")
public void testGetRelevantMetrics() throws Exception {
    final ObjectMapper objectMapper = new ObjectMapper();
    ClassLoader classLoader = getClass().getClassLoader();
    File statsFile = new File(classLoader.getResource("docker.stats.json").getFile());
    Map<String, Map<String, Object>> dockerStats = objectMapper.readValue(statsFile, Map.class);
    Map<String, Object> networks = dockerStats.get("networks");
    Map<String, Object> precpu_stats = dockerStats.get("precpu_stats");
    Map<String, Object> cpu_stats = dockerStats.get("cpu_stats");
    Map<String, Object> memory_stats = dockerStats.get("memory_stats");
    Map<String, Object> blkio_stats = dockerStats.get("blkio_stats");
    Docker.ContainerStats stats1 = new ContainerStatsImpl(networks, precpu_stats, memory_stats, blkio_stats);
    Docker.ContainerStats stats2 = new ContainerStatsImpl(networks, cpu_stats, memory_stats, blkio_stats);
    ContainerNodeSpec.Owner owner = new ContainerNodeSpec.Owner("tester", "testapp", "testinstance");
    ContainerNodeSpec.Membership membership = new ContainerNodeSpec.Membership("clustType", "clustId", "grp", 3, false);
    final ContainerNodeSpec nodeSpec = nodeSpecBuilder.wantedDockerImage(dockerImage).currentDockerImage(dockerImage).nodeState(Node.State.active).vespaVersion(vespaVersion).owner(owner).membership(membership).minMainMemoryAvailableGb(2).allowedToBeDown(true).build();
    NodeAgentImpl nodeAgent = makeNodeAgent(dockerImage, true);
    when(nodeRepository.getContainerNodeSpec(eq(hostName))).thenReturn(Optional.of(nodeSpec));
    when(storageMaintainer.getDiskUsageFor(eq(containerName))).thenReturn(Optional.of(39625000000L));
    when(dockerOperations.getContainerStats(eq(containerName))).thenReturn(Optional.of(stats1)).thenReturn(Optional.of(stats2));
    when(pathResolver.getApplicationStoragePathForHost()).thenReturn(Files.createTempDirectory("bar"));
    // Run the converge loop once to initialize lastNodeSpec
    nodeAgent.converge();
    // Update metrics once to init and lastCpuMetric
    nodeAgent.updateContainerNodeMetrics();
    clock.advance(Duration.ofSeconds(1234));
    Path pathToExpectedMetrics = Paths.get(classLoader.getResource("expected.container.system.metrics.txt").getPath());
    String expectedMetrics = new String(Files.readAllBytes(pathToExpectedMetrics)).replaceAll("\\s", "").replaceAll("\\n", "");
    String[] expectedCommand = { "vespa-rpc-invoke", "-t", "2", "tcp/localhost:19091", "setExtraMetrics", expectedMetrics };
    doAnswer(invocation -> {
        ContainerName calledContainerName = (ContainerName) invocation.getArguments()[0];
        long calledTimeout = (long) invocation.getArguments()[1];
        String[] calledCommand = new String[invocation.getArguments().length - 2];
        System.arraycopy(invocation.getArguments(), 2, calledCommand, 0, calledCommand.length);
        calledCommand[calledCommand.length - 1] = calledCommand[calledCommand.length - 1].replaceAll("\"timestamp\":\\d+", "\"timestamp\":0");
        assertEquals(containerName, calledContainerName);
        assertEquals(5L, calledTimeout);
        assertArrayEquals(expectedCommand, calledCommand);
        return null;
    }).when(dockerOperations).executeCommandInContainerAsRoot(any(), any(), anyVararg());
    nodeAgent.updateContainerNodeMetrics();
}
Also used : Path(java.nio.file.Path) ContainerStatsImpl(com.yahoo.vespa.hosted.dockerapi.ContainerStatsImpl) Docker(com.yahoo.vespa.hosted.dockerapi.Docker) ContainerName(com.yahoo.vespa.hosted.dockerapi.ContainerName) File(java.io.File) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ContainerNodeSpec(com.yahoo.vespa.hosted.node.admin.ContainerNodeSpec) Test(org.junit.Test)

Aggregations

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ContainerName (com.yahoo.vespa.hosted.dockerapi.ContainerName)1 ContainerStatsImpl (com.yahoo.vespa.hosted.dockerapi.ContainerStatsImpl)1 Docker (com.yahoo.vespa.hosted.dockerapi.Docker)1 ContainerNodeSpec (com.yahoo.vespa.hosted.node.admin.ContainerNodeSpec)1 File (java.io.File)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 Test (org.junit.Test)1