Graph engines¶
Queries are executed behind the GraphDB interface. The implementation is selected by Spring profile.
classDiagram
class GraphDB {
<<interface>>
delete(instanceId, space, stage)
upsert(stage, instance, linkingInstances)
reindex(stage, source) ReindexResult
executeQuery(query, stage, params, page, accessFilter, instanceId) GraphQueryResult
}
class Neo4J {
<<profile neo4j>>
}
class NoGraph {
<<profile nograph>>
}
class Neo4JInternal
class Neo4JReindexInternal
class QueryToCypherInternal
class Neo4JPayloadSplitterInternal
class QueryEvaluatorInternal
class PostgresGraphStoreInternal
class GraphVocabulary
GraphDB <|.. Neo4J
GraphDB <|.. NoGraph
Neo4J --> Neo4JInternal
Neo4J --> Neo4JReindexInternal
Neo4J --> QueryToCypherInternal
Neo4JInternal --> Neo4JPayloadSplitterInternal
Neo4JReindexInternal --> Neo4JPayloadSplitterInternal
Neo4J --> GraphVocabulary
NoGraph --> QueryEvaluatorInternal
QueryEvaluatorInternal --> PostgresGraphStoreInternal
| Profile | Writes | Queries |
|---|---|---|
nograph (default) |
No-ops | Evaluated in PostgreSQL |
neo4j |
Instances are mirrored to Neo4j | Translated to Cypher |
The Arango adapter of earlier versions doesn't exist in the current code.
nograph¶
QueryEvaluatorInternal evaluates queries on the primary store (ADR-037):
- SQL selects, traverses and restricts access.
PostgresGraphStoreInternalfinds the root instances, resolves references and finds linking instances through the relation tables. - Java reads and filters the values, since payloads are compressed. Each loaded payload is split into nodes, with
embedded objects as virtual nodes
<uuid>#<n>. - Root instances are processed in batches: all instances needed are loaded hop by hop, then each root is evaluated in memory.
The cost grows with the number of root candidates, not the result size. Filters on the root level and totals with such filters read every instance of the root type.
neo4j¶
| Class | Responsibility |
|---|---|
Neo4JStartup |
Creates the indexes on startup |
Neo4JPayloadSplitterInternal |
Splits a payload into nodes and edges |
Neo4JInternal |
Upserts and deletes single instances |
Neo4JReindexInternal |
Bulk rebuild of a stage |
Neo4JStatements |
Cypher statements shared by upsert and reindex |
QueryToCypherInternal |
Translates QuerySpecification to Cypher |
Vocabulary compaction¶
GraphVocabulary replaces namespaces with short prefixes before anything reaches the graph, because IRIs aren't valid
Neo4j labels or property names. https://schema.org/name becomes sch__name. Prefixes are stored in curie_prefix
and only ever added, and several application instances converge on the same mapping (ADR-016).
Synchronization¶
flowchart LR
subgraph tx["Transaction of the manipulation"]
M["Manipulation"] --> E["InstancesChangedEvent /<br/>InstanceRemovedEvent"]
end
E -- "after commit" --> L["GraphIndexingListener"]
L -- "reads current state" --> S["GraphSourceInternal"]
L --> G["GraphDB.upsert / delete"]
GraphIndexingListener only exists outside the nograph profile. It runs synchronously in the request thread after
the commit (ADR-017):
- The graph never contains changes that are rolled back.
- Query results reflect a change as soon as the write returns.
- A failure in the graph doesn't fail the write. It's logged, and the graph stays out of sync until the instance is written again or a reindex runs.
During an upsert, edges from other instances are preserved. Only if the instance is new to the stage are the linking instances loaded, to create edges that couldn't exist earlier.
Reindexing and locks¶
GraphReindexService rebuilds a stage in two passes, all nodes before any edge, using batches (ADR-013).
sequenceDiagram
participant Admin
participant Svc as GraphReindexService
participant Lock as ReindexLock
participant Graph as GraphDB
participant Write as Manipulation
Admin->>Svc: POST /graph/reindex
Svc-->>Admin: 202 Accepted
Svc->>Lock: write lock (waits for running manipulations)
Write->>Lock: @ReindexLock.Manipulation
Lock-->>Write: rejected, 503
Svc->>Graph: reindex(stage, source)
Graph-->>Svc: ReindexResult
Svc->>Lock: release
ReindexLock is an in-process lock, so it only protects a single application instance. A deployment with several
instances would need a distributed lock.
ManipulationThrottle limits concurrent writes to half of the connection pool, since each write needs one connection
for its transaction and one for the after-commit graph synchronization (ADR-018).
Adding a graph engine¶
- Add a package under
graph/with a class implementingGraphDB, and a@Profileconfiguration. - Evaluate queries with the semantics of
QueryEvaluatorInternaland apply the access filter on every hop. - Extend the shared query scenarios in
NoGraphIntegrationTestandQueryServiceNeo4jIntegrationTestto the new engine. - Record the decision in an ADR.