Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Add service endpoint for remote daemon shutdown #2138

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion example/config.docker
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
"mst_enable" : false,
"mst_expiration_time" : 1440,
"max_rounds_delay": 3000,
"stale_stream_max_rounds": 2
"stale_stream_max_rounds": 2,
"utility_service_port": 60001,
"shutdown_secret_key": "stopbombing"
}
4 changes: 3 additions & 1 deletion example/config.sample
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"mst_enable" : false,
"mst_expiration_time" : 1440,
"max_rounds_delay": 3000,
"stale_stream_max_rounds": 2
"stale_stream_max_rounds": 2,
"utility_service_port": 60001,
"shutdown_secret_key": "stopbombing"
}

1 change: 1 addition & 0 deletions irohad/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ add_subdirectory(simulator)
add_subdirectory(synchronizer)
add_subdirectory(multi_sig_transactions)
add_subdirectory(pending_txs_storage)
add_subdirectory(util)
1 change: 1 addition & 0 deletions irohad/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ target_link_libraries(irohad
rapidjson
keys_manager
common
irohad_utility
)

add_install_step_for_bin(irohad)
21 changes: 21 additions & 0 deletions irohad/main/iroha_conf_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace config_members {
const char *MstExpirationTime = "mst_expiration_time";
const char *MaxRoundsDelay = "max_rounds_delay";
const char *StaleStreamMaxRounds = "stale_stream_max_rounds";
const char *UtilityServicePort = "utility_service_port";
const char *ShutdownSecretKey = "shutdown_secret_key";
} // namespace config_members

static constexpr size_t kBadJsonPrintLength = 15;
Expand Down Expand Up @@ -114,6 +116,8 @@ inline rapidjson::Document parse_iroha_config(const std::string &conf_path) {
const auto kMaxRoundsDelayDefault = 3000u;
const auto kStaleStreamMaxRoundsDefault = 2u;
const auto kMstExpirationTimeDefault = 1440u;
const auto kUtilityServicePortDefault = 60001u;
const auto kShutdownSecretKeyDefault = "stopbombing";

if (not doc.HasMember(mbr::MstExpirationTime)) {
rapidjson::Value key(mbr::MstExpirationTime, allocator);
Expand All @@ -139,6 +143,23 @@ inline rapidjson::Document parse_iroha_config(const std::string &conf_path) {
ac::type_error(mbr::StaleStreamMaxRounds, kUintType));
}

if (not doc.HasMember(mbr::UtilityServicePort)) {
rapidjson::Value key(mbr::UtilityServicePort, allocator);
doc.AddMember(key, kUtilityServicePortDefault, allocator);
} else {
ac::assert_fatal(doc[mbr::UtilityServicePort].IsUint(),
ac::type_error(mbr::UtilityServicePort, kUintType));
}

if (not doc.HasMember(mbr::ShutdownSecretKey)) {
rapidjson::Value key(mbr::ShutdownSecretKey, allocator);
rapidjson::Value value(kShutdownSecretKeyDefault, allocator);
doc.AddMember(key, value, allocator);
} else {
ac::assert_fatal(doc[mbr::ShutdownSecretKey].IsString(),
ac::type_error(mbr::ShutdownSecretKey, kStrType));
}

return doc;
}

Expand Down
33 changes: 33 additions & 0 deletions irohad/main/irohad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
#include <gflags/gflags.h>
#include <grpc++/grpc++.h>
#include "ametsuchi/storage.hpp"
#include "common/bind.hpp"
#include "common/result.hpp"
#include "crypto/keys_manager_impl.hpp"
#include "main/application.hpp"
#include "main/iroha_conf_loader.hpp"
#include "main/raw_block_loader.hpp"
#include "main/server_runner.hpp"
#include "util/utility.hpp"

static const std::string kListenIp = "0.0.0.0";

Expand Down Expand Up @@ -84,6 +87,34 @@ DEFINE_int32(verbosity, spdlog::level::info, "Log verbosity");
DEFINE_validator(verbosity, validateVerbosity);

std::promise<void> exit_requested;
std::shared_ptr<iroha::service::UtilityService> utility_service;
std::unique_ptr<ServerRunner> utility_server;

void initShutdownService(const rapidjson::Document &config) {
namespace mbr = config_members;
using iroha::operator|;
using RunResult = iroha::expected::Result<void, std::string>;
auto log_ = logger::log("IrohaUtilityServer");

utility_service = std::make_shared<iroha::service::UtilityService>(
config[mbr::ShutdownSecretKey].GetString(), exit_requested);
utility_server = std::make_unique<ServerRunner>(
kListenIp + ":"
+ std::to_string(config[mbr::UtilityServicePort].GetUint()),
false,
log_);
utility_server->append(utility_service)
.run()
.match(
[&](const auto &port) -> RunResult {
log_->info("Utility server bound on port {}", port.value);
return {};
},
[&](const iroha::expected::Error<std::string> &e) -> RunResult {
log_->error(e.error);
return e;
});
}

int main(int argc, char *argv[]) {
// Parsing command line arguments
Expand Down Expand Up @@ -118,6 +149,8 @@ int main(int argc, char *argv[]) {
return EXIT_FAILURE;
}

initShutdownService(config);

// Configuring iroha daemon
Irohad irohad(
config[mbr::BlockStorePath].GetString(),
Expand Down
9 changes: 9 additions & 0 deletions irohad/util/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# Copyright Soramitsu Co., Ltd. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
#

add_library(irohad_utility utility.cpp)
target_link_libraries(irohad_utility
utility_endpoint
)
24 changes: 24 additions & 0 deletions irohad/util/utility.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include "util/utility.hpp"

namespace iroha {
namespace service {
UtilityService::UtilityService(const std::string &shutdown_key,
std::promise<void> &shutdown_promise)
: kShutdownKey(shutdown_key), kShutdownPromise(shutdown_promise) {}

grpc::Status UtilityService::Shutdown(
::grpc::ServerContext *context,
const ::iroha::protocol::ShutdownRequest *request,
::google::protobuf::Empty *response) {
if (request->shutdown_key() == kShutdownKey) {
kShutdownPromise.set_value();
}
return ::grpc::Status::OK;
}
} // namespace service
} // namespace iroha
32 changes: 32 additions & 0 deletions irohad/util/utility.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef IROHA_UTILITY_HPP
#define IROHA_UTILITY_HPP

#include <future>

#include "utility_endpoint.grpc.pb.h"
#include "utility_endpoint.pb.h"

namespace iroha {
namespace service {

class UtilityService : public iroha::protocol::UtilityService_v1::Service {
public:
UtilityService(const std::string &shutdown_key,
std::promise<void> &shutdown_promise);

grpc::Status Shutdown(grpc::ServerContext *context,
const iroha::protocol::ShutdownRequest *request,
google::protobuf::Empty *response);

private:
const std::string kShutdownKey;
std::promise<void> &kShutdownPromise;
};
} // namespace service
} // namespace iroha

#endif // IROHA_UTILITY_HPP
13 changes: 13 additions & 0 deletions schema/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ compile_proto_to_grpc_cpp(yac.proto)
compile_proto_to_grpc_cpp(ordering.proto "-I${SM_SCHEMA_PATH}")
compile_proto_to_grpc_cpp(loader.proto "-I${SM_SCHEMA_PATH}")
compile_proto_to_grpc_cpp(mst.proto "-I${SM_SCHEMA_PATH}")
compile_proto_to_grpc_cpp(utility_endpoint.proto)

add_library(endpoint
endpoint.grpc.pb.cc
Expand All @@ -18,6 +19,18 @@ target_link_libraries(endpoint
schema
)

add_library(utility_endpoint
utility_endpoint.pb.cc
utility_endpoint.grpc.pb.cc
)
target_link_libraries(utility_endpoint
protobuf
grpc++
)
target_include_directories(utility_endpoint PUBLIC
${SCHEMA_OUT_DIR}
)

add_library(yac_grpc
yac.pb.cc
yac.grpc.pb.cc
Expand Down
18 changes: 18 additions & 0 deletions schema/utility_endpoint.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

syntax = "proto3";

package iroha.protocol;

import "google/protobuf/empty.proto";

message ShutdownRequest {
string shutdown_key = 1;
}

service UtilityService_v1 {
rpc Shutdown (ShutdownRequest) returns (google.protobuf.Empty);
}