/
FatBuildCompacted.java
625 lines (491 loc) · 19.1 KB
/
FatBuildCompacted.java
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ignite.ci.teamcity.ignited.fatbuild;
import com.google.common.base.MoreObjects;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.ignite.ci.analysis.IVersionedEntity;
import org.apache.ignite.ci.db.Persisted;
import org.apache.ignite.ci.tcmodel.conf.BuildType;
import org.apache.ignite.ci.tcmodel.hist.BuildRef;
import org.apache.ignite.ci.tcmodel.result.Build;
import org.apache.ignite.ci.tcmodel.result.TestOccurrencesRef;
import org.apache.ignite.ci.tcmodel.result.Triggered;
import org.apache.ignite.ci.tcmodel.result.problems.ProblemOccurrence;
import org.apache.ignite.ci.tcmodel.result.stat.Statistics;
import org.apache.ignite.ci.tcmodel.result.tests.TestOccurrenceFull;
import org.apache.ignite.ci.tcmodel.result.tests.TestOccurrencesFull;
import org.apache.ignite.ci.tcmodel.user.User;
import org.apache.ignite.ci.tcmodel.vcs.Revision;
import org.apache.ignite.ci.tcmodel.vcs.Revisions;
import org.apache.ignite.ci.tcmodel.vcs.VcsRootInstance;
import org.apache.ignite.ci.teamcity.ignited.BuildRefCompacted;
import org.apache.ignite.ci.teamcity.ignited.IStringCompactor;
import org.apache.ignite.ci.teamcity.ignited.change.RevisionCompacted;
import org.apache.ignite.ci.teamcity.ignited.runhist.Invocation;
import org.apache.ignite.ci.teamcity.ignited.runhist.InvocationData;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Composed data from {@link Build} and other classes, compressed for storage.
*/
@Persisted
public class FatBuildCompacted extends BuildRefCompacted implements IVersionedEntity {
/** Latest version. */
public static final short LATEST_VERSION = 6;
/** Latest version. */
public static final short VER_FULL_DATA_BUT_ID_CONFLICTS_POSSIBLE = 5;
/** Default branch flag offset. */
public static final int DEF_BR_F = 0;
/** Composite flag offset. */
public static final int COMPOSITE_F = 2;
/** flag offset. */
public static final int FAKE_BUILD_F = 4;
/** Failed to start flag offset. */
public static final int FAILED_TO_START_F = 6;
public static final int[] EMPTY = new int[0];
/**
* Entity fields version.
* <ul>
* <li>{@link #VER_FULL_DATA_BUT_ID_CONFLICTS_POSSIBLE} - fully supported field set, tests, problems. </li>
* <li>6 - done double check if build ID is consistent with a key. If this check passes, version is set to 6, if
* not-build is deleted.</li>
* </ul>
*/
private short _ver = LATEST_VERSION;
/** Start date. The number of milliseconds since January 1, 1970, 00:00:00 GMT */
private long startDate;
/** Finish date. The number of milliseconds since January 1, 1970, 00:00:00 GMT */
private long finishDate;
/** Finish date. The number of milliseconds since January 1, 1970, 00:00:00 GMT */
private long queuedDate;
/** Project ID, where suite is located. */
private int projectId = -1;
/** Suite Name for this builds. */
private int name = -1;
@Nullable private List<TestCompacted> tests;
@Nullable private int snapshotDeps[];
private BitSet flags = new BitSet();
@Nullable private List<ProblemCompacted> problems;
@Nullable private StatisticsCompacted statistics;
@Nullable private int changesIds[];
@Nullable private TriggeredCompacted triggered;
@Nullable private RevisionCompacted revisions[];
/** {@inheritDoc} */
@Override public int version() {
return _ver;
}
/** {@inheritDoc} */
@Override public int latestVersion() {
return LATEST_VERSION;
}
/**
* Default constructor.
*/
public FatBuildCompacted() {
}
/**
* @param compactor Compactor.
* @param build Reference.
*/
public FatBuildCompacted(IStringCompactor compactor, Build build) {
super(compactor, build);
startDate = build.getStartDate() == null ? -1L : build.getStartDate().getTime();
finishDate = build.getFinishDate() == null ? -1L : build.getFinishDate().getTime();
queuedDate = build.getQueuedDate() == null ? -1L : build.getQueuedDate().getTime();
BuildType type = build.getBuildType();
if (type != null) {
projectId = compactor.getStringId(type.getProjectId());
buildTypeName(type.getName(), compactor);
}
AtomicBoolean failedToStart = new AtomicBoolean();
failedToStart.set(build.isFailedToStart());
int[] arr = build.getSnapshotDependenciesNonNull()
.stream()
.peek(b -> {
if (failedToStart.get())
return;
if (b.hasUnknownStatus())
failedToStart.set(true);
})
.filter(b -> b.getId() != null)
.mapToInt(BuildRef::getId)
.toArray();
snapshotDependencies(arr);
setFlag(DEF_BR_F, build.defaultBranch);
setFlag(COMPOSITE_F, build.composite);
if (failedToStart.get())
setFlag(FAILED_TO_START_F, true);
if (build.isFakeStub())
setFakeStub(true);
final Triggered trigXml = build.getTriggered();
if (trigXml != null) {
triggered = new TriggeredCompacted();
triggered.type = compactor.getStringId(trigXml.getType());
final User trigXmlUser = trigXml.getUser();
if (trigXmlUser != null) {
triggered.userId = Integer.valueOf(trigXmlUser.id);
triggered.userUsername = compactor.getStringId(trigXmlUser.username);
} else {
triggered.userId = -1;
triggered.userUsername = -1;
}
final BuildRef trigBuildRef = trigXml.getBuild();
triggered.buildId = trigBuildRef != null ? trigBuildRef.getId() : -1;
}
Revisions revisions = build.getRevisions();
if(revisions!=null) {
this.revisions = revisions.revisions()
.stream()
.filter(b -> b.version() != null)
.map(revision -> new RevisionCompacted(compactor, revision))
.toArray(RevisionCompacted[]::new);
}
}
public FatBuildCompacted setFakeStub(boolean val) {
setFlag(FAKE_BUILD_F, val);
return this;
}
public void buildTypeName(String btName, IStringCompactor compactor) {
name = compactor.getStringId(btName);
}
public FatBuildCompacted snapshotDependencies(int[] arr) {
snapshotDeps = arr.length > 0 ? arr : null;
return this;
}
/**
* @param compactor Compacter.
*/
public Build toBuild(IStringCompactor compactor) {
Build res = new Build();
fillBuildRefFields(compactor, res);
fillBuildFields(compactor, res);
return res;
}
/**
* @param compactor Compactor.
* @param res Response.
*/
private void fillBuildFields(IStringCompactor compactor, Build res) {
if (startDate > 0)
res.setStartDateTs(startDate);
if (finishDate > 0)
res.setFinishDateTs(finishDate);
if (queuedDate > 0)
res.setQueuedDateTs(queuedDate);
BuildType type = new BuildType();
type.setId(res.buildTypeId());
type.setName(buildTypeName(compactor));
type.setProjectId(projectId(compactor));
res.setBuildType(type);
if (tests != null) {
TestOccurrencesRef testOccurrencesRef = new TestOccurrencesRef();
testOccurrencesRef.href = "/app/rest/latest/testOccurrences?locator=build:(id:" + id() + ")";
testOccurrencesRef.count = tests.size();
res.testOccurrences = testOccurrencesRef;
}
if (snapshotDeps != null) {
List<BuildRef> snapshotDependencies = new ArrayList<>();
for (int depId : snapshotDeps) {
BuildRef ref = new BuildRef();
ref.setId(depId);
ref.href = getHrefForId(depId);
snapshotDependencies.add(ref);
}
res.snapshotDependencies(snapshotDependencies);
}
res.defaultBranch = getFlag(DEF_BR_F);
res.composite = getFlag(COMPOSITE_F);
if (triggered != null) {
final Triggered trigXml = new Triggered();
trigXml.setType(compactor.getStringFromId(triggered.type));
trigXml.setDate(res.queuedDate);
if (triggered.userId > 0) {
final User trigUser = new User();
trigUser.id = Integer.toString(triggered.userId);
trigUser.username = compactor.getStringFromId(triggered.userUsername);
trigXml.setUser(trigUser);
}
if (triggered.buildId > 0) {
final BuildRef trigBuild = new BuildRef();
trigBuild.setId(triggered.buildId);
trigXml.setBuild(trigBuild);
}
res.setTriggered(trigXml);
}
if (revisions != null) {
res.setRevisions(Arrays.stream(revisions).map(revComp -> {
Revision revision = new Revision()
.version(revComp.commitFullVersion())
.vcsBranchName(revComp.vcsBranchName(compactor));
String vcsRootId = revComp.vcsRootId(compactor);
Integer vcsRootInstanceId = revComp.vcsRootInstanceId();
if (vcsRootId == null && vcsRootInstanceId == null)
return revision;
return revision.vcsRootInstance(
new VcsRootInstance()
.id(vcsRootInstanceId)
.vcsRootId(vcsRootId));
}).collect(Collectors.toList()));
}
}
/**
* @param compactor Compactor.
* @param page Page.
*/
public FatBuildCompacted addTests(IStringCompactor compactor, List<TestOccurrenceFull> page) {
for (TestOccurrenceFull next : page) {
TestCompacted compacted = new TestCompacted(compactor, next);
if (tests == null)
tests = new ArrayList<>();
tests.add(compacted);
}
return this;
}
/**
* @param off Offset.
* @param val Value.
*/
private void setFlag(int off, Boolean val) {
flags.clear(off, off + 2);
boolean valPresent = val != null;
flags.set(off, valPresent);
if (valPresent)
flags.set(off + 1, val);
}
/**
* @param off Offset.
*/
private Boolean getFlag(int off) {
if (!flags.get(off))
return null;
return flags.get(off + 1);
}
/**
* @param compactor Compactor.
*/
public TestOccurrencesFull getTestOcurrences(IStringCompactor compactor) {
if (tests == null)
return new TestOccurrencesFull();
List<TestOccurrenceFull> res = new ArrayList<>();
for (TestCompacted compacted : tests)
res.add(compacted.toTestOccurrence(compactor, id()));
TestOccurrencesFull testOccurrences = new TestOccurrencesFull();
testOccurrences.count = res.size();
testOccurrences.setTests(res);
return testOccurrences;
}
/** Start date. */
@Nullable public Date getStartDate() {
return getStartDateTs() > 0 ? new Date(getStartDateTs()) : null;
}
public long getStartDateTs() {
return startDate;
}
/** {@inheritDoc} */
@Override public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
if (!super.equals(o))
return false;
FatBuildCompacted that = (FatBuildCompacted)o;
return _ver == that._ver &&
startDate == that.startDate &&
finishDate == that.finishDate &&
queuedDate == that.queuedDate &&
projectId == that.projectId &&
name == that.name &&
Objects.equals(tests, that.tests) &&
Arrays.equals(snapshotDeps, that.snapshotDeps) &&
Objects.equals(flags, that.flags) &&
Objects.equals(problems, that.problems) &&
Objects.equals(statistics, that.statistics) &&
Arrays.equals(changesIds, that.changesIds) &&
Objects.equals(triggered, that.triggered) &&
Arrays.equals(revisions, that.revisions);
}
/** {@inheritDoc} */
@Override public int hashCode() {
int res = Objects.hash(super.hashCode(), _ver, startDate, finishDate, queuedDate, projectId, name, tests, flags, problems, statistics, triggered);
res = 31 * res + Arrays.hashCode(snapshotDeps);
res = 31 * res + Arrays.hashCode(changesIds);
res = 31 * res + Arrays.hashCode(revisions);
return res;
}
/**
*
*/
public boolean isComposite() {
Boolean flag = getFlag(COMPOSITE_F);
return flag != null && flag;
}
/**
*
*/
public boolean isFakeStub() {
if (getId() == null)
return true;
Boolean flag = getFlag(FAKE_BUILD_F);
return flag != null && flag;
}
/**
*
*/
public boolean isFailedToStart() {
Boolean flag = getFlag(FAILED_TO_START_F);
return flag != null && flag;
}
public Stream<TestCompacted> getFailedNotMutedTests(IStringCompactor compactor) {
if (tests == null)
return Stream.of();
return tests.stream()
.filter(t -> t.isFailedButNotMuted(compactor));
}
public Stream<String> getFailedNotMutedTestNames(IStringCompactor compactor) {
return getFailedNotMutedTests(compactor).map(t -> t.testName(compactor));
}
public Stream<TestCompacted> getAllTests() {
if (tests == null)
return Stream.of();
return tests.stream();
}
public int getTestsCount() {
return tests != null ? tests.size() : 0;
}
public Stream<String> getAllTestNames(IStringCompactor compactor) {
return getAllTests().map(t -> t.testName(compactor));
}
public String buildTypeName(IStringCompactor compactor) {
return compactor.getStringFromId(name);
}
public String projectId(IStringCompactor compactor) {
return compactor.getStringFromId(projectId);
}
public List<ProblemOccurrence> problems(IStringCompactor compactor) {
if (this.problems == null)
return Collections.emptyList();
return this.problems.stream()
.map(pc -> pc.toProblemOccurrence(compactor, id()))
.collect(Collectors.toList());
}
public List<ProblemCompacted> problems() {
if (this.problems == null)
return Collections.emptyList();
return Collections.unmodifiableList(this.problems);
}
public void addProblems(IStringCompactor compactor,
@NotNull List<ProblemOccurrence> occurrences) {
if (occurrences.isEmpty())
return;
if (this.problems == null)
this.problems = new ArrayList<>();
occurrences.stream()
.map(p -> new ProblemCompacted(compactor, p))
.forEach(this.problems::add);
}
public Long buildDuration(IStringCompactor compactor) {
return statistics == null ? null : statistics.buildDuration(compactor);
}
public void statistics(IStringCompactor compactor, Statistics statistics) {
this.statistics = new StatisticsCompacted(compactor, statistics);
}
/**
* @param changes Changes.
*/
public FatBuildCompacted changes(int[] changes) {
this.changesIds = changes.clone();
return this;
}
public int[] changes() {
if (changesIds == null)
return EMPTY;
return changesIds;
}
public int[] snapshotDependencies() {
if (snapshotDeps == null)
return EMPTY;
return snapshotDeps.clone();
}
/** {@inheritDoc} */
@Override public String toString() {
return MoreObjects.toStringHelper(this)
.add("_", super.toString())
.add("_ver", _ver)
.add("startDate", startDate)
.add("finishDate", finishDate)
.add("queuedDate", queuedDate)
.add("projectId", projectId)
.add("name", name)
.add("tests", tests)
.add("snapshotDeps", snapshotDeps)
.add("flags", flags)
.add("problems", problems)
.add("statistics", statistics)
.add("changesIds", changesIds)
.add("triggered", triggered)
.add("revisions", revisions)
.toString();
}
public Invocation toInvocation(IStringCompactor compactor) {
boolean success = isSuccess(compactor);
final int failCode ;
if (success)
failCode = InvocationData.OK;
else {
if (problems()
.stream().anyMatch(occurrence ->
occurrence.isExecutionTimeout(compactor)
|| occurrence.isJvmCrash(compactor)
|| occurrence.isBuildFailureOnMetric(compactor)
|| occurrence.isCompilationError(compactor)))
failCode = InvocationData.CRITICAL_FAILURE;
else
failCode = InvocationData.FAILURE;
}
return new Invocation(getId())
.withStatus((byte)failCode)
.withStartDate(getStartDateTs())
.withChanges(changes());
}
public void setVersion(short ver) {
this._ver = ver;
}
public FatBuildCompacted setCancelled(IStringCompactor compactor) {
status(compactor.getStringId(BuildRef.STATUS_UNKNOWN));
state(compactor.getStringId(BuildRef.STATE_FINISHED));
return this;
}
/**
* @return revisions list or null if revisions was not reported by TC/for older build versions in DB.
*/
@Nullable
public List<RevisionCompacted> revisions() {
if (revisions == null)
return null;
return Collections.unmodifiableList(Arrays.asList(revisions));
}
}