Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start with env variables from Configuration with Debug/Non Debug mode. #62

Merged
merged 20 commits into from Feb 10, 2022
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -76,7 +76,8 @@ Done you now have nun-db running in your docker and exposing all the ports to be
* [How to create your simple version of google analytics real-time using Nun-db](https://mateusfreira.github.io/@mateusfreira-create-a-simple-verison-of-google-analytics-realtime-using-nun-db/)
* [NunDb How to backup one or all databases](https://mateusfreira.github.io/@mateusfreira-nundb-how-to-backups-all-databases-with-one-command/)


* [To snapshot the database from memory to disk]
thienpow marked this conversation as resolved.
Show resolved Hide resolved
nun-db --user $NUN_USER -p $NUN_PWD --host "https://http.nundb.org" exec "use-db $DB_NAME $DB_TOKEN; snapshot"

## Technical documentations

Expand Down
1 change: 1 addition & 0 deletions createdb.sh
@@ -0,0 +1 @@
cargo run -- -u mateus -p mateus create-db -d todos -t tokenxyz
thienpow marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions debug.sh
@@ -0,0 +1,2 @@
export NUN_DBS_DIR=/tmp/data/nun_db
thienpow marked this conversation as resolved.
Show resolved Hide resolved
cargo run -- -u mateus -p mateus start --http-address 0.0.0.0:3013 --tcp-address 0.0.0.0:3014 --ws-address 0.0.0.0:3012
1 change: 1 addition & 0 deletions snapshot.sh
@@ -0,0 +1 @@
cargo run -- -u mateus -p mateus exec "use-db todos tokenxyz; snapshot;"
4 changes: 2 additions & 2 deletions src/lib/commad_line/commands.rs
Expand Up @@ -10,14 +10,14 @@ pub fn prepare_args<'a>() -> ArgMatches<'static> {
Arg::with_name("user")
.short("u")
.long("user")
.required(true)
//.required(false)
thienpow marked this conversation as resolved.
Show resolved Hide resolved
.takes_value(true)
.help("Admin username"),
)
.arg(
Arg::with_name("pwd")
.short("p")
.required(true)
//.required(false)
thienpow marked this conversation as resolved.
Show resolved Hide resolved
.takes_value(true)
.help("Admin password"),
)
Expand Down
43 changes: 43 additions & 0 deletions src/lib/configuration.rs
@@ -0,0 +1,43 @@
use std::env;

#[derive(Debug)]
pub struct Configuration {
pub nun_user: String,
pub nun_pwd: String,
pub nun_dbs_dir: String,
pub nun_ws_addr: String,
pub nun_http_addr: String,
pub nun_tcp_addr: String,
pub nun_replicate_addr: String,
pub nun_log_level: String,
pub nun_log_dir: String,
pub nun_log_to_file: String,
}

#[cfg(debug_assertions)]
fn expect_env_var(name: &str, default: &str) -> String {
return env::var(name).unwrap_or(String::from(default));
}

#[cfg(not(debug_assertions))]
fn expect_env_var(name: &str, _default: &str) -> String {
thienpow marked this conversation as resolved.
Show resolved Hide resolved
return env::var(name).expect(&format!(
mateusfreira marked this conversation as resolved.
Show resolved Hide resolved
"Environment variable {name} is not defined",
name = name
));
}

pub fn get_configuration() -> Configuration {
Configuration {
nun_user: expect_env_var("NUN_USER", "mateus"),
thienpow marked this conversation as resolved.
Show resolved Hide resolved
nun_pwd: expect_env_var("NUN_PWD", "mateus"),
thienpow marked this conversation as resolved.
Show resolved Hide resolved
nun_dbs_dir: expect_env_var("NUN_DBS_DIR", "/tmp/data/nun_db"),
nun_ws_addr: expect_env_var("NUN_WS_ADDR", "0.0.0.0:3012"),
nun_http_addr: expect_env_var("NUN_HTTP_ADDR", "0.0.0.0:3013"),
nun_tcp_addr: expect_env_var("NUN_HTTP_ADDR", "0.0.0.0:3014"),
thienpow marked this conversation as resolved.
Show resolved Hide resolved
nun_replicate_addr: expect_env_var("NUN_REPLICATE_ADDR", ""),
nun_log_level: expect_env_var("NUN_LOG_LEVEL", "Info"), //(Off, Error, Warn, Info, Debug, Trace)
nun_log_dir: expect_env_var("NUN_LOG_DIR", "/tmp/data/nun_db/log"),
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used anywhere, are you planning to add this to some other part of the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah will hunt down where they should be in the codes and add more for the snapshot thing later.

nun_log_to_file: expect_env_var("NUN_LOG_TO_FILE", "true"), //true or false
}
}
1 change: 1 addition & 0 deletions src/lib/db_ops.rs
Expand Up @@ -94,6 +94,7 @@ pub fn create_db(name: &String, token: &String, dbs: &Arc<Databases>, client: &C
}
}
}

pub fn snapshot_db(db: &Database, dbs: &Databases) -> Response {
let name = db.name.clone();
{
Expand Down
11 changes: 6 additions & 5 deletions src/lib/disk_ops.rs
Expand Up @@ -17,10 +17,11 @@ use std::time::Instant;

use crate::bo::*;

use super::configuration::get_configuration;

const SNAPSHOT_TIME: i64 = 3000; // 30 secounds
const FILE_NAME: &'static str = "-nun.data";
const META_FILE_NAME: &'static str = "-nun.madadata";
const DIR_NAME: &'static str = "dbs";

const KEYS_FILE: &'static str = "keys-nun.keys";

Expand All @@ -34,10 +35,7 @@ const OP_OP_SIZE: usize = 1;
const OP_RECORD_SIZE: usize = OP_TIME_SIZE + OP_DB_ID_SIZE + OP_KEY_SIZE + OP_OP_SIZE;

pub fn get_dir_name() -> String {
match env::var_os("NUN_DBS_DIR") {
Some(dir_name) => dir_name.into_string().unwrap(),
None => DIR_NAME.to_string(),
}
get_configuration().nun_dbs_dir
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are building the config object with several props and keys to use one... we should find a better way to build it only once

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe can try lazy_static crate... later i try.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now is using lazy_static @mateusfreira can take a look

}

pub fn load_keys_map_from_disk() -> HashMap<String, u64> {
Expand Down Expand Up @@ -204,7 +202,10 @@ pub fn start_snap_shot_timer(timer: timer::Timer, dbs: Arc<Databases>) {

pub fn snapshot_all_pendding_dbs(dbs: &Arc<Databases>) {
let queue_len = { dbs.to_snapshot.read().unwrap().len() };
println!("called snapshot_all_pendding_dbs");
thienpow marked this conversation as resolved.
Show resolved Hide resolved
println!("queue_len > 0 == {}", queue_len);
thienpow marked this conversation as resolved.
Show resolved Hide resolved
if queue_len > 0 {
println!("inside queue_len");
thienpow marked this conversation as resolved.
Show resolved Hide resolved
snapshot_keys(&dbs);
let mut dbs_to_snapshot = {
let dbs = dbs.to_snapshot.write().unwrap();
Expand Down
1 change: 1 addition & 0 deletions src/lib/mod.rs
@@ -1,3 +1,4 @@
pub mod configuration;
pub mod bo;
pub mod commad_line;
pub mod db_ops;
Expand Down
28 changes: 16 additions & 12 deletions src/main.rs
Expand Up @@ -4,6 +4,7 @@ use futures::channel::mpsc::{channel, Receiver, Sender};
use futures::executor::block_on;
use futures::join;
use lib::*;
use lib::configuration::Configuration;
use log;
use signal_hook::{consts::SIGINT, iterator::Signals};
use std::thread;
Expand All @@ -13,8 +14,8 @@ use std::sync::Arc;
use clap::ArgMatches;
use env_logger::{Builder, Env, Target};

fn init_logger() {
let env = Env::default().filter_or("NUN_LOG_LEVEL", "info");
fn init_logger(config: &Configuration) {
let env = Env::default().filter_or(config.nun_log_level.as_str(), "info");
thienpow marked this conversation as resolved.
Show resolved Hide resolved
Builder::from_env(env)
.format_level(false)
.target(Target::Stdout)
Expand All @@ -23,21 +24,23 @@ fn init_logger() {
}

fn main() -> Result<(), String> {
init_logger();
let config = configuration::get_configuration();

init_logger(&config);
log::info!("nundb starting!");
let matches: ArgMatches<'_> = lib::commad_line::commands::prepare_args();
if let Some(start_match) = matches.subcommand_matches("start") {
return start_db(
matches.value_of("user").unwrap(),
matches.value_of("pwd").unwrap(),
start_match
.value_of("tcp-address")
.unwrap_or("0.0.0.0:3014"),
start_match.value_of("ws-address").unwrap_or("0.0.0.0:3012"),
matches.value_of("user").unwrap_or(config.nun_user.as_str()),
matches.value_of("pwd").unwrap_or(config.nun_pwd.as_str()),
start_match.value_of("ws-address").unwrap_or(config.nun_ws_addr.as_str()),
start_match
.value_of("http-address")
.unwrap_or("0.0.0.0:3013"),
start_match.value_of("replicate-address").unwrap_or(""),
.unwrap_or(config.nun_http_addr.as_str()),
start_match
.value_of("tcp-address")
.unwrap_or(config.nun_tcp_addr.as_str()),
start_match.value_of("replicate-address").unwrap_or(config.nun_replicate_addr.as_str()),
);
} else {
return lib::commad_line::commands::exec_command(&matches);
Expand All @@ -47,11 +50,12 @@ fn main() -> Result<(), String> {
fn start_db(
user: &str,
pwd: &str,
tcp_address: &str,
ws_address: &str,
http_address: &str,
tcp_address: &str,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change the order???

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bcos 3012,3013,3014...

replicate_address: &str,
) -> Result<(), String> {

let (replication_sender, replication_receiver): (Sender<String>, Receiver<String>) =
channel(100);

Expand Down