6
6
7
7
#include " config_generator.hpp"
8
8
9
+ #include " util/common/config.hpp"
9
10
#include " util/common/keys.hpp"
11
+ #include " util/common/variant_overloaded.hpp"
10
12
#include " util/network/tcp_listener.hpp"
11
13
12
14
#include < algorithm>
13
15
#include < cassert>
14
- #include < filesystem>
15
16
#include < functional>
16
17
#include < random>
17
18
#include < set>
18
19
19
20
// NORMAL CONFIGS
20
21
21
- // Integer that determines if this is 2PC or Atomizer. 1 means 2PC
22
- static constexpr auto two_phase_mode = " 2pc" ;
23
- // Number of shards to be created
24
- static constexpr auto shard_count_key = " shard_count" ;
25
- // Number of sentinels to be created
26
- static constexpr auto sentinel_count_key = " sentinel_count" ;
27
- // Number of coordinators to be created
28
- static constexpr auto coordinator_count_key = " coordinator_count" ;
29
- // Number of archivers to be created
30
- static constexpr auto archiver_count_key = " archiver_count" ;
31
- // Number of atomizers to be created
32
- static constexpr auto atomizer_count_key = " atomizer_count" ;
33
- // Number of watchtowers to be created
34
- static constexpr auto watchtower_count_key = " watchtower_count" ;
22
+ using namespace cbdc ::config;
23
+
24
+ // Using the following values from src/common/config.hpp:
25
+
26
+ // "cbdc::config::two_phase_mode" : Name of value in config template file that
27
+ // is an integer which determines if this is 2PC or Atomizer. 1 means 2PC
28
+ // "cbdc::config::shard_count_key" : Number of shards to be created
29
+ // "cbdc::config::sentinel_count_key" : Number of sentinels to be created
30
+ // "cbdc::config::coordinator_count_key" : Number of coordinators to be created
31
+ // "cbdc::config::archiver_count_key" : Number of archivers to be created
32
+ // "cbdc::config::atomizer_count_key" : Number of atomizers to be created
33
+ // "cbdc::config::watchtower_count_key" : Number of watchtowers to be created
34
+
35
35
// Prefix of interest that denotes parameters in the file that are used
36
36
// here to help generate the config file but will not be present in the
37
37
// final product
@@ -69,9 +69,41 @@ std::set<std::string> log_levels
69
69
namespace cbdc ::generate_config {
70
70
71
71
config_generator::config_generator (std::string& _template_config_file,
72
- const size_t _start_port)
72
+ const size_t _start_port,
73
+ std::string _build_dir)
73
74
: m_template_config_file(_template_config_file),
74
75
m_current_port (_start_port) {
76
+ // Get Project root dir and build dir
77
+ std::filesystem::path build_dir = std::filesystem::current_path ();
78
+ if (_build_dir.at (_build_dir.size () - 1 ) == ' /' ) {
79
+ _build_dir.erase (_build_dir.end () - 1 );
80
+ }
81
+ // This config generator class assumes project root is "opencbdc-tx"
82
+ while (build_dir.has_parent_path ()) {
83
+ if (build_dir.filename () == " opencbdc-tx" ) {
84
+ m_project_root_dir = build_dir;
85
+ std::string delimiter = " /" ;
86
+ std::string tmp_str = _build_dir;
87
+ size_t pos = 0 ;
88
+ std::string token;
89
+ while ((pos = tmp_str.find (" /" )) != std::string::npos) {
90
+ token = tmp_str.substr (0 , pos);
91
+ build_dir = build_dir.append (token);
92
+ tmp_str.erase (0 , pos + delimiter.length ());
93
+ }
94
+ token = tmp_str.substr (0 , pos);
95
+ build_dir = build_dir.append (token);
96
+ tmp_str.erase (0 , pos + delimiter.length ());
97
+ m_build_dir = build_dir;
98
+ std::cout << " Build directory determined to be "
99
+ << m_build_dir.string () << std::endl;
100
+ std::cout << " Project Root directory determined to be "
101
+ << m_project_root_dir.string () << std::endl;
102
+ break ;
103
+ } else {
104
+ build_dir = build_dir.parent_path ();
105
+ }
106
+ }
75
107
template_file_is_valid = true ;
76
108
if (!std::filesystem::exists (m_template_config_file)) {
77
109
template_file_is_valid = false ;
@@ -335,7 +367,7 @@ namespace cbdc::generate_config {
335
367
[[nodiscard]] auto config_generator::get_param_from_template_file (
336
368
const std::string option,
337
369
std::map<std::string, std::string>& config_map)
338
- -> std::variant<size_t, double, std::string> {
370
+ -> std::variant<std::string, size_t, double > {
339
371
auto it = config_map.find (option);
340
372
if (it != config_map.end ()) {
341
373
value_t parsed_val = parse_value (it->second , false );
@@ -346,10 +378,7 @@ namespace cbdc::generate_config {
346
378
} else if (std::holds_alternative<std::string>(parsed_val)) {
347
379
return std::get<std::string>(parsed_val);
348
380
} else {
349
- std::string error_msg
350
- = " Warning: Unrecognized type for param, " + option + " ." ;
351
- std::cout << error_msg << std::endl;
352
- return error_msg;
381
+ __builtin_unreachable ();
353
382
}
354
383
} else {
355
384
std::string error_msg
@@ -376,13 +405,12 @@ namespace cbdc::generate_config {
376
405
377
406
void config_generator::set_log_level (const std::string key,
378
407
std::string& log_level) {
379
- if (find_value (key, log_level) == false ) {
380
- find_value (tmpl_universal_override_log_level, log_level);
381
- }
382
408
if (log_levels.find (log_level) == log_levels.end ()) {
383
409
log_level = " DEBUG" ;
384
410
std::cout << " Warning: Log level not recognized. Setting to DEBUG"
385
411
<< std::endl;
412
+ } else if (find_value (key, log_level) == false ) {
413
+ find_value (tmpl_universal_override_log_level, log_level);
386
414
}
387
415
}
388
416
@@ -642,27 +670,16 @@ namespace cbdc::generate_config {
642
670
outFile.close ();
643
671
}
644
672
645
- // This method assumes project root is "opencbdc-tx" and build dir is
646
- // "build"
647
673
[[nodiscard]] auto
648
674
config_generator::copy_to_build_dir (const std::string filename) -> bool {
649
675
std::filesystem::path cwd = std::filesystem::current_path ();
650
676
cwd.append (filename);
651
- std::filesystem::path build_dir = std::filesystem::current_path ();
652
- while (build_dir.has_parent_path ()) {
653
- if (build_dir.filename () == " opencbdc-tx" ) {
654
- build_dir = build_dir.append (" build" );
655
- break ;
656
- } else {
657
- build_dir = build_dir.parent_path ();
658
- }
659
- }
660
677
if (std::filesystem::exists (filename)) {
661
678
const auto copyOptions
662
679
= std::filesystem::copy_options::overwrite_existing;
663
680
// Copy and remove file if we are not in build currently
664
- if (std::filesystem::current_path (). filename () != " build " ) {
665
- std::filesystem::copy (cwd, build_dir , copyOptions);
681
+ if (std::filesystem::current_path () != m_build_dir ) {
682
+ std::filesystem::copy (cwd, m_build_dir , copyOptions);
666
683
std::filesystem::remove (cwd);
667
684
}
668
685
return true ;
@@ -671,24 +688,11 @@ namespace cbdc::generate_config {
671
688
}
672
689
}
673
690
674
- // This method assumes project root is "opencbdc-tx" and build dir is
675
- // "build"
676
691
void config_generator::copy_templates_to_build_dir () {
677
- std::filesystem::path config_dir = std::filesystem::current_path ();
678
- std::filesystem::path build_dir = std::filesystem::current_path ();
679
- while (config_dir.has_parent_path ()) {
680
- if (config_dir.filename () == " opencbdc-tx" ) {
681
- config_dir = config_dir.append (" config" );
682
- config_dir = config_dir.append (" tools" );
683
- build_dir = build_dir.append (" build" );
684
- build_dir = build_dir.append (" config" );
685
- build_dir = build_dir.append (" tools" );
686
- break ;
687
- } else {
688
- config_dir = config_dir.parent_path ();
689
- build_dir = build_dir.parent_path ();
690
- }
691
- }
692
+ std::filesystem::path config_dir = m_project_root_dir;
693
+ config_dir.append (" config" ).append (" tools" );
694
+ std::filesystem::path build_config_dir = m_build_dir;
695
+ build_config_dir.append (" config" ).append (" tools" );
692
696
for (auto const & dir_entry :
693
697
std::filesystem::directory_iterator{config_dir}) {
694
698
std::string filename = dir_entry.path ().filename ();
@@ -697,7 +701,11 @@ namespace cbdc::generate_config {
697
701
if (tmp_str == match_str) {
698
702
const auto copyOptions
699
703
= std::filesystem::copy_options::overwrite_existing;
700
- std::filesystem::copy (dir_entry, build_dir, copyOptions);
704
+ std::filesystem::copy (dir_entry,
705
+ build_config_dir,
706
+ copyOptions);
707
+ std::cout << " Copying " << dir_entry.path ().string () << " to "
708
+ << build_config_dir.string () << std::endl;
701
709
}
702
710
}
703
711
}
@@ -728,8 +736,13 @@ namespace cbdc::generate_config {
728
736
std::string output_filename = " tmp.cfg" ;
729
737
730
738
if (!template_file_is_valid) {
739
+ std::filesystem::path temp_build_dir = m_build_dir;
740
+ temp_build_dir.append (" config" ).append (" tools" );
731
741
return_msg += " File provided, " + m_template_config_file
732
- + " , does not exist. Aborting operation. \n " ;
742
+ + " , did not exist but has likely been copied to "
743
+ + temp_build_dir.string ()
744
+ + " . Aborting operation. Please rerun with proper "
745
+ " template location \n " ;
733
746
return return_msg;
734
747
}
735
748
std::map<std::string, std::string> config_map;
@@ -757,8 +770,7 @@ namespace cbdc::generate_config {
757
770
config_map_it->first ,
758
771
std::get<std::string>(parsed_val));
759
772
} else {
760
- return_msg += " Warning: Unrecognized type for param, "
761
- + config_map_it->first + " . \n " ;
773
+ __builtin_unreachable ();
762
774
}
763
775
}
764
776
}
@@ -794,8 +806,7 @@ namespace cbdc::generate_config {
794
806
return_msg
795
807
+= " Warning: Two-phase mode requires at least one "
796
808
" configured shard. Fix configuration template "
797
- " and "
798
- " rerun.\n " ;
809
+ " and rerun.\n " ;
799
810
return return_msg;
800
811
} else {
801
812
create_2pc_component (shard_count_key,
@@ -853,8 +864,7 @@ namespace cbdc::generate_config {
853
864
return_msg
854
865
+= " Warning: Sentinels require at least one "
855
866
" configured shard. Fix configuration template "
856
- " and "
857
- " rerun. \n " ;
867
+ " and rerun. \n " ;
858
868
return return_msg;
859
869
} else {
860
870
create_atomizer_component (
0 commit comments