-
Notifications
You must be signed in to change notification settings - Fork 18
WMSDK-510: Remove fatal error through loadPersistentStores, add fallbacks and states. New trim DB size if needed. #594
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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
92 changes: 92 additions & 0 deletions
92
MindboxLogger/Shared/LoggerRepository/LogStoreTrimmer.swift
This file contains hidden or 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,92 @@ | ||
| // | ||
| // LogStoreTrimmer.swift | ||
| // MindboxLogger | ||
| // | ||
| // Created by Sergei Semko on 9/11/25. | ||
| // Copyright © 2025 Mindbox. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| protocol Clock { | ||
| var now: Date { get } | ||
| } | ||
|
|
||
| struct SystemClock: Clock { | ||
| public init() {} | ||
| public var now: Date { Date() } | ||
| } | ||
|
|
||
| protocol LogStoreTrimming { | ||
| /// Attempts to perform a trim operation if the policy allows it. | ||
| /// | ||
| /// - Parameters: | ||
| /// - precomputedSizeKB: Optional precomputed database size in kilobytes. | ||
| /// If provided, the trimmer will **not** call the measurer. | ||
| /// - delete: A callback that must perform deletion given the computed fraction | ||
| /// (e.g. delete the oldest `fraction * N` items). May throw. | ||
| /// - Returns: `true` if a trim was performed; `false` if skipped (below limit or under cooldown). | ||
| /// - Throws: Rethrows any error thrown by `delete`. | ||
| @discardableResult | ||
| func maybeTrim(precomputedSizeKB: Int?, delete: (Double) throws -> Void) throws -> Bool | ||
|
|
||
| /// Computes the fraction of items to delete in order to reach the configured | ||
| /// low-water mark. | ||
| /// | ||
| /// The result is clamped to `[minDeleteFraction, maxDeleteFraction]`. | ||
| /// Returns `nil` if the current size does not exceed the limit. | ||
| func computeTrimFraction(sizeKB: Int, limitKB: Int) -> Double? | ||
|
|
||
| /// Resets cooldown so that the next `maybeTrim` call may run immediately. | ||
| func resetCooldown() | ||
| } | ||
|
|
||
| final class LogStoreTrimmer: LogStoreTrimming { | ||
| private let config: LoggerDBConfig | ||
| private let sizeMeasurer: DatabaseSizeMeasuring | ||
| private let clock: Clock | ||
|
|
||
| private var cooldownUntil: Date? | ||
|
|
||
| init(config: LoggerDBConfig, | ||
| sizeMeasurer: DatabaseSizeMeasuring, | ||
| clock: Clock) { | ||
| self.config = config | ||
| self.sizeMeasurer = sizeMeasurer | ||
| self.clock = clock | ||
| } | ||
|
|
||
| convenience init(config: LoggerDBConfig, | ||
| sizeMeasurer: DatabaseSizeMeasuring) { | ||
| self.init(config: config, sizeMeasurer: sizeMeasurer, clock: SystemClock()) | ||
| } | ||
|
|
||
| func resetCooldown() { cooldownUntil = nil } | ||
|
|
||
| func computeTrimFraction(sizeKB: Int, limitKB: Int) -> Double? { | ||
| guard sizeKB > limitKB else { return nil } | ||
| let targetKB = Int(Double(limitKB) * config.lowWaterRatio) | ||
| let raw = Double(sizeKB - targetKB) / Double(max(sizeKB, 1)) | ||
| let fraction = min(config.maxDeleteFraction, max(config.minDeleteFraction, raw)) | ||
| return fraction | ||
| } | ||
|
|
||
| @discardableResult | ||
| func maybeTrim(precomputedSizeKB: Int? = nil, | ||
| delete: (Double) throws -> Void) rethrows -> Bool { | ||
| if let t = cooldownUntil, t > clock.now { return false } | ||
| let sizeKB = precomputedSizeKB ?? sizeMeasurer.sizeKB() | ||
| guard let fraction = computeTrimFraction(sizeKB: sizeKB, limitKB: config.dbSizeLimitKB) else { return false } | ||
| try delete(fraction) | ||
| cooldownUntil = clock.now.addingTimeInterval(config.trimCooldownSec) | ||
| return true | ||
| } | ||
| } | ||
|
|
||
| #if DEBUG | ||
| final class ManualClock: Clock { | ||
| var now: Date | ||
| init(_ now: Date) { self.now = now } | ||
| func advance(_ seconds: TimeInterval) { now = now.addingTimeInterval(seconds) } | ||
| } | ||
| #endif | ||
29 changes: 29 additions & 0 deletions
29
MindboxLogger/Shared/LoggerRepository/LoggerDBConfig.swift
This file contains hidden or 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,29 @@ | ||
| // | ||
| // LoggerDBConfig.swift | ||
| // MindboxLogger | ||
| // | ||
| // Created by Sergei Semko on 9/11/25. | ||
| // Copyright © 2025 Mindbox. All rights reserved. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| public struct LoggerDBConfig { | ||
| public let dbSizeLimitKB: Int | ||
| public let lowWaterRatio: Double | ||
| public let minDeleteFraction: Double | ||
| public let maxDeleteFraction: Double | ||
| public let batchSize: Int | ||
| public let writesPerTrimCheck: Int | ||
| public let trimCooldownSec: TimeInterval | ||
|
|
||
| public static let `default` = LoggerDBConfig( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Это просто эвристические параметры?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Да. Удобно вынести стало из-за их роста и для подстановки в тесты |
||
| dbSizeLimitKB: 10_240, | ||
| lowWaterRatio: 0.85, | ||
| minDeleteFraction: 0.05, | ||
| maxDeleteFraction: 0.50, | ||
| batchSize: 15, | ||
| writesPerTrimCheck: 5, | ||
| trimCooldownSec: 10 | ||
| ) | ||
| } | ||
Oops, something went wrong.
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.
А почему не наоборот? Хранить последнее время и каждый раз проверять добавляя интервал
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.
Удобно было сделать deadline "до какого времени нельзя". Но можно и как ты предложил. Пока нигде не аффектят оба варианта.