Skip to content

Commit

Permalink
move application facade up so that it can cover more verticals
Browse files Browse the repository at this point in the history
  • Loading branch information
lassewesth committed Apr 4, 2024
1 parent 216600f commit 9e78618
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,8 @@
import org.neo4j.gds.api.NodeLookup;
import org.neo4j.gds.api.User;
import org.neo4j.gds.applications.ApplicationsFacade;
import org.neo4j.gds.applications.algorithms.pathfinding.AlgorithmEstimationTemplate;
import org.neo4j.gds.applications.algorithms.pathfinding.AlgorithmProcessingTemplate;
import org.neo4j.gds.configuration.DefaultsConfiguration;
import org.neo4j.gds.configuration.LimitsConfiguration;
import org.neo4j.gds.core.utils.progress.TaskRegistryFactory;
import org.neo4j.gds.core.utils.warnings.UserLogRegistryFactory;
import org.neo4j.gds.core.write.NodePropertyExporterBuilder;
import org.neo4j.gds.core.write.RelationshipExporterBuilder;
import org.neo4j.gds.core.write.RelationshipStreamExporterBuilder;
import org.neo4j.gds.logging.Log;
import org.neo4j.gds.modelcatalogservices.ModelCatalogService;
import org.neo4j.gds.procedures.algorithms.configuration.ConfigurationCreator;
import org.neo4j.gds.procedures.algorithms.configuration.ConfigurationParser;
Expand All @@ -80,11 +72,9 @@
import org.neo4j.gds.procedures.embeddings.NodeEmbeddingsProcedureFacade;
import org.neo4j.gds.procedures.misc.MiscAlgorithmsProcedureFacade;
import org.neo4j.gds.procedures.similarity.SimilarityProcedureFacade;
import org.neo4j.gds.termination.TerminationFlag;

class AlgorithmFacadeFactory {
// Global scoped dependencies
private final Log log;
private final DefaultsConfiguration defaultsConfiguration;
private final LimitsConfiguration limitsConfiguration;

Expand All @@ -100,19 +90,10 @@ class AlgorithmFacadeFactory {
private final WriteRelationshipService writeRelationshipService;
private final AlgorithmEstimator algorithmEstimator;
private final AlgorithmRunner algorithmRunner;
private final AlgorithmProcessingTemplate algorithmProcessingTemplate;
private final AlgorithmEstimationTemplate algorithmEstimationTemplate;
private final NodePropertyExporterBuilder nodePropertyExporterBuilder;
private final RelationshipExporterBuilder relationshipExporterBuilder;
private final RelationshipStreamExporterBuilder relationshipStreamExporterBuilder;
private final TaskRegistryFactory taskRegistryFactory;
private final TerminationFlag terminationFlag;
private final User user;
private final UserLogRegistryFactory userLogRegistryFactory;
private final ModelCatalogService modelCatalogService;

AlgorithmFacadeFactory(
Log log,
DefaultsConfiguration defaultsConfiguration,
LimitsConfiguration limitsConfiguration,
CloseableResourceRegistry closeableResourceRegistry,
Expand All @@ -126,18 +107,9 @@ class AlgorithmFacadeFactory {
WriteRelationshipService writeRelationshipService,
AlgorithmRunner algorithmRunner,
AlgorithmEstimator algorithmEstimator,
AlgorithmProcessingTemplate algorithmProcessingTemplate,
AlgorithmEstimationTemplate algorithmEstimationTemplate,
NodePropertyExporterBuilder nodePropertyExporterBuilder,
RelationshipExporterBuilder relationshipExporterBuilder,
RelationshipStreamExporterBuilder relationshipStreamExporterBuilder,
TaskRegistryFactory taskRegistryFactory,
TerminationFlag terminationFlag,
User user,
UserLogRegistryFactory userLogRegistryFactory,
ModelCatalogService modelCatalogService
) {
this.log = log;
this.defaultsConfiguration = defaultsConfiguration;
this.limitsConfiguration = limitsConfiguration;

Expand All @@ -154,15 +126,6 @@ class AlgorithmFacadeFactory {
this.algorithmRunner = algorithmRunner;
this.algorithmEstimator = algorithmEstimator;
this.modelCatalogService = modelCatalogService;

this.algorithmProcessingTemplate = algorithmProcessingTemplate;
this.algorithmEstimationTemplate = algorithmEstimationTemplate;
this.nodePropertyExporterBuilder = nodePropertyExporterBuilder;
this.relationshipExporterBuilder = relationshipExporterBuilder;
this.relationshipStreamExporterBuilder = relationshipStreamExporterBuilder;
this.taskRegistryFactory = taskRegistryFactory;
this.terminationFlag = terminationFlag;
this.userLogRegistryFactory = userLogRegistryFactory;
}

CentralityProcedureFacade createCentralityProcedureFacade() {
Expand Down Expand Up @@ -285,20 +248,7 @@ MiscAlgorithmsProcedureFacade createMiscellaneousProcedureFacade() {
);
}

PathFindingProcedureFacade createPathFindingProcedureFacade() {
// eventually this moves up and is shared
var applicationsFacade = ApplicationsFacade.create(
log,
nodePropertyExporterBuilder,
relationshipExporterBuilder,
relationshipStreamExporterBuilder,
taskRegistryFactory,
terminationFlag,
userLogRegistryFactory,
algorithmProcessingTemplate,
algorithmEstimationTemplate
);

PathFindingProcedureFacade createPathFindingProcedureFacade(ApplicationsFacade applicationsFacade) {
return PathFindingProcedureFacade.create(
defaultsConfiguration,
limitsConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,98 +30,78 @@
import org.neo4j.gds.algorithms.similarity.MutateRelationshipService;
import org.neo4j.gds.algorithms.similarity.WriteRelationshipService;
import org.neo4j.gds.algorithms.writeservices.WriteNodePropertyService;
import org.neo4j.gds.applications.algorithms.pathfinding.AlgorithmEstimationTemplate;
import org.neo4j.gds.applications.algorithms.pathfinding.AlgorithmProcessingTemplate;
import org.neo4j.gds.applications.algorithms.pathfinding.DefaultAlgorithmProcessingTemplate;
import org.neo4j.gds.applications.algorithms.pathfinding.DefaultMemoryGuard;
import org.neo4j.gds.api.DatabaseId;
import org.neo4j.gds.api.User;
import org.neo4j.gds.configuration.DefaultsConfiguration;
import org.neo4j.gds.configuration.LimitsConfiguration;
import org.neo4j.gds.core.loading.GraphStoreCatalogService;
import org.neo4j.gds.core.write.ExporterContext;
import org.neo4j.gds.core.utils.progress.TaskRegistryFactory;
import org.neo4j.gds.core.utils.warnings.UserLogRegistryFactory;
import org.neo4j.gds.core.write.NodePropertyExporterBuilder;
import org.neo4j.gds.core.write.RelationshipExporterBuilder;
import org.neo4j.gds.logging.Log;
import org.neo4j.gds.mem.MemoryGauge;
import org.neo4j.gds.memest.DatabaseGraphStoreEstimationService;
import org.neo4j.gds.memest.FictitiousGraphStoreEstimationService;
import org.neo4j.gds.metrics.algorithms.AlgorithmMetricsService;
import org.neo4j.gds.modelcatalogservices.ModelCatalogServiceProvider;
import org.neo4j.gds.procedures.KernelTransactionAccessor;
import org.neo4j.gds.procedures.TaskRegistryFactoryService;
import org.neo4j.gds.procedures.TerminationFlagAccessor;
import org.neo4j.gds.procedures.algorithms.configuration.ConfigurationCreator;
import org.neo4j.gds.procedures.algorithms.configuration.ConfigurationParser;
import org.neo4j.gds.services.DatabaseIdAccessor;
import org.neo4j.gds.services.UserAccessor;
import org.neo4j.gds.services.UserLogServices;
import org.neo4j.internal.kernel.api.exceptions.ProcedureException;
import org.neo4j.gds.termination.TerminationFlag;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.procedure.Context;

import java.util.Optional;
import java.util.function.Function;

class AlgorithmFacadeFactoryProvider {
// dull utilities
private final AlgorithmMetaDataSetterService algorithmMetaDataSetterService = new AlgorithmMetaDataSetterService();
private final DatabaseIdAccessor databaseIdAccessor = new DatabaseIdAccessor();
private final FictitiousGraphStoreEstimationService fictitiousGraphStoreEstimationService = new FictitiousGraphStoreEstimationService();
private final KernelTransactionAccessor kernelTransactionAccessor = new KernelTransactionAccessor();
private final TerminationFlagAccessor terminationFlagAccessor = new TerminationFlagAccessor();
private final UserAccessor userAccessor = new UserAccessor();

// Global state and services
private final Log log;
private final ConfigurationParser configurationParser;
private final DefaultsConfiguration defaultsConfiguration;
private final GraphStoreCatalogService graphStoreCatalogService;
private final LimitsConfiguration limitsConfiguration;
private final MemoryGauge memoryGauge;
private final boolean useMaxMemoryEstimation;

// Request scoped state and services
private final AlgorithmMetricsService algorithmMetricsService;
private final ExporterBuildersProviderService exporterBuildersProviderService;
private final TaskRegistryFactoryService taskRegistryFactoryService;
private final UserLogServices userLogServices;
private final ModelCatalogServiceProvider modelCatalogServiceProvider;
private final Optional<Function<AlgorithmProcessingTemplate, AlgorithmProcessingTemplate>> algorithmProcessingTemplateDecorator;
//algorithm facade parameters

AlgorithmFacadeFactoryProvider(
Log log,
ConfigurationParser configurationParser,
DefaultsConfiguration defaultsConfiguration,
GraphStoreCatalogService graphStoreCatalogService,
LimitsConfiguration limitsConfiguration,
MemoryGauge memoryGauge,
boolean useMaxMemoryEstimation,
AlgorithmMetricsService algorithmMetricsService,
ExporterBuildersProviderService exporterBuildersProviderService,
ModelCatalogServiceProvider modelCatalogServiceProvider,
TaskRegistryFactoryService taskRegistryFactoryService,
UserLogServices userLogServices,
Optional<Function<AlgorithmProcessingTemplate, AlgorithmProcessingTemplate>> algorithmProcessingTemplateDecorator
ModelCatalogServiceProvider modelCatalogServiceProvider
) {
this.log = log;
this.configurationParser = configurationParser;
this.defaultsConfiguration = defaultsConfiguration;
this.graphStoreCatalogService = graphStoreCatalogService;
this.limitsConfiguration = limitsConfiguration;
this.memoryGauge = memoryGauge;
this.useMaxMemoryEstimation = useMaxMemoryEstimation;

this.exporterBuildersProviderService = exporterBuildersProviderService;
this.taskRegistryFactoryService = taskRegistryFactoryService;
this.algorithmMetricsService = algorithmMetricsService;
this.userLogServices = userLogServices;
this.algorithmProcessingTemplateDecorator = algorithmProcessingTemplateDecorator;
this.modelCatalogServiceProvider = modelCatalogServiceProvider;
}


AlgorithmFacadeFactory createAlgorithmFacadeFactory(Context context) throws ProcedureException {
// Neo4j's services
var graphDatabaseService = context.graphDatabaseAPI();
var kernelTransaction = kernelTransactionAccessor.getKernelTransaction(context);

AlgorithmFacadeFactory createAlgorithmFacadeFactory(
Context context,
NodePropertyExporterBuilder nodePropertyExporterBuilder,
RelationshipExporterBuilder relationshipExporterBuilder,
TaskRegistryFactory taskRegistryFactory,
UserLogRegistryFactory userLogRegistryFactory,
TerminationFlag terminationFlag,
DatabaseId databaseId,
User user,
KernelTransaction kernelTransaction,
GraphDatabaseService graphDatabaseService,
DatabaseGraphStoreEstimationService databaseGraphStoreEstimationService
) {
/*
* GDS services derived from Procedure Context.
* These come in layers, we can create some services readily,
Expand All @@ -131,32 +111,14 @@ AlgorithmFacadeFactory createAlgorithmFacadeFactory(Context context) throws Proc
var algorithmMetaDataSetter = algorithmMetaDataSetterService.getAlgorithmMetaDataSetter(kernelTransaction);
var algorithmMemoryValidationService = new AlgorithmMemoryValidationService(log, useMaxMemoryEstimation);
var closeableResourceRegistry = new TransactionCloseableResourceRegistry(kernelTransaction);
var databaseId = databaseIdAccessor.getDatabaseId(context.graphDatabaseAPI());
var exportBuildersProvider = exporterBuildersProviderService.identifyExportBuildersProvider(graphDatabaseService);
var exporterContext = new ExporterContext.ProcedureContextWrapper(context);
var memoryGuard = new DefaultMemoryGuard(log, useMaxMemoryEstimation, memoryGauge);
var mutateNodePropertyService = new MutateNodePropertyService(log);
var mutateRelationshipService = new MutateRelationshipService(log);
var nodeLookup = new TransactionNodeLookup(kernelTransaction);
var returnColumns = new ProcedureCallContextReturnColumns(context.procedureCallContext());
var terminationFlag = terminationFlagAccessor.createTerminationFlag(kernelTransaction);
var user = userAccessor.getUser(context.securityContext());
var taskRegistryFactory = taskRegistryFactoryService.getTaskRegistryFactory(databaseId, user);
var userLogRegistryFactory = userLogServices.getUserLogRegistryFactory(databaseId, user);

// Second layer
var configurationCreator = new ConfigurationCreator(configurationParser, algorithmMetaDataSetter, user);
var graphLoaderContext = GraphLoaderContextProvider.buildGraphLoaderContext(
context,
databaseId,
taskRegistryFactory,
terminationFlag,
userLogRegistryFactory,
log
);
var nodePropertyExporterBuilder = exportBuildersProvider.nodePropertyExporterBuilder(exporterContext);
var relationshipExporterBuilder = exportBuildersProvider.relationshipExporterBuilder(exporterContext);
var relationshipStreamExporterBuilder = exportBuildersProvider.relationshipStreamExporterBuilder(exporterContext);

var requestScopedDependencies = RequestScopedDependencies.builder()
.with(databaseId)
.with(user)
Expand All @@ -176,7 +138,6 @@ AlgorithmFacadeFactory createAlgorithmFacadeFactory(Context context) throws Proc
taskRegistryFactory,
terminationFlag
);
var databaseGraphStoreEstimationService = new DatabaseGraphStoreEstimationService(graphLoaderContext, user);

// Fourth layer
var algorithmEstimator = new AlgorithmEstimator(
Expand All @@ -196,30 +157,8 @@ AlgorithmFacadeFactory createAlgorithmFacadeFactory(Context context) throws Proc
userLogRegistryFactory
);

// business facades
AlgorithmProcessingTemplate algorithmProcessingTemplate = new DefaultAlgorithmProcessingTemplate(
log,
algorithmMetricsService,
graphStoreCatalogService,
memoryGuard,
databaseId,
user
);

if (this.algorithmProcessingTemplateDecorator.isPresent())
algorithmProcessingTemplate = this.algorithmProcessingTemplateDecorator.get().apply(
algorithmProcessingTemplate);

var algorithmEstimationTemplate = new AlgorithmEstimationTemplate(
graphStoreCatalogService,
databaseId,
databaseGraphStoreEstimationService,
user
);

// procedure facade
return new AlgorithmFacadeFactory(
log,
defaultsConfiguration,
limitsConfiguration,
closeableResourceRegistry,
Expand All @@ -233,15 +172,7 @@ AlgorithmFacadeFactory createAlgorithmFacadeFactory(Context context) throws Proc
writeRelationshipService,
algorithmRunner,
algorithmEstimator,
algorithmProcessingTemplate,
algorithmEstimationTemplate,
nodePropertyExporterBuilder,
relationshipExporterBuilder,
relationshipStreamExporterBuilder,
taskRegistryFactory,
terminationFlag,
user,
userLogRegistryFactory,
modelCatalogServiceProvider.createService(graphDatabaseService, log)
);
}
Expand Down
Loading

0 comments on commit 9e78618

Please sign in to comment.