From 49dfe3f16ae38d680ca20903ee6d80885ec05b62 Mon Sep 17 00:00:00 2001 From: Aurimas Liutikas Date: Tue, 17 Nov 2026 09:19:42 -0800 Subject: [PATCH] Skip redundant SHA-256 hashing and GCS existence check in getOrUploadApk Previously, when getOrUploadApk missed GCS, calling apkStore.uploadApk(name, bytes) re-computed SHA-256 over the full APK byte array (even though sha256 was already known from TestRunConfig) and queried googleCloudApi.existingFilePath() a second time before uploading. - Add ApkStore.getOrUploadApk(name, sha256, bytes) that checks GCS existence once using the provided sha256 and uploads directly via putApk on a miss. - Route TestRunnerServiceImpl.getOrUploadApk and TestScheduler.PendingApk through ApkStore.getOrUploadApk. - Track existingFilePath queries in FakeGoogleCloudApi and add unit tests in ApkStoreTest and TestRunnerServiceImplTest. Test: ./gradlew :lib:ktlintCheck :lib:test --- .../dev/androidx/ci/testRunner/ApkStore.kt | 34 ++++++++++++++++++- .../ci/testRunner/TestRunnerServiceImpl.kt | 17 +++------- .../androidx/ci/testRunner/TestScheduler.kt | 8 +++-- .../androidx/ci/fake/FakeGoogleCloudApi.kt | 3 ++ .../androidx/ci/testRunner/ApkStoreTest.kt | 30 ++++++++++++++++ .../testRunner/TestRunnerServiceImplTest.kt | 6 ++++ 6 files changed, 83 insertions(+), 15 deletions(-) diff --git a/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/ApkStore.kt b/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/ApkStore.kt index 60ea8eba..c40e6922 100644 --- a/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/ApkStore.kt +++ b/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/ApkStore.kt @@ -66,9 +66,41 @@ internal class ApkStore( getUploadedApk(apkInfo)?.let { return it } + return putApk( + apkInfo = apkInfo, + bytes = bytes + ) + } + + /** + * Returns the APK for the given [name] and [sha256] if it already exists in the backend; otherwise obtains the + * APK contents via [bytes] and uploads it without re-computing SHA-256 or re-checking GCS existence. + */ + suspend fun getOrUploadApk( + name: String, + sha256: String, + bytes: suspend () -> ByteArray + ): UploadedApk { + val apkInfo = ApkInfo( + filePath = name, + idHash = sha256 + ) + getUploadedApk(apkInfo)?.let { + return it + } + return putApk( + apkInfo = apkInfo, + bytes = bytes() + ) + } + + private suspend fun putApk( + apkInfo: ApkInfo, + bytes: ByteArray + ): UploadedApk { val relativePath = apkInfo.gcpRelativePath() logger.info { - "uploading $name to $relativePath" + "uploading ${apkInfo.filePath} to $relativePath" } val gcsPath = googleCloudApi.upload( relativePath = relativePath, diff --git a/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/TestRunnerServiceImpl.kt b/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/TestRunnerServiceImpl.kt index 6c0e9c0d..278b56bb 100644 --- a/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/TestRunnerServiceImpl.kt +++ b/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/TestRunnerServiceImpl.kt @@ -78,18 +78,11 @@ internal class TestRunnerServiceImpl internal constructor( name: String, sha256: String, bytes: suspend () -> ByteArray - ): UploadedApk { - apkStore.getUploadedApk( - name = name, - sha256 = sha256 - )?.let { - return it - } - return apkStore.uploadApk( - name = name, - bytes = bytes() - ) - } + ): UploadedApk = apkStore.getOrUploadApk( + name = name, + sha256 = sha256, + bytes = bytes + ) override suspend fun getOrUploadRemoteApk( sourceGcsPath: GcsPath, diff --git a/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/TestScheduler.kt b/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/TestScheduler.kt index e8867d34..d44689bf 100644 --- a/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/TestScheduler.kt +++ b/AndroidXCI/lib/src/main/kotlin/dev/androidx/ci/testRunner/TestScheduler.kt @@ -262,8 +262,12 @@ internal sealed interface TestScheduler { return } if (scope.entry.name == name) { - val alreadyUploaded = apkStore.getUploadedApk(name, sha256) - uploadedApk = alreadyUploaded ?: apkStore.uploadApk(name, scope.bytes) + uploadedApk = apkStore.getOrUploadApk( + name = name, + sha256 = sha256 + ) { + scope.bytes + } } } } diff --git a/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/fake/FakeGoogleCloudApi.kt b/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/fake/FakeGoogleCloudApi.kt index b5a32537..1c26fcfa 100644 --- a/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/fake/FakeGoogleCloudApi.kt +++ b/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/fake/FakeGoogleCloudApi.kt @@ -34,6 +34,8 @@ internal class FakeGoogleCloudApi( ) : GoogleCloudApi { var uploadCount = 0 private set + var existingFilePathQueryCount = 0 + private set val rootGcsPath = GcsPath("gs://$bucketName") private val artifacts = mutableMapOf() @@ -85,6 +87,7 @@ internal class FakeGoogleCloudApi( } override suspend fun existingFilePath(relativePath: String): GcsPath? { + existingFilePathQueryCount++ val path = makeGcsPath(relativePath) return if (artifacts.containsKey(path)) { path diff --git a/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/testRunner/ApkStoreTest.kt b/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/testRunner/ApkStoreTest.kt index 6d246d05..1c6207bf 100644 --- a/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/testRunner/ApkStoreTest.kt +++ b/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/testRunner/ApkStoreTest.kt @@ -70,4 +70,34 @@ internal class ApkStoreTest { assertThat(gcpApi.uploadCount).isEqualTo(1) assertThat(gcpApi.artifacts()[placeholderApk.gcsPath]).isNotNull() } + + @Test + fun getOrUploadApk_usesProvidedShaAndChecksExistenceOnce() = runBlocking { + val apkBytes = byteArrayOf(10, 20, 30, 40) + val providedSha = "precomputed-sha256-from-config" + + val uploaded = apkStore.getOrUploadApk( + name = "sub/dir/test.apk", + sha256 = providedSha + ) { + apkBytes + } + + assertThat(gcpApi.existingFilePathQueryCount).isEqualTo(1) + assertThat(gcpApi.uploadCount).isEqualTo(1) + assertThat(uploaded.apkInfo.idHash).isEqualTo(providedSha) + assertThat(uploaded.gcsPath.path).isEqualTo("gs://my-bucket/sub/dir/test/$providedSha.apk") + assertThat(gcpApi.artifacts()[uploaded.gcsPath]).isEqualTo(apkBytes) + + val cached = apkStore.getOrUploadApk( + name = "sub/dir/test.apk", + sha256 = providedSha + ) { + error("bytes provider should not be called when APK already exists in GCS") + } + + assertThat(cached).isEqualTo(uploaded) + assertThat(gcpApi.existingFilePathQueryCount).isEqualTo(2) + assertThat(gcpApi.uploadCount).isEqualTo(1) + } } diff --git a/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/testRunner/TestRunnerServiceImplTest.kt b/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/testRunner/TestRunnerServiceImplTest.kt index f7563021..05b3a0ce 100644 --- a/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/testRunner/TestRunnerServiceImplTest.kt +++ b/AndroidXCI/lib/src/test/kotlin/dev/androidx/ci/testRunner/TestRunnerServiceImplTest.kt @@ -64,6 +64,9 @@ class TestRunnerServiceImplTest { assertThat( fakeBackend.fakeGoogleCloudApi.uploadCount ).isEqualTo(1) + assertThat( + fakeBackend.fakeGoogleCloudApi.existingFilePathQueryCount + ).isEqualTo(1) // re-upload val upload2 = subject.getOrUploadApk( name = "foo.apk", @@ -77,6 +80,9 @@ class TestRunnerServiceImplTest { assertThat( fakeBackend.fakeGoogleCloudApi.uploadCount ).isEqualTo(1) + assertThat( + fakeBackend.fakeGoogleCloudApi.existingFilePathQueryCount + ).isEqualTo(2) } @Test