Repository navigation
next (5.8, do not merge) #3715
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
Open
csviri
wants to merge
3
commits into
main
Choose a base branch
from
next
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
next (5.8, do not merge) #3715
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
.../src/main/java/io/javaoperatorsdk/operator/health/InformerResourceVersionHealthCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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( | ||
| 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)) { | ||
| log.info( | ||
| "Informer resource version health check: '{}' resource version advanced again," | ||
| + " no longer stale", | ||
| key); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private record Observation( | ||
| InformerHealthIndicator informer, String resourceVersion, long since) {} | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Escape the components of each informer key.
Controller/event-source pairs
("a/b", "c")and("a", "b/c")both producea/b/c/ns. Explicit names retain these characters. The second insertion therefore replaces a distinct informer. That informer cannot appear in the stale result. Encode each component before joining them, including the escape character itself. Ordinary names can keep their current representation. (raw.githubusercontent.com)