RED-6860: fixed transaction timeout issue by loading files eagerly and making... #47

Merged
ali.oezyetimoglu1 merged 1 commits from RED-6860-2 into master 2023-07-07 17:22:59 +02:00
2 changed files with 16 additions and 17 deletions

View File

@ -64,7 +64,7 @@ public class DownloadStatusEntity {
@ManyToOne(fetch = FetchType.EAGER)
DossierEntity dossier;
@ManyToMany
@ManyToMany(fetch = FetchType.EAGER)
@Fetch(FetchMode.SUBSELECT)
List<FileEntity> files = new ArrayList<>();

View File

@ -132,7 +132,6 @@ public class DownloadPreparationService {
}
@Transactional
public void createDownload(RedactionResultMessage reportResultMessage) {
DownloadStatusEntity downloadStatus = downloadStatusPersistenceService.getStatus(reportResultMessage.getDownloadId());
@ -142,7 +141,7 @@ public class DownloadPreparationService {
try (FileSystemBackedArchiver fileSystemBackedArchiver = new FileSystemBackedArchiver()) {
generateAndAddFiles(downloadStatus, reportResultMessage, fileSystemBackedArchiver);
addReports(reportResultMessage.getDownloadId(), storedFileInformations, fileSystemBackedArchiver);
addReports(reportResultMessage.getDownloadId(), downloadStatus, storedFileInformations, fileSystemBackedArchiver);
storeZipFile(downloadStatus, fileSystemBackedArchiver);
updateStatusToReady(downloadStatus, fileSystemBackedArchiver);
@ -175,15 +174,13 @@ public class DownloadPreparationService {
int i = 1;
long fileGenerationStart = System.currentTimeMillis();
var fileIds = downloadStatus.getFiles().stream().map(FileEntity::getId).collect(Collectors.toList());
for (String fileId : fileIds) {
for (FileEntity file : downloadStatus.getFiles()) {
long start = System.currentTimeMillis();
FileEntity fileStatus = fileStatusPersistenceService.getStatus(fileId);
byte[] original = fileManagementStorageService.getStoredObjectBytes(fileStatus.getDossierId(), fileId, FileType.ORIGIN);
byte[] original = fileManagementStorageService.getStoredObjectBytes(file.getDossierId(), file.getId(), FileType.ORIGIN);
var isFileApproved = WorkflowStatus.APPROVED.equals(fileStatus.getWorkflowStatus());
String filename = isFileApproved ? fileStatus.getFilename() : "UNAPPROVED_" + fileStatus.getFilename();
var isFileApproved = WorkflowStatus.APPROVED.equals(file.getWorkflowStatus());
String filename = isFileApproved ? file.getFilename() : "UNAPPROVED_" + file.getFilename();
for (DownloadFileType downloadFileType : downloadStatus.getDownloadFileTypes()) {
if (downloadFileType.name().equals(DownloadFileType.ORIGINAL.name())) {
@ -191,21 +188,21 @@ public class DownloadPreparationService {
}
if (downloadFileType.name().equals(DownloadFileType.PREVIEW.name())) {
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Preview", addSuffix(filename, "highlighted"), //
getPreview(fileId, reportResultMessage.getRedactionResultDetails())));
getPreview(file.getId(), reportResultMessage.getRedactionResultDetails())));
}
if (downloadFileType.name().equals(DownloadFileType.DELTA_PREVIEW.name())) {
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Delta Preview", addSuffix(filename, "delta_highlighted"), //
getDeltaPreview(fileId, reportResultMessage.getRedactionResultDetails())));
getDeltaPreview(file.getId(), reportResultMessage.getRedactionResultDetails())));
}
if (downloadFileType.name().equals(DownloadFileType.REDACTED.name()) && isFileApproved) {
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Redacted", addSuffix(fileStatus.getFilename(), "redacted"), //
getRedacted(fileId, reportResultMessage.getRedactionResultDetails())));
fileSystemBackedArchiver.addEntry(new FileSystemBackedArchiver.ArchiveModel("Redacted", addSuffix(file.getFilename(), "redacted"), //
getRedacted(file.getId(), reportResultMessage.getRedactionResultDetails())));
}
}
log.info("Successfully added file {}/{} for downloadId {}, took {}", i, fileIds.size(), downloadStatus.getStorageId(), System.currentTimeMillis() - start);
log.info("Successfully added file {}/{} for downloadId {}, took {}", i, downloadStatus.getFiles().size(), downloadStatus.getStorageId(), System.currentTimeMillis() - start);
i++;
}
log.info("Successfully added {} files for downloadId {}, took {}", fileIds, downloadStatus.getStorageId(), System.currentTimeMillis() - fileGenerationStart);
log.info("Successfully added {} files for downloadId {}, took {}", downloadStatus.getFiles().stream().map(FileEntity::getId).toList(), downloadStatus.getStorageId(), System.currentTimeMillis() - fileGenerationStart);
}
@ -242,16 +239,18 @@ public class DownloadPreparationService {
}
private void addReports(String downloadId, List<StoredFileInformation> storedFileInformations, FileSystemBackedArchiver fileSystemBackedArchiver) {
private void addReports(String downloadId, DownloadStatusEntity downloadStatus, List<StoredFileInformation> storedFileInformations, FileSystemBackedArchiver fileSystemBackedArchiver) {
long addReportsStart = System.currentTimeMillis();
Map<String, FileEntity> fileById = downloadStatus.getFiles().stream().collect(Collectors.toMap(FileEntity::getId, a -> a));
for (StoredFileInformation storedFileInformation : storedFileInformations) {
FileEntity fileStatus = null;
if (storedFileInformation.getFileId() != null) {
fileStatus = fileStatusPersistenceService.getStatus(storedFileInformation.getFileId());
fileStatus = fileById.get(storedFileInformation.getFileId());
}
ReportTemplateEntity reportTemplate = reportTemplatePersistenceService.find(storedFileInformation.getTemplateId());