Search in sources :

Example 1 with DbOpsRestartStatus

use of io.stackgres.common.crd.sgdbops.DbOpsRestartStatus in project stackgres by ongres.

the class ClusterStateHandlerTest method givenAnUninitializedJobState_itShouldInitializeIt.

@Test
void givenAnUninitializedJobState_itShouldInitializeIt() {
    podTestUtil.preparePods(cluster, 0, 1, 2);
    var pods = podTestUtil.getClusterPods(cluster);
    final String dbOpsName = dbOps.getMetadata().getName();
    getRestartStateHandler().restartCluster(dbOps).await().atMost(Duration.ofMillis(50));
    var storedDbOps = kubeDb.getDbOps(dbOpsName, namespace);
    List<String> expectedInitialInstances = pods.stream().map(Pod::getMetadata).map(ObjectMeta::getName).sorted(String::compareTo).collect(Collectors.toUnmodifiableList());
    final DbOpsRestartStatus initializedSecurityUpgradeStatus = getRestartStatus(storedDbOps);
    Pod primaryPod = pods.stream().filter(pod -> pod.getMetadata().getName().endsWith("-0")).findAny().get();
    assertEquals(primaryPod.getMetadata().getName(), initializedSecurityUpgradeStatus.getPrimaryInstance());
    List<String> actualInitialInstances = initializedSecurityUpgradeStatus.getInitialInstances();
    assertEquals(expectedInitialInstances, actualInitialInstances);
    List<String> actualPendingRestartedInstances = initializedSecurityUpgradeStatus.getPendingToRestartInstances();
    assertEquals(expectedInitialInstances, actualPendingRestartedInstances);
    assertTrue(() -> initializedSecurityUpgradeStatus.getRestartedInstances() == null || initializedSecurityUpgradeStatus.getRestartedInstances().isEmpty());
    assertNull(initializedSecurityUpgradeStatus.getFailure());
    assertNull(initializedSecurityUpgradeStatus.getSwitchoverInitiated());
    assertEquals(dbOps, storedDbOps, "It should store the DBOps status changes");
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Pod(io.fabric8.kubernetes.api.model.Pod) DbOpsRestartStatus(io.stackgres.common.crd.sgdbops.DbOpsRestartStatus) ClusterDbOpsRestartStatus(io.stackgres.common.crd.sgcluster.ClusterDbOpsRestartStatus) Test(org.junit.jupiter.api.Test)

Example 2 with DbOpsRestartStatus

use of io.stackgres.common.crd.sgdbops.DbOpsRestartStatus in project stackgres by ongres.

the class AbstractRestartStateHandler method buildClusterRestartState.

protected ClusterRestartState buildClusterRestartState(StackGresDbOps dbOps, StackGresCluster cluster, Optional<StatefulSet> statefulSet, List<Pod> clusterPods) {
    DbOpsRestartStatus restartStatus = getDbOpRestartStatus(dbOps);
    Map<String, Pod> podsDict = clusterPods.stream().collect(Collectors.toMap(pod -> pod.getMetadata().getName(), Function.identity()));
    var initialInstances = Optional.ofNullable(restartStatus.getInitialInstances()).map(instances -> instances.stream().map(podsDict::get).collect(Collectors.toUnmodifiableList())).orElse(clusterPods);
    var restartedInstances = Optional.ofNullable(restartStatus.getRestartedInstances()).map(instances -> instances.stream().map(podsDict::get).collect(Collectors.toUnmodifiableList())).orElse(List.of());
    var podRestartReasonsMap = clusterPods.stream().collect(Collectors.toUnmodifiableMap(Function.identity(), pod -> getPodRestartReasons(cluster, statefulSet, pod)));
    final String method = getRestartMethod(dbOps).orElse(REDUCED_IMPACT_METHOD);
    final boolean onlyPendingRestart = Optional.of(dbOps.getSpec()).map(StackGresDbOpsSpec::getRestart).map(StackGresDbOpsRestart::getOnlyPendingRestart).orElse(false);
    return ImmutableClusterRestartState.builder().namespace(dbOps.getMetadata().getNamespace()).dbOpsName(dbOps.getMetadata().getName()).dbOpsOperation(dbOps.getSpec().getOp()).clusterName(cluster.getMetadata().getName()).restartMethod(method).isOnlyPendingRestart(onlyPendingRestart).primaryInstance(getPrimaryInstance(clusterPods)).isSwitchoverInitiated(restartStatus.getSwitchoverInitiated() != null).isSwitchoverFinalized(restartStatus.getSwitchoverFinalized() != null).initialInstances(initialInstances).restartedInstances(restartedInstances).totalInstances(clusterPods).podRestartReasonsMap(podRestartReasonsMap).build();
}
Also used : StackGresContext(io.stackgres.common.StackGresContext) StackGresDbOpsRestart(io.stackgres.common.crd.sgdbops.StackGresDbOpsRestart) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) DbOpsRestartStatus(io.stackgres.common.crd.sgdbops.DbOpsRestartStatus) InvalidCluster(io.stackgres.jobs.dbops.clusterrestart.InvalidCluster) Function(java.util.function.Function) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) RestartEvent(io.stackgres.jobs.dbops.clusterrestart.RestartEvent) ArrayList(java.util.ArrayList) Uni(io.smallrye.mutiny.Uni) Inject(javax.inject.Inject) ImmutableClusterRestartState(io.stackgres.jobs.dbops.clusterrestart.ImmutableClusterRestartState) ImmutableList(com.google.common.collect.ImmutableList) Duration(java.time.Duration) Map(java.util.Map) ClusterDbOpsRestartStatus(io.stackgres.common.crd.sgcluster.ClusterDbOpsRestartStatus) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) StackGresDbOpsSpec(io.stackgres.common.crd.sgdbops.StackGresDbOpsSpec) StackGresClusterStatus(io.stackgres.common.crd.sgcluster.StackGresClusterStatus) REDUCED_IMPACT_METHOD(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartImpl.REDUCED_IMPACT_METHOD) RestartReasons(io.stackgres.common.ClusterPendingRestartUtil.RestartReasons) Logger(org.slf4j.Logger) EventEmitter(io.stackgres.common.event.EventEmitter) ClusterRestart(io.stackgres.jobs.dbops.clusterrestart.ClusterRestart) ClusterRestartStateHandlerImpl(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartStateHandlerImpl) LabelFactoryForCluster(io.stackgres.common.LabelFactoryForCluster) Pod(io.fabric8.kubernetes.api.model.Pod) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) ClusterRestartState(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartState) List(java.util.List) CustomResourceScheduler(io.stackgres.common.resource.CustomResourceScheduler) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ResourceScanner(io.stackgres.common.resource.ResourceScanner) Optional(java.util.Optional) ResourceFinder(io.stackgres.common.resource.ResourceFinder) NotNull(org.jetbrains.annotations.NotNull) ClusterPendingRestartUtil(io.stackgres.common.ClusterPendingRestartUtil) Pod(io.fabric8.kubernetes.api.model.Pod) StackGresDbOpsSpec(io.stackgres.common.crd.sgdbops.StackGresDbOpsSpec) DbOpsRestartStatus(io.stackgres.common.crd.sgdbops.DbOpsRestartStatus) ClusterDbOpsRestartStatus(io.stackgres.common.crd.sgcluster.ClusterDbOpsRestartStatus)

Aggregations

ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)2 Pod (io.fabric8.kubernetes.api.model.Pod)2 ClusterDbOpsRestartStatus (io.stackgres.common.crd.sgcluster.ClusterDbOpsRestartStatus)2 DbOpsRestartStatus (io.stackgres.common.crd.sgdbops.DbOpsRestartStatus)2 ImmutableList (com.google.common.collect.ImmutableList)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)1 Uni (io.smallrye.mutiny.Uni)1 ClusterPendingRestartUtil (io.stackgres.common.ClusterPendingRestartUtil)1 RestartReasons (io.stackgres.common.ClusterPendingRestartUtil.RestartReasons)1 LabelFactoryForCluster (io.stackgres.common.LabelFactoryForCluster)1 StackGresContext (io.stackgres.common.StackGresContext)1 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)1 StackGresClusterStatus (io.stackgres.common.crd.sgcluster.StackGresClusterStatus)1 StackGresDbOps (io.stackgres.common.crd.sgdbops.StackGresDbOps)1 StackGresDbOpsRestart (io.stackgres.common.crd.sgdbops.StackGresDbOpsRestart)1 StackGresDbOpsSpec (io.stackgres.common.crd.sgdbops.StackGresDbOpsSpec)1 EventEmitter (io.stackgres.common.event.EventEmitter)1 CustomResourceFinder (io.stackgres.common.resource.CustomResourceFinder)1 CustomResourceScheduler (io.stackgres.common.resource.CustomResourceScheduler)1