summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/maintenance/InfrastructureVersions.java
blob: 31dd5d7440470ea8dc50a78931e4cb3c26d4474d (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
package com.yahoo.vespa.hosted.provision.maintenance;

import com.yahoo.component.Version;
import com.yahoo.config.provision.NodeType;
import com.yahoo.vespa.curator.Lock;
import com.yahoo.vespa.hosted.provision.persistence.CuratorDatabaseClient;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Logger;

/**
 * Multithread safe class to see and set target versions for infrastructure node types.
 * {@link InfrastructureProvisioner} maintainer will then allocate the nodes of given node type
 * with a wanted version equal to the given target version.
 *
 * @author freva
 */
public class InfrastructureVersions {

    private static Logger logger = Logger.getLogger(InfrastructureVersions.class.getName());

    private final CuratorDatabaseClient db;

    public InfrastructureVersions(CuratorDatabaseClient db) {
        this.db = db;
    }

    public void setTargetVersion(NodeType nodeType, Version newTargetVersion, boolean force) {
        switch (nodeType) {
            case config:
            case confighost:
            case proxyhost:
            case controller:
                break;
            default:
                throw new IllegalArgumentException("Cannot set version for type " + nodeType);
        }
        if (newTargetVersion.isEmpty()) {
            throw  new IllegalArgumentException("Invalid target version: " + newTargetVersion.toFullString());
        }

        try (Lock lock = db.lockInfrastructureVersions()) {
            Map<NodeType, Version> infrastructureVersions = db.readInfrastructureVersions();
            Optional<Version> currentTargetVersion = Optional.ofNullable(infrastructureVersions.get(nodeType));

            // Trying to set the version to the current version, skip
            if (currentTargetVersion.equals(Optional.of(newTargetVersion))) return;

            // If we don't force the set, we must set the new version to higher than the already set version
            if (!force && currentTargetVersion.isPresent()) {
                if (currentTargetVersion.get().isAfter(newTargetVersion))
                    throw new IllegalArgumentException(String.format("Cannot downgrade version without setting 'force'. " +
                            "Current target version: %s, attempted to set target version: %s",
                            currentTargetVersion.get().toFullString(), newTargetVersion.toFullString()));
            }

            infrastructureVersions.put(nodeType, newTargetVersion);
            db.writeInfrastructureVersions(infrastructureVersions);
            logger.info("Set target version for " + nodeType + " to " + newTargetVersion.toFullString());
        }
    }

    public Optional<Version> getTargetVersionFor(NodeType nodeType) {
        return Optional.ofNullable(db.readInfrastructureVersions().get(nodeType));
    }

    public Map<NodeType, Version> getTargetVersions() {
        return Collections.unmodifiableMap(db.readInfrastructureVersions());
    }
}