diff --git a/docs/services/redshift.md b/docs/services/redshift.md index 6f45e9890f..c1b2020a99 100644 --- a/docs/services/redshift.md +++ b/docs/services/redshift.md @@ -10,6 +10,8 @@ Floci emulates Amazon Redshift by managing a real [PostgreSQL](https://www.postg The container has **no persistent volume**: if the physical container survives a Floci restart it is adopted and its data is kept, but if the container itself is gone (host reboot, `docker rm`, a pruned dev box) the cluster comes back empty. Use `CreateClusterSnapshot` / `RestoreFromClusterSnapshot` to preserve data explicitly. +Snapshot-dump and catalog-seed uploads retry transient Docker I/O failures for up to six attempts with a fresh upload each time. The SQL commands themselves are not retried. An exhausted snapshot upload fails the restore; an exhausted catalog-seed upload retains the warning-only startup behavior. + For running SQL without a PostgreSQL wire connection (the way Lambda and Step Functions do), see the [Redshift Data API](redshift-data.md). ## Supported Actions diff --git a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java index bdfab83d37..340e2ecf06 100644 --- a/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/redshift/container/RedshiftContainerManager.java @@ -12,18 +12,14 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.E import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer; import io.github.hectorvent.floci.core.common.docker.ContainerSpec; import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; +import io.github.hectorvent.floci.core.common.docker.RetryingTarCopier; import io.github.hectorvent.floci.services.redshift.model.Cluster; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import org.apache.commons.compress.archivers.tar.TarArchiveEntry; import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; -import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream; import org.jboss.logging.Logger; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.Closeable; -import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; @@ -45,6 +41,7 @@ public class RedshiftContainerManager { private final ContainerLogStreamer logStreamer; private final ContainerDetector containerDetector; private final EmulatorConfig config; + private final long retryBackoffMillis; private final Map containers = new ConcurrentHashMap<>(); @Inject @@ -53,11 +50,21 @@ public class RedshiftContainerManager { ContainerLogStreamer logStreamer, ContainerDetector containerDetector, EmulatorConfig config) { + this(containerBuilder, lifecycleManager, logStreamer, containerDetector, config, + RetryingTarCopier.DEFAULT_BACKOFF_MS); + } + + RedshiftContainerManager(ContainerBuilder containerBuilder, + ContainerLifecycleManager lifecycleManager, + ContainerLogStreamer logStreamer, + ContainerDetector containerDetector, + EmulatorConfig config, long retryBackoffMillis) { this.containerBuilder = containerBuilder; this.lifecycleManager = lifecycleManager; this.logStreamer = logStreamer; this.containerDetector = containerDetector; this.config = config; + this.retryBackoffMillis = retryBackoffMillis; } public RedshiftContainerHandle start(String accountId, String clusterIdentifier, String masterUsername, String masterPassword) { @@ -328,10 +335,8 @@ public class RedshiftContainerManager { String fileName = sqlDumpFile.getFileName().toString(); try { - lifecycleManager.getDockerClient().copyArchiveToContainerCmd(handle.getContainerId()) - .withHostResource(sqlDumpFile.toString()) - .withRemotePath("/tmp") - .exec(); + RetryingTarCopier.copyHostResource(lifecycleManager.getDockerClient(), handle.getContainerId(), + "/tmp", sqlDumpFile.toString(), RetryingTarCopier.DEFAULT_MAX_ATTEMPTS, retryBackoffMillis); String[] cmd = stopOnError ? new String[]{"psql", "-U", effectiveUser, "-d", effectiveDb, "-v", "ON_ERROR_STOP=1", @@ -399,19 +404,6 @@ public class RedshiftContainerManager { return ContainerExec.run(lifecycleManager.getDockerClient(), containerId, cmd, timeoutSeconds); } - private byte[] buildSingleFileTar(String filename, byte[] content, int mode) throws IOException { - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - try (TarArchiveOutputStream tar = new TarArchiveOutputStream(bos)) { - tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); - TarArchiveEntry entry = new TarArchiveEntry(filename); - entry.setSize(content.length); - entry.setMode(mode); - tar.putArchiveEntry(entry); - tar.write(content); - tar.closeArchiveEntry(); - } - return bos.toByteArray(); - } private void waitForReady(String containerName, String containerId, String username, String dbName) { String effectiveUser = userOrDefault(username); String[] cmd = { @@ -461,11 +453,9 @@ public class RedshiftContainerManager { return; } byte[] sqlBytes = in.readAllBytes(); - byte[] tarBytes = buildSingleFileTar("bootstrap-catalog.sql", sqlBytes, 0644); - lifecycleManager.getDockerClient().copyArchiveToContainerCmd(containerId) - .withTarInputStream(new ByteArrayInputStream(tarBytes)) - .withRemotePath("/tmp") - .exec(); + RetryingTarCopier.copyBytes(lifecycleManager.getDockerClient(), containerId, "/tmp", + "bootstrap-catalog.sql", sqlBytes, 0644, + RetryingTarCopier.DEFAULT_MAX_ATTEMPTS, retryBackoffMillis); List targetDbs = effectiveDb.equals("template1") ? List.of("template1") diff --git a/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftArchiveRetryTest.java b/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftArchiveRetryTest.java new file mode 100644 index 0000000000..4996e65108 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/redshift/container/RedshiftArchiveRetryTest.java @@ -0,0 +1,268 @@ +package io.github.hectorvent.floci.services.redshift.container; + +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.command.CopyArchiveToContainerCmd; +import com.github.dockerjava.api.exception.DockerClientException; +import com.github.dockerjava.api.exception.NotFoundException; +import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.docker.ContainerBuilder; +import io.github.hectorvent.floci.core.common.docker.ContainerDetector; +import io.github.hectorvent.floci.core.common.docker.ContainerExecStubs; +import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager; +import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.ContainerInfo; +import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager.EndpointInfo; +import io.github.hectorvent.floci.core.common.docker.ContainerLogStreamer; +import io.github.hectorvent.floci.testing.DockerArchiveUploadFixture; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.InOrder; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.RETURNS_SELF; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class RedshiftArchiveRetryTest { + private static final String ACCOUNT = "111111111111"; + private static final String CONTAINER = "redshift-id"; + + @TempDir + Path temporary; + + private DockerClient docker; + private RedshiftContainerManager manager; + + @BeforeEach + void setUp() { + docker = mock(DockerClient.class); + ContainerBuilder builder = mock(ContainerBuilder.class); + when(builder.newContainer(anyString())).thenReturn(mock(ContainerBuilder.Builder.class, RETURNS_SELF)); + ContainerLifecycleManager lifecycle = mock(ContainerLifecycleManager.class); + when(lifecycle.getDockerClient()).thenReturn(docker); + when(lifecycle.createAndStart(any())).thenReturn( + new ContainerInfo(CONTAINER, Map.of(5432, new EndpointInfo("localhost", 5432)))); + EmulatorConfig config = mock(EmulatorConfig.class, RETURNS_DEEP_STUBS); + when(config.services().redshift().imageVersion()).thenReturn("postgres:16-alpine"); + when(config.services().redshift().dockerNetwork()).thenReturn(Optional.empty()); + when(config.defaultRegion()).thenReturn("us-east-1"); + ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", ""); + when(docker.copyArchiveToContainerCmd(CONTAINER)).thenReturn(mock(CopyArchiveToContainerCmd.class, RETURNS_SELF)); + manager = new RedshiftContainerManager(builder, lifecycle, mock(ContainerLogStreamer.class), + mock(ContainerDetector.class), config, 0); + manager.start(ACCOUNT, "cluster", "admin", "Secret123"); + } + + @ParameterizedTest + @ValueSource(ints = {0, 73, 8192}) + void startupRetriesConsumedCatalogUploadBeforeRunningSql(int consumedBytes) throws Exception { + List> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", ""); + DockerArchiveUploadFixture uploads = new DockerArchiveUploadFixture(docker, CONTAINER); + uploads.failures = 1; + uploads.consumedBytes = consumedBytes; + uploads.failure = transientFailure(); + + manager.start(ACCOUNT, "retrying-cluster", "admin", "Secret123"); + + assertEquals(2, uploads.commands.size()); + assertEquals(1, uploads.completed.size()); + assertNotSame(uploads.streams.get(0), uploads.streams.get(1)); + verify(uploads.commands.get(1)).withRemotePath("/tmp"); + try (TarArchiveInputStream tar = new TarArchiveInputStream(new ByteArrayInputStream(uploads.completed.getFirst())); + InputStream source = getClass().getResourceAsStream("/redshift/bootstrap-catalog.sql")) { + TarArchiveEntry entry = tar.getNextEntry(); + assertEquals("bootstrap-catalog.sql", entry.getName()); + assertEquals(0644, entry.getMode()); + assertArrayEquals(source.readAllBytes(), tar.readAllBytes()); + assertNull(tar.getNextEntry()); + } + assertEquals(List.of( + List.of("psql", "-h", "127.0.0.1", "-v", "ON_ERROR_STOP=1", "-U", "admin", "-d", "dev", "-c", "SELECT 1"), + catalogCommand("template1"), catalogCommand("dev")), commands); + InOrder order = inOrder(uploads.commands.get(1), docker); + order.verify(uploads.commands.get(1)).exec(); + order.verify(docker, times(2)).execCreateCmd(CONTAINER); + } + + @Test + void exhaustedCatalogUploadStaysOptionalAndDoesNotRunSql() { + List> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", ""); + DockerArchiveUploadFixture uploads = new DockerArchiveUploadFixture(docker, CONTAINER); + uploads.failures = 6; + uploads.consumedBytes = 73; + uploads.failure = transientFailure(); + + assertDoesNotThrow(() -> manager.bootstrapCatalog(CONTAINER, "admin", "dev")); + + assertEquals(6, uploads.commands.size()); + assertTrue(uploads.completed.isEmpty()); + assertTrue(commands.isEmpty()); + } + + @Test + void catalogDaemonRejectionIsNotRetried() { + List> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", ""); + DockerArchiveUploadFixture uploads = new DockerArchiveUploadFixture(docker, CONTAINER); + uploads.failures = 6; + uploads.failure = new NotFoundException("container missing"); + + assertDoesNotThrow(() -> manager.bootstrapCatalog(CONTAINER, "admin", "dev")); + + assertEquals(1, uploads.commands.size()); + assertTrue(commands.isEmpty()); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void snapshotUploadRetriesFreshHostResourceCommandsBeforeOneRestore(boolean stopOnError) throws Exception { + Path dump = Files.writeString(temporary.resolve("snapshot.sql"), "SELECT 42;"); + List> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", ""); + CopyArchiveToContainerCmd failed = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF); + CopyArchiveToContainerCmd succeeded = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF); + when(docker.copyArchiveToContainerCmd(CONTAINER)).thenReturn(failed, succeeded); + doThrow(transientFailure()).when(failed).exec(); + + manager.restoreSnapshot(ACCOUNT, "cluster", "admin", "analytics", dump, stopOnError); + + verify(failed).withHostResource(dump.toString()); + verify(succeeded).withHostResource(dump.toString()); + verify(succeeded).withRemotePath("/tmp"); + verify(succeeded).exec(); + List expected = new ArrayList<>(List.of("psql", "-U", "admin", "-d", "analytics")); + if (stopOnError) { + expected.addAll(List.of("-v", "ON_ERROR_STOP=1")); + } + expected.addAll(List.of("-f", "/tmp/snapshot.sql")); + assertEquals(List.of(expected), commands); + InOrder order = inOrder(succeeded, docker); + order.verify(succeeded).exec(); + order.verify(docker).execCreateCmd(CONTAINER); + } + + @Test + void exhaustedSnapshotUploadKeepsInternalFailureAndDoesNotRunSql() throws Exception { + snapshotUploadFailure(transientFailure(), 6); + } + + @Test + void snapshotDaemonRejectionIsNotRetried() throws Exception { + snapshotUploadFailure(new NotFoundException("container missing"), 1); + } + + @Test + void localHostArchiveFailureIsNotRetried() throws Exception { + snapshotUploadFailure(new DockerClientException("Unable to perform tar on host resource snapshot.sql", + new IOException("source unreadable")), 1); + } + + @Test + void failedSnapshotSqlIsNotRetried() throws Exception { + Path dump = Files.writeString(temporary.resolve("snapshot.sql"), "invalid SQL;"); + List> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 1, "", "invalid SQL"); + CopyArchiveToContainerCmd upload = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF); + when(docker.copyArchiveToContainerCmd(CONTAINER)).thenReturn(upload); + + AwsException error = assertThrows(AwsException.class, + () -> manager.restoreSnapshot(ACCOUNT, "cluster", "admin", "dev", dump, true)); + + assertEquals("InternalFailure", error.getErrorCode()); + verify(upload).exec(); + assertEquals(1, commands.size()); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void transientSnapshotSqlFailureDoesNotRepeatUploadOrSql(boolean stopOnError) throws Exception { + Path dump = Files.writeString(temporary.resolve("snapshot.sql"), "SELECT 42;"); + List> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", ""); + CopyArchiveToContainerCmd upload = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF); + when(docker.copyArchiveToContainerCmd(CONTAINER)).thenReturn(upload); + when(docker.execStartCmd(anyString())).thenThrow(transientFailure()); + + AwsException error = assertThrows(AwsException.class, + () -> manager.restoreSnapshot(ACCOUNT, "cluster", "admin", "dev", dump, stopOnError)); + + assertEquals("InternalFailure", error.getErrorCode()); + assertEquals(500, error.getHttpStatus()); + verify(upload).exec(); + assertEquals(1, commands.size()); + } + + @Test + void transientCatalogSqlFailureStaysOptionalWithoutRepeatingUploadOrSql() { + List> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", ""); + DockerArchiveUploadFixture uploads = new DockerArchiveUploadFixture(docker, CONTAINER); + when(docker.execStartCmd(anyString())).thenThrow(transientFailure()); + + assertDoesNotThrow(() -> manager.bootstrapCatalog(CONTAINER, "admin", "dev")); + + assertEquals(1, uploads.commands.size()); + assertEquals(1, uploads.completed.size()); + assertEquals(List.of(catalogCommand("template1")), commands); + } + + @Test + void absentSnapshotDoesNotUploadOrRunSql() { + List> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", ""); + DockerArchiveUploadFixture uploads = new DockerArchiveUploadFixture(docker, CONTAINER); + + manager.restoreSnapshot(ACCOUNT, "cluster", "admin", "dev", null); + manager.restoreSnapshot(ACCOUNT, "cluster", "admin", "dev", temporary.resolve("missing.sql")); + + assertTrue(uploads.commands.isEmpty()); + assertTrue(commands.isEmpty()); + } + + private void snapshotUploadFailure(RuntimeException failure, int expectedAttempts) throws Exception { + Path dump = Files.writeString(temporary.resolve("snapshot.sql"), "SELECT 42;"); + List> commands = ContainerExecStubs.completeEveryExec(docker, CONTAINER, 0, "", ""); + CopyArchiveToContainerCmd upload = mock(CopyArchiveToContainerCmd.class, RETURNS_SELF); + when(docker.copyArchiveToContainerCmd(CONTAINER)).thenReturn(upload); + doThrow(failure).when(upload).exec(); + + AwsException error = assertThrows(AwsException.class, + () -> manager.restoreSnapshot(ACCOUNT, "cluster", "admin", "dev", dump)); + + assertEquals("InternalFailure", error.getErrorCode()); + assertEquals(500, error.getHttpStatus()); + verify(upload, times(expectedAttempts)).exec(); + assertTrue(commands.isEmpty()); + } + + private static RuntimeException transientFailure() { + return new RuntimeException(new IOException("Connection reset")); + } + + private static List catalogCommand(String database) { + return List.of("psql", "-h", "127.0.0.1", "-v", "ON_ERROR_STOP=1", "-U", "admin", + "-d", database, "-f", "/tmp/bootstrap-catalog.sql"); + } +}