summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/restapi/ApplicationPatcher.java
blob: 871f30570f5843733c5650edff4329731c9b0b14 (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
83
84
85
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.restapi;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.io.IOUtils;
import com.yahoo.slime.Inspector;
import com.yahoo.slime.SlimeUtils;
import com.yahoo.slime.Type;
import com.yahoo.transaction.Mutex;
import com.yahoo.vespa.hosted.provision.NodeRepository;
import com.yahoo.vespa.hosted.provision.applications.Application;

import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.time.Duration;

/**
 * A class which can take a partial JSON node/v2 application JSON structure and apply it to an application object.
 * This is a one-time use object.
 *
 * @author bratseth
 */
public class ApplicationPatcher implements AutoCloseable {

    private final Inspector inspector;

    private final Mutex lock;
    private Application application;

    public ApplicationPatcher(InputStream json, ApplicationId applicationId, NodeRepository nodeRepository) {
        try {
            this.inspector = SlimeUtils.jsonToSlime(IOUtils.readBytes(json, 1000 * 1000)).get();
        } catch (IOException e) {
            throw new UncheckedIOException("Error reading request body", e);
        }
        // Use same timeout for acquiring lock as client timeout for patch request
        this.lock = nodeRepository.applications().lock(applicationId, Duration.ofSeconds(30));
        try {
            this.application = nodeRepository.applications().require(applicationId);
        }
        catch (RuntimeException e) {
            lock.close();
            throw e;
        }
    }

    /** Applies the json to the application and returns it. */
    public Application apply() {
        inspector.traverse((String name, Inspector value) -> {
            try {
                application = applyField(application, name, value);
            } catch (IllegalArgumentException e) {
                throw new IllegalArgumentException("Could not set field '" + name + "'", e);
            }
        });
        return application;
    }

    /** Returns the application in its current state (patch applied or not) */
    public Application application() { return application; }

    public Mutex lock() { return lock; }

    @Override
    public void close() {
        lock.close();
    }

    private Application applyField(Application application, String name, Inspector value) {
        return switch (name) {
            case "currentReadShare" -> application.with(application.status().withCurrentReadShare(asDouble(value)));
            case "maxReadShare" -> application.with(application.status().withMaxReadShare(asDouble(value)));
            default -> throw new IllegalArgumentException("Could not apply field '" + name +
                                                          "' on an application: No such modifiable field");
        };
    }

    private Double asDouble(Inspector field) {
        if (field.type() != Type.DOUBLE)
            throw new IllegalArgumentException("Expected a DOUBLE value, got a " + field.type());
        return field.asDouble();
    }

}