gyfora commented on code in PR #868: URL: https://github.com/apache/flink-kubernetes-operator/pull/868#discussion_r1726604195
########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/SnapshotTriggerTimestampStore.java: ########## @@ -0,0 +1,133 @@ +/* + * 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; + +import org.apache.flink.autoscaler.utils.DateTimeUtils; +import org.apache.flink.kubernetes.operator.api.AbstractFlinkResource; +import org.apache.flink.kubernetes.operator.api.CrdConstants; +import org.apache.flink.kubernetes.operator.api.FlinkStateSnapshot; +import org.apache.flink.kubernetes.operator.api.status.SnapshotInfo; + +import io.fabric8.kubernetes.api.model.HasMetadata; +import lombok.RequiredArgsConstructor; + +import java.time.Instant; +import java.util.Comparator; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; + +import static org.apache.flink.kubernetes.operator.api.status.FlinkStateSnapshotStatus.State.COMPLETED; +import static org.apache.flink.kubernetes.operator.api.status.SnapshotTriggerType.PERIODIC; +import static org.apache.flink.kubernetes.operator.reconciler.SnapshotType.SAVEPOINT; + +/** Class used to store latest timestamps of periodic checkpoint/savepoint. */ +@RequiredArgsConstructor +public class SnapshotTriggerTimestampStore { + private final ConcurrentHashMap<String, Instant> checkpointsLastTriggeredCache = + new ConcurrentHashMap<>(); + private final ConcurrentHashMap<String, Instant> savepointsLastTriggeredCache = + new ConcurrentHashMap<>(); + + /** + * Returns the time a periodic snapshot was last triggered for this resource. This is stored in + * memory, on operator start it will use the latest completed FlinkStateSnapshot CR creation + * timestamp. If none found, the return value will be the max of the resource creation timestamp + * and the latest triggered legacy snapshot. + * + * @param resource Flink resource + * @param snapshotsSupplier supplies related snapshot resources + * @return instant of last trigger + */ + public Instant getLastPeriodicTriggerInstant( + AbstractFlinkResource<?, ?> resource, + SnapshotType snapshotType, + Supplier<Set<FlinkStateSnapshot>> snapshotsSupplier) { + var cache = getCacheForSnapshotType(snapshotType); + if (cache.containsKey(resource.getMetadata().getUid())) { + return cache.get(resource.getMetadata().getUid()); + } + + var snapshots = Optional.ofNullable(snapshotsSupplier.get()).orElse(Set.of()); + return snapshots.stream() + .filter(s -> s.getStatus() != null && COMPLETED.equals(s.getStatus().getState())) + .filter(s -> (snapshotType == SAVEPOINT) == s.getSpec().isSavepoint()) + .filter( + s -> + PERIODIC.name() + .equals( + s.getMetadata() + .getLabels() + .get(CrdConstants.LABEL_SNAPSHOT_TYPE))) + .map(s -> DateTimeUtils.parseKubernetes(s.getMetadata().getCreationTimestamp())) + .max(Comparator.naturalOrder()) + .orElseGet( + () -> { + var legacyTs = getLegacyTimestamp(resource, snapshotType); + var creationTs = + Instant.parse(resource.getMetadata().getCreationTimestamp()); + + if (legacyTs.compareTo(creationTs) > 0) { + return legacyTs; + } else { + return creationTs; + } + }); Review Comment: Should we put into the cache before returning? ########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/SnapshotTriggerTimestampStore.java: ########## @@ -0,0 +1,133 @@ +/* + * 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; + +import org.apache.flink.autoscaler.utils.DateTimeUtils; +import org.apache.flink.kubernetes.operator.api.AbstractFlinkResource; +import org.apache.flink.kubernetes.operator.api.CrdConstants; +import org.apache.flink.kubernetes.operator.api.FlinkStateSnapshot; +import org.apache.flink.kubernetes.operator.api.status.SnapshotInfo; + +import io.fabric8.kubernetes.api.model.HasMetadata; +import lombok.RequiredArgsConstructor; + +import java.time.Instant; +import java.util.Comparator; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Supplier; + +import static org.apache.flink.kubernetes.operator.api.status.FlinkStateSnapshotStatus.State.COMPLETED; +import static org.apache.flink.kubernetes.operator.api.status.SnapshotTriggerType.PERIODIC; +import static org.apache.flink.kubernetes.operator.reconciler.SnapshotType.SAVEPOINT; + +/** Class used to store latest timestamps of periodic checkpoint/savepoint. */ +@RequiredArgsConstructor +public class SnapshotTriggerTimestampStore { + private final ConcurrentHashMap<String, Instant> checkpointsLastTriggeredCache = + new ConcurrentHashMap<>(); + private final ConcurrentHashMap<String, Instant> savepointsLastTriggeredCache = + new ConcurrentHashMap<>(); Review Comment: Do we want to ever clean this up? It's not gonna be a large size but I just wanted to point out that we "could" clean it up when a resource is deleted ########## flink-kubernetes-operator/src/main/java/org/apache/flink/kubernetes/operator/reconciler/deployment/AbstractJobReconciler.java: ########## @@ -360,6 +367,25 @@ public boolean reconcileOtherChanges(FlinkResourceContext<CR> ctx) throws Except } } + /** + * Returns a supplier of all relevant FlinkStateSnapshot resources for a given Flink resource. + * If FlinkStateSnapshot resources are disabled, the supplier returns an empty set. + * + * @param ctx resource context + * @return supplier for FlinkStateSnapshot resources + */ + private Supplier<Set<FlinkStateSnapshot>> getFlinkStateSnapshotsSupplier( + FlinkResourceContext<CR> ctx) { + return () -> { + if (FlinkStateSnapshotUtils.isSnapshotResourceEnabled( + ctx.getOperatorConfig(), ctx.getObserveConfig())) { + return ctx.getJosdkContext().getSecondaryResources(FlinkStateSnapshot.class); + } else { + return Set.of(); + } + }; + } Review Comment: Could this logic live in a static utility somewhere else? Maybe even in the timestamp storage or some other utility class -- 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