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>,
}
async fn seed_pool(client: &SurrealClient, total: usize) -> Result<(), String> {
let batch = 50_000;
let batches = total.div_ceil(batch);
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; 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(())
}
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
}
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 {
if client
.query(&sql)
.bind("s", suffix)
.bind("w", worker_id)
.bind("i", idx)
.exec()
.await
.is_ok()
{
claimed += 1;
}
}
claimed
}
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,
}
}
#[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;
let config = build_config("surmux_alloc_test", "alloc_1m_pool", 600);
let pool = SurrealPool::connect(&config, POOL_SIZE)
.await
.expect("pool connect");
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, "种子数不匹配");
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;
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}"
);
println!("=== 阶段 3: 验证 ===");
let client = pool.client();
let stat: Vec<serde_json::Value> = client
.query("SELECT status, count() FROM alloc_pool GROUP BY status")
.all()
.await
.expect("分组统计");
println!("状态分布: {stat:#?}");
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
);
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 条分配 → 无重复");
}