Skip to content

Latest commit

 

History

History

graph

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

rec-graph

rec-graph is a lightweight asynchronous DAG runtime. It has no recommendation or Spring dependencies: it compiles a graph definition, runs ready nodes concurrently, and exchanges data through a request-scoped context. Recommendation-specific nodes live in rec-server.

This module builds and runs with JDK 21 (--release 21), including when embedded outside rec-server.

Use the engine

Compile reusable graph metadata once, then create one engine per request:

NodeRegistry registry = NodeRegistry.builder()
    .register(new SimpleNodeFactory("recall", RecallNode::new))
    .build();
GraphPlan plan = GraphPlan.compile(graphConfig, registry);

GraphEngine engine = GraphEngine.getSessionGraphEngine();
engine.prepare(requestObject);       // declared fields become graph parameters
engine.execGraph(plan);
List<MyResult> result = engine.getResult();

GraphEngine is stateful and must not be shared between requests. GraphPlan is immutable and may be reused. The older buildGraph(...) followed by execGraph() API remains available.

destroy() clears only request-scoped state. Worker and timeout executors are shared by all engine instances and use daemon threads. JDK 21 virtual threads are the default for node execution.

Worker selection

Select the executor before the first GraphEngine use; changing it requires a process restart. JVM system properties override environment variables. This configuration also works without Spring; these are not Spring application.properties settings.

JVM property Environment variable Default
openrec.graph.executor OPENREC_GRAPH_EXECUTOR virtual (platform selects the previous fixed pool)
openrec.graph.worker.threads OPENREC_GRAPH_WORKER_THREADS max(8, availableProcessors × 4)
openrec.graph.worker.queue-capacity OPENREC_GRAPH_WORKER_QUEUE_CAPACITY 1024

Worker count and queue capacity apply only to platform. It retains a bounded queue and rejection on overload. virtual creates one named virtual thread per submitted node; it has no worker/queue limit of its own. Keep admission and downstream connection limits at the application boundary. rec-server retains its existing bounded HTTP blocking executor (32 threads and 256 queued tasks by default). Node deadlines, interruption, failure policies and output commit semantics are identical in both modes. Invalid modes and invalid platform pool sizes fail on engine initialization.

# Default; supported by both standalone and cluster Compose assemblies.
OPENREC_GRAPH_EXECUTOR=virtual docker compose -f docker-compose.standalone.yml up -d --build
# Roll back to platform workers; restart/recreate the application to apply.
OPENREC_GRAPH_EXECUTOR=platform OPENREC_GRAPH_WORKER_THREADS=32 \
  docker compose -f docker-compose.standalone.yml up -d
# Embedded / java -jar usage: place the JVM property before -jar.
java -Dopenrec.graph.executor=platform -jar server.jar

Virtual threads suit nodes waiting on Redis, Elasticsearch or HTTP. CPU-heavy nodes still execute normally, but virtual threads do not increase available CPU capacity. Both modes use the same DAG; there is no per-node executor selection.

Graph definition

{
  "nodes": [
    {
      "name": "hot",
      "type": "item.hot",
      "configClazz": "com.openrec.graph.config.HotConfig",
      "open": true,
      "timeout": 100,
      "content": {"size": 1000}
    }
  ],
  "edges": [
    {"from": "hot", "to": "combine"}
  ]
}
Field Purpose
name Unique node ID used by edges and the context config map
type Stable node type registered by the application
clazz Deprecated implementation-class alias for existing graphs
configClazz Type used to deserialize content; may be null
open Node-level feature switch interpreted by the node
timeout Execution timeout in milliseconds
content Node-specific configuration

GraphPlan.compile(...) validates registered node types and edges, resolves each node's factory, and precomputes dependency metadata. Unknown types and invalid edge references fail before request execution. In an acyclic definition, nodes with no incoming edges are roots. Cycle, duplicate-node, duplicate-edge, endpoint, timeout, node-factory, annotated-field, and typed data-contract validation all happen in GraphPlan.compile(...).

Write a node

Most server nodes extend AbstractSyncNode<C>:

public class HotNode extends AbstractSyncNode<HotConfig> {

    @Import("triggerItems")
    private List<ScoreResult> triggerItems;

    @Export("hotItems")
    private List<ScoreResult> hotItems = Lists.newArrayList();

    public HotNode(NodeConfig nodeConfig) {
        super(nodeConfig);
    }

    @Override
    public void run(GraphContext context) {
        if (!config.isOpen()) {
            return;
        }
        String scene = context.getParams().getValueToString("scene");
        // Populate hotItems.
    }
}

Nodes are created by their registered factory. In rec-server, the registry uses explicit constructors and Spring autowiring, so node dependencies use normal @Autowired injection. Legacy graphs containing clazz remain supported through registered class-name aliases.

Data exchange

The field annotation API is the primary concise contract for server nodes. Before a node runs, fields annotated with @Import("key") are populated from GraphContext. After it finishes, @Export("key") fields are published. GraphPlan.compile(...) reflects these fields once and validates that every required import has a reachable upstream export with a compatible Java type, including generic arguments such as List<ScoreResult>. Duplicate or blank ports are rejected. The compiled contract is reused for request execution, so fields are not rescanned for every node invocation.

Imports are required by default. Use @Import(value = "key", required = false) only when a node explicitly handles a missing value. A missing required value at runtime fails that node instead of silently injecting null.

TypedNode remains available for nodes that benefit from immutable NodeInput/NodeOutput snapshots. Such nodes declare DataKey<T> inputs and outputs, and the engine commits their complete output only after the node reaches SUCCESS.

Nodes may also call context.addData(key, value) and context.getData(key) for keys chosen at runtime. rec-server uses this for configurable recall channels while retaining fixed annotation exports for backward compatibility. A later write to the same key replaces the earlier value.

Initialize exported collections when an open node can legitimately produce no values. A failed, skipped, or timed-out producer does not publish output, so a required downstream import fails explicitly according to the graph's failure policy.

Request parameters and result

prepare(object) reflects over the object's declared fields and stores each value under its field name. Parameters can also be added explicitly:

engine.addParam("scene", "home");
String scene = context.getParams().getValueToString("scene");

The terminal node calls context.setResult(value); the caller retrieves it with engine.getResult().

Execution and timeout behavior

The runtime executes one dependency level at a time. All ready nodes in a level run concurrently, then the next level starts after the batch completes. Every node has a timeout and the whole request may have an earlier deadline. A timed-out or cancelled invocation is interrupted and its late output is never committed. failurePolicy controls whether failure continues, skips descendants, or fails the graph.

execGraph(plan, deadlineMillis, nodeTimeoutFloorMillis) provides a per-execution node timeout floor for warmup without mutating the shared plan. Ordinary serving calls retain the configured node deadlines. setWarmup(true) propagates an internal flag to node execution contexts; the serving collectors use it to suppress synthetic exposure writes during readiness probes.

Execution trace

Callers may supply a GraphTraceContext and GraphTraceObserver when creating an engine. The observer receives one immutable GraphExecutionTrace when execution finishes, including request, target, scene, experiment and graph-version metadata. Every node entry contains its name, type, terminal status, queue and execution durations, input/output counts, and a sanitized failure class.

GraphEngine engine = GraphEngine.getSessionGraphEngine(traceContext, observer);
engine.execGraph(plan, 1000L);
GraphExecutionTrace trace = engine.getTrace();

Trace context is passed explicitly instead of relying on thread-local state, so it remains correct when WebFlux dispatches work to the blocking and graph worker pools.

Test

mvn -pl graph test
mvn -pl graph test -Dopenrec.graph.executor=platform

The graph module tests are self-contained and require no external services.