Move envelope follow-up operations outside of the transaction.
This commit is contained in:
parent
297308ad76
commit
a8e02b9ced
3 changed files with 18 additions and 16 deletions
|
@ -6,6 +6,7 @@ import androidx.annotation.VisibleForTesting
|
||||||
import net.zetetic.database.sqlcipher.SQLiteOpenHelper
|
import net.zetetic.database.sqlcipher.SQLiteOpenHelper
|
||||||
import org.signal.core.util.SqlUtil
|
import org.signal.core.util.SqlUtil
|
||||||
import org.signal.core.util.logging.Log
|
import org.signal.core.util.logging.Log
|
||||||
|
import org.signal.core.util.withinTransaction
|
||||||
import org.thoughtcrime.securesms.crypto.AttachmentSecret
|
import org.thoughtcrime.securesms.crypto.AttachmentSecret
|
||||||
import org.thoughtcrime.securesms.crypto.DatabaseSecret
|
import org.thoughtcrime.securesms.crypto.DatabaseSecret
|
||||||
import org.thoughtcrime.securesms.crypto.MasterSecret
|
import org.thoughtcrime.securesms.crypto.MasterSecret
|
||||||
|
@ -345,13 +346,9 @@ open class SignalDatabase(private val context: Application, databaseSecret: Data
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun runInTransaction(operation: Runnable) {
|
fun <T> runInTransaction(block: (SQLiteDatabase) -> T): T {
|
||||||
instance!!.signalWritableDatabase.beginTransaction()
|
return instance!!.signalWritableDatabase.withinTransaction {
|
||||||
try {
|
block(it)
|
||||||
operation.run()
|
|
||||||
instance!!.signalWritableDatabase.setTransactionSuccessful()
|
|
||||||
} finally {
|
|
||||||
instance!!.signalWritableDatabase.endTransaction()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -292,10 +292,11 @@ public class RetrieveProfileJob extends BaseJob {
|
||||||
|
|
||||||
//noinspection SimplifyStreamApiCallChains
|
//noinspection SimplifyStreamApiCallChains
|
||||||
ListUtil.chunk(operationState.profiles, 150).stream().forEach(list -> {
|
ListUtil.chunk(operationState.profiles, 150).stream().forEach(list -> {
|
||||||
SignalDatabase.runInTransaction(() -> {
|
SignalDatabase.runInTransaction((db) -> {
|
||||||
for (Pair<Recipient, ProfileAndCredential> profile : list) {
|
for (Pair<Recipient, ProfileAndCredential> profile : list) {
|
||||||
process(profile.first(), profile.second());
|
process(profile.first(), profile.second());
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -392,18 +392,22 @@ class IncomingMessageObserver(private val context: Application) {
|
||||||
val startTime = System.currentTimeMillis()
|
val startTime = System.currentTimeMillis()
|
||||||
GroupsV2ProcessingLock.acquireGroupProcessingLock().use {
|
GroupsV2ProcessingLock.acquireGroupProcessingLock().use {
|
||||||
ReentrantSessionLock.INSTANCE.acquire().use {
|
ReentrantSessionLock.INSTANCE.acquire().use {
|
||||||
batch.forEach {
|
batch.forEach { response ->
|
||||||
Log.d(TAG, "Beginning database transaction...")
|
Log.d(TAG, "Beginning database transaction...")
|
||||||
SignalDatabase.runInTransaction {
|
val followUpOperations = SignalDatabase.runInTransaction { db ->
|
||||||
val followUpOperations: List<FollowUpOperation>? = processEnvelope(bufferedStore, it.envelope, it.serverDeliveredTimestamp)
|
val followUps: List<FollowUpOperation>? = processEnvelope(bufferedStore, response.envelope, response.serverDeliveredTimestamp)
|
||||||
bufferedStore.flushToDisk()
|
bufferedStore.flushToDisk()
|
||||||
if (followUpOperations != null) {
|
followUps
|
||||||
val jobs = followUpOperations.mapNotNull { it.run() }
|
|
||||||
ApplicationDependencies.getJobManager().addAll(jobs)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Log.d(TAG, "Ended database transaction.")
|
Log.d(TAG, "Ended database transaction.")
|
||||||
signalWebSocket.sendAck(it)
|
|
||||||
|
if (followUpOperations != null) {
|
||||||
|
Log.d(TAG, "Running ${followUpOperations.size} follow-up operations...")
|
||||||
|
val jobs = followUpOperations.mapNotNull { it.run() }
|
||||||
|
ApplicationDependencies.getJobManager().addAll(jobs)
|
||||||
|
}
|
||||||
|
|
||||||
|
signalWebSocket.sendAck(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue