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 @@ -53,6 +53,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 or a watch is registered on the `IoContext`. Use one `IoContext` per namespace when they are used at the same time.

Administrative commands are deliberately marked unsafe because their JSON schema and authorization are controlled by the connected Ceph release:

```ts
Expand Down
24 changes: 24 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,21 @@ class HandleState {
return closed_;
}

// Runs fn with the handle while no operation can use it, for settings that every operation reads
template <typename Fn>
auto change(Fn&& fn) -> decltype(fn(std::declval<Handle>())) {
std::unique_lock lock(mutex_);
if (closed_ || !handle_) throw std::runtime_error("resource is closed");
return fn(handle_);
}

// Operations queued or running on the handle, and watches registered on it. An operation reads the
// namespace, locator key and read snapshot when it runs on the thread pool, not when it is called, so
// they may only change while there are none
void begin_op() { ops_.fetch_add(1); }
void end_op() { ops_.fetch_sub(1); }
uint32_t ops() const { return ops_.load(); }

void add_child() { children_.fetch_add(1); }
void remove_child() { children_.fetch_sub(1); }
int close_checked() {
Expand All @@ -65,8 +80,17 @@ class HandleState {
Close close_;
bool closed_{false};
std::atomic<uint32_t> children_{0};
std::atomic<uint32_t> ops_{0};
};

// A reference to state that counts as an operation in flight until its last copy is released, which for
// a queued operation is when its worker is deleted after completing
template <typename State>
std::shared_ptr<State> in_flight(const std::shared_ptr<State>& state) {
state->begin_op();
return std::shared_ptr<State>(state.get(), [state](State*) { state->end_op(); });
}

using ClusterState = HandleState<rados_t>;
using IoState = HandleState<rados_ioctx_t>;
using ImageState = HandleState<rbd_image_t>;
Expand Down
4 changes: 4 additions & 0 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct ObjectWatchWrap::Data {
int flush_result = cluster->use([](rados_t handle) { return rados_watch_flush(handle); });
callback.Release();
io->remove_child();
io->end_op();
return result < 0 ? result : flush_result;
}
};
Expand Down Expand Up @@ -101,6 +102,8 @@ Napi::Object ObjectWatchWrap::Create(Napi::Env env, std::shared_ptr<IoState> io,
data->oid = std::move(oid);
data->callback = Napi::ThreadSafeFunction::New(env, callback, "RadosObjectWatch", queue_size, 1);
data->io->add_child();
// acks go out in the io context's namespace, which may not change while the watch is registered
data->io->begin_op();
int result = data->io->use([&](rados_ioctx_t handle) {
return rados_watch3(handle, data->oid.c_str(), &data->cookie, object_watch_cb,
object_watch_error_cb, timeout, data.get());
Expand All @@ -109,6 +112,7 @@ Napi::Object ObjectWatchWrap::Create(Napi::Env env, std::shared_ptr<IoState> io,
data->closed = true;
data->callback.Release();
data->io->remove_child();
data->io->end_op();
throw_ceph(env, result, "rados_watch3", data->oid);
return Napi::Object::New(env);
}
Expand Down
Loading
Loading