Skip to content

fix(resize): ensure size is updated after successful expansion#1649

Merged
inabao merged 1 commit into0.16from
fix-resize-size-order-0.16
Mar 11, 2026
Merged

fix(resize): ensure size is updated after successful expansion#1649
inabao merged 1 commit into0.16from
fix-resize-size-order-0.16

Conversation

@inabao
Copy link
Copy Markdown
Collaborator

@inabao inabao commented Mar 10, 2026

cp #1642 to 0.16
link: #1643
Move max_capacity_ update to after all expansion operations complete. This prevents inconsistent state where size is updated but memory allocation failed in subsequent resize operations.

Fixes resize ordering in:

  • HGraph::resize
  • GraphDataCell::Resize
  • FlattenDataCell::Resize
  • ExtraInfoDataCell::Resize
  • SparseVectorDataCell::Resize

Move max_capacity_ update to after all expansion operations complete.
This prevents inconsistent state where size is updated but memory
allocation failed in subsequent resize operations.

Fixes resize ordering in:
- HGraph::resize
- GraphDataCell::Resize
- FlattenDataCell::Resize
- ExtraInfoDataCell::Resize
- SparseVectorDataCell::Resize

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
@inabao inabao self-assigned this Mar 10, 2026
@inabao inabao added kind/bug Something isn't working version/0.16 labels Mar 10, 2026
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the robustness of resize operations across various data structures by reordering the max_capacity_ update. Previously, the capacity was updated before all underlying memory allocations were finalized, creating a potential for an inconsistent state if subsequent allocations failed. The change ensures that the capacity is only updated once all necessary memory has been successfully reserved, thereby improving the reliability of expansion processes.

Highlights

  • Consistent State Management: The update of max_capacity_ has been moved to occur after all memory allocation and resizing operations are successfully completed in several data structures. This ensures that the reported capacity accurately reflects the actual allocated resources, preventing inconsistent states if an allocation fails.
  • Affected Components: This fix has been applied to HGraph::resize, GraphDataCell::Resize, FlattenDataCell::Resize, ExtraInfoDataCell::Resize, and SparseVectorDataCell::Resize methods.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/algorithm/hgraph.cpp
    • Moved the max_capacity_ update to the end of the resize function, after all internal components have been resized.
  • src/data_cell/extra_info_datacell.h
    • Relocated the max_capacity_ assignment to occur after the I/O allocation operation.
  • src/data_cell/flatten_datacell.h
    • Adjusted the max_capacity_ update to happen after the I/O allocation.
  • src/data_cell/graph_datacell.h
    • Shifted the max_capacity_ update to follow the I/O allocation.
  • src/data_cell/sparse_vector_datacell.h
    • Placed the max_capacity_ update after both the I/O and offset I/O allocations.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly addresses a potential state inconsistency issue by moving the update of max_capacity_ to after all resize and allocation operations have completed successfully, ensuring capacity is updated only when storage is successfully expanded. However, a potential integer truncation issue was identified in HGraph::resize where a 64-bit size is truncated to a 32-bit InnerIdType, which could lead to data corruption if the index grows beyond 4 billion elements. Additionally, there are concerns regarding exception safety in some complex resize operations, which could leave objects in an inconsistent state if a failure occurs midway.

if (this->extra_infos_ != nullptr) {
this->extra_infos_->Resize(new_size_power_2);
}
this->max_capacity_.store(new_size_power_2);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

security-high high

A critical issue exists where the uint64_t variable new_size_power_2 is truncated when stored into this->max_capacity_ (an InnerIdType, 32-bit). If new_size_power_2 exceeds 2^32 - 1, this truncation will lead to an inconsistent state, incorrect capacity reporting, and potential ID collisions and data corruption. A check should be added to prevent this. Furthermore, while the max_capacity_ update is better placed, this function lacks strong exception safety. If Resize calls (e.g., bottom_graph_->Resize) or std::make_shared throw exceptions, the HGraph object can be left in a partially modified, inconsistent state. To ensure atomicity, consider performing all fallible allocations and resizes on temporary objects and swapping them only upon success.

        if (new_size_power_2 > std::numeric_limits<InnerIdType>::max()) {
            throw VsagException(ErrorType::INVALID_ARGUMENT, "new_size exceeds maximum capacity of InnerIdType");
        }
        this->max_capacity_.store(static_cast<InnerIdType>(new_size_power_2));

Comment on lines 247 to +253
node_versions_.resize(new_size);
}
this->max_capacity_ = new_size;
uint64_t io_size = static_cast<uint64_t>(new_size) * static_cast<uint64_t>(code_line_size_);
uint8_t end_flag =
127; // the value is meaningless, only to occupy the position for io allocate
this->io_->Write(&end_flag, 1, io_size);
this->max_capacity_ = new_size;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Similar to other Resize functions in this PR, moving the max_capacity_ update is a good fix. However, the function is still not fully exception-safe. If this->io_->Write fails after node_versions_.resize(new_size) has succeeded, the GraphDataCell object will be left in an inconsistent state (node_versions_ is resized but the underlying storage via io_ is not).

For stronger exception safety, you could write to a new I/O target and then switch to it, or find a way to rollback the node_versions_ resize on I/O failure.

Comment on lines 76 to +78
this->io_->Write(&end_flag, 1, io_size);
this->offset_io_->Write(&end_flag, 1, new_capacity * sizeof(uint32_t));
this->max_capacity_ = new_capacity;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

While moving the max_capacity_ update to the end is correct, this Resize operation is not atomic. If the second write to this->offset_io_ fails, the main data this->io_ has already been modified. This leaves the SparseVectorDataCell in an inconsistent state.

To ensure atomicity, you could consider a two-phase commit-like approach where changes are prepared and then applied, or use a journal to be able to roll back on failure.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@            Coverage Diff             @@
##             0.16    #1649      +/-   ##
==========================================
- Coverage   92.31%   92.28%   -0.03%     
==========================================
  Files         295      295              
  Lines       15686    15686              
==========================================
- Hits        14480    14476       -4     
- Misses       1206     1210       +4     
Flag Coverage Δ
cpp 92.28% <100.00%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
common 92.73% <ø> (+0.11%) ⬆️
datacell 92.29% <100.00%> (-0.23%) ⬇️
index 91.38% <100.00%> (+0.01%) ⬆️
simd 100.00% <ø> (ø)

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d4fec06...930f6a1. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Collaborator

@wxyucs wxyucs left a comment

Choose a reason for hiding this comment

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

lgtm

@inabao inabao merged commit 54c6f9f into 0.16 Mar 11, 2026
23 of 24 checks passed
@inabao inabao deleted the fix-resize-size-order-0.16 branch March 11, 2026 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/S version/0.16

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants