Skip to content
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

src: implement DataQueue and non-memory resident Blob #45258

Closed
wants to merge 3 commits into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Oct 31, 2022

This is the start of work being collaboratively done by myself and @flakey5 to update the implementation of Blob to support non-memory resident data sources such as files as well as enable proper streaming support.

It is a work in progress and remain draft status until it is ready to go. Opening it for transparency.

Some background:

Currently, Blob objects consist solely of memory-resident data, backed by a collection of v8::BackingStore instances. While this behavior is correct standard/spec defined behavior, it is extremely inefficient. To support other use cases of Blob, we want to be able to efficiently and effectively support non-memory resident data sources such as files. To do so will require a number of key changes on the internals of the Blob class.

Further, the current implementation of Blob suffers from an overly simplistic streaming model where the entire Blob is first read into a single ArrayBuffer before passing the data on to the stream API. Effectively meaning Blobs are never actually streamed.

While going through this evaluation, we came to realization that many of the underlying requirements for Blob are (surprisingly) shared by the internal data management requirements for the QUIC implementation that is in progress, and can enable a range of other use cases also, so we have decided to take an approach that addresses those multiple use cases.

When complete, this PR is going to do two things:

This PR does three things:

  1. Introduces a new C++ data structure called DataQueue which acts as a sequence of memory-resident and non-memory-resident data sources that can be consumed as a single logical stream of data. This is designed to meet the needs of both Blob and QUIC initially (the bits relevant to QUIC will come later)
  2. Update the current Blob implementation to use DataQueue internally in support of the existing all-memory-resident model.
  3. Provides an API for acquiring a File-backed Blob that will support efficient, non-memory resident use cases.

This PR is far from complete. I would strongly recommend that you hold off performing any review on it at all until after we flip the bit to mark it ready for review. We are opening this draft PR now purely for the sake of transparency so that folks can be aware of what we are working on.

I also want to avoid any and all bikeshedding on the design until we at least have the updated Blob implementation ready to go, and many of the design decisions made will likely not be clear until that concrete case is implemented.

Specifically for Blob, this PR will enable enables:

  1. Proper streaming support. No longer will the Blob data be collected into a single ArrayBuffer and then passed on to the stream, but data will flow into the stream as one would actually expect.
  2. File-backed Blob. The ability to acquire a Blob that is backed by an on-disc file. As well as the ability to use such Blob instances efficiently within other Blob instances, e.g. new Blob(['string', fileBackedBlob])

For QUIC, eventually the DataQueue implemented here will replace the Stream::Queue mechanism implemented in that PR. DataQueue will not completely replace that mechanism but covers a sizable chunk of it, which should further simplify and reduce the size of that rather complex bit of work.

As for other use cases of DataQueue... we'll address those later, but we do have a range of cases in mind (including the recent discussions around Node.js providing an efficient http static file server).

/cc @mcollina (who prompted this work due to Undici and Fetch use case requirements)

@nodejs-github-bot nodejs-github-bot added build Issues and PRs related to build files or the CI. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Oct 31, 2022
@nodejs nodejs deleted a comment from nodejs-github-bot Oct 31, 2022
src/dataqueue/queue.h Show resolved Hide resolved
src/dataqueue/queue.h Outdated Show resolved Hide resolved
@jasnell
Copy link
Member Author

jasnell commented Dec 17, 2022

@mcollina ... this is ready for review!

test/parallel/test-blob-file-backed.js Outdated Show resolved Hide resolved
doc/api/fs.md Outdated Show resolved Hide resolved
test/parallel/test-blob-file-backed.js Outdated Show resolved Hide resolved
@mcollina
Copy link
Member

cc @KhafraDev

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@jasnell jasnell added semver-minor PRs that contain new features and should be released in the next minor version. experimental Issues and PRs related to experimental features. labels Dec 20, 2022
@nodejs-github-bot
Copy link
Contributor

addaleax
addaleax previously requested changes Dec 21, 2022
src/dataqueue/queue.h Outdated Show resolved Hide resolved
src/dataqueue/queue.h Outdated Show resolved Hide resolved
src/node_blob.cc Outdated Show resolved Hide resolved
src/node_blob.cc Show resolved Hide resolved
src/dataqueue/queue.cc Outdated Show resolved Hide resolved
src/dataqueue/queue.cc Outdated Show resolved Hide resolved
src/dataqueue/queue.cc Show resolved Hide resolved
src/dataqueue/queue.cc Outdated Show resolved Hide resolved
src/dataqueue/queue.cc Outdated Show resolved Hide resolved
src/dataqueue/queue.cc Outdated Show resolved Hide resolved
@jasnell
Copy link
Member Author

jasnell commented Dec 22, 2022

Moving this back to Draft status. Trying to work through @addaleax's feedback and wanted to make it so that the FdEntry Reader is using FileHandle as a StreamBase but for some reason I'm having a difficult time getting it to work to perform parallel reads of the same file with two separate fds. Will move back out of draft status once I've got it working.

@jasnell jasnell marked this pull request as draft December 22, 2022 21:02
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@mcollina
Copy link
Member

@jasnell It seems the test failure for the ASAN build is relevant:

https://github.com/nodejs/node/actions/runs/4190697226/jobs/7264365006#step:6:5307

=================================================================
==94572==ERROR: AddressSanitizer: heap-use-after-free on address 0x6100000049d0 at pc 0x000000c78e3b bp 0x7ffc79279510 sp 0x7ffc79279508
READ of size 8 at 0x6100000049d0 thread T0
    #0 0xc78e3a in node::(anonymous namespace)::FdEntry::ReaderImpl::OnStreamRead(long, uv_buf_t const&) (/home/runner/work/node/node/out/Release/node+0xc78e3a)
    #1 0xe7fb45 in node::fs::FileHandle::ReadStart() (/home/runner/work/node/node/out/Release/node+0xe7fb45)
    #2 0xec6004 in node::fs::FileHandle::ReadStart()::$_1::__invoke(uv_fs_s*) (/home/runner/work/node/node/out/Release/node+0xec6004)
    #3 0xe4e45f in node::MakeLibuvRequestCallback<uv_fs_s, void (*)(uv_fs_s*)>::Wrapper(uv_fs_s*) (/home/runner/work/node/node/out/Release/node+0xe4e45f)
    #4 0x32354e3 in uv__work_done /home/runner/work/node/node/out/../deps/uv/src/threadpool.c:318:5
    #5 0x323d278 in uv__async_io /home/runner/work/node/node/out/../deps/uv/src/unix/async.c:163:5
    #6 0x3272e13 in uv__io_poll /home/runner/work/node/node/out/../deps/uv/src/unix/epoll.c:374:11
    #7 0x323e297 in uv_run /home/runner/work/node/node/out/../deps/uv/src/unix/core.c:406:5
    #8 0xbd20a6 in node::SpinEventLoopInternal(node::Environment*) (/home/runner/work/node/node/out/Release/node+0xbd20a6)
    #9 0xf5d2fe in node::NodeMainInstance::Run() (/home/runner/work/node/node/out/Release/node+0xf5d2fe)
    #10 0xd9b6f7 in node::LoadSnapshotDataAndRun(node::SnapshotData const**, node::InitializationResultImpl const*) (/home/runner/work/node/node/out/Release/node+0xd9b6f7)
    #11 0xd9c071 in node::Start(int, char**) (/home/runner/work/node/node/out/Release/node+0xd9c071)
    #12 0x7f1fe58d7082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)
    #13 0xb200ad in _start (/home/runner/work/node/node/out/Release/node+0xb200ad)

0x6100000049d0 is located 144 bytes inside of 184-byte region [0x610000004940,0x6100000049f8)
freed by thread T0 here:
    #0 0xbca61d in operator delete(void*) (/home/runner/work/node/node/out/Release/node+0xbca61d)
    #1 0xc7858b in node::(anonymous namespace)::FdEntry::ReaderImpl::OnStreamRead(long, uv_buf_t const&) (/home/runner/work/node/node/out/Release/node+0xc7858b)
    #2 0xe7fb45 in node::fs::FileHandle::ReadStart() (/home/runner/work/node/node/out/Release/node+0xe7fb45)
    #3 0xec6004 in node::fs::FileHandle::ReadStart()::$_1::__invoke(uv_fs_s*) (/home/runner/work/node/node/out/Release/node+0xec6004)
    #4 0xe4e45f in node::MakeLibuvRequestCallback<uv_fs_s, void (*)(uv_fs_s*)>::Wrapper(uv_fs_s*) (/home/runner/work/node/node/out/Release/node+0xe4e45f)
    #5 0x32354e3 in uv__work_done /home/runner/work/node/node/out/../deps/uv/src/threadpool.c:318:5
    #6 0x323d278 in uv__async_io /home/runner/work/node/node/out/../deps/uv/src/unix/async.c:163:5
    #7 0x3272e13 in uv__io_poll /home/runner/work/node/node/out/../deps/uv/src/unix/epoll.c:374:11
    #8 0x323e297 in uv_run /home/runner/work/node/node/out/../deps/uv/src/unix/core.c:406:5
    #9 0xbd20a6 in node::SpinEventLoopInternal(node::Environment*) (/home/runner/work/node/node/out/Release/node+0xbd20a6)
    #10 0xf5d2fe in node::NodeMainInstance::Run() (/home/runner/work/node/node/out/Release/node+0xf5d2fe)
    #11 0xd9b6f7 in node::LoadSnapshotDataAndRun(node::SnapshotData const**, node::InitializationResultImpl const*) (/home/runner/work/node/node/out/Release/node+0xd9b6f7)
    #12 0xd9c071 in node::Start(int, char**) (/home/runner/work/node/node/out/Release/node+0xd9c071)
    #13 0x7f1fe58d7082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)

previously allocated by thread T0 here:
    #0 0xbc9dbd in operator new(unsigned long) (/home/runner/work/node/node/out/Release/node+0xbc9dbd)
    #1 0xc7635c in node::(anonymous namespace)::FdEntry::get_reader() (/home/runner/work/node/node/out/Release/node+0xc7635c)
    #2 0xc7ed5b in node::(anonymous namespace)::IdempotentDataQueueReader::Pull(std::function<void (int, node::DataQueue::Vec const*, unsigned long, std::function<void (unsigned long)>)>, int, node::DataQueue::Vec*, unsigned long, unsigned long) (/home/runner/work/node/node/out/Release/node+0xc7ed5b)
    #3 0xdcef0c in node::Blob::Reader::Pull(v8::FunctionCallbackInfo<v8::Value> const&) (/home/runner/work/node/node/out/Release/node+0xdcef0c)
    #4 0x15850f1 in v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, unsigned long*, int) (/home/runner/work/node/node/out/Release/node+0x15850f1)
    #5 0x1582d5b in v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) (/home/runner/work/node/node/out/Release/node+0x1582d5b)
    #6 0x3311478 in Builtins_CEntry_Return1_DontSaveFPRegs_ArgvOnStack_BuiltinExit (/home/runner/work/node/node/out/Release/node+0x3311478)
    #7 0x32886db in Builtins_InterpreterEntryTrampoline (/home/runner/work/node/node/out/Release/node+0x32886db)
    #8 0x32886db in Builtins_InterpreterEntryTrampoline (/home/runner/work/node/node/out/Release/node+0x32886db)
    #9 0x32886db in Builtins_InterpreterEntryTrampoline (/home/runner/work/node/node/out/Release/node+0x32886db)
    #10 0x32bfe45 in Builtins_AsyncFunctionAwaitResolveClosure (/home/runner/work/node/node/out/Release/node+0x32bfe45)
    #11 0x336a2f0 in Builtins_PromiseFulfillReactionJob (/home/runner/work/node/node/out/Release/node+0x336a2f0)
    #12 0x32afd3d in Builtins_RunMicrotasks (/home/runner/work/node/node/out/Release/node+0x32afd3d)
    #13 0x3286d82 in Builtins_JSRunMicrotasksEntry (/home/runner/work/node/node/out/Release/node+0x3286d82)
    #14 0x186c8ee in v8::internal::(anonymous namespace)::Invoke(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) (/home/runner/work/node/node/out/Release/node+0x186c8ee)
    #15 0x186f004 in v8::internal::(anonymous namespace)::InvokeWithTryCatch(v8::internal::Isolate*, v8::internal::(anonymous namespace)::InvokeParams const&) (/home/runner/work/node/node/out/Release/node+0x186f004)
    #16 0x186f9c9 in v8::internal::Execution::TryRunMicrotasks(v8::internal::Isolate*, v8::internal::MicrotaskQueue*) (/home/runner/work/node/node/out/Release/node+0x186f9c9)
    #17 0x18ff78b in v8::internal::MicrotaskQueue::RunMicrotasks(v8::internal::Isolate*) (/home/runner/work/node/node/out/Release/node+0x18ff78b)
    #18 0x1900e32 in v8::internal::MicrotaskQueue::PerformCheckpoint(v8::Isolate*) (/home/runner/work/node/node/out/Release/node+0x1900e32)
    #19 0xbcff16 in node::InternalCallbackScope::Close() (/home/runner/work/node/node/out/Release/node+0xbcff16)
    #20 0xbcfa5d in node::InternalCallbackScope::~InternalCallbackScope() (/home/runner/work/node/node/out/Release/node+0xbcfa5d)
    #21 0xd91c30 in node::StartExecution(node::Environment*, std::function<v8::MaybeLocal<v8::Value> (node::StartExecutionCallbackInfo const&)>) (/home/runner/work/node/node/out/Release/node+0xd91c30)
    #22 0xbe16fb in node::LoadEnvironment(node::Environment*, std::function<v8::MaybeLocal<v8::Value> (node::StartExecutionCallbackInfo const&)>) (/home/runner/work/node/node/out/Release/node+0xbe16fb)
    #23 0xf5d2c9 in node::NodeMainInstance::Run() (/home/runner/work/node/node/out/Release/node+0xf5d2c9)
    #24 0xd9b6f7 in node::LoadSnapshotDataAndRun(node::SnapshotData const**, node::InitializationResultImpl const*) (/home/runner/work/node/node/out/Release/node+0xd9b6f7)
    #25 0xd9c071 in node::Start(int, char**) (/home/runner/work/node/node/out/Release/node+0xd9c071)
    #26 0x7f1fe58d7082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082)

jasnell and others added 3 commits February 19, 2023 13:52
See documentation in dataqueue/queue.h for details

Co-authored-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Co-authored-by: flakey5 <73616808+flakey5@users.noreply.github.com>
Co-authored-by: flakey5 <73616808+flakey5@users.noreply.github.com>
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Contributor

nodejs-github-bot commented Feb 19, 2023

jasnell added a commit that referenced this pull request Feb 20, 2023
See documentation in dataqueue/queue.h for details

Co-authored-by: flakey5 <73616808+flakey5@users.noreply.github.com>
PR-URL: #45258
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
jasnell added a commit that referenced this pull request Feb 20, 2023
Co-authored-by: flakey5 <73616808+flakey5@users.noreply.github.com>
PR-URL: #45258
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
jasnell added a commit that referenced this pull request Feb 20, 2023
Co-authored-by: flakey5 <73616808+flakey5@users.noreply.github.com>
PR-URL: #45258
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
@jasnell
Copy link
Member Author

jasnell commented Feb 20, 2023

Landed in 9a82938...71fb06f

@jasnell jasnell closed this Feb 20, 2023
@juanarbol juanarbol added the backport-requested-v18.x PRs awaiting manual backport to the v18.x-staging branch. label Mar 1, 2023
@juanarbol
Copy link
Member

Would you mind to backporting this to v18.x?

targos pushed a commit that referenced this pull request Mar 13, 2023
See documentation in dataqueue/queue.h for details

Co-authored-by: flakey5 <73616808+flakey5@users.noreply.github.com>
PR-URL: #45258
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
targos pushed a commit that referenced this pull request Mar 13, 2023
Co-authored-by: flakey5 <73616808+flakey5@users.noreply.github.com>
PR-URL: #45258
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
targos pushed a commit that referenced this pull request Mar 13, 2023
Co-authored-by: flakey5 <73616808+flakey5@users.noreply.github.com>
PR-URL: #45258
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
targos added a commit that referenced this pull request Mar 14, 2023
Notable changes:

buffer:
  * (SEMVER-MINOR) add Buffer.copyBytesFrom(...) (James M Snell) #46500
doc:
  * add marco-ippolito to collaborators (Marco Ippolito) #46816
events:
  * (SEMVER-MINOR) add listener argument to listenerCount (Paolo Insogna) #46523
lib:
  * (SEMVER-MINOR) add AsyncLocalStorage.bind() and .snapshot() (flakey5) #46387
src:
  * (SEMVER-MINOR) add `fs.openAsBlob` to support File-backed Blobs (James M Snell) #45258
tls:
  * (SEMVER-MINOR) support automatic DHE (Tobias Nießen) #46978
url:
  * (SEMVER-MINOR) implement URLSearchParams size getter (James M Snell) #46308
wasi:
  * (SEMVER-MINOR) add support for version when creating WASI (Michael Dawson) #46469
worker:
  * (SEMVER-MINOR) add support for worker name in inspector and trace_events (Debadree Chatterjee) #46832

PR-URL: #47086
targos added a commit that referenced this pull request Mar 14, 2023
Notable changes:

buffer:
  * (SEMVER-MINOR) add Buffer.copyBytesFrom(...) (James M Snell) #46500
doc:
  * add marco-ippolito to collaborators (Marco Ippolito) #46816
events:
  * (SEMVER-MINOR) add listener argument to listenerCount (Paolo Insogna) #46523
lib:
  * (SEMVER-MINOR) add AsyncLocalStorage.bind() and .snapshot() (flakey5) #46387
src:
  * (SEMVER-MINOR) add `fs.openAsBlob` to support File-backed Blobs (James M Snell) #45258
tls:
  * (SEMVER-MINOR) support automatic DHE (Tobias Nießen) #46978
url:
  * (SEMVER-MINOR) implement URLSearchParams size getter (James M Snell) #46308
wasi:
  * (SEMVER-MINOR) add support for version when creating WASI (Michael Dawson) #46469
worker:
  * (SEMVER-MINOR) add support for worker name in inspector and trace_events (Debadree Chatterjee) #46832

PR-URL: #47087
targos added a commit that referenced this pull request Mar 14, 2023
Notable changes:

buffer:
  * (SEMVER-MINOR) add Buffer.copyBytesFrom(...) (James M Snell) #46500
doc:
  * add marco-ippolito to collaborators (Marco Ippolito) #46816
events:
  * (SEMVER-MINOR) add listener argument to listenerCount (Paolo Insogna) #46523
lib:
  * (SEMVER-MINOR) add AsyncLocalStorage.bind() and .snapshot() (flakey5) #46387
src:
  * (SEMVER-MINOR) add `fs.openAsBlob` to support File-backed Blobs (James M Snell) #45258
tls:
  * (SEMVER-MINOR) support automatic DHE (Tobias Nießen) #46978
url:
  * (SEMVER-MINOR) implement URLSearchParams size getter (James M Snell) #46308
wasi:
  * (SEMVER-MINOR) add support for version when creating WASI (Michael Dawson) #46469
worker:
  * (SEMVER-MINOR) add support for worker name in inspector and trace_events (Debadree Chatterjee) #46832

PR-URL: #47087
targos added a commit that referenced this pull request Mar 14, 2023
Notable changes:

buffer:
  * (SEMVER-MINOR) add Buffer.copyBytesFrom(...) (James M Snell) #46500
doc:
  * add marco-ippolito to collaborators (Marco Ippolito) #46816
events:
  * (SEMVER-MINOR) add listener argument to listenerCount (Paolo Insogna) #46523
lib:
  * (SEMVER-MINOR) add AsyncLocalStorage.bind() and .snapshot() (flakey5) #46387
src:
  * (SEMVER-MINOR) add `fs.openAsBlob` to support File-backed Blobs (James M Snell) #45258
tls:
  * (SEMVER-MINOR) support automatic DHE (Tobias Nießen) #46978
url:
  * (SEMVER-MINOR) implement URLSearchParams size getter (James M Snell) #46308
wasi:
  * (SEMVER-MINOR) add support for version when creating WASI (Michael Dawson) #46469
worker:
  * (SEMVER-MINOR) add support for worker name in inspector and trace_events (Debadree Chatterjee) #46832

PR-URL: #47087
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-requested-v18.x PRs awaiting manual backport to the v18.x-staging branch. build Issues and PRs related to build files or the CI. c++ Issues and PRs that require attention from people who are familiar with C++. experimental Issues and PRs related to experimental features. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants