nautilus-infrastructure 0.52.0

Infrastructure components for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Provides a Redis backed `CacheDatabase` and `MessageBusDatabase` implementation.

pub mod cache;
pub mod msgbus;
pub mod queries;

use std::time::Duration;

use nautilus_common::{
    logging::log_task_awaiting,
    msgbus::database::{DatabaseConfig, MessageBusConfig},
};
use nautilus_core::UUID4;
use nautilus_model::identifiers::TraderId;
use redis::RedisError;
use semver::Version;

const REDIS_MIN_VERSION: &str = "6.2.0";
const REDIS_DELIMITER: char = ':';
const REDIS_INDEX_PATTERN: &str = ":index:";
const REDIS_XTRIM: &str = "XTRIM";
const REDIS_MINID: &str = "MINID";
const REDIS_FLUSHDB: &str = "FLUSHDB";

/// Extracts the index key from a full Redis key.
///
/// Handles keys with instance_id prefix by finding the `:index:` pattern.
/// e.g., "trader-id:uuid:index:order_position" -> "index:order_position"
pub(crate) fn get_index_key(key: &str) -> anyhow::Result<&str> {
    if let Some(pos) = key.find(REDIS_INDEX_PATTERN) {
        return Ok(&key[pos + 1..]);
    }

    if key.starts_with("index:") {
        return Ok(key);
    }

    anyhow::bail!("Invalid index key format: {key}")
}

async fn await_handle(handle: Option<tokio::task::JoinHandle<()>>, task_name: &str) {
    if let Some(handle) = handle {
        log_task_awaiting(task_name);

        let timeout = Duration::from_secs(2);
        match tokio::time::timeout(timeout, handle).await {
            Ok(result) => {
                if let Err(e) = result {
                    log::error!("Error awaiting task '{task_name}': {e:?}");
                }
            }
            Err(_) => {
                log::error!("Timeout {timeout:?} awaiting task '{task_name}'");
            }
        }
    }
}

/// Parses a Redis connection URL from the given database config, returning the
/// full URL and a redacted version with the password obfuscated.
///
/// Authentication matrix handled:
/// ┌───────────┬───────────┬────────────────────────────┐
/// │ Username  │ Password  │ Resulting user-info part   │
/// ├───────────┼───────────┼────────────────────────────┤
/// │ non-empty │ non-empty │ user:pass@                 │
/// │ empty     │ non-empty │ :pass@                     │
/// │ empty     │ empty     │ (omitted)                  │
/// └───────────┴───────────┴────────────────────────────┘
///
/// # Panics
///
/// Panics if a username is provided without a corresponding password.
#[must_use]
pub fn get_redis_url(config: DatabaseConfig) -> (String, String) {
    let host = config.host.unwrap_or("127.0.0.1".to_string());
    let port = config.port.unwrap_or(6379);
    let username = config.username.unwrap_or_default();
    let password = config.password.unwrap_or_default();
    let ssl = config.ssl;

    // Redact the password for logging/metrics: keep the first & last two chars.
    let redact_pw = |pw: &str| {
        if pw.len() > 4 {
            format!("{}...{}", &pw[..2], &pw[pw.len() - 2..])
        } else {
            pw.to_owned()
        }
    };

    // Build the `userinfo@` portion for both the real and redacted URLs.
    let (auth, auth_redacted) = match (username.is_empty(), password.is_empty()) {
        // user:pass@
        (false, false) => (
            format!("{username}:{password}@"),
            format!("{username}:{}@", redact_pw(&password)),
        ),
        // :pass@
        (true, false) => (
            format!(":{password}@"),
            format!(":{}@", redact_pw(&password)),
        ),
        // username but no password ⇒  configuration error
        (false, true) => panic!(
            "Redis config error: username supplied without password. \
            Either supply a password or omit the username."
        ),
        // no credentials
        (true, true) => (String::new(), String::new()),
    };

    let scheme = if ssl { "rediss" } else { "redis" };

    let url = format!("{scheme}://{auth}{host}:{port}");
    let redacted_url = format!("{scheme}://{auth_redacted}{host}:{port}");

    (url, redacted_url)
}

/// Creates a new Redis connection manager based on the provided database `config` and connection name.
///
/// # Errors
///
/// Returns an error if:
/// - Constructing the Redis client fails.
/// - Establishing or configuring the connection manager fails.
///
/// In case of reconnection issues, the connection will retry reconnection
/// `number_of_retries` times, with an exponentially increasing delay, calculated as
/// `factor * (exponent_base ^ current-try)`, bounded by `max_delay`.
///
/// The new connection will time out operations after `response_timeout` has passed.
/// Each connection attempt to the server will time out after `connection_timeout`.
pub async fn create_redis_connection(
    con_name: &str,
    config: DatabaseConfig,
) -> anyhow::Result<redis::aio::ConnectionManager> {
    tracing::debug!("Creating {con_name} redis connection");
    let (redis_url, redacted_url) = get_redis_url(config.clone());
    tracing::debug!("Connecting to {redacted_url}");

    let connection_timeout = Duration::from_secs(u64::from(config.connection_timeout));
    let response_timeout = Duration::from_secs(u64::from(config.response_timeout));
    let number_of_retries = config.number_of_retries;
    let exponent_base = config.exponent_base as f32;

    // Use factor as min_delay base for backoff: factor * (exponent_base ^ tries)
    let min_delay = Duration::from_millis(config.factor);
    let max_delay = Duration::from_secs(config.max_delay);

    let client = redis::Client::open(redis_url)?;

    let connection_manager_config = redis::aio::ConnectionManagerConfig::new()
        .set_exponent_base(exponent_base)
        .set_number_of_retries(number_of_retries)
        .set_response_timeout(Some(response_timeout))
        .set_connection_timeout(Some(connection_timeout))
        .set_min_delay(min_delay)
        .set_max_delay(max_delay);

    let mut con = client
        .get_connection_manager_with_config(connection_manager_config)
        .await?;

    let version = get_redis_version(&mut con).await?;
    let min_version = Version::parse(REDIS_MIN_VERSION)?;
    let con_msg = format!("Connected to redis v{version}");

    if version >= min_version {
        tracing::info!(con_msg);
    } else {
        // TODO: Using `log` error here so that the message is displayed regardless of whether
        // the logging config has pyo3 enabled. Later we can standardize this to `tracing`.
        log::error!("{con_msg}, but minimum supported version is {REDIS_MIN_VERSION}");
    }

    Ok(con)
}

/// Flushes the entire Redis database for the specified connection.
///
/// # Errors
///
/// Returns an error if the FLUSHDB command fails.
pub async fn flush_redis(
    con: &mut redis::aio::ConnectionManager,
) -> anyhow::Result<(), RedisError> {
    redis::cmd(REDIS_FLUSHDB).exec_async(con).await
}

/// Parse the stream key from the given identifiers and config.
#[must_use]
pub fn get_stream_key(
    trader_id: TraderId,
    instance_id: UUID4,
    config: &MessageBusConfig,
) -> String {
    let mut stream_key = String::new();

    if config.use_trader_prefix {
        stream_key.push_str("trader-");
    }

    if config.use_trader_id {
        stream_key.push_str(trader_id.as_str());
        stream_key.push(REDIS_DELIMITER);
    }

    if config.use_instance_id {
        stream_key.push_str(&format!("{instance_id}"));
        stream_key.push(REDIS_DELIMITER);
    }

    stream_key.push_str(&config.streams_prefix);
    stream_key
}

/// Retrieves and parses the Redis server version via the INFO command.
///
/// # Errors
///
/// Returns an error if the INFO command fails or version parsing fails.
pub async fn get_redis_version(
    conn: &mut redis::aio::ConnectionManager,
) -> anyhow::Result<Version> {
    let info: String = redis::cmd("INFO").query_async(conn).await?;
    let version_str = match info.lines().find_map(|line| {
        if line.starts_with("redis_version:") {
            line.split(':').nth(1).map(|s| s.trim().to_string())
        } else {
            None
        }
    }) {
        Some(info) => info,
        None => {
            anyhow::bail!("Redis version not available");
        }
    };

    parse_redis_version(&version_str)
}

fn parse_redis_version(version_str: &str) -> anyhow::Result<Version> {
    let mut components = version_str.split('.').map(str::parse::<u64>);

    let major = components.next().unwrap_or(Ok(0))?;
    let minor = components.next().unwrap_or(Ok(0))?;
    let patch = components.next().unwrap_or(Ok(0))?;

    Ok(Version::new(major, minor, patch))
}

#[cfg(test)]
mod tests {
    use rstest::rstest;
    use serde_json::json;

    use super::*;

    #[rstest]
    fn test_get_redis_url_default_values() {
        let config: DatabaseConfig = serde_json::from_value(json!({})).unwrap();
        let (url, redacted_url) = get_redis_url(config);
        assert_eq!(url, "redis://127.0.0.1:6379");
        assert_eq!(redacted_url, "redis://127.0.0.1:6379");
    }

    #[rstest]
    fn test_get_redis_url_password_only() {
        // Username omitted, but password present
        let config_json = json!({
            "host": "example.com",
            "port": 6380,
            "password": "secretpw",   // >4 chars ⇒ will be redacted
        });
        let config: DatabaseConfig = serde_json::from_value(config_json).unwrap();
        let (url, redacted_url) = get_redis_url(config);
        assert_eq!(url, "redis://:secretpw@example.com:6380");
        assert_eq!(redacted_url, "redis://:se...pw@example.com:6380");
    }

    #[rstest]
    fn test_get_redis_url_full_config_with_ssl() {
        let config_json = json!({
            "host": "example.com",
            "port": 6380,
            "username": "user",
            "password": "pass",
            "ssl": true,
        });
        let config: DatabaseConfig = serde_json::from_value(config_json).unwrap();
        let (url, redacted_url) = get_redis_url(config);
        assert_eq!(url, "rediss://user:pass@example.com:6380");
        assert_eq!(redacted_url, "rediss://user:pass@example.com:6380");
    }

    #[rstest]
    fn test_get_redis_url_full_config_without_ssl() {
        let config_json = json!({
            "host": "example.com",
            "port": 6380,
            "username": "username",
            "password": "password",
            "ssl": false,
        });
        let config: DatabaseConfig = serde_json::from_value(config_json).unwrap();
        let (url, redacted_url) = get_redis_url(config);
        assert_eq!(url, "redis://username:password@example.com:6380");
        assert_eq!(redacted_url, "redis://username:pa...rd@example.com:6380");
    }

    #[rstest]
    fn test_get_redis_url_missing_username_and_password() {
        let config_json = json!({
            "host": "example.com",
            "port": 6380,
            "ssl": false,
        });
        let config: DatabaseConfig = serde_json::from_value(config_json).unwrap();
        let (url, redacted_url) = get_redis_url(config);
        assert_eq!(url, "redis://example.com:6380");
        assert_eq!(redacted_url, "redis://example.com:6380");
    }

    #[rstest]
    fn test_get_redis_url_ssl_default_false() {
        let config_json = json!({
            "host": "example.com",
            "port": 6380,
            "username": "username",
            "password": "password",
            // "ssl" is intentionally omitted to test default behavior
        });
        let config: DatabaseConfig = serde_json::from_value(config_json).unwrap();
        let (url, redacted_url) = get_redis_url(config);
        assert_eq!(url, "redis://username:password@example.com:6380");
        assert_eq!(redacted_url, "redis://username:pa...rd@example.com:6380");
    }

    #[rstest]
    fn test_get_stream_key_with_trader_prefix_and_instance_id() {
        let trader_id = TraderId::from("tester-123");
        let instance_id = UUID4::new();
        let config = MessageBusConfig {
            use_instance_id: true,
            ..Default::default()
        };

        let key = get_stream_key(trader_id, instance_id, &config);
        assert_eq!(key, format!("trader-tester-123:{instance_id}:stream"));
    }

    #[rstest]
    fn test_get_stream_key_without_trader_prefix_or_instance_id() {
        let trader_id = TraderId::from("tester-123");
        let instance_id = UUID4::new();
        let config = MessageBusConfig {
            use_trader_prefix: false,
            use_trader_id: false,
            ..Default::default()
        };

        let key = get_stream_key(trader_id, instance_id, &config);
        assert_eq!(key, format!("stream"));
    }

    #[rstest]
    fn test_get_index_key_without_prefix() {
        let key = "index:order_position";
        assert_eq!(get_index_key(key).unwrap(), "index:order_position");
    }

    #[rstest]
    fn test_get_index_key_with_trader_prefix() {
        let key = "trader-tester-123:index:order_position";
        assert_eq!(get_index_key(key).unwrap(), "index:order_position");
    }

    #[rstest]
    fn test_get_index_key_with_instance_id() {
        let key = "trader-tester-123:abc-uuid-123:index:order_position";
        assert_eq!(get_index_key(key).unwrap(), "index:order_position");
    }

    #[rstest]
    fn test_get_index_key_invalid() {
        let key = "no_index_pattern";
        assert!(get_index_key(key).is_err());
    }
}