vecboost 0.2.0

High-performance embedding vector service written in Rust
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Copyright (c) 2025-2026 Kirky.X
//
// Licensed under MIT License
// See LICENSE file in the project root for full license information

#![allow(clippy::all)]

use log::{debug, error, info, warn};
use std::sync::Arc;
use tokio::sync::RwLock;

use super::config::{BufferPoolConfig, CudaPoolConfig, ModelWeightPoolConfig};
use super::{BufferPool, BufferPoolStats, CudaMemoryPool, ModelPoolStats, ModelWeightPool};
use crate::error::VecboostError;

/// 内存池管理器
///
/// 统一管理所有内存池
pub struct MemoryPoolManager {
    /// 缓冲区池
    buffer_pool: Arc<RwLock<BufferPool>>,
    /// 模型权重池
    model_pool: Arc<RwLock<ModelWeightPool>>,
    /// CUDA 池
    cuda_pool: Option<Arc<RwLock<CudaMemoryPool>>>,
    /// 配置
    config: MemoryPoolConfig,
}

/// 内存池配置
#[derive(Debug, Clone)]
pub struct MemoryPoolConfig {
    /// 缓冲区池配置
    pub buffer_pool: BufferPoolConfig,
    /// 模型权重池配置
    pub model_pool: ModelWeightPoolConfig,
    /// CUDA 池配置
    pub cuda_pool: CudaPoolConfig,
}

impl MemoryPoolManager {
    /// 创建新的内存池管理器
    pub fn new(config: MemoryPoolConfig) -> Self {
        info!("Creating MemoryPoolManager");

        Self {
            buffer_pool: Arc::new(RwLock::new(BufferPool::new(config.buffer_pool.clone()))),
            model_pool: Arc::new(RwLock::new(ModelWeightPool::new(
                "default".to_string(),
                config.model_pool.clone(),
            ))),
            cuda_pool: None, // 需要设备信息,稍后初始化
            config,
        }
    }

    /// 初始化 CUDA 池
    pub async fn initialize_cuda_pool(&mut self, device_id: i32) -> Result<(), VecboostError> {
        if !self.config.cuda_pool.enabled {
            info!("CUDA pool disabled, skipping initialization");
            return Ok(());
        }

        info!("Initializing CUDA pool for device {}...", device_id);

        let pool = CudaMemoryPool::new(device_id, self.config.cuda_pool.clone()).map_err(|e| {
            VecboostError::ConfigError(format!("Failed to initialize CUDA pool: {}", e))
        })?;

        self.cuda_pool = Some(Arc::new(RwLock::new(pool)));

        info!("CUDA pool initialized successfully");
        Ok(())
    }

    /// 初始化所有池

    pub async fn initialize_all(
        &mut self,
        device: Option<candle_core::Device>,
        cuda_device_id: Option<i32>,
    ) -> Result<(), VecboostError> {
        info!("Initializing all memory pools...");

        // 先验证所有必需的参数

        if self.config.cuda_pool.enabled && cuda_device_id.is_none() {
            return Err(VecboostError::ConfigError(
                "CUDA pool requires device_id but none provided".to_string(),
            ));
        }

        // 记录初始化状态

        let mut initialized_pools = Vec::new();

        // 初始化缓冲区池

        if self.config.buffer_pool.enabled {
            let mut buffer_pool = self.buffer_pool.write().await;

            buffer_pool.preallocate();

            initialized_pools.push("buffer_pool");

            info!("Buffer pool initialized");
        }

        // 初始化模型权重池

        if self.config.model_pool.enabled {
            initialized_pools.push("model_pool");

            info!("Model weight pool initialized");
        }

        // 初始化 CUDA 池

        if let Some(device_id) = cuda_device_id {
            match self.initialize_cuda_pool(device_id).await {
                Ok(_) => {
                    initialized_pools.push("cuda_pool");

                    info!("CUDA pool initialized");
                }

                Err(e) => {
                    // 回滚已初始化的池

                    error!("Failed to initialize CUDA pool: {}, rolling back", e);

                    self.clear_all().await;

                    return Err(VecboostError::ConfigError(format!(
                        "Failed to initialize CUDA pool: {}. Rollback completed.",
                        e
                    )));
                }
            }
        }

        info!(
            "All memory pools initialized successfully: {:?}",
            initialized_pools
        );

        Ok(())
    }

    /// 获取缓冲区池
    pub async fn get_buffer_pool(&self) -> Arc<RwLock<BufferPool>> {
        Arc::clone(&self.buffer_pool)
    }

    /// 获取模型权重池
    pub async fn get_model_pool(&self) -> Arc<RwLock<ModelWeightPool>> {
        Arc::clone(&self.model_pool)
    }

    /// 获取 CUDA 池
    pub async fn get_cuda_pool(&self) -> Option<Arc<RwLock<CudaMemoryPool>>> {
        self.cuda_pool.clone()
    }

    /// 获取内存统计信息
    pub async fn get_memory_stats(&self) -> MemoryPoolStats {
        let buffer_pool = self.buffer_pool.read().await;
        let model_pool = self.model_pool.read().await;

        let mut stats = MemoryPoolStats {
            buffer_pool_enabled: self.config.buffer_pool.enabled,
            buffer_pool_stats: Some(buffer_pool.get_stats()),
            model_pool_enabled: self.config.model_pool.enabled,
            model_pool_stats: Some(model_pool.get_stats()),
            cuda_pool_enabled: self.config.cuda_pool.enabled,
            cuda_pool_stats: None,
        };

        if let Some(ref cuda_pool) = self.cuda_pool {
            let pool = cuda_pool.read().await;
            let (used, total) = pool.get_memory_usage();
            stats.cuda_pool_stats = Some(CudaPoolStats {
                used_memory_mb: used / 1024 / 1024,
                total_memory_mb: total / 1024 / 1024,
                memory_usage_percent: pool.get_memory_usage_percent(),
            });
        }

        stats
    }

    /// 清空所有池
    pub async fn clear_all(&self) {
        info!("Clearing all memory pools...");

        {
            let mut buffer_pool = self.buffer_pool.write().await;
            buffer_pool.clear();
        }

        {
            let mut model_pool = self.model_pool.write().await;
            model_pool.clear();
        }

        if let Some(ref cuda_pool) = self.cuda_pool {
            let mut pool = cuda_pool.write().await;
            pool.clear();
        }

        info!("All memory pools cleared");
    }
}

/// 内存池统计信息
#[derive(Debug, Clone)]
pub struct MemoryPoolStats {
    /// 缓冲区池是否启用
    pub buffer_pool_enabled: bool,
    /// 缓冲区池统计
    pub buffer_pool_stats: Option<super::BufferPoolStats>,
    /// 模型权重池是否启用
    pub model_pool_enabled: bool,
    /// 模型权重池统计
    pub model_pool_stats: Option<super::ModelPoolStats>,
    /// CUDA 池是否启用
    pub cuda_pool_enabled: bool,
    /// CUDA 池统计
    pub cuda_pool_stats: Option<CudaPoolStats>,
}

/// CUDA 池统计信息
#[derive(Debug, Clone)]
pub struct CudaPoolStats {
    /// 已使用内存(MB)
    pub used_memory_mb: u64,
    /// 总内存(MB)
    pub total_memory_mb: u64,
    /// 内存使用率(百分比)
    pub memory_usage_percent: f64,
}

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

    #[test]
    fn test_pool_manager_creation() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig::default(),
            model_pool: ModelWeightPoolConfig::default(),
            cuda_pool: CudaPoolConfig::default(),
        };

        let manager = MemoryPoolManager::new(config);
        assert!(manager.cuda_pool.is_none());
    }

    #[tokio::test]
    async fn test_get_buffer_pool() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig::default(),
            model_pool: ModelWeightPoolConfig::default(),
            cuda_pool: CudaPoolConfig::default(),
        };

        let manager = MemoryPoolManager::new(config);
        let buffer_pool = manager.get_buffer_pool().await;

        let stats = buffer_pool.read().await.get_stats();
        assert_eq!(stats.text_allocations, 0);
    }

    #[tokio::test]
    async fn test_get_model_pool() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig::default(),
            model_pool: ModelWeightPoolConfig::default(),
            cuda_pool: CudaPoolConfig::default(),
        };

        let manager = MemoryPoolManager::new(config);
        let model_pool = manager.get_model_pool().await;

        let stats = model_pool.read().await.get_stats();
        assert_eq!(stats.loaded_models, 0);
    }

    #[tokio::test]
    async fn test_initialize_all() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig::default(),
            model_pool: ModelWeightPoolConfig::default(),
            cuda_pool: CudaPoolConfig {
                enabled: false, // 禁用 CUDA 池以避免需要 device_id
                ..Default::default()
            },
        };

        let mut manager = MemoryPoolManager::new(config);

        let result = manager.initialize_all(None, None).await;
        assert!(result.is_ok(), "initialize_all failed: {:?}", result);

        let stats = manager.get_memory_stats().await;
        assert!(stats.buffer_pool_stats.is_some());
        assert!(stats.model_pool_stats.is_some());
    }

    #[tokio::test]
    async fn test_clear_all() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig::default(),
            model_pool: ModelWeightPoolConfig::default(),
            cuda_pool: CudaPoolConfig::default(),
        };

        let manager = MemoryPoolManager::new(config);
        manager.clear_all().await;
    }

    #[tokio::test]
    async fn test_initialize_cuda_pool_disabled() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig::default(),
            model_pool: ModelWeightPoolConfig::default(),
            cuda_pool: CudaPoolConfig {
                enabled: false,
                ..Default::default()
            },
        };
        let mut manager = MemoryPoolManager::new(config);

        let result = manager.initialize_cuda_pool(0).await;
        assert!(result.is_ok());
        assert!(manager.cuda_pool.is_none());
    }

    #[tokio::test]
    async fn test_initialize_cuda_pool_enabled() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig::default(),
            model_pool: ModelWeightPoolConfig::default(),
            cuda_pool: CudaPoolConfig {
                enabled: true,
                max_memory_mb: 1024,
            },
        };
        let mut manager = MemoryPoolManager::new(config);

        let result = manager.initialize_cuda_pool(0).await;
        assert!(result.is_ok());
        assert!(manager.cuda_pool.is_some());
    }

    #[tokio::test]
    async fn test_get_cuda_pool_returns_none_before_init() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig::default(),
            model_pool: ModelWeightPoolConfig::default(),
            cuda_pool: CudaPoolConfig::default(),
        };
        let manager = MemoryPoolManager::new(config);

        assert!(manager.get_cuda_pool().await.is_none());
    }

    #[tokio::test]
    async fn test_initialize_all_error_cuda_enabled_no_device_id() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig::default(),
            model_pool: ModelWeightPoolConfig::default(),
            cuda_pool: CudaPoolConfig {
                enabled: true,
                ..Default::default()
            },
        };
        let mut manager = MemoryPoolManager::new(config);

        let result = manager.initialize_all(None, None).await;
        assert!(result.is_err());
        match result.unwrap_err() {
            VecboostError::ConfigError(msg) => {
                assert!(msg.contains("CUDA pool requires device_id"));
            }
            other => panic!("Expected ConfigError, got {:?}", other),
        }
    }

    #[tokio::test]
    async fn test_initialize_all_all_enabled() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig {
                enabled: true,
                text_buffer_sizes: vec![16],
                vector_buffer_sizes: vec![16],
                pool_size_per_size: 2,
            },
            model_pool: ModelWeightPoolConfig {
                enabled: true,
                ..Default::default()
            },
            cuda_pool: CudaPoolConfig {
                enabled: true,
                max_memory_mb: 1024,
            },
        };
        let mut manager = MemoryPoolManager::new(config);

        let result = manager
            .initialize_all(Some(candle_core::Device::Cpu), Some(0))
            .await;
        assert!(result.is_ok(), "initialize_all failed: {:?}", result);

        assert!(manager.cuda_pool.is_some());
    }

    #[tokio::test]
    async fn test_get_memory_stats_with_all_pools() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig {
                enabled: true,
                text_buffer_sizes: vec![16],
                vector_buffer_sizes: vec![16],
                pool_size_per_size: 2,
            },
            model_pool: ModelWeightPoolConfig {
                enabled: true,
                ..Default::default()
            },
            cuda_pool: CudaPoolConfig {
                enabled: true,
                max_memory_mb: 1024,
            },
        };
        let mut manager = MemoryPoolManager::new(config);

        manager
            .initialize_all(Some(candle_core::Device::Cpu), Some(0))
            .await
            .unwrap();

        let stats = manager.get_memory_stats().await;
        assert!(stats.buffer_pool_enabled);
        assert!(stats.model_pool_enabled);
        assert!(stats.cuda_pool_enabled);
        assert!(stats.buffer_pool_stats.is_some());
        assert!(stats.model_pool_stats.is_some());
        assert!(stats.cuda_pool_stats.is_some());

        let buffer_stats = stats.buffer_pool_stats.unwrap();
        assert!(buffer_stats.current_text_pool_size > 0);

        let cuda_stats = stats.cuda_pool_stats.unwrap();
        assert_eq!(cuda_stats.used_memory_mb, 0);
        // cuda feature 下 CudaMemoryPool 使用 config.max_memory_mb(1024);
        // 非 cuda feature 下走 no-op 分支,get_memory_usage() 返回 (0, 0)。
        #[cfg(feature = "cuda")]
        assert_eq!(cuda_stats.total_memory_mb, 1024);
        #[cfg(not(feature = "cuda"))]
        assert_eq!(cuda_stats.total_memory_mb, 0);
        assert!((cuda_stats.memory_usage_percent - 0.0).abs() < 0.001);
    }

    #[tokio::test]
    async fn test_get_memory_stats_disabled_pools() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig {
                enabled: false,
                ..Default::default()
            },
            model_pool: ModelWeightPoolConfig {
                enabled: false,
                ..Default::default()
            },
            cuda_pool: CudaPoolConfig {
                enabled: false,
                ..Default::default()
            },
        };
        let manager = MemoryPoolManager::new(config);

        let stats = manager.get_memory_stats().await;
        assert!(!stats.buffer_pool_enabled);
        assert!(!stats.model_pool_enabled);
        assert!(!stats.cuda_pool_enabled);
        assert!(stats.cuda_pool_stats.is_none());
        assert!(stats.buffer_pool_stats.is_some());
        assert!(stats.model_pool_stats.is_some());
    }

    #[tokio::test]
    async fn test_clear_all_after_initialization() {
        let config = MemoryPoolConfig {
            buffer_pool: BufferPoolConfig {
                enabled: true,
                text_buffer_sizes: vec![16],
                vector_buffer_sizes: vec![16],
                pool_size_per_size: 2,
            },
            model_pool: ModelWeightPoolConfig::default(),
            cuda_pool: CudaPoolConfig {
                enabled: true,
                ..Default::default()
            },
        };
        let mut manager = MemoryPoolManager::new(config);
        manager
            .initialize_all(Some(candle_core::Device::Cpu), Some(0))
            .await
            .unwrap();

        manager.clear_all().await;

        let stats = manager.get_memory_stats().await;
        let buffer_stats = stats.buffer_pool_stats.as_ref().unwrap();
        assert_eq!(buffer_stats.current_text_pool_size, 0);
        assert_eq!(buffer_stats.current_vector_pool_size, 0);
    }
}