Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support subcmpct using reserved resources for round-robin priority #10341

Conversation

littlepig2013
Copy link
Contributor

@littlepig2013 littlepig2013 commented Jul 11, 2022

Summary:

Earlier implementation of round-robin priority can only pick one file at a time and disallows parallel compactions within the same level. In this PR, round-robin compaction policy will expand towards more input files with respecting some additional constraints, which are summarized as follows:

  • Constraint 1: We can only pick consecutive files
    • Constraint 1a: When a file is being compacted (or some input files are being compacted after expanding), we cannot choose it and have to stop choosing more files
    • Constraint 1b: When we reach the last file (with the largest keys), we cannot choose more files (the next file will be the first one with small keys)
  • Constraint 2: We should ensure the total compaction bytes (including the overlapped files from the next level) is no more than mutable_cf_options_.max_compaction_bytes
  • Constraint 3: We try our best to pick as many files as possible so that the post-compaction level size can be just less than MaxBytesForLevel(start_level_)
  • Constraint 4: If trivial move is allowed, we reuse the logic of TryNonL0TrivialMove() instead of expanding files with Constraint 3

More details can be found in LevelCompactionBuilder::SetupOtherFilesWithRoundRobinExpansion().

The above optimization accelerates the process of moving the compaction cursor, in which the write-amp can be further reduced. While a large compaction may lead to high write stall, we break this large compaction into several subcompactions regardless of the max_subcompactions limit. The number of subcompactions for round-robin compaction priority is determined through the following steps:

  • Step 1: Initialized against max_output_file_limit, the number of input files in the start level, and also the range size limit ranges.size()
  • Step 2: Call AcquireSubcompactionResources()when max subcompactions is not sufficient, but we may or may not obtain desired resources, additional number of resources is stored in extra_num_subcompaction_threads_reserved_). Subcompaction limit is changed and update num_planned_subcompactions with GetSubcompactionLimit()
  • Step 3: Call ShrinkSubcompactionResources() to ensure extra resources can be released (extra resources may exist for round-robin compaction when the number of actual number of subcompactions is less than the number of planned subcompactions)

More details can be found in CompactionJob::AcquireSubcompactionResources(),CompactionJob::ShrinkSubcompactionResources(), and CompactionJob::ReleaseSubcompactionResources().

Test Plan:

Add CompactionPriMultipleFilesRoundRobin[1-3] unit test in compaction_picker_test.cc and RoundRobinSubcompactionsAgainstResources.SubcompactionsUsingResources/[0-4], RoundRobinSubcompactionsAgainstPressureToken.PressureTokenTest/[0-1] in db_compaction_test.cc

@littlepig2013 littlepig2013 changed the title Support subcmpct using reserved resources for round-robin priority [WIP] Support subcmpct using reserved resources for round-robin priority Jul 11, 2022
@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch 3 times, most recently from 9c2e415 to df35a2d Compare July 11, 2022 22:28
@littlepig2013 littlepig2013 marked this pull request as draft July 11, 2022 22:38
@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch 2 times, most recently from 9b5b52d to 1bc9245 Compare July 12, 2022 17:28
@littlepig2013 littlepig2013 changed the title [WIP] Support subcmpct using reserved resources for round-robin priority Support subcmpct using reserved resources for round-robin priority Jul 12, 2022
@littlepig2013 littlepig2013 marked this pull request as ready for review July 12, 2022 17:29
@littlepig2013 littlepig2013 requested review from ajkr and hx235 July 12, 2022 17:37
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 1bc9245 to 35b130f Compare July 13, 2022 20:16
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 35b130f to 34aea5a Compare July 14, 2022 18:57
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 34aea5a to 2d32d5a Compare July 14, 2022 20:16
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 2d32d5a to 524db97 Compare July 14, 2022 20:20
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@hx235 hx235 self-requested a review July 15, 2022 08:34
Copy link
Contributor

@hx235 hx235 left a comment

Choose a reason for hiding this comment

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

Review summary to @littlepig2013 + @ajkr:

  • I focused on reviewing resource reservation part of this PR (compaction_job.cc). My biggest advice is to use self-explanatory variables and function abstraction to clarify different layers of constraint we add to number of subcompactions allowed. Unit tests are missing for resource reservation logic at this point so I will take another round of review when they are in-place.
  • I haven't looked into compaction related part much but my impression based on my offline chat with @littlepig2013 is that it's so much easier to understand the code after knowing the big ideas (i.e, the 4 constraints in compaction_level_picker.cc level ). So if possible and haven’t done so, please reflect those ideas into coding (e.g, better abstraction, naming, paragraph, space ... ) to ease up the code reading. This is not just for domain expert like @ajkr but also for non-experts (such as me ... :)
  • An extra ask is to update PR summary before landing with more high-level descriptions like what you told me in the meeting but without getting into the codes too much (okay to cite function name though). This will save future developers' time from diving into coding just for big ideas.

Thank you!

@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 524db97 to 400bde5 Compare July 16, 2022 22:27
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 55d0a70 to 1319058 Compare July 23, 2022 07:45
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@littlepig2013 littlepig2013 requested a review from ajkr July 23, 2022 07:48
@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 1319058 to c1d5832 Compare July 23, 2022 07:58
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from c1d5832 to 06352e9 Compare July 23, 2022 08:00
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@littlepig2013 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 06352e9 to 091a50c Compare July 23, 2022 18:53
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@littlepig2013 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Contributor

@ajkr ajkr left a comment

Choose a reason for hiding this comment

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

LGTM. The Shrink and Release functions and client usages look perfect. Acquire/Increase could perhaps be cleaned up a bit further (Increase doesn't account for the initial value of extra_num_subcompaction_threads_reserved_ being positive; Acquire has some higher-level logic than subcompaction resources by accounting for subcompactions run without resource reservations). I don't have a concrete proposal and worry if I did it'd conflict with earlier review suggestions, so think we should land it as is unless you have a proposal :).

db/compaction/compaction_job.cc Show resolved Hide resolved
@ajkr
Copy link
Contributor

ajkr commented Jul 23, 2022

I don't have a concrete proposal

What if we merge the logic of AcquireSubcompactionResources() into GenSubcompactionBoundaries() (per Hui's comment: #10341 (comment)). Then rename IncreaseSubcompactionResources() to AcquireSubcompactionResources() since the latter naming implies we are starting with zero reserved resources (which is indeed the case). Would that improve it?

@littlepig2013
Copy link
Contributor Author

I don't have a concrete proposal

What if we merge the logic of AcquireSubcompactionResources() into GenSubcompactionBoundaries() (per Hui's comment: #10341 (comment)). Then rename IncreaseSubcompactionResources() to AcquireSubcompactionResources() since the latter naming implies we are starting with zero reserved resources (which is indeed the case). Would that improve it?

Yeah. I agree. Let me apply this change. Thanks!

@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 091a50c to 48c9315 Compare July 23, 2022 20:51
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@littlepig2013 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

…vel size is below `MaxBytesForLevel(level)`, split into sub-compactions, and execute them using reserved resources
@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 48c9315 to 30da312 Compare July 24, 2022 05:19
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

…vel size is below `MaxBytesForLevel(level)`, split into sub-compactions, and execute them using reserved resources
@littlepig2013 littlepig2013 force-pushed the reserve_threads_for_subcmpt_in_round_robin branch from 30da312 to a327849 Compare July 24, 2022 08:23
@facebook-github-bot
Copy link
Contributor

@littlepig2013 has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot
Copy link
Contributor

@littlepig2013 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

ajkr pushed a commit to ajkr/rocksdb that referenced this pull request Jul 25, 2022
…acebook#10341)

Summary:
Earlier implementation of round-robin priority can only pick one file at a time and disallows parallel compactions within the same level. In this PR, round-robin compaction policy will expand towards more input files with respecting some additional constraints, which are summarized as follows:
 * Constraint 1: We can only pick consecutive files
   - Constraint 1a: When a file is being compacted (or some input files are being compacted after expanding), we cannot choose it and have to stop choosing more files
   - Constraint 1b: When we reach the last file (with the largest keys), we cannot choose more files (the next file will be the first one with small keys)
 * Constraint 2: We should ensure the total compaction bytes (including the overlapped files from the next level) is no more than `mutable_cf_options_.max_compaction_bytes`
 * Constraint 3: We try our best to pick as many files as possible so that the post-compaction level size can be just less than `MaxBytesForLevel(start_level_)`
 * Constraint 4: If trivial move is allowed, we reuse the logic of `TryNonL0TrivialMove()` instead of expanding files with Constraint 3

More details can be found in `LevelCompactionBuilder::SetupOtherFilesWithRoundRobinExpansion()`.

The above optimization accelerates the process of moving the compaction cursor, in which the write-amp can be further reduced. While a large compaction may lead to high write stall, we break this large compaction into several subcompactions **regardless of** the `max_subcompactions` limit.  The number of subcompactions for round-robin compaction priority is determined through the following steps:
* Step 1: Initialized against `max_output_file_limit`, the number of input files in the start level, and also the range size limit `ranges.size()`
* Step 2: Call `AcquireSubcompactionResources()`when max subcompactions is not sufficient, but we may or may not obtain desired resources, additional number of resources is stored in `extra_num_subcompaction_threads_reserved_`). Subcompaction limit is changed and update `num_planned_subcompactions` with `GetSubcompactionLimit()`
* Step 3: Call `ShrinkSubcompactionResources()` to ensure extra resources can be released (extra resources may exist for round-robin compaction when the number of actual number of subcompactions is less than the number of planned subcompactions)

More details can be found in `CompactionJob::AcquireSubcompactionResources()`,`CompactionJob::ShrinkSubcompactionResources()`, and `CompactionJob::ReleaseSubcompactionResources()`.

Pull Request resolved: facebook#10341

Test Plan: Add `CompactionPriMultipleFilesRoundRobin[1-3]` unit test in `compaction_picker_test.cc` and `RoundRobinSubcompactionsAgainstResources.SubcompactionsUsingResources/[0-4]`, `RoundRobinSubcompactionsAgainstPressureToken.PressureTokenTest/[0-1]` in `db_compaction_test.cc`

Reviewed By: ajkr, hx235

Differential Revision: D37792644

Pulled By: littlepig2013

fbshipit-source-id: 7fecb7c4ffd97b34bbf6e3b760b2c35a772a0657
facebook-github-bot pushed a commit that referenced this pull request Jul 27, 2022
Summary:
Update HISTORY.md for CompactionPri::kRoundRobin. Detailed implementation can be found in [PR10107](#10107), [PR10227](#10227), [PR10250](#10250), [PR10278](#10278), [PR10316](#10316), and [PR10341](#10341)

Pull Request resolved: #10421

Reviewed By: ajkr

Differential Revision: D38194070

Pulled By: littlepig2013

fbshipit-source-id: 4ce153dc0bf22cd865d09c5429955023dbc90f37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants