forked from storj-archived/bridge
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from internxt/feat/v1-objects-cleanup
[_]: fix/handle-shards-deletion-when-no-mirrors-exist
- Loading branch information
Showing
22 changed files
with
827 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { PrepareFunctionReturnType } from "../init"; | ||
import { CommandId } from "./id"; | ||
import cleanStalledBucketEntries from "../tasks/clean-stalled-bucket-entries.task"; | ||
|
||
export default { | ||
id: CommandId.CleanStalledBucketEntries, | ||
version: '0.0.1', | ||
fn: async ( | ||
{ | ||
usecase: { bucketsUsecase, bucketEntriesUsecase }, | ||
repo: { framesRepository }, | ||
readers | ||
}: PrepareFunctionReturnType, | ||
): Promise<void> => { | ||
await cleanStalledBucketEntries( | ||
bucketsUsecase, | ||
bucketEntriesUsecase, | ||
framesRepository, | ||
readers.bucketEntriesReader | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { PrepareFunctionReturnType } from "../init"; | ||
import { cleanStalledFrames } from "../tasks/clean-stalled-frames.task"; | ||
import { CommandId } from "./id"; | ||
|
||
export default { | ||
id: CommandId.CleanStalledFrames, | ||
version: '0.0.1', | ||
fn: async ( | ||
{ usecase: { bucketEntriesUsecase }, readers }: PrepareFunctionReturnType, | ||
): Promise<void> => { | ||
await cleanStalledFrames(bucketEntriesUsecase, readers.framesReader); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { BucketEntriesUsecase } from "../../../lib/core/bucketEntries/usecase"; | ||
import { BucketsUsecase } from "../../../lib/core/buckets/usecase"; | ||
import { FramesRepository } from "../../../lib/core/frames/Repository"; | ||
import { BucketEntriesReader } from "../../delete-objects/ObjectStorage"; | ||
import { BucketEntryDocument } from "../../delete-objects/temp-shard.model"; | ||
|
||
export type CleanStalledBucketEntriesFunctionType = ( | ||
bucketsUsecase: BucketsUsecase, | ||
bucketEntriesUsecase: BucketEntriesUsecase, | ||
framesRepository: FramesRepository, | ||
reader: BucketEntriesReader, | ||
) => Promise<void>; | ||
|
||
const task: CleanStalledBucketEntriesFunctionType = async ( | ||
bucketsUsecase, | ||
bucketEntriesUsecase, | ||
framesRepository, | ||
reader, | ||
): Promise<void> => { | ||
const deleteInBulksOf = 20; | ||
const toDelete: BucketEntryDocument[] = []; | ||
const stats = { | ||
totalSize: 0, | ||
totalCount: 0, | ||
} | ||
|
||
for await (const bucketEntry of reader.list()) { | ||
const bucketDoesNotExist = await bucketsUsecase.findById(bucketEntry.bucket) === null; | ||
const isV1 = !bucketEntry.version; | ||
|
||
if (bucketDoesNotExist && isV1) { | ||
toDelete.push(bucketEntry); | ||
} | ||
|
||
if (toDelete.length === deleteInBulksOf) { | ||
const frames = await framesRepository.findByIds(toDelete.map(b => b.frame!)); | ||
const beIds = toDelete.map(be => be._id.toString()); | ||
|
||
await bucketEntriesUsecase.removeFiles(beIds); | ||
|
||
toDelete.forEach(be => { | ||
console.log(`deleting entry ${be._id}`); | ||
}); | ||
|
||
stats.totalSize += frames.reduce((acc, curr) => acc + (curr.size || 0), 0); | ||
stats.totalCount += toDelete.length; | ||
|
||
toDelete.length = 0; | ||
|
||
console.log(`total size ${stats.totalSize}, total count ${stats.totalCount}`); | ||
} | ||
} | ||
} | ||
|
||
export default task; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { BucketEntriesUsecase } from "../../../lib/core/bucketEntries/usecase"; | ||
import { FramesReader } from "../../delete-objects/ObjectStorage"; | ||
import { Frame } from "../../../lib/core/frames/Frame"; | ||
import { FrameDocument } from "../../delete-objects/temp-shard.model"; | ||
|
||
export type CleanStalledFramesFunctionType = ( | ||
bucketEntriesUsecase: BucketEntriesUsecase, | ||
reader: FramesReader, | ||
) => Promise<void>; | ||
|
||
export const cleanStalledFrames: CleanStalledFramesFunctionType = async ( | ||
bucketEntriesUsecase, | ||
reader, | ||
): Promise<void> => { | ||
const deleteInBulksOf = 20; | ||
const toDelete: { frame: Frame['id'], id: '', _frame: FrameDocument }[] = []; | ||
const stats = { | ||
totalSize: 0, | ||
totalCount: 0, | ||
} | ||
|
||
for await (const frame of reader.list()) { | ||
if (frame.bucketEntry) { | ||
const be = await bucketEntriesUsecase.findById(frame.bucketEntry); | ||
|
||
if (!be) { | ||
console.log(`deleting frame ${frame._id}, be ${frame.bucketEntry}, size ${frame.size}`); | ||
toDelete.push({ frame: frame._id.toString(), id: '', _frame: frame }); | ||
} | ||
} | ||
if (toDelete.length === deleteInBulksOf) { | ||
await bucketEntriesUsecase.removeFilesV1(toDelete as any); | ||
|
||
stats.totalSize += toDelete.reduce((acc, curr) => acc + curr._frame.size, 0); | ||
stats.totalCount += toDelete.length; | ||
|
||
toDelete.length = 0; | ||
|
||
console.log(`total size ${stats.totalSize}, total count ${stats.totalCount}`); | ||
} | ||
} | ||
} |
Oops, something went wrong.