-
Notifications
You must be signed in to change notification settings - Fork 60
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
chore: added size based retention policy #2098
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0b7e863
chore: added size based retention policy
ABresting 9fedc9f
chore: post review code - size based retention policy
ABresting 675687c
Merge branch 'master' of https://github.com/waku-org/nwaku into chore…
ABresting d7140dd
chore: review integrated, size retention updated, vacuuming in capaci…
ABresting c026cc9
chore: typo fixed
ABresting 5022d3e
chore: review integrated
ABresting 70781ad
Merge branch 'master' of https://github.com/waku-org/nwaku into chore…
ABresting 3a71923
chore: external config updated to support newly added retention policy
ABresting File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
87 changes: 87 additions & 0 deletions
87
waku/waku_archive/retention_policy/retention_policy_size.nim
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,87 @@ | ||
when (NimMajor, NimMinor) < (1, 4): | ||
{.push raises: [Defect].} | ||
else: | ||
{.push raises: [].} | ||
|
||
import | ||
std/times, | ||
stew/results, | ||
chronicles, | ||
chronos | ||
import | ||
../driver, | ||
../retention_policy | ||
|
||
logScope: | ||
topics = "waku archive retention_policy" | ||
|
||
# default size is 30 Gb | ||
const DefaultRetentionSize*: float = 30_720 | ||
|
||
# to remove 20% of the outdated data from database | ||
const DeleteLimit = 0.80 | ||
|
||
type | ||
# SizeRetentionPolicy implements auto delete as follows: | ||
# - sizeLimit is the size in megabytes (Mbs) the database can grow upto | ||
# to reduce the size of the databases, remove the rows/number-of-messages | ||
# DeleteLimit is the total number of messages to delete beyond this limit | ||
# when the database size crosses the sizeLimit, then only a fraction of messages are kept, | ||
# rest of the outdated message are deleted using deleteOldestMessagesNotWithinLimit(), | ||
# upon deletion process the fragmented space is retrieve back using Vacuum process. | ||
SizeRetentionPolicy* = ref object of RetentionPolicy | ||
sizeLimit: float | ||
|
||
proc init*(T: type SizeRetentionPolicy, size=DefaultRetentionSize): T = | ||
SizeRetentionPolicy( | ||
sizeLimit: size | ||
) | ||
|
||
method execute*(p: SizeRetentionPolicy, | ||
driver: ArchiveDriver): | ||
Future[RetentionPolicyResult[void]] {.async.} = | ||
## when db size overshoots the database limit, shread 20% of outdated messages | ||
|
||
# to get the size of the database, pageCount and PageSize is required | ||
# get page count in "messages" database | ||
let pageCountRes = await driver.getPagesCount() | ||
if pageCountRes.isErr(): | ||
return err("failed to get Pages count: " & pageCountRes.error) | ||
|
||
let pageCount: int64 = pageCountRes.value | ||
|
||
# get page size of database | ||
let pageSizeRes = await driver.getPagesSize() | ||
let pageSize: int64 = int64(pageSizeRes.valueOr(0) div 1024) | ||
|
||
if pageSize == 0: | ||
return err("failed to get Page size: " & pageSizeRes.error) | ||
|
||
# database size in megabytes (Mb) | ||
let totalSizeOfDB: float = float(pageSize * pageCount)/1024.0 | ||
|
||
# check if current databse size crosses the db size limit | ||
if totalSizeOfDB < p.sizeLimit: | ||
return ok() | ||
|
||
# to shread/delete messsges, get the total row/message count | ||
let numMessagesRes = await driver.getMessagesCount() | ||
if numMessagesRes.isErr(): | ||
return err("failed to get messages count: " & numMessagesRes.error) | ||
let numMessages = numMessagesRes.value | ||
|
||
# 80% of the total messages are to be kept, delete others | ||
let pageDeleteWindow = int(float(numMessages) * DeleteLimit) | ||
|
||
let res = await driver.deleteOldestMessagesNotWithinLimit(limit=pageDeleteWindow) | ||
if res.isErr(): | ||
return err("deleting oldest messages failed: " & res.error) | ||
|
||
# vacuum to get the deleted pages defragments to save storage space | ||
# this will resize the database size | ||
let resVaccum = await driver.performVacuum() | ||
if resVaccum.isErr(): | ||
return err("vacuumming failed: " & resVaccum.error) | ||
|
||
return ok() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great indeed! However, would it be possible to enforce a
require
instead?