Skip to content

Commit 80e7de4

Browse files
metacosmcsviri
authored andcommitted
refactor: avoid creating intermediate collections when unneeded (#3156)
* refactor: avoid creating intermediate collections when unneeded Also use constant filters and collectors Signed-off-by: Chris Laprun <metacosm@gmail.com> * fix: incorrect test Signed-off-by: Chris Laprun <metacosm@gmail.com> --------- Signed-off-by: Chris Laprun <metacosm@gmail.com>
1 parent 608cc95 commit 80e7de4

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/RuntimeInfo.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ private void checkIfStarted() {
6363
public boolean allEventSourcesAreHealthy() {
6464
checkIfStarted();
6565
return registeredControllers.stream()
66-
.filter(rc -> !rc.getControllerHealthInfo().unhealthyEventSources().isEmpty())
67-
.findFirst()
68-
.isEmpty();
66+
.noneMatch(rc -> rc.getControllerHealthInfo().hasUnhealthyEventSources());
6967
}
7068

7169
/**

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/health/ControllerHealthInfo.java

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
package io.javaoperatorsdk.operator.health;
1717

1818
import java.util.Map;
19+
import java.util.function.Predicate;
20+
import java.util.stream.Collector;
1921
import java.util.stream.Collectors;
22+
import java.util.stream.Stream;
2023

2124
import io.javaoperatorsdk.operator.processing.event.EventSourceManager;
2225
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
@@ -25,30 +28,49 @@
2528
@SuppressWarnings("rawtypes")
2629
public class ControllerHealthInfo {
2730

31+
private static final Predicate<EventSource> UNHEALTHY = e -> e.getStatus() == Status.UNHEALTHY;
32+
private static final Predicate<EventSource> INFORMER =
33+
e -> e instanceof InformerWrappingEventSourceHealthIndicator;
34+
private static final Predicate<EventSource> UNHEALTHY_INFORMER =
35+
e -> INFORMER.test(e) && e.getStatus() == Status.UNHEALTHY;
36+
private static final Collector<EventSource, ?, Map<String, EventSourceHealthIndicator>>
37+
NAME_TO_ES_MAP = Collectors.toMap(EventSource::name, e -> e);
38+
private static final Collector<
39+
EventSource, ?, Map<String, InformerWrappingEventSourceHealthIndicator>>
40+
NAME_TO_ES_HEALTH_MAP =
41+
Collectors.toMap(EventSource::name, e -> (InformerWrappingEventSourceHealthIndicator) e);
2842
private final EventSourceManager<?> eventSourceManager;
2943

3044
public ControllerHealthInfo(EventSourceManager eventSourceManager) {
3145
this.eventSourceManager = eventSourceManager;
3246
}
3347

3448
public Map<String, EventSourceHealthIndicator> eventSourceHealthIndicators() {
35-
return eventSourceManager.allEventSources().stream()
36-
.collect(Collectors.toMap(EventSource::name, e -> e));
49+
return eventSourceManager.allEventSourcesStream().collect(NAME_TO_ES_MAP);
50+
}
51+
52+
/**
53+
* Whether the associated {@link io.javaoperatorsdk.operator.processing.Controller} has unhealthy
54+
* event sources.
55+
*
56+
* @return {@code true} if any of the associated controller is unhealthy, {@code false} otherwise
57+
* @since 5.3.0
58+
*/
59+
public boolean hasUnhealthyEventSources() {
60+
return filteredEventSources(UNHEALTHY).findAny().isPresent();
3761
}
3862

3963
public Map<String, EventSourceHealthIndicator> unhealthyEventSources() {
40-
return eventSourceManager.allEventSources().stream()
41-
.filter(e -> e.getStatus() == Status.UNHEALTHY)
42-
.collect(Collectors.toMap(EventSource::name, e -> e));
64+
return filteredEventSources(UNHEALTHY).collect(NAME_TO_ES_MAP);
65+
}
66+
67+
private Stream<EventSource> filteredEventSources(Predicate<EventSource> filter) {
68+
return eventSourceManager.allEventSourcesStream().filter(filter);
4369
}
4470

4571
public Map<String, InformerWrappingEventSourceHealthIndicator>
4672
informerEventSourceHealthIndicators() {
47-
return eventSourceManager.allEventSources().stream()
48-
.filter(e -> e instanceof InformerWrappingEventSourceHealthIndicator)
49-
.collect(
50-
Collectors.toMap(
51-
EventSource::name, e -> (InformerWrappingEventSourceHealthIndicator) e));
73+
return filteredEventSources(INFORMER).collect(NAME_TO_ES_HEALTH_MAP);
5274
}
5375

5476
/**
@@ -58,11 +80,6 @@ public Map<String, EventSourceHealthIndicator> unhealthyEventSources() {
5880
*/
5981
public Map<String, InformerWrappingEventSourceHealthIndicator>
6082
unhealthyInformerEventSourceHealthIndicators() {
61-
return eventSourceManager.allEventSources().stream()
62-
.filter(e -> e.getStatus() == Status.UNHEALTHY)
63-
.filter(e -> e instanceof InformerWrappingEventSourceHealthIndicator)
64-
.collect(
65-
Collectors.toMap(
66-
EventSource::name, e -> (InformerWrappingEventSourceHealthIndicator) e));
83+
return filteredEventSources(UNHEALTHY_INFORMER).collect(NAME_TO_ES_HEALTH_MAP);
6784
}
6885
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/health/InformerHealthIndicator.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,5 @@ public interface InformerHealthIndicator extends EventSourceHealthIndicator {
2323

2424
boolean isRunning();
2525

26-
@Override
27-
Status getStatus();
28-
2926
String getTargetNamespace();
3027
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,12 @@ public Set<EventSource<?, P>> getRegisteredEventSources() {
217217

218218
@SuppressWarnings("rawtypes")
219219
public List<EventSource> allEventSources() {
220-
return eventSources.allEventSources().toList();
220+
return allEventSourcesStream().toList();
221+
}
222+
223+
@SuppressWarnings("rawtypes")
224+
public Stream<EventSource> allEventSourcesStream() {
225+
return eventSources.allEventSources();
221226
}
222227

223228
@SuppressWarnings("unused")

0 commit comments

Comments
 (0)