surmux 0.2.0

极简 SurrealDB WebSocket 客户端 — 单连接 JSON-RPC 多路复用, 极简依赖
Documentation
//! 预存数据 → 高并发分配集成测试.
//!
//! 场景: 预种 100 万条资源池记录, 然后 256 路 worker 并发 claim.
//!
//! 3 种分配策略 (worker 按 w % 3 轮换), 仅在 status marker 与 RETURN 子句上不同:
//! - **策略 A**: `UPDATE ... SET status='claimed_A' ... RETURN AFTER`
//! - **策略 B**: 同 A, RETURN BEFORE (取旧 payload, 对比 payload 大小对吞吐影响)
//! - **策略 C**: 同 A, status='claimed_C'
//!
//! 为什么不真做事务分配: SurrealPool::client() 是 round-robin (fetch_add),
//! 同一 worker 多次调用 pool.client() 会落到不同 WS 连接.
//! BEGIN/LET 与 UPDATE 若在池路径上跑, 会因跨连接而事务失效.
//! 当前测试只测简单 UPDATE 的纯并发吞吐, 事务路径用单连接另测 (本测试不覆盖).
//!
//! 索引分配: 步进法 (worker w 取 idx=w, w+W, w+2W...) 保证零冲突,
//! 测量纯并发 UPDATE 吞吐, 与 SurrealDB 调度无关.
//!
//! 验证:
//! 1. 已分配 idx 数量 == 预期 (本地累加器 == DB count == expected)
//! 2. 无重复分配 (SELECT DISTINCT idx WHERE status != 'free' 验证去重数)
//! 3. 抽样 round-trip (状态/worker/idx 字段一致)
//!
//! 运行: `SURMUX_PASS='...' cargo test --test alloc_pool -- --nocapture --test-threads=1`

mod common;

use std::sync::Arc;
use std::time::Instant;

use surmux::{SurrealClient, SurrealPool};

use common::{CountResult, build_config};

// ============================================================
// 数据结构
// ============================================================

#[derive(Debug, serde::Deserialize)]
struct PoolRow {
    #[allow(dead_code)]
    id: serde_json::Value,
    #[allow(dead_code)]
    idx: i64,
    status: String,
    #[serde(default)]
    worker: Option<i64>,
}

// ============================================================
// 预种: 在服务端用 FOR 循环高效生成.
// ============================================================

/// 用 `FOR $i IN 0..N { CREATE alloc_pool CONTENT { ... } }` 服务端批量生成.
///
/// SurrealDB 在服务端内部处理 FOR 循环, 避免逐条 INSERT 的 RPC 开销.
/// 每批 50k 条, 分多轮执行避免单次查询超时.
async fn seed_pool(client: &SurrealClient, total: usize) -> Result<(), String> {
    let batch = 50_000;
    let batches = total.div_ceil(batch);

    // 清表 + 建 schema
    common::ensure_table(client, "alloc_pool").await?;
    client
        .query("DEFINE INDEX idx_unique ON alloc_pool FIELDS idx UNIQUE")
        .exec()
        .await
        .map_err(|e| format!("建索引: {e}"))?;

    let start = Instant::now();
    for b in 0..batches {
        let from = b * batch;
        let to = ((b + 1) * batch).min(total) - 1; // inclusive
        client
            .query(format!(
                "FOR $i IN {from}..={to} {{ CREATE alloc_pool CONTENT {{ idx: $i, status: 'free', worker: NONE }} }}",
            ))
            .exec()
            .await
            .map_err(|e| format!("种子批 {b} ({from}..{to}): {e}"))?;

        let elapsed = start.elapsed();
        let done = to + 1;
        let rate = done as f64 / elapsed.as_secs_f64().max(0.1);
        println!(
            "  种子: {done}/{total} ({:.0}%  {:.0} rec/s)",
            done as f64 / total as f64 * 100.0,
            rate
        );
    }
    println!(
        "✅ 种子完成 {total} 条 {:.1}s",
        start.elapsed().as_secs_f64()
    );
    Ok(())
}

// ============================================================
// 分配策略
// ============================================================

/// 步进索引: worker w 从 w 开始, 每次 +WORKERS, 直到超出 total.
/// 返回 worker 负责的所有 idx.
fn strided_indices(
    worker_id: usize,
    total_workers: usize,
    total_seed: usize,
    quota: usize,
) -> Vec<i64> {
    let mut indices = Vec::with_capacity(quota);
    let mut idx = worker_id as i64;
    while indices.len() < quota && (idx as usize) < total_seed {
        indices.push(idx);
        idx += total_workers as i64;
    }
    indices
}

/// 通用分配: UPDATE ... SET status = '$suffix', worker = $w WHERE idx = $i AND status = 'free' RETURN [BEFORE|AFTER]
///
/// 三策略 (A/B/C) 仅在 marker 与 RETURN 子句上不同, 合并在此处.
async fn claim_one(
    client: &SurrealClient,
    worker_id: i64,
    indices: &[i64],
    suffix: &str,
    return_before: bool,
) -> usize {
    let ret = if return_before { "BEFORE" } else { "AFTER" };
    let sql = format!(
        "UPDATE alloc_pool SET status = $s, worker = $w WHERE idx = $i AND status = 'free' RETURN {ret}",
    );
    let mut claimed = 0usize;
    for &idx in indices {
        // Err: 已被别人抢走 (步进索引下不会发生)
        if client
            .query(&sql)
            .bind("s", suffix)
            .bind("w", worker_id)
            .bind("i", idx)
            .exec()
            .await
            .is_ok()
        {
            claimed += 1;
        }
    }
    claimed
}

/// 分配阶段入口: 按 strategy 选 suffix 与 return_before.
async fn claim_strategy(
    client: &SurrealClient,
    worker_id: i64,
    indices: &[i64],
    strategy: usize,
) -> usize {
    match strategy {
        0 => claim_one(client, worker_id, indices, "claimed_A", false).await,
        1 => claim_one(client, worker_id, indices, "claimed_B", true).await,
        _ => claim_one(client, worker_id, indices, "claimed_C", false).await,
    }
}

// ============================================================
// 测试 1: SurrealPool 4 连接, 256 路并发分配 (步进索引, 零冲突)
// ============================================================

#[tokio::test(flavor = "multi_thread")]
async fn pool_4conn_256w_claim_from_1m_pool() {
    const TOTAL_SEED: usize = 1_000_000;
    const WORKERS: usize = 256;
    const POOL_SIZE: usize = 4;
    const QUOTA_PER_WORKER: usize = 500; // 256 × 500 = 128k 条分配

    let config = build_config("surmux_alloc_test", "alloc_1m_pool", 600);
    let pool = SurrealPool::connect(&config, POOL_SIZE)
        .await
        .expect("pool connect");

    // ===== 阶段 1: 预种 100 万 =====
    println!("=== 阶段 1: 预种 {TOTAL_SEED} 条 (每批 50k) ===");
    let seed_start = Instant::now();
    {
        let client = pool.client().clone();
        seed_pool(&client, TOTAL_SEED).await.expect("种子失败");
    }
    println!("预种耗时: {:.1}s", seed_start.elapsed().as_secs_f64());

    // 验证种子数
    let cr: CountResult = pool
        .client()
        .query("SELECT count() FROM alloc_pool GROUP ALL")
        .one()
        .await
        .expect("count")
        .expect("count result");
    println!("验证: 实际 {} 条 (期望 {TOTAL_SEED})", cr.count);
    assert_eq!(cr.count as usize, TOTAL_SEED, "种子数不匹配");

    // ===== 阶段 2: 256 路并发分配 =====
    println!(
        "=== 阶段 2: {WORKERS} worker × {QUOTA_PER_WORKER} = {} 条 (Pool {POOL_SIZE} 连接, 步进索引) ===",
        WORKERS * QUOTA_PER_WORKER
    );
    let claimed_a = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let claimed_b = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let claimed_c = Arc::new(std::sync::atomic::AtomicUsize::new(0));
    let alloc_start = Instant::now();

    let mut handles = Vec::with_capacity(WORKERS);
    for w in 0..WORKERS {
        let pool = pool.clone();
        let claimed_a = claimed_a.clone();
        let claimed_b = claimed_b.clone();
        let claimed_c = claimed_c.clone();
        let strategy = w % 3;

        handles.push(tokio::spawn(async move {
            let client = pool.client().clone();
            let worker_id = w as i64;
            // 每个 worker 预计算自己的步进索引列表
            let indices = strided_indices(w, WORKERS, TOTAL_SEED, QUOTA_PER_WORKER);
            let n = claim_strategy(&client, worker_id, &indices, strategy).await;
            match strategy {
                0 => {
                    claimed_a.fetch_add(n, std::sync::atomic::Ordering::Relaxed);
                }
                1 => {
                    claimed_b.fetch_add(n, std::sync::atomic::Ordering::Relaxed);
                }
                _ => {
                    claimed_c.fetch_add(n, std::sync::atomic::Ordering::Relaxed);
                }
            }
            n
        }));
    }

    let mut total_claimed = 0usize;
    for h in handles {
        total_claimed += h.await.expect("worker task panic");
    }

    let alloc_elapsed = alloc_start.elapsed();
    let claimed_a_val = claimed_a.load(std::sync::atomic::Ordering::Relaxed);
    let claimed_b_val = claimed_b.load(std::sync::atomic::Ordering::Relaxed);
    let claimed_c_val = claimed_c.load(std::sync::atomic::Ordering::Relaxed);

    println!(
        "[pool {POOL_SIZE} conn] 已分配 {} 条 (A={claimed_a_val} B={claimed_b_val} C={claimed_c_val}) in {:.2}s → {:.0} alloc/s",
        total_claimed,
        alloc_elapsed.as_secs_f64(),
        total_claimed as f64 / alloc_elapsed.as_secs_f64(),
    );

    let expected_total = WORKERS * QUOTA_PER_WORKER;
    assert_eq!(
        total_claimed, expected_total,
        "分配数 {total_claimed} != 预期 {expected_total}"
    );

    // ===== 阶段 3: 验证唯一性 =====
    println!("=== 阶段 3: 验证 ===");
    let client = pool.client();

    // 3a: 状态分布
    let stat: Vec<serde_json::Value> = client
        .query("SELECT status, count() FROM alloc_pool GROUP BY status")
        .all()
        .await
        .expect("分组统计");
    println!("状态分布: {stat:#?}");

    // 3b: 已分配 idx 去重数 == 预期 (真正验证唯一性, 不是恒真断言)
    //
    // 步进索引保证零冲突, 但若有人误改 strided_indices 为冲突策略, 此断言会失败.
    // 用 `SELECT count() FROM (SELECT DISTINCT idx ...)` 一次 RPC 完成.
    let distinct_count: CountResult = client
        .query("SELECT count() FROM (SELECT idx FROM alloc_pool WHERE status != 'free' GROUP BY idx) GROUP ALL")
        .one()
        .await
        .expect("count distinct")
        .expect("count distinct result");
    assert_eq!(
        distinct_count.count as usize, expected_total,
        "已分配 idx 去重数 {} != 预期 {expected_total} (存在重复分配!)",
        distinct_count.count
    );
    println!(
        "验证: 已分配去重 idx {} == 预期 {expected_total} ✓",
        distinct_count.count
    );

    // 3c: 抽样 round-trip
    let sample: Vec<PoolRow> = client
        .query("SELECT id, idx, status, worker FROM alloc_pool WHERE status = 'claimed_A' LIMIT 5")
        .all()
        .await
        .expect("抽样 A");
    for row in &sample {
        assert_eq!(row.status, "claimed_A");
        assert!(row.worker.is_some(), "策略 A 的 worker 字段缺失: {row:#?}");
    }
    println!("✅ 抽样 {} 条 round-trip", sample.len());
    println!("✅ 全部通过 — 100 万预种 → 256 路 × 500 条分配 → 无重复");
}