summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/persistence/RunSerializer.java
blob: f29af1055d0b593cd299dbbc0f57f31d191b15b1 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.persistence;

import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.slime.ArrayTraverser;
import com.yahoo.slime.Cursor;
import com.yahoo.slime.Inspector;
import com.yahoo.slime.ObjectTraverser;
import com.yahoo.slime.Slime;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.JobType;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.RunId;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.ApplicationVersion;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.SourceRevision;
import com.yahoo.vespa.hosted.controller.deployment.Run;
import com.yahoo.vespa.hosted.controller.deployment.RunStatus;
import com.yahoo.vespa.hosted.controller.deployment.Step;
import com.yahoo.vespa.hosted.controller.deployment.Step.Status;
import com.yahoo.vespa.hosted.controller.deployment.Versions;

import java.time.Instant;
import java.util.EnumMap;
import java.util.Optional;
import java.util.SortedMap;
import java.util.TreeMap;

import static com.yahoo.vespa.hosted.controller.deployment.RunStatus.aborted;
import static com.yahoo.vespa.hosted.controller.deployment.RunStatus.deploymentFailed;
import static com.yahoo.vespa.hosted.controller.deployment.RunStatus.installationFailed;
import static com.yahoo.vespa.hosted.controller.deployment.RunStatus.outOfCapacity;
import static com.yahoo.vespa.hosted.controller.deployment.RunStatus.running;
import static com.yahoo.vespa.hosted.controller.deployment.RunStatus.success;
import static com.yahoo.vespa.hosted.controller.deployment.RunStatus.error;
import static com.yahoo.vespa.hosted.controller.deployment.RunStatus.testFailure;
import static com.yahoo.vespa.hosted.controller.deployment.Step.Status.failed;
import static com.yahoo.vespa.hosted.controller.deployment.Step.Status.succeeded;
import static com.yahoo.vespa.hosted.controller.deployment.Step.Status.unfinished;
import static com.yahoo.vespa.hosted.controller.deployment.Step.copyVespaLogs;
import static com.yahoo.vespa.hosted.controller.deployment.Step.deactivateReal;
import static com.yahoo.vespa.hosted.controller.deployment.Step.deactivateTester;
import static com.yahoo.vespa.hosted.controller.deployment.Step.deployInitialReal;
import static com.yahoo.vespa.hosted.controller.deployment.Step.deployReal;
import static com.yahoo.vespa.hosted.controller.deployment.Step.deployTester;
import static com.yahoo.vespa.hosted.controller.deployment.Step.installInitialReal;
import static com.yahoo.vespa.hosted.controller.deployment.Step.installReal;
import static com.yahoo.vespa.hosted.controller.deployment.Step.installTester;
import static com.yahoo.vespa.hosted.controller.deployment.Step.report;
import static com.yahoo.vespa.hosted.controller.deployment.Step.startTests;
import static com.yahoo.vespa.hosted.controller.deployment.Step.endTests;
import static java.util.Comparator.comparing;

/**
 * Serialises and deserialises {@link Run} objects for persistent storage.
 *
 * @author jonmv
 */
class RunSerializer {

    private static final String stepsField = "steps";
    private static final String applicationField = "id";
    private static final String jobTypeField = "type";
    private static final String numberField = "number";
    private static final String startField = "start";
    private static final String endField = "end";
    private static final String statusField = "status";
    private static final String versionsField = "versions";
    private static final String platformVersionField = "platform";
    private static final String repositoryField = "repository";
    private static final String branchField = "branch";
    private static final String commitField = "commit";
    private static final String authorEmailField = "authorEmail";
    private static final String compileVersionField = "compileVersion";
    private static final String buildTimeField = "buildTime";
    private static final String buildField = "build";
    private static final String sourceField = "source";
    private static final String lastTestRecordField = "lastTestRecord";

    Run runFromSlime(Slime slime) {
        return runFromSlime(slime.get());
    }

    SortedMap<RunId, Run> runsFromSlime(Slime slime) {
        SortedMap<RunId, Run> runs = new TreeMap<>(comparing(RunId::number));
        Inspector runArray = slime.get();
        runArray.traverse((ArrayTraverser) (__, runObject) -> {
            Run run = runFromSlime(runObject);
            runs.put(run.id(), run);
        });
        return runs;
    }

    private Run runFromSlime(Inspector runObject) {
        EnumMap<Step, Status> steps = new EnumMap<>(Step.class);
        runObject.field(stepsField).traverse((ObjectTraverser) (step, status) -> {
            steps.put(stepOf(step), stepStatusOf(status.asString()));
        });
        return new Run(new RunId(ApplicationId.fromSerializedForm(runObject.field(applicationField).asString()),
                                 JobType.fromJobName(runObject.field(jobTypeField).asString()),
                                 runObject.field(numberField).asLong()),
                       steps,
                       versionsFromSlime(runObject.field(versionsField)),
                       Instant.ofEpochMilli(runObject.field(startField).asLong()),
                       Optional.of(runObject.field(endField))
                               .filter(Inspector::valid)
                               .map(end -> Instant.ofEpochMilli(end.asLong())),
                       runStatusOf(runObject.field(statusField).asString()),
                       runObject.field(lastTestRecordField).asLong());
    }

    private Versions versionsFromSlime(Inspector versionsObject) {
        Version targetPlatformVersion = Version.fromString(versionsObject.field(platformVersionField).asString());
        ApplicationVersion targetApplicationVersion = applicationVersionFrom(versionsObject);

        Optional<Version> sourcePlatformVersion = versionsObject.field(sourceField).valid()
                ? Optional.of(Version.fromString(versionsObject.field(sourceField).field(platformVersionField).asString()))
                : Optional.empty();
        Optional<ApplicationVersion> sourceApplicationVersion = versionsObject.field(sourceField).valid()
                ? Optional.of(applicationVersionFrom(versionsObject.field(sourceField)))
                : Optional.empty();

        return new Versions(targetPlatformVersion, targetApplicationVersion, sourcePlatformVersion, sourceApplicationVersion);
    }

    private ApplicationVersion applicationVersionFrom(Inspector versionObject) {
        if ( ! versionObject.field(buildField).valid())
            return ApplicationVersion.unknown;

        SourceRevision revision = new SourceRevision(versionObject.field(repositoryField).asString(),
                                                     versionObject.field(branchField).asString(),
                                                     versionObject.field(commitField).asString());
        long buildNumber = versionObject.field(buildField).asLong();

        if ( ! versionObject.field(authorEmailField).valid())
            return ApplicationVersion.from(revision, buildNumber);

        if ( ! versionObject.field(compileVersionField).valid() || ! versionObject.field(buildTimeField).valid())
            return ApplicationVersion.from(revision, buildNumber, versionObject.field(authorEmailField).asString());

        return ApplicationVersion.from(revision, buildNumber, versionObject.field(authorEmailField).asString(),
                                       Version.fromString(versionObject.field(compileVersionField).asString()),
                                       Instant.ofEpochMilli(versionObject.field(buildTimeField).asLong()));
    }

    Slime toSlime(Iterable<Run> runs) {
        Slime slime = new Slime();
        Cursor runArray = slime.setArray();
        runs.forEach(run -> toSlime(run, runArray.addObject()));
        return slime;
    }

    Slime toSlime(Run run) {
        Slime slime = new Slime();
        toSlime(run, slime.setObject());
        return slime;
    }

    private void toSlime(Run run, Cursor runObject) {
        runObject.setString(applicationField, run.id().application().serializedForm());
        runObject.setString(jobTypeField, run.id().type().jobName());
        runObject.setLong(numberField, run.id().number());
        runObject.setLong(startField, run.start().toEpochMilli());
        run.end().ifPresent(end -> runObject.setLong(endField, end.toEpochMilli()));
        runObject.setString(statusField, valueOf(run.status()));
        runObject.setLong(lastTestRecordField, run.lastTestLogEntry());

        Cursor stepsObject = runObject.setObject(stepsField);
        run.steps().forEach((step, status) -> stepsObject.setString(valueOf(step), valueOf(status)));

        Cursor versionsObject = runObject.setObject(versionsField);
        toSlime(run.versions().targetPlatform(), run.versions().targetApplication(), versionsObject);
        run.versions().sourcePlatform().ifPresent(sourcePlatformVersion -> {
            toSlime(sourcePlatformVersion,
                    run.versions().sourceApplication()
                       .orElseThrow(() -> new IllegalArgumentException("Source versions must be both present or absent.")),
                    versionsObject.setObject(sourceField));
        });
    }

    private void toSlime(Version platformVersion, ApplicationVersion applicationVersion, Cursor versionsObject) {
        versionsObject.setString(platformVersionField, platformVersion.toString());
        if ( ! applicationVersion.isUnknown()) {
            versionsObject.setString(repositoryField, applicationVersion.source().get().repository());
            versionsObject.setString(branchField, applicationVersion.source().get().branch());
            versionsObject.setString(commitField, applicationVersion.source().get().commit());
            versionsObject.setLong(buildField, applicationVersion.buildNumber().getAsLong());
        }
        applicationVersion.authorEmail().ifPresent(email -> versionsObject.setString(authorEmailField, email));
        applicationVersion.compileVersion().ifPresent(version -> versionsObject.setString(compileVersionField, version.toString()));
        applicationVersion.buildTime().ifPresent(time -> versionsObject.setLong(buildTimeField, time.toEpochMilli()));
    }

    static String valueOf(Step step) {
        switch (step) {
            case deployInitialReal  : return "deployInitialReal";
            case installInitialReal : return "installInitialReal";
            case deployReal         : return "deployReal";
            case installReal        : return "installReal";
            case deactivateReal     : return "deactivateReal";
            case deployTester       : return "deployTester";
            case installTester      : return "installTester";
            case deactivateTester   : return "deactivateTester";
            case copyVespaLogs      : return "copyVespaLogs";
            case startTests         : return "startTests";
            case endTests           : return "endTests";
            case report             : return "report";

            default: throw new AssertionError("No value defined for '" + step + "'!");
        }
    }

    static Step stepOf(String step) {
        switch (step) {
            case "deployInitialReal"  : return deployInitialReal;
            case "installInitialReal" : return installInitialReal;
            case "deployReal"         : return deployReal;
            case "installReal"        : return installReal;
            case "deactivateReal"     : return deactivateReal;
            case "deployTester"       : return deployTester;
            case "installTester"      : return installTester;
            case "deactivateTester"   : return deactivateTester;
            case "copyVespaLogs"      : return copyVespaLogs;
            case "startTests"         : return startTests;
            case "endTests"           : return endTests;
            case "report"             : return report;

            default: throw new IllegalArgumentException("No step defined by '" + step + "'!");
        }
    }

    static String valueOf(Status status) {
        switch (status) {
            case unfinished : return "unfinished";
            case failed     : return "failed";
            case succeeded  : return "succeeded";

            default: throw new AssertionError("No value defined for '" + status + "'!");
        }
    }

    static Status stepStatusOf(String status) {
        switch (status) {
            case "unfinished" : return unfinished;
            case "failed"     : return failed;
            case "succeeded"  : return succeeded;

            default: throw new IllegalArgumentException("No status defined by '" + status + "'!");
        }
    }

    static String valueOf(RunStatus status) {
        switch (status) {
            case running            : return "running";
            case outOfCapacity      : return "outOfCapacity";
            case deploymentFailed   : return "deploymentFailed";
            case installationFailed : return "installationFailed";
            case testFailure        : return "testFailure";
            case error              : return "error";
            case success            : return "success";
            case aborted            : return "aborted";

            default: throw new AssertionError("No value defined for '" + status + "'!");
        }
    }

    static RunStatus runStatusOf(String status) {
        switch (status) {
            case "running"            : return running;
            case "outOfCapacity"      : return outOfCapacity;
            case "deploymentFailed"   : return deploymentFailed;
            case "installationFailed" : return installationFailed;
            case "testFailure"        : return testFailure;
            case "error"              : return error;
            case "success"            : return success;
            case "aborted"            : return aborted;

            default: throw new IllegalArgumentException("No run status defined by '" + status + "'!");
        }
    }

}