-
Notifications
You must be signed in to change notification settings - Fork 6
/
HomeResultsReader.java
711 lines (608 loc) · 22.7 KB
/
HomeResultsReader.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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
package tfb.status.service;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Comparator.comparing;
import static java.util.Comparator.reverseOrder;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.common.base.Joiner;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.text.NumberFormat;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tfb.status.util.ZipFiles;
import tfb.status.view.HomePageView.ResultsGitView;
import tfb.status.view.HomePageView.ResultsJsonView;
import tfb.status.view.HomePageView.ResultsView;
import tfb.status.view.HomePageView.ResultsZipView;
import tfb.status.view.HomePageView.ResultsZipView.Failure;
import tfb.status.view.Results;
/**
* Loads previously-uploaded results for display on the home page.
*/
@Singleton
public final class HomeResultsReader {
private final FileStore fileStore;
private final ObjectMapper objectMapper;
private final Clock clock;
private final Logger logger = LoggerFactory.getLogger(getClass());
private final LoadingCache<ViewCacheKey, ResultsJsonView> jsonCache =
Caffeine.newBuilder()
.maximumSize(VIEW_CACHE_MAX_SIZE)
.build(key -> viewJsonFile(key.file));
private final LoadingCache<ViewCacheKey, ResultsZipView> zipCache =
Caffeine.newBuilder()
.maximumSize(VIEW_CACHE_MAX_SIZE)
.build(key -> viewZipFile(key.file));
// This number should be greater than the total number of results files we'll
// ever have on disk at once.
private static final int VIEW_CACHE_MAX_SIZE = 10_000;
@GuardedBy("this") @Nullable private ScheduledThreadPoolExecutor purgeScheduler;
@GuardedBy("this") @Nullable private ScheduledFuture<?> purgeTask;
@Inject
public HomeResultsReader(FileStore fileStore,
ObjectMapper objectMapper,
Clock clock) {
this.fileStore = Objects.requireNonNull(fileStore);
this.objectMapper = Objects.requireNonNull(objectMapper);
this.clock = Objects.requireNonNull(clock);
}
/**
* Initializes resources used by this service.
*/
@PostConstruct
public synchronized void start() {
purgeScheduler = new ScheduledThreadPoolExecutor(1);
purgeScheduler.setRemoveOnCancelPolicy(true);
purgeScheduler.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
purgeScheduler.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
purgeTask =
purgeScheduler.scheduleWithFixedDelay(
/* command= */ () -> {
try {
purgeUnreachableCacheKeys();
} catch (RuntimeException e) {
// An uncaught exception would de-schedule this task.
logger.error("Error purging unreachable cache keys", e);
}
},
/* initialDelay= */ 1,
/* delay= */ 1,
/* unit= */ TimeUnit.HOURS);
}
/**
* Cleans up resources used by this service.
*/
@PreDestroy
public synchronized void stop() {
ScheduledFuture<?> task = this.purgeTask;
if (task != null) {
task.cancel(false);
this.purgeTask = null;
}
ScheduledThreadPoolExecutor scheduler = this.purgeScheduler;
if (scheduler != null) {
scheduler.shutdownNow();
this.purgeScheduler = null;
}
}
/**
* Returns a view of all the previously-uploaded results, suitable for
* rendering on the home page.
*
* @return a view of the results
* @throws IOException if an I/O error occurs while reading the results
*/
public ImmutableList<ResultsView> results() throws IOException {
var jsonByUuid = new HashMap<String, ResultsJsonView>();
var jsonWithoutUuid = new ArrayList<ResultsJsonView>();
viewAllJsonFiles().forEach(
(ResultsJsonView view) -> {
if (view.uuid == null)
jsonWithoutUuid.add(view);
else
jsonByUuid.merge(
view.uuid,
view,
(v1, v2) -> {
logger.warn(
"Ignoring results.json file {}, which has the same "
+ "uuid ({}) as another results.json file.",
view.fileName, view.uuid);
return v1;
});
});
var zipByUuid = new HashMap<String, ResultsZipView>();
var zipWithoutUuid = new ArrayList<ResultsZipView>();
viewAllZipFiles().forEach(
(ResultsZipView view) -> {
if (view.uuid == null)
zipWithoutUuid.add(view);
else
zipByUuid.merge(
view.uuid,
view,
(v1, v2) -> {
logger.warn(
"Ignoring results.zip file {}, which has the same "
+ "uuid ({}) as another results.zip file.",
view.fileName, view.uuid);
return v1;
});
});
var results = new ArrayList<ResultsView>();
Set<String> uuids = Sets.union(jsonByUuid.keySet(),
zipByUuid.keySet());
for (String uuid : uuids) {
ResultsJsonView json = jsonByUuid.get(uuid);
ResultsZipView zip = zipByUuid.get(uuid);
results.add(new ResultsView(json, zip));
}
for (ResultsJsonView json : jsonWithoutUuid)
results.add(
new ResultsView(
/* json= */ json,
/* zip= */ null));
for (ResultsZipView zip : zipWithoutUuid)
results.add(
new ResultsView(
/* json= */ null,
/* zip= */ zip));
return ImmutableList.sortedCopyOf(RESULTS_COMPARATOR, results);
}
/**
* Returns a view of the only previously-uploaded results having the given
* UUID, suitable for rendering on the home page, or {@code null} if there are
* no results with the given UUID.
*
* @param uuid the UUID of the results to be viewed
* @return a view of the results, or {@code null} if there are no matching
* results
* @throws IOException if an I/O error occurs while reading the results
*/
@Nullable
public ResultsView resultsByUuid(String uuid) throws IOException {
Objects.requireNonNull(uuid);
ResultsJsonView json =
viewAllJsonFiles()
.filter(view -> uuid.equals(view.uuid))
.findAny()
.orElse(null);
ResultsZipView zip =
viewAllZipFiles()
.filter(view -> uuid.equals(view.uuid))
.findAny()
.orElse(null);
return (json == null && zip == null)
? null
: new ResultsView(json, zip);
}
private Stream<ResultsJsonView> viewAllJsonFiles() throws IOException {
Stream.Builder<ViewCacheKey> keys = Stream.builder();
try (DirectoryStream<Path> jsonFiles =
Files.newDirectoryStream(fileStore.resultsDirectory(), "*.json")) {
for (Path file : jsonFiles)
keys.add(new ViewCacheKey(file));
}
return keys.build()
.map(key -> jsonCache.get(key))
.filter(view -> view != null);
}
private Stream<ResultsZipView> viewAllZipFiles() throws IOException {
Stream.Builder<ViewCacheKey> keys = Stream.builder();
try (DirectoryStream<Path> zipFiles =
Files.newDirectoryStream(fileStore.resultsDirectory(), "*.zip")) {
for (Path file : zipFiles)
keys.add(new ViewCacheKey(file));
}
return keys.build()
.map(key -> zipCache.get(key))
.filter(view -> view != null);
}
@Nullable
private ResultsJsonView viewJsonFile(Path jsonFile) {
Objects.requireNonNull(jsonFile);
Results results;
try (InputStream inputStream = Files.newInputStream(jsonFile)) {
results = objectMapper.readValue(inputStream, Results.class);
} catch (IOException e) {
logger.warn("Exception reading json file {}", jsonFile, e);
return null;
}
String uuid = results.uuid;
String name = results.name;
String environmentDescription = results.environmentDescription;
// The "completed" map in the results includes frameworks that won't show up
// in the "succeeded" or "failed" maps because they had an error before they
// could execute any of the test types. For example, this will happen when
// a rogue process holds onto a common port like 8080 and prevents a lot of
// frameworks from starting up.
int frameworksWithCleanSetup = 0;
int frameworksWithSetupProblems = 0;
for (String message : results.completed.values()) {
if (isCompletedTimestamp(message))
frameworksWithCleanSetup++;
else
frameworksWithSetupProblems++;
}
int completedFrameworks =
frameworksWithCleanSetup + frameworksWithSetupProblems;
int totalFrameworks = results.frameworks.size();
int successfulTests = results.succeeded.values().size();
int failedTests = results.failed.values().size();
for (String testType : Results.TEST_TYPES) {
for (String framework : results.frameworks) {
if (results.succeeded.containsEntry(testType, framework)
&& results.requests(testType, framework) == 0) {
successfulTests--;
failedTests++;
}
}
}
LocalDateTime startTime =
(results.startTime == null)
? null
: epochMillisToDateTime(results.startTime, clock.getZone());
LocalDateTime completionTime =
(results.completionTime == null)
? null
: epochMillisToDateTime(results.completionTime, clock.getZone());
Duration elapsedDuration;
Duration estimatedRemainingDuration;
if (startTime == null)
elapsedDuration = null;
else {
LocalDateTime endTime =
(completionTime == null)
? LocalDateTime.now(clock)
: completionTime;
elapsedDuration = Duration.between(startTime, endTime);
}
if (completionTime != null
|| startTime == null
|| elapsedDuration == null
|| completedFrameworks == 0)
estimatedRemainingDuration = null;
else
estimatedRemainingDuration =
elapsedDuration.multipliedBy(totalFrameworks)
.dividedBy(completedFrameworks)
.minus(elapsedDuration);
//
// TODO: Avoid using the last modified time of the file on disk, which may
// change for reasons completely unrelated to the run itself, and use
// something from the results.json file to give us a last modified
// time instead. The datetime strings in the "completed" object are
// no good because they are local to the TFB server, and we don't know
// that server's time zone.
//
FileTime lastUpdatedTime;
try {
lastUpdatedTime = Files.getLastModifiedTime(jsonFile);
} catch (IOException e) {
logger.warn("Exception reading last modified time of file {}", jsonFile, e);
return null;
}
DateTimeFormatter displayedTimeFormatter =
DateTimeFormatter.ofPattern(
"yyyy-MM-dd 'at' h:mm a",
Locale.ROOT);
String startTimeString =
(startTime == null)
? null
: displayedTimeFormatter.format(startTime);
String completionTimeString =
(completionTime == null)
? null
: displayedTimeFormatter.format(completionTime);
String lastUpdatedString =
lastUpdatedTime.toInstant()
.atZone(clock.getZone())
.toLocalDateTime()
.format(displayedTimeFormatter);
String elapsedDurationString =
(elapsedDuration == null)
? null
: formatDuration(elapsedDuration);
String estimatedRemainingDurationString =
(estimatedRemainingDuration == null)
? null
: formatDuration(estimatedRemainingDuration);
Path relativePath = fileStore.resultsDirectory().relativize(jsonFile);
String fileName = Joiner.on('/').join(relativePath);
ResultsGitView git;
if (results.git == null) {
git = null;
} else {
git = new ResultsGitView(
/* commitId= */ results.git.commitId,
/* repositoryUrl= */ results.git.repositoryUrl,
/* branchName= */ results.git.branchName);
}
return new ResultsJsonView(
/* uuid= */ uuid,
/* git= */ git,
/* fileName= */ fileName,
/* name= */ name,
/* environmentDescription= */ environmentDescription,
/* startTime= */ startTimeString,
/* completionTime= */ completionTimeString,
/* completedFrameworks= */ completedFrameworks,
/* frameworksWithCleanSetup= */ frameworksWithCleanSetup,
/* frameworksWithSetupProblems= */ frameworksWithSetupProblems,
/* totalFrameworks= */ totalFrameworks,
/* successfulTests= */ successfulTests,
/* failedTests= */ failedTests,
/* lastUpdated= */ lastUpdatedString,
/* elapsedDuration= */ elapsedDurationString,
/* estimatedRemainingDuration= */ estimatedRemainingDurationString);
}
@Nullable
private ResultsZipView viewZipFile(Path zipFile) {
Objects.requireNonNull(zipFile);
Results results;
try {
results =
ZipFiles.readZipEntry(
/* zipFile= */ zipFile,
/* entryPath= */ "results.json",
/* entryReader= */ inputStream ->
objectMapper.readValue(inputStream,
Results.class));
} catch (IOException e) {
logger.warn("Exception reading zip file {}", zipFile, e);
return null;
}
//
// If the zip doesn't contain a results.json at all, then we have nothing
// useful to say to users about it, and we want to pretend it doesn't exist.
//
if (results == null)
return null;
String uuid = results.uuid;
Path relativePath = fileStore.resultsDirectory().relativize(zipFile);
String fileName = Joiner.on('/').join(relativePath);
var failures = new ArrayList<Failure>();
SetMultimap<String, String> frameworkToFailedTestTypes =
HashMultimap.create(results.failed.inverse());
for (String testType : Results.TEST_TYPES) {
for (String framework : results.frameworks) {
if (results.succeeded.containsEntry(testType, framework)
&& results.requests(testType, framework) == 0) {
frameworkToFailedTestTypes.put(framework, testType);
}
}
}
var frameworksWithSetupIssues = new HashSet<String>();
results.completed.forEach(
(String framework, String message) -> {
if (!isCompletedTimestamp(message)) {
frameworksWithSetupIssues.add(framework);
}
});
for (String framework : Sets.union(frameworkToFailedTestTypes.keySet(),
frameworksWithSetupIssues)) {
Set<String> failedTestTypes = frameworkToFailedTestTypes.get(framework);
boolean hadSetupProblems = frameworksWithSetupIssues.contains(framework);
failures.add(
new Failure(
/* framework= */ framework,
/* failedTestTypes= */ ImmutableList.sortedCopyOf(failedTestTypes),
/* hadSetupProblems= */ hadSetupProblems));
}
failures.sort(comparing(failure -> failure.framework,
String.CASE_INSENSITIVE_ORDER));
ResultsGitView git;
if (results.git == null) {
// We used to collect the git commit id as a separate "commit_id.txt" file.
String gitCommitId;
try {
gitCommitId =
ZipFiles.readZipEntry(
/* zipFile= */ zipFile,
/* entryPath= */ "commit_id.txt",
/* entryReader= */
inputStream -> {
try (var isr = new InputStreamReader(inputStream, UTF_8);
var br = new BufferedReader(isr)) {
return br.readLine();
}
});
} catch (IOException e) {
logger.warn(
"Exception reading git commit id from zip file {}",
zipFile, e);
gitCommitId = null;
}
if (gitCommitId == null) {
git = null;
} else {
git = new ResultsGitView(
/* commitId= */ gitCommitId,
/* repositoryUrl= */ null,
/* branchName= */ null);
}
} else {
git = new ResultsGitView(
/* commitId= */ results.git.commitId,
/* repositoryUrl= */ results.git.repositoryUrl,
/* branchName= */ results.git.branchName);
}
return new ResultsZipView(
/* uuid= */ uuid,
/* git= */ git,
/* fileName= */ fileName,
/* failures= */ ImmutableList.copyOf(failures));
}
/**
* Trims the internal cache, removing entries that are "dead" because they
* have {@linkplain ViewCacheKey#isUnreachable() unreachable} keys.
*/
private void purgeUnreachableCacheKeys() {
purgeUnreachableCacheKeys(jsonCache);
purgeUnreachableCacheKeys(zipCache);
}
private static void purgeUnreachableCacheKeys(Cache<ViewCacheKey, ?> cache) {
ImmutableSet<ViewCacheKey> unreachableKeys =
cache.asMap()
.keySet()
.stream()
.filter(key -> key.isUnreachable())
.collect(toImmutableSet());
cache.invalidateAll(unreachableKeys);
}
@Immutable
private static final class ViewCacheKey {
final Path file;
// When the file is modified, this cache key becomes unreachable.
final FileTime lastModifiedTime;
ViewCacheKey(Path file) throws IOException {
this.file = Objects.requireNonNull(file);
this.lastModifiedTime = Files.getLastModifiedTime(file);
}
@Override
public boolean equals(@Nullable Object object) {
if (object == this)
return true;
if (!(object instanceof ViewCacheKey))
return false;
var that = (ViewCacheKey) object;
return this.file.equals(that.file)
&& this.lastModifiedTime.equals(that.lastModifiedTime);
}
@Override
public int hashCode() {
return file.hashCode() ^ lastModifiedTime.hashCode();
}
/**
* Returns {@code true} if the file has been modified since this cache key
* was created, meaning this cache key is effectively unreachable.
*/
boolean isUnreachable() {
try {
return !lastModifiedTime.equals(Files.getLastModifiedTime(file));
} catch (IOException ignored) {
//
// This would happen if the file is deleted, for example.
//
// Since an exception here implies that constructing a new key for this
// file would also throw an exception, this key is unreachable.
//
return true;
}
}
}
private static LocalDateTime epochMillisToDateTime(long epochMillis,
ZoneId zone) {
Objects.requireNonNull(zone);
Instant instant = Instant.ofEpochMilli(epochMillis);
return LocalDateTime.ofInstant(instant, zone);
}
private static String formatDuration(Duration duration) {
long seconds = duration.toSeconds();
long minutes = seconds / 60;
long hours = minutes / 60;
seconds %= 60;
minutes %= 60;
if (minutes >= 30)
hours++;
if (seconds >= 30)
minutes++;
if (hours > 0)
return "~"
+ NumberFormat.getIntegerInstance(Locale.ROOT).format(hours)
+ " hour"
+ ((hours == 1) ? "" : "s");
if (minutes > 0)
return "~"
+ NumberFormat.getIntegerInstance(Locale.ROOT).format(minutes)
+ " minute"
+ ((minutes == 1) ? "" : "s");
return "< 1 minute";
}
/**
* {@code true} if the message looks like a timestamp in the {@link
* Results#completed} map.
*
* @param message a value from the {@link Results#completed} map
* @return {@code true} if the value is a timestamp, indicating that the
* framework started and stopped correctly, or {@code false} if the
* message is an error message, indicating that the framework did not
* start or stop correctly
*/
private static boolean isCompletedTimestamp(String message) {
Objects.requireNonNull(message);
try {
LocalDateTime.parse(message, COMPLETED_TIMESTAMP_FORMATTER);
return true;
} catch (DateTimeParseException ignored) {
return false;
}
}
private static final DateTimeFormatter COMPLETED_TIMESTAMP_FORMATTER =
DateTimeFormatter.ofPattern("yyyyMMddHHmmss", Locale.ROOT);
/**
* The ordering of results displayed on the home page.
*/
//
// In practice, the results files are named like this:
//
// results.{uploaded_at_date_time}.{json|zip}
//
// where {uploaded_at_date_time} is in the format "yyyy-MM-dd-HH-mm-ss-SSS".
//
// Therefore, sorting by file name effectively sorts the results by when they
// were uploaded, and this comparator puts the most recently uploaded results
// first.
//
private static final Comparator<ResultsView> RESULTS_COMPARATOR =
comparing(
results -> {
if (results.json != null)
return results.json.fileName;
else if (results.zip != null)
return results.zip.fileName;
else
return "";
},
reverseOrder());
}