messages.rs 17.8 KB
Newer Older
Ryan Olson's avatar
Ryan Olson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use std::ops::Range;
use std::sync::Arc;

use serde::{Deserialize, Serialize};

use crate::{BlockId, G2, InstanceId, SequenceHash};
use kvbm_logical::blocks::ImmutableBlock;
use kvbm_physical::manager::LayoutHandle;

use super::SessionId;

/// Messages exchanged between leaders during onboarding sessions.
///
/// Phase 2 protocol (G2-only):
/// 1. Initiator sends CreateSession to multiple responders
/// 2. Each responder searches local G2 and sends G2Results back
/// 3. Initiator applies first-responder-wins and sends HoldBlocks to each
/// 4. Responders send Acknowledged after releasing unwanted blocks
///
/// Phase 3 protocol (G3 staging):
/// 5. Responders search G3 and send G3Results
/// 6. Initiator sends StageBlocks with blocks to stage G3->G2
/// 7. Responders stage blocks and send BlocksReady when complete
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OnboardMessage {
    /// Initiator creates a new onboarding session.
    CreateSession {
        requester: InstanceId,
        session_id: SessionId,
        sequence_hashes: Vec<SequenceHash>,
    },

    /// Responder signals local search (G2 and G3) is complete.
    SearchComplete {
        responder: InstanceId,
        session_id: SessionId,
    },

    /// Responder reports G2 search results.
    /// - sequence_hashes: ordered list of matched sequence hashes
    /// - block_ids: parallel list of block IDs (can be zipped with sequence_hashes)
    G2Results {
        responder: InstanceId,
        session_id: SessionId,
        sequence_hashes: Vec<SequenceHash>,
        block_ids: Vec<BlockId>,
    },

    /// Responder reports G3 search results.
    /// - sequence_hashes: ordered list of matched sequence hashes (no block IDs)
    G3Results {
        responder: InstanceId,
        session_id: SessionId,
        sequence_hashes: Vec<SequenceHash>,
    },

    /// Initiator tells responder which sequence hashes to hold/drop.
    /// Works across G2 and G3 tiers.
    HoldBlocks {
        requester: InstanceId,
        session_id: SessionId,
        hold_hashes: Vec<SequenceHash>,
        drop_hashes: Vec<SequenceHash>,
    },

    /// Initiator tells responder which G3 sequence hashes to stage to G2.
    /// Any G3 blocks with these hashes should be staged to G2.
    StageBlocks {
        requester: InstanceId,
        session_id: SessionId,
        stage_hashes: Vec<SequenceHash>,
    },

    /// Responder reports newly staged blocks are ready in G2 (after G3->G2 staging).
    /// Only reports blocks that were just staged, not all G2 blocks.
    /// - sequence_hashes: newly staged blocks
    /// - block_ids: parallel to sequence_hashes
    BlocksReady {
        responder: InstanceId,
        session_id: SessionId,
        sequence_hashes: Vec<SequenceHash>,
        block_ids: Vec<BlockId>,
    },

    /// Responder acknowledges hold/drop request.
    Acknowledged {
        responder: InstanceId,
        session_id: SessionId,
    },

    /// Initiator tells responder to release specific sequence hashes that weren't selected.
    /// Works across G2 and G3 tiers.
    ReleaseBlocks {
        requester: InstanceId,
        session_id: SessionId,
        release_hashes: Vec<SequenceHash>,
    },

    /// Initiator tells responder session is complete, responder can cleanup.
    CloseSession {
        requester: InstanceId,
        session_id: SessionId,
    },

    // =========================================================================
    // G4/Object Storage Messages (Internal - not sent over network)
    // =========================================================================
    /// G4 search results from object storage `has_blocks`.
    ///
    /// Internal message sent via mpsc channel from the G4 search task
    /// to the initiator session. Contains hashes found in object storage
    /// with their sizes.
    G4Results {
        session_id: SessionId,
        /// Hashes found in G4 with their sizes in bytes
        found_hashes: Vec<(SequenceHash, usize)>,
    },

    /// G4 load completion results from object storage `get_blocks`.
    ///
    /// Internal message sent via mpsc channel from the G4 load task
    /// to the initiator session. Contains per-block success/failure.
    G4LoadComplete {
        session_id: SessionId,
        /// Successfully loaded hashes
        success: Vec<SequenceHash>,
        /// Failed hashes with error messages
        failures: Vec<(SequenceHash, String)>,
        /// Successfully loaded and registered G2 blocks.
        /// These are ready to be added to local_g2_blocks.
        /// Wrapped in Arc for Clone derivation (internal message only).
        #[serde(skip)]
        blocks: Arc<Vec<ImmutableBlock<G2>>>,
    },
    // TODO: Add heartbeat/TTL mechanism for handling unresponsive initiators
    // Heartbeat {
    //     requester: InstanceId,
    //     session_id: SessionId,
    //     timestamp: u64,
    // },
    // TTL resets with each heartbeat. If TTL expires:
    // - Responder releases all held blocks
    // - Responder cleans up session state
    // - Session task exits
}

/// Represents a block match found during search.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockMatch {
    pub sequence_hash: SequenceHash,
    pub block_id: BlockId,
}

impl OnboardMessage {
    /// Extract the session ID from any message variant.
    pub fn session_id(&self) -> SessionId {
        match self {
            OnboardMessage::CreateSession { session_id, .. }
            | OnboardMessage::SearchComplete { session_id, .. }
            | OnboardMessage::G2Results { session_id, .. }
            | OnboardMessage::G3Results { session_id, .. }
            | OnboardMessage::HoldBlocks { session_id, .. }
            | OnboardMessage::StageBlocks { session_id, .. }
            | OnboardMessage::BlocksReady { session_id, .. }
            | OnboardMessage::Acknowledged { session_id, .. }
            | OnboardMessage::ReleaseBlocks { session_id, .. }
            | OnboardMessage::CloseSession { session_id, .. }
            | OnboardMessage::G4Results { session_id, .. }
            | OnboardMessage::G4LoadComplete { session_id, .. } => *session_id,
        }
    }

    /// Extract the requester/responder instance ID from the message.
    ///
    /// # Panics
    /// Panics if called on G4 messages (internal only, no instance ID).
    pub fn instance_id(&self) -> InstanceId {
        match self {
            OnboardMessage::CreateSession { requester, .. }
            | OnboardMessage::HoldBlocks { requester, .. }
            | OnboardMessage::StageBlocks { requester, .. }
            | OnboardMessage::ReleaseBlocks { requester, .. }
            | OnboardMessage::CloseSession { requester, .. } => *requester,
            OnboardMessage::SearchComplete { responder, .. }
            | OnboardMessage::G2Results { responder, .. }
            | OnboardMessage::G3Results { responder, .. }
            | OnboardMessage::BlocksReady { responder, .. }
            | OnboardMessage::Acknowledged { responder, .. } => *responder,
            OnboardMessage::G4Results { .. } | OnboardMessage::G4LoadComplete { .. } => {
                panic!("G4 messages are internal and do not have an instance ID")
            }
        }
    }

    /// Get the variant name as a string for logging.
    pub fn variant_name(&self) -> &'static str {
        match self {
            OnboardMessage::CreateSession { .. } => "CreateSession",
            OnboardMessage::SearchComplete { .. } => "SearchComplete",
            OnboardMessage::G2Results { .. } => "G2Results",
            OnboardMessage::G3Results { .. } => "G3Results",
            OnboardMessage::HoldBlocks { .. } => "HoldBlocks",
            OnboardMessage::StageBlocks { .. } => "StageBlocks",
            OnboardMessage::BlocksReady { .. } => "BlocksReady",
            OnboardMessage::Acknowledged { .. } => "Acknowledged",
            OnboardMessage::ReleaseBlocks { .. } => "ReleaseBlocks",
            OnboardMessage::CloseSession { .. } => "CloseSession",
            OnboardMessage::G4Results { .. } => "G4Results",
            OnboardMessage::G4LoadComplete { .. } => "G4LoadComplete",
        }
    }
}

// =============================================================================
// Unified Session Protocol
// =============================================================================
//
// These types support the unified session model where sessions can dynamically
// transition between control roles.

use super::state::{ControlRole, SessionPhase};

/// Unified session message protocol.
///
/// Unified, bidirectional protocol that supports dynamic control transfer.
///
/// # Protocol Overview
///
/// 1. **Connection**: `Attach`/`Detach` for peer management
/// 2. **Control Transfer**: `YieldControl`/`AcquireControl` for bidirectional role changes
/// 3. **Block Operations**: Commands from controller to controllee
/// 4. **State Sync**: Responses from controllee to controller
/// 5. **Lifecycle**: `Close`/`Error` for termination
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SessionMessage {
    // =========================================================================
    // Connection Management
    // =========================================================================
    /// Attach to a session as a specific role.
    ///
    /// Sent by a peer to establish the session relationship.
    Attach {
        /// The instance ID of the attaching peer.
        peer: InstanceId,
        /// The session to attach to.
        session_id: SessionId,
        /// The role this peer will assume (typically `Controllee` or `Controller`).
        as_role: ControlRole,
    },

    /// Detach from a session.
    ///
    /// Graceful disconnection. The session may continue if other peers are attached.
    Detach {
        /// The instance ID of the detaching peer.
        peer: InstanceId,
        /// The session to detach from.
        session_id: SessionId,
    },

    // =========================================================================
    // Control Transfer (Bidirectional)
    // =========================================================================
    /// Yield control to peer.
    ///
    /// The sender transitions from `Controller` to `Neutral`.
    /// The receiver (if in `Controllee`) can then `AcquireControl` or remain passive.
    YieldControl {
        /// The instance ID of the yielding peer.
        peer: InstanceId,
        /// The session.
        session_id: SessionId,
    },

    /// Acquire control from peer.
    ///
    /// The sender attempts to become `Controller`.
    /// Valid when sender is `Neutral` or `Controllee` and peer is `Neutral`.
    AcquireControl {
        /// The instance ID of the peer acquiring control.
        peer: InstanceId,
        /// The session.
        session_id: SessionId,
    },

    // =========================================================================
    // Block Operations (Controller → Controllee)
    // =========================================================================
    /// Trigger staging of blocks (e.g., G3→G2).
    TriggerStaging {
        /// The session.
        session_id: SessionId,
    },

    /// Request that specific blocks be held (kept alive).
    HoldBlocks {
        /// The session.
        session_id: SessionId,
        /// Sequence hashes of blocks to hold.
        hold_hashes: Vec<SequenceHash>,
    },

    /// Release specific blocks (they can now be evicted).
    ReleaseBlocks {
        /// The session.
        session_id: SessionId,
        /// Sequence hashes of blocks to release.
        release_hashes: Vec<SequenceHash>,
    },

    /// Notify that blocks have been pulled via RDMA.
    ///
    /// The controllee can release these blocks from its hold.
    BlocksPulled {
        /// The session.
        session_id: SessionId,
        /// Sequence hashes of blocks that were pulled.
        pulled_hashes: Vec<SequenceHash>,
    },

    // =========================================================================
    // State Synchronization (Controllee → Controller)
    // =========================================================================
    /// Full state snapshot.
    ///
    /// Sent after attachment and periodically on state changes.
    StateResponse {
        /// The session.
        session_id: SessionId,
        /// Complete state snapshot.
        state: SessionStateSnapshot,
    },

    /// Notification that blocks have been staged.
    ///
    /// This message supports layerwise transfer by optionally specifying
    /// which layer range is ready. When `layer_range` is `None`, all layers
    /// of the staged blocks are ready for transfer.
    BlocksStaged {
        /// The session.
        session_id: SessionId,
        /// Newly staged blocks (now in target tier).
        staged_blocks: Vec<BlockInfo>,
        /// Count of blocks remaining to stage.
        remaining: usize,
        /// Layer range that is ready for transfer.
        ///
        /// - `None`: All layers are ready (default behavior)
        /// - `Some(0..1)`: Only layer 0 is ready
        /// - `Some(0..60)`: Layers 0-59 are ready
        ///
        /// This enables layerwise streaming where the sender computes
        /// layer-by-layer and notifies the receiver as each layer completes.
        layer_range: Option<Range<usize>>,
    },

    // =========================================================================
    // Lifecycle
    // =========================================================================
    /// Close the session gracefully.
    Close {
        /// The session.
        session_id: SessionId,
    },

    /// Report an error.
    Error {
        /// The session.
        session_id: SessionId,
        /// Error description.
        message: String,
    },
}

impl SessionMessage {
    /// Extract the session ID from any message variant.
    pub fn session_id(&self) -> SessionId {
        match self {
            SessionMessage::Attach { session_id, .. }
            | SessionMessage::Detach { session_id, .. }
            | SessionMessage::YieldControl { session_id, .. }
            | SessionMessage::AcquireControl { session_id, .. }
            | SessionMessage::TriggerStaging { session_id, .. }
            | SessionMessage::HoldBlocks { session_id, .. }
            | SessionMessage::ReleaseBlocks { session_id, .. }
            | SessionMessage::BlocksPulled { session_id, .. }
            | SessionMessage::StateResponse { session_id, .. }
            | SessionMessage::BlocksStaged { session_id, .. }
            | SessionMessage::Close { session_id, .. }
            | SessionMessage::Error { session_id, .. } => *session_id,
        }
    }

    /// Extract the peer instance ID if present.
    pub fn peer(&self) -> Option<InstanceId> {
        match self {
            SessionMessage::Attach { peer, .. }
            | SessionMessage::Detach { peer, .. }
            | SessionMessage::YieldControl { peer, .. }
            | SessionMessage::AcquireControl { peer, .. } => Some(*peer),
            _ => None,
        }
    }

    /// Check if this is a control command (sent by controller).
    pub fn is_control_command(&self) -> bool {
        matches!(
            self,
            SessionMessage::TriggerStaging { .. }
                | SessionMessage::HoldBlocks { .. }
                | SessionMessage::ReleaseBlocks { .. }
                | SessionMessage::BlocksPulled { .. }
        )
    }

    /// Check if this is a state response (sent by controllee).
    pub fn is_state_response(&self) -> bool {
        matches!(
            self,
            SessionMessage::StateResponse { .. } | SessionMessage::BlocksStaged { .. }
        )
    }

    /// Get the variant name as a string for logging.
    pub fn variant_name(&self) -> &'static str {
        match self {
            SessionMessage::Attach { .. } => "Attach",
            SessionMessage::Detach { .. } => "Detach",
            SessionMessage::YieldControl { .. } => "YieldControl",
            SessionMessage::AcquireControl { .. } => "AcquireControl",
            SessionMessage::TriggerStaging { .. } => "TriggerStaging",
            SessionMessage::HoldBlocks { .. } => "HoldBlocks",
            SessionMessage::ReleaseBlocks { .. } => "ReleaseBlocks",
            SessionMessage::BlocksPulled { .. } => "BlocksPulled",
            SessionMessage::StateResponse { .. } => "StateResponse",
            SessionMessage::BlocksStaged { .. } => "BlocksStaged",
            SessionMessage::Close { .. } => "Close",
            SessionMessage::Error { .. } => "Error",
        }
    }
}

/// Complete session state snapshot.
///
/// Sent in `SessionMessage::StateResponse` to provide the controller
/// with full visibility into the controllee's state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionStateSnapshot {
    /// Current session phase.
    pub phase: SessionPhase,
    /// Current control role of the sender.
    pub control_role: ControlRole,
    /// Blocks currently in G2 (ready for RDMA pull).
    pub g2_blocks: Vec<BlockInfo>,
    /// Count of blocks pending staging to G2.
    pub g3_pending: usize,
    /// Layer range that is ready for transfer.
    ///
    /// - `None`: All layers are ready (or not applicable)
    /// - `Some(0..1)`: Only layer 0 is ready
    /// - `Some(0..60)`: Layers 0-59 are ready
    ///
    /// This is updated when receiving `BlocksStaged` messages with `layer_range`.
    /// The controller can use this to know which layers can be pulled.
    #[serde(default)]
    pub ready_layer_range: Option<Range<usize>>,
}

/// Block information for session messages.
///
/// Contains the metadata needed to identify and transfer a block.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockInfo {
    /// Physical block ID in the layout.
    pub block_id: BlockId,
    /// Logical sequence hash.
    pub sequence_hash: SequenceHash,
    /// Layout handle for RDMA operations.
    pub layout_handle: LayoutHandle,
}