[PATCH] libceph: don't assume the primary leads the acting set for replica reads

From: Xiubo Li via B4 Relay

Date: Mon Nov 30 2026 - 21:18:14 EST


From: Xiubo Li <xiubo.li@xxxxxxxxx>

A primary_temp ("ceph osd primary-temp") names the primary of a PG
without moving it to the front of the acting set: get_temp_osds()
takes it as is, like OSDMap::_get_temp_osds() does. calc_target()
however assumes that acting.osds[0] is the primary when it picks a
replica for a balanced or localized read:

- it warns, every time the target is recalculated, when that is not
the case;
- it derives used_replica from the index, so a read sent to
acting.osds[0], which then is a replica, is not marked as one, and
the EAGAIN that replica may return trips the WARN_ON in
handle_reply();
- pick_closest_replica() breaks ties in favour of acting.osds[0]
instead of the primary, against its own comment.

Set used_replica by comparing the chosen OSD with the primary, and
start the locality search from the primary.

Tested with read_from_replica=balance against a 3-OSD cluster with a
primary_temp on 8 PGs of the data pool, each naming the second OSD of
the acting set, writing a file and reading it back after dropping the
page cache:

+-------------------------------+-----------+-----------+
| | before | after |
+-------------------------------+-----------+-----------+
| WARN_ON in calc_target() | 52 | 0 |
| data read back matches | yes | yes |
| misdirected ops in OSD logs | 0 | 0 |
+-------------------------------+-----------+-----------+

Without a primary_temp the target does not change. An IO500 run with
read_from_replica=balance on the same cluster (16 processes over 4
mounts, one run each) is within the spread seen between repeated runs:

+-------------------------+-----------+-----------+
| | before | after |
+-------------------------+-----------+-----------+
| ior-easy-read (GiB/s) | 5.955 | 6.253 |
| ior-hard-read (GiB/s) | 3.900 | 3.876 |
| mdtest-hard-read (kIOPS)| 19.180 | 19.228 |
| mdtest-easy-stat (kIOPS)| 31.809 | 31.311 |
| score | 5.197 | 5.272 |
+-------------------------+-----------+-----------+

Fixes: 117d96a04f00 ("libceph: support for balanced and localized reads")
Signed-off-by: Xiubo Li <xiubo.li@xxxxxxxxx>
---
A primary_temp leaves the primary where it is in the acting set, and
balanced and localized reads then warn on every target calculation
and do not mark a read sent to acting.osds[0] as a replica read.
---
net/ceph/osd_client.c | 33 ++++++++++++++++++++++-----------
1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/net/ceph/osd_client.c b/net/ceph/osd_client.c
index 22a793a4bdc8..edb5c50045de 100644
--- a/net/ceph/osd_client.c
+++ b/net/ceph/osd_client.c
@@ -1549,27 +1549,38 @@ static int pick_random_replica(const struct ceph_osds *acting)
/*
* Picks the closest replica based on client's location given by
* crush_location option. Prefers the primary if the locality is
- * the same.
+ * the same. The primary need not be acting->osds[0]: a primary_temp
+ * names it without reordering the acting set.
*/
static int pick_closest_replica(struct ceph_osd_client *osdc,
const struct ceph_osds *acting)
{
struct ceph_options *opt = osdc->client->options;
- int best_i, best_locality;
- int i = 0, locality;
+ int best_i = 0, best_locality;
+ int i, locality;

- do {
+ for (i = 0; i < acting->size; i++) {
+ if (acting->osds[i] == acting->primary) {
+ best_i = i;
+ break;
+ }
+ }
+ best_locality = ceph_get_crush_locality(osdc->osdmap,
+ acting->osds[best_i],
+ &opt->crush_locs);
+
+ for (i = 0; i < acting->size; i++) {
+ if (i == best_i)
+ continue;
locality = ceph_get_crush_locality(osdc->osdmap,
acting->osds[i],
&opt->crush_locs);
- if (i == 0 ||
- (locality >= 0 && best_locality < 0) ||
- (locality >= 0 && best_locality >= 0 &&
- locality < best_locality)) {
+ if (locality >= 0 &&
+ (best_locality < 0 || locality < best_locality)) {
best_i = i;
best_locality = locality;
}
- } while (++i < acting->size);
+ }

dout("%s picked osd%d with locality %d, primary osd%d\n", __func__,
acting->osds[best_i], best_locality, acting->primary);
@@ -1692,14 +1703,14 @@ static enum calc_target_result calc_target(struct ceph_osd_client *osdc,
acting.size > 1) {
int pos;

- WARN_ON(!is_read || acting.osds[0] != acting.primary);
+ WARN_ON(!is_read);
if (t->flags & CEPH_OSD_FLAG_BALANCE_READS) {
pos = pick_random_replica(&acting);
} else {
pos = pick_closest_replica(osdc, &acting);
}
t->osd = acting.osds[pos];
- t->used_replica = pos > 0;
+ t->used_replica = t->osd != acting.primary;
} else {
t->osd = acting.primary;
t->used_replica = false;

---
base-commit: 3f90412e989cb381d4bf96260468f5c7b2d08655
change-id: 20261201-b4-libceph-replica-read-59fc718cba56

Best regards,
--
Xiubo Li <xiubo.li@xxxxxxxxx>