@@ -604,9 +604,27 @@ seastar::future<> level_zero_gc::worker() {
604604
605605seastar::future<std::expected<size_t , level_zero_gc::collection_error>>
606606level_zero_gc::try_to_collect () {
607- if (!delete_worker_->has_capacity ()) {
608- co_return 0 ;
607+ // Ultra-temporary cache to avoid repeatedly querying for max gc-able epoch.
608+ // Since the result will always be valid clusterwide, compute exactly once
609+ // per collection loop.
610+ std::optional<cluster_epoch> max_gc_epoch;
611+ size_t total_eligible{0 };
612+ while (delete_worker_->has_capacity ()) {
613+ auto res = co_await do_try_to_collect (std::ref (max_gc_epoch));
614+ if (!res.has_value ()) {
615+ co_return res;
616+ }
617+ if (res.value () == 0 ) {
618+ break ;
619+ }
620+ total_eligible += res.value ();
609621 }
622+
623+ co_return total_eligible;
624+ }
625+
626+ seastar::future<std::expected<size_t , level_zero_gc::collection_error>>
627+ level_zero_gc::do_try_to_collect (std::optional<cluster_epoch>& max_gc_epoch) {
610628 auto candidate_objects = co_await delete_worker_->next_page ();
611629 if (!candidate_objects.has_value ()) {
612630 vlog (
@@ -616,17 +634,19 @@ level_zero_gc::try_to_collect() {
616634 co_return std::unexpected (collection_error::service_error);
617635 }
618636
619- const auto maybe_max_gc_epoch
620- = co_await epoch_source_->max_gc_eligible_epoch (&asrc_);
621- if (!maybe_max_gc_epoch.has_value ()) {
622- vlog (
623- cd_log.debug ,
624- " Received error retrieving GC eligible epoch: {}" ,
625- maybe_max_gc_epoch.error ());
626- co_return std::unexpected (collection_error::service_error);
637+ if (!max_gc_epoch.has_value ()) {
638+ const auto maybe_max_gc_epoch
639+ = co_await epoch_source_->max_gc_eligible_epoch (&asrc_);
640+ if (!maybe_max_gc_epoch.has_value ()) {
641+ vlog (
642+ cd_log.debug ,
643+ " Received error retrieving GC eligible epoch: {}" ,
644+ maybe_max_gc_epoch.error ());
645+ co_return std::unexpected (collection_error::service_error);
646+ }
647+ max_gc_epoch = maybe_max_gc_epoch.value ();
627648 }
628649
629- const auto max_gc_epoch = maybe_max_gc_epoch.value ();
630650 if (!max_gc_epoch.has_value ()) {
631651 vlog (cd_log.info , " No GC eligible epoch currently exists" );
632652 co_return std::unexpected (collection_error::no_collectible_epoch);
0 commit comments