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

Save to Disk in Bio Thread - draft #1784

Draft
wants to merge 10 commits into
base: unstable
Choose a base branch
from

Conversation

nitaicaro
Copy link
Contributor

@nitaicaro nitaicaro commented Feb 26, 2025

Introduction

This PR introduces a new feature that enables replicas to perform disk-based synchronization on a dedicated background thread (Bio thread). Benchmarking results demonstrate significant improvements in synchronization duration. In extreme cases, this optimization allows syncs that would have previously failed to succeed.

This is an early draft pull request, as requested by the maintainers, to allow for review of the overall structure and approach before the full implementation is completed.

Problem Statement

Some administrators prefer the disk-based full synchronization mode for replicas. This mode allows replicas to continue serving clients with data while downloading the RDB file.

Valkey's predominantly single-threaded nature creates a challenge: serving client read requests and saving data from the socket to disk are not truly concurrent operations. In practice, the replica alternates between processing client requests and replication data, leading to inefficient behavior and prolonged sync durations, especially under high load.

Proposed Solution

To address this, the solution offloads the task of downloading the RDB file from the socket to a background thread. This allows the main thread to focus exclusively on handling client read requests while the background thread handles communication with the primary.

Benchmarking Results

Potential for Improvement

In theory, this optimization can lead to unbounded improvement in sync duration. By eliminating competition between client read events and socket communication (i.e., events related to handling RDB download with the primary), sync times become independent on load - the main thread handles only client reads, while the background thread focuses on primary RDB download events, allowing the system to perform consistently even under high load.

The full valkey-benchmark commands can be found in the appendix below.

Sync Duration with Feature Disabled (times in seconds)

16 threads, 64 clients: 172 seconds
32 threads, 128 clients: 436 seconds
48 threads, 192 clients: 710 seconds

Sync Duration with Feature Enabled (times in seconds)

16 threads, 64 clients: 33 seconds (80.8% improvement)
32 threads, 128 clients: 33 seconds (92.4% improvement)
48 threads, 192 clients: 33 seconds (95.3% improvement)

image

Alternative Solutions Considered

IO Threads
IO threads to not have an advantage over Bio in this case: The save-to-disk job is rare (most likely no more than several executions in a replica's lifetime), and there is never more than one simultaneous execution. Bio threads make more sense for a single, slow long running operation.

io_uring
For a single connection, io_uring doesn't provide as much of a performance boost because the primary advantage comes from batching many I/O operations together to reduce syscall overhead. With just one connection, we won't have enough operations to benefit significantly from these optimizations.

Prioritizing primary's socket in the event loop
This approach would help, but less effectively than using a Bio thread. We would still need to allocate attention to handling read requests, which could limit its benefit. It could be more useful on smaller instance types with limited CPU cores.

Appendix:

Benchmarking Setup

  • Client machine: AWS c5a.16xlarge
  • Server machines: AWS c5a.2xlarge
# Step 1: Fill the primary and replica DBs with 6GB of data:

./valkey-benchmark -h <host> -p <port> -l -d 128 -t set -r 30000000 --threads 16 -c 64

# Step 2: Initiate heavy read load on the replica:

./valkey-benchmark -h <host> -p <port> -t get -r 30000000 --threads <t> -c <t> -n 1000000000 -P <P>

# Step 3: Enable/disable the config controlling the new feature:

./valkey-cli -h <host> -p <port> config set replica-save-to-disk-in-bio-thread <yes/no>

# Step 4: Initiate sync:

./valkey-cli -h <replica host> -p <replica port> replicaof <primary host> <primary port>

@xbasel xbasel self-requested a review February 26, 2025 10:34
@nitaicaro nitaicaro changed the title save to disk in bio thread - draft Save to Disk in Bio Thread - draft Feb 26, 2025
Copy link

codecov bot commented Feb 26, 2025

Codecov Report

Attention: Patch coverage is 87.05882% with 22 lines in your changes missing coverage. Please review.

Project coverage is 71.02%. Comparing base (aa88453) to head (c0129a9).

Files with missing lines Patch % Lines
src/bio.c 82.10% 17 Missing ⚠️
src/replication.c 93.24% 5 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##           unstable    #1784      +/-   ##
============================================
- Coverage     71.09%   71.02%   -0.08%     
============================================
  Files           123      123              
  Lines         65671    65816     +145     
============================================
+ Hits          46692    46748      +56     
- Misses        18979    19068      +89     
Files with missing lines Coverage Δ
src/config.c 78.39% <ø> (ø)
src/server.c 87.54% <100.00%> (ø)
src/server.h 100.00% <ø> (ø)
src/replication.c 86.35% <93.24%> (-0.92%) ⬇️
src/bio.c 83.04% <82.10%> (-1.41%) ⬇️

... and 11 files with indirect coverage changes

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nitaicaro nitaicaro force-pushed the replica-save-to-disk-in-bio-thread branch 5 times, most recently from 2d9c776 to 466a0ca Compare March 4, 2025 14:20
@nitaicaro nitaicaro force-pushed the replica-save-to-disk-in-bio-thread branch 5 times, most recently from 6bcc6be to c67c618 Compare March 11, 2025 11:09
@nitaicaro nitaicaro force-pushed the replica-save-to-disk-in-bio-thread branch from c67c618 to 0a14eff Compare March 11, 2025 11:20
@nitaicaro nitaicaro force-pushed the replica-save-to-disk-in-bio-thread branch from 2b6e9a5 to ad1b8fc Compare March 11, 2025 12:09
@nitaicaro nitaicaro force-pushed the replica-save-to-disk-in-bio-thread branch from ad1b8fc to f1418b1 Compare March 11, 2025 12:15
Copy link
Member

@xbasel xbasel left a comment

Choose a reason for hiding this comment

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

Initial comments.

@@ -2649,6 +2678,12 @@ void freePendingReplDataBuf(void) {
server.pending_repl_data.len = 0;
}

void receiveRDBinBioThread(connection *conn) {
serverLog(LL_NOTICE, "Replica main thread creating Bio thread to save RDB to disk");
connSetReadHandler(conn, NULL);
Copy link
Member

Choose a reason for hiding this comment

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

What happens to the write handler? Is the main thread supposed to do any writes in the meantime?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question, it was never set to anything on the replica's side, see:
https://github.com/valkey-io/valkey/blob/unstable/src/replication.c#L3517

^ this is executed during sync handshake. After the sync is done and we enter steady-state we initialize it: https://github.com/valkey-io/valkey/blob/unstable/src/replication.c#L4380

@@ -3918,6 +3963,10 @@ void replicationAbortSyncTransfer(void) {
cleanupTransferResources();
}

void waitForDiskSaveBioThreadComplete(void) {
while (bioPendingJobsOfType(BIO_SAVE_TO_DISK));
Copy link
Member

Choose a reason for hiding this comment

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

This is a blocking operation and should be avoided in the main thread. Although this is already being done in the main thread I think, via: bioDrainWorker

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have to do this to prevent a race condition where a new sync starts before the previous Bio thread finishes.
Busy-waiting is also how it's done for io-threads, see waitForClient()

Copy link
Member

Choose a reason for hiding this comment

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

(1) can bioDrainWorker be used?
(2) Did you consider timing out the operation and shutting down ?

Copy link
Contributor Author

@nitaicaro nitaicaro Mar 18, 2025

Choose a reason for hiding this comment

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

Good point - since we switched to blocking mode on the bio thread we can have a deadlock here. I'll remove this, and then the timeouts of read() will guarantee that we eventually reach shouldAbortSave() which will free the main thread.

Edit:
Actually we set a timeout so we cannot block forever:
connRecvTimeout(conn, server.repl_syncio_timeout * 1000);

So we are guaranteed to eventually reach shouldAbortSave() which will free the main thread from busy-waiting.

Copy link
Member

Choose a reason for hiding this comment

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

Blocking the main thread forever is bad practice, even if the child thread is logically guaranteed to finish.
Maybe timing it out and crashing is the right approach.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your suggestion to switch to bioDrain seems to be the right solution. Updated.
About blocking forever, this seems to be a risk in other places in the code too, whether they busy-wait with bioDrain or with bioPendingJobsOfType. I suggest we open an issue to address this more comprehensively.

Comment on lines +374 to +375
error = 1;
goto done;
Copy link
Member

Choose a reason for hiding this comment

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

why not to create an error label instead of using a variable?

Copy link
Contributor Author

@nitaicaro nitaicaro Mar 18, 2025

Choose a reason for hiding this comment

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

I think we cannot have more than one label inside the else if (job_type == BIO_RDB_SAVE).
If we have

done:
...
error:
...

then error would always get executed after done (we cannot return after done since we need to reach the end of the iteration:

zfree(job);

/* Lock again before reiterating the loop, if there are no longer
 * jobs to process we'll block again in pthread_cond_wait(). */
pthread_mutex_lock(&bio_mutex[worker]);
listDelNode(bio_jobs[worker], ln);
bio_jobs_counter[job_type]--;
pthread_cond_signal(&bio_newjob_cond[worker]);

)

I guess we can do

done:
...
goto really_done;
error:
...
really_done:

But I'm not sure it makes things better

goto done;
} else if (ret == INSPECT_BULK_PAYLOAD_PRIMARY_PING) {
atomic_store_explicit(&server.repl_transfer_lastio, atomic_load_explicit(&server.unixtime, memory_order_relaxed), memory_order_relaxed);
memset(buf, 0, PROTO_IOBUF_LEN);
Copy link
Member

Choose a reason for hiding this comment

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

why this is being done?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the purpose of a ping from the primary - to refresh last_io field in order to avoid timeout

* We'll restore it when the RDB is received. */
connBlock(conn);
connRecvTimeout(conn, server.repl_syncio_timeout * 1000);
do {
Copy link
Member

Choose a reason for hiding this comment

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

why do we need two loops in the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are basically imitating what readSyncBulkPayload does for normal replication. We try to read the bulk payload length from the primary. Ideally one pass would be enough (no need for loop), but the primary is sometimes not fast enough in sending the length, so it periodically sends pings (newlines) until it's ready. We have to keep looping while we receive these pings.

The second loop goes on until the primary finishes sending all the data (we see an EOF or the amount we read is equal to the previously passed payload length).

So the first loop is conditioned on receiving pings, the second one on sync completion.

@nitaicaro nitaicaro force-pushed the replica-save-to-disk-in-bio-thread branch from 298e925 to ca16a76 Compare March 18, 2025 13:30
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