Skip to content

improve: remove parallel streams #1754

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.javaoperatorsdk.operator.api.config.ExecutorServiceManager;
import io.javaoperatorsdk.operator.processing.Controller;

/**
Expand All @@ -32,20 +33,27 @@ public synchronized void shouldStart() {
}

public synchronized void start(boolean startEventProcessor) {
controllers().parallelStream().forEach(c -> c.start(startEventProcessor));
ExecutorServiceManager.executeAndWaitForAllToComplete(controllers().stream(), c -> {
c.start(startEventProcessor);
return null;
}, c -> "Controller Starter for: " + c.getConfiguration().getName());
started = true;
}

public synchronized void stop() {
controllers().parallelStream().forEach(closeable -> {
log.debug("closing {}", closeable);
closeable.stop();
});
ExecutorServiceManager.executeAndWaitForAllToComplete(controllers().stream(), c -> {
log.debug("closing {}", c);
c.stop();
return null;
}, c -> "Controller Stopper for: " + c.getConfiguration().getName());
started = false;
}

public synchronized void startEventProcessing() {
controllers().parallelStream().forEach(Controller::startEventProcessing);
ExecutorServiceManager.executeAndWaitForAllToComplete(controllers().stream(), c -> {
c.startEventProcessing();
return null;
}, c -> "Event processor starter for: " + c.getConfiguration().getName());
}

@SuppressWarnings({"unchecked", "rawtypes"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public static <T> void executeAndWaitForAllToComplete(Stream<T> stream,
Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()));

try {
instrumented.invokeAll(stream.parallel().map(item -> (Callable<Void>) () -> {
instrumented.invokeAll(stream.map(item -> (Callable<Void>) () -> {
// change thread name for easier debugging
final var thread = Thread.currentThread();
final var name = thread.getName();
Expand All @@ -91,11 +91,13 @@ public static <T> void executeAndWaitForAllToComplete(Stream<T> stream,
} catch (ExecutionException e) {
throw new OperatorException(e.getCause());
} catch (InterruptedException e) {
log.warn("Interrupted.", e);
Thread.currentThread().interrupt();
}
});
shutdown(instrumented);
} catch (InterruptedException e) {
log.warn("Interrupted.", e);
Thread.currentThread().interrupt();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ private static Function<NamedEventSource, String> getThreadNamer(String stage) {
};
}

private static Function<NamespaceChangeable, String> getEventSourceThreadNamer(String stage) {
return es -> stage + " -> " + es;
}

@Override
public synchronized void stop() {
stopEventSource(eventSources.namedControllerResourceEventSource());
Expand Down Expand Up @@ -179,13 +183,15 @@ public void broadcastOnResourceEvent(ResourceAction action, P resource, P oldRes
public void changeNamespaces(Set<String> namespaces) {
eventSources.controllerResourceEventSource()
.changeNamespaces(namespaces);
eventSources
ExecutorServiceManager.executeAndWaitForAllToComplete(eventSources
.additionalEventSources()
.filter(NamespaceChangeable.class::isInstance)
.map(NamespaceChangeable.class::cast)
.filter(NamespaceChangeable::allowsNamespaceChanges)
.parallel()
.forEach(ies -> ies.changeNamespaces(namespaces));
.filter(NamespaceChangeable::allowsNamespaceChanges), e -> {
e.changeNamespaces(namespaces);
return null;
},
getEventSourceThreadNamer("changeNamespace"));
}

public Set<EventSource> getRegisteredEventSources() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ void invalidEventsShouldStopInformerAndCallInformerStoppedHandler() {
operator.create(v1res);

await()
.atMost(Duration.ofSeconds(1))
.atMost(Duration.ofSeconds(2))
.pollInterval(Duration.ofMillis(50))
.untilAsserted(() -> {
// v1 is the stored version so trying to create a v2 version should fail because we cannot
Expand Down