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

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Deployer;
import com.yahoo.jdisc.Metric;
import com.yahoo.vespa.flags.BooleanFlag;
import com.yahoo.vespa.flags.FetchVector;
import com.yahoo.vespa.flags.FlagSource;
import com.yahoo.vespa.flags.PermanentFlags;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeRepository;

import java.time.Duration;
import java.time.Instant;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * The application maintainer regularly redeploys all applications to make sure the node repo and application
 * model is in sync and to trigger background node allocation changes such as allocation optimizations and
 * flavor retirement.
 *
 * @author bratseth
 */
public class PeriodicApplicationMaintainer extends ApplicationMaintainer {

    private final Duration minTimeBetweenRedeployments;
    private final FlagSource flagSource;

    PeriodicApplicationMaintainer(Deployer deployer, Metric metric, NodeRepository nodeRepository,
                                  Duration interval, Duration minTimeBetweenRedeployments, FlagSource flagSource) {
        super(deployer, metric, nodeRepository, interval);
        this.minTimeBetweenRedeployments = minTimeBetweenRedeployments;
        this.flagSource = flagSource;
    }

    @Override
    protected boolean canDeployNow(ApplicationId application) {
        return deployer().lastDeployTime(application)
                // Don't deploy if a regular deploy just happened
                .map(lastDeployTime -> lastDeployTime.isBefore(nodeRepository().clock().instant().minus(minTimeBetweenRedeployments)))
                // We only know last deploy time for applications that were deployed on this config server,
                // the rest will be deployed on another config server
                .orElse(false);
    }

    // Returns the applications that need to be redeployed by this config server at this point in time.
    @Override
    protected Set<ApplicationId> applicationsNeedingMaintenance() {
        if (deployer().bootstrapping()) return Set.of();

        // Collect all deployment times before sorting as deployments may happen while we build the set, breaking
        // the comparable contract. Stale times are fine as the time is rechecked in ApplicationMaintainer#deployWithLock
        Map<ApplicationId, Instant> deploymentTimes = nodesNeedingMaintenance().stream()
                                                                               .map(node -> node.allocation().get().owner())
                                                                               .distinct()
                                                                               .filter(this::canDeployNow)
                                                                               .collect(Collectors.toMap(Function.identity(), this::getLastDeployTime));

        return deploymentTimes.entrySet().stream()
                              .sorted(Map.Entry.comparingByValue())
                              .map(Map.Entry::getKey)
                              .filter(id -> shouldMaintain(id))
                              .collect(Collectors.toCollection(LinkedHashSet::new));
    }

    private boolean shouldMaintain(ApplicationId id) {
        BooleanFlag skipMaintenanceDeployment = PermanentFlags.SKIP_MAINTENANCE_DEPLOYMENT.bindTo(flagSource)
                .with(FetchVector.Dimension.APPLICATION_ID, id.serializedForm());
        return ! skipMaintenanceDeployment.value();
    }

    protected List<Node> nodesNeedingMaintenance() {
        return nodeRepository().getNodes(Node.State.active);
    }

}