mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(rds): keep instance available when the liveness probe fails (#5070)
Only a definite not-running answer from Docker marks an instance failed. * Fix #4847 Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
32f01ba13d
commit
97906c09b9
6 files changed
+183
-17
No files matched your search
+100
@@ -0,0 +1,100 @@
|
||||
package io.github.hectorvent.floci.core.common.docker;
|
||||
|
||||
import com.github.dockerjava.api.DockerClient;
|
||||
import com.github.dockerjava.api.command.InspectContainerCmd;
|
||||
import com.github.dockerjava.api.command.InspectContainerResponse;
|
||||
import com.github.dockerjava.api.exception.NotFoundException;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ContainerLifecycleManagerLivenessTest {
|
||||
|
||||
@Mock
|
||||
private DockerClient dockerClient;
|
||||
|
||||
@Mock
|
||||
private ImageCacheService imageCacheService;
|
||||
|
||||
@Mock
|
||||
private ContainerDetector containerDetector;
|
||||
|
||||
@Mock
|
||||
private PortAllocator portAllocator;
|
||||
|
||||
@Mock
|
||||
private EmulatorConfig config;
|
||||
|
||||
@Mock
|
||||
private InspectContainerCmd inspectCmd;
|
||||
|
||||
private ContainerLifecycleManager manager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
manager = new ContainerLifecycleManager(
|
||||
dockerClient, imageCacheService, containerDetector, portAllocator, config);
|
||||
when(dockerClient.inspectContainerCmd("c1")).thenReturn(inspectCmd);
|
||||
}
|
||||
|
||||
private InspectContainerResponse inspectResponse(boolean running) {
|
||||
InspectContainerResponse response = mock(InspectContainerResponse.class);
|
||||
InspectContainerResponse.ContainerState state = mock(InspectContainerResponse.ContainerState.class);
|
||||
when(state.getRunning()).thenReturn(running);
|
||||
when(response.getState()).thenReturn(state);
|
||||
return response;
|
||||
}
|
||||
|
||||
@Test
|
||||
void probeContainerReportsRunning() {
|
||||
InspectContainerResponse response = inspectResponse(true);
|
||||
when(inspectCmd.exec()).thenReturn(response);
|
||||
|
||||
assertEquals(ContainerLiveness.RUNNING, manager.probeContainer("c1"));
|
||||
assertTrue(manager.isContainerRunning("c1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void probeContainerReportsStoppedContainerAsNotRunning() {
|
||||
InspectContainerResponse response = inspectResponse(false);
|
||||
when(inspectCmd.exec()).thenReturn(response);
|
||||
|
||||
assertEquals(ContainerLiveness.NOT_RUNNING, manager.probeContainer("c1"));
|
||||
assertFalse(manager.isContainerRunning("c1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void probeContainerReportsMissingContainerAsNotRunning() {
|
||||
when(inspectCmd.exec()).thenThrow(new NotFoundException("No such container"));
|
||||
|
||||
assertEquals(ContainerLiveness.NOT_RUNNING, manager.probeContainer("c1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void probeContainerReportsIoFailureAsUnknownNotStopped() {
|
||||
when(inspectCmd.exec()).thenThrow(new UncheckedIOException(new IOException("Broken pipe")));
|
||||
|
||||
assertEquals(ContainerLiveness.UNKNOWN, manager.probeContainer("c1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isContainerRunningStillTreatsProbeFailureAsNotRunningForWarmPoolCallers() {
|
||||
when(inspectCmd.exec()).thenThrow(new UncheckedIOException(new IOException("Broken pipe")));
|
||||
|
||||
assertFalse(manager.isContainerRunning("c1"));
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.rds;
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLiveness;
|
||||
import io.github.hectorvent.floci.core.common.docker.CurrentContainerNetworkResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.DockerHostResolver;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
@@ -7187,7 +7188,7 @@ class RdsServiceTest {
|
||||
@Test
|
||||
void refreshRuntimeHealthMarksDeadContainerFailedAndStopsProxy() {
|
||||
when(rdsConfig.mock()).thenReturn(false);
|
||||
when(containerManager.isContainerRunning("cont-id")).thenReturn(false);
|
||||
when(containerManager.probeContainer("cont-id")).thenReturn(ContainerLiveness.NOT_RUNNING);
|
||||
DbInstance instance = rdsService.createDbInstance(
|
||||
"dead-db", "postgres", "16", "admin", "password", "dbname",
|
||||
"db.t3.micro", 20, false, null, null, null, null, false, false,
|
||||
@@ -7207,6 +7208,44 @@ class RdsServiceTest {
|
||||
assertEquals(1, rdsService.describeEvents("dead-db", "db-instance", null, null, 60).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refreshRuntimeHealthKeepsAvailableStatusWhenLivenessProbeFails() {
|
||||
when(rdsConfig.mock()).thenReturn(false);
|
||||
when(containerManager.probeContainer("cont-id")).thenReturn(ContainerLiveness.UNKNOWN);
|
||||
DbInstance instance = rdsService.createDbInstance(
|
||||
"blip-db", "postgres", "16", "admin", "password", "dbname",
|
||||
"db.t3.micro", 20, false, null, null, null, null, false, false,
|
||||
null, Map.of(), List.of(), null, null, true);
|
||||
|
||||
DbInstance refreshed = rdsService.refreshDbInstanceRuntimeHealth(instance);
|
||||
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, refreshed.getStatus());
|
||||
verify(proxyManager, never()).stopProxy("rds-resource:" + refreshed.getDbInstanceArn());
|
||||
assertTrue(rdsService.describeEvents("blip-db", "db-instance", null, null, 60).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void refreshRuntimeHealthReprobesAfterAFailedProbeAndActsOnTheNextDefiniteAnswer() {
|
||||
when(rdsConfig.mock()).thenReturn(false);
|
||||
when(containerManager.probeContainer("cont-id"))
|
||||
.thenReturn(ContainerLiveness.UNKNOWN, ContainerLiveness.NOT_RUNNING);
|
||||
DbInstance instance = rdsService.createDbInstance(
|
||||
"recover-db", "postgres", "16", "admin", "password", "dbname",
|
||||
"db.t3.micro", 20, false, null, null, null, null, false, false,
|
||||
null, Map.of(), List.of(), null, null, true);
|
||||
String proxyKey = "rds-resource:" + instance.getDbInstanceArn();
|
||||
|
||||
DbInstance afterBlip = rdsService.refreshDbInstanceRuntimeHealth(instance);
|
||||
assertEquals(DbInstanceStatus.AVAILABLE, afterBlip.getStatus());
|
||||
verify(proxyManager, never()).stopProxy(proxyKey);
|
||||
|
||||
DbInstance afterDefiniteAnswer = rdsService.refreshDbInstanceRuntimeHealth(afterBlip);
|
||||
assertEquals(DbInstanceStatus.FAILED, afterDefiniteAnswer.getStatus());
|
||||
verify(proxyManager).stopProxy(proxyKey);
|
||||
verify(containerManager, times(2)).probeContainer("cont-id");
|
||||
assertEquals(1, rdsService.describeEvents("recover-db", "db-instance", null, null, 60).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeEventsPrunesEventsOlderThanFourteenDays() throws Exception {
|
||||
InMemoryStorage<String, RdsEvent> eventStore = new InMemoryStorage<>();
|
||||
|
||||
Reference in new issue
Block a user