Skip to content

feat(rados): compound write operations - #4

Merged
AlexanderSlaa merged 2 commits into
SourceRegistry:mainfrom
wido:feat/write-op
Nov 21, 2026
Merged

AlexanderSlaa merged 2 commits into
SourceRegistry:mainfrom
wido:feat/write-op

Conversation

@wido

@wido wido commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

This adds IoContext.writeOp(oid), a builder for a librados write operation. All of its steps apply to the object atomically, in one OSD operation.

Without it, writing an object's data together with its xattrs takes several calls that each can fail on their own. "Create only if it doesn't exist" also needs a stat and then a write, which races with other writers.

await io.writeOp("payload")
  .create({exclusive: true})
  .writeFull(data)
  .setXattr("checksum", checksum)
  .operate();

await io.writeOp("payload")
  .compareXattr("state", "eq", Buffer.from("pending"))
  .setXattr("state", Buffer.from("done"))
  .operate();

Supported steps: assertExists, assertVersion, compareXattr, create (exclusive or idempotent), write, writeFull, append, truncate, zero, remove, setXattr, removeXattr, setOmap and removeOmapKeys.

A failed check fails the whole operation before anything is written:

  • create({exclusive: true}) fails with EEXIST;
  • assertExists() fails with ENOENT;
  • assertVersion() fails with ERANGE;
  • compareXattr() fails with ECANCELED.

The integration test checks both that all steps apply and that none do. I confirmed the error codes against a Ceph 21 cluster. The new librados symbols are added to coverage/api.json.

@AlexanderSlaa AlexanderSlaa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for your contribution!

Most of it verified:

  • data steps combine in order (writeFull, then write at offset 0, append, truncate, zero);
  • create({exclusive: false}) works on an existing object;
  • a failed check leaves data, xattrs and omap untouched;
  • assertExists fails with ENOENT and creates nothing;
  • assertVersion passes on the current version and fails with ERANGE on an older one;
  • remove works inside an operation, and an operation with no steps is a no-op;
  • a builder can be operated twice, and later steps don't leak into an earlier operate();
  • invalid steps throw TypeError synchronously.

Two issues:

1. compareXattr compares the other way round from how it reads. With the stored xattr "m":

call result
compareXattr("v", "gt", "a") ECANCELED
compareXattr("v", "gt", "z") passes
compareXattr("v", "lt", "a") passes

librados evaluates value OP stored, but compareXattr(name, "gt", value) reads as "stored > value". A guard like .compareXattr("version", "lt", Buffer.from("5")) would check the opposite of what it says. I'd suggest swapping gt/lt and gte/lte in the comparisons map so the API means stored OP value, or else documenting it clearly. Either way, it's worth settling before this becomes public API.

2. assertVersion with a version newer than the object's fails with EOVERFLOW (75), not ERANGE. The doc comment, README and PR description only mention ERANGE.

Merge note (with #2): after #2, this merges with conflicts only in the README and tests, and WriteOp loses the in-flight guard. I built them together, and setNamespace() was allowed while writeOps were queued. Please use in_flight(state_) in WriteOp when rebasing.

wido and others added 2 commits November 21, 2026 22:44
IoContext.writeOp(oid) builds a librados write operation whose steps are
applied to the object atomically by one OSD operation: all of them take
effect or none does. Supported steps: assertExists, assertVersion,
compareXattr, create (exclusive or idempotent), write, writeFull, append,
truncate, zero, remove, setXattr, removeXattr, setOmap and removeOmapKeys.

Without it, writing an object's data and its xattrs takes several calls, and
"create only if absent" needs a stat followed by a write, which races with
other writers. Now that is one call:

  await io.writeOp(oid).create({exclusive: true}).writeFull(data)
    .setXattr("checksum", sum).operate();

A failed check fails the whole operation before anything is written: EEXIST,
ENOENT, ERANGE (assertVersion) or ECANCELED (compareXattr).
- compareXattr(name, "gt", value) passed when value was greater than the
  stored xattr, because the OSD evaluates "value OP stored". The ordering
  operators are now mirrored, so the call means "stored OP value".
- assertVersion() fails with EOVERFLOW, not ERANGE, when the object is
  older than the asserted version; documented both.
- writeOp() counts as an operation in flight, so setNamespace() and the
  other setters refuse with EBUSY while one is queued.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AlexanderSlaa
AlexanderSlaa merged commit e16926b into SourceRegistry:main Nov 21, 2026
9 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 21, 2026
# [1.2.0](v1.1.2...v1.2.0) (2026-11-21)

### Features

* **rados:** compound write operations ([#4](#4)) ([e16926b](e16926b))
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@AlexanderSlaa

AlexanderSlaa commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for your contribution! Merged.

I rebased this onto main (after #2, #6 and #3) and added one commit on top for the points from my review, ef350a1 fix(rados): compare stored xattrs as written and guard writeOp:

  • compareXattr direction: the ordering operators are mirrored in the comparisons map. compareXattr(name, "gt", value) now passes when the stored xattr is greater than value, which is how the call reads. eq and ne are unchanged.
  • assertVersion errors: the doc comment now says ERANGE when the object is newer and EOVERFLOW when it is older.
  • In-flight guard: writeOp() uses in_flight(state_), so the setters refuse with EBUSY while one is queued, as with every other operation since fix(rados): refuse io context setting changes while operations are in flight #2.
  • Tests: one new integration test covers all three: each comparison against a stored value, both assertVersion errnos, and EBUSY while writeOps are queued.

Your commit is unchanged apart from the conflict resolution: writeOp sits next to OpenObjectList from #6, and the README and tests keep both sides.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants