mateczagany commented on code in PR #821: URL: https://github.com/apache/flink-kubernetes-operator/pull/821#discussion_r1691933742
########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/snapshot/StateSnapshotReconciler.java: ########## @@ -0,0 +1,230 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.kubernetes.operator.reconciler.snapshot; + +import org.apache.flink.configuration.CheckpointingOptions; +import org.apache.flink.kubernetes.operator.api.FlinkDeployment; +import org.apache.flink.kubernetes.operator.api.FlinkStateSnapshot; +import org.apache.flink.kubernetes.operator.api.spec.FlinkStateSnapshotSpec; +import org.apache.flink.kubernetes.operator.controller.FlinkStateSnapshotContext; +import org.apache.flink.kubernetes.operator.exception.ReconciliationException; +import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils; +import org.apache.flink.kubernetes.operator.service.FlinkResourceContextFactory; +import org.apache.flink.kubernetes.operator.utils.EventRecorder; +import org.apache.flink.kubernetes.operator.utils.FlinkStateSnapshotUtils; +import org.apache.flink.kubernetes.operator.utils.SnapshotUtils; +import org.apache.flink.util.Preconditions; + +import io.javaoperatorsdk.operator.api.reconciler.DeleteControl; +import lombok.RequiredArgsConstructor; +import org.apache.commons.lang3.ObjectUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Optional; + +import static org.apache.flink.kubernetes.operator.api.status.FlinkStateSnapshotStatus.State.TRIGGER_PENDING; + +/** The reconciler for the {@link org.apache.flink.kubernetes.operator.api.FlinkStateSnapshot}. */ +@RequiredArgsConstructor +public class StateSnapshotReconciler { + + private static final Logger LOG = LoggerFactory.getLogger(StateSnapshotReconciler.class); + + private final FlinkResourceContextFactory ctxFactory; + private final EventRecorder eventRecorder; + + public void reconcile(FlinkStateSnapshotContext ctx) { + var resource = ctx.getResource(); + + var savepointState = resource.getStatus().getState(); + if (!TRIGGER_PENDING.equals(savepointState)) { + return; + } + + if (resource.getSpec().isSavepoint() + && resource.getSpec().getSavepoint().getAlreadyExists()) { + LOG.info( + "Snapshot {} is marked as completed in spec, skipping triggering savepoint.", + resource.getMetadata().getName()); + + FlinkStateSnapshotUtils.snapshotSuccessful( + resource, resource.getSpec().getSavepoint().getPath(), true); + return; + } + + if (FlinkStateSnapshotUtils.abandonSnapshotIfJobNotRunning( + ctx.getKubernetesClient(), + ctx.getResource(), + ctx.getSecondaryResource().orElse(null), + eventRecorder)) { + return; + } + + var jobId = ctx.getSecondaryResource().orElseThrow().getStatus().getJobStatus().getJobId(); + + Optional<String> triggerIdOpt; + try { + triggerIdOpt = triggerCheckpointOrSavepoint(resource.getSpec(), ctx, jobId); + } catch (Exception e) { + LOG.error("Failed to trigger snapshot for resource {}", ctx.getResource(), e); + throw new ReconciliationException(e); + } + + if (triggerIdOpt.isEmpty()) { + LOG.warn("Failed to trigger snapshot {}", resource.getMetadata().getName()); + return; + } + + FlinkStateSnapshotUtils.snapshotInProgress(resource, triggerIdOpt.get()); + } + + public DeleteControl cleanup(FlinkStateSnapshotContext ctx) throws Exception { + var resource = ctx.getResource(); + var state = resource.getStatus().getState(); + var resourceName = resource.getMetadata().getName(); + LOG.info("Cleaning up resource {}...", resourceName); + + if (resource.getSpec().isCheckpoint()) { + return DeleteControl.defaultDelete(); + } + if (!resource.getSpec().getSavepoint().getDisposeOnDelete()) { + return DeleteControl.defaultDelete(); + } + if (resource.getSpec().getJobReference() == null + || resource.getSpec().getJobReference().getName() == null) { + return DeleteControl.defaultDelete(); + } + + switch (state) { + case IN_PROGRESS: + LOG.info( + "Cannot delete resource {} yet as savepoint is still in progress...", + resourceName); + return DeleteControl.noFinalizerRemoval() + .rescheduleAfter(ctx.getOperatorConfig().getReconcileInterval().toMillis()); + case COMPLETED: + var flinkDeployment = getFlinkDeployment(ctx); + return handleSnapshotCleanup(resource, flinkDeployment, ctx); Review Comment: Definitely! I would not feel comfortable releasing a new operator version that does not have auto-cleanup of FlinkStateSnapshot CRs. For the now-deprecated way of taking snapshots this could be configured using `kubernetes.operator.savepoint.history.max.age` and `kubernetes.operator.savepoint.history.max.count`. You are right, `disposeOnDelete` is false by default, and it can be controlled by the config `kubernetes.operator.savepoint.dispose-on-delete`. I am not sure of the exact approach I will take to handle cleanup, but it will be implemented under [FLINK-35493](https://issues.apache.org/jira/browse/FLINK-35493) in another PR. That's a feature I felt we could easily separate to another PR, as this one is already growing quite big :D With a low periodic interval, there won't be any more load on the API server and etcd than tracking snapshots in the Flink resource CR (as the operator does currently). Periodically cleaning them up might affect the load on API server/etcd depending on what solution we will go with. But that will be done under FLINK-35493. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org