Repository navigation
Split batcher and ipfs publisher #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
02af268
feat: move batching logic into dedicated service
akru 92088ce
feat: added shutdown test
akru eeff298
Clarify blockchain-anchor functionality and event sources
akru c5e3ef0
Enhance flusher error handling logic
akru 60376d3
fix(batcher,ipfs-publisher): address PR review comments on offset safety
akru 474cc50
Adjust source_offset calculation in index.ts
akru c581cfd
Update metrics consumption logic on first attempt
akru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # `@scp/batcher` | ||
|
|
||
| Groups authorized telemetry into deterministic batches and emits them for | ||
| downstream publication. | ||
|
|
||
| ## Architecture | ||
|
|
||
| - **Pattern**: Standard consumer with manual commit | ||
| - **Input**: `telemetry.authorized.v1` from Kafka | ||
| - **Output**: `telemetry.batched.v1` (carries a serialized `SignedEnvelopeBatch`) | ||
| - **Autocommit**: Disabled (commit only after the batch is durably produced) | ||
| - **Concurrency**: Single active flush per instance (single-flight); a timer-triggered flush cannot publish the same batch as a size/lag-triggered flush | ||
| - **Shutdown**: Waits for any in-flight flush and flushes the remaining batch before closing resources | ||
|
|
||
| Separating batching from IPFS publishing keeps the publisher stateless and | ||
| lets each side scale and fail independently. The batcher owns buffering, | ||
| flush timing, and offset commits for the authorized topic; the | ||
| `@scp/ipfs-publisher` owns durable IPFS publication of `telemetry.batched.v1`. | ||
|
|
||
| ## Batching Strategy | ||
|
|
||
| - **Size-based flush**: Flushes when `BATCHER_BATCH_SIZE` messages are accumulated | ||
| - **Lag-based flush**: When consumer lag is high (>= batch size), flushes early to catch up | ||
| - **Time-based flush**: When lag is low, a bounded timer (`BATCHER_BATCH_TIMEOUT_MS`) flushes partial batches | ||
| - **Shutdown flush**: Any pending batch is flushed during graceful shutdown to avoid replay | ||
|
|
||
| The current batch is atomically detached before any asynchronous work, so | ||
| concurrent flush paths can never publish the same batch twice. | ||
|
|
||
| ## Environment | ||
|
|
||
| - `KAFKA_BROKERS` (default: `localhost:9092`) | ||
| - `BATCHER_GROUP_ID` (default: `batcher-v1`) | ||
| - `BATCHER_SOURCE` (default: `batcher`) | ||
| - `BATCHER_HEALTH_PORT` (default: `3041`) | ||
| - `BATCHER_BATCH_SIZE` (default: `10`) - Maximum messages per batch | ||
| - `BATCHER_BATCH_TIMEOUT_MS` (default: `30000`) - Timeout for partial batches under low load | ||
|
|
||
| ## Metrics | ||
|
|
||
| Available at `http://localhost:3041/metrics`: | ||
|
|
||
| - `consumed`: Total authorized telemetry messages consumed | ||
| - `batchesProduced`: Total batches produced to `telemetry.batched.v1` | ||
| - `eventsBatched`: Total individual telemetry events batched | ||
| - `produceFailure`: Failed batch produce attempts (re-attached for retry) | ||
|
|
||
| ## Data Flow | ||
|
|
||
| 1. Consume `telemetry.authorized.v1` event from Kafka | ||
| 2. Extract `SignedEnvelope` from payload and add to the current batch | ||
| 3. When the batch is full, lag is high, or the flush timer fires: | ||
| - Serialize the batch as a `SignedEnvelopeBatch` protobuf | ||
| - Wrap it in a `TelemetryBatchedPayload` (`batch_id`, `event_count`, `sensor_ids`) | ||
| - Produce a `telemetry.batched.v1` envelope to Kafka | ||
| - Commit `telemetry.authorized.v1` offsets (only after the batch is produced) | ||
|
|
||
| ## Result Event Schema | ||
|
|
||
| Published to `telemetry.batched.v1`: | ||
|
|
||
| ```typescript | ||
| { | ||
| batchId: string; // Unique batch identifier | ||
| signedEnvelopeBatch: Uint8Array; // Serialized crypto.v1.SignedEnvelopeBatch | ||
| eventCount: number; // Number of telemetry events in batch | ||
| sensorIds: Uint8Array[]; // Sensor IDs present in the batch (observability) | ||
| } | ||
| ``` | ||
|
|
||
| ## Development | ||
|
|
||
| ```bash | ||
| # Start batcher | ||
| pnpm --filter @scp/batcher dev | ||
|
|
||
| # Build | ||
| pnpm --filter @scp/batcher build | ||
|
|
||
| # Run tests | ||
| pnpm --filter @scp/batcher test | ||
|
|
||
| # Type check | ||
| pnpm --filter @scp/batcher typecheck | ||
|
|
||
| # Lint | ||
| pnpm --filter @scp/batcher lint | ||
| ``` | ||
|
|
||
| ## Dependencies | ||
|
|
||
| - **Kafka**: For consuming authorized telemetry and producing batched events | ||
|
|
||
| ## Commit Safety | ||
|
|
||
| The batcher commits `telemetry.authorized.v1` offsets only after the batch is | ||
| durably produced to `telemetry.batched.v1`. If producing fails, the batch is | ||
| re-attached and retried on the next flush, and offsets stay uncommitted so no | ||
| telemetry is lost. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| { | ||
| "name": "@scp/batcher", | ||
| "version": "0.1.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "main": "dist/index.mjs", | ||
| "types": "dist/index.d.mts", | ||
| "scripts": { | ||
| "build": "tsdown", | ||
| "typecheck": "tsc -p tsconfig.json --noEmit", | ||
| "lint": "eslint \"src/**/*.ts\"", | ||
| "test": "vitest run", | ||
| "dev": "tsx watch --env-file=../../.env src/index.ts" | ||
| }, | ||
| "dependencies": { | ||
| "@buf/airalab_connectivity-protocol.bufbuild_es": "2.14.0-20260828134001-309d77314e7b.1", | ||
| "@platformatic/kafka": "^2.9.0", | ||
| "@scp/core": "workspace:*", | ||
| "pino": "^10.3.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@bufbuild/protobuf": "^2.14.0", | ||
| "tsdown": "^0.22.14", | ||
| "tsx": "^4.23.12" | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.