aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/applications/Application.java
blob: df5044de05cf8f3e1db43e72dc999a6bc0dbdf73 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.applications;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Capacity;
import com.yahoo.config.provision.ClusterSpec;
import java.util.Collection;
import java.util.Map;
import java.util.HashMap;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * The node repository's view of an application deployment.
 *
 * This is immutable, and must be locked with the application lock on read-modify-write.
 *
 * @author bratseth
 */
public class Application {

    private final ApplicationId id;
    private final Status status;
    private final Map<ClusterSpec.Id, Cluster> clusters;

    /** Do not use */
    public Application(ApplicationId id, Status status, Collection<Cluster> clusters) {
        this(id, status, clusters.stream().collect(Collectors.toMap(c -> c.id(), c -> c)));
    }

    private Application(ApplicationId id, Status status, Map<ClusterSpec.Id, Cluster> clusters) {
        this.id = id;
        this.clusters = clusters;
        this.status = status;
    }

    public ApplicationId id() { return id; }

    public Status status() { return status; }

    public Application with(Status status) {
        return new Application(id, status, clusters);
    }

    public Map<ClusterSpec.Id, Cluster> clusters() { return clusters; }

    public Optional<Cluster> cluster(ClusterSpec.Id id) {
        return Optional.ofNullable(clusters.get(id));
    }

    public Application with(Cluster cluster) {
        Map<ClusterSpec.Id, Cluster> clusters = new HashMap<>(this.clusters);
        clusters.put(cluster.id(), cluster);
        return new Application(id, status, clusters);
    }

    /**
     * Returns an application with the given cluster having the min and max resource limits of the given cluster.
     * If the cluster has a target which is not inside the new limits, the target is removed.
     */
    public Application withCluster(ClusterSpec.Id id, boolean exclusive, Capacity requested) {
        Cluster cluster = clusters.get(id);
        if (cluster == null)
            cluster = Cluster.create(id, exclusive, requested);
        else
            cluster = cluster.withConfiguration(exclusive, requested);
        return with(cluster);
    }

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

    @Override
    public boolean equals(Object other) {
        if (other == this) return true;
        if ( ! (other instanceof Application)) return false;
        return ((Application)other).id().equals(this.id());
    }

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

    public static Application empty(ApplicationId id) {
        return new Application(id, Status.initial(), Map.of());
    }

}