Skip to content

Optimize diagnostics file management#1194

Merged
tonidero merged 5 commits into
mainfrom
toniricodiez/sdk-3194-ensure-that-sdk-cant-accidentally-end-up-loading-a-very
Aug 17, 2023
Merged

Optimize diagnostics file management#1194
tonidero merged 5 commits into
mainfrom
toniricodiez/sdk-3194-ensure-that-sdk-cant-accidentally-end-up-loading-a-very

Conversation

@tonidero

@tonidero tonidero commented Aug 8, 2023

Copy link
Copy Markdown
Contributor

Description

This PR is the first part of a possible approach to avoid loading huge files in memory in diagnostics at once. Basically, in here we change to reading the file per lines instead of reading everything in one go.

Behavior changes

  • Diagnostics will be Android 24+ only.
  • An important change in behavior is that, instead of using the number of events in the diagnostics file to set a limit, we use the file size. So if on SDK configuration we detect that it's reached X size, we would clear the whole file and start over.
  • When we reach the file size limit, we remove the whole file, instead of just the oldest events.
  • We remove the properties from the MAX_EVENTS_STORED_LIMIT_REACHED event since they don't make sense with the new approach. (This will require backend changes)
  • Now we will have a limit to the max number of diagnostics events in memory and per request.

Comment thread purchases/src/main/kotlin/com/revenuecat/purchases/common/FileHelper.kt Outdated
) {
companion object {
const val DIAGNOSTICS_FILE_PATH = "RevenueCat/diagnostics/diagnostic_entries.jsonl"
const val DIAGNOSTICS_FILE_LIMIT_IN_KB = 500

@tonidero tonidero Aug 8, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is TBD, this is a pretty big file already, but not sure how big these files might get... From my estimations, this might be ~1k events.

name = DiagnosticsEventName.MAX_EVENTS_STORED_LIMIT_REACHED,
properties = mapOf(
"total_number_events_stored" to totalEventsStored,
"events_removed" to eventsRemoved,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I thought these properties don't make sense anymore. We could send the file size, but in general, these files shouldn't get much bigger than the limit (currently of 500kb) so I thought it wouldn't be that useful.

@@ -0,0 +1,6 @@
package com.revenuecat.purchases.utils

interface DataListener<T> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since I'm using this in the FileHelper, it should have a pretty generic name... Still not completely in love with it... Lmk if you have any other thoughts.

var currentEventsToSync = 0
diagnosticsFileHelper.readDiagnosticsFile(object : DataListener<JSONObject> {
override fun onData(data: JSONObject) {
eventsToSync.add(data)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note how we currently still read the whole file and keep it in memory. I'm thinking about splitting things in a future PR, so we post diagnostics in batches. That's slightly tricky since we don't have much control over the listener (we can't stop it midway, nor ask for new events on demand).

@codecov

codecov Bot commented Aug 8, 2023

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 86.53846% with 7 lines in your changes missing coverage. Please review.

Project coverage is 85.88%. Comparing base (0853414) to head (7202adb).
Report is 528 commits behind head on main.

Files with missing lines Patch % Lines
...otlin/com/revenuecat/purchases/PurchasesFactory.kt 0.00% 1 Missing and 3 partials ⚠️
.../com/revenuecat/purchases/PurchasesOrchestrator.kt 66.66% 0 Missing and 1 partial ⚠️
...purchases/common/diagnostics/DiagnosticsTracker.kt 88.88% 0 Missing and 1 partial ⚠️
.../revenuecat/purchases/utils/AndroidVersionUtils.kt 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1194      +/-   ##
==========================================
- Coverage   85.92%   85.88%   -0.04%     
==========================================
  Files         181      183       +2     
  Lines        6217     6229      +12     
  Branches      900      905       +5     
==========================================
+ Hits         5342     5350       +8     
- Misses        533      534       +1     
- Partials      342      345       +3     

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

@tonidero tonidero marked this pull request as ready for review August 9, 2023 09:51
@tonidero tonidero requested a review from a team August 9, 2023 09:51
@tonidero tonidero marked this pull request as draft August 9, 2023 15:39

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is something that I don't fully like... But I didn't want to leak the responsibility of closing the readers.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It makes sense tho

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Adding a limit to the events in memory/per request was done previously in a separate PR, but I decided to move it to this PR for simplicity

@tonidero tonidero changed the title Optimize diagnostics file management (Part 1) Optimize diagnostics file management Aug 10, 2023
@tonidero tonidero marked this pull request as ready for review August 10, 2023 07:45
@tonidero tonidero requested a review from NachoSoto August 10, 2023 07:45

@NachoSoto NachoSoto left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry for the delay!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you think about a more "functional" test that adds say 500 events and verifies that it's not considered too big?
As a way to validate that the size limit allows for some number of events that we're happy with.

@tonidero tonidero Aug 17, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good idea, done in 2914d17

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need to put this in the docs too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We haven't added diagnostics to the docs yet since we were planning to add extra functionality before that, so nothing to update

@tonidero tonidero force-pushed the toniricodiez/sdk-3194-ensure-that-sdk-cant-accidentally-end-up-loading-a-very branch 2 times, most recently from 265d1f6 to 2914d17 Compare August 17, 2023 10:23
@tonidero tonidero force-pushed the toniricodiez/sdk-3194-ensure-that-sdk-cant-accidentally-end-up-loading-a-very branch from 2914d17 to 7202adb Compare August 17, 2023 10:39
@tonidero tonidero merged commit 7feda65 into main Aug 17, 2023
@tonidero tonidero deleted the toniricodiez/sdk-3194-ensure-that-sdk-cant-accidentally-end-up-loading-a-very branch August 17, 2023 10:53
tonidero added a commit that referenced this pull request Aug 24, 2023
**This is an automatic release.**

### Performance Improvements
* Optimize SDK initialization when requests executed before any activity
starts (#1204) via Toni Rico (@tonidero)
* Optimize diagnostics file management (#1194) via Toni Rico (@tonidero)
### Other Changes
* Use real debug view dependencies in magic weather compose (#1203) via
Toni Rico (@tonidero)

---------

Co-authored-by: revenuecat-ops <ops@revenuecat.com>
Co-authored-by: Toni Rico <antonio.rico.diez@revenuecat.com>
Co-authored-by: Toni Rico <toni.rico.diez@revenuecat.com>
@vegaro vegaro added pr:other and removed pr:perf labels Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants