Skip to content
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: support pd #395

Closed

Conversation

panlei-coder
Copy link
Collaborator

@panlei-coder panlei-coder commented Jul 30, 2024

1、进展:在单 raft 的基础之上支持的 pd 模块,pd 按照独立部署的形式设计(混合部署等后续功能差不多了再支持),pd 和 普通节点都默认使用的是 PRAFT 和 db 0,目前能够完成 pd 初始化为一个单 raft group 之后,普通节点启动之后会主动上报注册(需要传入 pd 的成员配置信息)。
2、后续安排:
(1)把 multi-raft 的代码合并进来,完善整体的功能。
(2)目前 pd 文件夹下的 pd.proto 文件和 src 文件夹下的 store.proto 文件的 cmake 自动生成有点问题,还没来得及解决,需要大家帮忙一起看一下,在代码调试的时候我是直接手动生成的:
protoc --proto_path=./src --proto_path=./src/pd --cpp_out=./cmake-build-debug/generated_pd/ pd.proto
protoc --proto_path=./src --cpp_out=./cmake-build-debug/generated_pd/ store.proto
(3)添加测试,目前只是简单验证了正确性。
3、测试:
pd节点:默认端口为 7777,pikiwidb.conf 中 as-pd 设置为 yes
普通节点:默认端口为8888,pikiwidb.conf 中 as-pd 设置为 no
先启动 pd 节点,然后再启动普通节点。

Summary by CodeRabbit

  • 新功能

    • 引入了对RAFT共识算法的支持,改进了分布式系统的配置。
    • 添加了用于管理存储和区域统计的Protocol Buffers定义,提供了新的RPC服务接口。
    • 创建了PlacementDriverServer,用于管理分布式数据库中的存储和区域标识符。
  • 修复

    • 加强了命令执行逻辑中的错误处理与控制流。
  • 文档

    • 更新了相关配置文件,以增强系统的功能性和可配置性。
  • 重构

    • 改进了PStore类的初始化和后端管理功能,以提高与Placement Driver服务的交互能力。

@github-actions github-actions bot added the ✏️ Feature New feature or request label Jul 30, 2024
Copy link

coderabbitai bot commented Jul 30, 2024

Walkthrough

此次更新对项目的构建配置和服务架构进行了显著改进。新增了对协议缓冲区(Protocol Buffers)的支持,增强了数据库管理功能,并引入了新的放置驱动服务,使得分布式数据库系统的操作更加高效和灵活。

Changes

文件 变更摘要
CMakeLists.txt, src/CMakeLists.txt, src/pd/CMakeLists.txt 添加了对新的子目录和协议缓冲区文件的支持,创建了新的静态库,并为可执行目标添加了依赖关系。
pikiwidb.conf 启用了RAFT共识算法,并调整了相关参数以增强配置功能。
src/base_cmd.cc, src/base_cmd.h 改进了命令执行逻辑,增强了错误处理和流控制。
src/cmd_raft.cc, src/cmd_raft.h 引入了数据库初始化和后端管理的新功能,增加了客户端相关的包含指令。
src/config.cc, src/config.h 添加了多个配置参数以支持新的“pd”功能,增强了PConfig类的可配置性。
src/db.cc, src/db.h 修改了数据库标识符的命名,并添加了一个新的初始化方法,增强了代码的清晰度和一致性。
src/pd/*.proto, src/pd/pd_server.cc, src/pd/pd_service.cc, src/pd/pd_service.h 新增了放置驱动服务及其相关的方法,提供对存储和区域的管理。
src/store.cc, src/store.h, src/store.proto, src/store_service.cc, src/store_service.h 引入了新的存储和区域统计管理功能,通过RPC方法提供更好的服务架构。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant StoreService
    participant PlacementDriverService

    User->>StoreService: 请求获取存储统计
    StoreService->>PlacementDriverService: 调用GetStoreStats
    PlacementDriverService-->>StoreService: 返回存储统计
    StoreService-->>User: 返回统计信息
Loading

Poem

🐇 在代码的世界里跳跃,
新服务如花儿绽放,
统计数据轻松拿,
构建更快不再忙。
协议缓冲真方便,
兔子欢快乐无边! 🌼


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 19

Outside diff range, codebase verification and nitpick comments (5)
src/praft/praft_service.h (1)

16-17: 确认删除对PRaft的依赖

构造函数已修改为默认构造函数,不再接受PRaft指针。请确保此更改不会影响其他依赖PRaft实例的组件。特别是,src/praft/praft.cc 中仍然使用旧的构造函数,这可能会导致代码破裂。请更新 src/praft/praft.cc 中的构造函数使用。

  • src/praft/praft.cc:
    DummyServiceImpl service(&PRAFT);
Analysis chain

确认删除对PRaft的依赖

构造函数已修改为默认构造函数,不再接受PRaft指针。请确保此更改不会影响其他依赖PRaft实例的组件。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify that the removal of `PRaft` dependency does not break other components.

# Test: Search for the usage of `DummyServiceImpl` constructor. Expect: No broken dependencies.
rg --type cpp -A 5 $'DummyServiceImpl'

Length of output: 1725

src/db.h (1)

57-57: 确认新成员变量praft_

已添加新私有成员变量praft_,类型为std::unique_ptr<PRaft>。然而,src/db.cc中有关于praft_初始化和使用的代码被注释掉了。请确保这些代码被正确地取消注释并经过测试,以确保praft_的正确初始化和使用。

  • src/db.cc:
    // praft_ = std::make_unique<PRaft>();
    // butil::Status bs = praft_->Init(std::move(group_id), true);
    // if (!bs.ok()) {
    //   return pstd::Status::Error(bs.error_str());
    // }
Analysis chain

确认新成员变量praft_

已添加新私有成员变量praft_,类型为std::unique_ptr<PRaft>,请确保其实现符合类设计,并且在使用过程中不会引入潜在问题。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the new `praft_` member variable.

# Test: Search for the usage of `praft_` member variable. Expect: Proper usage and initialization.
rg --type cpp -A 5 $'praft_'

Length of output: 957

src/pd/pd_service.h (1)

1-6: 版权声明

请确保版权声明和许可证信息是最新的,并且与项目的实际情况一致。

src/praft/praft.cc (2)

注释掉的服务器初始化代码会显著影响服务器的生命周期管理,可能会引入问题。

以下是需要注意的代码位置:

  • src/store.cc
  • src/praft/praft.cc

请确保这些更改不会引入问题。

Analysis chain

Line range hint 92-118:
请验证注释掉的服务器初始化代码的影响。

注释掉的服务器初始化代码可能会影响服务器的生命周期管理,请确保这些更改不会引入问题。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the impact of commented-out server initialization code.

# Test: Search for the usage of `server_` in the codebase. Expect: No critical usage that depends on it.
rg --type cpp -A 5 $'server_'

Length of output: 7592


125-130: IP 转换失败的处理逻辑需要改进

在 IP 转换失败时,当前代码仅记录错误并返回状态,但没有执行任何其他的恢复或清理操作。建议在 str2ip 失败时添加必要的资源清理或服务器状态重置步骤。

  • src/praft/praft.cc 第 127 行附近: // server_.reset(); 注释掉的代码需要实现具体的错误处理逻辑。
Analysis chain

请验证 IP 转换失败的处理逻辑。

确保在 IP 转换失败时,系统能正确处理错误情况。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the handling of IP conversion failure.

# Test: Search for error handling after `str2ip` function. Expect: Proper error logging and handling.
rg --type cpp -A 5 $'str2ip'

Length of output: 362


Script:

#!/bin/bash
# Description: Verify the handling of IP conversion failure by checking the implementation and usage of server_.reset.

# Search for the implementation of server_.reset
rg --type cpp 'server_\.reset'

# Additionally, search for any other error handling related to str2ip
rg --type cpp -A 5 'str2ip'

Length of output: 881


Script:

#!/bin/bash
# Description: Verify the handling of IP conversion failure by checking the implementation and usage of ERROR_LOG_AND_STATUS.

# Search for the implementation of ERROR_LOG_AND_STATUS
rg --type cpp 'ERROR_LOG_AND_STATUS'

Length of output: 890


Script:

#!/bin/bash
# Description: Verify the handling of IP conversion failure by checking the implementation of ERROR_LOG_AND_STATUS.

# Search for the definition of ERROR_LOG_AND_STATUS in src/praft/praft.cc
rg --type cpp --context 5 '#define ERROR_LOG_AND_STATUS' src/praft/praft.cc

Length of output: 298

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 110fa8c and 8310ffc.

Files selected for processing (27)
  • CMakeLists.txt (1 hunks)
  • pikiwidb.conf (1 hunks)
  • src/CMakeLists.txt (4 hunks)
  • src/base_cmd.cc (2 hunks)
  • src/base_cmd.h (1 hunks)
  • src/cmd_raft.cc (2 hunks)
  • src/cmd_raft.h (1 hunks)
  • src/config.cc (1 hunks)
  • src/config.h (1 hunks)
  • src/db.cc (6 hunks)
  • src/db.h (2 hunks)
  • src/pd/CMakeLists.txt (1 hunks)
  • src/pd/pd.proto (1 hunks)
  • src/pd/pd_server.cc (1 hunks)
  • src/pd/pd_server.h (1 hunks)
  • src/pd/pd_service.cc (1 hunks)
  • src/pd/pd_service.h (1 hunks)
  • src/pikiwidb.cc (2 hunks)
  • src/praft/CMakeLists.txt (2 hunks)
  • src/praft/praft.cc (8 hunks)
  • src/praft/praft.h (3 hunks)
  • src/praft/praft_service.h (1 hunks)
  • src/store.cc (3 hunks)
  • src/store.h (2 hunks)
  • src/store.proto (1 hunks)
  • src/store_service.cc (1 hunks)
  • src/store_service.h (1 hunks)
Files skipped from review due to trivial changes (2)
  • src/base_cmd.h
  • src/praft/CMakeLists.txt
Additional comments not posted (127)
src/pd/CMakeLists.txt (2)

6-14: 确保pd.proto文件的路径正确

请确保pd.proto文件路径正确,并且在构建过程中可以成功生成pd.pb.cc文件。

Verification successful

确保pd.proto文件的路径正确

已确认pd.proto文件存在于src/pd/pd.proto路径下,路径正确。

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the existence and path of `pd.proto`.

# Test: Search for `pd.proto` file. Expect: File exists in the specified directory.
fd 'pd.proto' src/pd

Length of output: 36


15-42: 确认库配置

请确保库的配置和依赖项正确无误,特别是TARGET_INCLUDE_DIRECTORIESTARGET_LINK_LIBRARIES部分。

src/db.h (5)

56-56: 初始化成员变量storage_

私有成员变量storage_已初始化为nullptr,请确保此更改符合类设计,并且不会引入潜在问题。


47-47: 确认成员变量类型更改

私有成员变量db_index_已替换为db_id_,类型为int64_t,请确保此更改与数据库标识符的管理需求一致,并且不会导致其他部分的兼容性问题。

Verification successful

确认成员变量类型更改

私有成员变量db_index_已替换为db_id_,类型为int64_t,并且在多个文件和上下文中使用。所有使用都与新类型一致,请确保此更改与数据库标识符的管理需求一致,并且不会导致其他部分的兼容性问题。

  • src/db.h
  • src/db.cc
  • src/pd/pd_server.h
  • src/pd/pd_server.cc
  • src/praft/praft.h
  • src/praft/praft.cc
  • src/storage/src/storage.cc
  • src/storage/include/storage/storage.h
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the new `db_id_` member variable.

# Test: Search for the usage of `db_id_` member variable. Expect: All usages match the new type.
rg --type cpp -A 5 $'db_id_'

Length of output: 11552


26-26: 确认新方法Init

已添加新方法Init,请确保其实现符合类设计,并且在初始化过程中不会引入潜在问题。


44-44: 确认方法重命名

方法GetDbIndex已重命名为GetDBID,请确保此更改符合新的命名约定,并且在代码库中所有引用均已更新。

Verification successful

确认方法重命名

方法GetDbIndex已重命名为GetDBID,并且在代码库中没有发现对旧方法名的引用。所有引用均已更新。

  • src/db.h: 确认新方法名的定义。
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the usage of the renamed `GetDBID` method.

# Test: Search for the usage of `GetDBID` method. Expect: All references are updated to the new name.
rg --type cpp -A 5 $'GetDBID\('

Length of output: 228


Script:

#!/bin/bash
# Description: Verify the usage of the renamed `GetDBID` method and ensure no references to `GetDbIndex` remain.

# Test: Search for the old method name `GetDbIndex`. Expect: No references should be found.
rg --type cpp -A 5 $'GetDbIndex\('

# Test: Search for the new method name `GetDBID`. Expect: All references should be updated to the new name.
rg --type cpp -A 5 $'GetDBID\('

Length of output: 264


23-23: 确认构造函数参数类型更改

构造函数参数类型已从int更改为int64_t,请确保此更改与数据库标识符的管理需求一致,并且不会导致其他部分的兼容性问题。

src/store_service.h (1)

14-16: 类声明和构造函数看起来不错。

类声明和默认构造函数都符合标准。

src/cmd_raft.h (1)

13-13: 添加 client.h 头文件。

添加 client.h 头文件看起来是为了增强功能。请确保在此文件中使用了 client.h 中的内容。

src/pd/pd_service.h (10)

14-14: 类定义

PlacementDriverServiceImpl 类实现了 PlacementDriverService 接口,确保类的设计符合项目的架构要求。


16-16: 默认构造函数

默认构造函数定义正确。


18-21: 方法 CreateAllRegions

确保 CreateAllRegions 方法的实现符合业务逻辑需求,并且正确处理了所有可能的错误情况。


22-25: 方法 DeleteAllRegions

确保 DeleteAllRegions 方法的实现符合业务逻辑需求,并且正确处理了所有可能的错误情况。


26-27: 方法 AddStore

确保 AddStore 方法的实现符合业务逻辑需求,并且正确处理了所有可能的错误情况。


29-30: 方法 RemoveStore

确保 RemoveStore 方法的实现符合业务逻辑需求,并且正确处理了所有可能的错误情况。


32-33: 方法 GetClusterInfo

确保 GetClusterInfo 方法的实现符合业务逻辑需求,并且正确处理了所有可能的错误情况。


35-37: 方法 OpenPDScheduling

确保 OpenPDScheduling 方法的实现符合业务逻辑需求,并且正确处理了所有可能的错误情况。


39-41: 方法 ClosePDScheduling

确保 ClosePDScheduling 方法的实现符合业务逻辑需求,并且正确处理了所有可能的错误情况。


44-44: 命名空间结束

确保命名空间的使用符合项目的命名规范。

src/pd/pd.proto (23)

1-1: 语法版本

确保 proto3 语法版本符合项目需求。


2-2: 包名

包名 pikiwidb 符合项目命名规范。


3-3: 选项

cc_generic_services 选项设置为 true,确保符合项目需求。


5-5: 导入文件

确保 store.proto 文件存在并且定义正确。


7-8: 消息 GetClusterInfoRequest

空消息定义正确。


10-13: 消息 GetClusterInfoResponse

确保 GetClusterInfoResponse 消息定义符合业务逻辑需求。


15-21: 消息 Store

确保 Store 消息定义符合业务逻辑需求。


23-29: 消息 Region

确保 Region 消息定义符合业务逻辑需求。


31-34: 消息 RegionEpoch

确保 RegionEpoch 消息定义符合业务逻辑需求。


36-40: 枚举 StoreState

确保 StoreState 枚举定义符合业务逻辑需求。


42-46: 消息 CreateAllRegionsRequest

确保 CreateAllRegionsRequest 消息定义符合业务逻辑需求。


48-50: 消息 CreateAllRegionsResponse

确保 CreateAllRegionsResponse 消息定义符合业务逻辑需求。


52-53: 消息 DeleteAllRegionsRequest

空消息定义正确。


55-57: 消息 DeleteAllRegionsResponse

确保 DeleteAllRegionsResponse 消息定义符合业务逻辑需求。


59-62: 消息 AddStoreRequest

确保 AddStoreRequest 消息定义符合业务逻辑需求。


64-68: 消息 AddStoreResponse

确保 AddStoreResponse 消息定义符合业务逻辑需求。


70-72: 消息 RemoveStoreRequest

确保 RemoveStoreRequest 消息定义符合业务逻辑需求。


74-76: 消息 RemoveStoreResponse

确保 RemoveStoreResponse 消息定义符合业务逻辑需求。


78-79: 消息 OpenPDSchedulingRequest

空消息定义正确。


81-83: 消息 OpenPDSchedulingResponse

确保 OpenPDSchedulingResponse 消息定义符合业务逻辑需求。


85-86: 消息 ClosePDSchedulingRequest

空消息定义正确。


88-90: 消息 ClosePDSchedulingResponse

确保 ClosePDSchedulingResponse 消息定义符合业务逻辑需求。


92-106: 服务 PlacementDriverService

确保 PlacementDriverService 服务定义符合业务逻辑需求,并且所有 RPC 方法都已正确定义。

src/store.h (8)

12-14: 新增头文件

确保新增的头文件符合项目的依赖需求。


18-22: 新增头文件

确保新增的头文件符合项目的依赖需求。


54-54: 方法 Init

Init 方法不再需要 db_number 参数,确保初始化逻辑符合项目需求。


55-56: 新增方法 InitRpcServerRegisterStoreToPDServer

确保新增方法实现了正确的服务器初始化和注册逻辑。


58-60: 新增方法 SetStoreIDGetStoreID

确保新增方法实现了正确的线程安全的存储ID管理。


62-62: 修改方法 GetBackend

确保 GetBackend 方法的修改符合项目需求,并且正确处理了所有可能的错误情况。


64-64: 新增方法 AddBackend

确保新增方法实现了正确的后端添加逻辑。


71-81: 新增成员变量

确保新增的成员变量符合项目的设计需求,并且正确处理了所有可能的并发情况。

src/CMakeLists.txt (6)

6-13: 新增的自定义命令用于生成 Protocol Buffer 源文件

此自定义命令用于从 Protocol Buffer 定义文件生成 C++ 源文件,确保正确处理数据序列化和反序列化。


14-14: 创建静态库 store_pb

此更改确保生成的 Protocol Buffer 源文件被编译成静态库,以便与其他目标链接。


15-15: 设置库输出路径

此更改确保编译的库被放置在正确的目录中。


16-16: 更新 store_pb 目标的包含目录

此更改确保 store_pb 目标可以访问 Protocol Buffers 所需的头文件。


60-63: 更新 pikiwidb 目标的依赖项

此更改确保 pikiwidb 目标正确链接所需的库。


88-88: 更新 pikiwidb 目标的链接库

此更改确保 pikiwidb 目标在链接过程中包含 store_pb 库。

src/pd/pd_server.h (2)

30-44: PlacementDriverOptions 类定义良好

此类定义了 Placement Driver 的选项,构造函数和 getter 方法都很合适,使用 std::move 有效地初始化字符串成员。


56-79: PlacementDriverServer 类定义良好

此类定义了 Placement Driver Server,包含初始化、启动、停止及管理 store 和 region ID 的方法。使用单例模式进行实例管理。

src/pd/pd_service.cc (2)

23-35: AddStore 方法实现良好

此方法添加一个 store 并根据操作成功与否设置响应,错误处理得当。


42-48: GetClusterInfo 方法实现良好

此方法检索集群信息并设置响应,适当使用了 ClosureGuard

src/store.proto (19)

5-7: 定义正确

StoreStatsRequest消息的字段定义正确。


9-12: 定义正确

StoreStatsResponse消息的字段定义正确。


14-29: 定义正确

StoreStats消息的字段定义正确。


32-34: 定义正确

RegionStatsRequest消息的字段定义正确。


37-40: 定义正确

RegionStatsResponse消息的字段定义正确。


42-52: 定义正确

RegionStats消息的字段定义正确。


55-57: 定义正确

DownPeers消息的字段定义正确。


59-61: 定义正确

PendingPeers消息的字段定义正确。


63-68: 定义正确

Peer消息的字段定义正确。


70-75: 定义正确

RegionOptions消息的字段定义正确。


77-79: 定义正确

InitRegionPeerRequest消息的字段定义正确。


81-84: 定义正确

InitRegionPeerResponse消息的字段定义正确。


86-89: 定义正确

AddRegionPeerRequest消息的字段定义正确。


91-95: 定义正确

AddRegionResponse消息的字段定义正确。


97-99: 定义正确

RemoveRegionPeerRequest消息的字段定义正确。


102-106: 定义正确

RemoveRegionPeerResponse消息的字段定义正确。


108-110: 定义正确

TransferLeaderRequest消息的字段定义正确。


113-115: 定义正确

TransferLeaderResponse消息的字段定义正确。


117-129: 定义正确

StoreService服务的RPC方法定义正确。

src/base_cmd.cc (3)

53-54: 代码更改通过

代码更改通过,使用auto关键字使代码更简洁。


55-69: 添加了数据库后端检查

添加了数据库后端检查,确保数据库存在。如果数据库不存在,并且命令不是kCmdNameRaftCluster,则返回错误信息。


70-78: 修改了锁机制

修改了锁机制,确保在命令执行后释放共享锁,改进了资源管理。

src/db.cc (6)

19-19: 构造函数更改通过

构造函数更改通过,使用db_id代替db_index,符合新的命名约定。


21-21: 析构函数更改通过

析构函数更改通过,使用db_id记录数据库关闭日志,符合新的命名约定。


23-37: 方法定义正确

Init方法定义正确,尝试打开数据库并包含未来与PRaft集成的注释代码。


Line range hint 63-78:
方法定义正确

Open方法定义正确,使用db_id并改进日志记录,符合新的命名约定。


83-83: 方法定义正确

CreateCheckpoint方法定义正确,使用db_id,符合新的命名约定。


Line range hint 99-154:
方法定义正确

LoadDBFromCheckpoint方法定义正确,使用db_id并改进日志记录,符合新的命名约定。

src/praft/praft.h (3)

144-144: 请注意初始化逻辑的更改

IsInitialized 方法的逻辑已简化,现在仅检查 node_ 是否为空。这可能会影响 PRaft 类的初始化逻辑,导致即使服务器未设置,类也被视为已初始化。

请确保此更改不会在依赖此方法的代码部分引发问题。


169-169: 更改成员变量类型以支持更大范围的值

db_id_ 成员变量的类型从 int 更改为 int64_t,增加了可以容纳的值范围,这对于处理更大的标识符或数据库条目可能是必要的。


105-105: 更改方法签名以提高性能

group_id 参数类型从 std::string& 更改为 std::string&&,允许方法更高效地接受临时字符串对象,启用移动语义以减少不必要的字符串数据复制。

请确保所有对 Init 方法的调用都匹配新的签名。

Verification successful

更改方法签名以提高性能

group_id 参数类型从 std::string& 更改为 std::string&&,允许方法更高效地接受临时字符串对象,启用移动语义以减少不必要的字符串数据复制。所有对 Init 方法的调用都已匹配新的签名。

  • src/cmd_raft.cc 中的调用:PRAFT.Init(std::move(cluster_id), false);
  • src/praft/praft.cc 中的调用:PRAFT.Init(std::move(raft_group_id), true);
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `Init` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type cpp -A 5 $'Init'

Length of output: 100918


Script:

#!/bin/bash
# Description: Verify all function calls to `PRaft::Init` match the new signature.

# Search for the `Init` method implementation in the `PRaft` class.
ast-grep --lang cpp --pattern $'class PRaft {
  $$$
  butil::Status Init(std::string&& group_id, bool initial_conf_is_null);
  $$$
}' -A 10

# Search for calls to `PRaft::Init` to ensure they match the new signature.
rg --type cpp 'PRAFT\.Init\(' -A 3

Length of output: 685

src/config.h (5)

178-178: 添加新的原子布尔变量 as_pd

添加了新的原子布尔变量 as_pd,可能用于管理与新 PD 功能相关的特定标志。


179-179: 添加新的原子布尔变量 fake

添加了新的原子布尔变量 fake,可能用于管理与测试或模拟目的相关的特定标志。


180-180: 添加新的原子字符串变量 pd_group_id

添加了新的原子字符串变量 pd_group_id,可能用于存储新 PD 功能的组 ID。


181-181: 添加新的原子字符串变量 pd_conf

添加了新的原子字符串变量 pd_conf,可能用于存储新 PD 功能的配置详情。


182-182: 添加新的原子整数变量 request_timeout_ms

添加了新的原子整数变量 request_timeout_ms,默认值为 1000 毫秒,用于设置请求的超时时间,这是管理请求持续时间的关键。

src/pd/pd_server.cc (9)

20-23: 单例模式实现

Instance 方法实现了 PlacementDriverServer 类的单例模式,确保只创建一个类的实例。


25-28: 析构函数实现

PlacementDriverServer 的析构函数记录一条消息并调用 Stop 方法,确保对象销毁时服务器正确停止。


30-48: 初始化方法实现

Init 方法初始化 PD 的数据库并处理独立部署。该方法包括错误处理和日志记录,确保正确初始化并为手动配置提供警告。


51-56: 启动方法实现

Start 方法设置 is_started_ 标志,并包括一个 TODO 注释以供将来增强。目前,该方法仅设置标志,并为将来实现工作线程池预留了位置。

请注意 TODO 注释,未来需要实现集群节点探索和元信息收集功能。


58-66: 停止方法实现

Stop 方法重置 is_started_ 标志,并包括一个 TODO 注释以供将来增强。目前,该方法仅重置标志,并为将来实现相关线程池释放任务预留了位置。

请注意 TODO 注释,未来需要实现相关线程池释放任务。


68-101: 生成StoreID方法实现

GenerateStoreID 方法生成一个新的 store ID,确保原子性并处理错误。该方法包括锁机制、错误处理和日志记录,以确保正确的 ID 生成和存储。


103-136: 生成RegionID方法实现

GenerateRegionID 方法生成一个新的 region ID,确保原子性并处理错误。该方法包括锁机制、错误处理和日志记录,以确保正确的 ID 生成和存储。


138-156: 检查Store是否存在方法实现

CheckStoreExistByIP 方法通过 IP 地址检查 store 是否存在。该方法包括共享锁机制、错误处理和日志记录,以确保正确的存在性检查。


161-213: 添加Store方法实现

AddStore 方法添加一个新的 store,确保原子性并处理错误。该方法包括检查存在性、生成新的 store ID,并使用锁机制、错误处理和日志记录更新 store 信息的步骤。

CMakeLists.txt (1)

182-182: 添加新子目录

添加 src/pd 子目录以包含 PD 模块的构建配置。

该更改是构建配置的一部分,确保新模块 src/pd 被正确包含在项目中。

src/config.cc (1)

143-147: 添加 PD 配置参数

添加了与 PD 功能相关的配置参数,包括 as-pdpd-fakepd-group-idpd-confrequest-timeout-ms

这些更改增强了 PConfig 类的可配置性,允许用户在运行时指定与 PD 功能相关的参数。

src/pikiwidb.cc (2)

162-162: 请验证 PSTORE.Init() 的更改是否影响功能。

PSTORE.Init() 的调用从带参数改为无参数调用,可能会影响数据库配置的处理方式。请确保此更改不会对系统的整体功能产生负面影响。


285-285: 请确认注释掉 InitLimit() 的影响。

注释掉 InitLimit() 可能会导致应用程序在启动时的行为发生变化。请确保此更改不会对系统的资源管理和运行时行为产生负面影响。

src/store.cc (5)

37-59: 请验证 PStore::Init 方法的新逻辑。

此方法的重构移除了 db_number 参数,并引入了新的逻辑来设置 RPC 服务器并将存储注册到 PD 服务器。请确保这些更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。


63-109: 请验证 PStore::InitRpcServer 方法的新逻辑。

此方法确保根据节点的角色(PD 或标准存储)正确配置服务器,并包括设置过程中的错误处理。请确保这些更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。


111-183: 请验证 PStore::RegisterStoreToPDServer 方法的新逻辑。

此方法允许存储将其配置注册到 PD 服务器,包括领导者选择的重试和注册过程中的错误处理。请确保这些更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。


185-194: 请验证 PStore::GetBackend 方法的新逻辑。

此方法使用互斥锁来确保线程安全,并确保对后端数据库实例的访问是同步的。请确保这些更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。


196-205: 请验证 PStore::AddBackend 方法的新逻辑。

此方法使用互斥锁来确保线程安全,并确保对后端数据库实例的访问是同步的。请确保这些更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。

pikiwidb.conf (6)

346-346: 请验证启用 use-raft 的影响。

use-raft 选项从 no 更改为 yes,表示系统现在将使用 RAFT 共识算法。请确保此更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。


350-350: 请验证 request-timeout-ms 配置项的影响。

添加 request-timeout-ms 选项,值为 1000,可能设置请求的超时时间(以毫秒为单位)。请确保此更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。


353-353: 请验证启用 as-pd 的影响。

as-pd 选项从 no 更改为 yes,表示系统现在将作为 PD 运行。请确保此更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。


355-355: 请验证 pd-fake 配置项的影响。

添加 pd-fake 选项,值为 no,表示部署不会使用假 PD 实例。请确保此更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。


357-357: 请验证 pd-group-id 配置项的影响。

添加 pd-group-id 选项,值为 PikiwiDB,为 PD 组建立了一个唯一标识符。请确保此更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。


359-360: 请验证 pd-conf 配置项的影响。

添加 pd-conf 选项,值为 127.0.0.1:7787:0,指定了 PD 的地址和端口。请确保此更改不会对系统的整体功能产生负面影响,并验证新逻辑的正确性。

src/praft/praft.cc (7)

83-84: 代码更改已批准。

此处的更改没有问题。


87-87: 代码更改已批准。

函数签名更改为使用 std::move 提高了性能。


122-122: 代码更改已批准。

使用 std::move 进行赋值是一个很好的优化。


416-416: 代码更改已批准。

使用 std::move 进行传递是一个很好的优化。


148-148: 请验证配置解析失败的处理逻辑。

确保在配置解析失败时,系统能正确处理错误情况。

Verification successful

验证成功:

在配置解析失败时,系统会记录错误并返回状态,确保错误情况得到正确处理。

  • 代码位置:
    • src/praft/praft.cc 第 148 行
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the handling of configuration parsing failure.

# Test: Search for error handling after `parse_from` function. Expect: Proper error logging and handling.
rg --type cpp -A 5 $'parse_from'

Length of output: 374


164-166: 请验证 Raft 节点初始化失败的处理逻辑。

确保在 Raft 节点初始化失败时,系统能正确处理错误情况。

Verification successful

Raft 节点初始化失败的处理逻辑已正确实现。

src/praft/praft.cc 文件中,如果 node_->init(node_options_) 失败,系统会记录错误并重置节点,表明错误处理逻辑已到位。

  • 位置: src/praft/praft.cc, 行 164-166
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the handling of Raft node initialization failure.

# Test: Search for error handling after `init` function. Expect: Proper error logging and handling.
rg --type cpp -A 5 $'init'

Length of output: 25962


576-578: 请验证注释掉的服务器生命周期管理代码的影响。

注释掉的服务器生命周期管理代码可能会影响系统的稳定性,请确保这些更改不会引入问题。

Also applies to: 587-589, 616-618

src/store_service.h Show resolved Hide resolved
src/store_service.h Show resolved Hide resolved
src/store_service.h Show resolved Hide resolved
src/store_service.h Show resolved Hide resolved
src/store_service.h Show resolved Hide resolved
src/pd/pd_service.cc Show resolved Hide resolved
src/pd/pd_service.cc Show resolved Hide resolved
src/pd/pd_service.cc Show resolved Hide resolved
src/pd/pd_service.cc Show resolved Hide resolved
src/pd/pd_service.cc Show resolved Hide resolved
if (!HasFlag(kCmdFlagsExclusive)) {
db->UnLockShared();
}
};
}

if (!DoInitial(client)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里可以更新一下代码,有一个解决死锁的 pr。

Comment on lines +176 to +183

// @todo
// For now, create the shards with raft.cluster init
auto db_id = client->GetCurrentDB();
auto status = PSTORE.AddBackend(db_id, "Pikiwidb");
if (!status.ok()) {
return client->SetRes(CmdRes::kErrOther, fmt::format("Failed to init db: ", status.ToString()));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

兔子说的有道理

@AlexStocks AlexStocks closed this Jul 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✏️ Feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants