diff options
author | Valerij Fredriksen <valerijf@verizonmedia.com> | 2019-04-17 09:01:00 +0200 |
---|---|---|
committer | Valerij Fredriksen <valerijf@verizonmedia.com> | 2019-04-17 09:01:00 +0200 |
commit | a203b1b780f8fc7cd6794c16a81c0ddae8f2041e (patch) | |
tree | 534970478ffe9b23d2a1d434395bdd48fe2be007 /docker-api/src | |
parent | 5ab193575090efa10cc5aa07744ad457e1af973f (diff) |
Upgrade docker-java to 3.1.2
Diffstat (limited to 'docker-api/src')
3 files changed, 109 insertions, 31 deletions
diff --git a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/ContainerStats.java b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/ContainerStats.java index 738a65bc08b..d33ddadb52c 100644 --- a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/ContainerStats.java +++ b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/ContainerStats.java @@ -1,9 +1,18 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.dockerapi; -import java.util.Collections; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.dockerjava.api.model.CpuStatsConfig; +import com.github.dockerjava.api.model.MemoryStatsConfig; +import com.github.dockerjava.api.model.StatisticNetworksConfig; +import com.github.dockerjava.api.model.Statistics; + +import java.io.IOException; +import java.io.UncheckedIOException; import java.util.Map; import java.util.Optional; +import java.util.TreeMap; +import java.util.stream.Collectors; /** * Wrapper class for {@link com.github.dockerjava.api.model.Statistics} to prevent leaking from docker-java library. @@ -11,33 +20,103 @@ import java.util.Optional; * @author freva */ public class ContainerStats { - private final Map<String, Object> networks; - private final Map<String, Object> cpuStats; - private final Map<String, Object> memoryStats; - private final Map<String, Object> blkioStats; + private final Map<String, NetworkStats> networkStatsByInterface; + private final MemoryStats memoryStats; + private final CpuStats cpuStats; - public ContainerStats(Map<String, Object> networks, Map<String, Object> cpuStats, - Map<String, Object> memoryStats, Map<String, Object> blkioStats) { + ContainerStats(Statistics statistics) { // Network stats are null when container uses host network - this.networks = Optional.ofNullable(networks).orElse(Collections.emptyMap()); - this.cpuStats = cpuStats; - this.memoryStats = memoryStats; - this.blkioStats = blkioStats; + this.networkStatsByInterface = Optional.ofNullable(statistics.getNetworks()).orElseGet(Map::of) + .entrySet().stream() + .collect(Collectors.toMap( + Map.Entry::getKey, + e -> new NetworkStats(e.getValue()), + (u, v) -> { throw new IllegalStateException(); }, + TreeMap::new)); + this.memoryStats = new MemoryStats(statistics.getMemoryStats()); + this.cpuStats = new CpuStats(statistics.getCpuStats()); + } + + public Map<String, NetworkStats> getNetworks() { + return networkStatsByInterface; } - public Map<String, Object> getNetworks() { - return networks; + public MemoryStats getMemoryStats() { + return memoryStats; } - public Map<String, Object> getCpuStats() { + public CpuStats getCpuStats() { return cpuStats; } - public Map<String, Object> getMemoryStats() { - return memoryStats; + public static class NetworkStats { + private final long rxBytes; + private final long rxDropped; + private final long rxErrors; + private final long txBytes; + private final long txDropped; + private final long txErrors; + + private NetworkStats(StatisticNetworksConfig statisticNetworksConfig) { + this.rxBytes = statisticNetworksConfig.getRxBytes(); + this.rxDropped = statisticNetworksConfig.getRxDropped(); + this.rxErrors = statisticNetworksConfig.getRxErrors(); + this.txBytes = statisticNetworksConfig.getTxBytes(); + this.txDropped = statisticNetworksConfig.getTxDropped(); + this.txErrors = statisticNetworksConfig.getTxErrors(); + } + + public long getRxBytes() { return this.rxBytes; } + public long getRxDropped() { return this.rxDropped; } + public long getRxErrors() { return this.rxErrors; } + public long getTxBytes() { return this.txBytes; } + public long getTxDropped() { return this.txDropped; } + public long getTxErrors() { return this.txErrors; } + } + + public class MemoryStats { + private final long cache; + private final long usage; + private final long limit; + + private MemoryStats(MemoryStatsConfig memoryStats) { + this.cache = memoryStats.getStats().getCache(); + this.usage = memoryStats.getUsage(); + this.limit = memoryStats.getLimit(); + } + + public long getCache() { return this.cache; } + public long getUsage() { return this.usage; } + public long getLimit() { return this.limit; } + } + + public class CpuStats { + private final int onlineCpus; + private final long systemCpuUsage; + private final long totalUsage; + private final long usageInKernelMode; + + public CpuStats(CpuStatsConfig cpuStats) { + // Added in 1.27 + this.onlineCpus = cpuStats.getCpuUsage().getPercpuUsage().size(); + this.systemCpuUsage = cpuStats.getSystemCpuUsage(); + this.totalUsage = cpuStats.getCpuUsage().getTotalUsage(); + this.usageInKernelMode = cpuStats.getCpuUsage().getUsageInKernelmode(); + } + + public int getOnlineCpus() { return this.onlineCpus; } + public long getSystemCpuUsage() { return this.systemCpuUsage; } + public long getTotalUsage() { return totalUsage; } + public long getUsageInKernelMode() { return usageInKernelMode; } } - public Map<String, Object> getBlkioStats() { - return blkioStats; + // For testing only, create ContainerStats from JSON returned by docker daemon stats API + public static ContainerStats fromJson(String json) { + try { + Statistics statistics = new ObjectMapper().readValue(json, Statistics.class); + return new ContainerStats(statistics); + } catch (IOException e) { + throw new UncheckedIOException(e); + } } } diff --git a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/CreateContainerCommandImpl.java b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/CreateContainerCommandImpl.java index db4dc303ab9..2e5cfab36cc 100644 --- a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/CreateContainerCommandImpl.java +++ b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/CreateContainerCommandImpl.java @@ -167,34 +167,34 @@ class CreateContainerCommandImpl implements Docker.CreateContainerCommand { List<Bind> volumeBinds = volumeBindSpecs.stream().map(Bind::parse).collect(Collectors.toList()); final HostConfig hostConfig = new HostConfig() - .withSecurityOpts(new ArrayList<>(securityOpts)); + .withSecurityOpts(new ArrayList<>(securityOpts)) + .withBinds(volumeBinds) + .withUlimits(ulimits) + .withCapAdd(addCapabilities.toArray(new Capability[0])) + .withCapDrop(dropCapabilities.toArray(new Capability[0])) + .withPrivileged(privileged); containerResources.ifPresent(cr -> hostConfig .withCpuShares(cr.cpuShares()) .withMemory(cr.memoryBytes()) // MemorySwap is the total amount of memory and swap, if MemorySwap == Memory, then container has no access swap .withMemorySwap(cr.memoryBytes()) - .withCpuPeriod(cr.cpuQuota() > 0 ? cr.cpuPeriod() : null) - .withCpuQuota(cr.cpuQuota() > 0 ? cr.cpuQuota() : null)); + .withCpuPeriod(cr.cpuQuota() > 0 ? (long) cr.cpuPeriod() : null) + .withCpuQuota(cr.cpuQuota() > 0 ? (long) cr.cpuQuota() : null)); final CreateContainerCmd containerCmd = docker .createContainerCmd(dockerImage.asString()) - .withHostConfig(hostConfig) // MUST BE FIRST (some of the later setters are simply proxied to HostConfig) + .withHostConfig(hostConfig) .withName(containerName.asString()) .withLabels(labels) - .withEnv(environmentAssignments) - .withBinds(volumeBinds) - .withUlimits(ulimits) - .withCapAdd(new ArrayList<>(addCapabilities)) - .withCapDrop(new ArrayList<>(dropCapabilities)) - .withPrivileged(privileged); + .withEnv(environmentAssignments); networkMode .filter(mode -> ! mode.toLowerCase().equals("host")) .ifPresent(mode -> containerCmd.withMacAddress(generateMACAddress(hostName, ipv4Address, ipv6Address))); hostName.ifPresent(containerCmd::withHostName); - networkMode.ifPresent(containerCmd::withNetworkMode); + networkMode.ifPresent(hostConfig::withNetworkMode); ipv4Address.ifPresent(containerCmd::withIpv4Address); ipv6Address.ifPresent(containerCmd::withIpv6Address); entrypoint.ifPresent(containerCmd::withEntrypoint); diff --git a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java index e57f61ce5f4..a8e08a19d3d 100644 --- a/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java +++ b/docker-api/src/main/java/com/yahoo/vespa/hosted/dockerapi/DockerImpl.java @@ -183,8 +183,7 @@ public class DockerImpl implements Docker { DockerStatsCallback statsCallback = dockerClient.statsCmd(containerName.asString()).exec(new DockerStatsCallback()); statsCallback.awaitCompletion(5, TimeUnit.SECONDS); - return statsCallback.stats.map(stats -> new ContainerStats( - stats.getNetworks(), stats.getCpuStats(), stats.getMemoryStats(), stats.getBlkioStats())); + return statsCallback.stats.map(ContainerStats::new); } catch (NotFoundException ignored) { return Optional.empty(); } catch (RuntimeException | InterruptedException e) { |