Commit 5764da5
Revamp component model stream/future host API (again) (#11515)
* Revamp component model stream/future host API (again)
This changes the host APIs for dealing with futures and streams from a
"rendezvous"-style API to a callback-oriented one.
Previously you would create e.g. a `StreamReader`/`StreamWriter` pair and call
their `read` and `write` methods, respectively, and those methods would return
`Future`s that resolved when the operation was matched with a corresponding
`write` or `read` operation on the other end.
With the new API, you instead provide a `StreamProducer` trait implementation
whe creating the stream, whose `produce` method will be called as soon as a read
happens, giving the implementation a chance to respond immediately without
making the reader wait for a rendezvous. Likewise, you can match the read end
of a stream to a `StreamConsumer` to respond immediately to writes. This model
should reduce scheduling overhead and make it easier to e.g. pipe items to/from
`AsyncWrite`/`AsyncRead` or `Sink`/`Stream` implementations without needing to
explicitly spawn background tasks. In addition, the new API provides direct
access to guest read and write buffers for `stream<u8>` operations, enabling
zero-copy operations.
Other changes:
- I've removed the `HostTaskOutput`; we were using it to run extra code with
access to the store after a host task completes, but we can do that more
elegantly inside the future using `tls::get`. This also allowed me to
simplify `Instance::poll_until` a bit.
- I've removed the `watch_{reader,writer}` functionality; it's not needed now
given that the runtime will automatically dispose of the producer or consumer
when the other end of the stream or future is closed -- no need for embedder
code to manage that.
- In order to make `UntypedWriteBuffer` `Send`, I had to wrap its raw pointer
`buf` field in a `SendSyncPtr`.
- I've removed `{Future,Stream}Writer` entirely and moved
`Instance::{future,stream}` to `{Future,Stream}Reader::new`, respectively.
- I've added a bounds check to the beginnings of `Instance::guest_read` and
`Instance::guest_write` so that we need not do it later in
`Guest{Source,Destination}::remaining`, meaning those functions can be
infallible.
Note that I haven't updated `wasmtime-wasi` yet to match; that will happen in
one or more follow-up commits.
Signed-off-by: Joel Dice <[email protected]>
* Add `Accessor::getter`, rename `with_data` to `with_getter`
* fixup bindgen invocation
Signed-off-by: Roman Volosatovs <[email protected]>
* add support for zero-length writes/reads to/from host
I've added a test to cover this; it also tests direct buffer access for
`stream<u8>`, which I realized I forgot to cover earlier. And of course there
was a bug 🤦.
Signed-off-by: Joel Dice <[email protected]>
* add `{Destination,Source}::remaining` methods
This can help `Stream{Producer,Consumer}` implementations determine how many
items to write or read, respectively.
Signed-off-by: Joel Dice <[email protected]>
* wasi: migrate sockets to new API
Signed-off-by: Roman Volosatovs <[email protected]>
* tests: read the socket stream until EOF
Signed-off-by: Roman Volosatovs <[email protected]>
* p3-sockets: account for cancellation
Signed-off-by: Roman Volosatovs <[email protected]>
* p3-sockets: mostly ensure byte buffer cancellation-safety
Signed-off-by: Roman Volosatovs <[email protected]>
* p3-filesystem: switch to new API
Signed-off-by: Roman Volosatovs <[email protected]>
* fixup! p3-sockets: mostly ensure byte buffer cancellation-safety
* p3-cli: switch to new API
Signed-off-by: Roman Volosatovs <[email protected]>
* p3: limit maximum buffer size
Signed-off-by: Roman Volosatovs <[email protected]>
* p3-sockets: remove reuseaddr test loop workaround
Signed-off-by: Roman Volosatovs <[email protected]>
* p3: drive I/O in `when_ready`
Signed-off-by: Roman Volosatovs <[email protected]>
* fixup! p3: drive I/O in `when_ready`
* Refine `Stream{Producer,Consumer}` APIs
Per conversations last week with Roman, Alex, and Lann, I've updated these
traits to present a lower-level API based on `poll_{consume,produce}` functions
and have documented the implementation requirements for various scenarios which
have come up in `wasmtime-wasi`, particularly around graceful cancellation. See
the doc comments for those functions for details.
Signed-off-by: Joel Dice <[email protected]>
* being integration of new API
Signed-off-by: Roman Volosatovs <[email protected]>
* update wasi/src/p3/filesystem to use new stream API
This is totally untested so far; I'll run the tests once we have everything else
compiling.
Signed-off-by: Joel Dice <[email protected]>
* update wasi/src/p3/cli to use new stream API
This is totally untested and doesn't even compile yet due to a lifetime issue I
don't have time to address yet. I'll follow up later with a fix.
Signed-off-by: Joel Dice <[email protected]>
* fix: remove `'a` bound on `&self`
Signed-off-by: Roman Volosatovs <[email protected]>
* finish `wasi:sockets` adaptation
Signed-off-by: Roman Volosatovs <[email protected]>
* finish `wasi:cli` adaptation
Note, that this removes the read optimization - let's get the
implementation complete first and optimize later
Signed-off-by: Roman Volosatovs <[email protected]>
* remove redundant loop in sockets
Signed-off-by: Roman Volosatovs <[email protected]>
* wasi: buffer on 0-length reads
Signed-off-by: Roman Volosatovs <[email protected]>
* finish `wasi:filesystem` adaptation
Signed-off-by: Roman Volosatovs <[email protected]>
* remove `MAX_BUFFER_CAPACITY`
Signed-off-by: Roman Volosatovs <[email protected]>
* refactor `Cursor` usage
Signed-off-by: Roman Volosatovs <[email protected]>
* impl Default for VecBuffer
Signed-off-by: Roman Volosatovs <[email protected]>
* refactor: use consistent import styling
Signed-off-by: Roman Volosatovs <[email protected]>
* feature-gate fs Arc accessors
Signed-off-by: Roman Volosatovs <[email protected]>
* Update test expectations
---------
Signed-off-by: Joel Dice <[email protected]>
Signed-off-by: Roman Volosatovs <[email protected]>
Co-authored-by: Alex Crichton <[email protected]>
Co-authored-by: Roman Volosatovs <[email protected]>1 parent e3561d5 commit 5764da5
File tree
49 files changed
+3691
-2614
lines changed- crates
- component-macro/tests/expanded
- misc/component-async-tests
- src
- tests
- scenario
- wit
- test-programs/src/bin
- wasi/src
- p3
- cli
- filesystem
- sockets/host/types
- wasmtime/src/runtime/component
- concurrent
- futures_and_streams
- wit-bindgen/src
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
49 files changed
+3691
-2614
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
218 | 218 | | |
219 | 219 | | |
220 | 220 | | |
221 | | - | |
| 221 | + | |
222 | 222 | | |
223 | 223 | | |
224 | 224 | | |
| |||
229 | 229 | | |
230 | 230 | | |
231 | 231 | | |
232 | | - | |
| 232 | + | |
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
| |||
Lines changed: 12 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
286 | 286 | | |
287 | 287 | | |
288 | 288 | | |
289 | | - | |
| 289 | + | |
290 | 290 | | |
291 | 291 | | |
292 | 292 | | |
| |||
299 | 299 | | |
300 | 300 | | |
301 | 301 | | |
302 | | - | |
| 302 | + | |
303 | 303 | | |
304 | 304 | | |
305 | 305 | | |
| |||
309 | 309 | | |
310 | 310 | | |
311 | 311 | | |
312 | | - | |
| 312 | + | |
313 | 313 | | |
314 | 314 | | |
315 | 315 | | |
| |||
320 | 320 | | |
321 | 321 | | |
322 | 322 | | |
323 | | - | |
| 323 | + | |
324 | 324 | | |
325 | 325 | | |
326 | 326 | | |
| |||
333 | 333 | | |
334 | 334 | | |
335 | 335 | | |
336 | | - | |
| 336 | + | |
337 | 337 | | |
338 | 338 | | |
339 | 339 | | |
| |||
343 | 343 | | |
344 | 344 | | |
345 | 345 | | |
346 | | - | |
| 346 | + | |
347 | 347 | | |
348 | 348 | | |
349 | 349 | | |
| |||
353 | 353 | | |
354 | 354 | | |
355 | 355 | | |
356 | | - | |
| 356 | + | |
357 | 357 | | |
358 | 358 | | |
359 | 359 | | |
| |||
364 | 364 | | |
365 | 365 | | |
366 | 366 | | |
367 | | - | |
| 367 | + | |
368 | 368 | | |
369 | 369 | | |
370 | 370 | | |
| |||
374 | 374 | | |
375 | 375 | | |
376 | 376 | | |
377 | | - | |
| 377 | + | |
378 | 378 | | |
379 | 379 | | |
380 | 380 | | |
| |||
384 | 384 | | |
385 | 385 | | |
386 | 386 | | |
387 | | - | |
| 387 | + | |
388 | 388 | | |
389 | 389 | | |
390 | 390 | | |
| |||
394 | 394 | | |
395 | 395 | | |
396 | 396 | | |
397 | | - | |
| 397 | + | |
398 | 398 | | |
399 | 399 | | |
400 | 400 | | |
| |||
404 | 404 | | |
405 | 405 | | |
406 | 406 | | |
407 | | - | |
| 407 | + | |
408 | 408 | | |
409 | 409 | | |
410 | 410 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
228 | 228 | | |
229 | 229 | | |
230 | 230 | | |
231 | | - | |
| 231 | + | |
232 | 232 | | |
233 | 233 | | |
234 | 234 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
184 | 184 | | |
185 | 185 | | |
186 | 186 | | |
187 | | - | |
| 187 | + | |
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
351 | 351 | | |
352 | 352 | | |
353 | 353 | | |
354 | | - | |
| 354 | + | |
355 | 355 | | |
356 | 356 | | |
357 | 357 | | |
| |||
362 | 362 | | |
363 | 363 | | |
364 | 364 | | |
365 | | - | |
| 365 | + | |
366 | 366 | | |
367 | 367 | | |
368 | 368 | | |
| |||
373 | 373 | | |
374 | 374 | | |
375 | 375 | | |
376 | | - | |
| 376 | + | |
377 | 377 | | |
378 | 378 | | |
379 | 379 | | |
| |||
384 | 384 | | |
385 | 385 | | |
386 | 386 | | |
387 | | - | |
| 387 | + | |
388 | 388 | | |
389 | 389 | | |
390 | 390 | | |
| |||
395 | 395 | | |
396 | 396 | | |
397 | 397 | | |
398 | | - | |
| 398 | + | |
399 | 399 | | |
400 | 400 | | |
401 | 401 | | |
| |||
409 | 409 | | |
410 | 410 | | |
411 | 411 | | |
412 | | - | |
| 412 | + | |
413 | 413 | | |
414 | 414 | | |
415 | 415 | | |
| |||
423 | 423 | | |
424 | 424 | | |
425 | 425 | | |
426 | | - | |
| 426 | + | |
427 | 427 | | |
428 | 428 | | |
429 | 429 | | |
| |||
Lines changed: 4 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
225 | 225 | | |
226 | 226 | | |
227 | 227 | | |
228 | | - | |
| 228 | + | |
229 | 229 | | |
230 | 230 | | |
231 | 231 | | |
| |||
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
239 | | - | |
| 239 | + | |
240 | 240 | | |
241 | 241 | | |
242 | 242 | | |
| |||
247 | 247 | | |
248 | 248 | | |
249 | 249 | | |
250 | | - | |
| 250 | + | |
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
| |||
257 | 257 | | |
258 | 258 | | |
259 | 259 | | |
260 | | - | |
| 260 | + | |
261 | 261 | | |
262 | 262 | | |
263 | 263 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
184 | 184 | | |
185 | 185 | | |
186 | 186 | | |
187 | | - | |
| 187 | + | |
188 | 188 | | |
189 | 189 | | |
190 | 190 | | |
| |||
0 commit comments