-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
FileSystemRepository.java
1951 lines (1647 loc) · 82 KB
/
FileSystemRepository.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
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.nifi.controller.repository;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.nifi.controller.repository.claim.ContentClaim;
import org.apache.nifi.controller.repository.claim.ResourceClaim;
import org.apache.nifi.controller.repository.claim.ResourceClaimManager;
import org.apache.nifi.controller.repository.claim.StandardContentClaim;
import org.apache.nifi.controller.repository.io.LimitedInputStream;
import org.apache.nifi.engine.FlowEngine;
import org.apache.nifi.processor.DataUnit;
import org.apache.nifi.stream.io.ByteCountingOutputStream;
import org.apache.nifi.stream.io.StreamUtils;
import org.apache.nifi.stream.io.SynchronizedByteCountingOutputStream;
import org.apache.nifi.util.FormatUtils;
import org.apache.nifi.util.NiFiProperties;
import org.apache.nifi.util.StopWatch;
import org.apache.nifi.util.file.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;
/**
* Is thread safe
*
*/
public class FileSystemRepository implements ContentRepository {
public static final int SECTIONS_PER_CONTAINER = 1024;
public static final long MIN_CLEANUP_INTERVAL_MILLIS = 1000;
public static final String ARCHIVE_DIR_NAME = "archive";
// 100 MB cap for the configurable NiFiProperties.MAX_APPENDABLE_CLAIM_SIZE property to prevent
// unnecessarily large resource claim files
public static final String APPENDABLE_CLAIM_LENGTH_CAP = "100 MB";
public static final Pattern MAX_ARCHIVE_SIZE_PATTERN = Pattern.compile("\\d{1,2}%");
private static final Logger LOG = LoggerFactory.getLogger(FileSystemRepository.class);
private final Logger archiveExpirationLog = LoggerFactory.getLogger(FileSystemRepository.class.getName() + ".archive.expiration");
private final Map<String, Path> containers;
private final List<String> containerNames;
private final AtomicLong index;
private final ScheduledExecutorService executor = new FlowEngine(4, "FileSystemRepository Workers", true);
private final ConcurrentMap<String, BlockingQueue<ResourceClaim>> reclaimable = new ConcurrentHashMap<>();
private final Map<String, ContainerState> containerStateMap = new HashMap<>();
// Queue for claims that are kept open for writing. Ideally, this will be at
// least as large as the number of threads that will be updating the repository simultaneously but we don't want
// to get too large because it will hold open up to this many FileOutputStreams.
// The queue is used to determine which claim to write to and then the corresponding Map can be used to obtain
// the OutputStream that we can use for writing to the claim.
private final BlockingQueue<ClaimLengthPair> writableClaimQueue;
private final ConcurrentMap<ResourceClaim, ByteCountingOutputStream> writableClaimStreams = new ConcurrentHashMap<>(100);
private final boolean archiveData;
// 1 MB default, as it means that we won't continually write to one
// file that keeps growing but gives us a chance to bunch together a lot of small files. Before, we had issues
// with creating and deleting too many files, as we had to delete 100's of thousands of files every 2 minutes
// in order to avoid backpressure on session commits. With 1 MB as the target file size, 100's of thousands of
// files would mean that we are writing gigabytes per second - quite a bit faster than any disks can handle now.
private final long maxAppendableClaimLength;
private final int maxFlowFilesPerClaim;
private final long maxArchiveMillis;
private final Map<String, Long> minUsableContainerBytesForArchive = new HashMap<>();
private final boolean alwaysSync;
private final ScheduledExecutorService containerCleanupExecutor;
private ResourceClaimManager resourceClaimManager; // effectively final
// Map of container to archived files that should be deleted next.
private final Map<String, BlockingQueue<ArchiveInfo>> archivedFiles = new HashMap<>();
// guarded by synchronizing on this
private final AtomicLong oldestArchiveDate = new AtomicLong(0L);
private final NiFiProperties nifiProperties;
/**
* Default no args constructor for service loading only
*/
public FileSystemRepository() {
containers = null;
containerNames = null;
index = null;
archiveData = false;
maxArchiveMillis = 0;
alwaysSync = false;
containerCleanupExecutor = null;
nifiProperties = null;
maxAppendableClaimLength = 0;
maxFlowFilesPerClaim = 0;
writableClaimQueue = null;
}
public FileSystemRepository(final NiFiProperties nifiProperties) throws IOException {
this.nifiProperties = nifiProperties;
// determine the file repository paths and ensure they exist
final Map<String, Path> fileRespositoryPaths = nifiProperties.getContentRepositoryPaths();
for (final Path path : fileRespositoryPaths.values()) {
Files.createDirectories(path);
}
this.maxFlowFilesPerClaim = nifiProperties.getMaxFlowFilesPerClaim();
this.writableClaimQueue = new LinkedBlockingQueue<>(maxFlowFilesPerClaim);
final long configuredAppendableClaimLength = DataUnit.parseDataSize(nifiProperties.getMaxAppendableClaimSize(), DataUnit.B).longValue();
final long appendableClaimLengthCap = DataUnit.parseDataSize(APPENDABLE_CLAIM_LENGTH_CAP, DataUnit.B).longValue();
if (configuredAppendableClaimLength > appendableClaimLengthCap) {
LOG.warn("Configured property '{}' with value {} exceeds cap of {}. Setting value to {}",
NiFiProperties.MAX_APPENDABLE_CLAIM_SIZE,
configuredAppendableClaimLength,
APPENDABLE_CLAIM_LENGTH_CAP,
APPENDABLE_CLAIM_LENGTH_CAP);
this.maxAppendableClaimLength = appendableClaimLengthCap;
} else {
this.maxAppendableClaimLength = configuredAppendableClaimLength;
}
this.containers = new HashMap<>(fileRespositoryPaths);
this.containerNames = new ArrayList<>(containers.keySet());
index = new AtomicLong(0L);
for (final String containerName : containerNames) {
reclaimable.put(containerName, new LinkedBlockingQueue<>(10000));
archivedFiles.put(containerName, new LinkedBlockingQueue<>(100000));
}
final String enableArchiving = nifiProperties.getProperty(NiFiProperties.CONTENT_ARCHIVE_ENABLED);
final String maxArchiveRetentionPeriod = nifiProperties.getProperty(NiFiProperties.CONTENT_ARCHIVE_MAX_RETENTION_PERIOD);
final String maxArchiveSize = nifiProperties.getProperty(NiFiProperties.CONTENT_ARCHIVE_MAX_USAGE_PERCENTAGE);
final String archiveBackPressureSize = nifiProperties.getProperty(NiFiProperties.CONTENT_ARCHIVE_BACK_PRESSURE_PERCENTAGE);
if ("true".equalsIgnoreCase(enableArchiving)) {
archiveData = true;
if (maxArchiveSize == null) {
throw new RuntimeException("No value specified for property '"
+ NiFiProperties.CONTENT_ARCHIVE_MAX_USAGE_PERCENTAGE + "' but archiving is enabled. You must configure the max disk usage in order to enable archiving.");
}
if (!MAX_ARCHIVE_SIZE_PATTERN.matcher(maxArchiveSize.trim()).matches()) {
throw new RuntimeException("Invalid value specified for the '" + NiFiProperties.CONTENT_ARCHIVE_MAX_USAGE_PERCENTAGE + "' property. Value must be in format: <XX>%");
}
} else if ("false".equalsIgnoreCase(enableArchiving)) {
archiveData = false;
} else {
LOG.warn("No property set for '{}'; will not archive content", NiFiProperties.CONTENT_ARCHIVE_ENABLED);
archiveData = false;
}
double maxArchiveRatio = 0D;
double archiveBackPressureRatio = 0.01D;
if (maxArchiveSize != null && MAX_ARCHIVE_SIZE_PATTERN.matcher(maxArchiveSize.trim()).matches()) {
maxArchiveRatio = getRatio(maxArchiveSize);
if (archiveBackPressureSize != null && MAX_ARCHIVE_SIZE_PATTERN.matcher(archiveBackPressureSize.trim()).matches()) {
archiveBackPressureRatio = getRatio(archiveBackPressureSize);
} else {
archiveBackPressureRatio = maxArchiveRatio + 0.02D;
}
}
if (maxArchiveRatio > 0D) {
for (final Map.Entry<String, Path> container : containers.entrySet()) {
final String containerName = container.getKey();
final long capacity = container.getValue().toFile().getTotalSpace();
if(capacity==0) {
throw new RuntimeException("System returned total space of the partition for " + containerName + " is zero byte. Nifi can not create a zero sized FileSystemRepository");
}
final long maxArchiveBytes = (long) (capacity * (1D - (maxArchiveRatio - 0.02)));
minUsableContainerBytesForArchive.put(container.getKey(), Long.valueOf(maxArchiveBytes));
LOG.info("Maximum Threshold for Container {} set to {} bytes; if volume exceeds this size, archived data will be deleted until it no longer exceeds this size",
containerName, maxArchiveBytes);
final long backPressureBytes = (long) (container.getValue().toFile().getTotalSpace() * archiveBackPressureRatio);
final ContainerState containerState = new ContainerState(containerName, true, backPressureBytes, capacity);
containerStateMap.put(containerName, containerState);
}
} else {
for (final String containerName : containerNames) {
containerStateMap.put(containerName, new ContainerState(containerName, false, Long.MAX_VALUE, Long.MAX_VALUE));
}
}
if (maxArchiveRatio <= 0D) {
maxArchiveMillis = 0L;
} else {
maxArchiveMillis = StringUtils.isEmpty(maxArchiveRetentionPeriod) ? Long.MAX_VALUE : FormatUtils.getTimeDuration(maxArchiveRetentionPeriod, TimeUnit.MILLISECONDS);
}
this.alwaysSync = Boolean.parseBoolean(nifiProperties.getProperty("nifi.content.repository.always.sync"));
LOG.info("Initializing FileSystemRepository with 'Always Sync' set to {}", alwaysSync);
initializeRepository();
containerCleanupExecutor = new FlowEngine(containers.size(), "Cleanup FileSystemRepository Container", true);
}
@Override
public void initialize(final ResourceClaimManager claimManager) {
this.resourceClaimManager = claimManager;
final Map<String, Path> fileRespositoryPaths = nifiProperties.getContentRepositoryPaths();
executor.scheduleWithFixedDelay(new BinDestructableClaims(), 1, 1, TimeUnit.SECONDS);
for (int i = 0; i < fileRespositoryPaths.size(); i++) {
executor.scheduleWithFixedDelay(new ArchiveOrDestroyDestructableClaims(), 1, 1, TimeUnit.SECONDS);
}
final long cleanupMillis = this.determineCleanupInterval(nifiProperties);
for (final Map.Entry<String, Path> containerEntry : containers.entrySet()) {
final String containerName = containerEntry.getKey();
final Path containerPath = containerEntry.getValue();
final Runnable cleanup = new DestroyExpiredArchiveClaims(containerName, containerPath);
containerCleanupExecutor.scheduleWithFixedDelay(cleanup, cleanupMillis, cleanupMillis, TimeUnit.MILLISECONDS);
}
}
@Override
public void shutdown() {
executor.shutdown();
containerCleanupExecutor.shutdown();
// Close any of the writable claim streams that are currently open.
// Other threads may be writing to these streams, and that's okay.
// If that happens, we will simply close the stream, resulting in an
// IOException that will roll back the session. Since this is called
// only on shutdown of the application, we don't have to worry about
// partially written files - on restart, we will simply start writing
// to new files and leave those trailing bytes alone.
for (final OutputStream out : writableClaimStreams.values()) {
try {
out.close();
} catch (final IOException ioe) {
}
}
}
private static double getRatio(final String value) {
final String trimmed = value.trim();
final String percentage = trimmed.substring(0, trimmed.length() - 1);
return Integer.parseInt(percentage) / 100D;
}
private synchronized void initializeRepository() throws IOException {
final Map<String, Path> realPathMap = new HashMap<>();
final ExecutorService executor = Executors.newFixedThreadPool(containers.size());
final List<Future<Long>> futures = new ArrayList<>();
// Run through each of the containers. For each container, create the sections if necessary.
// Then, we need to scan through the archived data so that we can determine what the oldest
// archived data is, so that we know when we have to start aging data off.
for (final Map.Entry<String, Path> container : containers.entrySet()) {
final String containerName = container.getKey();
final ContainerState containerState = containerStateMap.get(containerName);
final Path containerPath = container.getValue();
final boolean pathExists = Files.exists(containerPath);
final Path realPath;
if (pathExists) {
realPath = containerPath.toRealPath();
} else {
realPath = Files.createDirectories(containerPath).toRealPath();
}
for (int i = 0; i < SECTIONS_PER_CONTAINER; i++) {
Files.createDirectories(realPath.resolve(String.valueOf(i)));
}
realPathMap.put(containerName, realPath);
// We need to scan the archive directories to find out the oldest timestamp so that know whether or not we
// will have to delete archived data based on time threshold. Scanning all of the directories can be very
// expensive because of all of the disk accesses. So we do this in multiple threads. Since containers are
// often unique to a disk, we just map 1 thread to each container.
final Callable<Long> scanContainer = new Callable<Long>() {
@Override
public Long call() throws IOException {
final AtomicLong oldestDateHolder = new AtomicLong(0L);
// the path already exists, so scan the path to find any files and update maxIndex to the max of
// all filenames seen.
Files.walkFileTree(realPath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
LOG.warn("Content repository contains un-readable file or directory '" + file.getFileName() + "'. Skipping. ", exc);
return FileVisitResult.SKIP_SUBTREE;
}
@Override
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
if (attrs.isDirectory()) {
return FileVisitResult.CONTINUE;
}
// Check if this is an 'archive' directory
final Path relativePath = realPath.relativize(file);
if (relativePath.getNameCount() > 3 && ARCHIVE_DIR_NAME.equals(relativePath.subpath(1, 2).toString())) {
final long lastModifiedTime = getLastModTime(file);
if (lastModifiedTime < oldestDateHolder.get()) {
oldestDateHolder.set(lastModifiedTime);
}
containerState.incrementArchiveCount();
}
return FileVisitResult.CONTINUE;
}
});
return oldestDateHolder.get();
}
};
// If the path didn't exist to begin with, there's no archive directory, so don't bother scanning.
if (pathExists) {
futures.add(executor.submit(scanContainer));
}
}
executor.shutdown();
for (final Future<Long> future : futures) {
try {
final Long oldestDate = future.get();
if (oldestDate < oldestArchiveDate.get()) {
oldestArchiveDate.set(oldestDate);
}
} catch (final ExecutionException | InterruptedException e) {
if (e.getCause() instanceof IOException) {
throw (IOException) e.getCause();
} else {
throw new RuntimeException(e);
}
}
}
containers.clear();
containers.putAll(realPathMap);
}
@Override
public Set<String> getContainerNames() {
return new HashSet<>(containerNames);
}
@Override
public long getContainerCapacity(final String containerName) throws IOException {
final Path path = containers.get(containerName);
if (path == null) {
throw new IllegalArgumentException("No container exists with name " + containerName);
}
long capacity = FileUtils.getContainerCapacity(path);
if(capacity==0) {
throw new IOException("System returned total space of the partition for " + containerName + " is zero byte. "
+ "Nifi can not create a zero sized FileSystemRepository.");
}
return capacity;
}
@Override
public long getContainerUsableSpace(String containerName) throws IOException {
final Path path = containers.get(containerName);
if (path == null) {
throw new IllegalArgumentException("No container exists with name " + containerName);
}
return FileUtils.getContainerUsableSpace(path);
}
@Override
public String getContainerFileStoreName(final String containerName) {
final Path path = containers.get(containerName);
try {
return Files.getFileStore(path).name();
} catch (IOException e) {
return null;
}
}
@Override
public void cleanup() {
for (final Map.Entry<String, Path> entry : containers.entrySet()) {
final String containerName = entry.getKey();
final Path containerPath = entry.getValue();
final File[] sectionFiles = containerPath.toFile().listFiles();
if (sectionFiles != null) {
for (final File sectionFile : sectionFiles) {
removeIncompleteContent(containerName, containerPath, sectionFile.toPath());
}
}
}
}
private void removeIncompleteContent(final String containerName, final Path containerPath, final Path fileToRemove) {
if (Files.isDirectory(fileToRemove)) {
final Path lastPathName = fileToRemove.subpath(1, fileToRemove.getNameCount());
final String fileName = lastPathName.toFile().getName();
if (fileName.equals(ARCHIVE_DIR_NAME)) {
return;
}
final File[] children = fileToRemove.toFile().listFiles();
if (children != null) {
for (final File child : children) {
removeIncompleteContent(containerName, containerPath, child.toPath());
}
}
return;
}
final Path relativePath = containerPath.relativize(fileToRemove);
final Path sectionPath = relativePath.subpath(0, 1);
if (relativePath.getNameCount() < 2) {
return;
}
final Path idPath = relativePath.subpath(1, relativePath.getNameCount());
final String id = idPath.toFile().getName();
final String sectionName = sectionPath.toFile().getName();
final ResourceClaim resourceClaim = resourceClaimManager.newResourceClaim(containerName, sectionName, id, false, false);
if (resourceClaimManager.getClaimantCount(resourceClaim) == 0) {
removeIncompleteContent(fileToRemove);
}
}
private void removeIncompleteContent(final Path fileToRemove) {
String fileDescription = null;
try {
fileDescription = fileToRemove.toFile().getAbsolutePath() + " (" + Files.size(fileToRemove) + " bytes)";
} catch (final IOException e) {
fileDescription = fileToRemove.toFile().getAbsolutePath() + " (unknown file size)";
}
LOG.info("Found unknown file {} in File System Repository; {} file", fileDescription, archiveData ? "archiving" : "removing");
try {
if (archiveData) {
archive(fileToRemove);
} else {
Files.delete(fileToRemove);
}
} catch (final IOException e) {
final String action = archiveData ? "archive" : "remove";
LOG.warn("Unable to {} unknown file {} from File System Repository due to {}", action, fileDescription, e.toString());
LOG.warn("", e);
}
}
@Override
public boolean isActiveResourceClaimsSupported() {
return true;
}
@Override
public Set<ResourceClaim> getActiveResourceClaims(final String containerName) throws IOException {
final Path containerPath = containers.get(containerName);
if (containerPath == null) {
return Collections.emptySet();
}
final ScanForActiveResourceClaims scan = new ScanForActiveResourceClaims(containerPath, containerName, resourceClaimManager, containers.keySet());
Files.walkFileTree(containerPath, scan);
final Set<ResourceClaim> activeResourceClaims = scan.getActiveResourceClaims();
LOG.debug("Obtaining active resource claims, will return a list of {} resource claims for container {}", activeResourceClaims.size(), containerName);
if (LOG.isTraceEnabled()) {
LOG.trace("Listing of resource claims:");
activeResourceClaims.forEach(claim -> LOG.trace(claim.toString()));
}
return activeResourceClaims;
}
private Path getPath(final ContentClaim claim) {
final ResourceClaim resourceClaim = claim.getResourceClaim();
return getPath(resourceClaim);
}
private Path getPath(final ResourceClaim resourceClaim) {
final Path containerPath = containers.get(resourceClaim.getContainer());
if (containerPath == null) {
return null;
}
return containerPath.resolve(resourceClaim.getSection()).resolve(resourceClaim.getId());
}
public Path getPath(final ContentClaim claim, final boolean verifyExists) throws ContentNotFoundException {
final ResourceClaim resourceClaim = claim.getResourceClaim();
final Path containerPath = containers.get(resourceClaim.getContainer());
if (containerPath == null) {
if (verifyExists) {
throw new ContentNotFoundException(claim);
} else {
return null;
}
}
// Create the Path that points to the data
Path resolvedPath = containerPath.resolve(resourceClaim.getSection()).resolve(resourceClaim.getId());
// If the data does not exist, create a Path that points to where the data would exist in the archive directory.
if (!Files.exists(resolvedPath)) {
resolvedPath = getArchivePath(claim.getResourceClaim());
if (verifyExists && !Files.exists(resolvedPath)) {
throw new ContentNotFoundException(claim);
}
}
return resolvedPath;
}
@Override
public ContentClaim create(final boolean lossTolerant) throws IOException {
ResourceClaim resourceClaim;
final long resourceOffset;
final ClaimLengthPair pair = writableClaimQueue.poll();
if (pair == null) {
final long currentIndex = index.incrementAndGet();
String containerName = null;
boolean waitRequired = true;
ContainerState containerState = null;
for (long containerIndex = currentIndex; containerIndex < currentIndex + containers.size(); containerIndex++) {
final long modulatedContainerIndex = containerIndex % containers.size();
containerName = containerNames.get((int) modulatedContainerIndex);
containerState = containerStateMap.get(containerName);
if (!containerState.isWaitRequired()) {
waitRequired = false;
break;
}
}
if (waitRequired) {
containerState.waitForArchiveExpiration();
}
final long modulatedSectionIndex = currentIndex % SECTIONS_PER_CONTAINER;
final String section = String.valueOf(modulatedSectionIndex).intern();
final String claimId = System.currentTimeMillis() + "-" + currentIndex;
resourceClaim = resourceClaimManager.newResourceClaim(containerName, section, claimId, lossTolerant, true);
resourceOffset = 0L;
LOG.debug("Creating new Resource Claim {}", resourceClaim);
// we always append because there may be another ContentClaim using the same resource claim.
// However, we know that we will never write to the same claim from two different threads
// at the same time because we will call create() to get the claim before we write to it,
// and when we call create(), it will remove it from the Queue, which means that no other
// thread will get the same Claim until we've finished writing to it.
final File file = getPath(resourceClaim).toFile();
ByteCountingOutputStream claimStream = new SynchronizedByteCountingOutputStream(new FileOutputStream(file, true), file.length());
writableClaimStreams.put(resourceClaim, claimStream);
incrementClaimantCount(resourceClaim, true);
} else {
resourceClaim = pair.getClaim();
resourceOffset = pair.getLength();
LOG.debug("Reusing Resource Claim {}", resourceClaim);
incrementClaimantCount(resourceClaim, false);
}
final StandardContentClaim scc = new StandardContentClaim(resourceClaim, resourceOffset);
return scc;
}
@Override
public int incrementClaimaintCount(final ContentClaim claim) {
return incrementClaimantCount(claim == null ? null : claim.getResourceClaim(), false);
}
protected int incrementClaimantCount(final ResourceClaim resourceClaim, final boolean newClaim) {
if (resourceClaim == null) {
return 0;
}
return resourceClaimManager.incrementClaimantCount(resourceClaim, newClaim);
}
@Override
public int getClaimantCount(final ContentClaim claim) {
if (claim == null) {
return 0;
}
return resourceClaimManager.getClaimantCount(claim.getResourceClaim());
}
@Override
public int decrementClaimantCount(final ContentClaim claim) {
if (claim == null) {
return 0;
}
return resourceClaimManager.decrementClaimantCount(claim.getResourceClaim());
}
@Override
public boolean remove(final ContentClaim claim) {
if (claim == null) {
return false;
}
return remove(claim.getResourceClaim());
}
private boolean remove(final ResourceClaim claim) {
if (claim == null) {
return false;
}
// If the claim is still in use, we won't remove it.
if (claim.isInUse()) {
return false;
}
Path path = null;
try {
path = getPath(claim);
} catch (final ContentNotFoundException cnfe) {
}
// Ensure that we have no writable claim streams for this resource claim
final ByteCountingOutputStream bcos = writableClaimStreams.remove(claim);
if (bcos != null) {
try {
bcos.close();
} catch (final IOException e) {
LOG.warn("Failed to close Output Stream for {} due to {}", claim, e);
}
}
final File file = path.toFile();
if (!file.delete() && file.exists()) {
LOG.warn("Unable to delete {} at path {}", new Object[]{claim, path});
return false;
}
return true;
}
@Override
public ContentClaim clone(final ContentClaim original, final boolean lossTolerant) throws IOException {
if (original == null) {
return null;
}
final ContentClaim newClaim = create(lossTolerant);
try (final InputStream in = read(original);
final OutputStream out = write(newClaim)) {
StreamUtils.copy(in, out);
} catch (final IOException ioe) {
decrementClaimantCount(newClaim);
remove(newClaim);
throw ioe;
}
return newClaim;
}
@Override
public long merge(final Collection<ContentClaim> claims, final ContentClaim destination, final byte[] header, final byte[] footer, final byte[] demarcator) throws IOException {
if (claims.contains(destination)) {
throw new IllegalArgumentException("destination cannot be within claims");
}
try (final ByteCountingOutputStream out = new ByteCountingOutputStream(write(destination))) {
if (header != null) {
out.write(header);
}
int i = 0;
for (final ContentClaim claim : claims) {
try (final InputStream in = read(claim)) {
StreamUtils.copy(in, out);
}
if (++i < claims.size() && demarcator != null) {
out.write(demarcator);
}
}
if (footer != null) {
out.write(footer);
}
return out.getBytesWritten();
}
}
@Override
public long importFrom(final Path content, final ContentClaim claim) throws IOException {
try (final InputStream in = Files.newInputStream(content, StandardOpenOption.READ)) {
return importFrom(in, claim);
}
}
@Override
public long importFrom(final InputStream content, final ContentClaim claim) throws IOException {
try (final OutputStream out = write(claim, false)) {
return StreamUtils.copy(content, out);
}
}
@Override
public long exportTo(final ContentClaim claim, final Path destination, final boolean append) throws IOException {
if (claim == null) {
if (append) {
return 0L;
}
Files.createFile(destination);
return 0L;
}
try (final InputStream in = read(claim);
final FileOutputStream fos = new FileOutputStream(destination.toFile(), append)) {
final long copied = StreamUtils.copy(in, fos);
if (alwaysSync) {
fos.getFD().sync();
}
return copied;
}
}
@Override
public long exportTo(final ContentClaim claim, final Path destination, final boolean append, final long offset, final long length) throws IOException {
if (claim == null && offset > 0) {
throw new IllegalArgumentException("Cannot specify an offset of " + offset + " for a null claim");
}
if (claim == null) {
if (append) {
return 0L;
}
Files.createFile(destination);
return 0L;
}
final long claimSize = size(claim);
if (offset > claimSize) {
throw new IllegalArgumentException("Offset of " + offset + " exceeds claim size of " + claimSize);
}
try (final InputStream in = read(claim);
final FileOutputStream fos = new FileOutputStream(destination.toFile(), append)) {
if (offset > 0) {
StreamUtils.skip(in, offset);
}
StreamUtils.copy(in, fos, length);
if (alwaysSync) {
fos.getFD().sync();
}
return length;
}
}
@Override
public long exportTo(final ContentClaim claim, final OutputStream destination) throws IOException {
if (claim == null) {
return 0L;
}
try (final InputStream in = read(claim)) {
return StreamUtils.copy(in, destination);
}
}
@Override
public long exportTo(final ContentClaim claim, final OutputStream destination, final long offset, final long length) throws IOException {
if (offset < 0) {
throw new IllegalArgumentException("offset cannot be negative");
}
final long claimSize = size(claim);
if (offset > claimSize) {
throw new IllegalArgumentException("offset of " + offset + " exceeds claim size of " + claimSize);
}
if (offset == 0 && length == claimSize) {
return exportTo(claim, destination);
}
try (final InputStream in = read(claim)) {
StreamUtils.skip(in, offset);
final byte[] buffer = new byte[8192];
int len;
long copied = 0L;
while ((len = in.read(buffer, 0, (int) Math.min(length - copied, buffer.length))) > 0) {
destination.write(buffer, 0, len);
copied += len;
}
return copied;
}
}
@Override
public long size(final ContentClaim claim) throws IOException {
if (claim == null) {
return 0L;
}
// see javadocs for claim.getLength() as to why we do this.
if (claim.getLength() < 0) {
return Files.size(getPath(claim, true)) - claim.getOffset();
}
return claim.getLength();
}
@Override
public InputStream read(final ContentClaim claim) throws IOException {
if (claim == null) {
return new ByteArrayInputStream(new byte[0]);
}
final Path path = getPath(claim, true);
final FileInputStream fis = new FileInputStream(path.toFile());
if (claim.getOffset() > 0L) {
try {
StreamUtils.skip(fis, claim.getOffset());
} catch (final EOFException eof) {
final long resourceClaimBytes;
try {
resourceClaimBytes = Files.size(path);
} catch (final IOException e) {
throw new ContentNotFoundException(claim, "Content Claim has an offset of " + claim.getOffset()
+ " but Resource Claim has fewer than this many bytes (actual length of the resource claim could not be determined)");
}
throw new ContentNotFoundException(claim, "Content Claim has an offset of " + claim.getOffset() + " but Resource Claim " + path + " is only " + resourceClaimBytes + " bytes");
} catch (final IOException ioe) {
IOUtils.closeQuietly(fis);
throw ioe;
}
}
// A claim length of -1 indicates that the claim is still being written to and we don't know
// the length. In this case, we don't limit the Input Stream. If the Length has been populated, though,
// it is possible that the Length could then be extended. However, we do want to avoid ever allowing the
// stream to read past the end of the Content Claim. To accomplish this, we use a LimitedInputStream but
// provide a LongSupplier for the length instead of a Long value. this allows us to continue reading until
// we get to the end of the Claim, even if the Claim grows. This may happen, for instance, if we obtain an
// InputStream for this claim, then read from it, write more to the claim, and then attempt to read again. In
// such a case, since we have written to that same Claim, we should still be able to read those bytes.
if (claim.getLength() >= 0) {
return new LimitedInputStream(fis, claim::getLength);
} else {
return fis;
}
}
@Override
public OutputStream write(final ContentClaim claim) throws IOException {
return write(claim, false);
}
private OutputStream write(final ContentClaim claim, final boolean append) {
StandardContentClaim scc = validateContentClaimForWriting(claim);
ByteCountingOutputStream claimStream = writableClaimStreams.get(scc.getResourceClaim());
final int initialLength = append ? (int) Math.max(0, scc.getLength()) : 0;
final ByteCountingOutputStream bcos = claimStream;
// TODO: Refactor OS implementation out (deduplicate methods, etc.)
final OutputStream out = new ContentRepositoryOutputStream(scc, bcos, initialLength);
LOG.debug("Writing to {}", out);
if (LOG.isTraceEnabled()) {
LOG.trace("Stack trace: ", new RuntimeException("Stack Trace for writing to " + out));
}
return out;
}
public static StandardContentClaim validateContentClaimForWriting(ContentClaim claim) {
if (claim == null) {
throw new NullPointerException("ContentClaim cannot be null");
}
if (!(claim instanceof StandardContentClaim)) {
// we know that we will only create Content Claims that are of type StandardContentClaim, so if we get anything
// else, just throw an Exception because it is not valid for this Repository
throw new IllegalArgumentException("Cannot write to " + claim + " because that Content Claim does belong to this Content Repository");
}
if (claim.getLength() > 0) {
throw new IllegalArgumentException("Cannot write to " + claim + " because it has already been written to.");
}
return (StandardContentClaim) claim;
}
@Override
public void purge() {
// delete all content from repositories
for (final Path path : containers.values()) {
FileUtils.deleteFilesInDir(path.toFile(), null, LOG, true);
}
for (final Path path : containers.values()) {
if (!Files.exists(path)) {
throw new RepositoryPurgeException("File " + path.toFile().getAbsolutePath() + " does not exist");
}
// Try up to 10 times to see if the directory is writable, in case another process (like a
// virus scanner) has the directory temporarily locked
boolean writable = false;
for (int i = 0; i < 10; i++) {
if (Files.isWritable(path)) {
writable = true;
break;
} else {
try {
Thread.sleep(100L);
} catch (final Exception e) {
}
}
}
if (!writable) {
throw new RepositoryPurgeException("File " + path.toFile().getAbsolutePath() + " is not writable");
}
}
resourceClaimManager.purge();
}
private class BinDestructableClaims implements Runnable {
@Override
public void run() {
try {
// Get all of the Destructable Claims and bin them based on their Container. We do this
// because the Container generally maps to a physical partition on the disk, so we want a few