Skip to content

Commit 41bf6ad

Browse files
Add AGENTS.md for query-container
Signed-off-by: Aman Singh <[email protected]>
1 parent 94afc43 commit 41bf6ad

File tree

10 files changed

+196
-0
lines changed

10 files changed

+196
-0
lines changed

query-container/AGENTS.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# AGENTS.md: `query-container`
2+
3+
## 1. Purpose & Architectural Intent
4+
5+
This directory contains the **Query Container**, a set of Rust microservices providing the core runtime environment for executing Drasi's continuous queries.
6+
7+
**Core Intent**: To provide a scalable, stateful, and resilient host for the `drasi-core` query engine. It manages the complete lifecycle of a continuous query, from data ingestion and processing to publishing results and maintaining a queryable view.
8+
9+
**Key Technologies**:
10+
- **Rust**: For performance and reliability.
11+
- **Dapr**: Used for the actor model, which provides stateful, addressable instances for each query.
12+
- **Redis Streams**: The primary mechanism for ordered, reliable data ingestion into the query engine.
13+
- **MongoDB**: The persistent backend for the `view-svc` to store and serve materialized query results.
14+
15+
## 2. Component Interaction Diagram
16+
17+
```mermaid
18+
graph TD
19+
subgraph "Data Ingestion"
20+
A[Drasi Source's Change Dispatcher] -->|HTTP POST| B["publish-api"];
21+
B -->|Writes to Stream| C[Change Queue Redis Stream];
22+
end
23+
24+
subgraph "Core Query Processing"
25+
C -->|Reads from Change Queue Stream| D["query-host"];
26+
D -- "Hosts Engine" --> E["drasi-core"];
27+
E -- "Publishes Results" --> F[Dapr Pub/Sub];
28+
end
29+
30+
subgraph "Result Viewing"
31+
F -->|Subscribes to Topic| G["view-svc"];
32+
G -- "Persists View" --> H[MongoDB];
33+
I[Client Application] -->|HTTP GET| G;
34+
end
35+
```
36+
37+
## 3. Component Directory Guide
38+
39+
- **`publish-api/`**: A stateless service providing an HTTP endpoint for data ingestion. It receives data from Drasi Sources (`Change Dispatcher` component) and publishes it to a Redis Stream for consumption by the `query-host`.
40+
41+
- **`query-host/`**: A Dapr-enabled service that hosts and executes continuous queries. It uses the Dapr actor model to manage the lifecycle of each query as a stateful, addressable entity. It consumes data from Redis, processes it using the embedded `drasi-core` engine, and publishes results.
42+
43+
- **`view-svc/`**: A service that materializes and serves the results of continuous queries. It subscribes to result topics from the `query-host`, stores the data in MongoDB using a temporal model, and exposes an HTTP API for clients to query the current or historical state of a view.
44+
45+
## 4. Build and Test
46+
47+
The top-level `Makefile` orchestrates the build for all services in this workspace. It delegates targets (`docker-build`, `test`, `lint-check`) to the `Makefile` in each subdirectory.

query-container/CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AGENTS.md
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# AGENTS.md
2+
3+
Publish-API is an `axum` web-service that receives data via HTTP and publishes it to a Redis stream. It is invoked by `Change Dispatcher` component of Drasi `Source`s.
4+
5+
- **Configuration**: Reads `QUERY_NODE_ID`, `REDIS_BROKER`, and `PORT` from environment variables. It will exit if `QUERY_NODE_ID` is not set.
6+
7+
- **Publisher Initialization**: Creates a `Publisher` instance that connects to the specified Redis broker and publishes to a topic derived from the `QUERY_NODE_ID` (e.g., `{QUERY_NODE_ID}-publish`).
8+
9+
- **HTTP Endpoints**:
10+
- `/change`: Handles incoming change events.
11+
- `/data`: Handles incoming data.
12+
13+
- **Publishing**: Publishes messages containing the incoming data, along with some tracing metadata like, to the configured Redis stream using the `XADD` command.
14+
15+
## Mermaid Diagram
16+
17+
```mermaid
18+
sequenceDiagram
19+
participant Client
20+
participant Publish API (axum)
21+
participant Publisher
22+
participant Redis
23+
24+
Client->>Publish API (axum): POST /change (body, headers)
25+
Publish API (axum)->>Publisher: publish(body, trace_headers)
26+
Publisher->>Redis: XADD topic * data <body_string> + <metadata>
27+
Redis-->>Publisher: OK
28+
Publisher-->>Publish API (axum): Ok(())
29+
Publish API (axum)-->>Client: 200 OK
30+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AGENTS.md
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# AGENTS.md: `query-host`
2+
3+
## 1. Purpose & Architectural Intent
4+
5+
This directory contains the `query-host` service, the central component of the `query-container`.
6+
7+
**Core Intent**: To act as a robust, Dapr-enabled host for the `drasi-core` continuous query engine. It is responsible for managing the complete lifecycle of individual queries, making each query a stateful, addressable, and resilient entity.
8+
9+
**Architectural Strategy**:
10+
- **Dapr Actor Model**: Each continuous query is instantiated as a Dapr `QueryActor`. This pattern is used to:
11+
- Manage the state of each query independently (e.g., its configuration, status).
12+
- Provide a reliable, addressable endpoint for managing the query's lifecycle (configure, deprovision, reconcile).
13+
- Ensure resilience, as Dapr can automatically reactivate actors on failure.
14+
- **Decoupled Worker Logic**: The `QueryActor` spawns a `QueryWorker` which contains the actual data processing logic. This separates the actor's lifecycle management from the core task of running the query.
15+
16+
## 2. Core Dependencies & Data Flow
17+
18+
- **`drasi-core` (Git Submodule)**: This is the high-performance, embeddable Rust library that performs the actual continuous query evaluation. The `query-host` acts as a runtime environment for it.
19+
- **Redis Streams**: The `QueryWorker` consumes a dedicated Redis Stream for each query, ensuring ordered, reliable processing of incoming `ChangeEvent` messages. `publish-api` service publushes the Change Events to the Redis stream.
20+
- **Dapr Pub/Sub**: After processing data through `drasi-core`, the `QueryWorker` publishes the resulting diffs to a separate Dapr pub/sub topic for consumption by downstream services like the `view-svc`.
21+
22+
```mermaid
23+
graph TD
24+
subgraph Dapr Sidecar
25+
A[Dapr Runtime]
26+
end
27+
28+
subgraph Query Host Container
29+
B[DaprHttpServer]
30+
C[QueryActor]
31+
D[QueryWorker]
32+
E[drasi-core Engine]
33+
end
34+
35+
F[Input Redis Stream from Publish API]
36+
G[Output Dapr Pub/Sub]
37+
H[Query API component of Source]
38+
39+
A <--> B;
40+
B -- "Manages Lifecycle" --> C;
41+
C -- "Spawns & Supervises" --> D;
42+
D -- "Uses" --> E;
43+
D -- "Consumes From" --> F;
44+
E -- "Publishes To" --> G;
45+
D -- "Bootstraps From" --> H;
46+
```
47+
48+
```mermaid
49+
graph TD
50+
subgraph Dapr Actor Runtime
51+
A[QueryActor]
52+
end
53+
54+
subgraph QueryWorker Thread
55+
B[QueryWorker] -- Manages --> C{drasi-core Engine}
56+
D[RedisChangeStream] -- Feeds --> B
57+
C -- Publishes to --> E[ResultPublisher]
58+
C -- Schedules --> F[FutureQueue]
59+
end
60+
61+
G[Query API in Sources]
62+
H[Redis Stream]
63+
I[Pub/Sub Topic]
64+
65+
A -- Spawns/Manages --> B
66+
B -- Bootstraps from --> G
67+
D -- Reads from --> H
68+
E -- Publishes to --> I
69+
F -- Consumed by --> FutureConsumer -- Publishes back to --> H
70+
71+
```
72+
73+
## 3. Key Abstractions
74+
75+
- **`QueryActor`**: The Dapr actor responsible for state management and lifecycle of a single continuous query.
76+
- **`QueryWorker`**: The core processing loop for a query. It bootstraps data from sources, consumes the change stream, and drives the `drasi-core` engine.
77+
- **`IndexFactory`**: A factory for providing pluggable storage backends (`ElementIndex`, `ResultIndex`, `FutureQueue`) to `drasi-core`, allowing for different persistence strategies (in-memory, Redis, RocksDB).
78+
- **`MiddlewareTypeRegistry`**: Enables extensible, declarative data transformation pipelines for pre-processing source data before it reaches the query engine.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AGENTS.md
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# AGENTS.md: `query-container/query-host/src/change_stream`
2+
3+
Implements a sequential change stream consumer on top of Redis Streams ensuring that messages are processed in order and acknowledges them only after successful processing. Goal is to provide a reliable, ordered message consumption mechanism from a Redis stream, abstracting away the complexities of Redis Streams consumer groups, pending messages, and acknowledgements.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AGENTS.md

query-container/view-svc/AGENTS.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# AGENTS.md: `view-svc`
2+
3+
## 1. Purpose & Architectural Intent
4+
5+
View Service is responsible for creating, managing, and serving materialized views of Drasi continuous query results.
6+
7+
**Core Intent**: To provide a durable, queryable, and time-aware view of the output from a continuous query. It acts as the primary interface for client applications to consume query results.
8+
9+
**Architectural Strategy**:
10+
- **Dapr Actor Model**: Each view is managed by a Dapr `ViewActor`. This provides a stateful, addressable endpoint for each view's lifecycle (configure, deprovision) and encapsulates its state (e.g., retention policy).
11+
- **Decoupled Worker Logic**: The `ViewActor` spawns a `ViewWorker` that runs in the background, consuming the result stream for a specific query and updating the materialized view in the database. This decouples the actor's lifecycle from the data processing task.
12+
- **Temporal Data Model**: The `MongoViewStore` implements a temporal data model. Each result row is stored with `validFrom` and `validTo` timestamps. This allows clients to query the view not just for its current state, but for its state at any point in time.
13+
- **`RetentionPolicy`**: A key configuration that dictates how historical data is managed in the view (e.g., `Latest`, `Expire`, `All`). A background garbage collection task in the `MongoViewStore` enforces this policy.
14+
15+
## 2. Core Dependencies & Data Flow
16+
17+
- **Dapr Pub/Sub**: The `ViewWorker` subscribes to a Dapr pub/sub topic to receive `ResultEvent` streams from a `query-host`.
18+
- **MongoDB**: The `MongoViewStore` uses MongoDB as its persistence layer. It creates a separate collection for each view to store the temporal documents.
19+
- **Axum**: An Axum-based HTTP server exposes an API for clients to query the view.
20+
21+
```mermaid
22+
graph TD
23+
A[query-host] -- Publishes --> B[Dapr Pub/Sub];
24+
25+
subgraph view-svc
26+
C[ViewWorker] -- Subscribes to --> B;
27+
C -- "Writes to" --> D[MongoViewStore];
28+
D -- "Uses" --> E[MongoDB];
29+
F[Axum API] -- "Reads from" --> D;
30+
end
31+
32+
G[Client Application] -- "GET /:query_id" --> F;
33+
```

query-container/view-svc/CLAUDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
AGENTS.md

0 commit comments

Comments
 (0)