Skip to content

Commit 018f800

Browse files
authored
Merge pull request #5211 from s1ck/pregel-expose-log-methods
Expose log methods in PregelContext
2 parents 180ef72 + 62e2a7e commit 018f800

File tree

9 files changed

+76
-14
lines changed

9 files changed

+76
-14
lines changed

doc/asciidoc/algorithms/algorithms-pregel-api.adoc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,34 @@ public class CustomComputation implements PregelComputation<CustomConfig> {
349349
}
350350
----
351351

352+
[[algorithms-pregel-api-logging]]
353+
=== Logging
354+
355+
The following methods are available for all contexts (`InitContext`, `ComputeContext`, `MasterComputeContext`) to inject custom messages into the progress log of the algorithm execution.
356+
357+
.The log methods can be used in Pregel contexts
358+
[source, java]
359+
----
360+
// All contexts inherit from PregelContext
361+
public abstract class PregelContext<CONFIG extends PregelConfig> {
362+
363+
// Log a debug message to the Neo4j log.
364+
public void logDebug(String message) {
365+
progressTracker.logDebug(message);
366+
}
367+
368+
// Log a warning message to the Neo4j log.
369+
public void logWarning(String message) {
370+
progressTracker.logWarning(message);
371+
}
372+
373+
// Log a info message to the Neo4j log
374+
public void logMessage(String message) {
375+
progressTracker.logMessage(message);
376+
}
377+
378+
}
379+
----
352380

353381
[[algorithms-pregel-api-procedure]]
354382
== Run Pregel via Cypher

pregel/src/main/java/org/neo4j/gds/beta/pregel/ForkJoinComputeStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ public final class ForkJoinComputeStep<CONFIG extends PregelConfig, ITERATOR ext
7575
this.nodeBatch = nodeBatch;
7676
this.nodeValue = nodeValue;
7777
this.messenger = messenger;
78-
this.computeContext = new ComputeContext<>(this, config);
78+
this.computeContext = new ComputeContext<>(this, config, progressTracker);
7979
this.sentMessage = sentMessage;
8080
this.progressTracker = progressTracker;
81-
this.initContext = new InitContext<>(this, config, graph);
81+
this.initContext = new InitContext<>(this, config, graph, progressTracker);
8282
}
8383

8484
@Override

pregel/src/main/java/org/neo4j/gds/beta/pregel/PartitionedComputeStep.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ public final class PartitionedComputeStep<CONFIG extends PregelConfig, ITERATOR
6060
this.voteBits = voteBits;
6161
this.nodeBatch = nodeBatch;
6262
this.messenger = messenger;
63-
this.computeContext = new ComputeContext<>(this, config);
63+
this.computeContext = new ComputeContext<>(this, config, progressTracker);
6464
this.progressTracker = progressTracker;
65-
this.initContext = new InitContext<>(this, config, graph);
65+
this.initContext = new InitContext<>(this, config, graph, progressTracker);
6666
}
6767

6868
@Override

pregel/src/main/java/org/neo4j/gds/beta/pregel/Pregel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public void release() {
200200
}
201201

202202
private boolean runMasterComputeStep(int iteration) {
203-
var context = new MasterComputeContext<>(config, graph, iteration, nodeValues, executor);
203+
var context = new MasterComputeContext<>(config, graph, iteration, nodeValues, executor, progressTracker);
204204
var didConverge = computation.masterCompute(context);
205205
return didConverge;
206206
}

pregel/src/main/java/org/neo4j/gds/beta/pregel/context/ComputeContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.neo4j.gds.beta.pregel.ComputeStep;
2323
import org.neo4j.gds.beta.pregel.PregelConfig;
24+
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
2425

2526
/**
2627
* A context that is used during the computation. It allows an implementation
@@ -29,8 +30,8 @@
2930
*/
3031
public final class ComputeContext<CONFIG extends PregelConfig> extends NodeCentricContext<CONFIG> {
3132

32-
public ComputeContext(ComputeStep<CONFIG, ?> computeStep, CONFIG config) {
33-
super(computeStep, config);
33+
public ComputeContext(ComputeStep<CONFIG, ?> computeStep, CONFIG config, ProgressTracker progressTracker) {
34+
super(computeStep, config, progressTracker);
3435
this.sendMessagesFunction = config.hasRelationshipWeightProperty()
3536
? computeStep::sendToNeighborsWeighted
3637
: computeStep::sendToNeighbors;

pregel/src/main/java/org/neo4j/gds/beta/pregel/context/InitContext.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.neo4j.gds.api.properties.nodes.NodePropertyValues;
2424
import org.neo4j.gds.beta.pregel.ComputeStep;
2525
import org.neo4j.gds.beta.pregel.PregelConfig;
26+
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
2627

2728
import java.util.Set;
2829

@@ -35,8 +36,13 @@
3536
public final class InitContext<CONFIG extends PregelConfig> extends NodeCentricContext<CONFIG> {
3637
private final NodePropertyContainer nodePropertyContainer;
3738

38-
public InitContext(ComputeStep<CONFIG, ?> computeStep, CONFIG config, NodePropertyContainer nodePropertyContainer) {
39-
super(computeStep, config);
39+
public InitContext(
40+
ComputeStep<CONFIG, ?> computeStep,
41+
CONFIG config,
42+
NodePropertyContainer nodePropertyContainer,
43+
ProgressTracker progressTracker
44+
) {
45+
super(computeStep, config, progressTracker);
4046
this.nodePropertyContainer = nodePropertyContainer;
4147
}
4248

pregel/src/main/java/org/neo4j/gds/beta/pregel/context/MasterComputeContext.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.neo4j.gds.api.Graph;
2323
import org.neo4j.gds.beta.pregel.NodeValue;
2424
import org.neo4j.gds.beta.pregel.PregelConfig;
25+
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
2526

2627
import java.util.concurrent.ExecutorService;
2728
import java.util.function.LongPredicate;
@@ -39,9 +40,10 @@ public MasterComputeContext(
3940
Graph graph,
4041
int iteration,
4142
NodeValue nodeValue,
42-
ExecutorService executorService
43+
ExecutorService executorService,
44+
ProgressTracker progressTracker
4345
) {
44-
super(config);
46+
super(config, progressTracker);
4547
this.graph = graph;
4648
this.iteration = iteration;
4749
this.nodeValue = nodeValue;

pregel/src/main/java/org/neo4j/gds/beta/pregel/context/NodeCentricContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.neo4j.gds.beta.pregel.ComputeStep;
2323
import org.neo4j.gds.beta.pregel.PregelConfig;
24+
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
2425

2526
import java.util.function.LongConsumer;
2627

@@ -30,8 +31,8 @@ public abstract class NodeCentricContext<CONFIG extends PregelConfig> extends Pr
3031

3132
long nodeId;
3233

33-
NodeCentricContext(ComputeStep<CONFIG, ?> computeStep, CONFIG config) {
34-
super(config);
34+
NodeCentricContext(ComputeStep<CONFIG, ?> computeStep, CONFIG config, ProgressTracker progressTracker) {
35+
super(config, progressTracker);
3536
this.computeStep = computeStep;
3637
}
3738

pregel/src/main/java/org/neo4j/gds/beta/pregel/context/PregelContext.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
package org.neo4j.gds.beta.pregel.context;
2121

2222
import org.neo4j.gds.beta.pregel.PregelConfig;
23+
import org.neo4j.gds.core.utils.progress.tasks.ProgressTracker;
2324

2425
public abstract class PregelContext<CONFIG extends PregelConfig> {
2526

2627
protected final CONFIG config;
28+
private final ProgressTracker progressTracker;
2729

28-
protected PregelContext(CONFIG config) {
30+
protected PregelContext(CONFIG config, ProgressTracker progressTracker) {
2931
this.config = config;
32+
this.progressTracker = progressTracker;
3033
}
3134

3235
/**
@@ -36,6 +39,27 @@ public CONFIG config() {
3639
return config;
3740
}
3841

42+
/**
43+
* Log a debug message to the Neo4j log.
44+
*/
45+
public void logDebug(String message) {
46+
progressTracker.logDebug(message);
47+
}
48+
49+
/**
50+
* Log a warning message to the Neo4j log.
51+
*/
52+
public void logWarning(String message) {
53+
progressTracker.logWarning(message);
54+
}
55+
56+
/**
57+
* Log a info message to the Neo4j log
58+
*/
59+
public void logMessage(String message) {
60+
progressTracker.logMessage(message);
61+
}
62+
3963
/**
4064
* Indicates whether the input graph is a multi-graph.
4165
*/

0 commit comments

Comments
 (0)