-
Notifications
You must be signed in to change notification settings - Fork 768
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
Add spannable tracking around SyncResponseHandler #7514
Merged
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a6d6c89
Add spannable tracking around SyncResponseHandler
amitkma e312881
Update LICENSE header
amitkma f3ccec8
Refactor handleResponse and MetricsExtensions
amitkma 7f8781f
Merge branch 'develop' into feature/amitkma/sync-duration-tracking
amitkma e2c1a96
Update changelog.d
amitkma 5ced6c3
Improve code docs and comments
amitkma ecd51e7
Check if Sentry is enabled before tracking
amitkma 3e6dc49
Merge branch 'develop' into feature/amitkma/sync-duration-tracking
amitkma 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[Metrics] Add `SpannableMetricPlugin` to support spans within transactions. |
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 |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
package org.matrix.android.sdk.internal.session.sync | ||
|
||
import com.zhuinden.monarchy.Monarchy | ||
import io.realm.Realm | ||
import org.matrix.android.sdk.api.MatrixConfiguration | ||
import org.matrix.android.sdk.api.extensions.measureMetric | ||
import org.matrix.android.sdk.api.extensions.measureSpan | ||
|
@@ -60,7 +61,7 @@ internal class SyncResponseHandler @Inject constructor( | |
matrixConfiguration: MatrixConfiguration, | ||
) { | ||
|
||
private val metricPlugins = matrixConfiguration.metricPlugins | ||
private val relevantPlugins = matrixConfiguration.metricPlugins.filterIsInstance<SyncDurationMetricPlugin>() | ||
|
||
suspend fun handleResponse( | ||
syncResponse: SyncResponse, | ||
|
@@ -70,127 +71,165 @@ internal class SyncResponseHandler @Inject constructor( | |
val isInitialSync = fromToken == null | ||
Timber.v("Start handling sync, is InitialSync: $isInitialSync") | ||
|
||
val relevantPlugins = metricPlugins.filterIsInstance<SyncDurationMetricPlugin>() | ||
measureMetric(relevantPlugins) { | ||
// "start_crypto_service" span | ||
measureSpan(relevantPlugins, "task", "start_crypto_service") { | ||
measureTimeMillis { | ||
if (!cryptoService.isStarted()) { | ||
Timber.v("Should start cryptoService") | ||
cryptoService.start() | ||
} | ||
cryptoService.onSyncWillProcess(isInitialSync) | ||
}.also { | ||
Timber.v("Finish handling start cryptoService in $it ms") | ||
} | ||
} | ||
relevantPlugins.measureMetric { | ||
startCryptoService(isInitialSync) | ||
|
||
// Handle the to device events before the room ones | ||
// to ensure to decrypt them properly | ||
|
||
// "handle_to_device" span | ||
measureSpan(relevantPlugins, "task", "handle_to_device") { | ||
measureTimeMillis { | ||
Timber.v("Handle toDevice") | ||
reportSubtask(reporter, InitialSyncStep.ImportingAccountCrypto, 100, 0.1f) { | ||
if (syncResponse.toDevice != null) { | ||
cryptoSyncHandler.handleToDevice(syncResponse.toDevice, reporter) | ||
} | ||
} | ||
}.also { | ||
Timber.v("Finish handling toDevice in $it ms") | ||
} | ||
} | ||
handleToDevice(syncResponse, reporter) | ||
|
||
val aggregator = SyncResponsePostTreatmentAggregator() | ||
|
||
// Prerequisite for thread events handling in RoomSyncHandler | ||
// Disabled due to the new fallback | ||
// if (!lightweightSettingsStorage.areThreadMessagesEnabled()) { | ||
// threadsAwarenessHandler.fetchRootThreadEventsIfNeeded(syncResponse) | ||
// } | ||
|
||
// Start one big transaction | ||
// Big "monarchy_transaction" span | ||
measureSpan(relevantPlugins, "task", "monarchy_transaction") { | ||
monarchy.awaitTransaction { realm -> | ||
// IMPORTANT nothing should be suspend here as we are accessing the realm instance (thread local) | ||
// Child "handle_rooms" span | ||
measureSpan(relevantPlugins, "task", "handle_rooms") { | ||
measureTimeMillis { | ||
Timber.v("Handle rooms") | ||
reportSubtask(reporter, InitialSyncStep.ImportingAccountRoom, 1, 0.8f) { | ||
if (syncResponse.rooms != null) { | ||
roomSyncHandler.handle(realm, syncResponse.rooms, isInitialSync, aggregator, reporter) | ||
} | ||
} | ||
}.also { | ||
Timber.v("Finish handling rooms in $it ms") | ||
} | ||
} | ||
// Disabled due to the new fallback | ||
// if (!lightweightSettingsStorage.areThreadMessagesEnabled()) { | ||
// threadsAwarenessHandler.fetchRootThreadEventsIfNeeded(syncResponse) | ||
// } | ||
|
||
// Child "handle_account_data" span | ||
measureSpan(relevantPlugins, "task", "handle_account_data") { | ||
measureTimeMillis { | ||
reportSubtask(reporter, InitialSyncStep.ImportingAccountData, 1, 0.1f) { | ||
Timber.v("Handle accountData") | ||
userAccountDataSyncHandler.handle(realm, syncResponse.accountData) | ||
} | ||
}.also { | ||
Timber.v("Finish handling accountData in $it ms") | ||
} | ||
} | ||
startMonarchyTransaction(syncResponse, isInitialSync, reporter, aggregator) | ||
|
||
// Child "handle_presence" span | ||
measureSpan(relevantPlugins, "task", "handle_presence") { | ||
measureTimeMillis { | ||
Timber.v("Handle Presence") | ||
presenceSyncHandler.handle(realm, syncResponse.presence) | ||
}.also { | ||
Timber.v("Finish handling Presence in $it ms") | ||
} | ||
} | ||
tokenStore.saveToken(realm, syncResponse.nextBatch) | ||
aggregateSyncResponse(aggregator) | ||
|
||
postTreatmentSyncResponse(syncResponse, isInitialSync) | ||
|
||
markCryptoSyncCompleted(syncResponse) | ||
|
||
handlePostSync() | ||
|
||
Timber.v("On sync completed") | ||
} | ||
} | ||
|
||
private fun startCryptoService(isInitialSync: Boolean) { | ||
// "start_crypto_service" span | ||
relevantPlugins.measureSpan("task", "start_crypto_service") { | ||
measureTimeMillis { | ||
if (!cryptoService.isStarted()) { | ||
Timber.v("Should start cryptoService") | ||
cryptoService.start() | ||
} | ||
cryptoService.onSyncWillProcess(isInitialSync) | ||
}.also { | ||
Timber.v("Finish handling start cryptoService in $it ms") | ||
} | ||
} | ||
} | ||
|
||
// "aggregator_management" span | ||
measureSpan(relevantPlugins, "task", "aggregator_management") { | ||
// Everything else we need to do outside the transaction | ||
measureTimeMillis { | ||
aggregatorHandler.handle(aggregator) | ||
}.also { | ||
Timber.v("Aggregator management took $it ms") | ||
private suspend fun handleToDevice(syncResponse: SyncResponse, reporter: ProgressReporter?) { | ||
// "handle_to_device" span | ||
relevantPlugins.measureSpan("task", "handle_to_device") { | ||
measureTimeMillis { | ||
Timber.v("Handle toDevice") | ||
reportSubtask(reporter, InitialSyncStep.ImportingAccountCrypto, 100, 0.1f) { | ||
if (syncResponse.toDevice != null) { | ||
cryptoSyncHandler.handleToDevice(syncResponse.toDevice, reporter) | ||
} | ||
} | ||
}.also { | ||
Timber.v("Finish handling toDevice in $it ms") | ||
} | ||
} | ||
} | ||
|
||
private suspend fun startMonarchyTransaction(syncResponse: SyncResponse, isInitialSync: Boolean, reporter: ProgressReporter?, aggregator: SyncResponsePostTreatmentAggregator) { | ||
// Start one big transaction | ||
// Big "monarchy_transaction" span | ||
relevantPlugins.measureSpan("task", "monarchy_transaction") { | ||
monarchy.awaitTransaction { realm -> | ||
// IMPORTANT nothing should be suspend here as we are accessing the realm instance (thread local) | ||
handleRooms(reporter, syncResponse, realm, isInitialSync, aggregator) | ||
handleAccountData(reporter, realm, syncResponse) | ||
handlePresence(realm, syncResponse) | ||
|
||
// "sync_response_post_treatment" span | ||
measureSpan(relevantPlugins, "task", "sync_response_post_treatment") { | ||
measureTimeMillis { | ||
syncResponse.rooms?.let { | ||
checkPushRules(it, isInitialSync) | ||
userAccountDataSyncHandler.synchronizeWithServerIfNeeded(it.invite) | ||
dispatchInvitedRoom(it) | ||
tokenStore.saveToken(realm, syncResponse.nextBatch) | ||
} | ||
} | ||
} | ||
|
||
private fun handleRooms(reporter: ProgressReporter?, syncResponse: SyncResponse, realm: Realm, isInitialSync: Boolean, aggregator: SyncResponsePostTreatmentAggregator) { | ||
// Child "handle_rooms" span | ||
relevantPlugins.measureSpan("task", "handle_rooms") { | ||
measureTimeMillis { | ||
Timber.v("Handle rooms") | ||
reportSubtask(reporter, InitialSyncStep.ImportingAccountRoom, 1, 0.8f) { | ||
if (syncResponse.rooms != null) { | ||
roomSyncHandler.handle(realm, syncResponse.rooms, isInitialSync, aggregator, reporter) | ||
} | ||
}.also { | ||
Timber.v("SyncResponse.rooms post treatment took $it ms") | ||
} | ||
}.also { | ||
Timber.v("Finish handling rooms in $it ms") | ||
} | ||
} | ||
} | ||
|
||
// "crypto_sync_handler_onSyncCompleted" span | ||
measureSpan(relevantPlugins, "task", "crypto_sync_handler_onSyncCompleted") { | ||
measureTimeMillis { | ||
cryptoSyncHandler.onSyncCompleted(syncResponse) | ||
}.also { | ||
Timber.v("cryptoSyncHandler.onSyncCompleted took $it ms") | ||
private fun handleAccountData(reporter: ProgressReporter?, realm: Realm, syncResponse: SyncResponse) { | ||
// Child "handle_account_data" span | ||
relevantPlugins.measureSpan("task", "handle_account_data") { | ||
measureTimeMillis { | ||
reportSubtask(reporter, InitialSyncStep.ImportingAccountData, 1, 0.1f) { | ||
Timber.v("Handle accountData") | ||
userAccountDataSyncHandler.handle(realm, syncResponse.accountData) | ||
} | ||
}.also { | ||
Timber.v("Finish handling accountData in $it ms") | ||
} | ||
} | ||
} | ||
|
||
private fun handlePresence(realm: Realm, syncResponse: SyncResponse) { | ||
// Child "handle_presence" span | ||
relevantPlugins.measureSpan("task", "handle_presence") { | ||
measureTimeMillis { | ||
Timber.v("Handle Presence") | ||
presenceSyncHandler.handle(realm, syncResponse.presence) | ||
}.also { | ||
Timber.v("Finish handling Presence in $it ms") | ||
} | ||
} | ||
} | ||
|
||
// post sync stuffs | ||
monarchy.writeAsync { | ||
roomSyncHandler.postSyncSpaceHierarchyHandle(it) | ||
private suspend fun aggregateSyncResponse(aggregator: SyncResponsePostTreatmentAggregator) { | ||
// "aggregator_management" span | ||
relevantPlugins.measureSpan("task", "aggregator_management") { | ||
// Everything else we need to do outside the transaction | ||
measureTimeMillis { | ||
aggregatorHandler.handle(aggregator) | ||
}.also { | ||
Timber.v("Aggregator management took $it ms") | ||
} | ||
Timber.v("On sync completed") | ||
} | ||
} | ||
|
||
private suspend fun postTreatmentSyncResponse(syncResponse: SyncResponse, isInitialSync: Boolean) { | ||
// "sync_response_post_treatment" span | ||
relevantPlugins.measureSpan("task", "sync_response_post_treatment") { | ||
measureTimeMillis { | ||
syncResponse.rooms?.let { | ||
checkPushRules(it, isInitialSync) | ||
userAccountDataSyncHandler.synchronizeWithServerIfNeeded(it.invite) | ||
dispatchInvitedRoom(it) | ||
} | ||
}.also { | ||
Timber.v("SyncResponse.rooms post treatment took $it ms") | ||
} | ||
} | ||
} | ||
|
||
private fun markCryptoSyncCompleted(syncResponse: SyncResponse) { | ||
// "crypto_sync_handler_onSyncCompleted" span | ||
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. I wonder if it is useful to keep these comments about spans since now we have dedicated private methods? |
||
relevantPlugins.measureSpan("task", "crypto_sync_handler_onSyncCompleted") { | ||
measureTimeMillis { | ||
cryptoSyncHandler.onSyncCompleted(syncResponse) | ||
}.also { | ||
Timber.v("cryptoSyncHandler.onSyncCompleted took $it ms") | ||
} | ||
} | ||
} | ||
|
||
private fun handlePostSync() { | ||
// post sync stuffs | ||
monarchy.writeAsync { | ||
roomSyncHandler.postSyncSpaceHierarchyHandle(it) | ||
} | ||
} | ||
|
||
|
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.
Yes I think we should update to avoid any confusion.