Skip to content

Commit

Permalink
Hook AlgorithmMetricsService to ProcedureExecutor
Browse files Browse the repository at this point in the history
Co-authored-by: Ioannis Panagiotas <ioannis.panagiotas@neotechnology.com>
  • Loading branch information
vnickolov and IoannisPanagiotas committed Nov 15, 2023
1 parent 45ac77e commit 11b99b1
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions executor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
annotationProcessor group: 'org.immutables', name: 'builder', version: ver.'immutables'
annotationProcessor group: 'org.immutables', name: 'value', version: ver.'immutables'

implementation project(':algorithm-metrics-api')
implementation project(':annotations')
implementation project(':algo')
implementation project(':algo-common')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.jetbrains.annotations.Nullable;
import org.neo4j.common.DependencyResolver;
import org.neo4j.gds.algorithms.metrics.AlgorithmMetricsService;
import org.neo4j.gds.algorithms.metrics.PassthroughAlgorithmMetricRegistrar;
import org.neo4j.gds.annotation.ValueClass;
import org.neo4j.gds.api.AlgorithmMetaDataSetter;
import org.neo4j.gds.api.CloseableResourceRegistry;
Expand Down Expand Up @@ -70,6 +72,8 @@ public interface ExecutionContext {

boolean isGdsAdmin();

AlgorithmMetricsService algorithmMetricsService();

@Nullable
RelationshipStreamExporterBuilder relationshipStreamExporterBuilder();

Expand Down Expand Up @@ -173,6 +177,11 @@ public boolean isGdsAdmin() {
return false;
}

@Override
public AlgorithmMetricsService algorithmMetricsService() {
return new AlgorithmMetricsService(new PassthroughAlgorithmMetricRegistrar());
}

@Override
public @Nullable RelationshipStreamExporterBuilder relationshipStreamExporterBuilder() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.neo4j.gds.AlgorithmFactory;
import org.neo4j.gds.GraphAlgorithmFactory;
import org.neo4j.gds.GraphStoreAlgorithmFactory;
import org.neo4j.gds.algorithms.metrics.AlgorithmMetricsService;
import org.neo4j.gds.api.Graph;
import org.neo4j.gds.api.GraphStore;
import org.neo4j.gds.config.AlgoBaseConfig;
Expand Down Expand Up @@ -110,7 +111,8 @@ public RESULT compute(

algo.getProgressTracker().setEstimatedResourceFootprint(memoryEstimationInBytes, config.concurrency());

ALGO_RESULT result = executeAlgorithm(builder, algo);

ALGO_RESULT result = executeAlgorithm(builder, algo, executionContext.algorithmMetricsService());

var computationResult = builder
.graph(graph)
Expand All @@ -125,15 +127,26 @@ public RESULT compute(

private ALGO_RESULT executeAlgorithm(
ImmutableComputationResult.Builder<ALGO, ALGO_RESULT, CONFIG> builder,
ALGO algo
ALGO algo,
AlgorithmMetricsService algorithmMetricsService
) {
return runWithExceptionLogging(
"Computation failed",
() -> {
try (ProgressTimer ignored = ProgressTimer.start(builder::computeMillis)) {
var algorithmMetric = algorithmMetricsService.create(
// we don't want to use `spec.name()` because it's different for the different procedure modes;
// we want to capture the algorithm name as defined by the algorithm factory `taskName()`
algoSpec.algorithmFactory(executionContext).taskName()
);
try (
ProgressTimer ignored = ProgressTimer.start(builder::computeMillis);
algorithmMetric;
) {
algorithmMetric.start();
return algo.compute();
} catch (Throwable e) {
algo.getProgressTracker().endSubTaskWithFailure();
algorithmMetric.failed();
throw e;
} finally {
if (algoSpec.releaseProgressTask()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.neo4j.gds.ProcedureCallContextReturnColumns;
import org.neo4j.gds.algorithms.metrics.AlgorithmMetricsService;
import org.neo4j.gds.algorithms.metrics.PassthroughAlgorithmMetricRegistrar;
import org.neo4j.gds.api.AlgorithmMetaDataSetter;
import org.neo4j.gds.api.CloseableResourceRegistry;
import org.neo4j.gds.api.GraphStore;
Expand Down Expand Up @@ -136,6 +138,7 @@ private ExecutionContext executionContext(TaskStore taskStore) {
.algorithmMetaDataSetter(AlgorithmMetaDataSetter.EMPTY)
.nodeLookup(NodeLookup.EMPTY)
.userLogRegistryFactory(EmptyUserLogRegistryFactory.INSTANCE)
.algorithmMetricsService(new AlgorithmMetricsService(new PassthroughAlgorithmMetricRegistrar()))
.build();
}

Expand Down
1 change: 1 addition & 0 deletions proc/common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {

implementation project(':annotations')
implementation project(':algo-common')
implementation project(':algorithm-metrics-api')
implementation project(':config-api')
implementation project(':core')
implementation project(':core-write')
Expand Down
5 changes: 5 additions & 0 deletions proc/common/src/main/java/org/neo4j/gds/BaseProc.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/
package org.neo4j.gds;

import org.neo4j.gds.algorithms.metrics.AlgorithmMetricsService;
import org.neo4j.gds.api.DatabaseId;
import org.neo4j.gds.compat.GraphDatabaseApiProxy;
import org.neo4j.gds.config.BaseConfig;
Expand Down Expand Up @@ -75,6 +76,9 @@ public abstract class BaseProc {
@Context
public Username username = Username.EMPTY_USERNAME;

@Context
public AlgorithmMetricsService algorithmMetricsService;

protected String username() {
return username.username();
}
Expand Down Expand Up @@ -157,6 +161,7 @@ public ExecutionContext executionContext() {
.algorithmMetaDataSetter(new TransactionAlgorithmMetaDataSetter(transaction))
.nodeLookup(new TransactionNodeLookup(transaction))
.isGdsAdmin(transactionContext().isGdsAdmin())
.algorithmMetricsService(algorithmMetricsService)
.build();
}

Expand Down
1 change: 1 addition & 0 deletions procedures/extension/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {
compileOnly(group: 'org.neo4j', name: 'neo4j-logging', version: ver.'neo4j') { transitive = false }

// the necessary GDS things for the extension to construct the application
implementation project(':algorithm-metrics-api')
implementation project(':config-api')
implementation project(':core')
implementation project(':core-utils')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import org.neo4j.annotations.service.ServiceProvider;
import org.neo4j.configuration.Config;
import org.neo4j.gds.algorithms.metrics.AlgorithmMetricsService;
import org.neo4j.gds.algorithms.metrics.PassthroughAlgorithmMetricRegistrar;
import org.neo4j.gds.applications.graphstorecatalog.CatalogBusinessFacade;
import org.neo4j.gds.core.write.NativeExportBuildersProvider;
import org.neo4j.gds.procedures.GraphDataScience;
Expand Down Expand Up @@ -64,10 +66,20 @@ public Lifecycle newInstance(ExtensionContext extensionContext, Dependencies dep
// we have no extra checks to do in OpenGDS
Optional<Function<CatalogBusinessFacade, CatalogBusinessFacade>> businessFacadeDecorator = Optional.empty();

var algorithmMetricsService = new AlgorithmMetricsService(new PassthroughAlgorithmMetricRegistrar());

extensionBuilder
.withComponent(
GraphDataScience.class,
() -> extensionBuilder.gdsProvider(exporterBuildersProviderService, businessFacadeDecorator)
() -> extensionBuilder.gdsProvider(
exporterBuildersProviderService,
businessFacadeDecorator,
algorithmMetricsService
)
)
.withComponent(
AlgorithmMetricsService.class,
() -> ctx -> algorithmMetricsService
)
.registerExtension();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.gds.procedures.integration;

import org.neo4j.function.ThrowingFunction;
import org.neo4j.gds.algorithms.metrics.AlgorithmMetricsService;
import org.neo4j.gds.applications.graphstorecatalog.CatalogBusinessFacade;
import org.neo4j.gds.core.loading.GraphStoreCatalogService;
import org.neo4j.gds.core.utils.progress.ProgressFeatureSettings;
Expand Down Expand Up @@ -187,10 +188,12 @@ public void registerExtension() {
*
* @param exporterBuildersProviderService The catalog of writers
* @param businessFacadeDecorator Any checks added across requests
* @param algorithmMetricsService
*/
public ThrowingFunction<Context, GraphDataScience, ProcedureException> gdsProvider(
ExporterBuildersProviderService exporterBuildersProviderService,
Optional<Function<CatalogBusinessFacade, CatalogBusinessFacade>> businessFacadeDecorator
Optional<Function<CatalogBusinessFacade, CatalogBusinessFacade>> businessFacadeDecorator,
AlgorithmMetricsService algorithmMetricsService
) {
var catalogFacadeProvider = createCatalogFacadeProvider(
exporterBuildersProviderService,
Expand Down

0 comments on commit 11b99b1

Please sign in to comment.