-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introducing algorithm machinery to manage running algorithms and prod…
…ding progress tracker the right way
- Loading branch information
1 parent
04e32cf
commit 6660781
Showing
15 changed files
with
341 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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
54 changes: 54 additions & 0 deletions
54
...ery/src/main/java/org/neo4j/gds/applications/algorithms/machinery/AlgorithmMachinery.java
This file contains 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,54 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.gds.applications.algorithms.machinery; | ||
|
||
import org.neo4j.gds.Algorithm; | ||
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker; | ||
|
||
/** | ||
* I wish this did not exist quite like this; it is where we encapsulate running an algorithm, | ||
* managing termination, and handling (progress tracker) resources. | ||
* Somehow I wish that was encapsulated more naturally, but as you can hear from this use of language, | ||
* the design has not crystallized yet. | ||
* At least nothing here is tied to termination flag. | ||
*/ | ||
public class AlgorithmMachinery { | ||
/** | ||
* Runs algorithm. | ||
* Optionally releases progress tracker. | ||
* Exceptionally marks progress tracker state as failed. | ||
* | ||
* @return algorithm result, or an error in the form of an exception | ||
*/ | ||
public <RESULT> RESULT runAlgorithmsAndManageProgressTracker( | ||
Algorithm<RESULT> algorithm, | ||
ProgressTracker progressTracker, | ||
boolean shouldReleaseProgressTracker | ||
) { | ||
try { | ||
return algorithm.compute(); | ||
} catch (Exception e) { | ||
progressTracker.endSubTaskWithFailure(); | ||
throw e; | ||
} finally { | ||
if (shouldReleaseProgressTracker) progressTracker.release(); | ||
} | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
...src/test/java/org/neo4j/gds/applications/algorithms/machinery/AlgorithmMachineryTest.java
This file contains 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,104 @@ | ||
/* | ||
* Copyright (c) "Neo4j" | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* Neo4j is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package org.neo4j.gds.applications.algorithms.machinery; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.fail; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
|
||
class AlgorithmMachineryTest { | ||
@Test | ||
void shouldRunAlgorithm() { | ||
var algorithmMachinery = new AlgorithmMachinery(); | ||
|
||
var progressTracker = mock(ProgressTracker.class); | ||
var result = algorithmMachinery.runAlgorithmsAndManageProgressTracker( | ||
new RegurgitatingAlgorithm("Hello, world!"), | ||
progressTracker, | ||
false | ||
); | ||
|
||
assertThat(result).isEqualTo("Hello, world!"); | ||
|
||
verifyNoInteractions(progressTracker); | ||
} | ||
|
||
@Test | ||
void shouldReleaseProgressTrackerWhenAsked() { | ||
var algorithmMachinery = new AlgorithmMachinery(); | ||
|
||
var progressTracker = mock(ProgressTracker.class); | ||
var result = algorithmMachinery.runAlgorithmsAndManageProgressTracker( | ||
new RegurgitatingAlgorithm("Dodgers win world series!"), | ||
progressTracker, | ||
true | ||
); | ||
|
||
assertThat(result).isEqualTo("Dodgers win world series!"); | ||
|
||
verify(progressTracker).release(); | ||
} | ||
|
||
@Test | ||
void shouldMarkProgressTracker() { | ||
var algorithmMachinery = new AlgorithmMachinery(); | ||
|
||
var progressTracker = mock(ProgressTracker.class); | ||
var exception = new RuntimeException("Whoops!"); | ||
try { | ||
algorithmMachinery.runAlgorithmsAndManageProgressTracker( | ||
new FailingAlgorithm(exception), | ||
progressTracker, | ||
false | ||
); | ||
fail(); | ||
} catch (Exception e) { | ||
assertThat(e).hasMessage("Whoops!"); | ||
} | ||
|
||
verify(progressTracker).endSubTaskWithFailure(); | ||
} | ||
|
||
@Test | ||
void shouldMarkProgressTrackerAndReleaseIt() { | ||
var algorithmMachinery = new AlgorithmMachinery(); | ||
|
||
var progressTracker = mock(ProgressTracker.class); | ||
var exception = new RuntimeException("Yeah, no..."); | ||
try { | ||
algorithmMachinery.runAlgorithmsAndManageProgressTracker( | ||
new FailingAlgorithm(exception), | ||
progressTracker, | ||
true | ||
); | ||
fail(); | ||
} catch (Exception e) { | ||
assertThat(e).hasMessage("Yeah, no..."); | ||
} | ||
|
||
verify(progressTracker).endSubTaskWithFailure(); | ||
verify(progressTracker).release(); | ||
} | ||
} |
Oops, something went wrong.