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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
|
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed 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 com.google.devtools.build.skyframe;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.devtools.build.skyframe.NodeEntrySubjectFactory.assertThatNodeEntry;
import static org.junit.Assert.fail;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.events.ExtendedEventHandler.Postable;
import com.google.devtools.build.lib.util.GroupedList;
import com.google.devtools.build.lib.util.GroupedList.GroupedListHelper;
import com.google.devtools.build.skyframe.NodeEntry.DependencyState;
import com.google.devtools.build.skyframe.SkyFunctionException.ReifiedSkyFunctionException;
import com.google.devtools.build.skyframe.SkyFunctionException.Transience;
import com.google.devtools.build.skyframe.ThinNodeEntry.MarkedDirtyResult;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Tests for {@link InMemoryNodeEntry}.
*/
@RunWith(JUnit4.class)
public class InMemoryNodeEntryTest {
private static final NestedSet<TaggedEvents> NO_EVENTS =
NestedSetBuilder.<TaggedEvents>emptySet(Order.STABLE_ORDER);
private static final NestedSet<Postable> NO_POSTS =
NestedSetBuilder.<Postable>emptySet(Order.STABLE_ORDER);
private static SkyKey key(String name) {
return GraphTester.toSkyKey(name);
}
@Test
public void createEntry() {
InMemoryNodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
assertThat(entry.isDone()).isFalse();
assertThat(entry.isReady()).isTrue();
assertThat(entry.isDirty()).isFalse();
assertThat(entry.isChanged()).isFalse();
assertThat(entry.getTemporaryDirectDeps()).isEmpty();
}
@Test
public void signalEntry() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
SkyKey dep1 = key("dep1");
addTemporaryDirectDep(entry, dep1);
assertThat(entry.isReady()).isFalse();
assertThat(entry.signalDep()).isTrue();
assertThat(entry.isReady()).isTrue();
assertThatNodeEntry(entry).hasTemporaryDirectDepsThat().containsExactly(dep1);
SkyKey dep2 = key("dep2");
SkyKey dep3 = key("dep3");
addTemporaryDirectDep(entry, dep2);
addTemporaryDirectDep(entry, dep3);
assertThat(entry.isReady()).isFalse();
assertThat(entry.signalDep()).isFalse();
assertThat(entry.isReady()).isFalse();
assertThat(entry.signalDep()).isTrue();
assertThat(entry.isReady()).isTrue();
assertThat(setValue(entry, new SkyValue() {},
/*errorInfo=*/null, /*graphVersion=*/0L)).isEmpty();
assertThat(entry.isDone()).isTrue();
assertThat(entry.getVersion()).isEqualTo(IntVersion.of(0L));
assertThat(entry.getDirectDeps()).containsExactly(dep1, dep2, dep3);
}
@Test
public void reverseDeps() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
SkyKey mother = key("mother");
SkyKey father = key("father");
assertThat(entry.addReverseDepAndCheckIfDone(mother))
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
assertThat(entry.addReverseDepAndCheckIfDone(null))
.isEqualTo(DependencyState.ALREADY_EVALUATING);
assertThat(entry.addReverseDepAndCheckIfDone(father))
.isEqualTo(DependencyState.ALREADY_EVALUATING);
assertThat(setValue(entry, new SkyValue() {},
/*errorInfo=*/null, /*graphVersion=*/0L)).containsExactly(mother, father);
assertThat(entry.getReverseDepsForDoneEntry()).containsExactly(mother, father);
assertThat(entry.isDone()).isTrue();
entry.removeReverseDep(mother);
assertThat(entry.getReverseDepsForDoneEntry()).doesNotContain(mother);
}
@Test
public void errorValue() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
ReifiedSkyFunctionException exception = new ReifiedSkyFunctionException(
new GenericFunctionException(new SomeErrorException("oops"), Transience.PERSISTENT),
key("cause"));
ErrorInfo errorInfo = ErrorInfo.fromException(exception, false);
assertThat(setValue(entry, /*value=*/null, errorInfo, /*graphVersion=*/0L)).isEmpty();
assertThat(entry.isDone()).isTrue();
assertThat(entry.getValue()).isNull();
assertThat(entry.getErrorInfo()).isEqualTo(errorInfo);
}
@Test
public void errorAndValue() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
ReifiedSkyFunctionException exception = new ReifiedSkyFunctionException(
new GenericFunctionException(new SomeErrorException("oops"), Transience.PERSISTENT),
key("cause"));
ErrorInfo errorInfo = ErrorInfo.fromException(exception, false);
setValue(entry, new SkyValue() {}, errorInfo, /*graphVersion=*/0L);
assertThat(entry.isDone()).isTrue();
assertThat(entry.getErrorInfo()).isEqualTo(errorInfo);
}
@Test
public void crashOnNullErrorAndValue() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
try {
setValue(entry, /*value=*/null, /*errorInfo=*/null, /*graphVersion=*/0L);
fail();
} catch (IllegalStateException e) {
// Expected.
}
}
@Test
public void crashOnTooManySignals() {
InMemoryNodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
try {
entry.signalDep();
fail();
} catch (IllegalStateException e) {
// Expected.
}
}
@Test
public void crashOnDifferentValue() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/0L);
try {
// Value() {} and Value() {} are not .equals().
setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/1L);
fail();
} catch (IllegalStateException e) {
// Expected.
}
}
@Test
public void dirtyLifecycle() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
SkyKey dep = key("dep");
addTemporaryDirectDep(entry, dep);
entry.signalDep();
setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/0L);
assertThat(entry.isDirty()).isFalse();
assertThat(entry.isDone()).isTrue();
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/false);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isFalse();
assertThat(entry.isDone()).isFalse();
assertThatNodeEntry(entry)
.addReverseDepAndCheckIfDone(null)
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
assertThat(entry.isReady()).isTrue();
assertThat(entry.getTemporaryDirectDeps()).isEmpty();
SkyKey parent = key("parent");
entry.addReverseDepAndCheckIfDone(parent);
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep);
addTemporaryDirectDep(entry, dep);
entry.signalDep();
assertThatNodeEntry(entry).hasTemporaryDirectDepsThat().containsExactly(dep);
assertThat(entry.isReady()).isTrue();
entry.markRebuilding();
assertThat(setValue(entry, new SkyValue() {}, /*errorInfo=*/null,
/*graphVersion=*/1L)).containsExactly(parent);
}
@Test
public void changedLifecycle() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
SkyKey dep = key("dep");
addTemporaryDirectDep(entry, dep);
entry.signalDep();
setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/0L);
assertThat(entry.isDirty()).isFalse();
assertThat(entry.isDone()).isTrue();
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/true);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isTrue();
assertThat(entry.isDone()).isFalse();
assertThatNodeEntry(entry)
.addReverseDepAndCheckIfDone(null)
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
assertThat(entry.isReady()).isTrue();
SkyKey parent = key("parent");
entry.addReverseDepAndCheckIfDone(parent);
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.NEEDS_REBUILDING);
assertThat(entry.isReady()).isTrue();
assertThat(entry.getTemporaryDirectDeps()).isEmpty();
entry.markRebuilding();
assertThat(setValue(entry, new SkyValue() {}, /*errorInfo=*/null,
/*graphVersion=*/1L)).containsExactly(parent);
assertThat(entry.getVersion()).isEqualTo(IntVersion.of(1L));
}
@Test
public void markDirtyThenChanged() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
addTemporaryDirectDep(entry, key("dep"));
entry.signalDep();
setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/0L);
assertThat(entry.isDirty()).isFalse();
assertThat(entry.isDone()).isTrue();
MarkedDirtyResult firstResult = entry.markDirty(/*isChanged=*/false);
assertThat(firstResult.wasCallRedundant()).isFalse();
assertThat(firstResult.wasClean()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isFalse();
assertThat(entry.isDone()).isFalse();
MarkedDirtyResult secondResult = entry.markDirty(/*isChanged=*/true);
assertThat(secondResult.wasCallRedundant()).isFalse();
assertThat(secondResult.wasClean()).isFalse();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isTrue();
assertThat(entry.isDone()).isFalse();
assertThatNodeEntry(entry)
.addReverseDepAndCheckIfDone(null)
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
assertThat(entry.isReady()).isTrue();
}
@Test
public void markChangedThenDirty() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
addTemporaryDirectDep(entry, key("dep"));
entry.signalDep();
setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/0L);
assertThat(entry.isDirty()).isFalse();
assertThat(entry.isDone()).isTrue();
MarkedDirtyResult firstResult = entry.markDirty(/*isChanged=*/true);
assertThat(firstResult.wasCallRedundant()).isFalse();
assertThat(firstResult.wasClean()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isTrue();
assertThat(entry.isDone()).isFalse();
MarkedDirtyResult secondResult = entry.markDirty(/*isChanged=*/false);
assertThat(secondResult.wasCallRedundant()).isFalse();
assertThat(secondResult.wasClean()).isFalse();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isTrue();
assertThat(entry.isDone()).isFalse();
assertThatNodeEntry(entry)
.addReverseDepAndCheckIfDone(null)
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
assertThat(entry.isReady()).isTrue();
}
@Test
public void markChangedTwice() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
setValue(entry, new SkyValue() {}, /*errorInfo=*/ null, /*graphVersion=*/ 0L);
assertThat(entry.isDirty()).isFalse();
assertThat(entry.isChanged()).isFalse();
assertThat(entry.isDone()).isTrue();
MarkedDirtyResult firstResult = entry.markDirty(/*isChanged=*/ true);
assertThat(firstResult.getReverseDepsUnsafeIfWasClean()).isNotNull();
assertThat(firstResult.wasCallRedundant()).isFalse();
assertThat(firstResult.wasClean()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isTrue();
assertThat(entry.isDone()).isFalse();
MarkedDirtyResult secondResult = entry.markDirty(/*isChanged=*/ true);
assertThat(secondResult.wasClean()).isFalse();
assertThat(secondResult.wasCallRedundant()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isTrue();
assertThat(entry.isDone()).isFalse();
}
@Test
public void markDirtyTwice() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
// To successfully mark a node dirty (but not changed), that node must have at least one
// dependency. The node sanity-checks its deps while being marked dirty.
addTemporaryDirectDep(entry, key("dep"));
entry.signalDep();
setValue(entry, new SkyValue() {}, /*errorInfo=*/ null, /*graphVersion=*/ 0L);
assertThat(entry.isDirty()).isFalse();
assertThat(entry.isChanged()).isFalse();
assertThat(entry.isDone()).isTrue();
MarkedDirtyResult firstResult = entry.markDirty(/*isChanged=*/ false);
assertThat(firstResult.getReverseDepsUnsafeIfWasClean()).isNotNull();
assertThat(firstResult.wasCallRedundant()).isFalse();
assertThat(firstResult.wasClean()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isFalse();
assertThat(entry.isDone()).isFalse();
MarkedDirtyResult secondResult = entry.markDirty(/*isChanged=*/ false);
assertThat(secondResult.wasClean()).isFalse();
assertThat(secondResult.wasCallRedundant()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isFalse();
assertThat(entry.isDone()).isFalse();
}
@Test
public void crashOnAddReverseDepTwice() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
SkyKey parent = key("parent");
assertThat(entry.addReverseDepAndCheckIfDone(parent))
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
try {
entry.addReverseDepAndCheckIfDone(parent);
assertThat(setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/0L))
.containsExactly(parent);
fail("Cannot add same dep twice");
} catch (IllegalStateException e) {
assertThat(e).hasMessageThat().contains("Duplicate reverse deps");
}
}
@Test
public void crashOnAddReverseDepTwiceAfterDone() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/0L);
SkyKey parent = key("parent");
assertThat(entry.addReverseDepAndCheckIfDone(parent)).isEqualTo(DependencyState.DONE);
try {
entry.addReverseDepAndCheckIfDone(parent);
// We only check for duplicates when we request all the reverse deps.
entry.getReverseDepsForDoneEntry();
fail("Cannot add same dep twice");
} catch (IllegalStateException e) {
// Expected.
}
}
@Test
public void crashOnAddReverseDepBeforeAfterDone() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
SkyKey parent = key("parent");
assertThat(entry.addReverseDepAndCheckIfDone(parent))
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/0L);
try {
entry.addReverseDepAndCheckIfDone(parent);
// We only check for duplicates when we request all the reverse deps.
entry.getReverseDepsForDoneEntry();
fail("Cannot add same dep twice");
} catch (IllegalStateException e) {
// Expected.
}
}
@Test
public void pruneBeforeBuild() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
SkyKey dep = key("dep");
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
addTemporaryDirectDep(entry, dep);
entry.signalDep();
setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/0L);
assertThat(entry.isDirty()).isFalse();
assertThat(entry.isDone()).isTrue();
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/false);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isFalse();
assertThat(entry.isDone()).isFalse();
assertThatNodeEntry(entry)
.addReverseDepAndCheckIfDone(null)
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
assertThat(entry.isReady()).isTrue();
SkyKey parent = key("parent");
entry.addReverseDepAndCheckIfDone(parent);
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep);
addTemporaryDirectDep(entry, dep);
entry.signalDep(IntVersion.of(0L));
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.VERIFIED_CLEAN);
assertThat(entry.markClean()).containsExactly(parent);
assertThat(entry.isDone()).isTrue();
assertThat(entry.getVersion()).isEqualTo(IntVersion.of(0L));
}
private static class IntegerValue implements SkyValue {
private final int value;
IntegerValue(int value) {
this.value = value;
}
@Override
public boolean equals(Object that) {
return (that instanceof IntegerValue) && (((IntegerValue) that).value == value);
}
@Override
public int hashCode() {
return value;
}
}
@Test
public void pruneAfterBuild() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
SkyKey dep = key("dep");
addTemporaryDirectDep(entry, dep);
entry.signalDep();
setValue(entry, new IntegerValue(5), /*errorInfo=*/null, /*graphVersion=*/0L);
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/false);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep);
addTemporaryDirectDep(entry, dep);
entry.signalDep(IntVersion.of(1L));
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.NEEDS_REBUILDING);
assertThatNodeEntry(entry).hasTemporaryDirectDepsThat().containsExactly(dep);
entry.markRebuilding();
setValue(entry, new IntegerValue(5), /*errorInfo=*/null, /*graphVersion=*/1L);
assertThat(entry.isDone()).isTrue();
assertThat(entry.getVersion()).isEqualTo(IntVersion.of(0L));
}
@Test
public void noPruneWhenDetailsChange() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
SkyKey dep = key("dep");
addTemporaryDirectDep(entry, dep);
entry.signalDep();
setValue(entry, new IntegerValue(5), /*errorInfo=*/null, /*graphVersion=*/0L);
assertThat(entry.isDirty()).isFalse();
assertThat(entry.isDone()).isTrue();
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/false);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isFalse();
assertThat(entry.isDone()).isFalse();
assertThatNodeEntry(entry)
.addReverseDepAndCheckIfDone(null)
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
assertThat(entry.isReady()).isTrue();
SkyKey parent = key("parent");
entry.addReverseDepAndCheckIfDone(parent);
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep);
addTemporaryDirectDep(entry, dep);
entry.signalDep(IntVersion.of(1L));
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.NEEDS_REBUILDING);
assertThatNodeEntry(entry).hasTemporaryDirectDepsThat().containsExactly(dep);
ReifiedSkyFunctionException exception = new ReifiedSkyFunctionException(
new GenericFunctionException(new SomeErrorException("oops"), Transience.PERSISTENT),
key("cause"));
entry.markRebuilding();
setValue(entry, new IntegerValue(5), ErrorInfo.fromException(exception, false),
/*graphVersion=*/1L);
assertThat(entry.isDone()).isTrue();
assertWithMessage("Version increments when setValue changes")
.that(entry.getVersion())
.isEqualTo(IntVersion.of(1));
}
@Test
public void pruneWhenDepGroupReordered() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
SkyKey dep = key("dep");
SkyKey dep1InGroup = key("dep1InGroup");
SkyKey dep2InGroup = key("dep2InGroup");
addTemporaryDirectDep(entry, dep);
addTemporaryDirectDeps(entry, dep1InGroup, dep2InGroup);
entry.signalDep();
entry.signalDep();
entry.signalDep();
setValue(entry, new IntegerValue(5), /*errorInfo=*/ null, /*graphVersion=*/ 0L);
assertThat(entry.isDirty()).isFalse();
assertThat(entry.isDone()).isTrue();
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/ false);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
assertThat(entry.isDirty()).isTrue();
assertThat(entry.isChanged()).isFalse();
assertThat(entry.isDone()).isFalse();
assertThatNodeEntry(entry)
.addReverseDepAndCheckIfDone(null)
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
assertThat(entry.isReady()).isTrue();
entry.addReverseDepAndCheckIfDone(null);
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep);
addTemporaryDirectDep(entry, dep);
entry.signalDep(IntVersion.of(1L));
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.NEEDS_REBUILDING);
assertThatNodeEntry(entry).hasTemporaryDirectDepsThat().containsExactly(dep);
entry.markRebuilding();
addTemporaryDirectDeps(entry, dep2InGroup, dep1InGroup);
assertThat(entry.signalDep()).isFalse();
assertThat(entry.signalDep()).isTrue();
setValue(entry, new IntegerValue(5), /*errorInfo=*/ null, /*graphVersion=*/ 1L);
assertThat(entry.isDone()).isTrue();
assertWithMessage("Version does not change when dep group reordered")
.that(entry.getVersion())
.isEqualTo(IntVersion.of(0));
}
@Test
public void errorInfoCannotBePruned() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
SkyKey dep = key("dep");
addTemporaryDirectDep(entry, dep);
entry.signalDep();
ReifiedSkyFunctionException exception = new ReifiedSkyFunctionException(
new GenericFunctionException(new SomeErrorException("oops"), Transience.PERSISTENT),
key("cause"));
ErrorInfo errorInfo = ErrorInfo.fromException(exception, false);
setValue(entry, /*value=*/null, errorInfo, /*graphVersion=*/0L);
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/false);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
entry.addReverseDepAndCheckIfDone(null); // Restart evaluation.
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep);
addTemporaryDirectDep(entry, dep);
entry.signalDep(IntVersion.of(1L));
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.NEEDS_REBUILDING);
assertThatNodeEntry(entry).hasTemporaryDirectDepsThat().containsExactly(dep);
entry.markRebuilding();
setValue(entry, /*value=*/null, errorInfo, /*graphVersion=*/1L);
assertThat(entry.isDone()).isTrue();
// ErrorInfo is treated as a NotComparableSkyValue, so it is not pruned.
assertThat(entry.getVersion()).isEqualTo(IntVersion.of(1L));
}
@Test
public void getDependencyGroup() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
SkyKey dep = key("dep");
SkyKey dep2 = key("dep2");
SkyKey dep3 = key("dep3");
addTemporaryDirectDeps(entry, dep, dep2);
addTemporaryDirectDep(entry, dep3);
entry.signalDep();
entry.signalDep();
entry.signalDep();
setValue(entry, /*value=*/new IntegerValue(5), null, 0L);
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/false);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
entry.addReverseDepAndCheckIfDone(null); // Restart evaluation.
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep, dep2);
addTemporaryDirectDeps(entry, dep, dep2);
entry.signalDep(IntVersion.of(0L));
entry.signalDep(IntVersion.of(0L));
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep3);
}
@Test
public void maintainDependencyGroupAfterRemoval() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
SkyKey dep = key("dep");
SkyKey dep2 = key("dep2");
SkyKey dep3 = key("dep3");
SkyKey dep4 = key("dep4");
SkyKey dep5 = key("dep5");
addTemporaryDirectDeps(entry, dep, dep2, dep3);
addTemporaryDirectDep(entry, dep4);
addTemporaryDirectDep(entry, dep5);
entry.signalDep();
entry.signalDep();
// Oops! Evaluation terminated with an error, but we're going to set this entry's value anyway.
entry.removeUnfinishedDeps(ImmutableSet.of(dep2, dep3, dep5));
ReifiedSkyFunctionException exception = new ReifiedSkyFunctionException(
new GenericFunctionException(new SomeErrorException("oops"), Transience.PERSISTENT),
key("key"));
setValue(entry, null, ErrorInfo.fromException(exception, false), 0L);
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/false);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
entry.addReverseDepAndCheckIfDone(null); // Restart evaluation.
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep);
addTemporaryDirectDep(entry, dep);
entry.signalDep(IntVersion.of(0L));
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep4);
}
@Test
public void pruneWhenDepsChange() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
SkyKey dep = key("dep");
addTemporaryDirectDep(entry, dep);
entry.signalDep();
setValue(entry, new IntegerValue(5), /*errorInfo=*/null, /*graphVersion=*/0L);
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/false);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(dep);
addTemporaryDirectDep(entry, dep);
assertThat(entry.signalDep(IntVersion.of(1L))).isTrue();
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.NEEDS_REBUILDING);
assertThatNodeEntry(entry).hasTemporaryDirectDepsThat().containsExactly(dep);
entry.markRebuilding();
addTemporaryDirectDep(entry, key("dep2"));
assertThat(entry.signalDep(IntVersion.of(1L))).isTrue();
setValue(entry, new IntegerValue(5), /*errorInfo=*/null, /*graphVersion=*/1L);
assertThat(entry.isDone()).isTrue();
assertThatNodeEntry(entry).hasVersionThat().isEqualTo(IntVersion.of(0L));
}
@Test
public void checkDepsOneByOne() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(null); // Start evaluation.
List<SkyKey> deps = new ArrayList<>();
for (int ii = 0; ii < 10; ii++) {
SkyKey dep = key(Integer.toString(ii));
deps.add(dep);
addTemporaryDirectDep(entry, dep);
entry.signalDep();
}
setValue(entry, new IntegerValue(5), /*errorInfo=*/null, /*graphVersion=*/0L);
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/false);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
entry.addReverseDepAndCheckIfDone(null); // Start new evaluation.
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
for (int ii = 0; ii < 10; ii++) {
assertThat(entry.getNextDirtyDirectDeps()).containsExactly(deps.get(ii));
addTemporaryDirectDep(entry, deps.get(ii));
assertThat(entry.signalDep(IntVersion.of(0L))).isTrue();
if (ii < 9) {
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.CHECK_DEPENDENCIES);
} else {
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.VERIFIED_CLEAN);
}
}
}
@Test
public void signalOnlyNewParents() throws InterruptedException {
NodeEntry entry = new InMemoryNodeEntry();
entry.addReverseDepAndCheckIfDone(key("parent"));
setValue(entry, new SkyValue() {}, /*errorInfo=*/null, /*graphVersion=*/0L);
MarkedDirtyResult markedDirtyResult = entry.markDirty(/*isChanged=*/true);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
SkyKey newParent = key("new parent");
entry.addReverseDepAndCheckIfDone(newParent);
assertThat(entry.getDirtyState()).isEqualTo(NodeEntry.DirtyState.NEEDS_REBUILDING);
entry.markRebuilding();
assertThat(setValue(entry, new SkyValue() {}, /*errorInfo=*/null,
/*graphVersion=*/1L)).containsExactly(newParent);
}
@Test
public void testClone() throws InterruptedException {
InMemoryNodeEntry entry = new InMemoryNodeEntry();
IntVersion version = IntVersion.of(0);
IntegerValue originalValue = new IntegerValue(42);
SkyKey originalChild = key("child");
assertThatNodeEntry(entry)
.addReverseDepAndCheckIfDone(null)
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
addTemporaryDirectDep(entry, originalChild);
entry.signalDep();
entry.setValue(originalValue, version);
entry.addReverseDepAndCheckIfDone(key("parent1"));
InMemoryNodeEntry clone1 = entry.cloneNodeEntry();
entry.addReverseDepAndCheckIfDone(key("parent2"));
InMemoryNodeEntry clone2 = entry.cloneNodeEntry();
entry.removeReverseDep(key("parent1"));
entry.removeReverseDep(key("parent2"));
IntegerValue updatedValue = new IntegerValue(52);
MarkedDirtyResult markedDirtyResult = clone2.markDirty(true);
assertThat(markedDirtyResult.wasCallRedundant()).isFalse();
assertThat(markedDirtyResult.wasClean()).isTrue();
clone2.addReverseDepAndCheckIfDone(null);
SkyKey newChild = key("newchild");
addTemporaryDirectDep(clone2, newChild);
clone2.signalDep();
clone2.markRebuilding();
clone2.setValue(updatedValue, version.next());
assertThat(entry.getVersion()).isEqualTo(version);
assertThat(clone1.getVersion()).isEqualTo(version);
assertThat(clone2.getVersion()).isEqualTo(version.next());
assertThat(entry.getValue()).isEqualTo(originalValue);
assertThat(clone1.getValue()).isEqualTo(originalValue);
assertThat(clone2.getValue()).isEqualTo(updatedValue);
assertThat(entry.getDirectDeps()).containsExactly(originalChild);
assertThat(clone1.getDirectDeps()).containsExactly(originalChild);
assertThat(clone2.getDirectDeps()).containsExactly(newChild);
assertThat(entry.getReverseDepsForDoneEntry()).isEmpty();
assertThat(clone1.getReverseDepsForDoneEntry()).containsExactly(key("parent1"));
assertThat(clone2.getReverseDepsForDoneEntry()).containsExactly(key("parent1"), key("parent2"));
}
@Test
public void getGroupedDirectDeps() throws InterruptedException {
InMemoryNodeEntry entry = new InMemoryNodeEntry();
ImmutableList<ImmutableSet<SkyKey>> groupedDirectDeps = ImmutableList.of(
ImmutableSet.of(key("1A")),
ImmutableSet.of(key("2A"), key("2B")),
ImmutableSet.of(key("3A"), key("3B"), key("3C")),
ImmutableSet.of(key("4A"), key("4B"), key("4C"), key("4D")));
assertThatNodeEntry(entry)
.addReverseDepAndCheckIfDone(null)
.isEqualTo(DependencyState.NEEDS_SCHEDULING);
for (Set<SkyKey> depGroup : groupedDirectDeps) {
GroupedListHelper<SkyKey> helper = new GroupedListHelper<>();
helper.startGroup();
for (SkyKey item : depGroup) {
helper.add(item);
}
helper.endGroup();
entry.addTemporaryDirectDeps(helper);
for (int i = 0; i < depGroup.size(); i++) {
entry.signalDep();
}
}
entry.setValue(new IntegerValue(42), IntVersion.of(42L));
int i = 0;
GroupedList<SkyKey> entryGroupedDirectDeps = entry.getGroupedDirectDeps();
assertThat(Iterables.size(entryGroupedDirectDeps)).isEqualTo(groupedDirectDeps.size());
for (Iterable<SkyKey> depGroup : entryGroupedDirectDeps) {
assertThat(depGroup).containsExactlyElementsIn(groupedDirectDeps.get(i++));
}
}
private static Set<SkyKey> setValue(
NodeEntry entry, SkyValue value, @Nullable ErrorInfo errorInfo, long graphVersion)
throws InterruptedException {
return entry.setValue(
ValueWithMetadata.normal(value, errorInfo, NO_EVENTS, NO_POSTS),
IntVersion.of(graphVersion));
}
private static void addTemporaryDirectDep(NodeEntry entry, SkyKey key) {
GroupedListHelper<SkyKey> helper = new GroupedListHelper<>();
helper.add(key);
entry.addTemporaryDirectDeps(helper);
}
private static void addTemporaryDirectDeps(NodeEntry entry, SkyKey... keys) {
GroupedListHelper<SkyKey> helper = new GroupedListHelper<>();
helper.startGroup();
for (SkyKey key : keys) {
helper.add(key);
}
helper.endGroup();
entry.addTemporaryDirectDeps(helper);
}
}
|