Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 50 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,56 @@ std::shared_ptr<State> in_flight(const std::shared_ptr<State>& state) {
return std::shared_ptr<State>(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<Napi::Buffer<uint8_t>>();
data_ = reinterpret_cast<const char*>(buffer.Data());
size_ = buffer.Length();
ref_ = std::make_shared<Napi::Reference<Napi::Buffer<uint8_t>>>(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<Napi::Reference<Napi::Buffer<uint8_t>>> 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<uint8_t[]> 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<char*>(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<uint8_t>::Copy(env, data.get(), size);
const int64_t external = static_cast<int64_t>(capacity);
auto buffer = Napi::Buffer<uint8_t>::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<rados_t>;
using IoState = HandleState<rados_ioctx_t>;
using ImageState = HandleState<rbd_image_t>;
Expand Down
14 changes: 7 additions & 7 deletions src/io_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>(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<const char*>(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<const char*>(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<size_t>(uint64_arg(info[1], "length")); uint64_t offset = uint64_arg(info[2], "offset"); auto state = in_flight(state_);
return async_value<Bytes>(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<char*>(out.data()), out.size(), offset); }); if (rc >= 0) out.resize(static_cast<size_t>(rc)); return rc < 0 ? rc : 0; }, [](Napi::Env env, Bytes& out) { return Napi::Buffer<uint8_t>::Copy(env, out.data(), out.size()); });
return async_value<ReadBytes>(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<size_t>(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()); }); });
Expand Down
10 changes: 2 additions & 8 deletions src/rbd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ Napi::FunctionReference RbdPoolWrap::constructor;
Napi::FunctionReference RbdImageWrap::constructor;

namespace {
using Bytes = std::vector<uint8_t>;
struct ImageSpec { std::string id; std::string name; };
struct OpenImage { std::shared_ptr<ImageState> 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<std::pair<std::string, std::string>>;

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<Napi::Buffer<uint8_t>>(); return {b.Data(), b.Data() + b.Length()};
}

std::vector<std::string> nul_strings(const char* data, size_t length) {
std::vector<std::string> out;
for (const char* p = data; p < data + length && *p; p += std::strlen(p) + 1) out.emplace_back(p);
Expand Down Expand Up @@ -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<uint64_t>(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<uint64_t>(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<size_t>(uint64_arg(info[1], "length")); int flags = info.Length() > 2 ? static_cast<int>(int64_arg(info[2], "flags")) : 0; auto image = image_; return async_value<Bytes>(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<char*>(out.data()), flags); }); if (rc >= 0) out.resize(static_cast<size_t>(rc)); return rc < 0 ? static_cast<int>(rc) : 0; }, [](Napi::Env env, Bytes& out) { return Napi::Buffer<uint8_t>::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<int>(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<const char*>(data.data()), flags); }); return rc < 0 ? static_cast<int>(rc) : 0; }); }
Napi::Value RbdImageWrap::Read(const Napi::CallbackInfo& info) { uint64_t offset = uint64_arg(info[0], "offset"); size_t length = static_cast<size_t>(uint64_arg(info[1], "length")); int flags = info.Length() > 2 ? static_cast<int>(int64_arg(info[2], "flags")) : 0; auto image = image_; return async_value<ReadBytes>(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<size_t>(rc); return rc < 0 ? static_cast<int>(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<int>(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<int>(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); }); }); }
Expand Down
10 changes: 10 additions & 0 deletions tests/integration/rados-rbd.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading