diff --git a/src/main/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporter.java b/src/main/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporter.java index 93e58f4dda..4f381c8931 100644 --- a/src/main/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporter.java +++ b/src/main/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporter.java @@ -294,7 +294,8 @@ public class CodeArtifactPypiExternalConnectionImporter { importedRequested = importedAny; return importedRequested; } - String importLockKey = repoId + "|" + packageName.get() + "|" + version.get(); + String importLockKey = repoId + "|" + PypiserverSidecarClient.normalizePackageName(packageName.get()) + + "|" + version.get(); // Splits the version's own files into the one filename actually requested (imported // synchronously below, so downloadFile's own proxy retry has real bytes to serve the diff --git a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporterTest.java b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporterTest.java index fcfd5b6e38..3f276e0941 100644 --- a/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporterTest.java +++ b/src/test/java/io/floci/aws/services/codeartifact/CodeArtifactPypiExternalConnectionImporterTest.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import io.floci.aws.core.common.AwsException; import io.floci.aws.services.codeartifact.model.CodeArtifactRepository; import io.floci.aws.services.codeartifact.model.ExternalConnection; +import io.floci.aws.testing.ThreadStates; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InOrder; @@ -25,6 +26,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -532,6 +534,60 @@ class CodeArtifactPypiExternalConnectionImporterTest { any(), anyLong(), anyLong(), anyLong(), any(), eq(SDIST_FILENAME), anyLong(), any()); } + @Test + void hyphenatedSdistAndUnderscoredWheelShareTheRetentionLock() throws Exception { + String sdist = "widget-name-1.0.0.tar.gz"; + String wheel = "widget_name-1.0.0-py3-none-any.whl"; + CountDownLatch firstWheelWrite = new CountDownLatch(1); + CountDownLatch releaseWheelWrite = new CountDownLatch(1); + AtomicBoolean sdistWritten = new AtomicBoolean(false); + AtomicBoolean wheelWritten = new AtomicBoolean(false); + AtomicInteger wheelWrites = new AtomicInteger(); + when(externalConnectionClient.fetchSimpleIndex(eq("public:pypi"), anyString())) + .thenReturn(Optional.of(new PypiExternalConnectionClient.FetchedAsset( + simpleIndexJson(List.of(VERSION), sdist, wheel)))); + stubFetchFile(urlFor(sdist), SDIST_CONTENT); + stubFetchFile(urlFor(wheel), WHEEL_CONTENT); + when(pypiserver.listPackageVersionAssetNames(anyString(), anyString(), anyString(), any(), anyString(), + anyString())).thenAnswer(invocation -> wheelWritten.get() ? List.of(sdist, wheel) + : sdistWritten.get() ? List.of(sdist) : List.of()); + when(service.retainedVersionOriginConnection(anyString(), anyString(), anyString(), anyString(), + anyString(), any(), anyString(), anyString())).thenReturn(Optional.of("public:pypi")); + when(service.retainAssetUnderVersionFence(any(), any(), any(), any(), any(), any(), any(), any(), + anyLong(), anyLong(), anyLong(), any(), eq(sdist), anyLong(), any())).thenAnswer(invocation -> { + sdistWritten.set(true); + return new CodeArtifactService.AssetRetentionAttemptResult(true, true); + }); + when(service.retainAssetUnderVersionFence(any(), any(), any(), any(), any(), any(), any(), any(), + anyLong(), anyLong(), anyLong(), any(), eq(wheel), anyLong(), any())).thenAnswer(invocation -> { + if (wheelWrites.incrementAndGet() == 1) { + firstWheelWrite.countDown(); + assertTrue(releaseWheelWrite.await(5, TimeUnit.SECONDS), "wheel write was never released"); + } + wheelWritten.set(true); + return new CodeArtifactService.AssetRetentionAttemptResult(true, true); + }); + ExecutorService pool = Executors.newSingleThreadExecutor(); + try { + assertTrue(importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, sdist)); + assertTrue(firstWheelWrite.await(5, TimeUnit.SECONDS), "background wheel write never started"); + AtomicReference requestThread = new AtomicReference<>(); + Future requestedWheel = pool.submit(() -> { + requestThread.set(Thread.currentThread()); + return importer.tryImportAsset(REGION, DOMAIN, OWNER, REPOSITORY, REPO_ID, wheel); + }); + ThreadStates.awaitBlockedOrDone(requestThread, requestedWheel); + assertFalse(requestedWheel.isDone(), "the wheel request must wait for the background sibling"); + assertEquals(Thread.State.BLOCKED, requestThread.get().getState()); + releaseWheelWrite.countDown(); + assertTrue(requestedWheel.get(5, TimeUnit.SECONDS)); + assertEquals(1, wheelWrites.get()); + } finally { + releaseWheelWrite.countDown(); + pool.shutdownNow(); + } + } + private static String urlFor(String filename) { return "https://files.pythonhosted.org/packages/aa/bb/" + filename; }