mateczagany commented on code in PR #821:
URL: 
https://github.com/apache/flink-kubernetes-operator/pull/821#discussion_r1692766135


##########
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:
   I have done some investigation, and I think I will be able to easily 
implement this in the FlinkDeployment and FlinkSessionJob controller observe 
phase, just like the operator currently cleans up savepoints. I could just add 
a new `InformerEventSource` so we would be able to list `FlinkStateSnapshot` 
resources as secondary resources when we reconcile 
FlinkDeployment/FlinkSessionJob resources without querying the Kubernetes API 
server.
   
   Once this PR is merged, I will create a new PR for this functionality.



-- 
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

Reply via email to