summaryrefslogtreecommitdiffstats
path: root/config-provisioning/src/main/java/com/yahoo/config/provision/ClusterSpec.java
blob: 6a79f2f06ed2d7e0e799d13cfe61ff1d974a07f0 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config.provision;

import com.yahoo.component.Version;
import com.yahoo.component.Vtag;

import java.util.Objects;
import java.util.Optional;

/**
 * A specification of a cluster - or group in a grouped cluster - to be run on a set of hosts.
 * This is a value object.
 *
 * @author bratseth
 */
public final class ClusterSpec {

    private final Type type;
    private final Id id;

    /** The group id of these hosts, or empty if this is represents a request for hosts */
    private final Optional<Group> groupId;

    private final Version vespaVersion;

    private ClusterSpec(Type type, Id id, Optional<Group> groupId, Version vespaVersion) {
        this.type = type;
        this.id = id;
        this.groupId = groupId;
        this.vespaVersion = vespaVersion;
    }

    /** Returns the cluster type */
    public Type type() { return type; }

    /** Returns the cluster id */
    public Id id() { return id; }

    public Version vespaVersion() { return vespaVersion; }

    public String dockerImage() {
        return DockerImage.defaultImage.withTag(vespaVersion).toString();
    }

    /** Returns the group within the cluster this specifies, or empty to specify the whole cluster */
    public Optional<Group> group() { return groupId; }

    public ClusterSpec changeGroup(Optional<Group> newGroup) { return new ClusterSpec(type, id, newGroup, vespaVersion); }

    /** Create a specification <b>requesting</b> a cluster with these attributes */
    @Deprecated
    // TODO: April 2017 - Remove this when no version older than 6.94 is used anywhere
    public static ClusterSpec request(Type type, Id id, Optional<String> dockerImage) {
        return requestVersion(type, id, dockerImage.map(DockerImage::new).map(DockerImage::tagAsVersion));
    }

    /** Create a specification <b>requesting</b> a cluster with these attributes */
    // TODO: April 2017 - Remove this when no version older than 6.97 is used anywhere
    public static ClusterSpec requestVersion(Type type, Id id, Optional<Version> vespaVersion) {
        return new ClusterSpec(type, id, Optional.empty(), vespaVersion.orElse(Vtag.currentVersion));
    }

    public static ClusterSpec request(Type type, Id id, Version vespaVersion) {
        return new ClusterSpec(type, id, Optional.empty(), vespaVersion);
    }

    /** Create a specification <b>specifying</b> an existing cluster group having these attributes */
    // TODO: April 2017 - Remove this when no version older than 6.97 is used anywhere
    public static ClusterSpec from(Type type, Id id, Group groupId, Optional<Version> vespaVersion) {
        return new ClusterSpec(type, id, Optional.of(groupId), vespaVersion.orElse(Vtag.currentVersion));
    }

    public static ClusterSpec from(Type type, Id id, Group groupId, Version vespaVersion) {
        return new ClusterSpec(type, id, Optional.of(groupId), vespaVersion);
    }

    @Override
    public String toString() {
        return String.join(" ", type.toString(), id.toString(),
                           groupId.map(Group::toString).orElse(""),
                           vespaVersion.toString());
    }

    @Override
    public int hashCode() { return type.hashCode() + 17 * id.hashCode() + 31 * groupId.hashCode(); }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof ClusterSpec)) return false;
        ClusterSpec other = (ClusterSpec)o;
        if ( ! other.type.equals(this.type)) return false;
        if ( ! other.id.equals(this.id)) return false;
        if ( ! other.groupId.equals(this.groupId)) return false;
        if ( ! other.vespaVersion.equals(this.vespaVersion)) return false;
        return true;
    }

    /** Returns whether this is equal, disregarding the group value and wanted Vespa version */
    public boolean equalsIgnoringGroupAndVespaVersion(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof ClusterSpec)) return false;
        ClusterSpec other = (ClusterSpec)o;
        if ( ! other.type.equals(this.type)) return false;
        if ( ! other.id.equals(this.id)) return false;
        return true;
    }

    /** A cluster type */
    public enum Type {

        // These enum values are stored in ZooKeeper - do not change
        admin,
        container,
        content;

        public static Type from(String typeName) {
            switch (typeName) {
                case "admin" : return admin;
                case "container" : return container;
                case "content" : return content;
                default: throw new IllegalArgumentException("Illegal cluster type '" + typeName + "'");
            }
        }

    }

    public static final class Id {

        private final String id;

        public Id(String id) {
            Objects.requireNonNull(id, "Id cannot be null");
            this.id = id;
        }

        public static Id from(String id) {
            return new Id(id);
        }

        public String value() { return id; }

        @Override
        public String toString() { return "cluster '" + id + "'"; }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            return ((Id)o).id.equals(this.id);
        }

        @Override
        public int hashCode() {
            return id.hashCode();
        }

    }

    /** Identifier of a group within a cluster */
    @SuppressWarnings("deprecation")
    public static final class Group {

        private final int index;

        private Group(int index) {
            this.index = index;
        }

        public static Group from(int index) { return new Group(index); }

        public int index() { return index; }

        @Override
        public String toString() { return "group " + index; }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            return ((Group)o).index == this.index;
        }

        @Override
        public int hashCode() { return index; }

    }

}