Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pivotal ID # 184822139: Delete Submission Folder After Resubmission #707

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ interface FileStorageService {

fun persistSubmissionFile(sub: ExtSubmission, file: ExtFile): ExtFile

fun deleteSubmissionFiles(sub: ExtSubmission)

fun deleteSubmissionFile(sub: ExtSubmission, file: ExtFile)

fun deleteFtpFile(sub: ExtSubmission, file: ExtFile)
fun deleteSubmissionFiles(sub: ExtSubmission)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import ebi.ac.uk.extended.model.ExtSubmission
internal interface FilesService {
fun persistSubmissionFile(sub: ExtSubmission, file: ExtFile): ExtFile

fun deleteSubmissionFiles(sub: ExtSubmission)

fun deleteSubmissionFile(sub: ExtSubmission, file: ExtFile)

fun deleteFtpFile(sub: ExtSubmission, file: ExtFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@ import ebi.ac.uk.extended.model.FireFile
import ebi.ac.uk.extended.model.NfsFile
import ebi.ac.uk.extended.model.asFireFile
import ebi.ac.uk.extended.model.expectedPath
import mu.KotlinLogging
import uk.ac.ebi.extended.serialization.service.ExtSerializationService
import uk.ac.ebi.extended.serialization.service.fileSequence
import uk.ac.ebi.fire.client.integration.web.FireClient

private val logger = KotlinLogging.logger {}

class FireFilesService(
private val client: FireClient,
private val serializationService: ExtSerializationService,
) : FilesService {
/**
* Get or persist the given ext file from FIRE. Note that this method assumes that all the fire files belonging to
Expand Down Expand Up @@ -71,18 +65,4 @@ class FireFilesService(
override fun deleteFtpFile(sub: ExtSubmission, file: ExtFile) {
// No need to delete FTP links on FIRE as file deleting complete this
}

override fun deleteSubmissionFiles(sub: ExtSubmission) {
fun deleteFile(index: Int, file: FireFile) {
logger.info { "${sub.accNo} ${sub.owner} Deleting file $index, path='${file.filePath}'" }
deleteSubmissionFile(sub, file)
}

logger.info { "${sub.accNo} ${sub.owner} Started cleaning submission files for ${sub.accNo}" }
serializationService
.fileSequence(sub)
.filterIsInstance(FireFile::class.java)
.forEachIndexed { index, file -> deleteFile(index, file) }
logger.info { "${sub.accNo} ${sub.owner} Finished cleaning submission files for ${sub.accNo}" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ class NfsFilesService(
return getOrCreateFolder(submissionPath, permissions).toFile()
}

override fun deleteSubmissionFiles(sub: ExtSubmission) {
deleteFtpFolder(sub)
deleteSubFolder(sub)
}

override fun deleteSubmissionFile(sub: ExtSubmission, file: ExtFile) {
require(file is NfsFile) { "NfsFilesService should only handle NfsFile" }
FileUtils.deleteFile(folderResolver.getSubFolder(sub.relPath).resolve(file.relPath).toFile())

val subDirectory = folderResolver.getSubFolder(sub.relPath)
val subFile = subDirectory.resolve(file.relPath).toFile()
FileUtils.deleteFile(subFile)
}

override fun deleteFtpFile(sub: ExtSubmission, file: ExtFile) {
Expand All @@ -78,16 +76,4 @@ class NfsFilesService(
FileUtils.deleteFile(subFolder.resolve(file.relPath).toFile())
logger.info { "${sub.accNo} ${sub.owner} Finished un-publishing files of submission ${sub.accNo} on NFS" }
}

private fun deleteSubFolder(sub: ExtSubmission) {
logger.info { "${sub.accNo} ${sub.owner} Started deleting files of submission ${sub.accNo} on NFS" }
FileUtils.deleteFile(folderResolver.getSubFolder(sub.relPath).toFile())
logger.info { "${sub.accNo} ${sub.owner} Finished deleting files of submission ${sub.accNo} on NFS" }
}

private fun deleteFtpFolder(sub: ExtSubmission) {
logger.info { "${sub.accNo} ${sub.owner} Started un-publishing files of submission ${sub.accNo} on NFS" }
FileUtils.deleteFile(folderResolver.getSubmissionFtpFolder(sub.relPath).toFile())
logger.info { "${sub.accNo} ${sub.owner} Finished un-publishing files of submission ${sub.accNo} on NFS" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ import ebi.ac.uk.extended.model.ExtSubmission
import ebi.ac.uk.extended.model.StorageMode
import ebi.ac.uk.extended.model.StorageMode.FIRE
import ebi.ac.uk.extended.model.StorageMode.NFS
import mu.KotlinLogging
import uk.ac.ebi.extended.serialization.service.ExtSerializationService
import uk.ac.ebi.extended.serialization.service.fileSequence

private val logger = KotlinLogging.logger {}

@Suppress("LongParameterList")
class StorageService(
private val fireFtpService: FireFtpService,
private val fireFilesService: FireFilesService,
private val nfsFtpService: NfsFtpService,
private val nfsFilesService: NfsFilesService,
private val serializationService: ExtSerializationService,
) : FileStorageService {
override fun persistSubmissionFile(sub: ExtSubmission, file: ExtFile): ExtFile =
when (sub.storageMode) {
Expand All @@ -31,21 +37,19 @@ class StorageService(
}
}

override fun deleteFtpFile(sub: ExtSubmission, file: ExtFile) =
when (sub.storageMode) {
FIRE -> fireFilesService.deleteFtpFile(sub, file)
NFS -> nfsFilesService.deleteFtpFile(sub, file)
override fun deleteSubmissionFile(sub: ExtSubmission, file: ExtFile) = when (sub.storageMode) {
FIRE -> {
fireFilesService.deleteSubmissionFile(sub, file)
fireFilesService.deleteFtpFile(sub, file)
}

override fun deleteSubmissionFile(sub: ExtSubmission, file: ExtFile) =
when (sub.storageMode) {
FIRE -> fireFilesService.deleteSubmissionFile(sub, file)
NFS -> nfsFilesService.deleteSubmissionFile(sub, file)
NFS -> {
nfsFilesService.deleteSubmissionFile(sub, file)
nfsFilesService.deleteFtpFile(sub, file)
}
}

override fun deleteSubmissionFiles(sub: ExtSubmission) =
when (sub.storageMode) {
FIRE -> fireFilesService.deleteSubmissionFiles(sub)
NFS -> nfsFilesService.deleteSubmissionFiles(sub)
}
override fun deleteSubmissionFiles(sub: ExtSubmission) {
serializationService.fileSequence(sub).forEach { file -> deleteSubmissionFile(sub, file) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import uk.ac.ebi.extended.serialization.service.ExtSerializationService
import uk.ac.ebi.extended.serialization.service.fileSequence
import uk.ac.ebi.fire.client.integration.web.FireClient
import uk.ac.ebi.fire.client.model.FileSystemEntry
import uk.ac.ebi.fire.client.model.FireApiFile
Expand All @@ -34,7 +33,7 @@ internal class FireFilesServiceTest(
@MockK private val submission: ExtSubmission,
@MockK private val serializationService: ExtSerializationService,
) {
private val testInstance = FireFilesService(fireClient, serializationService)
private val testInstance = FireFilesService(fireClient)

@BeforeEach
fun beforeEach() {
Expand Down Expand Up @@ -142,22 +141,6 @@ internal class FireFilesServiceTest(
mockkStatic("uk.ac.ebi.extended.serialization.service.ExtSerializationServiceExtKt")
}

@Test
fun `delete submission files`(
@MockK submission: ExtSubmission,
) {
val file = fireFile(md5 = "md1", firePath = "path_1")

every { submission.accNo } returns "S-BSST1"
every { submission.owner } returns "user@mail.org"
every { serializationService.fileSequence(submission) } returns sequenceOf(file)
every { fireClient.delete(file.fireId) } answers { nothing }

testInstance.deleteSubmissionFiles(submission)

verify(exactly = 1) { fireClient.delete(file.fireId) }
}

@Test
fun `delete submission file`(
@MockK submission: ExtSubmission,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ class NfsFilesServiceTest(
assertThat(Files.exists(Paths.get("${subFolder.absolutePath}/${sub.relPath}/${fireFile.relPath}"))).isTrue()
}

@Test
fun `delete submission files`() {
val subFolder = subFolder.createDirectory("S-BSST1")
val ftpFolder = ftpFolder.createDirectory("S-BSST1")
val sub = basicExtSubmission.copy(relPath = "S-BSST1")

testInstance.deleteSubmissionFiles(sub)

assertThat(Files.exists(subFolder.toPath())).isFalse()
assertThat(Files.exists(ftpFolder.toPath())).isFalse()
}

@Test
fun `delete ftp links`() {
val ftpFolder = ftpFolder.createDirectory("S-BSST2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ class FilePersistenceConfig(
fireFilesService: FireFilesService,
nfsFtpService: NfsFtpService,
nfsFilesService: NfsFilesService,
): FileStorageService = StorageService(fireFtpService, fireFilesService, nfsFtpService, nfsFilesService)
extSerializationService: ExtSerializationService,
): FileStorageService =
StorageService(fireFtpService, fireFilesService, nfsFtpService, nfsFilesService, extSerializationService)

@Bean
fun nfsFtpService(): NfsFtpService = NfsFtpService(folderResolver)
Expand All @@ -48,9 +50,7 @@ class FilePersistenceConfig(
fun fireFtpService(): FireFtpService = FireFtpService(fireClient)

@Bean
fun fireFileService(
serializationService: ExtSerializationService,
): FireFilesService = FireFilesService(fireClient, serializationService)
fun fireFileService(): FireFilesService = FireFilesService(fireClient)

@Bean
fun pageTabService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class SubmissionConfig(
extSubmissionSubmitter,
submissionSubmitter,
eventsPublisherService,
fileStorageService,
submissionPersistenceService,
fileStorageService
)

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class SubmissionService(
private val extSubmissionSubmitter: ExtSubmissionSubmitter,
private val submissionSubmitter: SubmissionSubmitter,
private val eventsPublisherService: EventsPublisherService,
private val fileStorageService: FileStorageService,
private val submissionPersistenceService: SubmissionPersistenceService,
private val fileStorageService: FileStorageService,
) {
fun submit(rqt: SubmitRequest): ExtSubmission {
logger.info { "${rqt.accNo} ${rqt.owner} Received sync submit request with draft key '${rqt.draftKey}'" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ app:
username: # FIRE user
password: # FIRE password
version: v1.1
s3AccessKey: # FIRE s3 access key
s3SecretKey: # FIRE s3 access key
s3region: anyRegion # Fire s3 region
s3endpoint: # Fire s3 region
s3bucket: # Fire s3 region
retry:
maxAttempts: 20
initialInterval: 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class SubmissionRequestCleaner(
fun deleteFile(index: Int, file: ExtFile) {
logger.info { "${current.accNo} ${current.owner} Deleting file $index, path='${file.filePath}'" }
storageService.deleteSubmissionFile(current, file)
storageService.deleteFtpFile(current, file)
}

fun shouldDelete(newFiles: Map<String, FileEntry>, existing: ExtFile): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,24 @@ class SubmissionRequestFinalizer(
return sub
}

private fun deleteRemainingFiles(sub: ExtSubmission, previous: ExtSubmission) {
private fun deleteRemainingFiles(current: ExtSubmission?, previous: ExtSubmission) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is the logic that should be moved to the FileStorageService and then be used in both places since it makes more sense to me to have a storage services that deletes files that later is used by both, the RequestFinalizer and the SubmissionService than the SubmissionService using the RequestFinalizer directly

fun deleteFile(index: Int, file: ExtFile) {
logger.info { "${previous.accNo} ${previous.owner} Deleting file $index, path='${file.filePath}'" }
storageService.deleteSubmissionFile(previous, file)
}

val subFiles = subFilesSet(sub)
logger.info { "${sub.accNo} ${sub.owner} Started deleting remaining submission files" }
val subFiles = subFilesSet(current)
logger.info { "${previous.accNo} ${previous.owner} Started deleting remaining submission files" }
serializationService.fileSequence(previous)
.filter { subFiles.contains(it.filePath).not() || it.storageMode != sub.storageMode }
.filter { subFiles.contains(it.filePath).not() || it.storageMode != current?.storageMode }
.forEachIndexed { index, file -> deleteFile(index, file) }
logger.info { "${sub.accNo} ${sub.owner} Finished deleting remaining submission files" }
logger.info { "${previous.accNo} ${previous.owner} Finished deleting remaining submission files" }
}

private fun subFilesSet(sub: ExtSubmission) =
serializationService.fileSequence(sub)
.map { it.filePath }
.toSet()
private fun subFilesSet(sub: ExtSubmission?): Set<String> {
return when (sub) {
null -> emptySet()
else -> serializationService.fileSequence(sub).mapTo(mutableSetOf()) { it.filePath }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class SubmissionRequestCleanerTest(

verify(exactly = 1) { requestService.saveSubmissionRequest(cleanedRequest) }
verify(exactly = 0) {
storageService.deleteFtpFile(any(), any())
storageService.deleteSubmissionFile(any(), any())
}
}
Expand All @@ -89,7 +88,6 @@ class SubmissionRequestCleanerTest(

every { loadedRequest.submission } returns new

every { storageService.deleteFtpFile(current, currentFile) } answers { nothing }
every { loadedRequest.withNewStatus(CLEANED) } returns cleanedRequest
every { queryService.findExtByAccNo("S-BSST1", true) } returns current
every { requestService.getLoadedRequest("S-BSST1", 2) } returns loadedRequest
Expand All @@ -101,7 +99,6 @@ class SubmissionRequestCleanerTest(
testInstance.cleanCurrentVersion("S-BSST1", 2)

verify(exactly = 1) {
storageService.deleteFtpFile(current, currentFile)
requestService.saveSubmissionRequest(cleanedRequest)
storageService.deleteSubmissionFile(current, currentFile)
}
Expand Down