Skip to content

Commit

Permalink
Revert 'deleteObject' order of operations to debug changes
Browse files Browse the repository at this point in the history
  • Loading branch information
doulikecookiedough committed May 13, 2024
1 parent 3189118 commit a76fe7a
Showing 1 changed file with 61 additions and 55 deletions.
116 changes: 61 additions & 55 deletions src/main/java/org/dataone/hashstore/filehashstore/FileHashStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -1113,66 +1113,11 @@ public void deleteObject(String idType, String id) throws IllegalArgumentExcepti

// Before we begin deleting files, we handle orphaned files scenarios
try {
synchronized (referenceLockedPids) {
while (referenceLockedPids.contains(pid)) {
try {
referenceLockedPids.wait(TIME_OUT_MILLISEC);

} catch (InterruptedException ie) {
String errMsg =
"FileHashStore.deleteObject - referenceLockedPids lock was "
+ "interrupted while waiting to delete objects for pid: " + pid
+ ". InterruptedException: " + ie.getMessage();
logFileHashStore.error(errMsg);
throw new InterruptedException(errMsg);
}
}
logFileHashStore.debug(
"FileHashStore.deleteObject - Synchronizing referenceLockedPids for pid: "
+ pid);
referenceLockedPids.add(pid);
}
// Begin by looking for the cid and confirming state
// If there is an issue with finding an object (ex. orphaned reference files),
// custom exceptions will be thrown and handled in the catch blocks
cid = findObject(id);

// Proceed with comprehensive deletion - cid exists, nothing out of place
// Get all the required paths to streamline deletion process
// Permanent address of the object
Path objRealPath = getExpectedPath(pid, "object", null);
// Cid refs file
Path absCidRefsPath = getExpectedPath(cid, "refs", HashStoreIdTypes.cid.getName());
// Pid refs file
Path absPidRefsPath = getExpectedPath(pid, "refs", HashStoreIdTypes.pid.getName());

// Rename metadata documents for deletion
for (Path metadataDoc : metadataDocPaths) {
deleteList.add(FileHashStoreUtility.renamePathForDeletion(metadataDoc));
}

// Rename pid refs file for deletion
deleteList.add(FileHashStoreUtility.renamePathForDeletion(absPidRefsPath));
// Remove pid from cid refs file
updateRefsFile(pid, absCidRefsPath, "remove");
// Delete obj and cid refs file only if the cid refs file is empty
if (Files.size(absCidRefsPath) == 0) {
// Rename empty cid refs file for deletion
deleteList.add(FileHashStoreUtility.renamePathForDeletion(absCidRefsPath));
// Rename actual object for deletion
deleteList.add(FileHashStoreUtility.renamePathForDeletion(objRealPath));
} else {
String warnMsg = "FileHashStore.deleteObject - cid referenced by pid: " + pid
+ " is not empty (refs exist for cid). Skipping object deletion.";
logFileHashStore.warn(warnMsg);
}
// Delete all related/relevant items with the least amount of delay
FileHashStoreUtility.deleteListItems(deleteList);
logFileHashStore.info(
"FileHashStore.deleteObject - File and references deleted for: " + pid
+ " with object address: " + objRealPath
);

} catch (OrphanPidRefsFileException oprfe) {
// `findObject` throws this exception when the cid refs file doesn't exist,
// so we only need to delete the pid refs file and related metadata documents
Expand All @@ -1191,6 +1136,7 @@ public void deleteObject(String idType, String id) throws IllegalArgumentExcepti
"FileHashStore.deleteObject - Cid refs file does not exist for pid: " + pid
+ ". Deleted orphan pid refs file and metadata.";
logFileHashStore.warn(warnMsg);
return;

} catch (OrphanRefsFilesException orfe) {
// `findObject` throws this exception when the pid and cid refs file exists,
Expand Down Expand Up @@ -1223,6 +1169,7 @@ public void deleteObject(String idType, String id) throws IllegalArgumentExcepti
+ " does not exist, but pid and cid reference file found for pid: " + pid
+ ". Deleted pid and cid ref files and metadata.";
logFileHashStore.warn(warnMsg);
return;

} catch (PidNotFoundInCidRefsFileException pnficrfe) {
// `findObject` throws this exception when both the pid and cid refs file exists
Expand All @@ -1243,6 +1190,65 @@ public void deleteObject(String idType, String id) throws IllegalArgumentExcepti
"FileHashStore.deleteObject - Pid not found in expected cid refs file for pid: "
+ pid + ". Deleted orphan pid refs file and metadata.";
logFileHashStore.warn(warnMsg);
return;
}

try {
synchronized (referenceLockedPids) {
while (referenceLockedPids.contains(pid)) {
try {
referenceLockedPids.wait(TIME_OUT_MILLISEC);

} catch (InterruptedException ie) {
String errMsg =
"FileHashStore.deleteObject - referenceLockedPids lock was "
+ "interrupted while waiting to delete objects for pid: " + pid
+ ". InterruptedException: " + ie.getMessage();
logFileHashStore.error(errMsg);
throw new InterruptedException(errMsg);
}
}
logFileHashStore.debug(
"FileHashStore.deleteObject - Synchronizing referenceLockedPids for pid: "
+ pid);
referenceLockedPids.add(pid);
}

// Proceed with comprehensive deletion - cid exists, nothing out of place
// Get all the required paths to streamline deletion process
// Permanent address of the object
Path objRealPath = getExpectedPath(pid, "object", null);
// Cid refs file
Path absCidRefsPath = getExpectedPath(cid, "refs", HashStoreIdTypes.cid.getName());
// Pid refs file
Path absPidRefsPath = getExpectedPath(pid, "refs", HashStoreIdTypes.pid.getName());

// Rename metadata documents for deletion
for (Path metadataDoc : metadataDocPaths) {
deleteList.add(FileHashStoreUtility.renamePathForDeletion(metadataDoc));
}

// Rename pid refs file for deletion
deleteList.add(FileHashStoreUtility.renamePathForDeletion(absPidRefsPath));
// Remove pid from cid refs file
updateRefsFile(pid, absCidRefsPath, "remove");
// Delete obj and cid refs file only if the cid refs file is empty
if (Files.size(absCidRefsPath) == 0) {
// Rename empty cid refs file for deletion
deleteList.add(FileHashStoreUtility.renamePathForDeletion(absCidRefsPath));
// Rename actual object for deletion
deleteList.add(FileHashStoreUtility.renamePathForDeletion(objRealPath));
} else {
String warnMsg = "FileHashStore.deleteObject - cid referenced by pid: " + pid
+ " is not empty (refs exist for cid). Skipping object deletion.";
logFileHashStore.warn(warnMsg);
}
// Delete all related/relevant items with the least amount of delay
FileHashStoreUtility.deleteListItems(deleteList);
logFileHashStore.info(
"FileHashStore.deleteObject - File and references deleted for: " + pid
+ " with object address: " + objRealPath
);

} finally {
// Release lock
Expand Down

0 comments on commit a76fe7a

Please sign in to comment.