aboutsummaryrefslogtreecommitdiffstats
path: root/config-lib/src/main/java/com/yahoo/config/ChangesRequiringRestart.java
blob: 73f5e94468d41e4227f00a7e425e1c9a0db8a646 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static java.util.stream.Collectors.joining;

/**
 * This class aggregates information about config changes that causes a restart to be required.
 *
 * @author Magnar Nedland
 */
public class ChangesRequiringRestart {

    static class ReportLine {
        private String name;
        private final Node from;
        private final Node to;
        private final String comment;

        public ReportLine(String name, Node from, Node to, String comment) {
            this.name = name;
            this.from = from;
            this.to = to;
            this.comment = comment;
        }

        public void addNamePrefix(String prefix) {
            if (!name.isEmpty()) {
                name = prefix + "." + name;
            } else {
                name = prefix;
            }
        }

        private String getCommentAndName(String indent, String namePrefix) {
            return indent + (comment.isEmpty()? "" : "# " + comment.replace("\n", "\n" + indent + "# ") + "\n" + indent)
                    + namePrefix + name;
        }

        private static String formatValue(String indent, Node n) {
            String str = n.toString();
            if (str.contains("\n")) {  // Struct
                str = "\n" + indent + "  { " + str.replace("\n", "\n" + indent + "    ") + " }";
            }
            return str;
        }

        @Override
        public String toString() {
            return toString("", "");
        }

        public String toString(String indent, String namePrefix) {
            if (from == null) {
                return getCommentAndName(indent, namePrefix) + " was added with value " + formatValue(indent, to);
            } else if (to == null) {
                return getCommentAndName(indent, namePrefix) + " with value " + formatValue(indent, from) + " was removed";
            }
            return getCommentAndName(indent, namePrefix) + " has changed from " + formatValue(indent, from) + " to " + formatValue(indent, to);
        }
    }

    private final ArrayList<ReportLine> report = new ArrayList<>();
    private final String componentName;

    public ChangesRequiringRestart(String componentName) {
        this.componentName = componentName;
    }

    public String getName() {
        return componentName;
    }

    public ChangesRequiringRestart compare(Node from, Node to, String name, String comment) {
        if (!from.equals(to)) {
            report.add(new ReportLine(name, from, to, comment));
        }
        return this;
    }

    public void mergeChanges(String prefix, ChangesRequiringRestart childReport) {
        for (ReportLine line : childReport.getReportLines()) {
            line.addNamePrefix(prefix);
            report.add(line);
        }
    }

    /**
     * Interface used to pass lambda functions from generated code to compareArray/-Map functions.
     */
    public interface CompareFunc {
        // Generates a report based on a config change.
        ChangesRequiringRestart getChangesRequiringRestart(Node from, Node to);
    }

    public ChangesRequiringRestart compareArray(List<? extends Node> from,
                                                List<? extends Node> to,
                                                String name,
                                                String comment,
                                                CompareFunc func) {
        if (!from.equals(to)) {
            int commonElements = Math.min(from.size(), to.size());
            for (int i = 0; i < commonElements; ++i) {
                ChangesRequiringRestart childReport = func.getChangesRequiringRestart(from.get(i), to.get(i));
                String prefix = childReport.componentName + "[" + i + "]";
                mergeChanges(prefix, childReport);
            }
            for (int i = commonElements; i < from.size(); ++i) {
                report.add(new ReportLine(name + "[" + i + "]", from.get(i), null, comment));
            }
            for (int i = commonElements; i < to.size(); ++i) {
                report.add(new ReportLine(name + "[" + i + "]", null, to.get(i), comment));
            }
        }
        return this;
    }

    public ChangesRequiringRestart compareMap(Map<String, ? extends Node> from,
                                              Map<String, ? extends Node> to,
                                              String name,
                                              String comment,
                                              CompareFunc func) {
        if (!from.equals(to)) {
            for (String key : from.keySet()) {
                if (to.containsKey(key)) {
                    ChangesRequiringRestart childReport = func.getChangesRequiringRestart(from.get(key), to.get(key));
                    String prefix = childReport.componentName + "{" + key + "}";
                    mergeChanges(prefix, childReport);
                } else {
                    report.add(new ReportLine(name + "{" + key + "}", from.get(key), null, comment));
                }
            }
            for (String key : to.keySet()) {
                if (!from.containsKey(key)) {
                    report.add(new ReportLine(name + "{" + key + "}", null, to.get(key), comment));
                }
            }
        }
        return this;
    }

    List<ReportLine> getReportLines() {
        return report;
    }

    @Override
    public String toString() {
        return toString("");
    }

    public String toString(String indent) {
        return report.stream()
                .map(line -> line.toString(indent, componentName + "."))
                .collect(joining("\n"));
    }

    public boolean needsRestart() {
        return !report.isEmpty();
    }

}