Skip to content
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

Fix potential TopologyException when running multiple vulnerability-analyzer instances #796

Merged
merged 1 commit into from
Sep 11, 2023

Conversation

nscuro
Copy link
Member

@nscuro nscuro commented Sep 9, 2023

Kafka Streams works based on topologies. It relies on the fact that the exact same topology is present on all instances of a KS app. This means that the order of topology nodes must be identical.

For the vulnerability-analyzer, users can enable or disable individual scanners. Only enabled scanners will be added to the topology on startup.

Scanners are CDI beans, and we inject all of them via Instance. Instance can contain zero or more beans.

Scanners are added to the topology by iterating over the Instance bean. The problem here is that the order of the beans in Instance is not guaranteed. It could happen that instance A of the service added scanners in the order 123, whereas instance B added them in order 213.

A symptom of this problem is the following exception, followed by a shutdown of Kafka Streams:

org.apache.kafka.streams.errors.TopologyException: Invalid topology: Topic hyades-vulnerability-analyzer-scan-task-ossindex-repartition is unknown to the topology. This may happen if different KafkaStreams instances of the same application execute different Topologies. Note that Topologies are only identical if all operators are added in the same order.

Sorting the beans before adding them to the topology resolved the issue.

…-analyzer instances

Kafka Streams works based on topologies. It relies on the fact that the *exact* same topology is present on all instances of a KS app. This means that the order of topology nodes must be identical.

For the vulnerability-analyzer, users can enable or disable individual scanners. Only enabled scanners will be added to the topology on startup.

Scanners are CDI beans, and we inject all of them via [`Instance`](https://jakarta.ee/specifications/cdi/3.0/apidocs/jakarta/enterprise/inject/instance). `Instance` can contain zero or more beans.

Scanners are added to the topology by iterating over the `Instance` bean. The problem here is that the order of the beans in `Instance` is not guaranteed. It could happen that instance A of the service added scanners in the order 123, whereas instance B added them in order 213.

A symptom of this problem is the following exception, followed by a shutdown of Kafka Streams:

```
org.apache.kafka.streams.errors.TopologyException: Invalid topology: Topic hyades-vulnerability-analyzer-scan-task-ossindex-repartition is unknown to the topology. This may happen if different KafkaStreams instances of the same application execute different Topologies. Note that Topologies are only identical if all operators are added in the same order.
```

Sorting the beans before adding them to the topology resolved the issue.

Signed-off-by: nscuro <nscuro@protonmail.com>
@nscuro nscuro added defect Something isn't working domain/vuln-analysis labels Sep 9, 2023
@sonarcloud
Copy link

sonarcloud bot commented Sep 9, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@VithikaS VithikaS merged commit 3ace6ec into main Sep 11, 2023
12 checks passed
@VithikaS VithikaS deleted the fix-topology-exception-with-multiple-vuln-analyzers branch September 11, 2023 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect Something isn't working domain/vuln-analysis
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants