Search in sources :

Example 6 with StackGresDbOpsStatus

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

the class DbOpLauncherImpl method launchDbOp.

@Override
public void launchDbOp(String dbOpName, String namespace) {
    StackGresDbOps dbOps = dbOpsFinder.findByNameAndNamespace(dbOpName, namespace).orElseThrow(() -> new IllegalArgumentException(StackGresDbOps.KIND + " " + dbOpName + " does not exists in namespace " + namespace));
    Instance<DatabaseOperationJob> jobImpl = instance.select(new DatabaseOperationLiteral(dbOps.getSpec().getOp()));
    if (jobImpl.isResolvable()) {
        LOGGER.info("Initializing conditions for SgDbOps {}", dbOps.getMetadata().getName());
        var status = Optional.ofNullable(dbOps.getStatus()).or(() -> Optional.of(new StackGresDbOpsStatus())).map(dbOpsStatus -> {
            dbOpsStatus.setOpStarted(Instant.now().toString());
            dbOpsStatus.setOpRetries(Optional.ofNullable(dbOpsStatus.getOpRetries()).map(opRetries -> opRetries + 1).orElse(0));
            dbOpsStatus.setConditions(getStartingConditions());
            return dbOpsStatus;
        }).orElseThrow();
        dbOps.setStatus(status);
        final StackGresDbOps initializedDbOps = dbOpsScheduler.update(dbOps);
        try {
            final int lockPollInterval = Integer.parseInt(DBOPS_LOCK_POLL_INTERVAL.getString());
            final int timeout = Integer.parseInt(DBOPS_LOCK_TIMEOUT.getString());
            LockRequest lockRequest = ImmutableLockRequest.builder().namespace(initializedDbOps.getMetadata().getNamespace()).serviceAccount(JobsProperty.SERVICE_ACCOUNT.getString()).podName(JobsProperty.POD_NAME.getString()).pollInterval(lockPollInterval).timeout(timeout).lockResourceName(initializedDbOps.getSpec().getSgCluster()).build();
            Infrastructure.setDroppedExceptionHandler(err -> LOGGER.error("Dropped exception ", err));
            lockAcquirer.lockRun(lockRequest, (targetCluster) -> {
                databaseOperationEventEmitter.operationStarted(dbOpName, namespace);
                final DatabaseOperationJob databaseOperationJob = jobImpl.get();
                Uni<ClusterRestartState> jobUni = databaseOperationJob.runJob(initializedDbOps, targetCluster);
                if (initializedDbOps.getSpec().getTimeout() != null) {
                    jobUni.await().atMost(Duration.parse(initializedDbOps.getSpec().getTimeout()));
                } else {
                    jobUni.await().indefinitely();
                }
                databaseOperationEventEmitter.operationCompleted(dbOpName, namespace);
            });
            LOGGER.info("Operation completed for SgDbOp {}", dbOpName);
            updateToCompletedConditions(dbOpName, namespace);
        } catch (TimeoutException timeoutEx) {
            updateToTimeoutConditions(dbOpName, namespace);
            databaseOperationEventEmitter.operationTimedOut(dbOpName, namespace);
            throw timeoutEx;
        } catch (Exception e) {
            updateToFailedConditions(dbOpName, namespace);
            databaseOperationEventEmitter.operationFailed(dbOpName, namespace);
            throw e;
        }
    } else if (jobImpl.isAmbiguous()) {
        throw new IllegalStateException("Multiple implementations of the operation " + dbOps.getSpec().getOp() + " found");
    } else {
        throw new IllegalStateException("Implementation of operation " + dbOps.getSpec().getOp() + " not found");
    }
}
Also used : LockRequest(io.stackgres.jobs.dbops.lock.LockRequest) StackGresCluster(io.stackgres.common.crd.sgcluster.StackGresCluster) LoggerFactory(org.slf4j.LoggerFactory) StackGresDbOpsCondition(io.stackgres.common.crd.sgdbops.StackGresDbOpsCondition) JobsProperty(io.stackgres.jobs.app.JobsProperty) LockAcquirer(io.stackgres.jobs.dbops.lock.LockAcquirer) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) Uni(io.smallrye.mutiny.Uni) Inject(javax.inject.Inject) Duration(java.time.Duration) StackGresDbOpsStatus(io.stackgres.common.crd.sgdbops.StackGresDbOpsStatus) CustomResourceFinder(io.stackgres.common.resource.CustomResourceFinder) Any(javax.enterprise.inject.Any) Instance(javax.enterprise.inject.Instance) Infrastructure(io.smallrye.mutiny.infrastructure.Infrastructure) Logger(org.slf4j.Logger) ImmutableLockRequest(io.stackgres.jobs.dbops.lock.ImmutableLockRequest) TimeoutException(io.smallrye.mutiny.TimeoutException) Instant(java.time.Instant) ClusterRestartState(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartState) DBOPS_LOCK_TIMEOUT(io.stackgres.jobs.app.JobsProperty.DBOPS_LOCK_TIMEOUT) DbOpsStatusCondition(io.stackgres.common.crd.sgdbops.DbOpsStatusCondition) List(java.util.List) DBOPS_LOCK_POLL_INTERVAL(io.stackgres.jobs.app.JobsProperty.DBOPS_LOCK_POLL_INTERVAL) CustomResourceScheduler(io.stackgres.common.resource.CustomResourceScheduler) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) ApplicationScoped(javax.enterprise.context.ApplicationScoped) StackGresDbOps(io.stackgres.common.crd.sgdbops.StackGresDbOps) TimeoutException(io.smallrye.mutiny.TimeoutException) StackGresDbOpsStatus(io.stackgres.common.crd.sgdbops.StackGresDbOpsStatus) ClusterRestartState(io.stackgres.jobs.dbops.clusterrestart.ClusterRestartState) LockRequest(io.stackgres.jobs.dbops.lock.LockRequest) ImmutableLockRequest(io.stackgres.jobs.dbops.lock.ImmutableLockRequest) TimeoutException(io.smallrye.mutiny.TimeoutException)

Example 7 with StackGresDbOpsStatus

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

the class RestartJob method reportFailure.

private void reportFailure(StackGresDbOps dbOps, Throwable ex) {
    String message = ex.getMessage();
    String dbOpsName = dbOps.getMetadata().getName();
    String namespace = dbOps.getMetadata().getNamespace();
    dbOpsFinder.findByNameAndNamespace(dbOpsName, namespace).ifPresent(savedDbOps -> {
        if (savedDbOps.getStatus() == null) {
            savedDbOps.setStatus(new StackGresDbOpsStatus());
        }
        if (savedDbOps.getStatus().getRestart() == null) {
            savedDbOps.getStatus().setRestart(new StackGresDbOpsRestartStatus());
        }
        savedDbOps.getStatus().getRestart().setFailure(message);
        dbOpsScheduler.update(savedDbOps);
    });
}
Also used : StackGresDbOpsStatus(io.stackgres.common.crd.sgdbops.StackGresDbOpsStatus) StackGresDbOpsRestartStatus(io.stackgres.common.crd.sgdbops.StackGresDbOpsRestartStatus)

Aggregations

StackGresDbOpsStatus (io.stackgres.common.crd.sgdbops.StackGresDbOpsStatus)7 StackGresDbOps (io.stackgres.common.crd.sgdbops.StackGresDbOps)4 StackGresCluster (io.stackgres.common.crd.sgcluster.StackGresCluster)2 DbOpsStatusCondition (io.stackgres.common.crd.sgdbops.DbOpsStatusCondition)2 Instant (java.time.Instant)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 TimeoutException (io.smallrye.mutiny.TimeoutException)1 Uni (io.smallrye.mutiny.Uni)1 Infrastructure (io.smallrye.mutiny.infrastructure.Infrastructure)1 DbOpsStatus (io.stackgres.apiweb.dto.dbops.DbOpsStatus)1 StackGresDbOpsCondition (io.stackgres.common.crd.sgdbops.StackGresDbOpsCondition)1 StackGresDbOpsRestartStatus (io.stackgres.common.crd.sgdbops.StackGresDbOpsRestartStatus)1 StackGresDbOpsSecurityUpgradeStatus (io.stackgres.common.crd.sgdbops.StackGresDbOpsSecurityUpgradeStatus)1 CustomResourceFinder (io.stackgres.common.resource.CustomResourceFinder)1 CustomResourceScheduler (io.stackgres.common.resource.CustomResourceScheduler)1 JobsProperty (io.stackgres.jobs.app.JobsProperty)1 DBOPS_LOCK_POLL_INTERVAL (io.stackgres.jobs.app.JobsProperty.DBOPS_LOCK_POLL_INTERVAL)1 DBOPS_LOCK_TIMEOUT (io.stackgres.jobs.app.JobsProperty.DBOPS_LOCK_TIMEOUT)1 ClusterRestartState (io.stackgres.jobs.dbops.clusterrestart.ClusterRestartState)1