Skip to content

support restore publication db - #16448

Merged
mergify[bot] merged 10 commits into
matrixorigin:mainfrom
YANGGMM:fix-15353-2
May 29, 2024
Merged

mergify[bot] merged 10 commits into
matrixorigin:mainfrom
YANGGMM:fix-15353-2

Conversation

@YANGGMM

@YANGGMM YANGGMM commented May 28, 2024 •

Copy link
Copy Markdown
Contributor

User description

What type of PR is this?

  • API-change
  • BUG
  • Improvement
  • Documentation
  • Feature
  • Test and CI
  • Code Refactoring

Which issue(s) this PR fixes:

issue #16353

What this PR does / why we need it:

support restore publication db


PR Type

Feature, Tests


Description

  • Added support for deleting publication from the database.
  • Added shared transaction background execution function.
  • Added functions to handle publication database operations during snapshot restoration.
  • Added test cases and SQL scripts for restoring publication database from snapshot.

Changes walkthrough 📝

Relevant files
Enhancement
authenticate.go
Add support for deleting publication from database and shared
transaction execution.

pkg/frontend/authenticate.go

  • Added SQL format for deleting publication from database.
  • Added function to get SQL for deleting publication from database.
  • Modified isDbPublishing to use shared transaction background
    execution.
  • +11/-8   
    back_exec.go
    Add shared transaction background execution function.       

    pkg/frontend/back_exec.go

    • Added function to get shared transaction background execution.
    +40/-0   
    snapshot.go
    Add support for publication database operations in snapshot
    restoration.

    pkg/frontend/snapshot.go

  • Added constants and SQL formats for publication database operations.
  • Added functions to get SQL for publication count with snapshot and
    restore publication record.
  • Added functions to check and drop publication record, and to check and
    restore publication record.
  • +129/-0 
    types.go
    Add shared transaction background execution method to FeSession
    interface.

    pkg/frontend/types.go

    • Added GetShareTxnBackgroundExec method to FeSession interface.
    +1/-0     
    Tests
    snapshot_restore_publication.result
    Add test cases for restoring publication database from snapshot.

    test/distributed/cases/snapshot/snapshot_restore_publication.result

    • Added test cases for restoring publication database from snapshot.
    +446/-0 
    snapshot_restore_publication.sql
    Add SQL scripts for testing publication database restoration from
    snapshot.

    test/distributed/cases/snapshot/snapshot_restore_publication.sql

  • Added SQL scripts for testing publication database restoration from
    snapshot.
  • +402/-0 

    💡 PR-Agent usage:
    Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    @YANGGMM
    YANGGMM requested a review from a user May 28, 2024 08:51
    @matrix-meow matrix-meow added the size/XL Denotes a PR that changes [1000, 1999] lines label May 28, 2024
    @qodo-code-review

    Copy link
    Copy Markdown

    PR Review 🔍

    ⏱️ Estimated effort to review [1-5]

    4, because the PR involves multiple complex changes across several files, including SQL operations, transaction handling, and publication management. The changes are spread across authentication, background execution, and snapshot restoration functionalities, which requires a deep understanding of the existing system architecture and the implications of these changes.

    🧪 Relevant tests

    Yes

    ⚡ Possible issues

    Possible Bug: The new function GetShareTxnBackgroundExec in back_exec.go contains a placeholder comment //TODO implement me which suggests that the implementation is not complete. This needs to be addressed to ensure the function operates as expected.

    Error Handling: In several new functions such as getSqlForDeletePubFromDatabase and checkAndRestorePublicationRecord, errors are checked but there is no logging or detailed error handling before returning the error. Adding logging could help in debugging and maintaining the code.

    🔒 Security concerns

    No

    @mergify mergify Bot added the kind/feature label May 28, 2024
    @qodo-code-review

    Copy link
    Copy Markdown

    PR Code Suggestions ✨

    CategorySuggestion                                                                                                                                    Score
    Security
    Use parameterized queries to prevent SQL injection vulnerabilities

    To avoid potential SQL injection vulnerabilities, consider using parameterized queries
    instead of fmt.Sprintf for constructing SQL statements.

    pkg/frontend/snapshot.go [78]

    -return fmt.Sprintf(getDbPubCountWithSnapshotFormat, snapshot, dbName), nil
    +return fmt.Sprintf(getDbPubCountWithSnapshotFormat, snapshot, dbName), nil // Use parameterized queries here
     
    Suggestion importance[1-10]: 10

    Why: The suggestion correctly identifies a security risk related to SQL injection and proposes a safer approach, which is crucial for preventing potential security breaches.

    10
    Best practice
    Use IF EXISTS when dropping the database and publication to avoid potential errors

    Consider using IF EXISTS when dropping the database and publication to avoid errors if
    they do not exist.

    test/distributed/cases/snapshot/snapshot_restore_publication.sql [29-30]

    -drop publication pubname1;
    -drop database db1;
    +drop publication if exists pubname1;
    +drop database if exists db1;
     
    Suggestion importance[1-10]: 10

    Why: The suggestion correctly identifies a potential error scenario and provides a safer alternative for dropping databases and publications, which is crucial for avoiding runtime errors in SQL scripts.

    10
    Add error handling for the Close method to ensure proper error management

    Consider adding error handling for the Close method to ensure any potential errors are
    logged or managed appropriately.

    pkg/frontend/authenticate.go [3421]

    -defer bh.Close()
    +defer func() {
    +    if err := bh.Close(); err != nil {
    +        getLogger().Error("Failed to close background exec", err)
    +    }
    +}()
     
    Suggestion importance[1-10]: 5

    Why: Proper error handling for resource cleanup methods like Close is a good practice. This suggestion ensures that any failures during resource release are appropriately managed, although it's a relatively minor improvement in the context of the overall application.

    5
    Possible issue
    Ensure snapshot creation is successful before proceeding with further operations

    Add a check to ensure the snapshot creation was successful before proceeding with further
    operations.

    test/distributed/cases/snapshot/snapshot_restore_publication.sql [27-29]

     create snapshot snapshot2 for account sys;
    +-- Check if snapshot creation was successful
    +-- (Assuming a method to check snapshot status)
    +-- if snapshot_status(snapshot2) = 'success' then
     drop publication pubname1;
     
    Suggestion importance[1-10]: 8

    Why: This suggestion is important for ensuring that operations dependent on the snapshot do not proceed if the snapshot creation fails, which is critical for maintaining data integrity and system stability.

    8
    Verify the restoration of the account from the snapshot to ensure data integrity

    Add a step to verify the restoration of the account from the snapshot to ensure data
    integrity.

    test/distributed/cases/snapshot/snapshot_restore_publication.sql [41]

     restore account sys from snapshot snapshot2;
    +-- Verify restoration
    +-- (Assuming a method to verify restoration)
    +-- verify_restoration(snapshot2);
     
    Suggestion importance[1-10]: 8

    Why: Verifying the restoration from a snapshot is crucial for ensuring that the data integrity is maintained post-restoration, which is essential for reliable system operations.

    8
    Add validation to ensure snapshotName and dbName are not empty before executing SQL

    Add a check to ensure that snapshotName and dbName are not empty before proceeding with
    the SQL execution to avoid unnecessary database operations.

    pkg/frontend/snapshot.go [434]

    +if snapshotName == "" || dbName == "" {
    +    return errors.New("snapshotName and dbName cannot be empty")
    +}
     if err := checkPubAndDropPubRecord(ctx, bh, snapshotName, dbName); err != nil {
     
    Suggestion importance[1-10]: 7

    Why: Adding checks for empty strings before executing SQL operations is a good practice to avoid unnecessary database operations and potential errors. This suggestion improves the robustness of the code.

    7
    Add error handling for drop statements to capture and log errors

    Add error handling for the drop statements to capture and log any errors that occur during
    the drop operations. This will help in debugging and understanding any issues that arise
    during the execution.

    test/distributed/cases/snapshot/snapshot_restore_publication.result [62]

    -drop publication if exists pubname1;
    +BEGIN
    +  drop publication if exists pubname1;
    +EXCEPTION
    +  WHEN OTHERS THEN
    +    DBMS_OUTPUT.PUT_LINE(SQLERRM);
    +END;
     
    Suggestion importance[1-10]: 5

    Why: Adding error handling for drop statements is a good practice for better debugging and error management, but the specific syntax suggested does not match the SQL dialect used in the script.

    5
    Maintainability
    Refactor nested if-else blocks into smaller helper functions for better readability and maintainability

    To improve readability and maintainability, consider refactoring the nested if-else blocks
    in checkAndRestorePublicationRecord into smaller helper functions.

    pkg/frontend/snapshot.go [1184-1206]

     if pubCount > 0 {
    -    var curAccountId uint32
    -    curAccountId, err = defines.GetAccountId(ctx)
    +    if err := restorePublication(ctx, bh, snapshotName, dbName, toAccountId); err != nil {
    +        return
    +    }
    +}
    +
    +func restorePublication(ctx context.Context, bh BackgroundExec, snapshotName, dbName string, toAccountId uint32) error {
    +    curAccountId, err := defines.GetAccountId(ctx)
         if err != nil {
    -        return
    +        return err
         }
         ctx = defines.AttachAccountId(ctx, toAccountId)
         insertIntoSql := fmt.Sprintf(restorePubDbDataFmt, moCatalog, PubDbName, moCatalog, PubDbName, snapshotName, dbName)
         if curAccountId == toAccountId {
    -        if err = bh.Exec(ctx, insertIntoSql); err != nil {
    -            return
    -        }
    -    } else {
    -        if err = bh.ExecRestore(ctx, insertIntoSql, curAccountId, toAccountId); err != nil {
    -            return
    -        }
    +        return bh.Exec(ctx, insertIntoSql)
         }
    +    return bh.ExecRestore(ctx, insertIntoSql, curAccountId, toAccountId)
     }
     
    Suggestion importance[1-10]: 6

    Why: Refactoring complex logic into smaller, more manageable functions enhances code readability and maintainability. This suggestion is beneficial, though not critical.

    6
    Use descriptive names for snapshots to improve code readability and maintainability

    Use more descriptive names for snapshots to make the code easier to understand and
    maintain.

    test/distributed/cases/snapshot/snapshot_restore_publication.sql [27]

    -create snapshot snapshot2 for account sys;
    +create snapshot snapshot_before_restore for account sys;
     
    Suggestion importance[1-10]: 6

    Why: Using more descriptive names for snapshots enhances code readability and maintainability, although it's not a critical issue, it's a good practice.

    6
    Enhancement
    Add a CHECK constraint to ensure column a contains only positive integers

    Add a CHECK constraint to the create table statements to ensure that the column a only
    contains positive integers. This will help in maintaining data validity.

    test/distributed/cases/snapshot/snapshot_restore_publication.result [4]

    -create table t1(a int);
    +create table t1(a int CHECK (a > 0));
     
    Suggestion importance[1-10]: 6

    Why: Adding a CHECK constraint is a valid suggestion for enhancing data integrity. However, it is not critical as the script does not specify requirements for the values of a.

    6

    Comment thread pkg/frontend/back_exec.go Resolved
    @YANGGMM
    YANGGMM requested a review from daviszhen May 29, 2024 02:09
    @mergify
    mergify Bot merged commit fc1e78c into matrixorigin:main May 29, 2024
    YANGGMM added a commit to YANGGMM/matrixone that referenced this pull request May 30, 2024
    support restore publication db
    
    
    ___
    
    ### **PR Type**
    Feature, Tests
    
    
    ___
    
    ### **Description**
    - Added support for deleting publication from the database.
    - Added shared transaction background execution function.
    - Added functions to handle publication database operations during snapshot restoration.
    - Added test cases and SQL scripts for restoring publication database from snapshot.
    
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement
    </strong></td><td><table>
    <tr>
    <td>
    <details>
    <summary><strong>authenticate.go</strong><dd><code>Add support for deleting publication from database and shared </code><br><code>transaction execution.</code></dd></summary>
    <hr>
    
    pkg/frontend/authenticate.go
    
    <li>Added SQL format for deleting publication from database.<br> <li> Added function to get SQL for deleting publication from database.<br> <li> Modified <code>isDbPublishing</code> to use shared transaction background <br>execution.<br>
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-849f201c351210bd95807e99d1538e2602a5244b256c35e58467afe304c509e6">+11/-8</a>&nbsp; &nbsp; </td>
    
    </tr>
    
    <tr>
    <td>
    <details>
    <summary><strong>back_exec.go</strong><dd><code>Add shared transaction background execution function.</code>&nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    pkg/frontend/back_exec.go
    
    - Added function to get shared transaction background execution.
    
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-6c8c7605ddaba02dbfd483c021e3901f66c8205e467634fde29e0dbdbb79396a">+40/-0</a>&nbsp; &nbsp; </td>
    
    </tr>
    
    <tr>
    <td>
    <details>
    <summary><strong>snapshot.go</strong><dd><code>Add support for publication database operations in snapshot </code><br><code>restoration.</code></dd></summary>
    <hr>
    
    pkg/frontend/snapshot.go
    
    <li>Added constants and SQL formats for publication database operations.<br> <li> Added functions to get SQL for publication count with snapshot and <br>restore publication record.<br> <li> Added functions to check and drop publication record, and to check and <br>restore publication record.<br>
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-2774e67c5debc02b9642113381977ab7c4c591114aabe013627c63851d0c2f9c">+129/-0</a>&nbsp; </td>
    
    </tr>
    
    <tr>
    <td>
    <details>
    <summary><strong>types.go</strong><dd><code>Add shared transaction background execution method to FeSession </code><br><code>interface.</code></dd></summary>
    <hr>
    
    pkg/frontend/types.go
    
    - Added `GetShareTxnBackgroundExec` method to `FeSession` interface.
    
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-5426ed8bd66760011299bcef0a9d1d552477eaae49a8451e16b8ec5bf7d7310f">+1/-0</a>&nbsp; &nbsp; &nbsp; </td>
    
    </tr>
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
    <td>
    <details>
    <summary><strong>snapshot_restore_publication.result</strong><dd><code>Add test cases for restoring publication database from snapshot.</code></dd></summary>
    <hr>
    
    test/distributed/cases/snapshot/snapshot_restore_publication.result
    
    - Added test cases for restoring publication database from snapshot.
    
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-cd27b7e6665f58d3df027239321cc738648d682e32282cdba3bc4623a1f6339e">+446/-0</a>&nbsp; </td>
    
    </tr>
    
    <tr>
    <td>
    <details>
    <summary><strong>snapshot_restore_publication.sql</strong><dd><code>Add SQL scripts for testing publication database restoration from </code><br><code>snapshot.</code></dd></summary>
    <hr>
    
    test/distributed/cases/snapshot/snapshot_restore_publication.sql
    
    <li>Added SQL scripts for testing publication database restoration from <br>snapshot.<br>
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-628fac0767093e07f75ae64707642fb96eb836b169c4d011013cfda409934fa3">+402/-0</a>&nbsp; </td>
    
    </tr>
    </table></td></tr></tr></tbody></table>
    
    ___
    
    > 💡 **PR-Agent usage**:
    >Comment `/help` on the PR to get a list of all available PR-Agent tools and their descriptions
    
    Approved by: @daviszhen, @aressu1985
    @YANGGMM
    YANGGMM deleted the fix-15353-2 branch November 28, 2024 06:00
    fengttt pushed a commit to fengttt/matrixone that referenced this pull request May 7, 2026
    support restore publication db
    
    
    ___
    
    ### **PR Type**
    Feature, Tests
    
    
    ___
    
    ### **Description**
    - Added support for deleting publication from the database.
    - Added shared transaction background execution function.
    - Added functions to handle publication database operations during snapshot restoration.
    - Added test cases and SQL scripts for restoring publication database from snapshot.
    
    
    
    ___
    
    
    
    ### **Changes walkthrough** 📝
    <table><thead><tr><th></th><th align="left">Relevant files</th></tr></thead><tbody><tr><td><strong>Enhancement
    </strong></td><td><table>
    <tr>
    <td>
    <details>
    <summary><strong>authenticate.go</strong><dd><code>Add support for deleting publication from database and shared </code><br><code>transaction execution.</code></dd></summary>
    <hr>
    
    pkg/frontend/authenticate.go
    
    <li>Added SQL format for deleting publication from database.<br> <li> Added function to get SQL for deleting publication from database.<br> <li> Modified <code>isDbPublishing</code> to use shared transaction background <br>execution.<br>
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-849f201c351210bd95807e99d1538e2602a5244b256c35e58467afe304c509e6">+11/-8</a>&nbsp; &nbsp; </td>
    
    </tr>
    
    <tr>
    <td>
    <details>
    <summary><strong>back_exec.go</strong><dd><code>Add shared transaction background execution function.</code>&nbsp; &nbsp; &nbsp; &nbsp; </dd></summary>
    <hr>
    
    pkg/frontend/back_exec.go
    
    - Added function to get shared transaction background execution.
    
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-6c8c7605ddaba02dbfd483c021e3901f66c8205e467634fde29e0dbdbb79396a">+40/-0</a>&nbsp; &nbsp; </td>
    
    </tr>
    
    <tr>
    <td>
    <details>
    <summary><strong>snapshot.go</strong><dd><code>Add support for publication database operations in snapshot </code><br><code>restoration.</code></dd></summary>
    <hr>
    
    pkg/frontend/snapshot.go
    
    <li>Added constants and SQL formats for publication database operations.<br> <li> Added functions to get SQL for publication count with snapshot and <br>restore publication record.<br> <li> Added functions to check and drop publication record, and to check and <br>restore publication record.<br>
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-2774e67c5debc02b9642113381977ab7c4c591114aabe013627c63851d0c2f9c">+129/-0</a>&nbsp; </td>
    
    </tr>
    
    <tr>
    <td>
    <details>
    <summary><strong>types.go</strong><dd><code>Add shared transaction background execution method to FeSession </code><br><code>interface.</code></dd></summary>
    <hr>
    
    pkg/frontend/types.go
    
    - Added `GetShareTxnBackgroundExec` method to `FeSession` interface.
    
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-5426ed8bd66760011299bcef0a9d1d552477eaae49a8451e16b8ec5bf7d7310f">+1/-0</a>&nbsp; &nbsp; &nbsp; </td>
    
    </tr>
    </table></td></tr><tr><td><strong>Tests
    </strong></td><td><table>
    <tr>
    <td>
    <details>
    <summary><strong>snapshot_restore_publication.result</strong><dd><code>Add test cases for restoring publication database from snapshot.</code></dd></summary>
    <hr>
    
    test/distributed/cases/snapshot/snapshot_restore_publication.result
    
    - Added test cases for restoring publication database from snapshot.
    
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-cd27b7e6665f58d3df027239321cc738648d682e32282cdba3bc4623a1f6339e">+446/-0</a>&nbsp; </td>
    
    </tr>
    
    <tr>
    <td>
    <details>
    <summary><strong>snapshot_restore_publication.sql</strong><dd><code>Add SQL scripts for testing publication database restoration from </code><br><code>snapshot.</code></dd></summary>
    <hr>
    
    test/distributed/cases/snapshot/snapshot_restore_publication.sql
    
    <li>Added SQL scripts for testing publication database restoration from <br>snapshot.<br>
    
    
    </details>
    
    
    </td>
    <td><a href="https://github.com/matrixorigin/matrixone/pull/16448/files#diff-628fac0767093e07f75ae64707642fb96eb836b169c4d011013cfda409934fa3">+402/-0</a>&nbsp; </td>
    
    </tr>
    </table></td></tr></tr></tbody></table>
    
    ___
    
    > 💡 **PR-Agent usage**:
    >Comment `/help` on the PR to get a list of all available PR-Agent tools and their descriptions
    
    Approved by: @daviszhen, @aressu1985
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    kind/feature Review effort [1-5]: 4 size/XL Denotes a PR that changes [1000, 1999] lines Tests

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    4 participants