-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: pikiwidb support multi-raft cluster #442
Changes from 15 commits
7bfe16a
62415c0
450c1f2
e5bd390
5042a4d
a44050d
9977040
425d2c5
c6f100c
0420265
bad54e1
ff2f9db
5245b11
d242a2d
5dff21e
f1a1048
f9287eb
dfee101
4277387
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2023-present, OpenAtom Foundation, Inc. All rights reserved. | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#include "pd_service.h" | ||
|
||
int main(int argc, char* argv[]) { | ||
brpc::Server server; | ||
|
||
PlacementDriverServiceImpl service; | ||
if (server.AddService(&service, brpc::SERVER_OWNS_SERVICE) != 0) { | ||
fprintf(stderr, "Fail to add service!\n"); | ||
return -1; | ||
} | ||
longfar-ncy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 启动服务 | ||
if (server.Start(8080, nullptr) != 0) { | ||
longfar-ncy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fprintf(stderr, "Fail to start server!\n"); | ||
return -1; | ||
} | ||
|
||
server.RunUntilAskedToQuit(); | ||
return 0; | ||
} | ||
longfar-ncy marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,84 @@ | ||||||
syntax="proto3"; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 修正语法错误:在syntax声明中添加空格 Protocol Buffers的语法声明需要在等号两侧添加空格。正确的写法是 修复建议: -syntax="proto3";
+syntax = "proto3"; 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 格式修改下 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dingxiaoshuai123,感谢您修改格式! ^(๑˃̵ᴗ˂̵)و/ If you found this review helpful, would you consider giving us a shout-out on X? |
||||||
package pikiwidb; | ||||||
option cc_generic_services = true; | ||||||
|
||||||
import "store.proto"; | ||||||
|
||||||
message GetClusterInfoRequest { | ||||||
}; | ||||||
|
||||||
message GetClusterInfoResponse { | ||||||
bool success = 1; | ||||||
repeated Store store = 2; | ||||||
}; | ||||||
|
||||||
message Store { | ||||||
int64 store_id = 1; | ||||||
string ip = 2; | ||||||
int32 port = 3; | ||||||
StoreState state = 4; | ||||||
repeated Region region = 5; | ||||||
}; | ||||||
|
||||||
message Region { | ||||||
int64 region_id = 1; | ||||||
optional string start_key = 2; | ||||||
optional string end_key = 3; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 移除不必要的 在proto3中,标量类型默认是可选的,无需使用 修复建议: -optional string start_key = 2;
-optional string end_key = 3;
+string start_key = 2;
+string end_key = 3; -optional int64 store_id = 2;
-optional string redirect = 3;
+int64 store_id = 2;
+string redirect = 3; Also applies to: 66-67 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. proto3 中确实默认了很多关键字,按照兔子的改法改一下吧 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dingxiaoshuai123,好的,请按照建议进行修改。 (_/) If you found this review helpful, would you consider giving us a shout-out on X? |
||||||
repeated RegionEpoch region_epoch = 4; | ||||||
repeated Peer peers = 5; | ||||||
}; | ||||||
|
||||||
message RegionEpoch { | ||||||
int64 conf_change_ver = 1; // conf change version | ||||||
int64 region_ver = 2; // region version (split or merge) | ||||||
}; | ||||||
|
||||||
enum StoreState { | ||||||
UP = 0; | ||||||
OFFLINE = 1; | ||||||
TOMBSTONE = 2; | ||||||
}; | ||||||
|
||||||
message CreateAllRegionsRequest { | ||||||
int64 regions_count = 1; | ||||||
int32 region_peers_count = 2; | ||||||
repeated RegionOptions regionOptions = 3; | ||||||
longfar-ncy marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
}; | ||||||
|
||||||
message CreateAllRegionsResponse { | ||||||
bool success = 1; | ||||||
}; | ||||||
|
||||||
message DeleteAllRegionsRequest { | ||||||
}; | ||||||
|
||||||
message DeleteAllRegionsResponse { | ||||||
bool success = 1; | ||||||
}; | ||||||
|
||||||
message AddStoreRequest { | ||||||
string ip = 1; | ||||||
int32 port = 2; | ||||||
}; | ||||||
|
||||||
message AddStoreResponse { | ||||||
bool success = 1; | ||||||
optional int64 store_id = 2; | ||||||
optional string redirect = 3; | ||||||
}; | ||||||
|
||||||
message RemoveStoreRequest { | ||||||
int64 store_id = 1; | ||||||
}; | ||||||
|
||||||
message RemoveStoreResponse { | ||||||
bool success = 1; | ||||||
}; | ||||||
|
||||||
service PlacementDriverService { | ||||||
rpc CreateAllRegions(CreateAllRegionsRequest) returns (CreateAllRegionsResponse); | ||||||
rpc DeleteAllRegions(DeleteAllRegionsRequest) returns (DeleteAllRegionsResponse); | ||||||
rpc AddStore(AddStoreRequest) returns (AddStoreResponse); | ||||||
rpc RemoveStore(RemoveStoreRequest) returns (RemoveStoreResponse); | ||||||
rpc GetClusterInfo(GetClusterInfoRequest) returns (GetClusterInfoResponse); | ||||||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2023-present, OpenAtom Foundation, Inc. All rights reserved. | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#include "pd_service.h" | ||
|
||
#include "pd_server.h" | ||
|
||
namespace pikiwidb { | ||
void PlacementDriverServiceImpl::CreateAllRegions(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::CreateAllRegionsRequest* request, | ||
::pikiwidb::CreateAllRegionsResponse* response, | ||
::google::protobuf::Closure* done) {} | ||
|
||
void PlacementDriverServiceImpl::DeleteAllRegions(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::DeleteAllRegionsRequest* request, | ||
::pikiwidb::DeleteAllRegionsResponse* response, | ||
::google::protobuf::Closure* done) {} | ||
|
||
void PlacementDriverServiceImpl::AddStore(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::AddStoreRequest* request, | ||
::pikiwidb::AddStoreResponse* response, ::google::protobuf::Closure* done) { | ||
brpc::ClosureGuard done_guard(done); | ||
auto [success, store_id] = PDSERVER.AddStore(request->ip(), request->port()); | ||
if (!success) { | ||
response->set_success(false); | ||
return; | ||
} | ||
|
||
response->set_success(true); | ||
response->set_store_id(store_id); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion 在 为了提高系统的健壮性和安全性,建议在处理请求之前验证传入的 在 为了方便故障排查和系统监控,建议在成功添加 Store 后记录相关信息,例如新添加的 |
||
|
||
void PlacementDriverServiceImpl::RemoveStore(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::RemoveStoreRequest* request, | ||
::pikiwidb::RemoveStoreResponse* response, | ||
::google::protobuf::Closure* done) {} | ||
|
||
void PlacementDriverServiceImpl::GetClusterInfo(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::GetClusterInfoRequest* request, | ||
::pikiwidb::GetClusterInfoResponse* response, | ||
::google::protobuf::Closure* done) { | ||
brpc::ClosureGuard done_guard(done); | ||
PDSERVER.GetClusterInfo(response); | ||
} | ||
|
||
void PlacementDriverServiceImpl::OpenPDScheduling(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::OpenPDSchedulingRequest* request, | ||
::pikiwidb::OpenPDSchedulingResponse* response, | ||
::google::protobuf::Closure* done) {} | ||
|
||
void PlacementDriverServiceImpl::ClosePDScheduling(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::ClosePDSchedulingRequest* request, | ||
::pikiwidb::ClosePDSchedulingResponse* response, | ||
::google::protobuf::Closure* done) {} | ||
} // namespace pikiwidb |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) 2023-present, OpenAtom Foundation, Inc. All rights reserved. | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
Comment on lines
+1
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 请更新版权声明的年份和组织名称 文件顶部的版权声明可能需要更新,以反映当前的年份和正确的组织名称。 |
||
|
||
#pragma once | ||
|
||
#include "pd.pb.h" | ||
|
||
namespace pikiwidb { | ||
|
||
class PlacementDriverServiceImpl : public PlacementDriverService { | ||
public: | ||
PlacementDriverServiceImpl() = default; | ||
|
||
void CreateAllRegions(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::CreateAllRegionsRequest* request, | ||
::pikiwidb::CreateAllRegionsResponse* response, ::google::protobuf::Closure* done) override; | ||
|
||
void DeleteAllRegions(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::DeleteAllRegionsRequest* request, | ||
::pikiwidb::DeleteAllRegionsResponse* response, ::google::protobuf::Closure* done) override; | ||
|
||
void AddStore(::google::protobuf::RpcController* controller, const ::pikiwidb::AddStoreRequest* request, | ||
::pikiwidb::AddStoreResponse* response, ::google::protobuf::Closure* done) override; | ||
|
||
void RemoveStore(::google::protobuf::RpcController* controller, const ::pikiwidb::RemoveStoreRequest* request, | ||
::pikiwidb::RemoveStoreResponse* response, ::google::protobuf::Closure* done) override; | ||
|
||
void GetClusterInfo(::google::protobuf::RpcController* controller, const ::pikiwidb::GetClusterInfoRequest* request, | ||
::pikiwidb::GetClusterInfoResponse* response, ::google::protobuf::Closure* done) override; | ||
|
||
void OpenPDScheduling(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::OpenPDSchedulingRequest* request, | ||
::pikiwidb::OpenPDSchedulingResponse* response, ::google::protobuf::Closure* done) override; | ||
|
||
void ClosePDScheduling(::google::protobuf::RpcController* controller, | ||
const ::pikiwidb::ClosePDSchedulingRequest* request, | ||
::pikiwidb::ClosePDSchedulingResponse* response, ::google::protobuf::Closure* done) override; | ||
}; | ||
|
||
} // namespace pikiwidb |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright (c) 2023-present, OpenAtom Foundation, Inc. All rights reserved. | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#include "proxy_service.h" | ||
|
||
int main(int argc, char* argv[]) { | ||
brpc::Server server; | ||
|
||
ProxyServiceImpl service; | ||
if (server.AddService(&service, brpc::SERVER_OWNS_SERVICE) != 0) { | ||
fprintf(stderr, "Fail to add service!\n"); | ||
return -1; | ||
} | ||
|
||
// 启动服务 | ||
if (server.Start(8080, nullptr) != 0) { | ||
fprintf(stderr, "Fail to start server!\n"); | ||
return -1; | ||
} | ||
longfar-ncy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
server.RunUntilAskedToQuit(); | ||
return 0; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
syntax = "proto3"; | ||
package pikiwidb.proxy; | ||
option cc_generic_services = true; | ||
|
||
// 定义请求和响应 | ||
message RunCommandRequest { | ||
string command = 1; | ||
} | ||
|
||
message RunCommandResponse { | ||
string output = 1; | ||
} | ||
message GetRouteInfoRequest { | ||
} | ||
message GetRouteInfoResponse { | ||
message RouteInfo { | ||
string group_id = 1; | ||
string endpoint = 2; | ||
int32 role = 3; | ||
} | ||
repeated RouteInfo infos = 1; | ||
} | ||
|
||
// 定义服务 | ||
longfar-ncy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
service ProxyService { | ||
rpc RunCommand(RunCommandRequest) returns (RunCommandResponse); | ||
rpc GetRouteInfo(GetRouteInfoRequest) returns (GetRouteInfoResponse); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright (c) 2023-present, OpenAtom Foundation, Inc. All rights reserved. | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#include "proxy_service.h" | ||
|
||
namespace pikiwidb::proxy { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification 缺少必要的标准库头文件 在 #include <string>
#include <array>
#include <memory> 🔗 Analysis chain验证头文件包含是否完整 文件头部包含了版权信息和 请运行以下脚本来验证是否需要额外的头文件: 根据脚本结果,可能需要添加以下头文件: #include <string>
#include <array>
#include <memory> 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# 描述:检查是否需要额外的头文件
# 测试:搜索 proxy_service.h 文件中的头文件包含
echo "Checking proxy_service.h for includes:"
rg '#include' pproxy/proxy_service.h
# 测试:搜索当前文件中使用的 STL 类型
echo "\nChecking for STL types usage in proxy_service.cc:"
rg '\b(string|array|unique_ptr)\b' pproxy/proxy_service.cc
Length of output: 628 |
||
void ProxyServiceImpl::RunCommand(::google::protobuf::RpcController* cntl, | ||
const pikiwidb::proxy::RunCommandRequest* request, | ||
pikiwidb::proxy::RunCommandResponse* response, ::google::protobuf::Closure* done) { | ||
std::string command = request->command(); | ||
std::string output = ExecuteCommand(command); | ||
|
||
response->set_output(output); | ||
|
||
done->Run(); | ||
} | ||
longfar-ncy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
void ProxyServiceImpl::GetRouteINfo(::google::protobuf::RpcController* cntl, | ||
const pikiwidb::proxy::GetRouteInfoRequest* request, | ||
pikiwidb::proxy::GetRouteInfoResponse* response, | ||
::google::protobuf::Closure* done) { | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 修复 GetRouteInfo 方法的拼写错误并实现功能 当前 GetRouteInfo 方法存在以下问题:
请进行以下修改:
示例修复: void ProxyServiceImpl::GetRouteInfo(::google::protobuf::RpcController* cntl,
const pikiwidb::proxy::GetRouteInfoRequest* request,
pikiwidb::proxy::GetRouteInfoResponse* response,
::google::protobuf::Closure* done) {
// TODO: 实现获取路由信息的逻辑
// 示例:
// auto route_info = GetCurrentRouteInfo();
// response->set_route_info(route_info);
done->Run();
} 请确保实现获取实际路由信息的逻辑。 |
||
|
||
std::string ProxyServiceImpl::ExecuteCommand(const std::string& command) { | ||
std::array<char, 128> buffer; | ||
std::string result; | ||
|
||
// 使用 popen 执行命令 | ||
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.c_str(), "r"), pclose); | ||
if (!pipe) { | ||
return "popen() failed!"; | ||
} | ||
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { | ||
result += buffer.data(); | ||
} | ||
return result; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 改进 ExecuteCommand 方法的安全性和健壮性 ExecuteCommand 方法存在以下问题:
建议进行以下改进:
示例改进: std::string ProxyServiceImpl::ExecuteCommand(const std::string& command) {
if (!IsCommandAllowed(command)) {
return "Command not allowed";
}
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(command.c_str(), "r"), pclose);
if (!pipe) {
return "Failed to execute command";
}
while (true) {
if (fgets(buffer.data(), buffer.size(), pipe.get()) == nullptr) {
if (feof(pipe.get())) {
break; // 正常结束
} else {
return "Error reading command output";
}
}
result += buffer.data();
}
return result;
} 请确保实现 |
||
|
||
} // namespace pikiwidb::proxy |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2023-present, OpenAtom Foundation, Inc. All rights reserved. | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "proxy.pb.h" | ||
|
||
class ProxyServiceImpl : public ProxyService { | ||
public: | ||
void RunCommand(::google::protobuf::RpcController* cntl, const pikiwidb::proxy::RunCommandRequest* request, | ||
pikiwidb::proxy::RunCommandResponse* response, ::google::protobuf::Closure* done) override; | ||
void GetRouteInfo(::google::protobuf::RpcController* cntl, const pikiwidb::proxy::GetRouteInfoRequest* request, | ||
pikiwidb::proxy::GetRouteInfoResponse* response, ::google::protobuf::Closure* done) override; | ||
|
||
private: | ||
std::string ExecuteCommand(const std::string& command); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
数据库数量的重大变更
数据库数量从16减少到2是一个显著的变化。
请考虑以下几点:
您能否解释一下为什么选择减少到2个数据库?这个决定是基于什么考虑做出的?