aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/Deployment.java
blob: de26ca73cd8f91ea39d91db0ac646384ae9866ff (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
// 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.application;

import com.yahoo.component.Version;
import com.yahoo.config.provision.CloudAccount;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.vespa.hosted.controller.api.integration.dataplanetoken.TokenId;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.RevisionId;

import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalDouble;

/**
 * A deployment of an application in a particular zone.
 * 
 * @author bratseth
 * @author smorgrav
 */
public class Deployment {

    private final ZoneId zone;
    private final CloudAccount cloudAccount;
    private final RevisionId revision;
    private final Version version;
    private final Instant deployTime;
    private final DeploymentMetrics metrics;
    private final DeploymentActivity activity;
    private final QuotaUsage quota;
    private final OptionalDouble cost;
    private final Map<TokenId, Instant> dataPlaneTokens;

    public Deployment(ZoneId zone, CloudAccount cloudAccount, RevisionId revision, Version version, Instant deployTime,
                      DeploymentMetrics metrics, DeploymentActivity activity, QuotaUsage quota, OptionalDouble cost,
                      Map<TokenId, Instant> dataPlaneTokens) {
        this.zone = Objects.requireNonNull(zone, "zone cannot be null");
        this.cloudAccount = Objects.requireNonNull(cloudAccount, "cloudAccount cannot be null");
        this.revision = Objects.requireNonNull(revision, "revision cannot be null");
        this.version = Objects.requireNonNull(version, "version cannot be null");
        this.deployTime = Objects.requireNonNull(deployTime, "deployTime cannot be null");
        this.metrics = Objects.requireNonNull(metrics, "deploymentMetrics cannot be null");
        this.activity = Objects.requireNonNull(activity, "activity cannot be null");
        this.quota = Objects.requireNonNull(quota, "usage cannot be null");
        this.cost = Objects.requireNonNull(cost, "cost cannot be null");
        this.dataPlaneTokens = Map.copyOf(dataPlaneTokens);
    }

    /** Returns the zone this was deployed to */
    public ZoneId zone() { return zone; }

    /** Returns the cloud account this was deployed to */
    public CloudAccount cloudAccount() { return cloudAccount; }

    /** Returns the deployed application revision */
    public RevisionId revision() { return revision; }
    
    /** Returns the deployed Vespa version */
    public Version version() { return version; }

    /** Returns the time this was deployed */
    public Instant at() { return deployTime; }

    /** Returns metrics for this */
    public DeploymentMetrics metrics() {
        return metrics;
    }

    /** Returns activity for this */
    public DeploymentActivity activity() { return activity; }

    /** Returns quota usage for this */
    public QuotaUsage quota() { return quota; }

    /** Returns cost, in dollars per hour, for this */
    public OptionalDouble cost() { return cost; }

    /** Returns the data plane token IDs referenced by this deployment, and the last update time of this token at the time of deployment. */
    public Map<TokenId, Instant> dataPlaneTokens() { return dataPlaneTokens; }

    public Deployment recordActivityAt(Instant instant) {
        return new Deployment(zone, cloudAccount, revision, version, deployTime, metrics,
                              activity.recordAt(instant, metrics), quota, cost, dataPlaneTokens);
    }

    public Deployment withMetrics(DeploymentMetrics metrics) {
        return new Deployment(zone, cloudAccount, revision, version, deployTime, metrics, activity, quota, cost, dataPlaneTokens);
    }

    public Deployment withCost(double cost) {
        if (this.cost.isPresent() && Double.compare(this.cost.getAsDouble(), cost) == 0) return this;
        return new Deployment(zone, cloudAccount, revision, version, deployTime, metrics, activity, quota, OptionalDouble.of(cost), dataPlaneTokens);
    }

    public Deployment withoutCost() {
        if (cost.isEmpty()) return this;
        return new Deployment(zone, cloudAccount, revision, version, deployTime, metrics, activity, quota, OptionalDouble.empty(), dataPlaneTokens);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Deployment that = (Deployment) o;
        return    Objects.equals(zone, that.zone)
               && Objects.equals(cloudAccount, that.cloudAccount)
               && Objects.equals(revision, that.revision)
               && Objects.equals(version, that.version)
               && Objects.equals(deployTime, that.deployTime)
               && Objects.equals(metrics, that.metrics)
               && Objects.equals(activity, that.activity)
               && Objects.equals(quota, that.quota)
               && Objects.equals(cost, that.cost)
               && Objects.equals(dataPlaneTokens, that.dataPlaneTokens);
    }

    @Override
    public int hashCode() {
        return Objects.hash(zone, cloudAccount, revision, version, deployTime, metrics, activity, quota, cost, dataPlaneTokens);
    }

    @Override
    public String toString() {
        return "deployment to " + zone + " of " + revision + " on version " + version + " at " + deployTime;
    }

}