forked from apache/asterixdb
-
Notifications
You must be signed in to change notification settings - Fork 6
/
PersistentLocalResourceRepository.java
605 lines (553 loc) · 27.1 KB
/
PersistentLocalResourceRepository.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
/*
* 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.asterix.transaction.management.resource;
import static org.apache.asterix.common.storage.ResourceReference.getComponentSequence;
import static org.apache.asterix.common.utils.StorageConstants.INDEX_CHECKPOINT_FILE_PREFIX;
import static org.apache.asterix.common.utils.StorageConstants.METADATA_FILE_NAME;
import static org.apache.hyracks.api.exceptions.ErrorCode.CANNOT_CREATE_FILE;
import static org.apache.hyracks.storage.am.lsm.common.impls.AbstractLSMIndexFileManager.COMPONENT_FILES_FILTER;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.asterix.common.dataflow.DatasetLocalResource;
import org.apache.asterix.common.exceptions.AsterixException;
import org.apache.asterix.common.replication.IReplicationManager;
import org.apache.asterix.common.replication.IReplicationStrategy;
import org.apache.asterix.common.replication.ReplicationJob;
import org.apache.asterix.common.storage.DatasetCopyIdentifier;
import org.apache.asterix.common.storage.DatasetResourceReference;
import org.apache.asterix.common.storage.IIndexCheckpointManager;
import org.apache.asterix.common.storage.IIndexCheckpointManagerProvider;
import org.apache.asterix.common.storage.ResourceReference;
import org.apache.asterix.common.storage.ResourceStorageStats;
import org.apache.asterix.common.utils.StorageConstants;
import org.apache.asterix.common.utils.StoragePathUtil;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.hyracks.api.exceptions.HyracksDataException;
import org.apache.hyracks.api.io.FileReference;
import org.apache.hyracks.api.io.IIOManager;
import org.apache.hyracks.api.io.IODeviceHandle;
import org.apache.hyracks.api.io.IPersistedResourceRegistry;
import org.apache.hyracks.api.replication.IReplicationJob.ReplicationExecutionType;
import org.apache.hyracks.api.replication.IReplicationJob.ReplicationJobType;
import org.apache.hyracks.api.replication.IReplicationJob.ReplicationOperation;
import org.apache.hyracks.api.util.IoUtil;
import org.apache.hyracks.storage.am.common.api.ITreeIndexFrame;
import org.apache.hyracks.storage.am.lsm.common.impls.IndexComponentFileReference;
import org.apache.hyracks.storage.am.lsm.common.impls.LSMComponentId;
import org.apache.hyracks.storage.common.ILocalResourceRepository;
import org.apache.hyracks.storage.common.LocalResource;
import org.apache.hyracks.util.ExitUtil;
import org.apache.hyracks.util.file.FileUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
public class PersistentLocalResourceRepository implements ILocalResourceRepository {
private static final Logger LOGGER = LogManager.getLogger();
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final String METADATA_FILE_MASK_NAME =
StorageConstants.MASK_FILE_PREFIX + StorageConstants.METADATA_FILE_NAME;
private static final FilenameFilter LSM_INDEX_FILES_FILTER =
(dir, name) -> !name.startsWith(INDEX_CHECKPOINT_FILE_PREFIX);
private static final FilenameFilter MASK_FILES_FILTER =
(dir, name) -> name.startsWith(StorageConstants.MASK_FILE_PREFIX);
private static final int MAX_CACHED_RESOURCES = 1000;
private static final IOFileFilter METADATA_FILES_FILTER = new IOFileFilter() {
@Override
public boolean accept(File file) {
return file.getName().equals(StorageConstants.METADATA_FILE_NAME);
}
@Override
public boolean accept(File dir, String name) {
return false;
}
};
private static final IOFileFilter METADATA_MASK_FILES_FILTER = new IOFileFilter() {
@Override
public boolean accept(File file) {
return file.getName().equals(METADATA_FILE_MASK_NAME);
}
@Override
public boolean accept(File dir, String name) {
return false;
}
};
private static final IOFileFilter ALL_DIR_FILTER = new IOFileFilter() {
@Override
public boolean accept(File file) {
return true;
}
@Override
public boolean accept(File dir, String name) {
return true;
}
};
// Finals
private final IIOManager ioManager;
private final Cache<String, LocalResource> resourceCache;
// Mutables
private boolean isReplicationEnabled = false;
private Set<String> filesToBeReplicated;
private IReplicationManager replicationManager;
private final Path[] storageRoots;
private final IIndexCheckpointManagerProvider indexCheckpointManagerProvider;
private final IPersistedResourceRegistry persistedResourceRegistry;
public PersistentLocalResourceRepository(IIOManager ioManager,
IIndexCheckpointManagerProvider indexCheckpointManagerProvider,
IPersistedResourceRegistry persistedResourceRegistry) {
this.ioManager = ioManager;
this.indexCheckpointManagerProvider = indexCheckpointManagerProvider;
this.persistedResourceRegistry = persistedResourceRegistry;
storageRoots = new Path[ioManager.getIODevices().size()];
final List<IODeviceHandle> ioDevices = ioManager.getIODevices();
for (int i = 0; i < ioDevices.size(); i++) {
storageRoots[i] =
Paths.get(ioDevices.get(i).getMount().getAbsolutePath(), StorageConstants.STORAGE_ROOT_DIR_NAME);
}
createStorageRoots();
resourceCache = CacheBuilder.newBuilder().maximumSize(MAX_CACHED_RESOURCES).build();
}
@Override
public String toString() {
StringBuilder aString = new StringBuilder().append(PersistentLocalResourceRepository.class.getSimpleName())
.append(Character.LINE_SEPARATOR).append(ioManager.getClass().getSimpleName()).append(':')
.append(Character.LINE_SEPARATOR).append(ioManager.toString()).append(Character.LINE_SEPARATOR)
.append("Cached Resources:").append(Character.LINE_SEPARATOR);
resourceCache.asMap().forEach(
(key, value) -> aString.append(key).append("->").append(value).append(Character.LINE_SEPARATOR));
return aString.toString();
}
@Override
public synchronized LocalResource get(String relativePath) throws HyracksDataException {
LocalResource resource = resourceCache.getIfPresent(relativePath);
if (resource == null) {
FileReference resourceFile = getLocalResourceFileByName(ioManager, relativePath);
if (resourceFile.getFile().exists()) {
resource = readLocalResource(resourceFile.getFile());
resourceCache.put(relativePath, resource);
}
}
return resource;
}
@SuppressWarnings("squid:S1181")
@Override
public synchronized void insert(LocalResource resource) throws HyracksDataException {
String relativePath = getFileName(resource.getPath());
FileReference resourceFile = ioManager.resolve(relativePath);
if (resourceFile.getFile().exists()) {
throw new HyracksDataException("Duplicate resource: " + resourceFile.getAbsolutePath());
}
final File parent = resourceFile.getFile().getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw HyracksDataException.create(CANNOT_CREATE_FILE, parent.getAbsolutePath());
}
// The next block should be all or nothing
try {
createResourceFileMask(resourceFile);
byte[] bytes = OBJECT_MAPPER.writeValueAsBytes(resource.toJson(persistedResourceRegistry));
FileUtil.writeAndForce(Paths.get(resourceFile.getAbsolutePath()), bytes);
indexCheckpointManagerProvider.get(DatasetResourceReference.of(resource)).init(Long.MIN_VALUE, 0,
LSMComponentId.EMPTY_INDEX_LAST_COMPONENT_ID.getMaxId());
deleteResourceFileMask(resourceFile);
} catch (Exception e) {
cleanup(resourceFile);
throw HyracksDataException.create(e);
} catch (Throwable th) {
LOGGER.error("Error creating resource {}", resourceFile, th);
ExitUtil.halt(ExitUtil.EC_ERROR_CREATING_RESOURCES);
}
resourceCache.put(resource.getPath(), resource);
//if replication enabled, send resource metadata info to remote nodes
if (isReplicationEnabled) {
createReplicationJob(ReplicationOperation.REPLICATE, resourceFile);
}
}
@SuppressWarnings("squid:S1181")
private void cleanup(FileReference resourceFile) {
if (resourceFile.getFile().exists()) {
try {
IoUtil.delete(resourceFile);
} catch (Throwable th) {
LOGGER.error("Error cleaning up corrupted resource {}", resourceFile, th);
ExitUtil.halt(ExitUtil.EC_FAILED_TO_DELETE_CORRUPTED_RESOURCES);
}
}
}
@Override
public synchronized void delete(String relativePath) throws HyracksDataException {
FileReference resourceFile = getLocalResourceFileByName(ioManager, relativePath);
if (resourceFile.getFile().exists()) {
if (isReplicationEnabled) {
createReplicationJob(ReplicationOperation.DELETE, resourceFile);
}
final LocalResource localResource = readLocalResource(resourceFile.getFile());
// Invalidate before deleting the file just in case file deletion throws some exception.
// Since it's just a cache invalidation, it should not affect correctness.
resourceCache.invalidate(relativePath);
IoUtil.delete(resourceFile);
// delete all checkpoints
indexCheckpointManagerProvider.get(DatasetResourceReference.of(localResource)).delete();
} else {
throw HyracksDataException.create(org.apache.hyracks.api.exceptions.ErrorCode.RESOURCE_DOES_NOT_EXIST,
relativePath);
}
}
private static FileReference getLocalResourceFileByName(IIOManager ioManager, String resourcePath)
throws HyracksDataException {
String fileName = resourcePath + File.separator + StorageConstants.METADATA_FILE_NAME;
return ioManager.resolve(fileName);
}
public synchronized Map<Long, LocalResource> getResources(Predicate<LocalResource> filter)
throws HyracksDataException {
Map<Long, LocalResource> resourcesMap = new HashMap<>();
for (Path root : storageRoots) {
final Collection<File> files = FileUtils.listFiles(root.toFile(), METADATA_FILES_FILTER, ALL_DIR_FILTER);
try {
for (File file : files) {
final LocalResource localResource = readLocalResource(file);
if (filter.test(localResource)) {
resourcesMap.put(localResource.getId(), localResource);
}
}
} catch (IOException e) {
throw HyracksDataException.create(e);
}
}
return resourcesMap;
}
public Map<Long, LocalResource> loadAndGetAllResources() throws HyracksDataException {
return getResources(p -> true);
}
@Override
public long maxId() throws HyracksDataException {
final Map<Long, LocalResource> allResources = loadAndGetAllResources();
final Optional<Long> max = allResources.keySet().stream().max(Long::compare);
return max.isPresent() ? max.get() : 0;
}
private static String getFileName(String path) {
return path.endsWith(File.separator) ? (path + StorageConstants.METADATA_FILE_NAME)
: (path + File.separator + StorageConstants.METADATA_FILE_NAME);
}
private LocalResource readLocalResource(File file) throws HyracksDataException {
final Path path = Paths.get(file.getAbsolutePath());
try {
final JsonNode jsonNode = OBJECT_MAPPER.readValue(Files.readAllBytes(path), JsonNode.class);
LocalResource resource = (LocalResource) persistedResourceRegistry.deserialize(jsonNode);
if (resource.getVersion() == ITreeIndexFrame.Constants.VERSION) {
return resource;
} else {
throw new AsterixException("Storage version mismatch.");
}
} catch (Exception e) {
throw HyracksDataException.create(e);
}
}
public void setReplicationManager(IReplicationManager replicationManager) {
this.replicationManager = replicationManager;
isReplicationEnabled = replicationManager.isReplicationEnabled();
if (isReplicationEnabled) {
filesToBeReplicated = new HashSet<>();
}
}
private void createReplicationJob(ReplicationOperation operation, FileReference fileRef)
throws HyracksDataException {
filesToBeReplicated.clear();
filesToBeReplicated.add(fileRef.getAbsolutePath());
ReplicationJob job = new ReplicationJob(ReplicationJobType.METADATA, operation, ReplicationExecutionType.SYNC,
filesToBeReplicated);
try {
replicationManager.submitJob(job);
} catch (IOException e) {
throw HyracksDataException.create(e);
}
}
/**
* Deletes physical files of all data verses.
*
* @throws IOException
*/
public void deleteStorageData() throws IOException {
for (Path root : storageRoots) {
final File rootFile = root.toFile();
if (rootFile.exists()) {
FileUtils.deleteDirectory(rootFile);
}
}
createStorageRoots();
}
public Set<Integer> getAllPartitions() throws HyracksDataException {
return loadAndGetAllResources().values().stream().map(LocalResource::getResource)
.map(DatasetLocalResource.class::cast).map(DatasetLocalResource::getPartition)
.collect(Collectors.toSet());
}
public Optional<DatasetResourceReference> getLocalResourceReference(String absoluteFilePath)
throws HyracksDataException {
final String localResourcePath = StoragePathUtil.getIndexFileRelativePath(absoluteFilePath);
final LocalResource lr = get(localResourcePath);
return lr != null ? Optional.of(DatasetResourceReference.of(lr)) : Optional.empty();
}
/**
* Gets a set of files for the indexes in partition {@code partition}. Each file points
* to where the index's files are stored.
*
* @param partition
* @return The set of indexes files
* @throws HyracksDataException
*/
public Set<File> getPartitionIndexes(int partition) throws HyracksDataException {
final Map<Long, LocalResource> partitionResourcesMap = getResources(resource -> {
DatasetLocalResource dsResource = (DatasetLocalResource) resource.getResource();
return dsResource.getPartition() == partition;
});
Set<File> indexes = new HashSet<>();
for (LocalResource localResource : partitionResourcesMap.values()) {
indexes.add(ioManager.resolve(localResource.getPath()).getFile());
}
return indexes;
}
public Map<Long, LocalResource> getPartitionResources(int partition) throws HyracksDataException {
return getResources(resource -> {
DatasetLocalResource dsResource = (DatasetLocalResource) resource.getResource();
return dsResource.getPartition() == partition;
});
}
public List<String> getPartitionReplicatedFiles(int partition, IReplicationStrategy strategy)
throws HyracksDataException {
final List<String> partitionReplicatedFiles = new ArrayList<>();
final Set<File> replicatedIndexes = new HashSet<>();
final Map<Long, LocalResource> partitionResources = getPartitionResources(partition);
for (LocalResource lr : partitionResources.values()) {
DatasetLocalResource datasetLocalResource = (DatasetLocalResource) lr.getResource();
if (strategy.isMatch(datasetLocalResource.getDatasetId())) {
replicatedIndexes.add(ioManager.resolve(lr.getPath()).getFile());
}
}
for (File indexDir : replicatedIndexes) {
partitionReplicatedFiles.addAll(getIndexFiles(indexDir));
}
return partitionReplicatedFiles;
}
public long getReplicatedIndexesMaxComponentId(int partition, IReplicationStrategy strategy)
throws HyracksDataException {
long maxComponentId = LSMComponentId.MIN_VALID_COMPONENT_ID;
final Map<Long, LocalResource> partitionResources = getPartitionResources(partition);
for (LocalResource lr : partitionResources.values()) {
DatasetLocalResource datasetLocalResource = (DatasetLocalResource) lr.getResource();
if (strategy.isMatch(datasetLocalResource.getDatasetId())) {
final IIndexCheckpointManager indexCheckpointManager =
indexCheckpointManagerProvider.get(DatasetResourceReference.of(lr));
maxComponentId = Math.max(maxComponentId, indexCheckpointManager.getLatest().getLastComponentId());
}
}
return maxComponentId;
}
private List<String> getIndexFiles(File indexDir) {
final List<String> indexFiles = new ArrayList<>();
if (indexDir.isDirectory()) {
File[] indexFilteredFiles = indexDir.listFiles(LSM_INDEX_FILES_FILTER);
if (indexFilteredFiles != null) {
Stream.of(indexFilteredFiles).map(File::getAbsolutePath).forEach(indexFiles::add);
}
}
return indexFiles;
}
private void createStorageRoots() {
for (Path root : storageRoots) {
try {
Files.createDirectories(root);
} catch (IOException e) {
throw new IllegalStateException("Failed to create storage root directory at " + root, e);
}
}
}
public void cleanup(int partition) throws HyracksDataException {
final Set<File> partitionIndexes = getPartitionIndexes(partition);
try {
for (File index : partitionIndexes) {
deleteIndexMaskedFiles(index);
if (isValidIndex(index)) {
deleteIndexInvalidComponents(index);
}
}
} catch (IOException | ParseException e) {
throw HyracksDataException.create(e);
}
}
public List<ResourceStorageStats> getStorageStats() throws HyracksDataException {
final List<DatasetResourceReference> allResources = loadAndGetAllResources().values().stream()
.map(DatasetResourceReference::of).collect(Collectors.toList());
final List<ResourceStorageStats> resourcesStats = new ArrayList<>();
for (DatasetResourceReference res : allResources) {
final ResourceStorageStats resourceStats = getResourceStats(res);
if (resourceStats != null) {
resourcesStats.add(resourceStats);
}
}
return resourcesStats;
}
public void deleteCorruptedResources() throws HyracksDataException {
for (Path root : storageRoots) {
final Collection<File> metadataMaskFiles =
FileUtils.listFiles(root.toFile(), METADATA_MASK_FILES_FILTER, ALL_DIR_FILTER);
for (File metadataMaskFile : metadataMaskFiles) {
final File resourceFile = new File(metadataMaskFile.getParent(), METADATA_FILE_NAME);
if (resourceFile.exists()) {
IoUtil.delete(resourceFile);
}
IoUtil.delete(metadataMaskFile);
}
}
}
private void deleteIndexMaskedFiles(File index) throws IOException {
File[] masks = index.listFiles(MASK_FILES_FILTER);
if (masks != null) {
for (File mask : masks) {
deleteIndexMaskedFiles(index, mask);
// delete the mask itself
Files.delete(mask.toPath());
}
}
}
private boolean isValidIndex(File index) throws IOException {
// any index without any checkpoint files is invalid
// this can happen if a crash happens when the index metadata file is created
// but before the initial checkpoint is persisted. The index metadata file will
// be deleted and recreated when the index is created again
return getIndexCheckpointManager(index).getCheckpointCount() != 0;
}
private void deleteIndexInvalidComponents(File index) throws IOException, ParseException {
final File[] indexComponentFiles = index.listFiles(COMPONENT_FILES_FILTER);
if (indexComponentFiles == null) {
throw new IOException(index + " doesn't exist or an IO error occurred");
}
final long validComponentSequence = getIndexCheckpointManager(index).getValidComponentSequence();
for (File componentFile : indexComponentFiles) {
// delete any file with start or end sequence > valid component sequence
final long fileStart = IndexComponentFileReference.of(componentFile.getName()).getSequenceStart();
final long fileEnd = IndexComponentFileReference.of(componentFile.getName()).getSequenceEnd();
if (fileStart > validComponentSequence || fileEnd > validComponentSequence) {
LOGGER.warn(() -> "Deleting invalid component file " + componentFile.getAbsolutePath()
+ " based on valid sequence " + validComponentSequence);
Files.delete(componentFile.toPath());
}
}
}
private IIndexCheckpointManager getIndexCheckpointManager(File index) throws HyracksDataException {
final String indexFile = Paths.get(index.getAbsolutePath(), StorageConstants.METADATA_FILE_NAME).toString();
final ResourceReference indexRef = ResourceReference.of(indexFile);
return indexCheckpointManagerProvider.get(indexRef);
}
private void deleteIndexMaskedFiles(File index, File mask) throws IOException {
if (!mask.getName().startsWith(StorageConstants.MASK_FILE_PREFIX)) {
throw new IllegalArgumentException("Unrecognized mask file: " + mask);
}
File[] maskedFiles;
if (isComponentMask(mask)) {
final String componentId = mask.getName().substring(StorageConstants.COMPONENT_MASK_FILE_PREFIX.length());
maskedFiles = index.listFiles((dir, name) -> name.startsWith(componentId));
} else {
final String maskedFileName = mask.getName().substring(StorageConstants.MASK_FILE_PREFIX.length());
maskedFiles = index.listFiles((dir, name) -> name.equals(maskedFileName));
}
if (maskedFiles != null) {
for (File maskedFile : maskedFiles) {
LOGGER.info(() -> "deleting masked file: " + maskedFile.getAbsolutePath());
Files.delete(maskedFile.toPath());
}
}
}
private ResourceStorageStats getResourceStats(DatasetResourceReference resource) {
try {
final FileReference resolvedPath = ioManager.resolve(resource.getRelativePath().toString());
long totalSize = 0;
final File[] indexFiles = resolvedPath.getFile().listFiles();
final Map<String, Long> componentsStats = new HashMap<>();
if (indexFiles != null) {
for (File file : indexFiles) {
long fileSize = file.length();
totalSize += fileSize;
if (isComponentFile(resolvedPath.getFile(), file.getName())) {
String componentSeq = getComponentSequence(file.getAbsolutePath());
componentsStats.put(componentSeq, componentsStats.getOrDefault(componentSeq, 0L) + fileSize);
}
}
}
return new ResourceStorageStats(resource, componentsStats, totalSize);
} catch (Exception e) {
LOGGER.warn("Couldn't get stats for resource {}", resource.getRelativePath(), e);
}
return null;
}
public long getDatasetSize(DatasetCopyIdentifier datasetIdentifier) throws HyracksDataException {
long totalSize = 0;
final Map<Long, LocalResource> dataverse = getResources(lr -> {
final ResourceReference resourceReference = ResourceReference.ofIndex(lr.getPath());
return datasetIdentifier.isMatch(resourceReference);
});
final List<DatasetResourceReference> allResources =
dataverse.values().stream().map(DatasetResourceReference::of).collect(Collectors.toList());
for (DatasetResourceReference res : allResources) {
final ResourceStorageStats resourceStats = getResourceStats(res);
if (resourceStats != null) {
totalSize += resourceStats.getTotalSize();
}
}
return totalSize;
}
private void createResourceFileMask(FileReference resourceFile) throws HyracksDataException {
Path maskFile = getResourceMaskFilePath(resourceFile);
try {
Files.createFile(maskFile);
} catch (IOException e) {
throw HyracksDataException.create(e);
}
}
private void deleteResourceFileMask(FileReference resourceFile) throws HyracksDataException {
Path maskFile = getResourceMaskFilePath(resourceFile);
IoUtil.delete(maskFile);
}
private Path getResourceMaskFilePath(FileReference resourceFile) {
return Paths.get(resourceFile.getFile().getParentFile().getAbsolutePath(), METADATA_FILE_MASK_NAME);
}
private static boolean isComponentMask(File mask) {
return mask.getName().startsWith(StorageConstants.COMPONENT_MASK_FILE_PREFIX);
}
private static boolean isComponentFile(File indexDir, String fileName) {
return COMPONENT_FILES_FILTER.accept(indexDir, fileName);
}
}