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

Fix snapshot mapping merge bug #230

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

ValentaTomas
Copy link
Member

@ValentaTomas ValentaTomas commented Jan 7, 2025

There is a bug that sometimes happens during the sandbox pause. This PR fixed it. It will also include tests for the merged mapping.

Description by Callstackai

This PR fixes a bug related to snapshot mapping merging during sandbox pause and includes tests for the merged mapping functionality.

Diagrams of code changes
sequenceDiagram
    participant Client
    participant HeaderMerger
    participant Storage
    participant Validator

    Client->>Storage: Load Base Header
    Storage-->>Client: Base Header Data
    Client->>Storage: Load Diff Header
    Storage-->>Client: Diff Header Data

    Client->>HeaderMerger: MergeMappings(baseHeader, diffHeader)
    activate HeaderMerger
    HeaderMerger->>HeaderMerger: Process Mapping Overlaps
    HeaderMerger->>HeaderMerger: Handle Base/Diff Positions
    HeaderMerger-->>Client: Merged Header
    deactivate HeaderMerger

    Client->>Validator: ValidateMappings(mergedHeader)
    activate Validator
    Validator->>Validator: Check Contiguous Blocks
    Validator->>Validator: Verify Block Sizes
    Validator->>Validator: Validate Total Size
    Validator-->>Client: Validation Result
    deactivate Validator

    opt Visualization Enabled
        Client->>Client: Visualize Header Blocks
    end
Loading
Files Changed
FileSummary
packages/orchestrator/cmd/inspect-header/main.goReplaced direct mapping print statements with a call to the new Format method for better readability.
packages/orchestrator/cmd/simulate-headers-merge/main.goAdded a new file to handle simulation of header merges with command-line flags for base and diff build IDs.
packages/shared/pkg/storage/header/mapping.goUpdated MergeMappings function to handle edge cases and added Format method for BuildMap.
packages/shared/pkg/storage/header/mapping_test.goAdded unit tests for MergeMappings to ensure correct behavior under various scenarios.

@ValentaTomas ValentaTomas added the bug Something isn't working label Jan 7, 2025
@ValentaTomas ValentaTomas self-assigned this Jan 7, 2025
@ValentaTomas ValentaTomas marked this pull request as ready for review January 7, 2025 23:24
@ValentaTomas ValentaTomas requested a review from jakubno as a code owner January 7, 2025 23:24
Copy link

@callstackai callstackai bot left a comment

Choose a reason for hiding this comment

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

Key Issues

Critical issues include missing null checks for deserialized headers, potential division by zero and overflow errors in calculations, and incorrect conditions for handling overlapping segments, all of which could lead to application crashes or incorrect data processing.

Copy link
Member

@jakubno jakubno left a comment

Choose a reason for hiding this comment

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

Just to make sure I understand it correctly BuildStorageOffset corresponds to the data in the file right, which consists of only the changed blocks for that BuildId, so based on BuildStorageOffset you know where to read at that file.

I didn't check the visualizer. Thanks for tests! I think they are really helpful here. Maybe adding tests for BuildStorageOffset would make sense as well.

@@ -88,13 +96,13 @@ func MergeMappings(
base := baseMapping[baseIdx]
diff := diffMapping[diffIdx]

if base.Length == 0 {
if base.Length <= 0 {
Copy link
Member

Choose a reason for hiding this comment

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

is this just to make sure or how can it be negative?

baseIdx++

continue
}

if diff.Length == 0 {
if diff.Length <= 0 {
Copy link
Member

Choose a reason for hiding this comment

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

same here

packages/shared/pkg/storage/header/mapping.go Show resolved Hide resolved
}

mappings = append(mappings, baseMapping[baseIdx:]...)
mappings = append(mappings, diffMapping[diffIdx:]...)

return mappings
}

// Format returns a string representation of the mapping as:
Copy link
Member

Choose a reason for hiding this comment

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

I would move this to separate file, it isn't the core logic, so it's separated

Comment on lines +48 to +64
require.True(t, Equal(m, []*BuildMap{
{
Offset: 0,
Length: 2 * blockSize,
BuildId: ignoreID,
},
{
Offset: 2 * blockSize,
Length: 4 * blockSize,
BuildId: baseID,
},
{
Offset: 6 * blockSize,
Length: 2 * blockSize,
BuildId: ignoreID,
},
}))
Copy link
Member

Choose a reason for hiding this comment

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

Can you do this?

Suggested change
require.True(t, Equal(m, []*BuildMap{
{
Offset: 0,
Length: 2 * blockSize,
BuildId: ignoreID,
},
{
Offset: 2 * blockSize,
Length: 4 * blockSize,
BuildId: baseID,
},
{
Offset: 6 * blockSize,
Length: 2 * blockSize,
BuildId: ignoreID,
},
}))
require.True(t, Equal(m, simpleBase))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants