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

feat: Add MediaRetentionPolicy to the EventCacheStore, take 2 #4571

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

zecakeh
Copy link
Collaborator

@zecakeh zecakeh commented Jan 22, 2025

Supercedes #3918. Since I needed to rework everything, I used a new branch.

I believe that I addressed all the comments on #3918. The API around MediaService was not clear to me so I came up with something that works.

It is now split into several commits that can be reviewed individually, in order.

  • Public API changes documented in changelogs (optional)

This will be used as a configuration to decide whether or not to keep
media in the cache, allowing to do periodic cleanups to avoid to have
the size of the media cache grow indefinitely.

Signed-off-by: Kévin Commaille <[email protected]>
@zecakeh zecakeh requested a review from Hywan January 22, 2025 10:04
@zecakeh zecakeh requested a review from a team as a code owner January 22, 2025 10:04
@zecakeh zecakeh force-pushed the media-retention-policy branch from 966a3e9 to ff37e93 Compare January 22, 2025 10:06
This is an API to handle the MediaRetentionPolicy with a lower level
EventCacheStoreMedia trait.

Signed-off-by: Kévin Commaille <[email protected]>
It is already a 3-tuple and we want to add more data so it will be clearer to use a stuct with named fields.

Signed-off-by: Kévin Commaille <[email protected]>
@zecakeh zecakeh force-pushed the media-retention-policy branch from ff37e93 to 504a9d4 Compare January 22, 2025 10:10
@bnjbvr bnjbvr removed the request for review from a team January 22, 2025 10:20
Copy link

codecov bot commented Jan 22, 2025

Codecov Report

Attention: Patch coverage is 94.37628% with 55 lines in your changes missing coverage. Please review.

Project coverage is 85.67%. Comparing base (e826c54) to head (7c4c359).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
...e/src/event_cache/store/media/integration_tests.rs 96.75% 21 Missing ⚠️
crates/matrix-sdk-sqlite/src/event_cache_store.rs 88.98% 13 Missing ⚠️
...rix-sdk-base/src/event_cache/store/memory_store.rs 89.15% 9 Missing ⚠️
...es/matrix-sdk-base/src/event_cache/store/traits.rs 28.57% 5 Missing ⚠️
crates/matrix-sdk/src/media.rs 25.00% 3 Missing ⚠️
...-base/src/event_cache/store/media/media_service.rs 95.55% 2 Missing ⚠️
crates/matrix-sdk/src/send_queue/upload.rs 85.71% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4571      +/-   ##
==========================================
+ Coverage   85.42%   85.67%   +0.25%     
==========================================
  Files         285      288       +3     
  Lines       32226    33150     +924     
==========================================
+ Hits        27528    28402     +874     
- Misses       4698     4748      +50     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@zecakeh zecakeh force-pushed the media-retention-policy branch from 6835778 to 7c4c359 Compare January 22, 2025 10:55
Copy link
Member

@Hywan Hywan left a comment

Choose a reason for hiding this comment

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

Excellent work 👏. It's definitely an improvement over #3918. I like the new design. Thank you!

I've left a couple of comments, but nothing big.

// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

Copy link
Member

Choose a reason for hiding this comment

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

The commit message (fd7f88e) is a good basis for this module documentation :-].

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

use ruma::time::{Duration, SystemTime};
use serde::{Deserialize, Serialize};

/// The retention policy for media content used by the `EventCacheStore`.
Copy link
Member

Choose a reason for hiding this comment

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

Can we have an intra-link to EventCacheStore please?

/// … by the [`EventCacheStore`].
///
/// [`EventCacheStore`]: crate::event_cache::store::EventCacheStore

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Applied

}

impl MediaRetentionPolicy {
/// Create a `MediaRetentionPolicy` with the default values.
Copy link
Member

Choose a reason for hiding this comment

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

Intra-link:

- /// Create a `MediaRetentionPolicy` with
+ /// Create a [`MediaRetentionPolicy`] with

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Applied

Self::default()
}

/// Create an empty `MediaRetentionPolicy`.
Copy link
Member

Choose a reason for hiding this comment

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

Intra-link for the win 🤘

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

/// Create an empty `MediaRetentionPolicy`.
///
/// This means that all media will be cached and cleanups have no effect.
pub fn empty() -> Self {
Copy link
Member

Choose a reason for hiding this comment

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

I propose to rename empty to infinite or unlimited, I guess it conveys the intent better, thoughts?

- pub fn empty() -> Self {
+ pub fn unlimited() -> Self {

Copy link
Collaborator Author

@zecakeh zecakeh Jan 24, 2025

Choose a reason for hiding this comment

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

In a future PR, I am planning to allow to launch periodic cleanup jobs. It will add a field that is not a limitation, but a setting of how often the cleanup jobs should run. If this struct does not contain only limitations, infinite and unlimited do not seem to fit.

OpenStoreError,
};

mod keys {
// Entries in Key-value store
pub const MEDIA_RETENTION_POLICY: &str = "media-retention-policy";
Copy link
Member

Choose a reason for hiding this comment

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

Can we have _ instead of - please?

- "media-retention-policy"
+ "media_retention_policy"

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Applied

let current_timestamp = time_to_timestamp(current_time);
let expiry_secs = last_access_expiry.as_secs();
let count = txn.execute(
"DELETE FROM media WHERE ignore_policy IS FALSE AND (? - last_access) >= ?",
Copy link
Member

Choose a reason for hiding this comment

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

Why - last_access?

Copy link
Collaborator Author

@zecakeh zecakeh Jan 24, 2025

Choose a reason for hiding this comment

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

The last_access_expiry contains the maximum age of the content. last_access is the timestamp of the last time that the content was accessed. So to get the age of the content we need to do (current_timestamp - last_access).

/// since Unix Epoch.
///
/// Returns an `i64` as it is the numeric type used by SQLite.
pub(crate) fn time_to_timestamp(time: SystemTime) -> i64 {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe I go too far but… can we have a small tiny unit test for time_to_timestamp please?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added

crates/matrix-sdk/src/send_queue/upload.rs Show resolved Hide resolved
Signed-off-by: Kévin Commaille <[email protected]>
Signed-off-by: Kévin Commaille <[email protected]>
Signed-off-by: Kévin Commaille <[email protected]>
Signed-off-by: Kévin Commaille <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants