Skip to content

refactor: Migrate to folly::available_concurrency#27326

Merged
amitkdutta merged 1 commit intoprestodb:masterfrom
yfeldblum:misc-migrate-folly-available-concurrency
Mar 16, 2026
Merged

refactor: Migrate to folly::available_concurrency#27326
amitkdutta merged 1 commit intoprestodb:masterfrom
yfeldblum:misc-migrate-folly-available-concurrency

Conversation

@yfeldblum
Copy link
Copy Markdown
Contributor

@yfeldblum yfeldblum commented Mar 13, 2026

The name hardware_concurrency, while parallel to std::thread::hardware_concurrency, may be misleading. Migrate to the name available_concurrency.

== NO RELEASE NOTE ==

Summary by Sourcery

Standardize concurrency detection on folly::available_concurrency across the Presto C++ server, configuration utilities, and related tests.

Enhancements:

  • Replace uses of folly::hardware_concurrency with folly::available_concurrency in server initialization, overload checks, and connector thread pool sizing.
  • Update the hardwareConcurrency helper to wrap folly::available_concurrency with validation to ensure a non-zero result.

Tests:

  • Adjust existing configuration and server operation tests to assert against folly::available_concurrency-based values.
  • Update trace tool replayer tests to construct thread pool executors using folly::available_concurrency.

The name `hardware_concurrency`, while parallel to `std::thread::hardware_concurrency`, may be misleading. Migrate to the name `available_concurrency`.
@yfeldblum yfeldblum requested review from a team as code owners March 13, 2026 12:31
@sourcery-ai
Copy link
Copy Markdown
Contributor

sourcery-ai bot commented Mar 13, 2026

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Refactors the server and tests to use folly::available_concurrency() instead of folly::hardware_concurrency() everywhere, keeping configuration and thread-pool sizing logic otherwise identical.

Class diagram for usage of folly_available_concurrency in PrestoServer and Configs

classDiagram
  class PrestoServer {
    +void initializeThreadPools()
    +void checkOverload()
    +protocol_NodeStatus fetchNodeStatus()
  }

  class Configs {
    +static uint32_t hardwareConcurrency()
  }

  class FollyRuntime {
    +static size_t available_concurrency()
  }

  PrestoServer ..> FollyRuntime : uses_available_concurrency
  Configs ..> FollyRuntime : uses_available_concurrency
  PrestoServer ..> Configs : may_call_hardwareConcurrency
Loading

Flow diagram for thread pool sizing using folly_available_concurrency

flowchart TD
  Start([Start initializeThreadPools]) --> GetConfig[Get SystemConfig_instance]
  GetConfig --> GetHw[hwConcurrency = folly_available_concurrency]
  GetHw --> DriverCpu["Compute numDriverCpuThreads = max(driverMultiplier * hwConcurrency, 1)"]
  DriverCpu --> ExchangeIo["Compute numExchangeHttpClientIoThreads = max(exchangeIoMultiplier * folly_available_concurrency, 1)"]
  ExchangeIo --> ExchangeCpu["Compute numExchangeHttpClientCpuThreads = max(exchangeCpuMultiplier * folly_available_concurrency, 1)"]
  ExchangeCpu --> ConnectorCpu["Compute numConnectorCpuThreads = max(connectorCpuMultiplier * folly_available_concurrency, 0)"]
  ConnectorCpu --> ConnectorIo["Compute numConnectorIoThreads = max(connectorIoMultiplier * folly_available_concurrency, 0)"]
  ConnectorIo --> End([Create thread pool executors with computed sizes])
Loading

File-Level Changes

Change Details Files
Use folly::available_concurrency() instead of folly::hardware_concurrency() for thread-pool sizing, overload checks, configuration defaults, and tests.
  • Update PrestoServer thread-pool and overload-threshold calculations to call folly::available_concurrency() while preserving existing multipliers and bounds.
  • Adjust hardwareConcurrency() helper to delegate to folly::available_concurrency() and update its documentation comment accordingly.
  • Update tests that assert on concurrency-derived values to expect folly::available_concurrency() results instead of folly::hardware_concurrency().
  • Change CPU thread pool executors in trace-related tests to be constructed with folly::available_concurrency().
presto-native-execution/presto_cpp/main/PrestoServer.cpp
presto-native-execution/presto_cpp/main/common/Configs.cpp
presto-native-execution/presto_cpp/main/tests/ServerOperationTest.cpp
presto-native-execution/presto_cpp/main/common/tests/ConfigTest.cpp
presto-native-execution/presto_cpp/main/tool/trace/tests/BroadcastWriteReplayerTest.cpp
presto-native-execution/presto_cpp/main/tool/trace/tests/PartitionAndSerializeReplayerTest.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In PrestoServer::checkOverload, hwConcurrency is now derived from folly::available_concurrency() but still cached in a static const—if available_concurrency is meant to reflect dynamic limits (e.g., cgroup/numa changes), consider recomputing it each time instead of storing it statically so overload thresholds stay aligned with the current environment.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In `PrestoServer::checkOverload`, `hwConcurrency` is now derived from `folly::available_concurrency()` but still cached in a `static const`—if `available_concurrency` is meant to reflect dynamic limits (e.g., cgroup/numa changes), consider recomputing it each time instead of storing it statically so overload thresholds stay aligned with the current environment.

## Individual Comments

### Comment 1
<location path="presto-native-execution/presto_cpp/main/PrestoServer.cpp" line_range="1710" />
<code_context>
   }

-  static const auto hwConcurrency = folly::hardware_concurrency();
+  static const auto hwConcurrency = folly::available_concurrency();
   const auto overloadedThresholdCpuPct =
       systemConfig->workerOverloadedThresholdCpuPct();
</code_context>
<issue_to_address>
**issue:** Guard against available_concurrency() returning 0 when computing overload thresholds.

At other call sites you wrap `folly::available_concurrency()` in `std::max(..., 1)`, but here the static `hwConcurrency` feeds directly into the overload logic. If `available_concurrency()` returns 0 (as noted in `hardwareConcurrency()`), `overloadedThresholdQueuedDrivers` becomes 0 and overload detection effectively stops working. Please normalize this value here (e.g., `std::max<size_t>(folly::available_concurrency(), 1)` or by using `hardwareConcurrency()`).
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@amitkdutta amitkdutta merged commit af30a1a into prestodb:master Mar 16, 2026
116 of 118 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants