Skip to content

Commit 5829f48

Browse files
committed
wasm demo script + filtering proxy
1 parent 4c15c69 commit 5829f48

30 files changed

+1084
-254
lines changed

edge/wasm-sensors/Makefile

+23-14
Original file line numberDiff line numberDiff line change
@@ -62,29 +62,38 @@ push-puller: ## Push the puller Docker image for all architectures
6262
--push \
6363
-f containers/docker/sqs-puller/Dockerfile containers/docker/sqs-puller
6464

65-
6665
##@ WASM
6766

68-
build-wasm: build-sqs-publisher build-config-updater build-config-reader build-dir-lister
67+
build-wasm: build-sqs-publisher
68+
6969
build-sqs-publisher: ## Build the SQS publisher WASM module
7070
@echo "Building SQS publisher WASM module..."
7171
@cd containers/wasm/sqs-publisher && tinygo build -o ../../../network/modules/publisher.wasm -target wasi .
7272
@echo "SQS publisher WASM build completed successfully!"
7373

74-
build-config-updater: ## Build the config updater WASM module
75-
@echo "Building config updater WASM module..."
76-
@cd containers/wasm/config-updater && tinygo build -o ../../../network/modules/config-updater.wasm -target wasi .
77-
@echo "Config updater WASM build completed successfully!"
74+
##@ Legacy WASM
75+
76+
build-legacy: build-legacy-config-updater build-legacy-config-reader build-legacy-dir-lister build-legacy-sqs-publisher
77+
78+
build-legacy-config-updater: ## Build the legacy config updater WASM module
79+
@echo "Building legacy config updater WASM module..."
80+
@cd containers/wasm/legacy/config-updater && tinygo build -o ../../../../network/modules/legacy/config-updater.wasm -target wasi .
81+
@echo "Legacy config updater WASM build completed successfully!"
82+
83+
build-legacy-config-reader: ## Build the legacy config reader WASM module
84+
@echo "Building legacy config reader WASM module..."
85+
@cd containers/wasm/legacy/config-reader && tinygo build -o ../../../../network/modules/legacy/config-reader.wasm -target wasi .
86+
@echo "Legacy config reader WASM build completed successfully!"
7887

79-
build-config-reader: ## Build the config reader WASM module
80-
@echo "Building config reader WASM module..."
81-
@cd containers/wasm/config-reader && tinygo build -o ../../../network/modules/config-reader.wasm -target wasi .
82-
@echo "Config reader WASM build completed successfully!"
88+
build-legacy-dir-lister: ## Build the legacy directory lister WASM module
89+
@echo "Building legacy directory lister WASM module..."
90+
@cd containers/wasm/legacy/dir-lister && tinygo build -o ../../../../network/modules/legacy/dir-lister.wasm -target wasi .
91+
@echo "Legacy directory lister WASM build completed successfully!"
8392

84-
build-dir-lister: ## Build the directory lister WASM module
85-
@echo "Building directory lister WASM module..."
86-
@cd containers/wasm/dir-lister && tinygo build -o ../../../network/modules/dir-lister.wasm -target wasi .
87-
@echo "Directory lister WASM build completed successfully!"
93+
build-legacy-sqs-publisher: ## Build the legacy SQS publisher WASM module
94+
@echo "Building legacy SQS publisher WASM module..."
95+
@cd containers/wasm/legacy/sqs-publisher && tinygo build -o ../../../../network/modules/legacy/sqs-publisher.wasm -target wasi .
96+
@echo "Legacy SQS publisher WASM build completed successfully!"
8897

8998
##@ Development
9099

edge/wasm-sensors/README.md

+133-39
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,46 @@ The WASM Sensors project demonstrates:
1616
The Docker Compose network setup establishes a comprehensive Bacalhau environment with the following components:
1717

1818
### Orchestrator
19+
1920
- Central coordinator for the Bacalhau network
2021
- Manages job scheduling and distribution
2122
- Exposes NATS port (4222) for communication with compute nodes
2223
- Exposes WebUI port (8438) for monitoring and management
2324

2425
### Edge Compute Nodes
26+
2527
The setup includes multiple edge compute nodes organized by geographical regions:
28+
2629
- **edge-us**: Edge nodes simulating US region deployment
2730
- **edge-eu**: Edge nodes simulating European region deployment
2831
- **edge-as**: Edge nodes simulating Asian region deployment
2932

3033
Each edge compute node:
34+
3135
- Is labeled with its region and type (e.g., `region=us,type=edge`)
3236
- Has access to WASM modules from the host system
3337
- Connects to the orchestrator for job coordination
3438

3539
### Web Services Compute Node
40+
3641
- Dedicated node for running centralized web services
3742
- Labeled as `type=web-services`
3843
- Runs in privileged mode to support Docker-in-Docker
3944
- Hosts the SQS Proxy and SQS Puller when deployed as Bacalhau service jobs
4045

4146
### Supporting Services
47+
4248
- **SQS Proxy**: Direct service running on port 9090
4349
- **SQS Puller**: Direct service processing messages from SQS
4450

4551
### Client Node
52+
4653
- Interactive shell for interacting with the Bacalhau network
4754
- Configured to connect to the orchestrator
4855
- Useful for submitting jobs and monitoring status
4956

5057
### Network Diagram
58+
5159
```
5260
┌─────────────────────────────────────────────────────────────────┐
5361
│ bacalhau-edge Network │
@@ -64,7 +72,7 @@ Each edge compute node:
6472
│ ┌─────┴─────┐ ┌──────┴───────┐ │
6573
│ │ │ │ │ │
6674
│ │ Client │ │ SQS Proxy │ │
67-
│ │ │ │ (9090) │ │
75+
│ │ │ │ (8080/9090) │ │
6876
│ └───────────┘ └──────────────┘ │
6977
│ ▲ │
7078
│ │ │
@@ -85,66 +93,78 @@ Each edge compute node:
8593
The SQS Publisher is a WebAssembly module that generates simulated sensor data and sends it to the SQS Proxy. It can be configured with parameters:
8694

8795
- **Interval**: Time between messages (in seconds)
88-
- **Color**: Hex color code for visual identification
89-
- **RandomOff**: Whether to use deterministic or random icons
96+
- **Color**: Hex color code for visual identification or "-1" for random color
97+
- **Emoji**: Index of emoji to use (-1 for random)
9098

91-
It reads its configuration from a YAML file that can be updated while the job is running.
99+
The configuration is set at startup and remains constant throughout the job's execution.
92100

93101
### Message Schema
94102

95103
The SQS Publisher sends messages with the following JSON schema:
96104

97105
```json
98106
{
99-
"job_id": "bacalhau-job-id", // The Bacalhau job ID of the publisher
100-
"execution_id": "execution-id", // The Bacalhau execution ID
101-
"icon_name": "🚀", // An emoji representing the message
107+
"job_id": "bacalhau-job-id", // The Bacalhau job ID of the publisher
108+
"execution_id": "execution-id", // The Bacalhau execution ID
109+
"hostname": "edge-node-1", // The hostname of the edge node
110+
"job_submission_time": 1711449600, // Unix timestamp of when the Bacalhau job was submitted
111+
"icon_name": "😀", // An emoji from the supported list
102112
"timestamp": "2025-03-26T12:34:56Z", // ISO 8601 timestamp in UTC
103-
"color": "#FF5733", // Hex color code from configuration
104-
"sequence": 42 // Sequential message counter
113+
"color": "#FF5733", // Hex color code
114+
"sequence": 42, // Sequential message counter
115+
"region": "us" // Region from environment
105116
}
106117
```
107118

108119
### Message Fields:
109120

110121
- **job_id**: The Bacalhau job ID that identifies the publisher job
111122
- **execution_id**: The Bacalhau execution ID for the specific execution instance
112-
- **icon_name**: An emoji selected from a predefined list (random or fixed depending on the `randomOff` configuration)
123+
- **hostname**: The hostname of the edge node sending the message
124+
- **job_submission_time**: Unix timestamp of when the Bacalhau job was submitted
125+
- **icon_name**: An emoji from a predefined list of supported emojis
113126
- **timestamp**: ISO 8601 formatted timestamp in UTC
114-
- **color**: Hex color code specified in the configuration
127+
- **color**: Hex color code (can be specified or random)
115128
- **sequence**: Incrementing counter for each message sent by this publisher instance
129+
- **region**: AWS region read from the REGION environment variable
130+
131+
### Legacy Implementation
132+
133+
The `legacy` directory contains an initial implementation attempt to support updating configuration files at runtime using WebAssembly. This approach was ultimately not pursued due to limitations with the current wazero-based executor. Instead, we now use direct job configuration updates.
116134

117-
These messages are sent to the SQS Proxy, which forwards them to Amazon SQS. The SQS Puller then retrieves and processes these messages from the queue.
135+
The legacy implementation included:
136+
137+
- `config-reader`: Attempted to read configuration files at runtime
138+
- `config-updater`: Attempted to update configuration files at runtime
139+
- `dir-lister`: Utility for listing directory contents
140+
- `sqs-publisher`: Previous version of the message publisher
118141

119142
### SQS Proxy
120143

121144
The SQS Proxy is a FastAPI application that:
145+
122146
- Receives HTTP POST requests from edge nodes
123147
- Forwards valid JSON messages to Amazon SQS
124148
- Provides a health check endpoint
125149
- Can be deployed either as a Bacalhau service job or alongside the Docker Compose setup
126150

127-
**Important Note**: The SQS Proxy is available at two different ports:
128-
- Port `8080` when deployed as a Bacalhau service job
129-
- Port `9090` when deployed via Docker Compose
151+
**Important Note**: The SQS Proxy URL varies depending on the deployment method:
152+
153+
- When deployed via Docker Compose: `http://bacalhau-edge-sqs-proxy-1:9090`
154+
- When deployed as a Bacalhau service job: `http://bacalhau-edge-web-services-1:8080`
155+
156+
Make sure to use the appropriate URL when deploying the SQS Publisher jobs.
130157

131158
### SQS Puller
132159

133160
The SQS Puller retrieves messages from Amazon SQS and processes them. In this demo, it simply logs messages to stdout but could be extended to:
161+
134162
- Process data
135163
- Trigger alerts
136164
- Store data in a database
137165

138166
Like the SQS Proxy, it can be deployed either as a Bacalhau service job or alongside Docker Compose.
139167

140-
### Config Updater
141-
142-
The Config Updater is a WASM module that can update the SQS Publisher's configuration file. Due to limitations in Bacalhau v1.7.0, it cannot directly update a running job's configuration. To apply changes:
143-
144-
1. Use Config Updater to modify the configuration file
145-
2. Stop the current SQS Publisher job
146-
3. Deploy a new SQS Publisher job that will use the updated configuration
147-
148168
## 🚀 Getting Started
149169

150170
### Prerequisites
@@ -158,6 +178,7 @@ The Config Updater is a WASM module that can update the SQS Publisher's configur
158178
### Initial Setup
159179

160180
1. Clone the repository:
181+
161182
```bash
162183
git clone https://github.com/bacalhau-project/wasm-sensors.git
163184
cd wasm-sensors
@@ -166,58 +187,131 @@ The Config Updater is a WASM module that can update the SQS Publisher's configur
166187
2. Create an SQS queue in your AWS account
167188

168189
3. Configure your environment:
190+
169191
```bash
170192
cp network/.env.template network/.env
171193
```
194+
172195
Edit `network/.env` and add your AWS credentials and SQS queue URL.
173196

174-
4. Start the Bacalhau network:
197+
### Complete Demo Scenario
198+
199+
This scenario demonstrates the message flow and deployment behavior across different regions:
200+
201+
1. Start the network:
202+
203+
```bash
204+
./demo.sh start_network
205+
```
206+
207+
2. Deploy to US region with red color and rocket emoji:
208+
209+
```bash
210+
./demo.sh deploy "us" "#FF0000" 0
211+
```
212+
213+
This will deploy the SQS Publisher to US edge nodes with red color and rocket emoji 🚀.
214+
215+
3. Deploy to EU region with blue color and satellite emoji:
216+
217+
```bash
218+
./demo.sh deploy "eu" "#0000FF" 1
219+
```
220+
221+
This will deploy the SQS Publisher to EU edge nodes with blue color and satellite emoji 📡.
222+
223+
4. Deploy to AS region with green color and light bulb emoji:
224+
225+
```bash
226+
./demo.sh deploy "as" "#00FF00" 2
227+
```
228+
229+
This will deploy the SQS Publisher to AS edge nodes with green color and light bulb emoji 💡.
230+
231+
5. Update US deployment with new configuration (purple color and lightning emoji):
232+
175233
```bash
176-
cd network
177-
docker-compose up -d
234+
./demo.sh deploy "us" "#800080" 3
178235
```
179236

237+
This will deploy a new version of the SQS Publisher to US nodes with purple color and lightning emoji ⚡.
238+
239+
6. Disconnect EU region:
240+
241+
```bash
242+
./demo.sh disconnect "eu"
243+
```
244+
245+
This will stop the EU deployment.
246+
247+
7. Deploy new configuration to EU (yellow color and battery emoji):
248+
249+
```bash
250+
./demo.sh deploy "eu" "#FFFF00" 4
251+
```
252+
253+
This will deploy a new version of the SQS Publisher to EU nodes with yellow color and battery emoji 🔋.
254+
255+
8. Reconnect EU region:
256+
257+
```bash
258+
./demo.sh reconnect "eu"
259+
```
260+
261+
This will restart the EU deployment.
262+
263+
9. Monitor message flow:
264+
```bash
265+
./demo.sh logs
266+
```
267+
You should see:
268+
- Messages from US nodes (new deployment with purple color and lightning emoji ⚡)
269+
- Messages from EU nodes (new deployment with yellow color and battery emoji 🔋)
270+
- Messages from AS nodes (green color and light bulb emoji 💡)
271+
272+
This demo shows how to deploy and update the SQS Publisher across different regions, with each region having its own unique visual identity through colors and emojis.
273+
180274
### Deployment Options
181275

182276
#### Option 1: Deploy using Docker Compose Services
183277

184278
The provided Docker Compose file already includes the SQS Proxy and SQS Puller services, running on ports:
279+
185280
- SQS Proxy: `9090`
186281
- SQS Puller: logs to Docker Compose logs
187282

188283
#### Option 2: Deploy using Bacalhau Service Jobs
189284

190285
Deploy the SQS Proxy as a Bacalhau service job:
286+
191287
```bash
192288
bacalhau job run -V queue_url=wasm-sensors-demo jobs/sqs-proxy.yaml
193289
```
194290

195291
Deploy the SQS Puller as a Bacalhau service job:
292+
196293
```bash
197294
bacalhau job run -V queue_url=wasm-sensors-demo jobs/sqs-puller.yaml
198295
```
199296

200297
### Deploy the SQS Publisher to Edge Nodes
201298

202299
Deploy the SQS Publisher WebAssembly module targeting the Docker Compose SQS Proxy:
203-
```bash
204-
bacalhau job run -V proxy=http://bacalhau-edge-sqs-proxy-1:9090 jobs/sqs-publisher.yaml
205-
```
206300

207-
To target the Bacalhau service job SQS Proxy instead:
208301
```bash
209-
bacalhau job run -V proxy=http://bacalhau-edge-web-services-1:8080 jobs/sqs-publisher.yaml
302+
# With specific color and emoji
303+
bacalhau job run -V proxy=http://bacalhau-edge-sqs-proxy-1:9090 -V color="#FF5733" -V emoji=0 jobs/sqs-publisher.yaml
304+
305+
# With random color and emoji
306+
bacalhau job run -V proxy=http://bacalhau-edge-sqs-proxy-1:9090 -V color="-1" -V emoji=-1 jobs/sqs-publisher.yaml
210307
```
211308

212-
### Update Configuration
309+
To target the Bacalhau service job SQS Proxy instead:
213310

214-
To update the configuration (note: currently doesn't work with Bacalhau v1.7.0):
215311
```bash
216-
bacalhau job run -V color="#FEFEFE" jobs/config-updater.yaml
312+
bacalhau job run -V proxy=http://bacalhau-edge-web-services-1:8080 -V color="#FF5733" -V emoji=0 jobs/sqs-publisher.yaml
217313
```
218314

219-
As noted earlier, you'll need to restart the SQS Publisher job for these changes to take effect.
220-
221315
## 🛠️ Development
222316

223317
### Development Notes
@@ -275,8 +369,9 @@ curl http://localhost:9090/health
275369
```
276370

277371
This should return:
372+
278373
```json
279-
{"status":"healthy"}
374+
{ "status": "healthy" }
280375
```
281376

282377
## 🔄 Message Flow
@@ -289,6 +384,5 @@ This should return:
289384

290385
## ⚠️ Known Limitations
291386

292-
- **Configuration Updates**: Due to Bacalhau v1.7.0 limitations, you cannot dynamically update a running job's configuration. You need to stop and redeploy the job.
387+
- **Configuration Updates**: Configuration is set at job startup and cannot be changed during execution. To change configuration, you need to stop and redeploy the job.
293388
- **Direct File Access**: WASM modules have limited filesystem access through Bacalhau.
294-

0 commit comments

Comments
 (0)