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


##########
flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkStateSnapshotController.java:
##########
@@ -0,0 +1,202 @@
+/*
+ * 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.controller;
+
+import org.apache.flink.kubernetes.operator.api.FlinkStateSnapshot;
+import org.apache.flink.kubernetes.operator.api.status.FlinkStateSnapshotState;
+import 
org.apache.flink.kubernetes.operator.api.status.FlinkStateSnapshotStatus;
+import 
org.apache.flink.kubernetes.operator.observer.snapshot.StateSnapshotObserver;
+import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils;
+import 
org.apache.flink.kubernetes.operator.reconciler.snapshot.StateSnapshotReconciler;
+import 
org.apache.flink.kubernetes.operator.service.FlinkResourceContextFactory;
+import org.apache.flink.kubernetes.operator.utils.EventRecorder;
+import org.apache.flink.kubernetes.operator.utils.EventSourceUtils;
+import org.apache.flink.kubernetes.operator.utils.FlinkStateSnapshotUtils;
+import org.apache.flink.kubernetes.operator.utils.StatusRecorder;
+import org.apache.flink.kubernetes.operator.validation.FlinkResourceValidator;
+
+import io.javaoperatorsdk.operator.api.reconciler.Cleaner;
+import io.javaoperatorsdk.operator.api.reconciler.Context;
+import io.javaoperatorsdk.operator.api.reconciler.ControllerConfiguration;
+import io.javaoperatorsdk.operator.api.reconciler.DeleteControl;
+import io.javaoperatorsdk.operator.api.reconciler.ErrorStatusHandler;
+import io.javaoperatorsdk.operator.api.reconciler.ErrorStatusUpdateControl;
+import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
+import io.javaoperatorsdk.operator.api.reconciler.EventSourceInitializer;
+import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
+import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
+import io.javaoperatorsdk.operator.processing.event.source.EventSource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.time.Duration;
+import java.util.Map;
+import java.util.Set;
+
+/** Controller that runs the main reconcile loop for {@link 
FlinkStateSnapshot}. */
+@ControllerConfiguration
+public class FlinkStateSnapshotController
+        implements Reconciler<FlinkStateSnapshot>,
+                ErrorStatusHandler<FlinkStateSnapshot>,
+                EventSourceInitializer<FlinkStateSnapshot>,
+                Cleaner<FlinkStateSnapshot> {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(FlinkStateSnapshotController.class);
+
+    private final Set<FlinkResourceValidator> validators;
+    private final FlinkResourceContextFactory ctxFactory;
+    private final StateSnapshotReconciler reconciler;
+    private final StateSnapshotObserver observer;
+    private final EventRecorder eventRecorder;
+    private final StatusRecorder<FlinkStateSnapshot, FlinkStateSnapshotStatus> 
statusRecorder;
+
+    public FlinkStateSnapshotController(
+            Set<FlinkResourceValidator> validators,
+            FlinkResourceContextFactory ctxFactory,
+            StateSnapshotReconciler reconciler,
+            StateSnapshotObserver observer,
+            EventRecorder eventRecorder,
+            StatusRecorder<FlinkStateSnapshot, FlinkStateSnapshotStatus> 
statusRecorder) {
+        this.validators = validators;
+        this.ctxFactory = ctxFactory;
+        this.reconciler = reconciler;
+        this.observer = observer;
+        this.eventRecorder = eventRecorder;
+        this.statusRecorder = statusRecorder;
+    }
+
+    @Override
+    public UpdateControl<FlinkStateSnapshot> reconcile(
+            FlinkStateSnapshot flinkStateSnapshot, Context<FlinkStateSnapshot> 
josdkContext) {
+        statusRecorder.updateStatusFromCache(flinkStateSnapshot);
+
+        var ctx = ctxFactory.getFlinkStateSnapshotContext(flinkStateSnapshot, 
josdkContext);
+
+        // observe
+        observer.observe(ctx);
+
+        // validate
+        if (!validateSnapshot(ctx)) {
+            statusRecorder.patchAndCacheStatus(flinkStateSnapshot, 
ctx.getKubernetesClient());
+            UpdateControl<FlinkStateSnapshot> updateControl = 
UpdateControl.noUpdate();
+            return updateControl.rescheduleAfter(
+                    ctx.getOperatorConfig().getReconcileInterval().toMillis());
+        }
+
+        // reconcile
+        try {
+            statusRecorder.patchAndCacheStatus(flinkStateSnapshot, 
ctx.getKubernetesClient());
+            reconciler.reconcile(ctx);
+        } catch (Exception e) {
+            FlinkStateSnapshotUtils.snapshotFailed(
+                    josdkContext.getClient(), eventRecorder, 
flinkStateSnapshot, e.getMessage());
+            LOG.error("Failed to reconcile {}", flinkStateSnapshot, e);

Review Comment:
   This codepath threw `ReconciliationException` before commit 
https://github.com/apache/flink-kubernetes-operator/pull/821/commits/71a4137fe362c6ee20735787d40c9530268d0b49
 but instead changing the state to `FAILED` here, then letting 
`getUpdateControl()` decide if we have reached the backoff limit and possibly 
setting the state back to `TRIGGER_PENDING` seems like a better solution.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to