mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 17:33:03 -05:00
fix(rds): retry grant and snapshot archive uploads (#6069)
* fix(rds): retry master grant archive uploads * fix(rds): retry snapshot restore archive uploads
This commit is contained in:
1 parent
9d1565fda3
commit
1a598b7f0c
3 files changed
+255
-29
No files matched your search
+25
-29
@@ -19,15 +19,14 @@ import io.github.hectorvent.floci.core.common.docker.ContainerLiveness;
|
||||
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.rds.model.DatabaseEngine;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
|
||||
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;
|
||||
@@ -68,6 +67,7 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
private final EmulatorConfig config;
|
||||
private final RegionResolver regionResolver;
|
||||
private final ServiceConfigAccess serviceConfigAccess;
|
||||
private final long uploadBackoffMillis;
|
||||
private final Map<String, RdsContainerHandle> activeContainers = new ConcurrentHashMap<>();
|
||||
private final Map<String, String> activeStorageOwners = new ConcurrentHashMap<>();
|
||||
private final Set<String> claimedRuntimes = ConcurrentHashMap.newKeySet();
|
||||
@@ -105,6 +105,18 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver,
|
||||
ServiceConfigAccess serviceConfigAccess) {
|
||||
this(containerBuilder, lifecycleManager, logStreamer, containerDetector, config,
|
||||
regionResolver, serviceConfigAccess, RetryingTarCopier.DEFAULT_BACKOFF_MS);
|
||||
}
|
||||
|
||||
RdsContainerManager(ContainerBuilder containerBuilder,
|
||||
ContainerLifecycleManager lifecycleManager,
|
||||
ContainerLogStreamer logStreamer,
|
||||
ContainerDetector containerDetector,
|
||||
EmulatorConfig config,
|
||||
RegionResolver regionResolver,
|
||||
ServiceConfigAccess serviceConfigAccess,
|
||||
long uploadBackoffMillis) {
|
||||
this.containerBuilder = containerBuilder;
|
||||
this.lifecycleManager = lifecycleManager;
|
||||
this.logStreamer = logStreamer;
|
||||
@@ -112,6 +124,7 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
this.config = config;
|
||||
this.regionResolver = regionResolver;
|
||||
this.serviceConfigAccess = serviceConfigAccess;
|
||||
this.uploadBackoffMillis = uploadBackoffMillis;
|
||||
}
|
||||
|
||||
public RdsContainerHandle start(String instanceId, String volumeId, DatabaseEngine engine,
|
||||
@@ -846,23 +859,9 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
*/
|
||||
private void installMasterGrantInitScript(String containerId, String masterUsername) {
|
||||
byte[] sql = mysqlMasterGrantSql(masterUsername).getBytes(StandardCharsets.UTF_8);
|
||||
try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
|
||||
try (TarArchiveOutputStream tar = new TarArchiveOutputStream(bos)) {
|
||||
TarArchiveEntry entry = new TarArchiveEntry("floci-master-grants.sql");
|
||||
entry.setSize(sql.length);
|
||||
entry.setMode(0644);
|
||||
tar.putArchiveEntry(entry);
|
||||
tar.write(sql);
|
||||
tar.closeArchiveEntry();
|
||||
}
|
||||
lifecycleManager.getDockerClient().copyArchiveToContainerCmd(containerId)
|
||||
.withRemotePath("/docker-entrypoint-initdb.d")
|
||||
.withTarInputStream(new ByteArrayInputStream(bos.toByteArray()))
|
||||
.exec();
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(
|
||||
"Failed to install the master-grant init script into container " + containerId, e);
|
||||
}
|
||||
RetryingTarCopier.copyBytes(lifecycleManager.getDockerClient(), containerId,
|
||||
"/docker-entrypoint-initdb.d", "floci-master-grants.sql", sql, 0644,
|
||||
RetryingTarCopier.DEFAULT_MAX_ATTEMPTS, uploadBackoffMillis);
|
||||
}
|
||||
|
||||
private void execUntilSuccess(String containerName, String containerId, String[] cmd, String description) {
|
||||
@@ -962,21 +961,18 @@ public class RdsContainerManager implements RdsBackendGate, Resettable {
|
||||
try (Lease _ = holdContainer(containerId)) {
|
||||
String restoreScript = postgresRestoreScript();
|
||||
|
||||
java.io.ByteArrayOutputStream bos = new java.io.ByteArrayOutputStream();
|
||||
try (org.apache.commons.compress.archivers.tar.TarArchiveOutputStream tar =
|
||||
new org.apache.commons.compress.archivers.tar.TarArchiveOutputStream(bos)) {
|
||||
tar.setLongFileMode(org.apache.commons.compress.archivers.tar.TarArchiveOutputStream.LONGFILE_GNU);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
try (TarArchiveOutputStream tar = new TarArchiveOutputStream(bos)) {
|
||||
tar.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
|
||||
|
||||
addTarEntry(tar, "dump.sql", sqlDump);
|
||||
addTarEntry(tar, "restore.sh", restoreScript);
|
||||
}
|
||||
|
||||
try (java.io.InputStream tarStream = new java.io.ByteArrayInputStream(bos.toByteArray())) {
|
||||
lifecycleManager.getDockerClient().copyArchiveToContainerCmd(containerId)
|
||||
.withRemotePath("/tmp")
|
||||
.withTarInputStream(tarStream)
|
||||
.exec();
|
||||
}
|
||||
byte[] archive = bos.toByteArray();
|
||||
RetryingTarCopier.copyStreamed(lifecycleManager.getDockerClient(), containerId,
|
||||
"/tmp", "postgres-restore", out -> out.write(archive),
|
||||
RetryingTarCopier.DEFAULT_MAX_ATTEMPTS, uploadBackoffMillis);
|
||||
|
||||
String[] cmd = { "sh", "/tmp/restore.sh", effectiveUser };
|
||||
|
||||
|
||||
Reference in new issue
Block a user