Clean oldest gossiping entries on open

Add a dump of DB size on bug report
This commit is contained in:
Valere 2020-11-02 16:41:30 +01:00
parent c3b8ed223d
commit 3c7f61e45c
7 changed files with 99 additions and 1 deletions

View file

@ -238,4 +238,9 @@ interface Session :
}
val sharedSecretStorageService: SharedSecretStorageService
/**
* Maintenance API, allows to print outs info on DB size to logcat
*/
fun dbgTraceDbInfo()
}

View file

@ -155,4 +155,6 @@ interface CryptoService {
// For testing shared session
fun getSharedWithInfo(roomId: String?, sessionId: String): MXUsersDevicesMap<Int>
fun getWithHeldMegolmSession(roomId: String, sessionId: String): RoomKeyWithHeldContent?
fun logDbUsageInfo()
}

View file

@ -314,6 +314,10 @@ internal class DefaultCryptoService @Inject constructor(
}
// Just update
fetchDevicesList(NoOpMatrixCallback())
cryptoCoroutineScope.launch(coroutineDispatchers.crypto) {
cryptoStore.tidyUpDataBase()
}
}
fun ensureDevice() {
@ -1291,6 +1295,11 @@ internal class DefaultCryptoService @Inject constructor(
override fun getWithHeldMegolmSession(roomId: String, sessionId: String): RoomKeyWithHeldContent? {
return cryptoStore.getWithHeldMegolmSession(roomId, sessionId)
}
override fun logDbUsageInfo() {
cryptoStore.logDbUsageInfo()
}
/* ==========================================================================================
* For test only
* ========================================================================================== */

View file

@ -456,4 +456,6 @@ internal interface IMXCryptoStore {
fun setDeviceKeysUploaded(uploaded: Boolean)
fun getDeviceKeysUploaded(): Boolean
fun tidyUpDataBase()
fun logDbUsageInfo()
}

View file

@ -95,6 +95,7 @@ import org.matrix.android.sdk.internal.session.SessionScope
import org.matrix.olm.OlmAccount
import org.matrix.olm.OlmException
import timber.log.Timber
import java.lang.StringBuilder
import javax.inject.Inject
import kotlin.collections.set
@ -1666,4 +1667,61 @@ internal class RealmCryptoStore @Inject constructor(
result
}
}
/**
* Some entries in the DB can get a bit out of control with time
* So we need to tidy up a bit
*/
override fun tidyUpDataBase() {
val prevWeekTs = System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1_000
doRealmTransaction(realmConfiguration) { realm ->
// Only keep one week history
realm.where<IncomingGossipingRequestEntity>()
.lessThan(IncomingGossipingRequestEntityFields.LOCAL_CREATION_TIMESTAMP, prevWeekTs)
.findAll().let {
Timber.i("## Crypto Clean up ${it.size} IncomingGossipingRequestEntity")
it.deleteAllFromRealm()
}
// Clean the cancelled ones?
realm.where<OutgoingGossipingRequestEntity>()
.equalTo(OutgoingGossipingRequestEntityFields.REQUEST_STATE_STR, OutgoingGossipingRequestState.CANCELLED.name)
.equalTo(OutgoingGossipingRequestEntityFields.TYPE_STR, GossipRequestType.KEY.name)
.findAll().let {
Timber.i("## Crypto Clean up ${it.size} OutgoingGossipingRequestEntity")
it.deleteAllFromRealm()
}
// Only keep one week history
realm.where<GossipingEventEntity>()
.lessThan(GossipingEventEntityFields.AGE_LOCAL_TS, prevWeekTs)
.findAll().let {
Timber.i("## Crypto Clean up ${it.size} GossipingEventEntityFields")
it.deleteAllFromRealm()
}
// Can we do something for WithHeldSessionEntity?
}
}
/**
* Prints out database info
*/
override fun logDbUsageInfo() {
Realm.getInstance(realmConfiguration).use { realm ->
val info = StringBuilder()
// Check if we have data
info.append("\n==============================================")
info.append("\n==============================================")
info.append("\nCrypto Realm is empty: ${realm.isEmpty}")
realmConfiguration.realmObjectClasses.forEach { modelClazz ->
val count = realm.where(modelClazz).count()
info.append("\nCrypto Realm - count ${modelClazz.simpleName}: $count")
}
info.append("\n==============================================")
info.append("\n==============================================")
Timber.i(info.toString())
}
}
}

View file

@ -18,7 +18,9 @@ package org.matrix.android.sdk.internal.session
import androidx.annotation.MainThread
import dagger.Lazy
import io.realm.Realm
import io.realm.RealmConfiguration
import io.realm.kotlin.where
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
@ -197,7 +199,7 @@ internal class DefaultSession @Inject constructor(
override fun close() {
assert(isOpen)
stopSync()
// timelineEventDecryptor.destroy()
// timelineEventDecryptor.destroy()
uiHandler.post {
lifecycleObservers.forEach { it.onStop() }
}
@ -284,4 +286,22 @@ internal class DefaultSession @Inject constructor(
override fun toString(): String {
return "$myUserId - ${sessionParams.deviceId}"
}
override fun dbgTraceDbInfo() {
Realm.getInstance(realmConfiguration).use { realm ->
val info = StringBuilder()
// Check if we have data
info.append("\n==============================================")
info.append("\n==============================================")
info.append("\nSession Realm is empty: ${realm.isEmpty}")
realmConfiguration.realmObjectClasses.forEach { modelClazz ->
val count = realm.where(modelClazz).count()
info.append("\nSession Realm - count ${modelClazz.simpleName}: $count")
}
info.append("\n==============================================")
info.append("\n==============================================")
Timber.i(info.toString())
}
}
}

View file

@ -446,6 +446,8 @@ class BugReporter @Inject constructor(
*/
fun openBugReportScreen(activity: FragmentActivity, forSuggestion: Boolean = false) {
screenshot = takeScreenshot(activity)
activeSessionHolder.getSafeActiveSession()?.dbgTraceDbInfo()
activeSessionHolder.getSafeActiveSession()?.cryptoService()?.logDbUsageInfo()
val intent = Intent(activity, BugReportActivity::class.java)
intent.putExtra("FOR_SUGGESTION", forSuggestion)