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

Sync: avoid deleting root event of CurrentState on gappy sync #5137

Merged
merged 7 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class RealmSessionStoreMigration @Inject constructor(
) : RealmMigration {

companion object {
const val SESSION_STORE_SCHEMA_VERSION = 21L
const val SESSION_STORE_SCHEMA_VERSION = 22L
}

/**
Expand Down Expand Up @@ -90,6 +90,7 @@ internal class RealmSessionStoreMigration @Inject constructor(
if (oldVersion <= 18) migrateTo19(realm)
if (oldVersion <= 19) migrateTo20(realm)
if (oldVersion <= 20) migrateTo21(realm)
if (oldVersion <= 21) migrateTo22(realm)
}

private fun migrateTo1(realm: DynamicRealm) {
Expand Down Expand Up @@ -445,4 +446,14 @@ internal class RealmSessionStoreMigration @Inject constructor(
}
}
}

private fun migrateTo22(realm: DynamicRealm) {
Timber.d("Step 21 -> 22")
val hasMissingStateEvent = realm.where("CurrentStateEventEntity").isNull(CurrentStateEventEntityFields.ROOT.`$`).findFirst() != null
Copy link
Member

Choose a reason for hiding this comment

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

I you left some room, wouldn't this lead to null root and trigger an unwanted initial sync?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we have updated the query together. Thanks!

if (hasMissingStateEvent) {
Timber.v("Has some missing state event, clear session cache")
realm.deleteAll()
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ internal fun ChunkEntity.deleteOnCascade(deleteStateEvents: Boolean, canDeleteRo
if (deleteStateEvents) {
stateEvents.deleteAllFromRealm()
}
timelineEvents.clearWith { it.deleteOnCascade(canDeleteRoot) }
timelineEvents.clearWith {
val deleteRoot = canDeleteRoot && (it.root?.stateKey == null || deleteStateEvents)
it.deleteOnCascade(deleteRoot)
}
deleteFromRealm()
}
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ internal class RoomSyncHandler @Inject constructor(private val readReceiptHandle
aggregator: SyncResponsePostTreatmentAggregator): ChunkEntity {
val lastChunk = ChunkEntity.findLastForwardChunkOfRoom(realm, roomEntity.roomId)
if (isLimited && lastChunk != null) {
lastChunk.deleteOnCascade(deleteStateEvents = true, canDeleteRoot = true)
lastChunk.deleteOnCascade(deleteStateEvents = false, canDeleteRoot = true)
}
val chunkEntity = if (!isLimited && lastChunk != null) {
lastChunk
Expand Down