aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/application/validation/change/NodeResourceChangeValidator.java
blob: 7979c3c806979b40cf796435a48da74ab77ada36 (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation.change;

import com.yahoo.config.model.api.ConfigChangeAction;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.config.provision.HostSpec;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.container.ApplicationContainerCluster;
import com.yahoo.vespa.model.content.cluster.ContentCluster;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Emits restart change actions for clusters where the node resources are changed in a way
 * which requires a "restart" (container recreation) to take effect.
 * Nodes will restart on their own on this condition, but we want to emit restart actions to
 * defer applying new config until restart.
 *
 * @author bratseth
 */
public class NodeResourceChangeValidator implements ChangeValidator {

    @Override
    public List<ConfigChangeAction> validate(VespaModel current, VespaModel next, DeployState deployState) {
        var restartActions = new ArrayList<ConfigChangeAction>();
        for (ClusterSpec.Id clusterId : current.allClusters()) {
            Optional<NodeResources> currentResources = resourcesOf(clusterId, current);
            Optional<NodeResources> nextResources = resourcesOf(clusterId, next);
            if (currentResources.isEmpty() || nextResources.isEmpty()) continue; // new or removed cluster
            if ( changeRequiresRestart(currentResources.get(), nextResources.get()))
                restartActions.addAll(createRestartActionsFor(clusterId, current));
        }
        return restartActions;
    }

    private boolean changeRequiresRestart(NodeResources currentResources, NodeResources nextResources) {
        return currentResources.memoryGb() != nextResources.memoryGb();
    }

    private Optional<NodeResources> resourcesOf(ClusterSpec.Id clusterId, VespaModel model) {
        return model.allocatedHosts().getHosts().stream().filter(host -> host.membership().isPresent())
                                                         .filter(host -> host.membership().get().cluster().id().equals(clusterId))
                                                         .findFirst()
                                                         .map(HostSpec::advertisedResources);
    }

    private List<ConfigChangeAction> createRestartActionsFor(ClusterSpec.Id clusterId, VespaModel model) {
        ApplicationContainerCluster containerCluster = model.getContainerClusters().get(clusterId.value());
        if (containerCluster != null)
            return createRestartActionsFor(containerCluster);

        ContentCluster contentCluster = model.getContentClusters().get(clusterId.value());
        if (contentCluster != null)
            return createRestartActionsFor(contentCluster);

        return List.of();
    }

    private List<ConfigChangeAction> createRestartActionsFor(ApplicationContainerCluster cluster) {
        return cluster.getContainers().stream()
                                      .map(container -> new VespaRestartAction(cluster.id(),
                                                                               "Node resource change",
                                                                               container.getServiceInfo(),
                                                                               false))
                                     .collect(Collectors.toList());
    }

    private List<ConfigChangeAction> createRestartActionsFor(ContentCluster cluster) {
        return cluster.getSearch().getSearchNodes().stream()
                                                   .map(node -> new VespaRestartAction(cluster.id(),
                                                                                       "Node resource change",
                                                                                       node.getServiceInfo(),
                                                                                      false))
                                                   .collect(Collectors.toList());
    }

}