Skip to content

Commit 0e76301

Browse files
authored
add transport initialize to support service discovery for mpc (#84)
* add transport initialize to support service discovery for mpc * optimize log * grpc disable port reuse
1 parent ba4c67b commit 0e76301

File tree

12 files changed

+131
-27
lines changed

12 files changed

+131
-27
lines changed

cpp/ppc-framework/protocol/GrpcConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class GrpcConfig
106106
// the max send message size in bytes
107107
uint64_t m_maxSendMessageSize = 1024 * 1024 * 1024;
108108
// the max received message size in bytes
109-
uint16_t m_maxReceivedMessageSize = 1024 * 1024 * 1024;
109+
uint64_t m_maxReceivedMessageSize = 1024 * 1024 * 1024;
110110
int m_compressAlgorithm = 0;
111111
};
112112

cpp/tools/build_wedpr_mpc.sh

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ generate_config_ini() {
326326
local rpc_listen_port="${3}"
327327
local agency_info="${4}"
328328
local agency_id="${5}"
329+
local grpc_listen_ip="${6}"
330+
local grpc_listen_port="${7}"
331+
local nodeid="${8}"
329332

330333
cat <<EOF >"${output}"
331334
[agency]
@@ -364,6 +367,19 @@ generate_config_ini() {
364367
; the connection-timeout, in ms, default is 1000ms
365368
connection-timeout = 2000
366369
370+
[transport]
371+
; the endpoint information
372+
listen_ip = ${grpc_listen_ip}
373+
listen_port = ${grpc_listen_port}
374+
host_ip =
375+
; the threadPoolSize
376+
thread_count = 4
377+
; the gatewayService endpoint information
378+
gateway_target =
379+
; the components
380+
components =
381+
nodeid=${nodeid}
382+
367383
[cert]
368384
; directory the certificates located in
369385
cert_path=./conf
@@ -584,6 +600,22 @@ gen_rsa_node_cert() {
584600
LOG_INFO "Generate ${ndpath} cert successful!"
585601
}
586602

603+
# we use sm_param to generate the private key
604+
generate_private_key() {
605+
local output_path="${1}"
606+
if [ ! -d "${output_path}" ]; then
607+
mkdir -p ${output_path}
608+
fi
609+
if [ ! -f ${sm2_params} ]; then
610+
generate_sm_sm2_param ${sm2_params}
611+
fi
612+
${OPENSSL_CMD} genpkey -paramfile ${sm2_params} -out ${output_path}/node.pem 2>/dev/null
613+
$OPENSSL_CMD ec -in "$output_path/node.pem" -text -noout 2> /dev/null | sed -n '3,5p' | sed 's/://g' | tr "\n" " " | sed 's/ //g' | cat > "$output_path/node.privateKey"
614+
${OPENSSL_CMD} ec -text -noout -in "${output_path}/node.pem" 2>/dev/null | sed -n '7,11p' | tr -d ": \n" | awk '{print substr($0,3);}' | cat >"$output_path"/node.nodeid
615+
private_key=$(cat $output_path/node.privateKey)
616+
echo ${private_key}
617+
}
618+
587619
gen_sm_node_cert_with_ext() {
588620
local capath="$1"
589621
local certpath="$2"
@@ -686,7 +718,11 @@ deploy_nodes()
686718
# generate the config.ini
687719
local rpc_port=5894
688720
local agency_id="agency${count}"
689-
generate_config_ini "${output_dir}/config.ini" "${listen_ip}" "${rpc_port}" "${agency_info}" ${agency_id}
721+
local grpc_port=18100
722+
# the nodeid
723+
private_key=$(generate_private_key "${output_dir}/conf")
724+
node_id=$(cat "${output_dir}/conf/node.nodeid")
725+
generate_config_ini "${output_dir}/config.ini" "${listen_ip}" "${rpc_port}" "${agency_info}" ${agency_id} "${listen_ip}" "${grpc_port}" "${node_id}"
690726
print_result
691727
}
692728

cpp/wedpr-computing/ppc-mpc/src/MPCService.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ void MPCService::execCommand(const std::string cmd, int& outExitStatus, std::str
420420
}
421421
catch (const std::exception& e)
422422
{
423+
MPC_LOG(WARNING) << LOG_DESC("[MPCService] execCommand failed") << LOG_KV("cmd", cmd);
423424
BOOST_THROW_EXCEPTION(
424425
RunMpcFailException() << errinfo_comment(
425426
"invalid params:" + std::string(boost::diagnostic_information(e))));

cpp/wedpr-helper/ppc-tools/src/config/PPCConfig.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,14 @@ class PPCConfig
248248
return m_frontConfig->selfEndPoint().host() + ":" + std::to_string(m_rpcConfig.listenPort);
249249
}
250250

251-
private:
252-
virtual void loadEndpointConfig(ppc::protocol::EndPoint& endPoint, bool requireHostIp,
253-
std::string const& sectionName, boost::property_tree::ptree const& pt);
254251
// load the front config
255252
virtual void loadFrontConfig(bool requireTransport,
256253
ppc::front::FrontConfigBuilder::Ptr const& frontConfigBuilder,
257254
boost::property_tree::ptree const& pt);
255+
256+
private:
257+
virtual void loadEndpointConfig(ppc::protocol::EndPoint& endPoint, bool requireHostIp,
258+
std::string const& sectionName, boost::property_tree::ptree const& pt);
258259
// load the grpc config
259260
ppc::protocol::GrpcConfig::Ptr loadGrpcConfig(
260261
std::string const& sectionName, boost::property_tree::ptree const& pt);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
aux_source_directory(. SRC_LIST)
22

33
add_executable(${MPC_BINARY_NAME} ${SRC_LIST})
4-
target_link_libraries(${MPC_BINARY_NAME} PUBLIC ${MPC_TARGET} ${RPC_TARGET} ${HELPER_TARGET} TBB::tbb)
4+
target_link_libraries(${MPC_BINARY_NAME} PUBLIC ${MPC_TARGET} ${RPC_TARGET} ${HELPER_TARGET} ${WEDPR_TRANSPORT_SDK_TARGET} TBB::tbb)

cpp/wedpr-main/mpc-node/MPCInitializer.cpp

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@
1818
* @date 2023-03-24
1919
*/
2020
#include "MPCInitializer.h"
21+
#include "ppc-framework/front/FrontConfig.h"
22+
#include "ppc-framework/protocol/ServiceType.h"
2123
#include "ppc-mpc/src/MPCService.h"
2224
#include "ppc-tools/src/config/PPCConfig.h"
25+
#include "wedpr-protocol/protocol/src/ServiceConfig.h"
26+
#include "wedpr-transport/sdk/src/TransportBuilder.h"
2327

2428
using namespace ppc::rpc;
2529
using namespace bcos;
30+
using namespace ppc::sdk;
2631
using namespace ppc::mpc;
2732
using namespace ppc::tools;
33+
using namespace ppc::front;
34+
using namespace ppc::protocol;
35+
36+
MPCInitializer::MPCInitializer() : m_transportBuilder(std::make_shared<TransportBuilder>()) {}
2837

29-
/// TODO: mpc support the gateway
3038
void MPCInitializer::init(std::string const& _configPath)
3139
{
3240
// init the log
@@ -40,28 +48,54 @@ void MPCInitializer::init(std::string const& _configPath)
4048
// init the rpc
4149
INIT_LOG(INFO) << LOG_DESC("init the rpc");
4250
// load the rpc config
43-
auto ppcConfig = std::make_shared<PPCConfig>();
44-
ppcConfig->loadRpcConfig(pt);
45-
ppcConfig->loadMPCConfig(pt);
51+
m_config = std::make_shared<PPCConfig>();
52+
m_config->loadRpcConfig(pt);
53+
m_config->loadMPCConfig(pt);
4654
// bool useMysql = pt.get<bool>("mpc.use_mysql", false);
47-
auto storageConfig = ppcConfig->storageConfig();
48-
auto mpcConfig = ppcConfig->mpcConfig();
49-
auto rpcFactory = std::make_shared<RpcFactory>(ppcConfig->agencyID());
50-
m_rpc = rpcFactory->buildRpc(ppcConfig, nullptr);
55+
auto storageConfig = m_config->storageConfig();
56+
auto mpcConfig = m_config->mpcConfig();
57+
auto rpcFactory = std::make_shared<RpcFactory>(m_config->agencyID());
58+
m_rpc = rpcFactory->buildRpc(m_config, nullptr);
5159
auto mpcService = std::make_shared<MPCService>();
5260
mpcService->setMPCConfig(mpcConfig);
5361
mpcService->setStorageConfig(storageConfig);
5462
m_rpc->registerHandler("run", std::bind(&MPCService::runMpcRpc, mpcService,
5563
std::placeholders::_1, std::placeholders::_2));
5664
m_rpc->registerHandler("kill", std::bind(&MPCService::killMpcRpc, mpcService,
5765
std::placeholders::_1, std::placeholders::_2));
66+
INIT_LOG(INFO) << LOG_DESC("init the mpc rpc success");
67+
// init the transport
68+
initTransport(pt);
69+
}
5870

71+
void MPCInitializer::initTransport(boost::property_tree::ptree const& property)
72+
{
73+
INIT_LOG(INFO) << LOG_DESC("initTransport: load front config");
74+
m_config->loadFrontConfig(true, m_transportBuilder->frontConfigBuilder(), property);
75+
INIT_LOG(INFO) << LOG_DESC("initTransport: load front config success");
5976

60-
INIT_LOG(INFO) << LOG_DESC("init the mpc rpc success");
77+
// add the service meta
78+
ServiceConfigBuilder serviceConfigBuilder;
79+
auto entryPoint =
80+
serviceConfigBuilder.buildEntryPoint(MPC_SERVICE_TYPE, m_config->accessEntrypoint());
81+
auto serviceConfig = serviceConfigBuilder.buildServiceConfig();
82+
serviceConfig.addEntryPoint(entryPoint);
83+
auto serviceMeta = serviceConfig.encode();
84+
m_config->frontConfig()->setMeta(serviceMeta);
85+
INIT_LOG(INFO) << LOG_DESC("initTransport: register serviceMeta")
86+
<< LOG_KV("serviceMeta", serviceMeta);
87+
INIT_LOG(INFO) << LOG_DESC("initTransport: buildProTransport");
88+
m_transport = m_transportBuilder->buildProTransport(m_config->frontConfig());
89+
INIT_LOG(INFO) << LOG_DESC("initTransport: buildProTransport success");
6190
}
6291

6392
void MPCInitializer::start()
6493
{
94+
// start the transport
95+
if (m_transport)
96+
{
97+
m_transport->start();
98+
}
6599
// start the ppc mpc
66100
if (m_rpc)
67101
{
@@ -75,4 +109,8 @@ void MPCInitializer::stop()
75109
{
76110
m_rpc->stop();
77111
}
112+
if (m_transport)
113+
{
114+
m_transport->stop();
115+
}
78116
}

cpp/wedpr-main/mpc-node/MPCInitializer.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,36 @@ namespace ppc::rpc
2626
{
2727
class Rpc;
2828
}
29+
namespace ppc::tools
30+
{
31+
class PPCConfig;
32+
}
33+
namespace ppc::sdk
34+
{
35+
class TransportBuilder;
36+
class Transport;
37+
}; // namespace ppc::sdk
2938
namespace ppc::mpc
3039
{
3140
class MPCInitializer
3241
{
3342
public:
3443
using Ptr = std::shared_ptr<MPCInitializer>;
35-
MPCInitializer() {}
44+
MPCInitializer();
3645
virtual ~MPCInitializer() { stop(); }
3746

3847
virtual void init(std::string const& _configPath);
3948
virtual void start();
4049
virtual void stop();
4150

51+
protected:
52+
virtual void initTransport(boost::property_tree::ptree const& property);
53+
4254
private:
55+
std::shared_ptr<ppc::tools::PPCConfig> m_config;
56+
std::shared_ptr<ppc::sdk::TransportBuilder> m_transportBuilder;
57+
std::shared_ptr<ppc::sdk::Transport> m_transport;
58+
4359
bcos::BoostLogInitializer::Ptr m_logInitializer;
4460
std::shared_ptr<ppc::rpc::Rpc> m_rpc;
4561
};

cpp/wedpr-protocol/grpc/server/GrpcServer.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ void GrpcServer::start()
3939
}
4040
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
4141
grpc::ServerBuilder builder;
42+
// disable port reuse
43+
builder.AddChannelArgument(GRPC_ARG_ALLOW_REUSEPORT, 0);
4244
// without authentication
4345
builder.AddListeningPort(m_config->listenEndPoint(), grpc::InsecureServerCredentials());
4446
// register the service

cpp/wedpr-transport/ppc-front/ppc-front/FrontImpl.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ void FrontImpl::start()
5656
return;
5757
}
5858
m_running = true;
59+
if (m_nodeDiscovery)
60+
{
61+
m_nodeDiscovery->start();
62+
}
5963
m_thread = std::make_shared<std::thread>([&] {
6064
bcos::pthread_setThreadName("front_io_service");
6165
while (m_running)
@@ -77,10 +81,7 @@ void FrontImpl::start()
7781
}
7882
FRONT_LOG(INFO) << "Front exit";
7983
});
80-
if (m_nodeDiscovery)
81-
{
82-
m_nodeDiscovery->start();
83-
}
84+
FRONT_LOG(INFO) << LOG_DESC("start front success");
8485
}
8586

8687

@@ -97,6 +98,10 @@ void FrontImpl::stop()
9798
return;
9899
}
99100
m_running = false;
101+
if (m_nodeDiscovery)
102+
{
103+
m_nodeDiscovery->stop();
104+
}
100105
if (m_ioService)
101106
{
102107
m_ioService->stop();
@@ -113,10 +118,7 @@ void FrontImpl::stop()
113118
m_thread->detach();
114119
}
115120
}
116-
if (m_nodeDiscovery)
117-
{
118-
m_nodeDiscovery->stop();
119-
}
121+
FRONT_LOG(INFO) << LOG_DESC("stop front success");
120122
}
121123

122124
void FrontImpl::asyncSendResponse(bcos::bytesConstRef dstNode, std::string const& traceID,

cpp/wedpr-transport/ppc-front/ppc-front/NodeDiscovery.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ NodeDiscovery::NodeDiscovery(ppc::gateway::IGateway::Ptr gatewayClient)
2727
{
2828
// fetch the meta information every 10s
2929
m_metaFetcher = std::make_shared<bcos::Timer>(10 * 1000, "metaFetcher");
30-
m_metaFetcher->registerTimeoutHandler([this]() { fetchMetaInfoFromGateway(); });
3130
}
3231

3332
std::vector<ppc::protocol::INodeInfo::Ptr> NodeDiscovery::getAliveNodeList() const
@@ -38,6 +37,15 @@ std::vector<ppc::protocol::INodeInfo::Ptr> NodeDiscovery::getAliveNodeList() con
3837

3938
void NodeDiscovery::start()
4039
{
40+
auto self = weak_from_this();
41+
m_metaFetcher->registerTimeoutHandler([self]() {
42+
auto fetcher = self.lock();
43+
if (!fetcher)
44+
{
45+
return;
46+
}
47+
fetcher->fetchMetaInfoFromGateway();
48+
});
4149
if (m_metaFetcher)
4250
{
4351
m_metaFetcher->start();

0 commit comments

Comments
 (0)