From 2caa795139eb012acf6f1b1d27305164930b2c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arpad=20M=C3=BCller?= Date: Tue, 7 Nov 2023 09:52:00 +0100 Subject: [PATCH] Do a linear search after half of the range This way we are more likely ensured to have an exponential pivot, preventing the case where we get arbitrarily close to the high multiple times in a row. --- libs/utils/src/exp_counter.rs | 6 ++++++ pageserver/src/pgdatadir_mapping.rs | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/libs/utils/src/exp_counter.rs b/libs/utils/src/exp_counter.rs index 89a59a9c45..161ac951cb 100644 --- a/libs/utils/src/exp_counter.rs +++ b/libs/utils/src/exp_counter.rs @@ -32,6 +32,12 @@ pub struct ExpCounter { } impl ExpCounter { + /// Creates an iterator that uses `ExpCounter` for the first half of the + /// range and a linear range counter for the second half. + pub fn with_max_and_linear_search(max: u64) -> impl Iterator { + let linear_start = max / 2; + ExpCounter::with_max(linear_start).chain(linear_start..max) + } /// Creates a new `ExpCounter` instance that counts to the (exclusive) maximum pub fn with_max(max: u64) -> Self { Self { diff --git a/pageserver/src/pgdatadir_mapping.rs b/pageserver/src/pgdatadir_mapping.rs index 3b29f2d2cf..938672a908 100644 --- a/pageserver/src/pgdatadir_mapping.rs +++ b/pageserver/src/pgdatadir_mapping.rs @@ -370,7 +370,7 @@ impl Timeline { // We don't do an explosive search as we want to prove that // every single lsn up to high is giving inconclusive results. // This can only be done by trying all lsns. - for offs in ExpCounter::with_max(high - mid_start) { + for offs in ExpCounter::with_max_and_linear_search(high - mid_start) { mid = offs + mid_start; // Do the query for mid + 1 instead of mid so that we can make definite statements