summaryrefslogtreecommitdiffstats
path: root/config-provisioning/src/main/java/com/yahoo/config/provision/FlavorSpec.java
blob: 62cfb59c51cf3ccb73d429810095520cd920afb4 (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
106
107
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision;

import java.util.Objects;

/**
 * The node capacity specified by an application, which is matched to an actual flavor during provisioning.
 *
 * @author bratseth
 */
public class FlavorSpec {

    private final double cpuCores;
    private final double memoryGb;
    private final double diskGb;

    private final boolean allocateByLegacyName;
    private final String legacyFlavorName;

    public FlavorSpec(double cpuCores, double memoryGb, double diskGb) {
        this.cpuCores = cpuCores;
        this.memoryGb = memoryGb;
        this.diskGb = diskGb;
        this.allocateByLegacyName = false;
        this.legacyFlavorName = null;
    }

    private FlavorSpec(double cpuCores, double memoryGb, double diskGb, boolean allocateByLegacyName, String legacyFlavorName) {
        this.cpuCores = cpuCores;
        this.memoryGb = memoryGb;
        this.diskGb = diskGb;
        this.allocateByLegacyName = allocateByLegacyName;
        this.legacyFlavorName = legacyFlavorName;
    }

    public double cpuCores() { return cpuCores; }
    public double memoryGb() { return memoryGb; }
    public double diskGb() { return diskGb; }

    /**
     * If this is true, a non-docker legacy name was used to specify this and we'll respect that by mapping directly.
     * The other getters of this will return 0.
     */
    public boolean allocateByLegacyName() { return allocateByLegacyName; }

    /** Returns the legacy flavor string of this. This is never null. */
    public String legacyFlavorName() {
        if (legacyFlavorName != null)
            return legacyFlavorName;
        else
            return "d-" + (int)Math.ceil(cpuCores) + "-" + (int)Math.ceil(memoryGb) + "-" + (int)Math.ceil(diskGb);
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof FlavorSpec)) return false;
        FlavorSpec other = (FlavorSpec)o;
        if (allocateByLegacyName) {
            return this.legacyFlavorName.equals(other.legacyFlavorName);
        }
        else {
            if (this.cpuCores != other.cpuCores) return false;
            if (this.memoryGb != other.memoryGb) return false;
            if (this.diskGb != other.diskGb) return false;
            return true;
        }
    }

    @Override
    public int hashCode() {
        if (allocateByLegacyName)
            return legacyFlavorName.hashCode();
        else
            return (int)(2503 * cpuCores + 22123 * memoryGb + 26987 * diskGb);
    }

    @Override
    public String toString() {
        if (allocateByLegacyName)
            return "flavor '" + legacyFlavorName + "'";
        else
            return "cpu cores: " + cpuCores + ", memory: " + memoryGb + " Gb, disk " + diskGb + " Gb";
    }

    /**
     * Create this from a legacy flavor string.
     *
     * @throws IllegalArgumentException if the given string does not map to a legacy flavor
     */
    public static FlavorSpec fromLegacyFlavorName(String flavorString) {
        if (flavorString.startsWith("d-")) { // A docker flavor
            String[] parts = flavorString.split("-");
            double cpu = Integer.parseInt(parts[1]);
            double mem = Integer.parseInt(parts[2]);
            double dsk = Integer.parseInt(parts[3]);
            if (cpu == 0) cpu = 0.5;
            if (cpu == 2 && mem == 8 ) cpu = 1.5;
            if (cpu == 2 && mem == 12 ) cpu = 2.3;
            return new FlavorSpec(cpu, mem, dsk, false, flavorString);
        }
        else {
            return new FlavorSpec(0, 0, 0, true, flavorString);
        }
    }

}