summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/maintenance/RetiredExpirer.java
blob: 4c5c8adf576f4259c9e61718bd0e762c288edb0f (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
// 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.collections.ListMap;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Deployer;
import com.yahoo.config.provision.Deployment;
import com.yahoo.vespa.hosted.provision.Node;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.vespa.hosted.provision.node.History;

import java.time.Clock;
import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;

/**
 * Maintenance job which deactivates nodes which has been retired.
 * This should take place after the system has been given sufficient time to migrate data to other nodes.
 * <p>
 * As these nodes are active, and therefore part of the configuration the impacted applications must be
 * reconfigured after inactivation.
 *
 * @author bratseth
 */
public class RetiredExpirer extends Expirer {

    private final NodeRepository nodeRepository;
    private final Deployer deployer;

    public RetiredExpirer(NodeRepository nodeRepository, Deployer deployer, Clock clock, 
                          Duration retiredDuration, JobControl jobControl) {
        super(Node.State.active, History.Event.Type.retired, nodeRepository, clock, retiredDuration, jobControl);
        this.nodeRepository = nodeRepository;
        this.deployer = deployer;
    }

    @Override
    protected void expire(List<Node> expired) {
        // Only expire nodes which are retired. Do one application at the time.
        ListMap<ApplicationId, Node> applicationNodes = new ListMap<>();
        for (Node node : expired) {
            if (node.allocation().isPresent() && node.allocation().get().membership().retired())
                applicationNodes.put(node.allocation().get().owner(), node);
        }

        for (Map.Entry<ApplicationId, List<Node>> entry : applicationNodes.entrySet()) {
            ApplicationId application = entry.getKey();
            List<Node> nodesToRemove = entry.getValue();
            try {
                Optional<Deployment> deployment = deployer.deployFromLocalActive(application);
                if ( ! deployment.isPresent()) continue; // this will be done at another config server

                nodeRepository.setRemovable(application, nodesToRemove);

                deployment.get().activate();

                log.info("Redeployed " + application + " to deactivate " + nodesToRemove.size() + " retired nodes");
            }
            catch (RuntimeException e) {
                log.log(Level.WARNING, "Exception trying to remove previously retired nodes " + nodesToRemove +
                        "from " + application, e);
            }
        }
    }

}