summaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/resource/ResourceSnapshot.java
blob: 85ee23f4df0bd19ccb5b46d4779b51febf068b7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.resource;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.Node;

import java.time.Instant;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Represents the resources allocated to a deployment at a specific point in time.
 *
 * @author olaa
 */
public class ResourceSnapshot {

    private final ApplicationId applicationId;
    private final ResourceAllocation resourceAllocation;
    private final Instant timestamp;
    private final ZoneId zoneId;

    public ResourceSnapshot(ApplicationId applicationId, double cpuCores, double memoryGb, double diskGb, NodeResources.Architecture architecture, Instant timestamp, ZoneId zoneId) {
        this.applicationId = applicationId;
        this.resourceAllocation = new ResourceAllocation(cpuCores, memoryGb, diskGb, architecture);
        this.timestamp = timestamp;
        this.zoneId = zoneId;
    }

    public static ResourceSnapshot from(ApplicationId applicationId, int nodes, double cpuCores, double memoryGb, double diskGb, NodeResources.Architecture architecture, Instant timestamp, ZoneId zoneId) {
        return new ResourceSnapshot(applicationId, cpuCores * nodes, memoryGb * nodes, diskGb * nodes, architecture, timestamp, zoneId);
    }

    public static ResourceSnapshot from(List<Node> nodes, Instant timestamp, ZoneId zoneId) {
        Set<ApplicationId> applicationIds = nodes.stream()
                                                 .filter(node -> node.owner().isPresent())
                                                 .map(node -> node.owner().get())
                                                 .collect(Collectors.toSet());

        if (applicationIds.size() != 1) throw new IllegalArgumentException("List of nodes can only represent one application");

        return new ResourceSnapshot(
                applicationIds.iterator().next(),
                nodes.stream().map(Node::resources).mapToDouble(NodeResources::vcpu).sum(),
                nodes.stream().map(Node::resources).mapToDouble(NodeResources::memoryGb).sum(),
                nodes.stream().map(Node::resources).mapToDouble(NodeResources::diskGb).sum(),
                nodes.stream().map(node -> node.resources().architecture()).findFirst().orElse(NodeResources.Architecture.getDefault()),
                timestamp,
                zoneId
        );
    }

    public ApplicationId getApplicationId() {
        return applicationId;
    }

    public ResourceAllocation allocation() {
        return resourceAllocation;
    }

    public double getCpuCores() {
        return resourceAllocation.getCpuCores();
    }

    public double getMemoryGb() {
        return resourceAllocation.getMemoryGb();
    }

    public double getDiskGb() {
        return resourceAllocation.getDiskGb();
    }

    public Instant getTimestamp() {
        return timestamp;
    }

    public ZoneId getZoneId() {
        return zoneId;
    }

    public NodeResources.Architecture getArchitecture() {
        return resourceAllocation.getArchitecture();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ResourceSnapshot)) return false;

        ResourceSnapshot other = (ResourceSnapshot) o;
        return this.applicationId.equals(other.applicationId) &&
                this.resourceAllocation.equals(other.resourceAllocation) &&
                this.timestamp.equals(other.timestamp) &&
                this.zoneId.equals(other.zoneId);
    }

    @Override
    public int hashCode(){
        return Objects.hash(applicationId, resourceAllocation, timestamp, zoneId);
    }
}