Skip to content

Commit

Permalink
Refactor Irohad:
Browse files Browse the repository at this point in the history
- Create class fields for threads and services
- Move thread joins to destructor
- Set irohad output path to /bin
  • Loading branch information
lebdron committed Aug 4, 2017
1 parent e8e213b commit fb54299
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
2 changes: 2 additions & 0 deletions irohad/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

add_library(server_runner server_runner.cpp)
target_link_libraries(server_runner
torii_service
Expand Down
23 changes: 14 additions & 9 deletions irohad/main/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ Irohad::Irohad(const std::string &block_store_dir,
pg_conn)),
peer_number_(peer_number) {}

Irohad::~Irohad() {
torii_server->shutdown();
internal_thread.join();
server_thread.join();
loop_thread.join();
}

class MockBlockLoader : public iroha::network::BlockLoader {
public:
MOCK_METHOD2(requestBlocks,
Expand Down Expand Up @@ -134,7 +141,7 @@ void Irohad::run() {
// --- Transactions:
auto tx_processor = createTransactionProcessor(pcs, stateless_validator);

auto comand_service = createCommandService(pb_tx_factory, tx_processor);
command_service = createCommandService(pb_tx_factory, tx_processor);

// --- Queries
auto query_proccessing_factory =
Expand All @@ -143,7 +150,7 @@ void Irohad::run() {
auto query_processor = createQueryProcessor(
std::move(query_proccessing_factory), stateless_validator);

auto query_service = createQueryService(
query_service = createQueryService(
pb_query_factory, pb_query_response_factory, query_processor);

grpc::ServerBuilder builder;
Expand All @@ -154,14 +161,12 @@ void Irohad::run() {
builder.RegisterService(ordering_init.ordering_service.get());
builder.RegisterService(yac_init.consensus_network.get());
internal_server = builder.BuildAndStart();
std::thread internal_thread([this] { internal_server->Wait(); });
std::thread server_thread([this] {
torii_server->run(std::move(comand_service), std::move(query_service));
internal_thread = std::thread([this] { internal_server->Wait(); });
server_thread = std::thread([this] {
torii_server->run(std::move(command_service), std::move(query_service));
});
std::thread loop_thread([this] { loop->run(); });
internal_thread.join();
server_thread.join();
loop_thread.join();
loop_thread = std::thread([this] { loop->run(); });
torii_server->waitForServersReady();
}

std::shared_ptr<Simulator> Irohad::createSimulator(
Expand Down
6 changes: 6 additions & 0 deletions irohad/main/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Irohad {
size_t redis_port, const std::string &pg_conn, size_t torii_port,
uint64_t peer_number);
void run();
~Irohad();

private:
std::shared_ptr<iroha::synchronizer::Synchronizer> initializeSynchronizer(
Expand Down Expand Up @@ -109,11 +110,16 @@ class Irohad {
size_t torii_port_;
std::shared_ptr<uvw::Loop> loop;

std::unique_ptr<::torii::CommandService> command_service;
std::unique_ptr<::torii::QueryService> query_service;

std::unique_ptr<ServerRunner> torii_server;
std::unique_ptr<grpc::Server> internal_server;
iroha::network::OrderingInit ordering_init;
iroha::consensus::yac::YacInit yac_init;

std::thread internal_thread, server_thread, loop_thread;

public:
std::shared_ptr<iroha::ametsuchi::StorageImpl> storage;
uint64_t peer_number_;
Expand Down
11 changes: 5 additions & 6 deletions irohad/main/irohad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,11 @@ int main(int argc, char *argv[]) {
gflags::ShutDownCommandLineFlags();

auto config = parse_iroha_config(FLAGS_config);
auto irohad =
Irohad(config[mbr::BlockStorePath].GetString(),
config[mbr::RedisHost].GetString(),
config[mbr::RedisPort].GetUint(),
config[mbr::PgOpt].GetString(),
config[mbr::ToriiPort].GetUint(), FLAGS_peer_number);
Irohad irohad(config[mbr::BlockStorePath].GetString(),
config[mbr::RedisHost].GetString(),
config[mbr::RedisPort].GetUint(),
config[mbr::PgOpt].GetString(),
config[mbr::ToriiPort].GetUint(), FLAGS_peer_number);

iroha::main::BlockInserter inserter(irohad.storage);
auto file = inserter.loadFile(FLAGS_genesis_block);
Expand Down

0 comments on commit fb54299

Please sign in to comment.