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: distinguish between normal node startup and snapshot loading #319

Open
wants to merge 9 commits into
base: unstable
Choose a base branch
from

Conversation

panlei-coder
Copy link
Collaborator

@panlei-coder panlei-coder commented May 15, 2024

1.方案:
(1)如果数据库正常关闭,理论上是不需要进行日志回放的,因为数据库在关闭是会将内存数据刷盘,并将日志截断到最新的位置,这样就可以保证正常关闭下的节点启动是no-op的。那么在数据库非正常关闭时,节点启动如果加载了快照,只会是一个多余的操作,因为快照本身就是空的,只有snapshot_meta数据。所以,在节点启动的时候,第一次调用on_snapshot_load接口理论上是不需要进行快照的加载,需要对第一个启动和正常快照安装进行区分。但是有一点需要注意,节点在刚加入集群且没有数据时,它在初始化之后是不会加载本地的快照(因为快照文件夹是空的),所以在安装来自 leader 的快照时需要 LoadDBFromCheckpoint 过程。
(2)节点在启动的时候,它是按照快照中 snapshot_meta 记录的 last_applied_index 作为日志回放点,那么在故障重启时,节点需要过滤掉一些日志,这无疑是影响效率的。我们可以在节点调用 on_load_snapshot 开始位置就设置这个日志回放点的位置,根据 DB 中最小 flush-index 来设置(这里的逻辑需要进一步的完善)。
(3)braft 的 pr : pikiwidb/braft#4

2.测试
因为获取 DB 中最小的 flush-index 目前还没有完善,所以在测试的时候将一个 DB 下 rocksdb 的数量设置成了1,使用save_load.sh 脚本进行测试。
47b1ee317bbd6abc2713ba8fc34f44a
d8c1c35829b6ed6534ff03275342364

Summary by CodeRabbit

  • New Features

    • Introduced methods to retrieve term and last log index in the Raft implementation.
    • Enhanced snapshot handling to support both synchronous and asynchronous cases.
    • Added functionality for node startup and snapshot loading.
  • Bug Fixes

    • Updated external project repository and branch for testing purposes.
    • Adjusted the number of RocksDB instances per DB from 3 to 1.
  • Improvements

    • Modified OpenSSL directory paths for better configuration.
    • Enhanced logging for flushing operations in the storage component.
  • Refactor

    • Consolidated library linking in the build configuration.
    • Replaced std::bind with lambda expressions for snapshot functions.
  • Chores

    • Removed unnecessary sleep command in initialization script.

@github-actions github-actions bot added 🧹 Updates This will not be worked on Invalid PR Title labels May 15, 2024
@panlei-coder panlei-coder changed the title New log playback to latest feat: distinguish between normal node startup and snapshot loading May 15, 2024
@AlexStocks
Copy link
Collaborator

@panlei-coder please fix ci failure and file confliction.

@panlei-coder
Copy link
Collaborator Author

@panlei-coder please fix ci failure and file confliction.

ok

@github-actions github-actions bot added the ✏️ Feature New feature or request label May 27, 2024
pikiwidb.conf Outdated
@@ -343,6 +343,6 @@ rocksdb-ttl-second 604800
rocksdb-periodic-second 259200;

############################### RAFT ###############################
use-raft yes
use-raft no
Copy link
Collaborator

Choose a reason for hiding this comment

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

默认不启用 raft?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

想的是默认单机场景下不启用 raft,而使用 rocksdb 的 wal,不过这个需要提 pr,我记个 issue 吧,看有谁愿意领一下

src/praft/praft.cc Outdated Show resolved Hide resolved
src/praft/praft.cc Outdated Show resolved Hide resolved
Copy link

coderabbitai bot commented Jun 4, 2024

Walkthrough

The recent updates encompass modifications to various CMake files, configuration files, and source code to enhance functionality, fix bugs, and optimize performance. Key changes include updating external project repositories, adjusting OpenSSL paths, modifying snapshot handling in RaftNodeCmd, and adding new methods for managing snapshots and log indices in the PRaft and Storage classes.

Changes

File(s) Change Summary
cmake/braft.cmake Updated GIT_REPOSITORY and GIT_TAG for testing purposes.
cmake/openssl.cmake Adjusted OpenSSL directory paths.
pikiwidb.conf Changed RocksDB instances per DB from 3 to 1.
save_load.sh Added redis-benchmark commands and removed a sleep command.
src/CMakeLists.txt Consolidated TARGET_LINK_LIBRARIES section without functional changes.
src/cmd_raft.cc Updated DoCmdSnapshot method to obtain self_snapshot_index before calling PRAFT.DoSnapshot.
src/db.cc Modified storage_options.do_snapshot_function to use a lambda capturing pikiwidb::PRAFT.
src/praft/praft.cc Added new methods and logic for snapshot handling and node startup.
src/praft/praft.h Added methods GetTerm, GetLastLogIndex, and member is_node_first_start_up_.
src/praft/psnapshot.cc Added includes and logic for snapshot parsing and generation.
src/storage/include/storage/storage.h Added GetSmallestFlushedLogIndex method in Storage class.
src/storage/src/log_index.cc Added logging statement using INFO in OnFlushCompleted method.
src/storage/src/redis.h Added GetSmallestFlushedLogIndex method in Redis class.
src/storage/src/storage.cc Added GetSmallestFlushedLogIndex method to find the minimum flushed log index.

Sequence Diagram(s) (Beta)

sequenceDiagram
    participant Client
    participant RaftNodeCmd
    participant PRAFT
    participant Storage

    Client->>RaftNodeCmd: Request DoCmdSnapshot
    RaftNodeCmd->>Storage: GetSmallestFlushedLogIndex
    Storage-->>RaftNodeCmd: Return self_snapshot_index
    RaftNodeCmd->>PRAFT: Call DoSnapshot(self_snapshot_index)
    PRAFT-->>RaftNodeCmd: Return status
    RaftNodeCmd->>Client: SetRes(CmdRes::kOK)

Poem

In the code where changes flow,
New paths for data now we show.
Snapshots taken, logs aligned,
With OpenSSL, our paths refined.
Raft commands in sync, they gleam,
Pikiwidb, our dream team! 🚀🐇


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 Configration 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: 4

Outside diff range and nitpick comments (1)
src/praft/psnapshot.cc (1)

Line range hint 69-89: The snapshot generation logic is comprehensive. However, consider replacing the hardcoded last_log_index with a dynamically retrieved or configured value to avoid potential issues in different environments.

- auto last_log_index = 30000;  // @todo
+ auto last_log_index = PSTORE.GetBackend(db_id)->GetStorage()->GetSmallestFlushedLogIndex();  // Dynamic retrieval
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between eec3030 and dcdf232.

Files selected for processing (14)
  • cmake/braft.cmake (1 hunks)
  • cmake/openssl.cmake (2 hunks)
  • pikiwidb.conf (1 hunks)
  • save_load.sh (2 hunks)
  • src/CMakeLists.txt (1 hunks)
  • src/cmd_raft.cc (1 hunks)
  • src/db.cc (1 hunks)
  • src/praft/praft.cc (5 hunks)
  • src/praft/praft.h (2 hunks)
  • src/praft/psnapshot.cc (3 hunks)
  • src/storage/include/storage/storage.h (1 hunks)
  • src/storage/src/log_index.cc (2 hunks)
  • src/storage/src/redis.h (1 hunks)
  • src/storage/src/storage.cc (1 hunks)
Files skipped from review due to trivial changes (3)
  • cmake/openssl.cmake
  • pikiwidb.conf
  • src/CMakeLists.txt
Additional context used
Shellcheck
save_load.sh

[info] 6-6: Use ./glob or -- glob so names with dashes won't become options. (SC2035)


[info] 7-7: Use ./glob or -- glob so names with dashes won't become options. (SC2035)

Additional comments not posted (12)
save_load.sh (1)

14-16: The addition of repeated redis-benchmark and redis-cli commands appears to be for stress testing or functionality checks.

cmake/braft.cmake (1)

19-23: The change in GIT repository and tag points to a different branch for testing. Ensure that this is intended for permanent integration or revert after testing.

src/db.cc (1)

40-43: Switching from std::bind to a lambda expression for do_snapshot_function enhances readability and performance. Good use of modern C++ practices.

src/praft/psnapshot.cc (1)

Line range hint 69-89: Handling of files in the snapshot directory is appropriately managed to ensure only relevant files are considered.

src/praft/praft.h (1)

140-141: The addition of new methods and a member variable supports new functionalities and enhances the RAFT module's capabilities. Good use of default arguments for backward compatibility.

Also applies to: 170-170

src/storage/src/log_index.cc (1)

172-172: The addition of logging statements enhances system visibility and aids in debugging and monitoring. Good practice.

src/storage/src/redis.h (1)

113-115: Implementation of GetSmallestFlushedLogIndex looks correct.

This method correctly retrieves the smallest flushed log index, which is crucial for ensuring that the node's state is consistent with the persisted state.

src/praft/praft.cc (3)

245-252: Added methods GetTerm and GetLastLogIndex enhance the functionality of the PRaft class by providing access to specific log details.

Also applies to: 254-261


659-682: The logic in on_snapshot_load handles different startup scenarios effectively. However, ensure that the method GetSmallestFlushedLogIndex is implemented correctly and efficiently.

Verification successful

The method GetSmallestFlushedLogIndex is indeed implemented in the codebase. Here are the relevant locations:

  • Declaration: src/storage/include/storage/storage.h
  • Implementation: src/storage/src/storage.cc

This confirms that the method is implemented and used correctly.

  • src/storage/include/storage/storage.h: LogIndex GetSmallestFlushedLogIndex() const;
  • src/storage/src/storage.cc: LogIndex Storage::GetSmallestFlushedLogIndex() const {
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the implementation of `GetSmallestFlushedLogIndex`.

# Test: Search for the implementation and usage of `GetSmallestFlushedLogIndex`. Expect: Efficient and correct implementation.
ast-grep --lang cpp --pattern $'class Storage { $$$ LogIndex GetSmallestFlushedLogIndex() const { $$$ } $$$ }' src/storage/include/storage/storage.h

Length of output: 494



Script:

#!/bin/bash
# Description: Verify the implementation of `GetSmallestFlushedLogIndex`.

# Test: Search for the implementation and usage of `GetSmallestFlushedLogIndex`. Expect: Efficient and correct implementation.
# Refine the search pattern and broaden the search scope.
ast-grep --lang cpp --pattern $'LogIndex GetSmallestFlushedLogIndex() const { $$$ }' 

Length of output: 85



Script:

#!/bin/bash
# Description: Verify the implementation of `GetSmallestFlushedLogIndex`.

# Test: Search for the implementation and usage of `GetSmallestFlushedLogIndex`. Expect: Efficient and correct implementation.
# Broaden the search to include potential variations of the method name and search across all files.
rg --glob '!*.md' 'GetSmallestFlushedLogIndex'

Length of output: 869


154-154: Ensure the path construction uses secure and efficient string handling.

src/storage/include/storage/storage.h (1)

1126-1127: Add documentation for GetSmallestFlushedLogIndex.

It's good practice to document public methods, especially for complex functionalities like this one. Consider adding a brief comment explaining what "smallest flushed log index" refers to and how it should be used.

src/storage/src/storage.cc (1)

2520-2527: The implementation of GetSmallestFlushedLogIndex method is correct and efficient.

Comment on lines +125 to +127
// @todo need to get self_snapshot_index
// auto self_snapshot_index = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetSmallestFlushedLogIndex();
auto s = PRAFT.DoSnapshot(); // self_snapshot_index
Copy link

Choose a reason for hiding this comment

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

Complete the implementation for self_snapshot_index or clarify the TODO.

The commented-out code suggests that self_snapshot_index is intended to be used but is not implemented. Would you like assistance in completing this part of the code?

@@ -9,6 +9,7 @@

#include <cassert>

#include "braft/raft.h"
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The verification process did not find any direct or indirect usage of entities from braft/raft.h in src/praft/praft.cc. The references to raft found were in comments and error messages, which do not necessitate the inclusion of the header file.

  • Lines: 12-12
  • File: src/praft/praft.cc
Analysis chain

Ensure the newly added #include "braft/raft.h" is necessary.


Let's further investigate if there are any indirect usages or dependencies that might require the inclusion of braft/raft.h.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify if the newly added include is used in the file.

# Test: Search for usage of entities from `braft/raft.h`. Expect: At least one usage.
ast-grep --lang cpp --pattern $'$_ { $$$ braft::raft::$$$ }' src/praft/praft.cc

Length of output: 80



Script:

#!/bin/bash
# Description: Verify if there are any indirect usages or dependencies that might require the inclusion of `braft/raft.h`.

# Test 1: Search for any usage of `braft` namespace in the file.
ast-grep --lang cpp --pattern $'$_ { $$$ braft::$$$ }' src/praft/praft.cc

# Test 2: Search for any usage of `raft` keyword in the file.
rg --word-regexp 'raft' src/praft/praft.cc

Length of output: 343

save_load.sh Outdated
@@ -1,4 +1,5 @@
#!/bin/bash

Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The save_load.sh script contains glob patterns (rm -fr *) that could potentially be affected by filenames with dashes. To prevent filenames with dashes from being interpreted as options, consider using ./* instead of *.

  • Line 2: rm -fr * should be updated to rm -fr ./ *
  • Line 7: rm -fr * should be updated to rm -fr ./ *
Analysis chain

Consider using ./ or -- with glob patterns to prevent filenames with dashes from being interpreted as options.

Also applies to: 7-7

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify if filenames with dashes are being interpreted as options.

# Test: Check if filenames with dashes exist and could potentially be interpreted as options.
find . -name "*-*" -exec echo "Filename with dash found: {}" \;

Length of output: 3925



Script:

#!/bin/bash
# Description: Check for glob patterns in save_load.sh that could be affected by filenames with dashes.

# Test: Search for glob patterns in save_load.sh
grep -E '\*|\?' save_load.sh

Length of output: 217

Comment on lines +14 to +21
#include "braft/snapshot.h"
#include "butil/files/file_path.h"

#include "pstd/log.h"
#include "pstd/pstd_string.h"

#include "config.h"
#include "praft.h"
Copy link

Choose a reason for hiding this comment

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

The additions and changes to snapshot path parsing are necessary for the new functionality. Consider adding unit tests to cover potential edge cases in the parsing logic.

Would you like me to help in creating unit tests for the snapshot path parsing logic?

Also applies to: 31-51

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: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between dcdf232 and 6f9f6fb.

Files selected for processing (1)
  • src/praft/praft.cc (5 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/praft/praft.cc

@AlexStocks
Copy link
Collaborator

@CodiumAI-Agent /improve

@CodiumAI-Agent
Copy link

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Score
Possible bug
Ensure the snapshot index is correctly retrieved and used in the snapshot operation

The line where DoSnapshot is called uses a placeholder comment for self_snapshot_index but
does not implement the retrieval of this index. It's crucial to ensure the snapshot index
is correctly retrieved and passed to the DoSnapshot method to maintain data consistency.

src/cmd_raft.cc [127]

-auto s = PRAFT.DoSnapshot();  // self_snapshot_index
+auto self_snapshot_index = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetSmallestFlushedLogIndex();
+auto s = PRAFT.DoSnapshot(self_snapshot_index);
 
Suggestion importance[1-10]: 10

Why: This suggestion addresses a critical issue where the snapshot index is not retrieved and passed correctly, which can lead to data inconsistency. Implementing this change ensures the snapshot operation is performed accurately.

10
Enhancement
Replace the hardcoded last log index with a dynamically retrieved value

The snapshot meta update process uses a hardcoded last_log_index value. This should be
dynamically retrieved to ensure the snapshot reflects the current state accurately.

src/praft/psnapshot.cc [84]

-auto last_log_index = 30000;  // @todo PSTORE.GetBackend(db_id)->GetStorage()->GetSmallestFlushedLogIndex();
+auto last_log_index = PSTORE.GetBackend(db_id)->GetStorage()->GetSmallestFlushedLogIndex();
 
Suggestion importance[1-10]: 9

Why: Replacing the hardcoded value with a dynamically retrieved value ensures that the snapshot accurately reflects the current state, which is crucial for data integrity and correctness.

9
Possible issue
Initialize the status object with a default value to ensure consistent behavior

The method DoSnapshot should handle the case where is_sync is false more robustly.
Currently, it initializes a butil::Status object but does not set any value before
returning it, which could lead to undefined behavior or incorrect status handling.

src/praft/praft.cc [551-552]

-butil::Status status;
+butil::Status status = butil::Status::OK();
 return status;
 
Suggestion importance[1-10]: 8

Why: Initializing the status object with a default value ensures consistent behavior and prevents potential undefined behavior or incorrect status handling, which is important for robustness.

8
Best practice
Add checks to ensure only valid log indices are considered when determining the smallest flushed log index

The method GetSmallestFlushedLogIndex iterates over instances and uses std::min without
checking if the instances are valid or if GetSmallestFlushedLogIndex returns a meaningful
value. It's important to add error handling or checks to ensure the value is valid.

src/storage/src/storage.cc [2523]

-smallest_flushed_log_index = std::min(smallest_flushed_log_index, inst->GetSmallestFlushedLogIndex());
+auto inst_index = inst->GetSmallestFlushedLogIndex();
+if (inst_index != INT64_MAX) {
+  smallest_flushed_log_index = std::min(smallest_flushed_log_index, inst_index);
+}
 
Suggestion importance[1-10]: 7

Why: Adding checks to ensure only valid log indices are considered improves the robustness and reliability of the method, although it addresses a less critical issue compared to the other suggestions.

7

@AlexStocks
Copy link
Collaborator

@CodiumAI-Agent /review

@CodiumAI-Agent
Copy link

PR Review 🔍

⏱️ Estimated effort to review [1-5]

4, due to the complexity of the changes involving snapshot management, log indexing, and integration with the raft protocol. The PR modifies critical parts of the system which require careful review to ensure correctness and stability.

🧪 Relevant tests

No

⚡ Possible issues

Possible Bug: The method PRaft::DoSnapshot uses a synchronous closure only when is_sync is true, but does not handle the asynchronous case properly. The method returns an uninitialized butil::Status which might lead to undefined behavior.

Possible Bug: The snapshot path parsing in PPosixFileSystemAdaptor::open assumes directory structure which might not be robust against changes or errors in path formatting.

🔒 Security concerns

No

Code feedback:
relevant filesrc/praft/praft.cc
suggestion      

Initialize butil::Status before returning in the asynchronous branch of PRaft::DoSnapshot. This avoids returning an uninitialized status which can lead to undefined behavior. [important]

relevant linereturn status;

relevant filesrc/praft/psnapshot.cc
suggestion      

Validate db_id after parsing from the snapshot path to ensure it is a valid database identifier before using it. This adds a layer of error checking that can prevent potential runtime errors. [important]

relevant lineassert(db_id >= 0);

relevant filesrc/praft/praft.cc
suggestion      

Consider handling the case where node_ is null in PRaft::GetLastLogIndex and PRaft::GetTerm by returning a default value or an error status, instead of just logging an error. This can prevent further undefined behavior in the system. [important]

relevant lineERROR("Node is not initialized");

relevant filesrc/praft/psnapshot.cc
suggestion      

Add error handling for the failure of snapshot_meta_memtable.save_to_file in PPosixFileSystemAdaptor::open. Logging the error is good, but the system should also handle the error appropriately, possibly by retrying or aborting the operation. [important]

relevant lineif (rc == 0) {

@AlexStocks
Copy link
Collaborator

@CodiumAI-Agent /improve

@CodiumAI-Agent
Copy link

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Score
Possible bug
Add error handling for uninitialized db_id after parsing the snapshot path

Add error handling for the scenario where db_id remains uninitialized (-1) after parsing
the snapshot path, which could lead to incorrect behavior or crashes.

src/praft/psnapshot.cc [69]

-assert(db_id >= 0);
+if (db_id < 0) {
+    ERROR("Failed to parse db_id from snapshot path");
+    return -1;
+}
 
Suggestion importance[1-10]: 10

Why: Adding error handling for an uninitialized db_id is crucial to prevent potential crashes or incorrect behavior. This suggestion addresses a significant potential bug and improves the robustness of the code.

10
Initialize the self_snapshot_index properly before using it in DoSnapshot()

Ensure that the self_snapshot_index is properly initialized before calling DoSnapshot().
Currently, the code is marked with a @todo comment and uses a placeholder, which might
lead to incorrect behavior or runtime errors.

src/cmd_raft.cc [125-127]

-// @todo need to get self_snapshot_index
-// auto self_snapshot_index = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetSmallestFlushedLogIndex();
-auto s = PRAFT.DoSnapshot();  // self_snapshot_index
+auto self_snapshot_index = PSTORE.GetBackend(client->GetCurrentDB())->GetStorage()->GetSmallestFlushedLogIndex();
+auto s = PRAFT.DoSnapshot(self_snapshot_index);
 
Suggestion importance[1-10]: 9

Why: This suggestion addresses a potential bug where self_snapshot_index is not initialized, which could lead to incorrect behavior or runtime errors. Proper initialization is crucial for the correct functioning of the snapshot process.

9
Enhancement
Use a dynamically retrieved last_log_index for setting snapshot metadata

Replace the hard-coded last_log_index with a dynamic value retrieved from the storage
system to ensure the snapshot metadata is accurate and up-to-date.

src/praft/praft.cc [84-85]

-auto last_log_index = 30000;  // @todo PSTORE.GetBackend(db_id)->GetStorage()->GetSmallestFlushedLogIndex();
+auto last_log_index = PSTORE.GetBackend(db_id)->GetStorage()->GetSmallestFlushedLogIndex();
 new_meta.set_last_included_index(last_log_index);
 
Suggestion importance[1-10]: 8

Why: This suggestion improves the accuracy and reliability of the snapshot metadata by using a dynamically retrieved last_log_index instead of a hard-coded value. This ensures that the snapshot reflects the current state of the system.

8
Performance
Optimize the loop for calculating the smallest flushed log index by adding an early exit condition

Optimize the calculation of smallest_flushed_log_index by early exiting the loop if the
smallest possible index is found, reducing unnecessary iterations.

src/storage/src/storage.cc [2522-2523]

 for (auto& inst : insts_) {
     smallest_flushed_log_index = std::min(smallest_flushed_log_index, inst->GetSmallestFlushedLogIndex());
+    if (smallest_flushed_log_index == 0) break;  // Early exit if smallest possible index is found
 }
 
Suggestion importance[1-10]: 6

Why: This suggestion offers a minor performance improvement by adding an early exit condition to the loop. While it optimizes the code, the impact is relatively small compared to other suggestions.

6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✏️ Feature New feature or request 🧹 Updates This will not be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants