mirror of
https://github.com/fluencelabs/registry.git
synced 2025-04-25 10:12:13 +00:00
add config file
This commit is contained in:
parent
7942e40d5f
commit
b0fa49da08
6
Cargo.lock
generated
6
Cargo.lock
generated
@ -30,6 +30,8 @@ dependencies = [
|
||||
"fstrings",
|
||||
"marine-sqlite-connector",
|
||||
"rusqlite",
|
||||
"serde",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -625,9 +627,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.3.2"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87cbf45460356b7deeb5e3415b5563308c0a9b057c85e12b06ad551f98d0a6ac"
|
||||
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
|
||||
dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
@ -15,6 +15,8 @@ marine-sqlite-connector = "0.4.1"
|
||||
fstrings = "0.2.3"
|
||||
eyre = "0.6.5"
|
||||
boolinator = "2.4.0"
|
||||
toml = "0.5.6"
|
||||
serde = { version = "1.0.118", features = ["derive"] }
|
||||
|
||||
[dev-dependencies]
|
||||
fluence-test = "0.1.9"
|
||||
|
@ -1,14 +1,11 @@
|
||||
data Key:
|
||||
key: string
|
||||
peer_id: string
|
||||
timestamp_created: u64
|
||||
pinned: bool
|
||||
weight: u32
|
||||
|
||||
data GetKeyMetadataResult:
|
||||
data DhtResult:
|
||||
success: bool
|
||||
error: string
|
||||
key: Key
|
||||
|
||||
data RepublishValuesResult:
|
||||
success: bool
|
||||
error: string
|
||||
updated: u64
|
||||
|
||||
data Record:
|
||||
value: string
|
||||
@ -24,20 +21,12 @@ data MergeResult:
|
||||
error: string
|
||||
result: []Record
|
||||
|
||||
data GetValuesResult:
|
||||
success: bool
|
||||
error: string
|
||||
result: []Record
|
||||
|
||||
data ClearExpiredResult:
|
||||
success: bool
|
||||
error: string
|
||||
count_keys: u64
|
||||
count_values: u64
|
||||
|
||||
data DhtResult:
|
||||
success: bool
|
||||
error: string
|
||||
data Key:
|
||||
key: string
|
||||
peer_id: string
|
||||
timestamp_created: u64
|
||||
pinned: bool
|
||||
weight: u32
|
||||
|
||||
data EvictStaleItem:
|
||||
key: Key
|
||||
@ -48,10 +37,21 @@ data EvictStaleResult:
|
||||
error: string
|
||||
results: []EvictStaleItem
|
||||
|
||||
data RepublishValuesResult:
|
||||
data GetKeyMetadataResult:
|
||||
success: bool
|
||||
error: string
|
||||
updated: u64
|
||||
key: Key
|
||||
|
||||
data ClearExpiredResult:
|
||||
success: bool
|
||||
error: string
|
||||
count_keys: u64
|
||||
count_values: u64
|
||||
|
||||
data GetValuesResult:
|
||||
success: bool
|
||||
error: string
|
||||
result: []Record
|
||||
|
||||
service AquaDHT("aqua-dht"):
|
||||
clear_expired(current_timestamp: u64) -> ClearExpiredResult
|
||||
@ -70,3 +70,6 @@ service AquaDHT("aqua-dht"):
|
||||
renew_host_value(key: string, current_timestamp: u64) -> DhtResult
|
||||
republish_key(key: Key, current_timestamp: u64) -> DhtResult
|
||||
republish_values(key: string, records: []Record, current_timestamp: u64) -> RepublishValuesResult
|
||||
set_expired_timeout(timeout: u64) -> ()
|
||||
set_host_expired_timeout(timeout: u64) -> ()
|
||||
set_stale_timeout(timeout: u64) -> ()
|
||||
|
32
src/impls.rs
32
src/impls.rs
@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::{KEYS_TABLE_NAME, VALUES_TABLE_NAME, DB_PATH, TRUSTED_TIMESTAMP_SERVICE_ID, TRUSTED_TIMESTAMP_FUNCTION_NAME, EXPIRED_VALUE_AGE, STALE_VALUE_AGE, EXPIRED_HOST_VALUE_AGE, VALUES_LIMIT};
|
||||
use crate::{Config, KEYS_TABLE_NAME, VALUES_TABLE_NAME, DB_PATH, TRUSTED_TIMESTAMP_SERVICE_ID, TRUSTED_TIMESTAMP_FUNCTION_NAME, DEFAULT_EXPIRED_VALUE_AGE, DEFAULT_STALE_VALUE_AGE, DEFAULT_EXPIRED_HOST_VALUE_AGE, VALUES_LIMIT, CONFIG_FILE};
|
||||
use crate::results::{Key, Record, EvictStaleItem};
|
||||
use marine_sqlite_connector::{Connection, Result as SqliteResult, Error as SqliteError, State, Statement};
|
||||
use fluence::{CallParameters};
|
||||
@ -22,7 +22,8 @@ use eyre;
|
||||
use eyre::ContextCompat;
|
||||
use std::collections::HashMap;
|
||||
use boolinator::Boolinator;
|
||||
|
||||
use toml;
|
||||
use std::fs;
|
||||
|
||||
fn get_custom_option(value: String) -> Vec<String> {
|
||||
if value.is_empty() {
|
||||
@ -110,6 +111,26 @@ pub(crate) fn create_values_table() -> bool {
|
||||
).is_ok()
|
||||
}
|
||||
|
||||
pub fn write_config(config: Config) {
|
||||
fs::write(CONFIG_FILE, toml::to_string(&config).unwrap()).unwrap();
|
||||
}
|
||||
|
||||
pub fn load_config() -> Config {
|
||||
let file_content = fs::read_to_string(CONFIG_FILE).unwrap();
|
||||
let config: Config = toml::from_str(&file_content).unwrap();
|
||||
config
|
||||
}
|
||||
|
||||
pub(crate) fn create_config() {
|
||||
if fs::metadata(CONFIG_FILE).is_err() {
|
||||
write_config(Config {
|
||||
expired_timeout: DEFAULT_EXPIRED_VALUE_AGE,
|
||||
stale_timeout: DEFAULT_STALE_VALUE_AGE,
|
||||
host_expired_timeout: DEFAULT_EXPIRED_HOST_VALUE_AGE,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn get_key_metadata_helper(connection: &Connection, key: String, current_timestamp: u64) -> SqliteResult<Key> {
|
||||
connection.execute(
|
||||
f!("UPDATE {KEYS_TABLE_NAME} \
|
||||
@ -272,9 +293,10 @@ pub fn clear_expired_impl(current_timestamp: u64) -> SqliteResult<(u64, u64)> {
|
||||
check_timestamp_tetraplets(&call_parameters, 0)
|
||||
.map_err(|e| SqliteError { code: None, message: Some(e.to_string()) })?;
|
||||
let connection = get_connection()?;
|
||||
let config = load_config();
|
||||
|
||||
let expired_host_timestamp = current_timestamp - EXPIRED_HOST_VALUE_AGE;
|
||||
let expired_timestamp = current_timestamp - EXPIRED_VALUE_AGE;
|
||||
let expired_host_timestamp = current_timestamp - config.host_expired_timeout;
|
||||
let expired_timestamp = current_timestamp - config.expired_timeout;
|
||||
let mut deleted_values = 0u64;
|
||||
let host_id = call_parameters.host_id;
|
||||
connection.execute(f!("DELETE FROM {VALUES_TABLE_NAME} WHERE key IN (SELECT key FROM {KEYS_TABLE_NAME} \
|
||||
@ -306,7 +328,7 @@ pub fn evict_stale_impl(current_timestamp: u64) -> SqliteResult<Vec<EvictStaleIt
|
||||
check_timestamp_tetraplets(&call_parameters, 0)
|
||||
.map_err(|e| SqliteError { code: None, message: Some(e.to_string()) })?;
|
||||
let connection = get_connection()?;
|
||||
let stale_timestamp = current_timestamp - STALE_VALUE_AGE;
|
||||
let stale_timestamp = current_timestamp - load_config().stale_timeout;
|
||||
|
||||
let mut stale_keys: Vec<Key> = vec![];
|
||||
let mut statement =
|
||||
|
40
src/main.rs
40
src/main.rs
@ -20,11 +20,13 @@ mod tests;
|
||||
mod impls;
|
||||
|
||||
use crate::results::{Key, GetKeyMetadataResult, DhtResult, GetValuesResult, Record, RepublishValuesResult, ClearExpiredResult, EvictStaleResult, MergeResult};
|
||||
use crate::impls::{create_keys_table, create_values_table, register_key_impl, get_key_metadata_impl, republish_key_impl, put_value_impl, get_values_impl, republish_values_impl, clear_expired_impl, evict_stale_impl, merge_impl, renew_host_value_impl, clear_host_value_impl};
|
||||
use crate::impls::{create_keys_table, create_values_table, register_key_impl, get_key_metadata_impl, republish_key_impl, put_value_impl, get_values_impl, republish_values_impl, clear_expired_impl, evict_stale_impl, merge_impl, renew_host_value_impl, clear_host_value_impl, create_config, load_config, write_config};
|
||||
|
||||
use fluence::marine;
|
||||
use fluence::module_manifest;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[macro_use]
|
||||
extern crate fstrings;
|
||||
|
||||
@ -32,18 +34,27 @@ module_manifest!();
|
||||
|
||||
pub static KEYS_TABLE_NAME: &str = "dht_keys";
|
||||
pub static VALUES_TABLE_NAME: &str = "dht_values";
|
||||
pub static CONFIG_FILE: &str = "/tmp/Config.toml";
|
||||
pub static DB_PATH: &str = "/tmp/dht.db";
|
||||
pub static STALE_VALUE_AGE: u64 = 60 * 60;
|
||||
pub static EXPIRED_VALUE_AGE: u64 = 24 * 60 * 60;
|
||||
pub static EXPIRED_HOST_VALUE_AGE: u64 = 10 * EXPIRED_VALUE_AGE;
|
||||
pub static DEFAULT_STALE_VALUE_AGE: u64 = 60 * 60;
|
||||
pub static DEFAULT_EXPIRED_VALUE_AGE: u64 = 24 * 60 * 60;
|
||||
pub static DEFAULT_EXPIRED_HOST_VALUE_AGE: u64 = 10 * DEFAULT_EXPIRED_VALUE_AGE;
|
||||
pub static VALUES_LIMIT: usize = 20;
|
||||
|
||||
pub static TRUSTED_TIMESTAMP_SERVICE_ID: &str = "peer";
|
||||
pub static TRUSTED_TIMESTAMP_FUNCTION_NAME: &str = "timestamp_sec";
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Config {
|
||||
pub expired_timeout: u64,
|
||||
pub stale_timeout: u64,
|
||||
pub host_expired_timeout: u64,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
create_keys_table();
|
||||
create_values_table();
|
||||
create_config();
|
||||
}
|
||||
|
||||
// KEYS
|
||||
@ -134,4 +145,25 @@ pub fn merge_hack_get_values(records: Vec<GetValuesResult>) -> MergeResult {
|
||||
.flatten()
|
||||
.collect()
|
||||
).into()
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn set_expired_timeout(timeout: u64) {
|
||||
let mut config = load_config();
|
||||
config.expired_timeout = timeout;
|
||||
write_config(config);
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn set_host_expired_timeout(timeout: u64) {
|
||||
let mut config = load_config();
|
||||
config.host_expired_timeout = timeout;
|
||||
write_config(config);
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn set_stale_timeout(timeout: u64) {
|
||||
let mut config = load_config();
|
||||
config.stale_timeout = timeout;
|
||||
write_config(config);
|
||||
}
|
14
src/tests.rs
14
src/tests.rs
@ -18,7 +18,7 @@
|
||||
mod tests {
|
||||
use fluence_test::marine_test;
|
||||
use rusqlite::{Connection};
|
||||
use crate::{KEYS_TABLE_NAME, VALUES_TABLE_NAME, DB_PATH, TRUSTED_TIMESTAMP_FUNCTION_NAME, TRUSTED_TIMESTAMP_SERVICE_ID, EXPIRED_VALUE_AGE, STALE_VALUE_AGE, VALUES_LIMIT};
|
||||
use crate::{KEYS_TABLE_NAME, VALUES_TABLE_NAME, DB_PATH, TRUSTED_TIMESTAMP_FUNCTION_NAME, TRUSTED_TIMESTAMP_SERVICE_ID, DEFAULT_EXPIRED_VALUE_AGE, DEFAULT_STALE_VALUE_AGE, VALUES_LIMIT};
|
||||
use fluence::{CallParameters, SecurityTetraplet};
|
||||
use std::time::SystemTime;
|
||||
|
||||
@ -510,7 +510,7 @@ mod tests {
|
||||
let expired_timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
|
||||
register_key_and_check!(aqua_dht, key.clone(), expired_timestamp.clone(), false, 8u32, get_correct_timestamp_cp(1));
|
||||
|
||||
let result = aqua_dht.clear_expired_cp(expired_timestamp + EXPIRED_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
let result = aqua_dht.clear_expired_cp(expired_timestamp + DEFAULT_EXPIRED_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
|
||||
assert!(result.success);
|
||||
assert_eq!(result.error, "");
|
||||
@ -530,7 +530,7 @@ mod tests {
|
||||
register_key_and_check!(aqua_dht, key.clone(), expired_timestamp.clone(), true, 8u32, get_correct_timestamp_cp(1));
|
||||
put_value_and_check!(aqua_dht, key.clone(), "some_value".to_string(), expired_timestamp.clone(), vec![], vec![], 8u32, get_correct_timestamp_cp(2));
|
||||
|
||||
let result = aqua_dht.clear_expired_cp(expired_timestamp + EXPIRED_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
let result = aqua_dht.clear_expired_cp(expired_timestamp + DEFAULT_EXPIRED_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
|
||||
assert!(result.success);
|
||||
assert_eq!(result.error, "");
|
||||
@ -546,7 +546,7 @@ mod tests {
|
||||
register_key_and_check!(aqua_dht, key.clone(), expired_timestamp.clone(), false, 8u32, get_correct_timestamp_cp(1));
|
||||
put_host_value_and_check!(aqua_dht, key.clone(), "some_value".to_string(), expired_timestamp.clone(), vec![], vec![], 8u32, get_correct_timestamp_cp(2));
|
||||
|
||||
let result = aqua_dht.clear_expired_cp(expired_timestamp + EXPIRED_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
let result = aqua_dht.clear_expired_cp(expired_timestamp + DEFAULT_EXPIRED_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
|
||||
assert!(result.success);
|
||||
assert_eq!(result.error, "");
|
||||
@ -562,7 +562,7 @@ mod tests {
|
||||
register_key_and_check!(aqua_dht, key.clone(), expired_timestamp.clone(), false, 8u32, get_correct_timestamp_cp(1));
|
||||
put_value_and_check!(aqua_dht, key.clone(), "some_value".to_string(), expired_timestamp.clone(), vec![], vec![], 8u32, get_correct_timestamp_cp(2));
|
||||
|
||||
let result = aqua_dht.clear_expired_cp(expired_timestamp + EXPIRED_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
let result = aqua_dht.clear_expired_cp(expired_timestamp + DEFAULT_EXPIRED_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
|
||||
assert!(result.success);
|
||||
assert_eq!(result.error, "");
|
||||
@ -623,7 +623,7 @@ mod tests {
|
||||
let stale_timestamp = 0u64;
|
||||
register_key_and_check!(aqua_dht, key.clone(), stale_timestamp.clone(), false, 8u32, get_correct_timestamp_cp(1));
|
||||
|
||||
let result = aqua_dht.evict_stale_cp(stale_timestamp + STALE_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
let result = aqua_dht.evict_stale_cp(stale_timestamp + DEFAULT_STALE_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
|
||||
assert!(result.success);
|
||||
assert_eq!(result.error, "");
|
||||
@ -646,7 +646,7 @@ mod tests {
|
||||
register_key_and_check!(aqua_dht, key.clone(), stale_timestamp.clone(), false, 8u32, get_correct_timestamp_cp(1));
|
||||
put_value_and_check!(aqua_dht, key.clone(), value.clone(), stale_timestamp.clone(), vec![], vec![], 8u32, get_correct_timestamp_cp(2));
|
||||
|
||||
let result = aqua_dht.evict_stale_cp(stale_timestamp + STALE_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
let result = aqua_dht.evict_stale_cp(stale_timestamp + DEFAULT_STALE_VALUE_AGE, get_correct_timestamp_cp(0));
|
||||
|
||||
assert!(result.success);
|
||||
assert_eq!(result.error, "");
|
||||
|
Loading…
x
Reference in New Issue
Block a user