diff --git a/README.md b/README.md index d2d3c37..c9b5918 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ for await (const object of io.objects()) { An operation uses the namespace, locator key and read snapshot of its `IoContext` when it runs on the libuv thread pool, not when it is called. `setNamespace()`, `setLocatorKey()` and `setReadSnapshot()` therefore throw a `CephError` with `errno` 16 (`EBUSY`) while operations are in flight, a watch is registered or an `objects()` listing is open on the `IoContext`. Use one `IoContext` per namespace when they are used at the same time. +Data is not copied on its way to or from Ceph. A `Buffer` passed to `write()`, `writeFull()`, `append()` or `RbdImage.write()` is used in place until the returned promise settles, so like with `fs.write()` it must not be changed before then. A read returns a `Buffer` over the memory librados read into. + Administrative commands are deliberately marked unsafe because their JSON schema and authorization are controlled by the connected Ceph release: ```ts diff --git a/src/common.h b/src/common.h index 0f27b80..1ee2b03 100644 --- a/src/common.h +++ b/src/common.h @@ -92,6 +92,56 @@ std::shared_ptr in_flight(const std::shared_ptr& state) { return std::shared_ptr(state.get(), [state](State*) { state->end_op(); }); } +// The bytes of a Buffer argument, for an operation on the thread pool, without copying them. The Buffer is +// referenced until the operation's worker is deleted on the main thread. As with fs.write(), the caller must +// not change or transfer the Buffer before the operation settles +class BufferView { + public: + BufferView(const Napi::CallbackInfo& info, size_t index, const char* name) { + if (info.Length() <= index || !info[index].IsBuffer()) + throw Napi::TypeError::New(info.Env(), std::string(name) + " must be a Buffer"); + auto buffer = info[index].As>(); + data_ = reinterpret_cast(buffer.Data()); + size_ = buffer.Length(); + ref_ = std::make_shared>>(Napi::Persistent(buffer)); + } + const char* data() const { return data_; } + size_t size() const { return size_; } + + private: + const char* data_{}; + size_t size_{}; + std::shared_ptr>> ref_; +}; + +// Bytes read on the thread pool into memory that becomes the Buffer handed to JavaScript, instead of being +// copied into one on the main thread +struct ReadBytes { + std::unique_ptr data; + size_t capacity{}; + size_t size{}; + + char* allocate(size_t length) { + data.reset(new uint8_t[length ? length : 1]); + capacity = length; + size = 0; + return reinterpret_cast(data.get()); + } + + Napi::Value to_buffer(Napi::Env env) { + // a short read would keep the whole allocation alive with the Buffer, so it is copied instead + if (size < capacity / 2) return Napi::Buffer::Copy(env, data.get(), size); + const int64_t external = static_cast(capacity); + auto buffer = Napi::Buffer::NewOrCopy(env, data.release(), size, [external](Napi::BasicEnv env, uint8_t* bytes) { + delete[] bytes; + Napi::MemoryManagement::AdjustExternalMemory(env, -external); + }); + // the garbage collector does not see memory it did not allocate unless it is told + Napi::MemoryManagement::AdjustExternalMemory(env, external); + return buffer; + } +}; + using ClusterState = HandleState; using IoState = HandleState; using ImageState = HandleState; diff --git a/src/io_context.cpp b/src/io_context.cpp index dace767..c95ff7a 100644 --- a/src/io_context.cpp +++ b/src/io_context.cpp @@ -128,20 +128,20 @@ Napi::Value IoContextWrap::RequiresAlignment(const Napi::CallbackInfo& info) { } Napi::Value IoContextWrap::Write(const Napi::CallbackInfo& info) { - auto oid = string_arg(info, 0, "oid"); auto data = buffer_arg(info, 1, "data"); uint64_t offset = uint64_arg(info[2], "offset"); auto state = in_flight(state_); - return async_void(info.Env(), "rados_write", oid, [state, oid, data, offset] { return state->use([&](rados_ioctx_t h) { return rados_write(h, oid.c_str(), reinterpret_cast(data.data()), data.size(), offset); }); }); + auto oid = string_arg(info, 0, "oid"); BufferView data(info, 1, "data"); uint64_t offset = uint64_arg(info[2], "offset"); auto state = in_flight(state_); + return async_void(info.Env(), "rados_write", oid, [state, oid, data, offset] { return state->use([&](rados_ioctx_t h) { return rados_write(h, oid.c_str(), data.data(), data.size(), offset); }); }); } Napi::Value IoContextWrap::WriteFull(const Napi::CallbackInfo& info) { - auto oid = string_arg(info, 0, "oid"); auto data = buffer_arg(info, 1, "data"); auto state = in_flight(state_); - return async_void(info.Env(), "rados_write_full", oid, [state, oid, data] { return state->use([&](rados_ioctx_t h) { return rados_write_full(h, oid.c_str(), reinterpret_cast(data.data()), data.size()); }); }); + auto oid = string_arg(info, 0, "oid"); BufferView data(info, 1, "data"); auto state = in_flight(state_); + return async_void(info.Env(), "rados_write_full", oid, [state, oid, data] { return state->use([&](rados_ioctx_t h) { return rados_write_full(h, oid.c_str(), data.data(), data.size()); }); }); } Napi::Value IoContextWrap::Append(const Napi::CallbackInfo& info) { - auto oid = string_arg(info, 0, "oid"); auto data = buffer_arg(info, 1, "data"); auto state = in_flight(state_); - return async_void(info.Env(), "rados_append", oid, [state, oid, data] { return state->use([&](rados_ioctx_t h) { return rados_append(h, oid.c_str(), reinterpret_cast(data.data()), data.size()); }); }); + auto oid = string_arg(info, 0, "oid"); BufferView data(info, 1, "data"); auto state = in_flight(state_); + return async_void(info.Env(), "rados_append", oid, [state, oid, data] { return state->use([&](rados_ioctx_t h) { return rados_append(h, oid.c_str(), data.data(), data.size()); }); }); } Napi::Value IoContextWrap::Read(const Napi::CallbackInfo& info) { auto oid = string_arg(info, 0, "oid"); size_t length = static_cast(uint64_arg(info[1], "length")); uint64_t offset = uint64_arg(info[2], "offset"); auto state = in_flight(state_); - return async_value(info.Env(), "rados_read", oid, [state, oid, length, offset](Bytes& out) { out.resize(length); int rc = state->use([&](rados_ioctx_t h) { return rados_read(h, oid.c_str(), reinterpret_cast(out.data()), out.size(), offset); }); if (rc >= 0) out.resize(static_cast(rc)); return rc < 0 ? rc : 0; }, [](Napi::Env env, Bytes& out) { return Napi::Buffer::Copy(env, out.data(), out.size()); }); + return async_value(info.Env(), "rados_read", oid, [state, oid, length, offset](ReadBytes& out) { char* buffer = out.allocate(length); int rc = state->use([&](rados_ioctx_t h) { return rados_read(h, oid.c_str(), buffer, length, offset); }); if (rc >= 0) out.size = static_cast(rc); return rc < 0 ? rc : 0; }, [](Napi::Env env, ReadBytes& out) { return out.to_buffer(env); }); } Napi::Value IoContextWrap::Remove(const Napi::CallbackInfo& info) { auto oid = string_arg(info, 0, "oid"); auto state = in_flight(state_); return async_void(info.Env(), "rados_remove", oid, [state, oid] { return state->use([&](rados_ioctx_t h) { return rados_remove(h, oid.c_str()); }); }); diff --git a/src/rbd.cpp b/src/rbd.cpp index 8535bc4..ee6c25d 100644 --- a/src/rbd.cpp +++ b/src/rbd.cpp @@ -10,18 +10,12 @@ Napi::FunctionReference RbdPoolWrap::constructor; Napi::FunctionReference RbdImageWrap::constructor; namespace { -using Bytes = std::vector; struct ImageSpec { std::string id; std::string name; }; struct OpenImage { std::shared_ptr state; std::string name; }; struct ImageStat { uint64_t size{}; uint64_t object_size{}; uint64_t objects{}; int order{}; }; struct Snap { uint64_t id{}; uint64_t size{}; std::string name; }; using StringMap = std::vector>; -Bytes data_arg(const Napi::CallbackInfo& info, size_t index, const char* name) { - if (info.Length() <= index || !info[index].IsBuffer()) throw Napi::TypeError::New(info.Env(), std::string(name) + " must be a Buffer"); - auto b = info[index].As>(); return {b.Data(), b.Data() + b.Length()}; -} - std::vector nul_strings(const char* data, size_t length) { std::vector out; for (const char* p = data; p < data + length && *p; p += std::strlen(p) + 1) out.emplace_back(p); @@ -146,8 +140,8 @@ Napi::Value RbdImageWrap::Size(const Napi::CallbackInfo& info) { auto image = im Napi::Value RbdImageWrap::Resize(const Napi::CallbackInfo& info) { uint64_t size = uint64_arg(info[0], "size"); bool shrink = info.Length() > 1 && info[1].ToBoolean(); auto image = image_; return async_void(info.Env(), "rbd_resize2", name_, [image, size, shrink] { return image->use([&](rbd_image_t h) { return rbd_resize2(h, size, shrink, ignore_progress, nullptr); }); }); } Napi::Value RbdImageWrap::Features(const Napi::CallbackInfo& info) { auto image = image_; return async_value(info.Env(), "rbd_get_features", name_, [image](uint64_t& out) { return image->use([&](rbd_image_t h) { return rbd_get_features(h, &out); }); }, [](Napi::Env env, uint64_t& out) { return Napi::BigInt::New(env, out); }); } Napi::Value RbdImageWrap::Flags(const Napi::CallbackInfo& info) { auto image = image_; return async_value(info.Env(), "rbd_get_flags", name_, [image](uint64_t& out) { return image->use([&](rbd_image_t h) { return rbd_get_flags(h, &out); }); }, [](Napi::Env env, uint64_t& out) { return Napi::BigInt::New(env, out); }); } -Napi::Value RbdImageWrap::Read(const Napi::CallbackInfo& info) { uint64_t offset = uint64_arg(info[0], "offset"); size_t length = static_cast(uint64_arg(info[1], "length")); int flags = info.Length() > 2 ? static_cast(int64_arg(info[2], "flags")) : 0; auto image = image_; return async_value(info.Env(), "rbd_read2", name_, [image, offset, length, flags](Bytes& out) { out.resize(length); ssize_t rc = image->use([&](rbd_image_t h) { return rbd_read2(h, offset, length, reinterpret_cast(out.data()), flags); }); if (rc >= 0) out.resize(static_cast(rc)); return rc < 0 ? static_cast(rc) : 0; }, [](Napi::Env env, Bytes& out) { return Napi::Buffer::Copy(env, out.data(), out.size()); }); } -Napi::Value RbdImageWrap::Write(const Napi::CallbackInfo& info) { uint64_t offset = uint64_arg(info[0], "offset"); auto data = data_arg(info, 1, "data"); int flags = info.Length() > 2 ? static_cast(int64_arg(info[2], "flags")) : 0; auto image = image_; return async_void(info.Env(), "rbd_write2", name_, [image, offset, data, flags] { ssize_t rc = image->use([&](rbd_image_t h) { return rbd_write2(h, offset, data.size(), reinterpret_cast(data.data()), flags); }); return rc < 0 ? static_cast(rc) : 0; }); } +Napi::Value RbdImageWrap::Read(const Napi::CallbackInfo& info) { uint64_t offset = uint64_arg(info[0], "offset"); size_t length = static_cast(uint64_arg(info[1], "length")); int flags = info.Length() > 2 ? static_cast(int64_arg(info[2], "flags")) : 0; auto image = image_; return async_value(info.Env(), "rbd_read2", name_, [image, offset, length, flags](ReadBytes& out) { char* buffer = out.allocate(length); ssize_t rc = image->use([&](rbd_image_t h) { return rbd_read2(h, offset, length, buffer, flags); }); if (rc >= 0) out.size = static_cast(rc); return rc < 0 ? static_cast(rc) : 0; }, [](Napi::Env env, ReadBytes& out) { return out.to_buffer(env); }); } +Napi::Value RbdImageWrap::Write(const Napi::CallbackInfo& info) { uint64_t offset = uint64_arg(info[0], "offset"); BufferView data(info, 1, "data"); int flags = info.Length() > 2 ? static_cast(int64_arg(info[2], "flags")) : 0; auto image = image_; return async_void(info.Env(), "rbd_write2", name_, [image, offset, data, flags] { ssize_t rc = image->use([&](rbd_image_t h) { return rbd_write2(h, offset, data.size(), data.data(), flags); }); return rc < 0 ? static_cast(rc) : 0; }); } Napi::Value RbdImageWrap::Discard(const Napi::CallbackInfo& info) { uint64_t offset = uint64_arg(info[0], "offset"), length = uint64_arg(info[1], "length"); auto image = image_; return async_void(info.Env(), "rbd_discard", name_, [image, offset, length] { return image->use([&](rbd_image_t h) { return rbd_discard(h, offset, length); }); }); } Napi::Value RbdImageWrap::Flush(const Napi::CallbackInfo& info) { auto image = image_; return async_void(info.Env(), "rbd_flush", name_, [image] { return image->use([](rbd_image_t h) { return rbd_flush(h); }); }); } Napi::Value RbdImageWrap::InvalidateCache(const Napi::CallbackInfo& info) { auto image = image_; return async_void(info.Env(), "rbd_invalidate_cache", name_, [image] { return image->use([](rbd_image_t h) { return rbd_invalidate_cache(h); }); }); } diff --git a/tests/integration/rados-rbd.test.ts b/tests/integration/rados-rbd.test.ts index 4eb7509..edfe7c3 100644 --- a/tests/integration/rados-rbd.test.ts +++ b/tests/integration/rados-rbd.test.ts @@ -82,6 +82,16 @@ test("lists objects a page at a time", {skip: !enabled}, async () => { for (const name of names) await io.remove(name); }); +test("large writes and reads, and reads past the end", {skip: !enabled}, async () => { + await using io = cluster.ioContext(pool); + const payload = Buffer.alloc(4 * 1024 * 1024 + 3, 7); + await io.writeFull("zero-copy", payload); + assert.deepEqual(await io.read("zero-copy", BigInt(payload.length)), payload); + const tail = await io.read("zero-copy", 1024n * 1024n, BigInt(payload.length - 3)); + assert.deepEqual(tail, Buffer.from([7, 7, 7])); + await io.remove("zero-copy"); +}); + test("RBD image, I/O, metadata, snapshots, resize, and trash", {skip: !enabled}, async () => { await using io = cluster.ioContext(pool); const rbd = io.rbd();