Skip to content

Commit

Permalink
Have basics of a configuration generation tool for expanded testing. …
Browse files Browse the repository at this point in the history
…I foresee much more expanded functionality in the future and a lot of potential. This will require some possible rework of some existing code, however, as well as integration tests to make use of this proposed functionality. Also reorganized config files into their own directory which seems neater. Also needed to fix docker stff. Might need to revert docker stuff. Not sure how that will affect others.

Signed-off-by: Yiannis Karavas <[email protected]>
  • Loading branch information
ykaravas committed Jun 22, 2022
1 parent 0d7b5cf commit 4837cc6
Show file tree
Hide file tree
Showing 15 changed files with 1,134 additions and 500 deletions.
5 changes: 5 additions & 0 deletions config/tools/2pc_config_template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,8 @@ tmpl_randomize_values=1
tmpl_shard_start=0
tmpl_shard_size=255
tmpl_avg_shard_start_end_overlap_percent=0.15
tmpl_default_log_level="INFO"
tmpl_universal_override_log_level="WARN"
tmpl_sentinel_log_level="WARN"
tmpl_coordinator_log_level="DEBUG"
tmpl_shard_log_level="DEBUG"
9 changes: 5 additions & 4 deletions config/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
project(config_gen)

include_directories(../../src ../../3rdparty/secp256k1/include)
include_directories(../../src ../../config/tools ../../3rdparty/secp256k1/include)

set(SECP256K1_LIBRARY $<TARGET_FILE:secp256k1>)
add_library(config_generator config_generator.cpp)

add_executable(generate_config config_generator.cpp)
add_executable(generate_config generate_configd.cpp)

target_link_libraries(generate_config util
target_link_libraries(generate_config config_generator
util
network
common
crypto
Expand Down
7 changes: 7 additions & 0 deletions config/tools/atomizer_config_template.tmpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2pc=0
archiver_count=1
atomizer_count=1
shard_count=1
Expand All @@ -23,3 +24,9 @@ tmpl_randomize_values=1
tmpl_shard_start=0
tmpl_shard_size=255
tmpl_avg_shard_start_end_overlap_percent=0.15
tmpl_default_log_level="INFO"
tmpl_sentinel_log_level="DEBUG"
tmpl_shard_log_level="INFO"
tmpl_watchtower_log_level="DEBUG"
tmpl_archiver_log_level="DEBUG"
tmpl_atomizer_log_level="DEBUG"
1,293 changes: 808 additions & 485 deletions config/tools/config_generator.cpp

Large diffs are not rendered by default.

149 changes: 149 additions & 0 deletions config/tools/config_generator.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright (c) 2022 MIT Digital Currency Initiative,
// Federal Reserve Bank of Boston
// MITRE Corporation
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef OPENCBDC_TX_CONFIG_TOOLS_CONFIG_GENERATOR_H_
#define OPENCBDC_TX_CONFIG_TOOLS_CONFIG_GENERATOR_H_

#include "util/common/config.hpp"
#include "util/common/random_source.hpp"

#include <random>
#include <secp256k1.h>

#define MAX_PORT_NUM 65535

// Structure to assist in creating shard id coverage
using ShardInfo = struct shard_info_struct {
std::vector<size_t> coverage;
size_t shard_id;
size_t numbers_covered;
double overlap_percentage_allowed;
bool still_expanding;
bool allow_overlap;
std::pair<size_t, size_t> current_coverage_expansion_limits;
};

using value_t = std::variant<std::string, size_t, double>;

namespace cbdc::generate_config {
/// \brief Config_generator implementation.
///
/// This class takes in a template configuration file, denoted by the
/// ending "*.tmpl" and produces are usable (*.cfg) configuration file. The
/// purpose of this class is to allow the user to create more complicated
/// testing scenarios by removing some amount of manual effort when
/// creating configurations.
class config_generator {
public:
/// Constructor.
///
/// \param _template_config_file The template configuration file from which
/// the larger more intricate configuration file will be generated.
/// \param _start_port Port to begin using and incrementing from for generated
/// configuration file's endpoints
config_generator(std::string& _template_config_file,
size_t _start_port);

~config_generator() = default;

/// \brief generate_configuration_file
/// Main workhorse method of this class. This method will generate a
/// usable configuration file
///
/// \return a string with all the error/warning/success messages, if any,
/// that were produced while executing
[[maybe_unused]] auto generate_configuration_file() -> std::string;

private:
// Boolean that tells us if file is valid or not
bool template_file_is_valid;
// Template file loaded to create configuration file from
std::string& m_template_config_file;
// Incrementing port to use in config file for all ports
unsigned short m_current_port;
// Map with shard ranges (shard_id, (start range, end_range)
std::vector<ShardInfo> shard_info;
std::default_random_engine generator;
static const inline auto m_random_source
= std::make_unique<cbdc::random_source>(
cbdc::config::random_source);
static const inline auto m_secp
= std::unique_ptr<secp256k1_context,
decltype(&secp256k1_context_destroy)>(
secp256k1_context_create(SECP256K1_CONTEXT_SIGN),
&secp256k1_context_destroy);

// Where the newly created configuration parameters will be stored as
// we go along generating them
std::stringstream m_new_config;

std::map<std::string, value_t> template_options;

// Calculate shard coverage
void calculate_shard_coverage(size_t num_shards,
bool randomize,
size_t shard_size);
// Helper for calculating shard id coverage
void shard_bookkeeping(const std::vector<size_t>& array_total,
size_t shard_id);
// Get random value based on mean and standard deviation
[[nodiscard]] auto calculate_normal_distribution_point(size_t mean,
double std_dev,
bool randomize)
-> double;
// This is not a failsafe because we are simply generating a
// configuration file here, however, it is still good to check that the
// port is available
[[nodiscard]] auto get_open_port() -> unsigned short;
// Create Private/Public key pair repeatably (repeatable from run to
// run)
[[nodiscard]] auto create_repeatable_key_pair()
-> std::pair<std::string, std::string>;
// Create Private/Public key pair randomly (NOT repeatable from run to
// run)
[[nodiscard]] auto create_random_key_pair()
-> std::pair<std::string, std::string>;
// Create Private/Public key pair
[[nodiscard]] auto create_key_pair(bool randomize)
-> std::pair<std::string, std::string>;
// Parse value as int, double or string from config file
[[nodiscard]] auto parse_value(const std::string& value,
bool keep_quotes) -> value_t;
[[nodiscard]] auto get_param_from_template_file(
std::string option,
std::map<std::string, std::string>& config_map)
-> std::variant<size_t, double, std::string>;
void set_param_to_config_file(std::string key, std::string value);
void set_param_to_config_file(std::string key, size_t value);
void set_param_to_config_file(std::string key, double value);
// Helper to set proper log level for either Two-Phase Commit or
// Atomizer components
void set_log_level(std::string key, std::string& log_level);
// Method to create all the Two-Phase Commit related components for
// generated config file
void create_2pc_component(const char* type, size_t number);
// Method to create all the Atomizer related components for generated
// config file
void create_atomizer_component(const char* type, size_t number);
// Load template file from which we will generate the configuration
// file.
void load_template(std::string filename,
std::map<std::string, std::string>& config_map);
// Write out the generated configuration file out to
// <project_root>/build/config/tools
void write_generated_config_to_file(const std::string& _config_file);
// Copies the generated *.cfg file to the <project_root>/build
// directory
[[nodiscard]] auto copy_to_build_dir(std::string filename) -> bool;
// Copies the *.tmpl file to the <project_root>/build/config/tools
// directory
void copy_templates_to_build_dir();
template<typename T>
[[maybe_unused]] auto find_value(std::string key, T& output) -> bool;
};
}

#endif // OPENCBDC_TX_CONFIG_TOOLS_CONFIG_GENERATOR_H_
37 changes: 37 additions & 0 deletions config/tools/generate_configd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "config_generator.hpp"

#include <iostream>
#include <random>

auto main(int argc, char** argv) -> int {
auto args = cbdc::config::get_args(argc, argv);

std::mt19937 rng;
static const auto m_random_source
= std::make_unique<cbdc::random_source>(cbdc::config::random_source);

if(args.size() < 3) {
std::cerr << "Usage: " << args[0]
<< " <config template file> <starting port number to "
"increment from>"
<< std::endl;
return 0;
}

auto port_is_valid = std::isdigit(*args[2].c_str());
if(!port_is_valid) {
std::cerr << "Port number provided, " << args[2]
<< ", is not a valid number. Exiting..." << std::endl;
return 0;
}
size_t port_num = static_cast<size_t>(std::stoull(args[2]));
if(port_num > MAX_PORT_NUM) {
std::cerr << "Port number provided, " << args[2]
<< ", is too large. Exiting..." << std::endl;
return 0;
}
cbdc::generate_config::config_generator new_config_gen(args[1], port_num);
auto cfg_or_err = new_config_gen.generate_configuration_file();
std::cout << cfg_or_err << std::endl;
return 0;
}
27 changes: 27 additions & 0 deletions config/unit/2pc_template_unit_test.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
2pc=1
sentinel_count=1
shard_count=1
coordinator_count=1
coordinator_max_threads=1
election_timeout_upper=4000
election_timeout_lower=3000
heartbeat=1000
raft_max_batch=100000
snapshot_distance=1000000000
batch_size=1
wait_for_followers=0
loadgen_invalid_tx_rate=0.00
loadgen_fixed_tx_rate=0.01
loadgen_sendtx_output_count=1
loadgen_sendtx_input_count=1
initial_mint_count=20000
initial_mint_value=100
tmpl_randomize_values=1
tmpl_shard_start=0
tmpl_shard_size=255
tmpl_avg_shard_start_end_overlap_percent=0.15
tmpl_default_log_level="INFO"
tmpl_universal_override_log_level="WARN"
tmpl_sentinel_log_level="WARN"
tmpl_coordinator_log_level="DEBUG"
tmpl_shard_log_level="DEBUG"
32 changes: 32 additions & 0 deletions config/unit/atomizer_template_unit_test.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
2pc=0
archiver_count=1
atomizer_count=1
shard_count=1
sentinel_count=1
watchtower_count=1
target_block_interval=3000
stxo_cache_depth=2
target_block_interval=250
election_timeout_upper=4000
election_timeout_lower=3000
heartbeat=1000
raft_max_batch=100000
snapshot_distance=1000000000
batch_size=1
wait_for_followers=0
loadgen_invalid_tx_rate=0.00
loadgen_fixed_tx_rate=0.01
loadgen_sendtx_output_count=1
loadgen_sendtx_input_count=1
initial_mint_count=20000
initial_mint_value=100
tmpl_randomize_values=1
tmpl_shard_start=0
tmpl_shard_size=255
tmpl_avg_shard_start_end_overlap_percent=0.15
tmpl_default_log_level="INFO"
tmpl_sentinel_log_level="DEBUG"
tmpl_shard_log_level="INFO"
tmpl_watchtower_log_level="DEBUG"
tmpl_archiver_log_level="DEBUG"
tmpl_atomizer_log_level="DEBUG"
6 changes: 3 additions & 3 deletions docker-compose-2pc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ services:
image: opencbdc-tx
tty: true
restart: always
command: ./build/src/uhs/twophase/sentinel_2pc/sentineld-2pc 2pc-compose.cfg 0
command: ./build/src/uhs/twophase/sentinel_2pc/sentineld-2pc ./config/general/2pc-compose.cfg 0
ports:
- 5555:5555
depends_on:
Expand All @@ -25,7 +25,7 @@ services:
build: .
image: opencbdc-tx
tty: true
command: ./build/src/uhs/twophase/coordinator/coordinatord 2pc-compose.cfg 0 0
command: ./build/src/uhs/twophase/coordinator/coordinatord ./config/general/2pc-compose.cfg 0 0
expose:
- "7777"
depends_on:
Expand All @@ -43,7 +43,7 @@ services:
build: .
image: opencbdc-tx
tty: true
command: ./build/src/uhs/twophase/locking_shard/locking-shardd 2pc-compose.cfg 0 0
command: ./build/src/uhs/twophase/locking_shard/locking-shardd ./config/general/2pc-compose.cfg 0 0
expose:
- "6666"
ports:
Expand Down
10 changes: 5 additions & 5 deletions docker-compose-atomizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
build: .
image: opencbdc-tx
tty: true
command: ./build/src/uhs/atomizer/watchtower/watchtowerd atomizer-compose.cfg 0
command: ./build/src/uhs/atomizer/watchtower/watchtowerd ./config/general/atomizer-compose.cfg 0
ports:
- 8555:8555
expose:
Expand All @@ -23,7 +23,7 @@ services:
build: .
image: opencbdc-tx
tty: true
command: ./build/src/uhs/atomizer/atomizer/atomizer-raftd atomizer-compose.cfg 0
command: ./build/src/uhs/atomizer/atomizer/atomizer-raftd ./config/general/atomizer-compose.cfg 0
expose:
- "5555"
depends_on:
Expand All @@ -40,7 +40,7 @@ services:
build: .
image: opencbdc-tx
tty: true
command: ./build/src/uhs/atomizer/archiver/archiverd atomizer-compose.cfg 0
command: ./build/src/uhs/atomizer/archiver/archiverd ./config/general/atomizer-compose.cfg 0
expose:
- "4555"
depends_on:
Expand All @@ -59,7 +59,7 @@ services:
build: .
image: opencbdc-tx
tty: true
command: ./build/src/uhs/atomizer/shard/shardd atomizer-compose.cfg 0
command: ./build/src/uhs/atomizer/shard/shardd ./config/general/atomizer-compose.cfg 0
expose:
- "6555"
depends_on:
Expand All @@ -79,7 +79,7 @@ services:
build: .
image: opencbdc-tx
tty: true
command: ./build/src/uhs/atomizer/sentinel/sentineld atomizer-compose.cfg 0
command: ./build/src/uhs/atomizer/sentinel/sentineld ./config/general/atomizer-compose.cfg 0
ports:
- 7555:7555
depends_on:
Expand Down
2 changes: 1 addition & 1 deletion docker-compose-test-2pc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ services:
build: .
image: opencbdc-tx
tty: true
command: sh ./scripts/test-transaction.sh 2pc-compose.cfg
command: sh ./scripts/test-transaction.sh ./config/general/2pc-compose.cfg
networks:
- 2pc-network
3 changes: 2 additions & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ run_test_suite () {
}

echo "Running unit tests..."
run_test_suite "tests/unit/run_unit_tests" "unit_tests_coverage"
#run_test_suite "tests/unit/run_unit_tests" "unit_tests_coverage"

echo "Running integration tests..."
cp config/integration/*.cfg $BUILD_DIR
cp config/tools/*.tmpl $BUILD_DIR"/config/tools"
run_test_suite "tests/integration/run_integration_tests" "integration_tests_coverage"
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(tests)
find_package(GTest REQUIRED)
include(../3rdparty/FindGMock.cmake)
find_package(Threads)
include_directories(. ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS} ../src ../tools/watchtower ../3rdparty ../3rdparty/secp256k1/include)
include_directories(. ${GTEST_INCLUDE_DIRS} ${GMOCK_INCLUDE_DIRS} ../src ../ ../config/tools ../tools/watchtower ../3rdparty ../3rdparty/secp256k1/include)
set(SECP256K1_LIBRARY $<TARGET_FILE:secp256k1>)

add_library(util util.cpp)
Expand Down
Loading

0 comments on commit 4837cc6

Please sign in to comment.