-
Notifications
You must be signed in to change notification settings - Fork 131
/
RepositorySubsystemTest.java
950 lines (828 loc) · 33.3 KB
/
RepositorySubsystemTest.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
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
package org.dcache.tests.repository;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.WRITE;
import static org.dcache.pool.repository.ReplicaState.BROKEN;
import static org.dcache.pool.repository.ReplicaState.CACHED;
import static org.dcache.pool.repository.ReplicaState.DESTROYED;
import static org.dcache.pool.repository.ReplicaState.FROM_CLIENT;
import static org.dcache.pool.repository.ReplicaState.FROM_STORE;
import static org.dcache.pool.repository.ReplicaState.NEW;
import static org.dcache.pool.repository.ReplicaState.PRECIOUS;
import static org.dcache.pool.repository.ReplicaState.REMOVED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.sleepycat.je.DatabaseException;
import diskCacheV111.util.CacheException;
import diskCacheV111.util.DiskErrorCacheException;
import diskCacheV111.util.DiskSpace;
import diskCacheV111.util.FileInCacheException;
import diskCacheV111.util.FileNotInCacheException;
import diskCacheV111.util.LockedCacheException;
import diskCacheV111.util.PnfsHandler;
import diskCacheV111.util.PnfsId;
import diskCacheV111.vehicles.GenericStorageInfo;
import diskCacheV111.vehicles.PnfsAddCacheLocationMessage;
import diskCacheV111.vehicles.PnfsClearCacheLocationMessage;
import diskCacheV111.vehicles.StorageInfo;
import dmg.cells.nucleus.CellAddressCore;
import dmg.cells.nucleus.CellPath;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.OptionalLong;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;
import org.dcache.namespace.FileAttribute;
import org.dcache.pool.classic.SpaceSweeper2;
import org.dcache.pool.repository.AbstractStateChangeListener;
import org.dcache.pool.repository.Account;
import org.dcache.pool.repository.CacheEntry;
import org.dcache.pool.repository.FileStore;
import org.dcache.pool.repository.FlatFileStore;
import org.dcache.pool.repository.IllegalTransitionException;
import org.dcache.pool.repository.ModifiableReplicaDescriptor;
import org.dcache.pool.repository.ReplicaDescriptor;
import org.dcache.pool.repository.ReplicaState;
import org.dcache.pool.repository.ReplicaStore;
import org.dcache.pool.repository.Repository.OpenFlags;
import org.dcache.pool.repository.RepositoryChannel;
import org.dcache.pool.repository.SpaceRecord;
import org.dcache.pool.repository.StateChangeEvent;
import org.dcache.pool.repository.StickyRecord;
import org.dcache.pool.repository.meta.file.FileMetaDataRepository;
import org.dcache.pool.repository.v5.ReplicaRepository;
import org.dcache.tests.cells.CellEndpointHelper;
import org.dcache.tests.cells.CellStubHelper;
import org.dcache.tests.cells.Message;
import org.dcache.util.ByteUnit;
import org.dcache.vehicles.FileAttributes;
import org.dcache.vehicles.PnfsSetFileAttributes;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
public class RepositorySubsystemTest
extends AbstractStateChangeListener {
private final static long repoSize = ByteUnit.MiB.toBytes(
512); // allocator chunks 50MB. Make some room for allocation
private long size1 = 1024;
private long size2 = 1024;
private long size3 = 1024;
private long size4 = 1024;
private long size5 = 1024;
private PnfsId id1; // Precious
private PnfsId id2; // Cached
private PnfsId id3; // Cached + Sticky
private PnfsId id4; // Non existing entry
private PnfsId id5; // Non existing entry
private StorageInfo info1;
private StorageInfo info2;
private StorageInfo info3;
private StorageInfo info4;
private StorageInfo info5;
private FileAttributes attributes1;
private FileAttributes attributes2;
private FileAttributes attributes3;
private FileAttributes attributes4;
private FileAttributes attributes5;
private PnfsHandler pnfs;
private Account account;
private ReplicaRepository repository;
private SpaceSweeper2 sweeper;
private ReplicaStore replicaStore;
private Path metaRoot;
private Path dataRoot;
private Path metaDir;
private Path dataDir;
private BlockingQueue<StateChangeEvent> stateChangeEvents =
new LinkedBlockingQueue<>();
private CellEndpointHelper cell;
private final CellAddressCore address = new CellAddressCore("pool", "test");
private void createFile(ReplicaDescriptor descriptor, long size)
throws IOException, CacheException {
try (RepositoryChannel channel = descriptor.createChannel()) {
channel.write(ByteBuffer.allocate((int) size));
}
}
private void createEntry(final FileAttributes attributes,
final ReplicaState state,
final List<StickyRecord> sticky)
throws Throwable {
new CellStubHelper(cell) {
@Message(cell = "pnfs")
public Object message(PnfsSetFileAttributes msg) {
msg.setSucceeded();
return msg;
}
@Override
protected void run()
throws CacheException, IOException, InterruptedException {
ModifiableReplicaDescriptor handle =
repository.createEntry(attributes,
ReplicaState.FROM_CLIENT,
state,
sticky,
EnumSet.noneOf(OpenFlags.class),
OptionalLong.empty());
try {
createFile(handle, attributes.getSize());
handle.commit();
} finally {
handle.close();
}
}
};
}
private FileAttributes createFileAttributes(PnfsId pnfsId, long size, StorageInfo info) {
FileAttributes attributes = new FileAttributes();
attributes.setPnfsId(pnfsId);
attributes.setStorageInfo(info);
attributes.setSize(size);
attributes.setAccessLatency(StorageInfo.DEFAULT_ACCESS_LATENCY);
attributes.setRetentionPolicy(StorageInfo.DEFAULT_RETENTION_POLICY);
return attributes;
}
private void deleteDirectory(Path dir) throws IOException {
Path[] fileArray;
try (Stream<Path> list = Files.list(dir)) {
fileArray = list.toArray(Path[]::new);
}
for (Path file : fileArray) {
if (Files.isDirectory(file)) {
deleteDirectory(file);
} else {
Files.delete(file);
}
}
if (dir.getParent() != null) {
Files.delete(dir);
}
}
private void initRepository()
throws IOException, DatabaseException {
FileStore fileStore = new FlatFileStore(dataRoot);
replicaStore =
new FileMetaDataRepository(fileStore, metaRoot, "pool");
account = new Account();
sweeper = new SpaceSweeper2();
repository = new ReplicaRepository();
repository.setCellAddress(address);
repository.setPnfsHandler(pnfs);
repository.setAccount(account);
repository.setReplicaStore(replicaStore);
repository.setExecutor(Executors.newSingleThreadScheduledExecutor());
repository.setSynchronousNotification(true);
repository.addListener(this);
repository.setSpaceSweeperPolicy(sweeper);
repository.setMaxDiskSpace(new DiskSpace(repoSize));
repository.addFaultListener(
event -> System.err.println(event.getMessage() + ": " + event.getCause()));
repository.setScanThreads(1);
}
@Before
public void setUp()
throws Throwable {
id1 = new PnfsId("000000000001");
id2 = new PnfsId("000000000002");
id3 = new PnfsId("000000000003");
id4 = new PnfsId("000000000004");
id5 = new PnfsId("000000000005");
info1 = new GenericStorageInfo();
info2 = new GenericStorageInfo();
info3 = new GenericStorageInfo();
info4 = new GenericStorageInfo();
info5 = new GenericStorageInfo();
attributes1 = createFileAttributes(id1, size1, info1);
attributes2 = createFileAttributes(id2, size2, info2);
attributes3 = createFileAttributes(id3, size3, info3);
attributes4 = createFileAttributes(id4, 0, info4);
attributes5 = createFileAttributes(id5, size5, info5);
dataRoot = Jimfs.newFileSystem(Configuration.unix()).getPath("/");
metaRoot = Files.createTempDirectory("dtest");
dataDir = dataRoot.resolve("data");
metaDir = metaRoot.resolve("meta");
Files.createDirectory(dataDir);
Files.createDirectory(metaDir);
cell = new CellEndpointHelper(address);
pnfs = new PnfsHandler(new CellPath("pnfs"), "pool");
pnfs.setCellEndpoint(cell);
/* Create test data. Notice that the repository automatically
* applies a short lived sticky record if we don't request
* one. That is fine for normal operation, but for testing it
* is not what we want. So we explicitly specify an expired
* sticky record to avoid that the automatic sticky record is
* created.
*/
initRepository();
repository.init();
repository.load();
createEntry(attributes1, ReplicaState.PRECIOUS,
Arrays.asList(new StickyRecord("system", 0)));
createEntry(attributes2, ReplicaState.CACHED,
Arrays.asList(new StickyRecord("system", 0)));
createEntry(attributes3, ReplicaState.CACHED,
Arrays.asList(new StickyRecord("system", -1)));
repository.shutdown();
replicaStore.close();
/* Create repository.
*/
initRepository();
sweeper.setAccount(account);
sweeper.setRepository(repository);
sweeper.start();
}
@After
public void tearDown()
throws InterruptedException, IOException {
sweeper.stop();
repository.shutdown();
replicaStore.close();
if (metaRoot != null) {
deleteDirectory(metaRoot);
}
if (dataRoot != null) {
deleteDirectory(dataRoot);
}
}
@Override
public void stateChanged(StateChangeEvent event) {
stateChangeEvents.add(event);
}
public void expectStateChangeEvent(PnfsId id, ReplicaState oldState, ReplicaState newState) {
StateChangeEvent event = stateChangeEvents.poll();
assertNotNull(event);
assertEquals(id, event.getPnfsId());
assertEquals(oldState, event.getOldState());
assertEquals(newState, event.getNewState());
}
public void assertNoStateChangeEvent() {
if (stateChangeEvents.size() > 0) {
fail("Unexpected state change event: "
+ stateChangeEvents.remove());
}
}
private void assertSpaceRecord(long total, long free, long precious, long removable) {
SpaceRecord space = repository.getSpaceRecord();
assertEquals(total, space.getTotalSpace());
assertEquals(free, space.getFreeSpace());
assertEquals(precious, space.getPreciousSpace());
assertEquals(removable, space.getRemovableSpace());
}
private void assertCacheEntry(CacheEntry entry, PnfsId id,
long size, ReplicaState state) {
assertEquals(id, entry.getPnfsId());
assertEquals(size, entry.getReplicaSize());
assertEquals(state, entry.getState());
}
private void assertCanOpen(PnfsId id, long size, ReplicaState state) {
try {
ReplicaDescriptor handle =
repository.openEntry(id, EnumSet.noneOf(OpenFlags.class));
try {
try (RepositoryChannel channel = handle.createChannel()) {
}
assertCacheEntry(repository.getEntry(id), id, size, state);
} finally {
handle.close();
}
} catch (FileNotInCacheException e) {
fail("Expected entry " + id + " not found");
} catch (CacheException | InterruptedException | IOException e) {
fail("Failed to open " + id + ": " + e.getMessage());
}
}
@Test(expected = IllegalStateException.class)
public void testInitTwiceFails()
throws IOException, CacheException, InterruptedException {
repository.init();
repository.init();
}
@Test(expected = IllegalStateException.class)
public void testLoadTwiceFails()
throws IOException, CacheException, InterruptedException {
repository.init();
repository.load();
repository.load();
}
@Test(expected = IllegalStateException.class)
public void testLoadBeforeInitFail()
throws IOException, CacheException, InterruptedException {
repository.load();
}
@Test(expected = IllegalStateException.class)
public void testCreateEntryFailsBeforeLoad() throws Exception {
repository.init();
List<StickyRecord> stickyRecords = Collections.emptyList();
repository.createEntry(FileAttributes.of().pnfsId(id1).storageInfo(info1).build(),
FROM_CLIENT, PRECIOUS, stickyRecords, EnumSet.noneOf(OpenFlags.class),
OptionalLong.empty());
}
@Test(expected = IllegalStateException.class)
public void testOpenEntryFailsBeforeInit() throws Exception {
repository.openEntry(id1, EnumSet.noneOf(OpenFlags.class));
}
@Test(expected = IllegalStateException.class)
public void testGetEntryFailsBeforeInit() throws Exception {
repository.getEntry(id1);
}
@Test(expected = IllegalStateException.class)
public void testSetStickyFailsBeforeInit() throws Exception {
repository.setSticky(id2, "system", 0, true);
}
@Test(expected = IllegalStateException.class)
public void testGetStateFailsBeforeInit() throws Exception {
repository.getState(id1);
}
@Test(expected = IllegalStateException.class)
public void testSetStateFailsBeforeLoad() throws Exception {
repository.init();
repository.setState(id1, CACHED, "test");
}
@Test
public void testGetSpaceRecord()
throws IOException, CacheException, InterruptedException {
assertSpaceRecord(0, 0, 0, 0);
repository.init();
assertSpaceRecord(0, 0, 0, 0);
repository.load();
assertSpaceRecord(repoSize, repoSize - 2048 - 1024, 1024, 1024);
}
@Test
public void testOpenEntryBeforeLoad()
throws IOException, CacheException, InterruptedException {
repository.init();
stateChangeEvents.clear();
assertCanOpen(id1, size1, PRECIOUS);
assertCanOpen(id2, size2, CACHED);
assertCanOpen(id3, size3, CACHED);
}
@Test
public void testOpenEntry()
throws IOException, CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
assertCanOpen(id1, size1, PRECIOUS);
assertCanOpen(id2, size2, CACHED);
assertCanOpen(id3, size3, CACHED);
}
@Test(expected = FileNotInCacheException.class)
public void testOpenEntryFileNotFound()
throws Throwable {
repository.init();
repository.load();
stateChangeEvents.clear();
new CellStubHelper(cell) {
/* Attempting to open a non-existing entry triggers a
* clear cache location message.
*/
@Message(required = true, step = 1, cell = "pnfs")
public Object message(PnfsClearCacheLocationMessage msg) {
msg.setSucceeded();
return msg;
}
@Override
protected void run()
throws CacheException, InterruptedException {
repository.openEntry(id4, EnumSet.noneOf(OpenFlags.class));
}
};
}
@Test
public void testCreateEntryFromStore() throws Throwable {
repository.init();
repository.load();
stateChangeEvents.clear();
new CellStubHelper(cell) {
@Message(required = true, step = 1, cell = "pnfs")
public Object message(PnfsSetFileAttributes msg) {
if (msg.getFileAttributes().isDefined(FileAttribute.SIZE)) {
return new CacheException("");
}
msg.setSucceeded();
return msg;
}
@Override
protected void run()
throws CacheException, InterruptedException {
List<StickyRecord> stickyRecords = Collections.emptyList();
ModifiableReplicaDescriptor handle = repository.createEntry(attributes5, FROM_STORE, CACHED,
stickyRecords,
EnumSet.noneOf(OpenFlags.class), OptionalLong.empty());
try {
createFile(handle, attributes5.getSize());
handle.commit();
} catch (IOException e) {
throw new DiskErrorCacheException(e.getMessage());
} finally {
handle.close();
}
}
};
}
@Test(expected = CacheException.class)
public void testFileIsBroken()
throws IOException, IllegalTransitionException,
CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
ReplicaDescriptor handle = repository.openEntry(id1, EnumSet.noneOf(OpenFlags.class));
Path file = Paths.get(handle.getReplicaFile());
Files.write(file, "APPEND".getBytes(), WRITE, APPEND);
handle.createChannel();
}
@Test
public void testSetState()
throws IOException, IllegalTransitionException,
CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
assertCanOpen(id1, size1, PRECIOUS);
repository.setState(id1, CACHED, "test");
expectStateChangeEvent(id1, PRECIOUS, CACHED);
assertCanOpen(id1, size1, CACHED);
repository.setState(id1, PRECIOUS, "test");
expectStateChangeEvent(id1, CACHED, PRECIOUS);
assertCanOpen(id1, size1, PRECIOUS);
}
@Test(expected = IllegalTransitionException.class)
public void testSetStateFileNotFound()
throws IOException, IllegalTransitionException,
CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
repository.setState(id4, CACHED, "test");
}
@Test(expected = IllegalTransitionException.class)
public void testSetStateToNew()
throws IOException, IllegalTransitionException,
CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
repository.setState(id1, NEW, "test");
}
@Test(expected = IllegalTransitionException.class)
public void testSetStateToDestroyed()
throws IOException, IllegalTransitionException,
CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
repository.setState(id1, DESTROYED, "test");
}
@Test(expected = IllegalStateException.class)
public void testClosedReadHandleClose()
throws IOException, CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
ReplicaDescriptor handle =
repository.openEntry(id1, EnumSet.noneOf(OpenFlags.class));
handle.close();
handle.close();
}
@Test(expected = IllegalStateException.class)
public void testClosedReadHandleGetFile()
throws IOException, CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
ReplicaDescriptor handle =
repository.openEntry(id1, EnumSet.noneOf(OpenFlags.class));
handle.close();
handle.getReplicaFile();
}
@Test
public void testClosedReadHandleGetFileAttributes()
throws IOException, CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
ReplicaDescriptor handle =
repository.openEntry(id1, EnumSet.noneOf(OpenFlags.class));
handle.close();
FileAttributes fileAttributes = handle.getFileAttributes();
assertEquals(id1, fileAttributes.getPnfsId());
assertEquals(size1, fileAttributes.getSize());
assertEquals(info1, fileAttributes.getStorageInfo());
}
@Test
public void testSetSize()
throws IOException, CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
repository.setMaxDiskSpace(new DiskSpace(3072));
assertSpaceRecord(3072, 0, 1024, 1024);
}
@Test(expected = IllegalArgumentException.class)
public void testSetSizeNegative()
throws IOException, CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
repository.setMaxDiskSpace(new DiskSpace(-1));
}
@Test
public void testRemoval()
throws Throwable {
repository.init();
repository.load();
stateChangeEvents.clear();
new CellStubHelper(cell) {
@Message(required = true, step = 1, cell = "pnfs")
public Object message(PnfsClearCacheLocationMessage msg) {
msg.setSucceeded();
return msg;
}
@Override
protected void run()
throws IllegalTransitionException,
CacheException, InterruptedException {
repository.setState(id1, REMOVED, "test");
expectStateChangeEvent(id1, PRECIOUS, REMOVED);
expectStateChangeEvent(id1, REMOVED, DESTROYED);
assertStep("Cache location cleared", 1);
assertEquals(repository.getState(id1), NEW);
}
};
}
@Test
public void testRemoveWhileReading()
throws Throwable {
repository.init();
repository.load();
stateChangeEvents.clear();
new CellStubHelper(cell) {
@Message(required = true, step = 0, cell = "pnfs")
public Object message(PnfsClearCacheLocationMessage msg) {
msg.setSucceeded();
return msg;
}
@Override
protected void run()
throws CacheException, InterruptedException,
IllegalTransitionException {
ReplicaDescriptor handle1 =
repository.openEntry(id1, EnumSet.noneOf(OpenFlags.class));
repository.setState(id1, REMOVED, "test");
expectStateChangeEvent(id1, PRECIOUS, REMOVED);
assertNoStateChangeEvent();
assertStep("Cache location cleared", 1);
handle1.close();
expectStateChangeEvent(id1, REMOVED, DESTROYED);
}
};
}
@Test(expected = LockedCacheException.class)
public void testRemoveOpenAgain()
throws Throwable {
repository.init();
repository.load();
stateChangeEvents.clear();
new CellStubHelper(cell) {
@Message(required = true, step = 1, cell = "pnfs")
public Object message(PnfsClearCacheLocationMessage msg) {
msg.setSucceeded();
return msg;
}
@Override
protected void run()
throws CacheException, InterruptedException,
IllegalTransitionException {
ReplicaDescriptor h1 =
repository.openEntry(id1, EnumSet.noneOf(OpenFlags.class));
repository.setState(id1, REMOVED, "test");
expectStateChangeEvent(id1, PRECIOUS, REMOVED);
assertStep("Cache location should have been cleared", 1);
ReplicaDescriptor h2 =
repository.openEntry(id1, EnumSet.noneOf(OpenFlags.class));
}
};
}
@Test(expected = FileInCacheException.class)
public void testCreateEntryFileExists()
throws Throwable {
repository.init();
repository.load();
stateChangeEvents.clear();
new CellStubHelper(cell) {
/* Attempting to create an existing entry triggers a
* add cache location message.
*/
@Message(required = true, step = 1, cell = "pnfs")
public Object message(PnfsAddCacheLocationMessage msg) {
msg.setSucceeded();
return msg;
}
@Override
protected void run()
throws CacheException, InterruptedException {
List<StickyRecord> stickyRecords = Collections.emptyList();
repository.createEntry(attributes1, FROM_CLIENT, PRECIOUS, stickyRecords,
EnumSet.noneOf(OpenFlags.class), OptionalLong.empty());
}
};
}
/* Helper method for creating a fourth entry in the repository.
*/
private void createEntry4(final boolean failSetAttributes,
final boolean cancel,
final ReplicaState transferState,
final ReplicaState finalState)
throws Throwable {
new CellStubHelper(cell) {
boolean setAttr;
boolean addCache;
@Message(required = false, step = 1, cell = "pnfs")
public Object whenFileIsGarbageCollected(PnfsClearCacheLocationMessage msg) {
msg.setSucceeded();
return msg;
}
@Message(required = false, step = 3, cell = "pnfs")
public Object whenDescriptorIsCommitted(PnfsSetFileAttributes msg) {
assertEquals(size4, msg.getFileAttributes().getSize());
if (failSetAttributes) {
msg.setFailed(1, null);
} else {
msg.setSucceeded();
}
setAttr = true;
return msg;
}
@Message(required = false, step = 5, cell = "pnfs")
public Object whenDescriptorFails(PnfsAddCacheLocationMessage msg) {
assertTrue(failSetAttributes || cancel);
msg.setSucceeded();
addCache = true;
return msg;
}
@Message(required = false, step = 5, cell = "pnfs")
public Object whenDescriptorFails(PnfsSetFileAttributes msg) {
assertTrue(failSetAttributes || cancel);
msg.setSucceeded();
return msg;
}
@Override
protected void run()
throws FileInCacheException,
CacheException,
InterruptedException,
IOException {
List<StickyRecord> stickyRecords = Collections.emptyList();
ModifiableReplicaDescriptor handle =
repository.createEntry(attributes4, transferState,
finalState, stickyRecords, EnumSet.noneOf(OpenFlags.class),
OptionalLong.empty());
try {
assertStep("No clear after this point", 2);
createFile(handle, size4);
if (!cancel) {
handle.commit();
}
} finally {
assertStep("Only failure registration after this point", 4);
handle.close();
}
assertEquals("SetFileAttributes must be sent unless we don't try to commit",
!cancel, setAttr);
assertEquals("AddCacheLocation must be sent if not committed",
cancel || failSetAttributes, addCache);
}
};
}
@Test
public void testCreateEntry()
throws Throwable {
repository.init();
repository.load();
stateChangeEvents.clear();
SpaceRecord r = repository.getSpaceRecord();
createEntry4(false, false, FROM_CLIENT, PRECIOUS);
assertCanOpen(id4, size4, PRECIOUS);
assertSpaceRecord(repoSize, r.getFreeSpace() - size4, r.getPreciousSpace() + size4,
r.getRemovableSpace());
}
@Test(expected = CacheException.class)
public void testCreateEntrySetAttributesFailed()
throws Throwable {
repository.init();
repository.load();
stateChangeEvents.clear();
SpaceRecord r = repository.getSpaceRecord();
try {
createEntry4(true, false, FROM_CLIENT, PRECIOUS);
} finally {
assertCacheEntry(repository.getEntry(id4), id4, size4, BROKEN);
assertSpaceRecord(repoSize, r.getFreeSpace() - size4, r.getPreciousSpace(),
r.getRemovableSpace());
}
// TODO: Check notification
}
// See http://rt.dcache.org/Ticket/Display.html?id=7337
@Ignore("Time-critical test; may fail under extreme load")
@Test
public void testStickyExpiration()
throws IOException, CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
long now = System.currentTimeMillis();
assertFalse(repository.getEntry(id2).isSticky());
repository.setSticky(id2, "system", now + 500, true);
assertTrue(repository.getEntry(id2).isSticky());
Thread.currentThread().sleep(600 + ReplicaRepository.EXPIRATION_CLOCKSHIFT_EXTRA_TIME);
assertFalse(repository.getEntry(id2).isSticky());
}
@Test
public void testStickyClear()
throws IOException, CacheException, InterruptedException {
repository.init();
repository.load();
stateChangeEvents.clear();
long now = System.currentTimeMillis();
assertFalse(repository.getEntry(id2).isSticky());
repository.setSticky(id2, "system", now + 500, true);
assertTrue(repository.getEntry(id2).isSticky());
repository.setSticky(id2, "system", 0, true);
assertFalse(repository.getEntry(id2).isSticky());
}
@Test
public void testDoubleAccountingOnCache()
throws IOException, CacheException,
InterruptedException, IllegalTransitionException {
repository.init();
repository.load();
stateChangeEvents.clear();
SpaceRecord r = repository.getSpaceRecord();
repository.setState(id1, CACHED, "test");
assertSpaceRecord(repoSize, r.getFreeSpace(), r.getPreciousSpace() - size1,
r.getRemovableSpace() + size1);
r = repository.getSpaceRecord();
repository.setState(id1, CACHED, "test");
assertSpaceRecord(repoSize, r.getFreeSpace(), r.getPreciousSpace(), r.getRemovableSpace());
}
@Test
public void testDoubleAccountingOnPrecious()
throws IOException, CacheException,
InterruptedException, IllegalTransitionException {
repository.init();
repository.load();
stateChangeEvents.clear();
SpaceRecord r = repository.getSpaceRecord();
repository.setState(id2, PRECIOUS, "test");
assertSpaceRecord(repoSize, r.getFreeSpace(), r.getPreciousSpace() + size2,
r.getRemovableSpace() - size2);
r = repository.getSpaceRecord();
repository.setState(id2, PRECIOUS, "test");
assertSpaceRecord(repoSize, r.getFreeSpace(), r.getPreciousSpace(), r.getRemovableSpace());
}
@Test
public void testWaitforLoad() throws CacheException, InterruptedException {
AtomicBoolean notifyed = new AtomicBoolean();
repository.waitForLoad().thenRun(() -> notifyed.set(true));
repository.init();
repository.load();
assertTrue("Repository load complete without propagating the info", notifyed.get());
}
@Test
public void testPropagateErrorOnLoad()
throws CacheException, InterruptedException, IOException {
AtomicReference<Throwable> throwableHolder = new AtomicReference<>();
repository.waitForLoad().whenComplete((r, t) -> throwableHolder.set(t));
repository.init();
// corrupt pool's repository
try {
Files.newDirectoryStream(dataDir).forEach(f -> {
try {
Files.delete(f);
} catch (IOException e) {
}
});
Files.delete(dataDir);
repository.load();
fail("load should fail");
} catch (CacheException expected) {
}
assertNotNull("Repository load error not propagated", throwableHolder.get());
}
}