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

KAFKA-15022: Detect negative cycle from one source #14696

Merged
merged 4 commits into from
Nov 28, 2023

Conversation

lihaosky
Copy link
Contributor

@lihaosky lihaosky commented Nov 2, 2023

Description

Introduce a dummy node connected to every other node and run Bellman-ford from the dummy node once instead of from every node in the graph.

Testing

Existing test

Committer Checklist (excluded from commit message)

  • Verify design and implementation
  • Verify test coverage and CI build status
  • Verify documentation (including upgrade notes)

@lihaosky lihaosky requested a review from mjsax November 2, 2023 22:35
@lihaosky lihaosky added streams kip Requires or implements a KIP labels Nov 2, 2023
@ex172000
Copy link
Contributor

ex172000 commented Nov 3, 2023

Will this impact the performance? If so, do we plan to add a performance comparison?


@Override
public int compare(final V o1, final V o2) {
if (o1 == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we plan to have different results returned under two different null cases? If not, maybe we can do

if (o1 == null || o2 == null) {
    return -1;
}

to simplify the condition.


private void removeDummySourceNode(final Graph<V> residualGraph) {
if (!residualGraph.isResidualGraph) {
throw new IllegalStateException("Graph should be residual graph to add dummy source node");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe 'Graph should be residual graph to remove dummy source node' ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@lihaosky
Copy link
Contributor Author

lihaosky commented Nov 3, 2023

@ex172000 , this will improve performance. Since it's obvious that we replace the loop of number of nodes to a single iteration, I'm not planning to add comparison.

@mjsax mjsax changed the title [KAFKA-15022] optimization: detect negative cycle from one source KAFKA-15022: detect negative cycle from one source Nov 6, 2023
@mjsax mjsax changed the title KAFKA-15022: detect negative cycle from one source KAFKA-15022: Detect negative cycle from one source Nov 6, 2023
@Override
public int compare(final V o1, final V o2) {
if (o1 == null || o2 == null) {
return -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If one of both is null, why is o1 considered smaller than o2? Would it not be better to define null as either smallest or largest globally? -- Also, should we return zero if both are null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, if both are null, we can return 0;

@@ -117,6 +131,8 @@ private Graph(final boolean isResidualGraph) {
}

public void addEdge(final V u, final V v, final int capacity, final int cost, final int flow) {
Objects.requireNonNull(u);
Objects.requireNonNull(v);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null is a special node we introduced in graph as dummy source to detect negative cycle. So users can use it

@mjsax mjsax merged commit bbd75b8 into apache:trunk Nov 28, 2023
1 check failed
@lihaosky lihaosky deleted the graph-opt branch December 7, 2023 21:16
ex172000 pushed a commit to ex172000/kafka that referenced this pull request Dec 15, 2023
Introduce a dummy node connected to every other node and run Bellman-ford from the dummy node once instead of from every node in the graph.

Reviewers: Qichao Chu (@ex172000), Matthias J. Sax <[email protected]>
yyu1993 pushed a commit to yyu1993/kafka that referenced this pull request Feb 15, 2024
Introduce a dummy node connected to every other node and run Bellman-ford from the dummy node once instead of from every node in the graph.

Reviewers: Qichao Chu (@ex172000), Matthias J. Sax <[email protected]>
AnatolyPopov pushed a commit to aiven/kafka that referenced this pull request Feb 16, 2024
Introduce a dummy node connected to every other node and run Bellman-ford from the dummy node once instead of from every node in the graph.

Reviewers: Qichao Chu (@ex172000), Matthias J. Sax <[email protected]>
clolov pushed a commit to clolov/kafka that referenced this pull request Apr 5, 2024
Introduce a dummy node connected to every other node and run Bellman-ford from the dummy node once instead of from every node in the graph.

Reviewers: Qichao Chu (@ex172000), Matthias J. Sax <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kip Requires or implements a KIP streams
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants