gyfora commented on code in PR #821: URL: https://github.com/apache/flink-kubernetes-operator/pull/821#discussion_r1666495408
########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/deployment/AbstractFlinkResourceReconciler.java: ########## @@ -122,12 +127,20 @@ public void reconcile(FlinkResourceContext<CR> ctx) throws Exception { var deployConfig = ctx.getDeployConfig(spec); updateStatusBeforeFirstDeployment( cr, spec, deployConfig, status, ctx.getKubernetesClient()); - deploy( - ctx, - spec, - deployConfig, - Optional.ofNullable(spec.getJob()).map(JobSpec::getInitialSavepointPath), - false); + + Optional<String> savepointPathOpt = + Optional.ofNullable(spec.getJob()).map(JobSpec::getInitialSavepointPath); + if (spec.getJob() != null && spec.getJob().getFlinkStateSnapshotReference() != null) { + var snapshotRef = spec.getJob().getFlinkStateSnapshotReference(); + if (snapshotRef.getName() != null) { + savepointPathOpt = + Optional.of( + FlinkStateSnapshotUtils.getAndValidateFlinkStateSnapshotPath( + ctx.getKubernetesClient(), snapshotRef)); + } + } Review Comment: At this point we should capture this in a method ########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/crd/CustomResourceDefinitionWatcherImpl.java: ########## @@ -0,0 +1,52 @@ +/* + * 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.crd; + +import io.fabric8.kubernetes.api.model.HasMetadata; +import io.fabric8.kubernetes.client.KubernetesClient; +import lombok.RequiredArgsConstructor; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashSet; +import java.util.Set; + +/** Default implementation for CustomResourceDefinitionWatcher. */ +@RequiredArgsConstructor +public class CustomResourceDefinitionWatcherImpl implements CustomResourceDefinitionWatcher { Review Comment: The ` CustomResourceDefinitionWatcher` interface and the way we use it feels a bit overly complicated for the simple purpose we want. My recommendation would be to remove the generic interface and replace it with some specific utility that checks the Snapshot CRD and based on this we simply set a boolean base config in the config manager. This way we can get if it's enabled from the operator config instead of having to pass and use the watcher from the controllers. ########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkStateSnapshotController.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.exception.ReconciliationException; +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.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 (!validateSavepoint(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()); Review Comment: I think for FlinkStateSnapshot resources we should avoid using the custom status patching. We can just rely on the JOSDK built in Update control mechanism. We are using a custom patching logic for the deployments because we are executing destructive actions before which we must record the status in some cases. Here I believe it's not the case in general. ########## flink-kubernetes-operator-api/src/main/java/org/apache/flink/kubernetes/operator/api/status/FlinkStateSnapshotStatus.java: ########## @@ -0,0 +1,66 @@ +/* + * 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.api.status; + +import org.apache.flink.annotation.Experimental; +import org.apache.flink.kubernetes.operator.api.diff.Diffable; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import io.fabric8.crd.generator.annotation.PrinterColumn; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.ToString; +import lombok.experimental.SuperBuilder; + +/** Last observed status of the Flink state snapshot. */ +@Experimental +@Data +@AllArgsConstructor +@NoArgsConstructor +@EqualsAndHashCode +@ToString +@SuperBuilder +@JsonIgnoreProperties(ignoreUnknown = true) +public class FlinkStateSnapshotStatus implements Diffable<FlinkStateSnapshotStatus> { + + /** Current state of the snapshot. */ + @PrinterColumn(name = "Snapshot Status") Review Comment: Can we simply call it `State` ? ########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/controller/FlinkStateSnapshotController.java: ########## @@ -0,0 +1,207 @@ +/* + * 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.exception.ReconciliationException; +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.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 (!validateSavepoint(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) { + eventRecorder.triggerSnapshotEvent( + flinkStateSnapshot, + EventRecorder.Type.Warning, + EventRecorder.Reason.SnapshotError, + EventRecorder.Component.Snapshot, + e.getMessage(), + josdkContext.getClient()); + throw new ReconciliationException(e); + } + + var updateControl = getUpdateControl(ctx); + statusRecorder.patchAndCacheStatus(flinkStateSnapshot, ctx.getKubernetesClient()); + return updateControl; + } + + @Override + public DeleteControl cleanup( + FlinkStateSnapshot flinkStateSnapshot, Context<FlinkStateSnapshot> josdkContext) { + var ctx = ctxFactory.getFlinkStateSnapshotContext(flinkStateSnapshot, josdkContext); + DeleteControl deleteControl; + try { + deleteControl = reconciler.cleanup(ctx); + } catch (Exception e) { + eventRecorder.triggerSnapshotEvent( + flinkStateSnapshot, + EventRecorder.Type.Warning, + EventRecorder.Reason.CleanupFailed, + EventRecorder.Component.Snapshot, + e.getMessage(), + ctx.getKubernetesClient()); + LOG.error( + "Error during cleanup of snapshot {}", + flinkStateSnapshot.getMetadata().getName(), + e); + return DeleteControl.noFinalizerRemoval() + .rescheduleAfter(ctx.getOperatorConfig().getReconcileInterval().toMillis()); + } + + if (deleteControl.isRemoveFinalizer()) { + statusRecorder.removeCachedStatus(flinkStateSnapshot); + } + return deleteControl; + } + + private UpdateControl<FlinkStateSnapshot> getUpdateControl(FlinkStateSnapshotContext ctx) { + var resource = ctx.getResource(); + if (FlinkStateSnapshotState.FAILED.equals(resource.getStatus().getState())) { + if (resource.getStatus().getFailures() > resource.getSpec().getBackoffLimit()) { + LOG.info( + "Snapshot {} failed and won't be retried as failure count exceeded the backoff limit", + resource.getMetadata().getName()); + return UpdateControl.noUpdate(); + } else { + long retrySeconds = 10L * (1L << resource.getStatus().getFailures() - 1); + LOG.info( + "Snapshot {} failed and will be retried in {} seconds...", + resource.getMetadata().getName(), + retrySeconds); + resource.getStatus().setState(FlinkStateSnapshotState.TRIGGER_PENDING); + return UpdateControl.<FlinkStateSnapshot>noUpdate() + .rescheduleAfter(Duration.ofSeconds(retrySeconds)); + } + } + return UpdateControl.<FlinkStateSnapshot>noUpdate() + .rescheduleAfter(ctx.getOperatorConfig().getReconcileInterval().toMillis()); + } + + @Override + public ErrorStatusUpdateControl<FlinkStateSnapshot> updateErrorStatus( + FlinkStateSnapshot flinkStateSnapshot, + Context<FlinkStateSnapshot> context, + Exception e) { + var ctx = ctxFactory.getFlinkStateSnapshotContext(flinkStateSnapshot, context); + return ReconciliationUtils.toErrorStatusUpdateControl(ctx, e, statusRecorder); + } + + @Override + public Map<String, EventSource> prepareEventSources( + EventSourceContext<FlinkStateSnapshot> context) { + return EventSourceInitializer.nameEventSources( + EventSourceUtils.getFlinkStateSnapshotInformerEventSources(context)); + } + + private boolean validateSavepoint(FlinkStateSnapshotContext ctx) { + var savepoint = ctx.getResource(); + for (var validator : validators) { + var validationError = + validator.validateStateSnapshot(savepoint, ctx.getSecondaryResource()); + if (validationError.isPresent()) { + eventRecorder.triggerSnapshotEvent( + savepoint, + EventRecorder.Type.Warning, + EventRecorder.Reason.ValidationError, + EventRecorder.Component.Operator, + validationError.get(), + ctx.getKubernetesClient()); + return true; Review Comment: we always seem to return "true" is this correct for the validation error case? -- 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