Repository navigation
feat: add informer resource version health check utility #3733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4335701
a9434cf
79f1670
a41d1bc
a0ec419
3058dd4
928445c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed 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 io.javaoperatorsdk.operator.health; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
| import java.util.function.LongSupplier; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import io.javaoperatorsdk.operator.RuntimeInfo; | ||
|
|
||
| /** | ||
| * Tracks how long each informer's last synchronized resource version has remained unchanged. An | ||
| * unchanged version can be normal in a quiet cluster, so callers should choose a suitable timeout | ||
| * and decide how to use the resulting diagnostic signal. | ||
| * | ||
| * <p>Keep one instance for successive checks and pass all current informers to each call. The keys | ||
| * should uniquely identify informers across controllers, event sources and namespaces. Informers | ||
| * that disappear, have no resource version, or are already unhealthy are forgotten. | ||
| */ | ||
| public class InformerResourceVersionHealthCheck { | ||
|
|
||
| private static final Logger log = | ||
| LoggerFactory.getLogger(InformerResourceVersionHealthCheck.class); | ||
|
|
||
| private final Duration timeout; | ||
| private final LongSupplier ticker; | ||
| private final Map<String, Observation> observations = new HashMap<>(); | ||
| private Set<String> previouslyStale = Set.of(); | ||
|
|
||
| public InformerResourceVersionHealthCheck(Duration timeout) { | ||
| this(timeout, System::nanoTime); | ||
| } | ||
|
|
||
| // package-private seam so tests can supply a controllable ticker instead of System::nanoTime | ||
| InformerResourceVersionHealthCheck(Duration timeout, LongSupplier ticker) { | ||
| this.timeout = Objects.requireNonNull(timeout); | ||
| if (timeout.isZero() || timeout.isNegative()) { | ||
| throw new IllegalArgumentException("timeout must be positive"); | ||
| } | ||
| this.ticker = Objects.requireNonNull(ticker); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the keys of informers that are considered unhealthy because, despite reporting a | ||
| * healthy status, their resource version has not advanced within the timeout. | ||
| */ | ||
| public synchronized Set<String> staleInformers( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please support log messages similar in FlinkOperator: So we interested in when an informer became unhealthy and/or became healthy again.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. I also wired the check into the |
||
| Map<String, ? extends InformerHealthIndicator> informers) { | ||
| Objects.requireNonNull(informers); | ||
| observations.keySet().retainAll(informers.keySet()); | ||
| var now = ticker.getAsLong(); | ||
| Set<String> stale = new HashSet<>(); | ||
| informers.forEach( | ||
| (key, informer) -> { | ||
| var resourceVersion = informer.getLastSyncResourceVersion(); | ||
| if (informer.getStatus() != Status.HEALTHY || resourceVersion == null) { | ||
| observations.remove(key); | ||
| return; | ||
| } | ||
|
|
||
| var last = observations.get(key); | ||
| if (last == null | ||
| || last.informer() != informer | ||
| || !last.resourceVersion().equals(resourceVersion)) { | ||
| observations.put(key, new Observation(informer, resourceVersion, now)); | ||
| } else if (Duration.ofNanos(now - last.since()).compareTo(timeout) >= 0) { | ||
| stale.add(key); | ||
| } | ||
| }); | ||
| logTransitions(stale); | ||
| previouslyStale = Set.copyOf(stale); | ||
| return previouslyStale; | ||
| } | ||
|
|
||
| /** | ||
| * Convenience overload that collects the current informer health indicators from {@code | ||
| * runtimeInfo} and delegates to {@link #staleInformers(Map)}. | ||
| */ | ||
| public synchronized Set<String> staleInformers(RuntimeInfo runtimeInfo) { | ||
| return staleInformers(collectInformerHealthIndicators(runtimeInfo)); | ||
| } | ||
|
|
||
| private static Map<String, InformerHealthIndicator> collectInformerHealthIndicators( | ||
| RuntimeInfo runtimeInfo) { | ||
| Map<String, InformerHealthIndicator> informers = new HashMap<>(); | ||
| runtimeInfo | ||
| .getRegisteredControllers() | ||
| .forEach( | ||
| controller -> | ||
| controller | ||
| .getControllerHealthInfo() | ||
| .informerEventSourceHealthIndicators() | ||
| .forEach( | ||
| (eventSourceName, eventSource) -> { | ||
| InformerWrappingEventSourceHealthIndicator<?> wrapping = eventSource; | ||
| wrapping | ||
| .informerHealthIndicators() | ||
| .forEach( | ||
| (namespace, informer) -> | ||
| informers.put( | ||
| controller.getConfiguration().getName() | ||
| + "/" | ||
| + eventSourceName | ||
| + "/" | ||
| + namespace, | ||
| informer)); | ||
| })); | ||
| return informers; | ||
| } | ||
|
|
||
| private void logTransitions(Set<String> stale) { | ||
| for (var key : stale) { | ||
| if (!previouslyStale.contains(key)) { | ||
| log.warn( | ||
| "Informer resource version health check: '{}' has not advanced its resource version" | ||
| + " for at least {}, marking as stale", | ||
| key, | ||
| timeout); | ||
| } | ||
| } | ||
| for (var key : previouslyStale) { | ||
| if (!stale.contains(key) && observations.containsKey(key)) { | ||
|
csviri marked this conversation as resolved.
Resolved
|
||
| log.info( | ||
| "Informer resource version health check: '{}' resource version advanced again," | ||
| + " no longer stale", | ||
| key); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private record Observation( | ||
| InformerHealthIndicator informer, String resourceVersion, long since) {} | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is purely for test seam.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, it's a package-private seam purely for the test —
InformerResourceVersionHealthCheckTestis in the same package and injects a controllableLongSupplierso tests don't need real sleeps to exercise the timeout. It isn't used anywhere else (verified via a repo-wide search). Added a one-line comment above it to make that explicit rather than changing the API shape, since it's already package-private and not part of the public surface.