fred 10.1.0

An async client for Redis and Valkey.
Documentation
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
521
522
523
524
pub mod centralized;
pub mod clustered;
pub mod commands;
pub mod connections;
#[cfg(feature = "replicas")]
pub mod replicas;
pub mod responses;
pub mod sentinel;
pub mod types;
pub mod utils;

use crate::{
  error::Error,
  modules::inner::ClientInner,
  protocol::{
    command::Command,
    connection::{Connection, Counters},
    types::Server,
  },
  router::{
    connections::Connections,
    types::{ReadAllFuture, ReadFuture},
  },
  runtime::RefCount,
  types::Resp3Frame,
  utils as client_utils,
};
use futures::future::join_all;
#[cfg(feature = "replicas")]
use futures::future::try_join;
use std::{
  collections::{HashSet, VecDeque},
  future::pending,
  hash::{Hash, Hasher},
};
#[cfg(feature = "transactions")]
pub mod transactions;
#[cfg(feature = "replicas")]
use replicas::Replicas;

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ReconnectServer {
  All,
  One(Server),
}

impl Hash for ReconnectServer {
  fn hash<H: Hasher>(&self, state: &mut H) {
    match self {
      ReconnectServer::All => "all".hash(state),
      ReconnectServer::One(server) => server.hash(state),
    }
  }
}

/// A struct for routing commands to the server(s).
pub struct Router {
  /// The connection map for each deployment type.
  pub connections:          Connections,
  /// Storage for commands that should be deferred or retried later.
  pub retry_buffer:         VecDeque<Command>,
  /// A set to dedup pending reconnection commands.
  pub pending_reconnection: HashSet<ReconnectServer>,
  /// The replica routing interface.
  #[cfg(feature = "replicas")]
  pub replicas:             Replicas,
}

impl Router {
  /// Create a new `Router` without connecting to the server(s).
  pub fn new(inner: &RefCount<ClientInner>) -> Self {
    let connections = if inner.config.server.is_clustered() {
      Connections::new_clustered()
    } else if inner.config.server.is_sentinel() {
      Connections::new_sentinel()
    } else {
      Connections::new_centralized()
    };

    Router {
      retry_buffer: VecDeque::new(),
      pending_reconnection: HashSet::new(),
      connections,
      #[cfg(feature = "replicas")]
      replicas: Replicas::new(),
    }
  }

  /// Find the primary node that owns the hash slot used by the command.
  #[cfg(feature = "replicas")]
  pub fn cluster_owner(&self, command: &Command) -> Option<&Server> {
    match self.connections {
      Connections::Clustered { ref cache, .. } => command.cluster_hash().and_then(|slot| cache.get_server(slot)),
      _ => None,
    }
  }

  /// Whether a deferred reconnection command exists for the provided server.
  pub fn has_pending_reconnection(&self, server: &Option<&Server>) -> bool {
    match server {
      Some(server) => {
        self.pending_reconnection.contains(&ReconnectServer::All)
          || self
            .pending_reconnection
            .contains(&ReconnectServer::One((*server).clone()))
      },
      None => self.pending_reconnection.contains(&ReconnectServer::All),
    }
  }

  pub fn reset_pending_reconnection(&mut self, server: Option<&Server>) {
    if let Some(server) = server {
      self.pending_reconnection.remove(&ReconnectServer::One(server.clone()));
    } else {
      self.pending_reconnection.clear();
    }
  }

  /// Find the connection that should receive the provided command.
  #[cfg(feature = "replicas")]
  pub fn route(&mut self, command: &Command) -> Option<&mut Connection> {
    if command.is_all_cluster_nodes() {
      return None;
    }

    match command.cluster_node.as_ref() {
      Some(server) => {
        if command.use_replica {
          self
            .replicas
            .routing
            .next_replica(server)
            .and_then(|replica| self.replicas.connections.get_mut(replica))
        } else {
          self.connections.get_connection_mut(server)
        }
      },
      None => {
        if command.use_replica {
          match self.cluster_owner(command).cloned() {
            Some(primary) => match self.replicas.routing.next_replica(&primary) {
              Some(replica) => self.replicas.connections.get_mut(replica),
              None => None,
            },
            None => None,
          }
        } else {
          match self.connections {
            Connections::Centralized {
              connection: ref mut writer,
            } => writer.as_mut(),
            Connections::Sentinel {
              connection: ref mut writer,
            } => writer.as_mut(),
            Connections::Clustered {
              connections: ref mut writers,
              ref cache,
            } => {
              let server = command.cluster_hash().and_then(|slot| cache.get_server(slot));
              let has_server = server.map(|server| writers.contains_key(server)).unwrap_or(false);

              if has_server {
                server.and_then(|server| writers.get_mut(server))
              } else {
                writers.values_mut().next()
              }
            },
          }
        }
      },
    }
  }

  /// Find the connection that should receive the provided command.
  #[cfg(not(feature = "replicas"))]
  pub fn route<'a>(&'a mut self, command: &Command) -> Option<&'a mut Connection> {
    if command.is_all_cluster_nodes() {
      return None;
    }

    match command.cluster_node.as_ref() {
      Some(server) => self.connections.get_connection_mut(server),
      None => match self.connections {
        Connections::Centralized {
          connection: ref mut writer,
          ..
        } => writer.as_mut(),
        Connections::Sentinel {
          connection: ref mut writer,
          ..
        } => writer.as_mut(),
        Connections::Clustered {
          connections: ref mut writers,
          ref cache,
        } => {
          let server = command.cluster_hash().and_then(|slot| cache.get_server(slot));
          let has_server = server.map(|server| writers.contains_key(server)).unwrap_or(false);

          if has_server {
            server.and_then(|server| writers.get_mut(server))
          } else {
            writers.values_mut().next()
          }
        },
      },
    }
  }

  #[cfg(feature = "replicas")]
  pub fn get_connection_mut(&mut self, server: &Server) -> Option<&mut Connection> {
    self
      .connections
      .get_connection_mut(server)
      .or_else(|| self.replicas.connections.get_mut(server))
  }

  #[cfg(not(feature = "replicas"))]
  pub fn get_connection_mut(&mut self, server: &Server) -> Option<&mut Connection> {
    self.connections.get_connection_mut(server)
  }

  #[cfg(feature = "replicas")]
  pub fn take_connection(&mut self, server: &Server) -> Option<Connection> {
    self
      .connections
      .take_connection(Some(server))
      .or_else(|| self.replicas.connections.remove(server))
  }

  #[cfg(not(feature = "replicas"))]
  pub fn take_connection(&mut self, server: &Server) -> Option<Connection> {
    self.connections.take_connection(Some(server))
  }

  /// Disconnect from all the servers, moving the in-flight messages to the internal command buffer and triggering a
  /// reconnection, if necessary.
  pub async fn disconnect_all(&mut self, inner: &RefCount<ClientInner>) {
    let commands = self.connections.disconnect_all(inner).await;
    self.retry_commands(commands);
    self.disconnect_replicas(inner).await;
  }

  /// Disconnect from all the servers, moving the in-flight messages to the internal command buffer and triggering a
  /// reconnection, if necessary.
  #[cfg(feature = "replicas")]
  pub async fn disconnect_replicas(&mut self, inner: &RefCount<ClientInner>) {
    if let Err(e) = self.replicas.clear_connections(inner).await {
      _warn!(inner, "Error disconnecting replicas: {:?}", e);
    }
  }

  #[cfg(not(feature = "replicas"))]
  pub async fn disconnect_replicas(&mut self, _: &RefCount<ClientInner>) {}

  /// Add the provided commands to the retry buffer.
  pub fn retry_commands(&mut self, commands: impl IntoIterator<Item = Command>) {
    for command in commands.into_iter() {
      self.retry_command(command);
    }
  }

  /// Add the provided command to the retry buffer.
  pub fn retry_command(&mut self, command: Command) {
    self.retry_buffer.push_back(command);
  }

  /// Clear all the commands in the retry buffer.
  pub fn clear_retry_buffer(&mut self) {
    self.retry_buffer.clear();
  }

  /// Connect to the server(s), discarding any previous connection state.
  pub async fn connect(&mut self, inner: &RefCount<ClientInner>) -> Result<(), Error> {
    let result = self.connections.initialize(inner, &mut self.retry_buffer).await;

    if result.is_ok() {
      #[cfg(feature = "replicas")]
      self.refresh_replica_routing(inner).await?;

      Ok(())
    } else {
      result
    }
  }

  /// Gracefully reset the replica routing table.
  #[cfg(feature = "replicas")]
  pub async fn refresh_replica_routing(&mut self, inner: &RefCount<ClientInner>) -> Result<(), Error> {
    self.replicas.clear_routing();
    if let Err(e) = self.sync_replicas(inner).await {
      if !inner.ignore_replica_reconnect_errors() {
        return Err(e);
      }
    }

    Ok(())
  }

  /// Sync the cached cluster state with the server via `CLUSTER SLOTS`.
  ///
  /// This will also create new connections or drop old connections as needed.
  pub async fn sync_cluster(&mut self, inner: &RefCount<ClientInner>) -> Result<(), Error> {
    let result = match self.connections {
      Connections::Clustered {
        connections: ref mut writers,
        ref mut cache,
      } => {
        let result = clustered::sync(inner, writers, cache, &mut self.retry_buffer).await;

        if result.is_ok() {
          #[cfg(feature = "replicas")]
          self.refresh_replica_routing(inner).await?;

          // surface errors from the retry process, otherwise return the reconnection result
          Box::pin(self.retry_buffer(inner)).await?;
        }
        result
      },
      _ => Ok(()),
    };

    inner.backchannel.update_connection_ids(&self.connections);
    result
  }

  /// Rebuild the cached replica routing table based on the primary node connections.
  #[cfg(feature = "replicas")]
  pub async fn sync_replicas(&mut self, inner: &RefCount<ClientInner>) -> Result<(), Error> {
    _debug!(inner, "Syncing replicas...");
    self.replicas.drop_broken_connections().await;
    let old_connections = self.replicas.active_connections().await;
    let new_replica_map = self.connections.replica_map(inner).await?;

    let old_connections_idx: HashSet<_> = old_connections.iter().collect();
    let new_connections_idx: HashSet<_> = new_replica_map.keys().collect();
    let remove: Vec<_> = old_connections_idx.difference(&new_connections_idx).collect();

    for server in remove.into_iter() {
      _debug!(inner, "Dropping replica connection to {}", server);
      self.replicas.drop_writer(inner, server).await;
      self.replicas.remove_replica(server);
    }

    for (mut replica, primary) in new_replica_map.into_iter() {
      let should_use = if let Some(filter) = inner.connection.replica.filter.as_ref() {
        filter.filter(&primary, &replica).await
      } else {
        true
      };

      if should_use {
        replicas::map_replica_tls_names(inner, &primary, &mut replica);

        self.replicas.add_connection(inner, primary, replica, false).await?;
      }
    }

    inner
      .server_state
      .write()
      .update_replicas(self.replicas.routing_table());
    Ok(())
  }

  /// Attempt to replay all queued commands on the internal buffer without backpressure.
  pub async fn retry_buffer(&mut self, inner: &RefCount<ClientInner>) -> Result<(), Error> {
    #[cfg(feature = "replicas")]
    {
      let commands = self.replicas.take_retry_buffer();
      self.retry_buffer.extend(commands);
    }

    while let Some(command) = self.retry_buffer.pop_front() {
      if client_utils::read_bool_atomic(&command.timed_out) {
        _debug!(
          inner,
          "Ignore retrying timed out command: {}",
          command.kind.to_str_debug()
        );
        continue;
      }

      _trace!(
        inner,
        "Retry `{}` ({}) command, attempts remaining: {}",
        command.kind.to_str_debug(),
        command.debug_id(),
        command.attempts_remaining,
      );
      if let Err(err) = Box::pin(commands::write_command(inner, self, command)).await {
        _debug!(inner, "Error retrying command: {:?}", err);
        break;
      }
    }

    let _ = self.flush().await;
    Ok(())
  }

  /// Wait and read frames until there are no in-flight frames on primary connections.
  pub async fn drain_all(&mut self, inner: &RefCount<ClientInner>) -> Result<(), Error> {
    let inner = inner.clone();
    _trace!(inner, "Draining all connections...");
    self.flush().await?;

    let primary_ft = async {
      match self.connections {
        Connections::Clustered {
          connections: ref mut writers,
          ..
        } => {
          // drain all connections even if one of them breaks out early with an error
          let _ = join_all(writers.iter_mut().map(|(_, conn)| conn.drain(&inner)))
            .await
            .into_iter()
            .collect::<Result<Vec<()>, Error>>()?;

          Ok(())
        },
        Connections::Centralized {
          connection: ref mut writer,
        }
        | Connections::Sentinel {
          connection: ref mut writer,
        } => match writer {
          Some(ref mut conn) => conn.drain(&inner).await,
          None => Ok(()),
        },
      }
    };

    #[cfg(feature = "replicas")]
    return try_join(primary_ft, self.replicas.drain(&inner)).await.map(|_| ());
    #[cfg(not(feature = "replicas"))]
    primary_ft.await
  }

  pub async fn flush(&mut self) -> Result<(), Error> {
    self.connections.flush().await?;
    #[cfg(feature = "replicas")]
    self.replicas.flush().await?;
    Ok(())
  }

  pub async fn has_healthy_centralized_connection(&mut self) -> bool {
    match self.connections {
      Connections::Centralized {
        connection: ref mut writer,
      }
      | Connections::Sentinel {
        connection: ref mut writer,
      } => {
        if let Some(writer) = writer {
          writer.peek_reader_errors().await.is_none()
        } else {
          false
        }
      },
      _ => false,
    }
  }

  /// Try to read from all sockets concurrently in a select loop.
  #[cfg(feature = "replicas")]
  pub async fn select_read(
    &mut self,
    inner: &RefCount<ClientInner>,
  ) -> Vec<(Server, Option<Result<Resp3Frame, Error>>)> {
    match self.connections {
      Connections::Centralized {
        connection: ref mut writer,
      }
      | Connections::Sentinel {
        connection: ref mut writer,
      } => {
        if let Some(writer) = writer {
          ReadFuture::new(inner, writer, &mut self.replicas.connections).await
        } else {
          pending().await
        }
      },
      Connections::Clustered {
        connections: ref mut writers,
        ..
      } => ReadAllFuture::new(inner, writers, &mut self.replicas.connections).await,
    }
  }

  /// Try to read from all sockets concurrently in a select loop.
  #[cfg(not(feature = "replicas"))]
  pub async fn select_read(
    &mut self,
    inner: &RefCount<ClientInner>,
  ) -> Vec<(Server, Option<Result<Resp3Frame, Error>>)> {
    match self.connections {
      Connections::Centralized {
        connection: ref mut writer,
      }
      | Connections::Sentinel {
        connection: ref mut writer,
      } => {
        if let Some(writer) = writer {
          ReadFuture::new(inner, writer).await
        } else {
          pending().await
        }
      },
      Connections::Clustered {
        connections: ref mut writers,
        ..
      } => ReadAllFuture::new(inner, writers).await,
    }
  }

  #[cfg(feature = "replicas")]
  pub fn is_replica(&self, server: &Server) -> bool {
    self.replicas.connections.contains_key(server)
  }

  #[cfg(not(feature = "replicas"))]
  pub fn is_replica(&self, _: &Server) -> bool {
    false
  }
}