Skip to content

Commit b33aa5d

Browse files
committed
created per-ledger cache for FFIs
Signed-off-by: wadeking98 <[email protected]>
1 parent 20bb31e commit b33aa5d

File tree

3 files changed

+77
-13
lines changed

3 files changed

+77
-13
lines changed

libindy_vdr/src/ffi/mod.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@ mod resolver;
1515

1616
use crate::common::error::prelude::*;
1717
use crate::config::{PoolConfig, LIB_VERSION};
18-
use crate::pool::cache::storage::{new_fs_ordered_store, OrderedHashMap};
19-
use crate::pool::cache::strategy::CacheStrategyTTL;
20-
use crate::pool::cache::Cache;
2118
use crate::pool::{FilesystemCache, PoolTransactionsCache, ProtocolVersion};
2219
use crate::utils::Validatable;
2320

2421
use self::error::{set_last_error, ErrorCode};
25-
use self::pool::{LEDGER_TXN_CACHE, POOL_CACHE, POOL_CONFIG};
22+
use self::pool::{LegerCacheConfig, LEDGER_TXN_CACHE_CONFIG, POOL_CACHE, POOL_CONFIG};
2623

2724
pub type CallbackId = i64;
2825

@@ -79,8 +76,7 @@ pub extern "C" fn indy_vdr_set_cache_directory(path: FfiStr) -> ErrorCode {
7976
pub extern "C" fn indy_vdr_set_ledger_txn_cache(capacity: usize, expire_offset: u64) -> ErrorCode {
8077
catch_err! {
8178
debug!("Setting pool ledger transactions cache: capacity={}, expire_offset={}", capacity, expire_offset);
82-
let cache = Cache::new(CacheStrategyTTL::new(capacity, expire_offset.into(), None, None));
83-
*write_lock!(LEDGER_TXN_CACHE)? = Some(cache);
79+
*write_lock!(LEDGER_TXN_CACHE_CONFIG)? = Some(LegerCacheConfig::new(capacity, expire_offset.into(), None));
8480
Ok(ErrorCode::Success)
8581
}
8682
}
@@ -93,9 +89,7 @@ pub extern "C" fn indy_vdr_set_ledger_txn_fs_cache(
9389
) -> ErrorCode {
9490
catch_err! {
9591
debug!("Setting pool ledger transactions cache: capacity={}, expire_offset={}", capacity, expire_offset);
96-
let store = OrderedHashMap::new(new_fs_ordered_store(path.into())?);
97-
let cache = Cache::new(CacheStrategyTTL::new(capacity, expire_offset.into(), Some(store), None));
98-
*write_lock!(LEDGER_TXN_CACHE)? = Some(cache);
92+
*write_lock!(LEDGER_TXN_CACHE_CONFIG)? = Some(LegerCacheConfig::new(capacity, expire_offset.into(), Some(path.into_string())));
9993
Ok(ErrorCode::Success)
10094
}
10195
}

libindy_vdr/src/ffi/pool.rs

+61-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
use std::collections::{btree_map::Entry, BTreeMap, HashMap};
22
use std::os::raw::c_char;
3+
use std::path::Path;
34
use std::sync::{Arc, RwLock};
4-
use std::thread;
5+
use std::{fs, thread};
56

67
use ffi_support::{rust_string_to_c, FfiStr};
78
use once_cell::sync::Lazy;
89

910
use crate::common::error::prelude::*;
1011
use crate::common::handle::ResourceHandle;
1112
use crate::config::PoolConfig;
13+
use crate::pool::cache::storage::{new_fs_ordered_store, OrderedHashMap};
14+
use crate::pool::cache::strategy::CacheStrategyTTL;
1215
use crate::pool::cache::Cache;
1316
use crate::pool::{
1417
InMemoryCache, PoolBuilder, PoolRunner, PoolTransactions, PoolTransactionsCache, RequestMethod,
@@ -31,6 +34,50 @@ pub struct PoolInstance {
3134
pub node_weights: Option<NodeWeights>,
3235
}
3336

37+
#[derive(Clone)]
38+
pub struct LegerCacheConfig {
39+
pub cache_size: usize,
40+
pub cache_ttl: u128,
41+
pub path: Option<String>,
42+
}
43+
44+
impl LegerCacheConfig {
45+
pub fn new(cache_size: usize, cache_ttl: u128, path: Option<String>) -> Self {
46+
Self {
47+
cache_size,
48+
cache_ttl,
49+
path,
50+
}
51+
}
52+
pub fn create_cache(
53+
&self,
54+
id: Option<String>,
55+
) -> VdrResult<Cache<String, (String, RequestResultMeta)>> {
56+
if let Some(path) = &self.path {
57+
let full_path = Path::new(path).join(id.unwrap_or("default".to_string()));
58+
let full_path_string = full_path
59+
.into_os_string()
60+
.into_string()
61+
.unwrap_or(path.to_string());
62+
fs::create_dir_all(full_path_string.clone())?;
63+
let store = OrderedHashMap::new(new_fs_ordered_store(full_path_string)?);
64+
Ok(Cache::new(CacheStrategyTTL::new(
65+
self.cache_size,
66+
self.cache_ttl,
67+
Some(store),
68+
None,
69+
)))
70+
} else {
71+
Ok(Cache::new(CacheStrategyTTL::new(
72+
self.cache_size,
73+
self.cache_ttl,
74+
None,
75+
None,
76+
)))
77+
}
78+
}
79+
}
80+
3481
pub type NodeWeights = HashMap<String, f32>;
3582

3683
pub static POOL_CONFIG: Lazy<RwLock<PoolConfig>> = Lazy::new(|| RwLock::new(PoolConfig::default()));
@@ -41,7 +88,7 @@ pub static POOLS: Lazy<RwLock<BTreeMap<PoolHandle, PoolInstance>>> =
4188
pub static POOL_CACHE: Lazy<RwLock<Option<Arc<dyn PoolTransactionsCache>>>> =
4289
Lazy::new(|| RwLock::new(Some(Arc::new(InMemoryCache::new()))));
4390

44-
pub static LEDGER_TXN_CACHE: Lazy<RwLock<Option<Cache<String, (String, RequestResultMeta)>>>> =
91+
pub static LEDGER_TXN_CACHE_CONFIG: Lazy<RwLock<Option<LegerCacheConfig>>> =
4592
Lazy::new(|| RwLock::new(None));
4693

4794
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -72,6 +119,12 @@ pub extern "C" fn indy_vdr_pool_create(params: FfiStr, handle_p: *mut PoolHandle
72119
"Invalid pool create parameters: must provide transactions or transactions_path"
73120
));
74121
};
122+
let txn_cache_config = read_lock!(LEDGER_TXN_CACHE_CONFIG)?.clone();
123+
let txn_cache = if let Some(config) = txn_cache_config {
124+
config.create_cache(txns.root_hash_base58().ok()).ok()
125+
} else {
126+
None
127+
};
75128
let mut cached = false;
76129
if let Some(cache) = read_lock!(POOL_CACHE)?.as_ref() {
77130
if let Some(newer_txns) = cache.resolve_latest(&txns)? {
@@ -80,7 +133,6 @@ pub extern "C" fn indy_vdr_pool_create(params: FfiStr, handle_p: *mut PoolHandle
80133
}
81134
}
82135
let config = read_lock!(POOL_CONFIG)?.clone();
83-
let txn_cache = read_lock!(LEDGER_TXN_CACHE)?.clone();
84136
let runner = PoolBuilder::new(config, txns.clone()).node_weights(params.node_weights.clone()).refreshed(cached).into_runner(txn_cache)?;
85137
let handle = PoolHandle::next();
86138
let mut pools = write_lock!(POOLS)?;
@@ -107,7 +159,12 @@ fn handle_pool_refresh(
107159
cache.update(&init_txns, latest_txns)?;
108160
}
109161
if let Some(new_txns) = new_txns {
110-
let txn_cache = read_lock!(LEDGER_TXN_CACHE)?.clone();
162+
let txn_cache_config = read_lock!(LEDGER_TXN_CACHE_CONFIG)?.clone();
163+
let txn_cache = if let Some(config) = txn_cache_config {
164+
config.create_cache(init_txns.root_hash_base58().ok()).ok()
165+
} else {
166+
None
167+
};
111168
let runner = PoolBuilder::new(config, new_txns).node_weights(node_weights).refreshed(true).into_runner(txn_cache)?;
112169
let mut pools = write_lock!(POOLS)?;
113170
if let Entry::Occupied(mut entry) = pools.entry(pool_handle) {

wrappers/javascript/indy-vdr-shared/src/builder/PoolCreate.ts

+13
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,24 @@ export type PoolCreateOptions = {
55
transactions?: string
66
transactions_path?: string
77
node_weights?: Record<string, number>
8+
cache?: {
9+
size: number
10+
timeout: number,
11+
path?: string
12+
}
813
}
914
}
1015

1116
export class PoolCreate extends IndyVdrPool {
1217
public constructor(options: PoolCreateOptions) {
18+
if (options.parameters.cache) {
19+
const { size, timeout, path } = options.parameters.cache
20+
if (path) {
21+
indyVdr.setLedgerTxnFsCache({ capacity: size, expiry_offset_ms: timeout, path })
22+
} else {
23+
indyVdr.setLedgerTxnCache({ capacity: size, expiry_offset_ms: timeout })
24+
}
25+
}
1326
const handle = indyVdr.poolCreate(options)
1427
super({ handle })
1528
}

0 commit comments

Comments
 (0)