aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/resource/ResourceAllocation.java
blob: 3443d9b2f3ff8ed98116954eb08c905b641936b3 (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
// Copyright Vespa.ai. 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.NodeResources;

import java.util.Objects;

/**
 * An allocation of node resources.
 *
 * @author ldalves
 */
public class ResourceAllocation {

    public static final ResourceAllocation ZERO = new ResourceAllocation(0, 0, 0, NodeResources.Architecture.getDefault());

    private final double cpuCores;
    private final double memoryGb;
    private final double diskGb;
    private final NodeResources.Architecture architecture;

    public ResourceAllocation(double cpuCores, double memoryGb, double diskGb, NodeResources.Architecture architecture) {
        this.cpuCores = cpuCores;
        this.memoryGb = memoryGb;
        this.diskGb = diskGb;
        this.architecture = architecture;
    }

    public double usageFraction(ResourceAllocation total) {
        return (cpuCores / total.cpuCores + memoryGb / total.memoryGb + diskGb / total.diskGb) / 3;
    }

    public double getCpuCores() {
        return cpuCores;
    }

    public double getMemoryGb() {
        return memoryGb;
    }

    public double getDiskGb() {
        return diskGb;
    }

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

    /** Returns a copy of this with the given allocation added */
    public ResourceAllocation plus(ResourceAllocation allocation) {
        return new ResourceAllocation(cpuCores + allocation.cpuCores, memoryGb + allocation.memoryGb, diskGb + allocation.diskGb, architecture);
    }

    /** Returns a copy of this with each resource multiplied by given factor */
    public ResourceAllocation multiply(double multiplicand) {
        return new ResourceAllocation(cpuCores * multiplicand, memoryGb * multiplicand, diskGb * multiplicand, architecture);
    }

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

        ResourceAllocation other = (ResourceAllocation) o;
        return Double.compare(this.cpuCores, other.cpuCores) == 0 &&
                Double.compare(this.memoryGb, other.memoryGb) == 0 &&
                Double.compare(this.diskGb, other.diskGb) == 0;

    }

    @Override
    public int hashCode() {
        return Objects.hash(cpuCores, memoryGb, diskGb);
    }

}