rmycli 0.1.0

A Redis shell with live command help and type-aware completion
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
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::mpsc::{self, Receiver, Sender, SyncSender};
use std::thread;

use redis::Value;

use crate::estimate;
use crate::metadata::Catalog;
use crate::scan::{self, Discovery, Limits};
use crate::session::{Config, Session, command};

pub enum Execution {
    Run(Vec<Vec<u8>>),
    Scan {
        pattern: Vec<u8>,
        cancelled: Arc<AtomicBool>,
    },
    Reconnect,
}

pub enum Assistance {
    Scan {
        generation: u64,
        config: Config,
        prefix: Vec<u8>,
        kind: Option<&'static str>,
        state: Discovery,
        limits: Limits,
        probe: Option<(Vec<Vec<u8>>, usize)>,
    },
    Estimate {
        generation: u64,
        config: Config,
        args: Vec<Vec<u8>>,
    },
    Cancel {
        config: Config,
        client_id: i64,
    },
}

pub enum Event {
    Executed {
        lines: Vec<String>,
        config: Config,
        allowed: bool,
        streaming: bool,
        client_id: Option<i64>,
        connected: bool,
    },
    Connected {
        catalog: Box<Catalog>,
        config: Config,
        client_id: Option<i64>,
    },
    Output(Vec<String>),
    Push(Vec<String>),
    Scanned {
        generation: u64,
        state: Discovery,
    },
    Estimated {
        generation: u64,
        count: Option<u64>,
    },
    Failed(String),
    Notice(String),
}

pub struct Workers {
    pub execution: tokio::sync::mpsc::UnboundedSender<Execution>,
    pub assistance: Sender<Assistance>,
    pub events: Receiver<Event>,
    pub generation: Arc<AtomicU64>,
}

async fn client_id(session: &mut Session) -> Option<i64> {
    let result = tokio::time::timeout(
        session.config.timeout,
        session.connection.query(redis::cmd("CLIENT").arg("ID")),
    )
    .await;
    match result {
        Ok(Ok(value)) => redis::from_redis_value(value).ok(),
        Ok(Err(_)) => None,
        Err(_) => {
            session.connection.close();
            None
        }
    }
}

impl Workers {
    pub fn start(session: Session) -> Self {
        let (event_tx, events) = mpsc::sync_channel(64);
        let (execution, execution_rx) = tokio::sync::mpsc::unbounded_channel();
        let (assistance, assistance_rx) = mpsc::channel();
        let generation = Arc::new(AtomicU64::new(0));
        let assist_generation = Arc::clone(&generation);
        let assist_tx = event_tx.clone();
        thread::spawn(move || {
            assist_loop(&assistance_rx, &assist_tx, &assist_generation);
        });
        let runtime = tokio::runtime::Handle::current();
        thread::spawn(move || {
            runtime.block_on(execution_loop(session, execution_rx, &event_tx));
        });
        Self {
            execution,
            assistance,
            events,
            generation,
        }
    }

    pub fn invalidate(&self) -> u64 {
        self.generation.fetch_add(1, Ordering::Relaxed) + 1
    }

    pub fn current(&self) -> u64 {
        self.generation.load(Ordering::Relaxed)
    }
}

fn assist_loop(
    receiver: &Receiver<Assistance>,
    events: &SyncSender<Event>,
    generation: &AtomicU64,
) {
    while let Ok(request) = receiver.recv() {
        match request {
            Assistance::Scan {
                generation: expected,
                config,
                prefix,
                kind,
                state,
                limits,
                probe,
            } => {
                let cancelled =
                    || generation.load(Ordering::Relaxed) != expected;
                if cancelled() {
                    continue;
                }
                let state = match config.connect_auxiliary() {
                    Ok(mut connection) => {
                        if probe.as_ref().is_some_and(|(args, index)| {
                            crate::metadata::probe_key_position(
                                &mut connection,
                                args,
                                *index,
                            ) != Some(true)
                        }) {
                            Discovery { complete: true, notice: "No key position confirmed; complete the arguments and try again".into(), ..state }
                        } else {
                            scan::discover(
                                &mut connection,
                                &prefix,
                                kind,
                                state,
                                &limits,
                                cancelled,
                            )
                        }
                    }
                    Err(error) => Discovery {
                        notice: error.to_string(),
                        ..state
                    },
                };
                let _ = events.send(Event::Scanned {
                    generation: expected,
                    state,
                });
            }
            Assistance::Estimate {
                generation: expected,
                config,
                args,
            } => {
                if generation.load(Ordering::Relaxed) != expected {
                    continue;
                }
                let count = config.connect_auxiliary().ok().and_then(
                    |mut connection| estimate::estimate(&mut connection, &args),
                );
                let _ = events.send(Event::Estimated {
                    generation: expected,
                    count,
                });
            }
            Assistance::Cancel { config, client_id } => {
                let result =
                    config.connect_auxiliary().and_then(|mut connection| {
                        command(&[
                            b"CLIENT".to_vec(),
                            b"KILL".to_vec(),
                            b"ID".to_vec(),
                            client_id.to_string().into_bytes(),
                        ])
                        .query::<i64>(&mut connection)
                    });
                if let Err(error) = result {
                    let _ = events.send(Event::Notice(format!(
                        "Cannot cancel connection: {error}. Ctrl-D exits."
                    )));
                }
            }
        }
    }
}

async fn announce(
    session: &mut Session,
    events: &SyncSender<Event>,
) -> Option<i64> {
    let id = client_id(session).await;
    let catalog = session.config.connect_auxiliary().map_or_else(
        |error| {
            let mut catalog = Catalog::bundled();
            catalog
                .notices
                .push(format!("Metadata connection failed: {error}"));
            catalog
        },
        |mut connection| Catalog::load(&mut connection),
    );
    let _ = events.send(Event::Connected {
        catalog: Box::new(catalog),
        config: session.config.clone(),
        client_id: id,
    });
    id
}

fn received_message(
    session: &mut Session,
    events: &SyncSender<Event>,
    reply: redis::RedisResult<Value>,
) -> bool {
    match reply {
        Ok(reply) => {
            let push = matches!(reply, Value::Push { .. });
            if !push || session.streaming || session.config.show_pushes {
                let lines = session.config.output.lines(&reply);
                let event = if push {
                    Event::Push(lines)
                } else {
                    Event::Output(lines)
                };
                return events.send(event).is_ok();
            }
        }
        Err(error) => {
            let state = if session.streaming {
                "Stream ended"
            } else {
                "Connection lost"
            };
            session.streaming = false;
            let _ = events.send(Event::Failed(format!(
                "{state}: {error}. Use \\reconnect."
            )));
            return false;
        }
    }
    true
}

async fn execution_loop(
    mut session: Session,
    mut requests: tokio::sync::mpsc::UnboundedReceiver<Execution>,
    events: &SyncSender<Event>,
) {
    let mut id = announce(&mut session, events).await;
    let mut listening = true;
    loop {
        let request = tokio::select! {
            request = requests.recv() => match request {
                Some(request) => request,
                None => return,
            },
            reply = session.connection.recv_response(), if listening => {
                listening = received_message(&mut session, events, reply);
                continue;
            }
        };
        match request {
            request @ (Execution::Run(_) | Execution::Scan { .. }) => {
                let previous_addr = session.config.info.addr().clone();
                let output = session.config.output.clone();
                let show_pushes = session.config.show_pushes;
                let push = |reply: &Value| {
                    if show_pushes {
                        let _ = events.send(Event::Push(output.lines(reply)));
                    }
                };
                let result = match request {
                    Execution::Run(args) => session
                        .execute_with_push(&args, push)
                        .await
                        .map(|reply| output.lines(&reply)),
                    Execution::Scan { pattern, cancelled } => scan_keys(
                        &mut session,
                        &pattern,
                        &cancelled,
                        events,
                        push,
                    )
                    .await
                    .map(|()| Vec::new()),
                    Execution::Reconnect => unreachable!(),
                };
                if previous_addr != *session.config.info.addr() {
                    id = client_id(&mut session).await;
                }
                let connected = result
                    .as_ref()
                    .err()
                    .is_none_or(|error| !error.is_io_error());
                listening = connected;
                let lines = result.unwrap_or_else(|error| {
                    vec![format!(
                        "(error) {}",
                        crate::output::error_message(&error)
                    )]
                });
                if events
                    .send(Event::Executed {
                        lines,
                        config: session.config.clone(),
                        allowed: session.assistance_allowed() && connected,
                        streaming: session.streaming,
                        client_id: id,
                        connected,
                    })
                    .is_err()
                {
                    return;
                }
            }
            Execution::Reconnect => {
                match Session::open(session.config.clone()).await {
                    Ok(fresh) => {
                        session = fresh;
                        listening = true;
                        id = announce(&mut session, events).await;
                    }
                    Err(error) => {
                        let _ = events.send(Event::Failed(format!(
                            "Reconnect failed: {error}"
                        )));
                    }
                }
            }
        }
    }
}

async fn scan_keys(
    session: &mut Session,
    pattern: &[u8],
    cancelled: &AtomicBool,
    events: &SyncSender<Event>,
    mut push: impl FnMut(&Value),
) -> redis::RedisResult<()> {
    if !session.assistance_allowed() {
        return Err(redis::RedisError::from((
            redis::ErrorKind::Client,
            "Alternate KEYS is unavailable during MULTI, WATCH, or streaming",
        )));
    }
    let mut cursor = 0_u64;
    while !cancelled.load(Ordering::Relaxed) {
        let args = [
            b"SCAN".to_vec(),
            cursor.to_string().into_bytes(),
            b"MATCH".to_vec(),
            pattern.to_vec(),
            b"COUNT".to_vec(),
            b"128".to_vec(),
        ];
        let reply = session.execute_with_push(&args, &mut push).await?;
        let (next, keys): (u64, Vec<Vec<u8>>) = redis::from_redis_value(reply)?;
        if cancelled.load(Ordering::Relaxed) {
            break;
        }
        let lines = keys
            .into_iter()
            .flat_map(|key| {
                session.config.output.lines(&Value::BulkString(key))
            })
            .collect();
        if events.send(Event::Output(lines)).is_err() {
            break;
        }
        cursor = next;
        if cursor == 0 {
            return Ok(());
        }
    }
    Ok(())
}