Skip to content

Commit 52e358d

Browse files
Updates for cascade & codec (#166)
* Add wait after transaction broadcast when processing Tx after broadcast * codec: simplify to fixed policy (linux/amd64) and prune docs - Fix policy: concurrency=4, symbol_size=65535, redundancy=5, headroom=10% of cgroup/system mem - Remove profiles/CPU/env logic and dead code from pkg/codec/raptorq.go - Update pkg/codec/codec.go comments to reflect fixed policy - Decode comment: note fixed policy - Trim pkg/codec/README.md accordingly - Simplify pkg/codecconfig to fixed policy; remove env/profile/CPU fields - supernode.proto: prune CodecConfig (reserve profile/effective_cores/cpu_limit_source) - Regenerate protobufs and swagger - Update status server and gateway to match trimmed CodecConfig; gateway returns minimal JSON * docs: document fixed-policy codec and /api/v1/codec endpoint * feat: enhance progressive symbol retrieval and decoding logic * fix: update fixed policy parameters for RaptorQ codec * feat: add comprehensive documentation for Cascade downloads and performance tuning * fix: increase download timeout and improve supernode ranking for downloads * feat : set block size to 128MB * feat: Add new supernode event types and enhance logging for upload/download processes * fix conflicts --------- Co-authored-by: Bilal Rafique <mejrafique@gmail.com>
1 parent 188a97b commit 52e358d

File tree

39 files changed

+1150
-1208
lines changed

39 files changed

+1150
-1208
lines changed

.github/actions/setup-env/action.yml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,3 @@ runs:
3838
shell: bash
3939
run: |
4040
go mod download
41-
42-
- name: Tidy Go modules
43-
shell: bash
44-
run: |
45-
# Ensure go.mod/go.sum are up to date to prevent CI build failures
46-
go mod tidy
47-
if [ -f sn-manager/go.mod ]; then
48-
(cd sn-manager && go mod tidy)
49-
fi

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ go.work.sum
2828
/release
2929
/tests/system/data
3030
tests/system/**/supernode-data*
31+
AGENTS.md

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ enum SupernodeEventType {
207207

208208
See docs/gateway.md for the full gateway guide (endpoints, examples, Swagger links).
209209

210+
### Codec Configuration (fixed policy)
211+
212+
The supernode uses a fixed RaptorQ codec policy (linux/amd64 only):
213+
- Concurrency: 4
214+
- Symbol size: 65535
215+
- Redundancy: 5
216+
- Max memory: detected cgroup/system memory minus 10% headroom
217+
218+
Status includes these effective values under `codec` in `StatusResponse`.
219+
The HTTP gateway also exposes a minimal view at `GET /api/v1/codec` with:
220+
- `symbol_size`, `redundancy`, `max_memory_mb`, `concurrency`, `headroom_pct`, `mem_limit_mb`, `mem_limit_source`.
221+
210222
## CLI Commands
211223

212224
### Core Commands

docs/cascade-performance.md

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
# Cascade Downloads & Performance: Concepts, Limits, and Tuning
2+
3+
This document explains how Cascade encoding/decoding works, the performance and memory factors involved, and practical configuration guidance. It consolidates the “blocks and symbols” primer and expands it with deeper operational tuning, error references, and code pointers — in a concise, professional format.
4+
5+
## Overview
6+
7+
- Cascade uses RaptorQ forward error correction to split a file into blocks and symbols that can be stored/fetched from a P2P network.
8+
- Decoding requires enough symbols to reconstruct each block; integrity is verified with hashes recorded in the layout.
9+
- Performance and reliability are driven by four main levers: block size, redundancy, concurrency, and memory headroom. Batching and ordering in the store path, and supernode selection in the download path, also matter.
10+
11+
## Current Defaults (Implementation)
12+
13+
- RaptorQ (codec)
14+
- Block cap: 256 MB (encode‑time upper bound per block)
15+
- Decode concurrency: 1
16+
- Memory headroom: 20% of detected RAM
17+
- Symbol size: ~65,535 bytes
18+
- Redundancy: 5
19+
20+
- Store path (foreground adaptor)
21+
- Batch size: 2,500 files per batch (≈156 MiB typical at default symbol size)
22+
- Downsampling: if total files > 2,500, take 10% sorted prefix for initial store
23+
- Per‑batch P2P store timeout: 5 minutes
24+
25+
- Store path (background worker)
26+
- Batch size: 1,000 files per batch (≈62.5 MiB typical)
27+
28+
- Download path
29+
- SDK per‑supernode download deadline: 10 minutes
30+
- Supernode ranking: status probe ~2 seconds per node; sorted by available memory (desc)
31+
- P2P exec timeouts (per RPC):
32+
- FindValue: 5s
33+
- BatchFindValues: 60s
34+
- BatchGetValues: 75s
35+
- StoreData: 10s
36+
- BatchStoreData: 75s
37+
- Replicate: 90s
38+
39+
- Upload constraints
40+
- Max file size: 1 GB (enforced in SDK and server)
41+
- Adaptive upload chunk size: ~64 KB → 4 MB based on file size
42+
43+
## Core Concepts
44+
45+
- Block: A contiguous segment of the original file. Think of it as a “chapter”.
46+
- Symbol: A small piece produced by RaptorQ for a block. You only need “enough” symbols to reconstruct the block.
47+
- Layout: Metadata that lists all blocks (block_id, size, original offset, per‑block hash) and the symbol IDs belonging to each block.
48+
49+
Encode (upload):
50+
- Choose a block size; RaptorQ creates symbols per block; symbols + layout are stored.
51+
52+
Decode (download):
53+
- Fetch symbols from the network; reconstruct each block independently; write each block back at its original offset; verify hashes; stream the file.
54+
55+
Key facts:
56+
- Symbols never mix across blocks.
57+
- Peak memory during decode scales roughly with the chosen block size (plus overhead).
58+
59+
## File Size Limits & Upload Chunking
60+
61+
- Maximum file size: 1 GB (enforced both in SDK and server handlers).
62+
- Adaptive upload chunk size: ~64 KB → 4 MB depending on total file size for throughput vs memory stability.
63+
64+
## Encoding/Decoding Workflow (high level)
65+
66+
1) SDK uploads file to a supernode (gRPC stream). Server writes to a temporary file, validates size and integrity.
67+
2) Server encodes with RaptorQ: produces a symbols directory and a layout JSON.
68+
3) Server stores artefacts: layout/ID files and symbols into P2P in batches.
69+
4) Later, SDK requests download; supernode fetches symbols progressively and decodes to reconstruct the file; integrity is verified.
70+
71+
## Contexts & Timeouts (download path)
72+
73+
- SDK: wraps the download RPC with a 10‑minute deadline.
74+
- Server: uses that context; P2P layer applies per‑RPC timeouts (e.g., 5s for single key FindValue, ~75s for BatchGetValues), with internal early cancellation once enough symbols are found.
75+
- RaptorQ: uses the same context for logging; no additional deadline inside decode.
76+
77+
## Memory Model
78+
79+
- Decoder memory is primarily a function of block size and concurrency.
80+
- Headroom percentage reduces the usable memory budget to leave safety buffer for the OS and other processes.
81+
- Example formula: usable_memory ≈ TotalRAM × (1 − headroom%).
82+
83+
## Configuration Levers
84+
85+
The implementation uses simple fixed constants for safety and predictability. You can adjust them and rebuild.
86+
87+
1) Block Size Cap (`targetBlockMB`, encode‑time)
88+
- What: Upper bound on block size. Actual used size = min(recommended_by_codec, cap).
89+
- Effect: Smaller cap lowers peak decode memory (more blocks, more symbols/keys). Larger cap reduces block count (faster on big machines) but raises peak memory.
90+
- Current default: 256 MB (good balance on well-provisioned machines). Only affects newly encoded artefacts.
91+
92+
2) Redundancy (`defaultRedundancy`, encode‑time)
93+
- What: Extra protection (more symbols) to tolerate missing data.
94+
- Effect: Higher redundancy improves recoverability but costs more storage and network I/O. Does not materially change peak memory.
95+
- Current default: 5 (good real‑world trade‑off).
96+
97+
3) Concurrency (`fixedConcurrency`, decode‑time)
98+
- What: Number of RaptorQ decode workers.
99+
- Effect: Higher is faster but multiplies memory; lower is safer and predictable.
100+
- Current default: 1 (safe default for wide environments).
101+
102+
4) Headroom (`headroomPct`, decode‑time)
103+
- What: Percentage of detected RAM left unused by the RaptorQ processor.
104+
- Effect: More headroom = safer under load; less headroom = more memory available to decode.
105+
- Current default: 20% (conservative and robust for shared hosts).
106+
107+
## Batching Strategy (store path)
108+
109+
Why batching matters:
110+
- Store batches are loaded wholly into memory before sending to P2P.
111+
- A fixed “files‑per‑batch” limit gives variable memory usage because symbol files can differ slightly in size.
112+
113+
Current defaults:
114+
- Foreground adaptor: `loadSymbolsBatchSize = 2500` → ≈ 2,500 × 65,535 B ≈ 156 MiB per batch (typical).
115+
- Background worker: `loadSymbolsBatchSize = 1000` → ≈ 62.5 MiB per batch.
116+
117+
Byte‑budget alternative (conceptual, not implemented):
118+
- Cap the total bytes per batch (e.g., 128–256 MiB), with a secondary cap on file count.
119+
- Benefits: predictable peak memory; better throughput on small symbols; avoids spikes on larger ones.
120+
121+
## Ordering for Throughput (store path)
122+
123+
- We sort relative file paths before batching (e.g., `block_0/...`, `block_1/...`) to improve filesystem locality and reduce disk seeks. This favors speed.
124+
- Trade‑off: If a process stops mid‑way, earlier blocks (lexicographically smaller) are more likely stored than later ones. For fairness across blocks at partial completion, interleaving could be used at some CPU cost.
125+
126+
## Supernode Selection (download path)
127+
128+
- The SDK ranks supernodes by available memory (fast 2s status probe per node) and attempts downloads in that order.
129+
- This increases the chances of successful decode for large files.
130+
131+
## Defaults & Suggested Settings
132+
133+
1 GB files (general)
134+
- Block cap: 256 MB (≈4 blocks)
135+
- Concurrency: 1
136+
- Headroom: 20%
137+
- Redundancy: 5
138+
139+
Large‑memory machines (performance‑leaning)
140+
- Block cap: 256 MB (or 512 MB) to reduce block count and increase throughput.
141+
- Concurrency: 1–2.
142+
- Headroom: 15–20% depending on other workloads.
143+
- Redundancy: 5 (or 6 in sparse networks).
144+
145+
Small‑memory machines
146+
- Block cap: 64–128 MB
147+
- Concurrency: 1
148+
- Headroom: 20%
149+
- Redundancy: 5
150+
151+
## Error Reference
152+
153+
- memory limit exceeded
154+
- The decoder exceeded its memory budget. Reduce block size or concurrency, increase RAM, or lower headroom.
155+
156+
- hash mismatch for block X
157+
- Data reconstructed for the block did not match the expected hash. Often indicates wrong/corrupt symbols; can also occur when decoding fails mid‑way under memory pressure. Re‑fetching or re‑encoding may be required.
158+
159+
- insufficient symbols
160+
- Not enough valid symbols were available; the retriever will fetch more.
161+
162+
- gRPC Internal on download stream
163+
- The supernode returned an error during decode (e.g., memory failure). The SDK will try the next supernode.
164+
165+
## Code Pointers
166+
167+
- Block cap, headroom, concurrency (RaptorQ): `pkg/codec/raptorq.go`
168+
- Store batching (foreground path): `supernode/services/cascade/adaptors/p2p.go`
169+
- Store batching (background worker): `p2p/kademlia/rq_symbols.go`
170+
- Batch symbol loading / deletion: `pkg/utils/utils.go` (LoadSymbols, DeleteSymbols)
171+
- Supernode ranking by memory (download): `sdk/task/download.go`
172+
- File size cap & adaptive upload chunking: SDK and server sides (`sdk/adapters/supernodeservice/adapter.go`, `supernode/node/action/server/cascade/cascade_action_server.go`)
173+
174+
## Notes & Scope
175+
176+
- Changing block size only affects new encodes; existing artefacts keep their original layout.
177+
- Tuning should reflect your fleet: prefer safety defaults for heterogeneous environments; be aggressive only on known large‑RAM hosts.
178+
179+
## FAQ
180+
181+
- Why might a smaller file decode but a larger file fail?
182+
- Peak memory grows with data size and chosen block size. A smaller file may fit within the decoder’s memory budget on a given machine, while a larger one may exceed it. Smaller blocks and/or more RAM resolve this.
183+
184+
- Does changing block size affect old files?
185+
- No. It only affects newly encoded content. Existing artefacts retain their original layout.
186+
187+
- Will smaller blocks slow things down?
188+
- Slightly, due to more pieces and network lookups. For constrained machines, the reliability gain outweighs the small performance cost.
189+
190+
- What’s the best block size?
191+
- There’s no single best value. 128 MB is a solid default. Use 64 MB for smaller machines and 256–512 MB for large servers when maximizing throughput.

docs/gateway.md

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
11
# Supernode HTTP Gateway
22

3-
The Supernode exposes its gRPC services via an HTTP/JSON gateway on port `8002`.
3+
The HTTP gateway exposes the gRPC services via REST on port `8002` using grpc-gateway.
44

5-
- Swagger UI: http://localhost:8002/swagger-ui/
6-
- OpenAPI Spec: http://localhost:8002/swagger.json
5+
## Endpoints
76

8-
## Status API
7+
### GET `/api/v1/status`
8+
Returns supernode status: system resources (CPU, memory, storage), service info, and optionally P2P metrics.
99

10-
GET `/api/v1/status`
10+
- Query `include_p2p_metrics=true` enables detailed P2P metrics and peer info.
11+
- When omitted or false, peer count, peer addresses, and `p2p_metrics` are not included.
1112

12-
Returns the current supernode status including system resources (CPU, memory, storage), running tasks, registered services, network info, and codec configuration.
13+
Examples:
1314

14-
- Query `include_p2p_metrics=true` adds detailed P2P metrics and peer information.
15-
16-
Example:
1715
```bash
16+
# Lightweight status
1817
curl "http://localhost:8002/api/v1/status"
19-
```
2018

21-
With P2P metrics:
22-
```bash
19+
# Include P2P metrics and peer info
2320
curl "http://localhost:8002/api/v1/status?include_p2p_metrics=true"
2421
```
2522

26-
## Services API
27-
28-
GET `/api/v1/services`
29-
30-
Returns the list of available services and methods exposed by this supernode.
31-
32-
Example:
33-
```bash
34-
curl http://localhost:8002/api/v1/services
23+
Example responses are shown in the main README under the SupernodeService section.
24+
25+
### GET `/api/v1/codec`
26+
Returns the minimal effective RaptorQ codec configuration used by the node (fixed policy):
27+
28+
```json
29+
{
30+
"symbol_size": 65535,
31+
"redundancy": 5,
32+
"max_memory_mb": 12288,
33+
"concurrency": 4,
34+
"headroom_pct": 10,
35+
"mem_limit_mb": 13653,
36+
"mem_limit_source": "cgroupv2:memory.max"
37+
}
3538
```
3639

37-
## Notes
40+
## API Documentation
41+
42+
- Swagger UI: `http://localhost:8002/swagger-ui/`
43+
- OpenAPI Spec: `http://localhost:8002/swagger.json`
3844

39-
- The gateway translates between HTTP/JSON and gRPC/protobuf, enabling easy integration with web tooling and monitoring.
40-
- Interactive exploration is available via Swagger UI.
45+
The Swagger UI provides an interactive interface to explore and test all available API endpoints.

0 commit comments

Comments
 (0)