mateczagany commented on code in PR #821: URL: https://github.com/apache/flink-kubernetes-operator/pull/821#discussion_r1667680584
########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/snapshot/StateSnapshotReconciler.java: ########## @@ -0,0 +1,235 @@ +/* + * 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.autoscaler.utils.DateTimeUtils; +import org.apache.flink.configuration.CheckpointingOptions; +import org.apache.flink.kubernetes.operator.api.CrdConstants; +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.api.status.FlinkStateSnapshotState; +import org.apache.flink.kubernetes.operator.api.status.SnapshotTriggerType; +import org.apache.flink.kubernetes.operator.controller.FlinkResourceContext; +import org.apache.flink.kubernetes.operator.controller.FlinkStateSnapshotContext; +import org.apache.flink.kubernetes.operator.reconciler.ReconciliationUtils; +import org.apache.flink.kubernetes.operator.service.FlinkResourceContextFactory; +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.time.Instant; +import java.util.Optional; + +/** 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; + + public void reconcile(FlinkStateSnapshotContext ctx) throws Exception { + var source = ctx.getResource().getSpec().getJobReference(); + var resource = ctx.getResource(); + + var savepointState = resource.getStatus().getState(); + if (!FlinkStateSnapshotState.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()); + resource.getStatus().setState(FlinkStateSnapshotState.COMPLETED); + resource.getStatus().setPath(resource.getSpec().getSavepoint().getPath()); + var time = DateTimeUtils.kubernetes(Instant.now()); + resource.getStatus().setTriggerTimestamp(time); + resource.getStatus().setResultTimestamp(time); + return; + } + + var secondaryResource = + ctx.getSecondaryResource() + .orElseThrow( + () -> + new RuntimeException( + String.format( + "Secondary resource %s not found", + source))); + if (!ReconciliationUtils.isJobRunning(secondaryResource.getStatus())) { + LOG.warn( + "Target job {} for savepoint {} is not running, cannot trigger snapshot.", + secondaryResource.getMetadata().getName(), + resource.getMetadata().getName()); + return; + } Review Comment: I have added a commit which will make sure that we abandon snapshots if referenced Flink resource is not found, or the job is not running, in both observe and reconcile phase. An event will also be generated for the snapshot resource in these cases. -- 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