sz-orm-core 4.7.0

Core ORM engine: Model trait, ActiveRecord, QueryBuilder, Pool, Transaction, migration, and SQL dialect abstraction
Documentation
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
//! 缓存一致性协议模块(v4.1.0,`cache-coherence` feature gate)
//!
//! 实现 MESI 风格缓存一致性状态机,支持 WriteThrough/WriteBehind 策略,
//! 通过 trait-based 广播器实现跨实例失效广播。

use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{SystemTime, UNIX_EPOCH};

/// MESI 缓存行状态
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MesiState {
    /// 已修改(本地修改,其他实例无副本)
    Modified,
    /// 独占(本地独有,未修改,其他实例无副本)
    Exclusive,
    /// 共享(多实例共享只读副本)
    Shared,
    /// 无效(缓存行不存在或已失效)
    Invalid,
}

/// 写策略
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConsistencyStrategy {
    /// 写穿透(同步写缓存+DB)
    WriteThrough,
    /// 写后行(异步写 DB,先写缓存)
    WriteBehind,
}

/// 失效操作类型
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InvalidationOp {
    /// 修改操作
    Modify,
    /// 删除操作
    Delete,
}

/// 失效广播事件
#[derive(Debug, Clone)]
pub struct InvalidationEvent {
    /// 缓存键
    pub key: String,
    /// 发送实例 ID
    pub instance_id: String,
    /// 时间戳(Unix 毫秒)
    pub timestamp: u64,
    /// 操作类型
    pub op: InvalidationOp,
}

/// 失效广播器 trait(抽象消息队列,避免硬依赖具体 MQ 实现)
pub trait InvalidationBroadcaster: Send + Sync {
    /// 广播失效事件
    fn broadcast(&self, event: &InvalidationEvent) -> Result<(), CoherenceError>;
}

/// 一致性指标
#[derive(Debug, Clone, Default)]
pub struct CoherenceMetrics {
    /// Modified 状态计数
    pub modified_count: u64,
    /// Exclusive 状态计数
    pub exclusive_count: u64,
    /// Shared 状态计数
    pub shared_count: u64,
    /// Invalid 状态计数
    pub invalid_count: u64,
    /// 失效广播次数
    pub invalidation_broadcasts: u64,
    /// 一致性违规次数
    pub coherence_violations: u64,
    /// Write-behind 回滚次数
    pub write_behind_rollbacks: u64,
}

/// 一致性错误
#[derive(Debug, Clone, thiserror::Error)]
pub enum CoherenceError {
    /// 广播失败
    #[error("broadcast failed: {0}")]
    BroadcastFailed(String),
    /// Write-behind 失败
    #[error("write-behind failed for key: {key}")]
    WriteBehindFailed {
        /// 失败的缓存键
        key: String,
    },
    /// 脑裂检测
    #[error("split-brain detected for key: {key}")]
    SplitBrain {
        /// 发生脑裂的缓存键
        key: String,
    },
    /// 缓存未命中
    #[error("cache miss for key: {0}")]
    CacheMiss(String),
}

/// 缓存一致性协议
pub struct CacheCoherenceProtocol {
    /// 缓存行状态表(key → MESI 状态)
    states: RwLock<HashMap<String, MesiState>>,
    /// 广播器
    broadcaster: Arc<dyn InvalidationBroadcaster>,
    /// 实例 ID
    instance_id: String,
    /// 写策略
    strategy: ConsistencyStrategy,
    /// 指标
    metrics: Arc<RwLock<CoherenceMetrics>>,
}

impl CacheCoherenceProtocol {
    /// 创建新的一致性协议实例
    pub fn new(
        instance_id: String,
        strategy: ConsistencyStrategy,
        broadcaster: Arc<dyn InvalidationBroadcaster>,
    ) -> Self {
        Self {
            states: RwLock::new(HashMap::new()),
            broadcaster,
            instance_id,
            strategy,
            metrics: Arc::new(RwLock::new(CoherenceMetrics::default())),
        }
    }

    /// 获取缓存行状态
    pub fn state(&self, key: &str) -> MesiState {
        self.states
            .read()
            .unwrap()
            .get(key)
            .copied()
            .unwrap_or(MesiState::Invalid)
    }

    /// 读操作(触发状态转换:Invalid→Exclusive/Shared)
    pub fn read(&self, key: &str, other_instances_have: bool) -> MesiState {
        let mut states = self.states.write().unwrap();
        let mut metrics = self.metrics.write().unwrap();
        let current = states.get(key).copied().unwrap_or(MesiState::Invalid);
        let new_state = match current {
            MesiState::Invalid => {
                if other_instances_have {
                    MesiState::Shared
                } else {
                    MesiState::Exclusive
                }
            }
            other => other,
        };
        states.insert(key.to_string(), new_state);
        Self::update_metrics(&mut metrics, &new_state);
        new_state
    }

    /// 写操作(触发状态转换:→Modified,广播失效)
    pub fn write(&self, key: &str) -> Result<MesiState, CoherenceError> {
        let event = InvalidationEvent {
            key: key.to_string(),
            instance_id: self.instance_id.clone(),
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis() as u64,
            op: InvalidationOp::Modify,
        };
        self.broadcaster.broadcast(&event)?;

        let mut states = self.states.write().unwrap();
        let mut metrics = self.metrics.write().unwrap();
        states.insert(key.to_string(), MesiState::Modified);
        metrics.invalidation_broadcasts += 1;
        Self::update_metrics(&mut metrics, &MesiState::Modified);
        Ok(MesiState::Modified)
    }

    /// 处理收到的失效广播(→Invalid)
    pub fn handle_invalidation(&self, event: &InvalidationEvent) {
        if event.instance_id == self.instance_id {
            return;
        }
        let mut states = self.states.write().unwrap();
        let mut metrics = self.metrics.write().unwrap();
        states.insert(event.key.clone(), MesiState::Invalid);
        metrics.invalid_count += 1;
    }

    /// 获取指标快照
    pub fn metrics(&self) -> CoherenceMetrics {
        self.metrics.read().unwrap().clone()
    }

    /// 获取写策略
    pub fn strategy(&self) -> ConsistencyStrategy {
        self.strategy
    }

    fn update_metrics(metrics: &mut CoherenceMetrics, state: &MesiState) {
        match state {
            MesiState::Modified => metrics.modified_count += 1,
            MesiState::Exclusive => metrics.exclusive_count += 1,
            MesiState::Shared => metrics.shared_count += 1,
            MesiState::Invalid => metrics.invalid_count += 1,
        }
    }
}

/// 空广播器(单实例模式,不广播)
pub struct NoopBroadcaster;

impl InvalidationBroadcaster for NoopBroadcaster {
    fn broadcast(&self, _event: &InvalidationEvent) -> Result<(), CoherenceError> {
        Ok(())
    }
}

/// 本地广播器(收集事件用于测试)
pub struct LocalBroadcaster {
    events: RwLock<Vec<InvalidationEvent>>,
}

impl LocalBroadcaster {
    /// 创建本地广播器
    pub fn new() -> Self {
        Self {
            events: RwLock::new(Vec::new()),
        }
    }

    /// 获取已收集的事件
    pub fn events(&self) -> Vec<InvalidationEvent> {
        self.events.read().unwrap().clone()
    }
}

impl Default for LocalBroadcaster {
    fn default() -> Self {
        Self::new()
    }
}

impl InvalidationBroadcaster for LocalBroadcaster {
    fn broadcast(&self, event: &InvalidationEvent) -> Result<(), CoherenceError> {
        self.events.write().unwrap().push(event.clone());
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_mesi_state_transitions() {
        let broadcaster = Arc::new(LocalBroadcaster::new());
        let protocol = CacheCoherenceProtocol::new(
            "instance-A".to_string(),
            ConsistencyStrategy::WriteThrough,
            broadcaster,
        );

        assert_eq!(protocol.state("key1"), MesiState::Invalid);

        let s = protocol.read("key1", false);
        assert_eq!(s, MesiState::Exclusive);

        let s = protocol.read("key1", true);
        assert_eq!(s, MesiState::Exclusive);

        let s = protocol.write("key1").unwrap();
        assert_eq!(s, MesiState::Modified);
        assert_eq!(protocol.state("key1"), MesiState::Modified);
    }

    #[test]
    fn test_invalid_to_shared() {
        let broadcaster = Arc::new(LocalBroadcaster::new());
        let protocol = CacheCoherenceProtocol::new(
            "instance-A".to_string(),
            ConsistencyStrategy::WriteThrough,
            broadcaster,
        );

        let s = protocol.read("key1", true);
        assert_eq!(s, MesiState::Shared);
    }

    #[test]
    fn test_invalid_to_exclusive() {
        let broadcaster = Arc::new(LocalBroadcaster::new());
        let protocol = CacheCoherenceProtocol::new(
            "instance-A".to_string(),
            ConsistencyStrategy::WriteThrough,
            broadcaster,
        );

        let s = protocol.read("key1", false);
        assert_eq!(s, MesiState::Exclusive);
    }

    #[test]
    fn test_write_broadcasts_invalidation() {
        let broadcaster = Arc::new(LocalBroadcaster::new());
        let protocol = CacheCoherenceProtocol::new(
            "instance-A".to_string(),
            ConsistencyStrategy::WriteThrough,
            broadcaster.clone(),
        );

        protocol.write("key1").unwrap();
        let events = broadcaster.events();
        assert_eq!(events.len(), 1);
        assert_eq!(events[0].key, "key1");
        assert_eq!(events[0].op, InvalidationOp::Modify);
    }

    #[test]
    fn test_handle_invalidation_sets_invalid() {
        let broadcaster = Arc::new(LocalBroadcaster::new());
        let protocol = CacheCoherenceProtocol::new(
            "instance-A".to_string(),
            ConsistencyStrategy::WriteThrough,
            broadcaster,
        );

        protocol.read("key1", false);
        assert_eq!(protocol.state("key1"), MesiState::Exclusive);

        let event = InvalidationEvent {
            key: "key1".to_string(),
            instance_id: "instance-B".to_string(),
            timestamp: 0,
            op: InvalidationOp::Modify,
        };
        protocol.handle_invalidation(&event);
        assert_eq!(protocol.state("key1"), MesiState::Invalid);
    }

    #[test]
    fn test_ignore_self_invalidation() {
        let broadcaster = Arc::new(LocalBroadcaster::new());
        let protocol = CacheCoherenceProtocol::new(
            "instance-A".to_string(),
            ConsistencyStrategy::WriteThrough,
            broadcaster,
        );

        protocol.read("key1", false);
        assert_eq!(protocol.state("key1"), MesiState::Exclusive);

        let event = InvalidationEvent {
            key: "key1".to_string(),
            instance_id: "instance-A".to_string(),
            timestamp: 0,
            op: InvalidationOp::Modify,
        };
        protocol.handle_invalidation(&event);
        assert_eq!(protocol.state("key1"), MesiState::Exclusive);
    }

    #[test]
    fn test_metrics_tracking() {
        let broadcaster = Arc::new(LocalBroadcaster::new());
        let protocol = CacheCoherenceProtocol::new(
            "instance-A".to_string(),
            ConsistencyStrategy::WriteThrough,
            broadcaster,
        );

        protocol.read("key1", false);
        protocol.read("key2", true);
        protocol.write("key1").unwrap();

        let metrics = protocol.metrics();
        assert!(metrics.exclusive_count > 0);
        assert!(metrics.shared_count > 0);
        assert!(metrics.modified_count > 0);
        assert!(metrics.invalidation_broadcasts > 0);
    }

    #[test]
    fn test_noop_broadcaster() {
        let broadcaster = Arc::new(NoopBroadcaster);
        let protocol = CacheCoherenceProtocol::new(
            "instance-A".to_string(),
            ConsistencyStrategy::WriteBehind,
            broadcaster,
        );

        let result = protocol.write("key1");
        assert!(result.is_ok());
    }

    #[test]
    fn test_shared_to_modified_on_write() {
        let broadcaster = Arc::new(LocalBroadcaster::new());
        let protocol = CacheCoherenceProtocol::new(
            "instance-A".to_string(),
            ConsistencyStrategy::WriteThrough,
            broadcaster,
        );

        let s = protocol.read("key1", true);
        assert_eq!(s, MesiState::Shared);

        let s = protocol.write("key1").unwrap();
        assert_eq!(s, MesiState::Modified);
    }

    #[test]
    fn test_strategy_access() {
        let broadcaster = Arc::new(NoopBroadcaster);
        let protocol = CacheCoherenceProtocol::new(
            "instance-A".to_string(),
            ConsistencyStrategy::WriteBehind,
            broadcaster,
        );
        assert_eq!(protocol.strategy(), ConsistencyStrategy::WriteBehind);
    }
}