Skip to content

Commit

Permalink
feat(raiko): config redis ttl from cmdline & default 1 hour (taikoxyz…
Browse files Browse the repository at this point in the history
…#406)

* feat(raiko): config redis ttl from cmdline

Signed-off-by: smtmfft <smtm@taiko.xyz>

* fix CI

Signed-off-by: smtmfft <smtm@taiko.xyz>

---------

Signed-off-by: smtmfft <smtm@taiko.xyz>
  • Loading branch information
smtmfft authored Nov 11, 2024
1 parent 831efbe commit a497171
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ mod tests {
height - 100
}

#[ignore = "public node does not support long distance MPT proof query."]
#[tokio::test(flavor = "multi_thread")]
async fn test_prove_block_ethereum() {
let proof_type = get_proof_type_from_env();
Expand Down
1 change: 1 addition & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ services:
- SKIP_SIMULATION=true
- SP1_VERIFIER_RPC_URL=${SP1_VERIFIER_RPC_URL}
- SP1_VERIFIER_ADDRESS=${SP1_VERIFIER_ADDRESS}
- PROVER_NETWORK_RPC=${PROVER_NETWORK_RPC}
- REDIS_URL=${REDIS_URL:-redis://redis:6379}
depends_on:
- redis
Expand Down
5 changes: 5 additions & 0 deletions host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ pub struct Opts {

#[arg(long, require_equals = true, default_value = "redis://localhost:6379")]
pub redis_url: String,

#[arg(long, require_equals = true, default_value = "3600")]
pub redis_ttl: u64,
}

impl Opts {
Expand Down Expand Up @@ -132,6 +135,7 @@ impl From<Opts> for TaskManagerOpts {
sqlite_file: val.sqlite_file,
max_db_size: val.max_db_size,
redis_url: val.redis_url.to_string(),
redis_ttl: val.redis_ttl,
}
}
}
Expand All @@ -142,6 +146,7 @@ impl From<&Opts> for TaskManagerOpts {
sqlite_file: val.sqlite_file.clone(),
max_db_size: val.max_db_size,
redis_url: val.redis_url.to_string(),
redis_ttl: val.redis_ttl,
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions taskdb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ pub struct TaskManagerOpts {
pub sqlite_file: PathBuf,
pub max_db_size: usize,
pub redis_url: String,
pub redis_ttl: u64,
}

#[async_trait::async_trait]
Expand Down Expand Up @@ -445,6 +446,7 @@ mod test {
sqlite_file: sqlite_file.to_path_buf(),
max_db_size: 1024 * 1024,
redis_url: "redis://localhost:6379".to_string(),
redis_ttl: 3600,
};
let mut task_manager = get_task_manager(&opts);

Expand Down
23 changes: 16 additions & 7 deletions taskdb/src/redis_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::{

pub struct RedisTaskDb {
conn: Connection,
config: RedisConfig,
}

pub struct RedisTaskManager {
Expand Down Expand Up @@ -129,14 +130,18 @@ impl ToRedisArgs for TaskIdDescriptor {
}
}

// redis key ttl
const TTL_SECS: u64 = 2 * 24 * 3600; // 2 days
#[derive(Debug, Clone, Default)]
pub struct RedisConfig {
url: String,
ttl: u64,
}

impl RedisTaskDb {
fn new(url: &str) -> RedisDbResult<Self> {
fn new(config: RedisConfig) -> RedisDbResult<Self> {
let url = config.url.clone();
let client = Client::open(url).map_err(RedisDbError::RedisDb)?;
let conn = client.get_connection().map_err(RedisDbError::RedisDb)?;
Ok(RedisTaskDb { conn })
Ok(RedisTaskDb { conn, config })
}

fn insert_proof_task(
Expand All @@ -161,7 +166,7 @@ impl RedisTaskDb {
V: ToRedisArgs,
{
self.conn
.set_ex(key, value, TTL_SECS)
.set_ex(key, value, self.config.ttl)
.map_err(RedisDbError::RedisDb)?;
Ok(())
}
Expand Down Expand Up @@ -272,7 +277,7 @@ impl RedisTaskDb {
}

fn update_status_redis(&mut self, k: &String, v: &String) -> RedisDbResult<()> {
self.conn.set_ex(k, v, TTL_SECS)?;
self.conn.set_ex(k, v, self.config.ttl)?;
Ok(())
}
}
Expand Down Expand Up @@ -553,7 +558,11 @@ impl TaskManager for RedisTaskManager {
INIT.call_once(|| {
unsafe {
CONN = Some(Arc::new(Mutex::new({
let db = RedisTaskDb::new(&opts.redis_url).unwrap();
let db = RedisTaskDb::new(RedisConfig {
url: opts.redis_url.clone(),
ttl: opts.redis_ttl.clone(),
})
.unwrap();
db
})))
};
Expand Down
2 changes: 2 additions & 0 deletions taskdb/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ mod tests {
sqlite_file: file,
max_db_size: 1_000_000,
redis_url: env::var("REDIS_URL").unwrap_or_default(),
redis_ttl: 3600,
});

let (chain_id, blockhash, request) =
Expand Down Expand Up @@ -106,6 +107,7 @@ mod tests {
sqlite_file: file,
max_db_size: 1_000_000,
redis_url: env::var("REDIS_URL").unwrap_or_default(),
redis_ttl: 3600,
});

let mut rng = ChaCha8Rng::seed_from_u64(123);
Expand Down

0 comments on commit a497171

Please sign in to comment.