pending.rs 16.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! # Transfer Managers
//!
//! Transfer managers are responsible for multiple things:
//! - Before the transfer:
//!     - Rate-limiting the number of transfers that can be initiated concurrently. This is implemented through bounded channels.
//!         - Due to the nature of the [`super::OffloadManager`], we only apply this rate-limiting to offloads.
//! - During the transfer:
//!     - Initiating the transfer
//!     - Holding strong references to blocks being transfered.
//! - After the transfer:
//!     - Dropping these references once the transfer is complete.
//!     - Registering the blocks with the target pool.
//!     - Returning the registered blocks to the caller.
//!
//! This is implemented through the [`TransferManager`] trait, which takes a single [`PendingTransfer`]
//! and initiates the transfer.
//!
//! Since CUDA and NIXL transfers use completely different semantics, we implement two separate transfer managers.
//!
//! ## Workflow
36
37
//! 1. A transfer request is made by calling [`TransferManager::enqueue_transfer`]
//! 2. [`TransferManager::enqueue_transfer`] performs the transfer, and enqueues relevant data into a bounded channel.
38
39
40
//! 3. A worker thread (consuming this bounded channel and enforcing rate limiting) awaits the incoming transfers.
//! 4. After a transfer is complete, the worker thread registers the blocks with the target pool, and returns the registered blocks to the caller.

41
use std::marker::PhantomData;
42
use std::pin::Pin;
43
44
use std::sync::Arc;
use std::thread::spawn;
45
use tokio::runtime::Handle;
46
use tokio::sync::mpsc;
47
use tokio_util::sync::CancellationToken;
48

49
50
51
52
use crate::block_manager::block::{
    transfer::{WriteTo, WriteToStrategy},
    BlockError, BlockExt, BlockMetadata, BlockState, MutableBlock, ReadableBlock, WritableBlock,
};
53
use crate::block_manager::pool::BlockPoolError;
54
55
use crate::block_manager::state::TransferContext;
use crate::block_manager::storage::{Local, Storage};
56
use crate::block_manager::BlockPool;
57

58
use anyhow::Result;
59
60
use async_trait::async_trait;
use cudarc::driver::{sys::CUevent_flags, CudaEvent};
61
use futures::{stream::FuturesUnordered, StreamExt};
62

63
use super::BlockResult;
64

65
66
use dynamo_runtime::utils::task::CriticalTaskExecutionHandle;

67
68
69
/// Manage a set of pending transfers.
pub struct PendingTransfer<Source: Storage, Target: Storage, Metadata: BlockMetadata> {
    /// The block being copied from.
70
    sources: Vec<Arc<MutableBlock<Source, Metadata>>>,
71
72
73
    /// The block being copied to.
    targets: Vec<MutableBlock<Target, Metadata>>,
    /// The oneshot sender that optionally returns the registered blocks once the transfer is complete.
74
    completion_indicator: Option<oneshot::Sender<BlockResult<Target, Metadata>>>,
75
    /// The target pool that will receive the registered block.
76
    target_pool: Arc<BlockPool<Target, Metadata>>,
77
78
79
80
81
82
83
84
}

impl<Source: Storage, Target: Storage, Metadata: BlockMetadata>
    PendingTransfer<Source, Target, Metadata>
{
    pub fn new(
        sources: Vec<Arc<MutableBlock<Source, Metadata>>>,
        targets: Vec<MutableBlock<Target, Metadata>>,
85
        completion_indicator: Option<oneshot::Sender<BlockResult<Target, Metadata>>>,
86
        target_pool: Arc<BlockPool<Target, Metadata>>,
87
    ) -> Self {
88
        assert_eq!(sources.len(), targets.len());
89
        Self {
90
91
92
            sources,
            targets,
            completion_indicator,
93
            target_pool,
94
95
96
97
98
        }
    }

    fn handle_complete(self) -> Result<()> {
        let Self {
99
100
101
            sources,
            mut targets,
            target_pool,
102
            completion_indicator,
103
104
105
            ..
        } = self;

106
107
108
109
110
        for (source, target) in sources.iter().zip(targets.iter_mut()) {
            transfer_metadata(source, target)?;
        }

        let blocks = target_pool.register_blocks_blocking(targets)?;
111

112
113
        if let Some(completion_indicator) = completion_indicator {
            completion_indicator.send(Ok(blocks))?;
114
        }
115
116
117
118
119
120
121
122
123
124

        Ok(())
    }
}

fn transfer_metadata<Source: Storage, Target: Storage, Metadata: BlockMetadata>(
    source: &Arc<MutableBlock<Source, Metadata>>,
    target: &mut MutableBlock<Target, Metadata>,
) -> Result<()> {
    // Only registered blocks can be transferred. There are upstream checks for this, so this shouldn't ever fail.
125
    if let BlockState::Registered(reg_handle, _) = source.state() {
126
127
128
129
130
131
132
133
134
135
        // Bring the block back to the 'Reset' state.
        target.reset();
        // Transfer metadata.
        target.update_metadata(source.metadata().clone());
        // Copy tokens
        target.apply_token_block(reg_handle.token_block().clone())?;
    } else {
        Err(BlockPoolError::BlockError(BlockError::InvalidState(
            "Block is not registered.".to_string(),
        )))?;
136
    }
137
138
139
140
141
142
143
144
145

    Ok(())
}

#[async_trait]
pub trait TransferManager<Source: Storage, Target: Storage, Metadata: BlockMetadata>:
    Send + Sync
{
    /// Begin a transfer. Blocks if the pending queue is full.
146
    async fn enqueue_transfer(
147
148
149
        &self,
        pending_transfer: PendingTransfer<Source, Target, Metadata>,
    ) -> Result<()>;
150
151
}

152
153
154
pub struct CudaTransferManager<Source: Storage, Target: Storage, Metadata: BlockMetadata> {
    pending_transfer_q: mpsc::Sender<(PendingTransfer<Source, Target, Metadata>, CudaEvent)>,
    transfer_ctx: Arc<TransferContext>,
155
156
157
}

impl<Source: Storage, Target: Storage, Metadata: BlockMetadata>
158
    CudaTransferManager<Source, Target, Metadata>
159
{
160
161
162
163
164
    pub fn new(
        transfer_ctx: Arc<TransferContext>,
        max_concurrent_transfers: usize,
        cancellation_token: CancellationToken,
    ) -> Self {
165
166
167
        let (tx, mut rx) = mpsc::channel::<(PendingTransfer<Source, Target, Metadata>, CudaEvent)>(
            max_concurrent_transfers,
        );
168
169

        spawn(move || {
170
            while let Some((pending_transfer, event)) = rx.blocking_recv() {
171
                // Wait for the event.
172
173
                event.synchronize()?;
                // Only finalize the transfer after the event is signaled.
174
175
176
177
178
179
180
181
                match pending_transfer.handle_complete() {
                    Ok(_) => {}
                    Err(e) => {
                        // The only case where this can fail is if the progress engine is shutdown.
                        // This is not a problem, so we can just ignore it.
                        tracing::warn!("Error handling transfer completion: {:?}", e);
                    }
                }
182
183
184
185
186
187

                // Flush any remaining transfers.
                if cancellation_token.is_cancelled() {
                    while rx.blocking_recv().is_some() {}
                    break;
                }
188
189
190
191
192
193
            }
            Ok::<(), anyhow::Error>(())
        });

        Self {
            pending_transfer_q: tx,
194
            transfer_ctx,
195
196
        }
    }
197
}
198

199
200
201
202
203
204
205
206
207
208
209
210
211
212
#[async_trait]
impl<Source, Target, Metadata> TransferManager<Source, Target, Metadata>
    for CudaTransferManager<Source, Target, Metadata>
where
    Source: Storage,
    Target: Storage,
    Metadata: BlockMetadata,
    // Check that the source block is readable, local, and writable to the target block.
    MutableBlock<Source, Metadata>: ReadableBlock<StorageType = Source>
        + Local
        + WriteToStrategy<MutableBlock<Target, Metadata>>,
    // Check that the target block is writable.
    MutableBlock<Target, Metadata>: WritableBlock<StorageType = Target>,
{
213
    async fn enqueue_transfer(
214
        &self,
215
        mut pending_transfer: PendingTransfer<Source, Target, Metadata>,
216
    ) -> Result<()> {
217
218
219
220
221
        pending_transfer.sources.write_to(
            &mut pending_transfer.targets,
            None,
            self.transfer_ctx.clone(),
        )?;
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

        // Use a cuda event to record the completion of the transfers.
        let event = self
            .transfer_ctx
            .stream()
            .record_event(Some(CUevent_flags::CU_EVENT_BLOCKING_SYNC))?;

        // Send the pending transfer and event to the worker thread.
        // If the queue is full, we block the worker until space becomes available.
        self.pending_transfer_q
            .send((pending_transfer, event))
            .await?;

        Ok(())
    }
}

pub struct DiskTransferManager {
    futures_tx: mpsc::Sender<Pin<Box<dyn std::future::Future<Output = ()> + Send + Sync>>>,
    transfer_ctx: Arc<TransferContext>,
}

impl DiskTransferManager {
245
246
247
248
249
250
    pub fn new(
        transfer_ctx: Arc<TransferContext>,
        max_concurrent_transfers: usize,
        runtime: &Handle,
        cancellation_token: CancellationToken,
    ) -> Self {
251
252
        let (futures_tx, mut futures_rx) = mpsc::channel(1);

253
        runtime.spawn(async move {
254
255
256
257
258
259
            // Keep track of our pending transfers.
            // Consume the futures as they complete, while also receiving new ones.

            let mut pending_transfers = FuturesUnordered::new();
            loop {
                tokio::select! {
260
261
262
263
264
265
266

                    _ = cancellation_token.cancelled() => {
                        // Flush remaining transfers.
                        while (pending_transfers.next().await).is_some() {}
                        return;
                    }

267
268
                    Some(future) = futures_rx.recv() => {
                        // If we're at max size, block the worker thread on the next() call until we have capacity.
269
                        while pending_transfers.len() >= max_concurrent_transfers {
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
                            pending_transfers.next().await;
                        }
                        // Once we have capacity, push the new future onto the queue.
                        pending_transfers.push(future);
                    }
                    Some(_) = pending_transfers.next(), if !pending_transfers.is_empty() => {
                        // A transfer completed, just continue to process more
                    }
                }
            }
        });

        Self {
            futures_tx,
            transfer_ctx,
        }
    }
}

#[async_trait]
impl<Source, Target, Metadata> TransferManager<Source, Target, Metadata> for DiskTransferManager
where
    Source: Storage,
    Target: Storage,
    Metadata: BlockMetadata,
    // Check that the source block is readable, local, and writable to the target block.
    MutableBlock<Source, Metadata>: ReadableBlock<StorageType = Source>
        + Local
        + WriteToStrategy<MutableBlock<Target, Metadata>>,
    // Check that the target block is writable.
    MutableBlock<Target, Metadata>: WritableBlock<StorageType = Target>,
{
302
    async fn enqueue_transfer(
303
304
305
        &self,
        mut pending_transfer: PendingTransfer<Source, Target, Metadata>,
    ) -> Result<()> {
306
307
308
309
310
        let future = pending_transfer.sources.nixl_write_to(
            &mut pending_transfer.targets,
            None,
            self.transfer_ctx.clone(),
        )?;
311
312

        let completion_future = async move {
313
314
315
316
317
318
319
320
321
            let _ = future.await;
            match pending_transfer.handle_complete() {
                Ok(_) => {}
                Err(e) => {
                    // The only case where this can fail is if the progress engine is being shutdown.
                    // This is not a problem, so we can just ignore it.
                    tracing::warn!("Error handling transfer completion: {:?}", e);
                }
            }
322
323
324
325
326
        };

        // Futures_(tx/rx) has a capacity of 1. If the queue worker has received another future and is awaiting next() due to a full `FuturesUnordered`,
        // this call will block until the worker has processed the prior future.
        self.futures_tx.send(Box::pin(completion_future)).await?;
327
328
329
330

        Ok(())
    }
}
331
332
333
334
335
336
337
338
339
340
341

/// A transfer manager that enforces a max batch size for transfers.
pub struct TransferBatcher<Source, Target, Metadata, Manager>
where
    Source: Storage,
    Target: Storage,
    Metadata: BlockMetadata,
    Manager: TransferManager<Source, Target, Metadata>,
{
    transfer_manager: Manager,
    max_transfer_batch_size: usize,
342
343
    runtime: Handle,
    cancellation_token: CancellationToken,
344
345
346
347
348
349
350
351
352
353
    _phantom: PhantomData<(Source, Target, Metadata)>,
}

impl<Source, Target, Metadata, Manager> TransferBatcher<Source, Target, Metadata, Manager>
where
    Source: Storage,
    Target: Storage,
    Metadata: BlockMetadata,
    Manager: TransferManager<Source, Target, Metadata>,
{
354
355
356
357
358
359
    pub fn new(
        transfer_manager: Manager,
        max_transfer_batch_size: usize,
        runtime: &Handle,
        cancellation_token: CancellationToken,
    ) -> Self {
360
361
362
        Self {
            transfer_manager,
            max_transfer_batch_size,
363
364
            runtime: runtime.clone(),
            cancellation_token,
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<Source, Target, Metadata, Manager> TransferManager<Source, Target, Metadata>
    for TransferBatcher<Source, Target, Metadata, Manager>
where
    Source: Storage,
    Target: Storage,
    Metadata: BlockMetadata,
    Manager: TransferManager<Source, Target, Metadata>,
{
    async fn enqueue_transfer(
        &self,
        pending_transfer: PendingTransfer<Source, Target, Metadata>,
    ) -> Result<()> {
        // If it's smaller than the max batch size, just enqueue it.
        if pending_transfer.sources.len() < self.max_transfer_batch_size {
            return self
                .transfer_manager
                .enqueue_transfer(pending_transfer)
                .await;
        }

        // Otherwise, we need to split the transfer into multiple smaller transfers.

        let PendingTransfer {
            mut sources,
            mut targets,
            completion_indicator,
            target_pool,
        } = pending_transfer;

        let mut indicators = Vec::new();

        while !sources.is_empty() {
            let sources = sources
                .drain(..std::cmp::min(self.max_transfer_batch_size, sources.len()))
                .collect();
            let targets = targets
                .drain(..std::cmp::min(self.max_transfer_batch_size, targets.len()))
                .collect();

            // If we have a completion indicator, we need to create a new one for each sub-transfer.
            let indicator = if completion_indicator.is_some() {
                let (batch_tx, batch_rx) = oneshot::channel();
                indicators.push(batch_rx);
                Some(batch_tx)
            } else {
                None
            };

            let request = PendingTransfer::new(sources, targets, indicator, target_pool.clone());
            // Enqueue our reduced transfer. This may block if the queue is full.
            self.transfer_manager.enqueue_transfer(request).await?;
        }

        if let Some(completion_indicator) = completion_indicator {
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
            CriticalTaskExecutionHandle::new_with_runtime(
                move |cancel_token| async move {
                    let mut results = Vec::new();

                    for indicator in indicators.into_iter() {
                        // Await each sub-transfer, and append the results to our final results.
                        tokio::select! {
                            _ = cancel_token.cancelled() => {
                                return Ok(());
                            }

                            Ok(indicator) = indicator => {
                                let result = match indicator {
                                    Ok(result) => result,
                                    Err(e) => {
                                        tracing::error!("Error receiving transfer results: {:?}", e);
                                        completion_indicator.send(Err(e)).unwrap();
                                        return Ok(());
                                    }
                                };
                                results.extend(result);
                            }
447
                        }
448
449
450
451
                    }

                    // Send the final results to the top-level completion indicator.
                    completion_indicator.send(Ok(results))?;
452

453
454
455
456
457
458
                    Ok(())
                },
                self.cancellation_token.clone(),
                "Transfer Batcher",
                &self.runtime,
            )?.detach();
459
460
461
462
463
        }

        Ok(())
    }
}