aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/DeploymentExpirer.java
blob: c22cb1efdb3f84a5bccc0d97918f942f32bff9af (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.maintenance;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.vespa.hosted.controller.Application;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.Instance;
import com.yahoo.vespa.hosted.controller.application.Deployment;
import com.yahoo.yolean.Exceptions;

import java.time.Duration;
import java.util.Optional;
import java.util.logging.Level;

/**
 * Expires instances in zones that have configured expiration using TimeToLive.
 * 
 * @author mortent
 * @author bratseth
 */
public class DeploymentExpirer extends ControllerMaintainer {

    public DeploymentExpirer(Controller controller, Duration interval) {
        super(controller, interval);
    }

    @Override
    protected double maintain() {
        int attempts = 0;
        int failures = 0;
        for (Application application : controller().applications().readable()) {
            for (Instance instance : application.instances().values())
                for (Deployment deployment : instance.deployments().values()) {
                    if (!isExpired(deployment, instance.id())) continue;

                    try {
                        log.log(Level.INFO, "Expiring deployment of " + instance.id() + " in " + deployment.zone());
                        attempts++;
                        controller().applications().deactivate(instance.id(), deployment.zone());
                    } catch (Exception e) {
                        failures++;
                        log.log(Level.WARNING, "Could not expire " + deployment + " of " + instance +
                                               ": " + Exceptions.toMessageString(e) + ". Retrying in " +
                                               interval());
                    }
                }
        }
        return asSuccessFactorDeviation(attempts, failures);
    }

    /** Returns whether given deployment has expired according to its TTL */
    private boolean isExpired(Deployment deployment, ApplicationId instance) {
        if (deployment.zone().environment().isProduction()) return false; // Never expire production deployments

        Optional<Duration> ttl = controller().zoneRegistry().getDeploymentTimeToLive(deployment.zone());
        if (ttl.isEmpty()) return false;

        return controller().jobController().lastDeploymentStart(instance, deployment)
                           .plus(ttl.get()).isBefore(controller().clock().instant());
    }

}