Repository navigation
fix(migration): stream export manifest into zip to bound memory (COLUMBA-DS) #1189
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
5d61e4a
fix(migration): stream export manifest into zip to bound memory (COLU…
torlando-tech a70a41c
test(migration): heap-capped repro for the export manifest OOM (COLUM…
torlando-tech af49a88
address greptile review feedback (greploop iteration 1)
torlando-tech 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 hidden or 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 hidden or 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
157 changes: 157 additions & 0 deletions
157
app/src/test/java/network/columba/app/migration/MigrationExporterOomTest.kt
This file contains hidden or 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,157 @@ | ||
| package network.columba.app.migration | ||
|
|
||
| import android.content.Context | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import kotlinx.serialization.json.Json | ||
| import kotlinx.serialization.json.decodeFromStream | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
| import java.io.File | ||
| import java.util.zip.ZipFile | ||
|
|
||
| /** | ||
| * Repro for COLUMBA-DS: the export manifest used to be serialized with | ||
| * `json.encodeToString(bundle)`, which buffers the full manifest as a | ||
| * char array, a String, and a byte array on top of the in-memory bundle. | ||
| * A large bundle OOMs the process once the manifest outgrows the heap headroom. | ||
| * | ||
| * [MigrationExporterOomTest] forks this [MigrationExportOomChild] main in a | ||
| * separate JVM capped at 256 MB (the production target footprint) and runs | ||
| * the real [MigrationExporter.createExportZip]. The bundle is sized so the | ||
| * whole-string encoding needs ~3x the manifest text (~300+ MB live) while | ||
| * the streaming encoder stays bounded to the bundle plus small buffers. | ||
| * Pre-fix the child dies with `OutOfMemoryError`; post-fix it completes and | ||
| * the manifest round-trips through the import-side `decodeFromStream`. | ||
| */ | ||
| @Suppress("NoRelaxedMocks") // createExportZip only touches context.cacheDir/filesDir; collaborators are unused | ||
| object MigrationExportOomChild { | ||
| private const val MESSAGE_COUNT = 200_000 | ||
| private const val CONTENT_LENGTH = 384 | ||
| private const val MANIFEST_FILENAME = "manifest.json" | ||
|
|
||
| @JvmStatic | ||
| fun main(args: Array<String>) { | ||
| val json = Json { ignoreUnknownKeys = true } | ||
| val workDir = | ||
| File.createTempFile("columba-oom-repro", null).apply { | ||
| delete() | ||
| mkdirs() | ||
| } | ||
|
torlando-tech marked this conversation as resolved.
|
||
| val context = | ||
| mockk<Context>().apply { | ||
| every { cacheDir } returns File(workDir, "cache").apply { mkdirs() } | ||
| every { filesDir } returns File(workDir, "files").apply { mkdirs() } | ||
| } | ||
|
|
||
| val exporter = | ||
| MigrationExporter( | ||
| context, | ||
| mockk(relaxed = true), | ||
| mockk(relaxed = true), | ||
| mockk(relaxed = true), | ||
| mockk(relaxed = true), | ||
| mockk(relaxed = true), | ||
| ) | ||
|
|
||
| val bundle = | ||
| MigrationBundle( | ||
| identities = emptyList(), | ||
| conversations = emptyList(), | ||
| messages = buildMessages(), | ||
| contacts = emptyList(), | ||
| settings = | ||
| SettingsExport( | ||
| notificationsEnabled = true, | ||
| notificationReceivedMessage = true, | ||
| notificationReceivedMessageFavorite = false, | ||
| notificationHeardAnnounce = true, | ||
| notificationBleConnected = false, | ||
| notificationBleDisconnected = false, | ||
| autoAnnounceEnabled = true, | ||
| autoAnnounceIntervalMinutes = 5, | ||
| themePreference = "preset:VIBRANT", | ||
| ), | ||
| ) | ||
|
|
||
| // createExportZip is private; invoke the production method directly. | ||
| val method = | ||
| MigrationExporter::class.java.getDeclaredMethod( | ||
| "createExportZip", | ||
| MigrationBundle::class.java, | ||
| List::class.java, | ||
| Function1::class.java, | ||
| ).apply { isAccessible = true } | ||
| val onProgress: Function1<Float, Unit> = { } | ||
| val exportFile = method.invoke(exporter, bundle, emptyList<Any>(), onProgress) as File | ||
| check(exportFile.exists()) { "export file missing: $exportFile" } | ||
|
|
||
| // The streamed manifest must decode through the import-side path. | ||
| ZipFile(exportFile).use { zip -> | ||
| val entry = zip.getEntry(MANIFEST_FILENAME) ?: error("missing $MANIFEST_FILENAME") | ||
| zip.getInputStream(entry).use { stream -> | ||
| val decoded = json.decodeFromStream(MigrationBundle.serializer(), stream) | ||
| check(decoded.messages.size == MESSAGE_COUNT) { | ||
| "manifest message count mismatch: ${decoded.messages.size}" | ||
| } | ||
| } | ||
| } | ||
| println("OK messages=$MESSAGE_COUNT size=${exportFile.length()}") | ||
| } | ||
|
|
||
| private fun buildMessages(): List<MessageExport> { | ||
| val content = "x".repeat(CONTENT_LENGTH) | ||
| val status = "SENT" | ||
| val identityHash = "identity-1" | ||
| return List(MESSAGE_COUNT) { i -> | ||
| MessageExport( | ||
| id = "message-$i", | ||
| conversationHash = "conversation-${i % 1000}", | ||
| identityHash = identityHash, | ||
| content = content, | ||
| timestamp = i.toLong(), | ||
| isFromMe = i % 2 == 0, | ||
| status = status, | ||
| isRead = i % 3 == 0, | ||
| fieldsJson = null, | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class MigrationExporterOomTest { | ||
| /** | ||
| * Runs the production export zip path under a 256 MB heap cap. Fails with | ||
| * the pre-fix `encodeToString` implementation (child OOMs) and passes with | ||
| * the streaming implementation. | ||
| */ | ||
| @Test | ||
| fun `export zip manifest stays bounded under a 256MB heap (COLUMBA-DS)`() { | ||
| val javaExe = File(System.getProperty("java.home"), "bin/java").absolutePath | ||
| val classpath = System.getProperty("java.class.path") | ||
|
torlando-tech marked this conversation as resolved.
Outdated
|
||
| val process = | ||
| ProcessBuilder( | ||
| javaExe, | ||
| "-Xmx256m", | ||
| // mockk self-attaches a ByteBuddy agent in the forked JVM. | ||
| "-Djdk.attach.allowAttachSelf=true", | ||
| "-cp", | ||
| classpath, | ||
| "network.columba.app.migration.MigrationExportOomChild", | ||
| ) | ||
| .redirectErrorStream(true) | ||
| .start() | ||
| val output = process.inputStream.bufferedReader().readText() | ||
| val exitCode = process.waitFor() | ||
| assertEquals( | ||
| "createExportZip must complete under a 256MB heap (COLUMBA-DS). Child output:\n$output", | ||
| 0, | ||
| exitCode, | ||
| ) | ||
| assertTrue( | ||
| "child must report a verified manifest. Output:\n$output", | ||
| output.contains("OK messages="), | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.