Skip to main content

kevy_cli/
bulk.rs

1//! Prefix bulk ops + diagnostics:
2//! `copy-prefix` / `delete-prefix` (token-bucket rate limit,
3//! `--dry-run`), `digest`, `diff`, `inspect`.
4
5use std::io::{self, Write};
6use std::time::{Duration, Instant};
7
8use kevy_resp::Reply;
9use kevy_resp_client::RespClient;
10
11/// Token bucket: `rate` ops/second, starting EMPTY (strict pacing —
12/// a full-bucket start lets a small job burn its whole burst
13/// unthrottled, defeating the point of `--rate` for short sweeps).
14/// `rate == 0` = unlimited.
15#[derive(Debug)]
16pub struct RateLimiter {
17    rate: u64,
18    tokens: f64,
19    last: Instant,
20}
21
22impl RateLimiter {
23    /// New limiter at `rate` ops/s (0 = off).
24    pub fn new(rate: u64) -> Self {
25        Self { rate, tokens: 0.0, last: Instant::now() }
26    }
27
28    /// Block until one op is admitted.
29    pub fn take(&mut self) {
30        if self.rate == 0 {
31            return;
32        }
33        loop {
34            let now = Instant::now();
35            self.tokens = (self.tokens
36                + now.duration_since(self.last).as_secs_f64() * self.rate as f64)
37                .min(self.rate as f64);
38            self.last = now;
39            if self.tokens >= 1.0 {
40                self.tokens -= 1.0;
41                return;
42            }
43            std::thread::sleep(Duration::from_millis(2));
44        }
45    }
46}
47
48fn scan_page(
49    client: &mut RespClient,
50    cursor: &[u8],
51    pattern: &[u8],
52) -> io::Result<(Vec<u8>, Vec<Vec<u8>>)> {
53    let reply = client.request_borrowed(&[b"SCAN", cursor, b"MATCH", pattern, b"COUNT", b"512"])?;
54    let Reply::Array(items) = reply else {
55        return Err(io::Error::new(io::ErrorKind::InvalidData, "SCAN reply shape"));
56    };
57    let (Some(Reply::Bulk(next)), Some(Reply::Array(keys))) = (items.first(), items.get(1)) else {
58        return Err(io::Error::new(io::ErrorKind::InvalidData, "SCAN reply shape"));
59    };
60    let keys = keys
61        .iter()
62        .filter_map(|k| if let Reply::Bulk(b) = k { Some(b.clone()) } else { None })
63        .collect();
64    Ok((next.clone(), keys))
65}
66
67/// `delete-prefix`: SCAN + UNLINK, rate-limited. Returns deleted count.
68pub fn run_delete_prefix(
69    client: &mut RespClient,
70    prefix: &[u8],
71    rate: u64,
72    dry_run: bool,
73) -> io::Result<u64> {
74    let mut pattern = prefix.to_vec();
75    pattern.push(b'*');
76    let mut cursor: Vec<u8> = b"0".to_vec();
77    let mut limiter = RateLimiter::new(rate);
78    let mut n = 0u64;
79    loop {
80        let (next, keys) = scan_page(client, &cursor, &pattern)?;
81        for key in &keys {
82            if dry_run {
83                n += 1;
84                continue;
85            }
86            limiter.take();
87            if let Reply::Int(d) = client.request_borrowed(&[b"UNLINK", key])? {
88                n += d as u64;
89            }
90        }
91        cursor = next;
92        if cursor == b"0" {
93            return Ok(n);
94        }
95    }
96}
97
98/// `copy-prefix`: SCAN src prefix, re-key under dst prefix via
99/// read+rebuild frames (the server has no COPY verb; TTL carried as
100/// absolute PEXPIREAT). Rate-limited per source key.
101///
102/// Returns what it copied **and what it could not** — same contract as
103/// `export`, for the same reason: the rebuild set does not cover every
104/// type, and a copy that quietly drops one is worse than a refusal.
105pub fn run_copy_prefix(
106    client: &mut RespClient,
107    src_prefix: &[u8],
108    dst_prefix: &[u8],
109    rate: u64,
110) -> io::Result<crate::migrate::Export> {
111    let mut pattern = src_prefix.to_vec();
112    pattern.push(b'*');
113    let mut cursor: Vec<u8> = b"0".to_vec();
114    let mut limiter = RateLimiter::new(rate);
115    let mut n = 0u64;
116    // Same skip as `export`, and it must be as loud: a copy that leaves
117    // a type behind and says "copied N keys" is the same silence.
118    let mut skipped: std::collections::BTreeMap<Vec<u8>, u64> = Default::default();
119    loop {
120        let (next, keys) = scan_page(client, &cursor, &pattern)?;
121        for key in &keys {
122            limiter.take();
123            let mut dst = dst_prefix.to_vec();
124            dst.extend_from_slice(&key[src_prefix.len()..]);
125            let frames = match crate::migrate::rebuild_frames(client, key, &dst)? {
126                crate::migrate::Rebuilt::Frames(f) => f,
127                crate::migrate::Rebuilt::Vanished => continue,
128                crate::migrate::Rebuilt::UnsupportedType(ty) => {
129                    *skipped.entry(ty).or_insert(0u64) += 1;
130                    continue;
131                }
132            };
133            let n_cmds = count_commands(&frames);
134            for r in client.pipeline_raw(&frames, n_cmds)? {
135                if let Reply::Error(e) = r {
136                    return Err(io::Error::new(
137                        io::ErrorKind::InvalidData,
138                        String::from_utf8_lossy(&e).into_owned(),
139                    ));
140                }
141            }
142            n += 1;
143        }
144        cursor = next;
145        if cursor == b"0" {
146            return Ok(crate::migrate::Export { keys: n, skipped });
147        }
148    }
149}
150
151/// Number of RESP commands in a frame buffer (top-level '*' headers).
152fn count_commands(mut b: &[u8]) -> usize {
153    let mut n = 0;
154    while let Some(len) = crate::migrate::command_len_pub(b) {
155        n += 1;
156        b = &b[len..];
157    }
158    n
159}
160
161/// `digest <prefix>` → (count, hex).
162pub fn run_digest(client: &mut RespClient, prefix: &[u8]) -> io::Result<(i64, String)> {
163    let r = client.request_borrowed(&[b"PREFIX.DIGEST", prefix])?;
164    let Reply::Array(items) = r else {
165        return Err(io::Error::new(io::ErrorKind::InvalidData, "PREFIX.DIGEST reply"));
166    };
167    let (Some(Reply::Int(n)), Some(Reply::Bulk(hex))) = (items.first(), items.get(1)) else {
168        return Err(io::Error::new(io::ErrorKind::InvalidData, "PREFIX.DIGEST reply"));
169    };
170    Ok((*n, String::from_utf8_lossy(hex).into_owned()))
171}
172
173/// `diff`: compare prefixes across two servers. Returns mismatching
174/// prefixes.
175pub fn run_diff(
176    a: &mut RespClient,
177    b: &mut RespClient,
178    prefixes: &[Vec<u8>],
179    out: &mut impl Write,
180) -> io::Result<Vec<Vec<u8>>> {
181    let mut bad = Vec::new();
182    for p in prefixes {
183        let (na, da) = run_digest(a, p)?;
184        let (nb, db) = run_digest(b, p)?;
185        let ok = na == nb && da == db;
186        writeln!(
187            out,
188            "{}  A: {na} keys {da}  B: {nb} keys {db}  {}",
189            String::from_utf8_lossy(p),
190            if ok { "OK" } else { "MISMATCH" }
191        )?;
192        if !ok {
193            bad.push(p.clone());
194        }
195    }
196    Ok(bad)
197}
198
199/// `inspect <prefix>`: sample keys, type distribution, sizes.
200pub fn run_inspect(client: &mut RespClient, prefix: &[u8], out: &mut impl Write) -> io::Result<()> {
201    let mut pattern = prefix.to_vec();
202    pattern.push(b'*');
203    let mut cursor: Vec<u8> = b"0".to_vec();
204    let mut total = 0u64;
205    let mut by_type: Vec<(String, u64)> = Vec::new();
206    let mut samples: Vec<String> = Vec::new();
207    loop {
208        let (next, keys) = scan_page(client, &cursor, &pattern)?;
209        for key in &keys {
210            total += 1;
211            if samples.len() < 8 {
212                samples.push(String::from_utf8_lossy(key).into_owned());
213            }
214            if let Reply::Simple(t) = client.request_borrowed(&[b"TYPE", key])? {
215                let t = String::from_utf8_lossy(&t).into_owned();
216                match by_type.iter_mut().find(|(n, _)| *n == t) {
217                    Some((_, c)) => *c += 1,
218                    None => by_type.push((t, 1)),
219                }
220            }
221        }
222        cursor = next;
223        if cursor == b"0" {
224            break;
225        }
226    }
227    writeln!(out, "prefix {}: {total} keys", String::from_utf8_lossy(prefix))?;
228    by_type.sort_by_key(|(_, c)| std::cmp::Reverse(*c));
229    for (t, c) in &by_type {
230        writeln!(out, "  {t}: {c}")?;
231    }
232    if !samples.is_empty() {
233        writeln!(out, "  samples: {}", samples.join(", "))?;
234    }
235    Ok(())
236}