Skip to content

Commit 304d2f1

Browse files
committed
default_policy: introduce and respect ReplicaOrder
For LWT optimisation, we want to request replicas to be given in the ring order. For other cases (non-LWT requests) the order: - either does not matter (can be arbitrary), - or it is required to be random, but then the replicas are to be shuffled anyway, so before shuffling they can be given, again, in arbitrary order. This leads to introducing the enum ReplicaOrder with Arbitrary and RingOrder variants.
1 parent 920e6df commit 304d2f1

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

scylla/src/transport/load_balancing/default.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ impl ReplicaLocationPreference {
5353
}
5454
}
5555

56+
#[derive(Clone, Copy)]
57+
enum ReplicaOrder {
58+
Arbitrary,
59+
RingOrder,
60+
}
61+
5662
// TODO: LWT optimisation
5763
/// The default load balancing policy.
5864
///
@@ -386,12 +392,22 @@ impl DefaultPolicy {
386392
replica_location: ReplicaLocationCriteria<'a>,
387393
predicate: impl Fn(&NodeRef<'a>) -> bool + 'a,
388394
cluster: &'a ClusterData,
395+
order: ReplicaOrder,
389396
) -> impl Iterator<Item = NodeRef<'a>> {
390397
let predicate = Self::make_rack_predicate(predicate, replica_location);
391398

392-
self.nonfiltered_replica_set(ts, replica_location, cluster)
393-
.into_iter()
394-
.filter(move |node: &NodeRef<'a>| predicate(node))
399+
let replica_iter = match order {
400+
ReplicaOrder::Arbitrary => Either::Left(
401+
self.nonfiltered_replica_set(ts, replica_location, cluster)
402+
.into_iter(),
403+
),
404+
ReplicaOrder::RingOrder => Either::Right(
405+
self.nonfiltered_replica_set(ts, replica_location, cluster)
406+
.into_replicas_ordered()
407+
.into_iter(),
408+
),
409+
};
410+
replica_iter.filter(move |node: &NodeRef<'a>| predicate(node))
395411
}
396412

397413
fn pick_replica<'a>(
@@ -420,7 +436,13 @@ impl DefaultPolicy {
420436
predicate: impl Fn(&NodeRef<'a>) -> bool + 'a,
421437
cluster: &'a ClusterData,
422438
) -> impl Iterator<Item = NodeRef<'a>> {
423-
let replicas = self.replicas(ts, replica_location, predicate, cluster);
439+
let replicas = self.replicas(
440+
ts,
441+
replica_location,
442+
predicate,
443+
cluster,
444+
ReplicaOrder::Arbitrary,
445+
);
424446

425447
self.shuffle(replicas)
426448
}

0 commit comments

Comments
 (0)