"lib/runtime/src/discovery/kube/crd.rs" did not exist on "d2faf0e6f5ebb0edc9334cd7da4fc1d85f608437"
state.rs 5.31 KB
Newer Older
Ryan Olson's avatar
Ryan Olson committed
1
2
3
4
5
6
7
8
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use std::sync::Arc;

use derive_getters::Getters;

use super::Result;
9
use super::registry::{BlockHandle, RegistrationHandle};
Ryan Olson's avatar
Ryan Olson committed
10
11
12
13
14
15
16
17
18
19
20
use crate::tokens::{PartialTokenBlock, SaltHash, Token, TokenBlock, Tokens};

#[derive(Debug, thiserror::Error)]
#[error("Block state is invalid: {0}")]
pub struct BlockStateInvalid(pub String);

#[derive(Debug)]
pub enum BlockState {
    Reset,
    Partial(PartialState),
    Complete(CompleteState),
21
    Registered(Arc<RegistrationHandle>, Arc<BlockHandle>),
Ryan Olson's avatar
Ryan Olson committed
22
23
24
25
26
27
28
29
30
31
32
33
}

impl BlockState {
    pub fn initialize_sequence(
        &mut self,
        page_size: usize,
        salt_hash: SaltHash,
    ) -> Result<(), BlockStateInvalid> {
        if !matches!(self, BlockState::Reset) {
            return Err(BlockStateInvalid("Block is not reset".to_string()));
        }

34
        let block = PartialTokenBlock::create_sequence_root(page_size as u32, salt_hash);
Ryan Olson's avatar
Ryan Olson committed
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
        *self = BlockState::Partial(PartialState::new(block));
        Ok(())
    }

    pub fn add_token(&mut self, token: Token) -> Result<()> {
        match self {
            BlockState::Partial(state) => Ok(state.block.push_token(token)?),
            _ => Err(BlockStateInvalid("Block is not partial".to_string()))?,
        }
    }

    pub fn add_tokens(&mut self, tokens: Tokens) -> Result<Tokens> {
        match self {
            BlockState::Partial(state) => Ok(state.block.push_tokens(tokens)),
            _ => Err(BlockStateInvalid("Block is not partial".to_string()))?,
        }
    }

    pub fn pop_token(&mut self) -> Result<()> {
        match self {
            BlockState::Partial(state) => {
                state.block.pop_token()?;
                Ok(())
            }
            _ => Err(BlockStateInvalid("Block is not partial".to_string()))?,
        }
    }

    pub fn pop_tokens(&mut self, count: usize) -> Result<()> {
        match self {
            BlockState::Partial(state) => {
                state.block.pop_tokens(count)?;
                Ok(())
            }
            _ => Err(BlockStateInvalid("Block is not partial".to_string()))?,
        }
    }

    pub fn commit(&mut self) -> Result<()> {
        match self {
            BlockState::Partial(state) => {
                let token_block = state.block.commit()?;
                *self = BlockState::Complete(CompleteState::new(token_block));
                Ok(())
            }
            _ => Err(BlockStateInvalid("Block is not partial".to_string()))?,
        }
    }

Ryan Olson's avatar
Ryan Olson committed
84
85
86
    /// Apply an entry [TokenBlock] to the block.
    /// The block must be in the reset state on entry. The block will transition to
    /// the completed state after this call.
Ryan Olson's avatar
Ryan Olson committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    pub fn apply_token_block(&mut self, token_block: TokenBlock) -> Result<()> {
        match self {
            BlockState::Reset => {
                *self = BlockState::Complete(CompleteState::new(token_block));
                Ok(())
            }
            _ => Err(BlockStateInvalid("Block is not reset".to_string()))?,
        }
    }

    /// Returns the number of tokens currently in the block.
    pub fn len(&self) -> Option<usize> {
        match self {
            BlockState::Reset => Some(0),
            BlockState::Partial(state) => Some(state.block.len()),
            BlockState::Complete(state) => Some(state.token_block.tokens().len()),
103
            BlockState::Registered(_, _) => None,
Ryan Olson's avatar
Ryan Olson committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
        }
    }

    /// Returns the number of additional tokens that can be added.
    pub fn remaining(&self) -> usize {
        match self {
            BlockState::Partial(state) => state.block.remaining(),
            _ => 0, // Reset, Complete, Registered have 0 remaining capacity
        }
    }

    /// Returns true if the block contains no tokens.
    pub fn is_empty(&self) -> bool {
        match self {
            BlockState::Reset => true,
            BlockState::Partial(state) => state.block.is_empty(),
120
121
            BlockState::Complete(_) => false,      // Always full
            BlockState::Registered(_, _) => false, // Always full
Ryan Olson's avatar
Ryan Olson committed
122
123
124
125
126
127
        }
    }

    /// Returns a reference to the underlying TokenBlock if the state is Complete or Registered.
    pub fn tokens(&self) -> Option<&Tokens> {
        match self {
128
            BlockState::Reset | BlockState::Registered(_, _) => None,
Ryan Olson's avatar
Ryan Olson committed
129
130
131
132
133
134
135
136
137
138
139
140
            BlockState::Partial(state) => Some(state.block.tokens()),
            BlockState::Complete(state) => Some(state.token_block.tokens()),
        }
    }

    /// Returns true if the block is empty
    pub fn is_reset(&self) -> bool {
        matches!(self, BlockState::Reset)
    }

    /// Returns true if the block is in the complete or registered state
    pub fn is_complete(&self) -> bool {
141
        matches!(self, BlockState::Complete(_) | BlockState::Registered(_, _))
Ryan Olson's avatar
Ryan Olson committed
142
143
144
145
    }

    /// Returns true if the block is in the registered state
    pub fn is_registered(&self) -> bool {
146
        matches!(self, BlockState::Registered(_state, _))
Ryan Olson's avatar
Ryan Olson committed
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    }
}

#[derive(Debug)]
pub struct PartialState {
    block: PartialTokenBlock,
}

impl PartialState {
    pub fn new(block: PartialTokenBlock) -> Self {
        Self { block }
    }
}

#[derive(Debug, Getters)]
pub struct CompleteState {
    token_block: TokenBlock,
}

impl CompleteState {
    pub fn new(token_block: TokenBlock) -> Self {
        Self { token_block }
    }
}