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

server: implement spill disk for cursorFetch result #45163

Merged
merged 14 commits into from
Jul 6, 2023

Conversation

YangKeao
Copy link
Member

@YangKeao YangKeao commented Jul 5, 2023

This PR is same with #44730. Some bugs of Github makes me fail to update #44730, so I have to create a new one 😮‍💨 .

What problem does this PR solve?

Issue Number: close #43233

What is changed and how it works?

  1. Use the rowContainer to store the rows after execution.
  2. Reset the memory/disk tracker when the statement is closed.

TODO:

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

I used the following codes to do some basic tests. The preloaded data is 5M rows sysbench ./oltp_insert.lua :

public static void tidbBigSelectTest() throws SQLException, InterruptedException {
    // the following are test codes for MySQL
    ConnectionImpl conn = (ConnectionImpl) DriverManager.getConnection("jdbc:mysql://127.0.0.1:4000/test?useCursorFetch=true&defaultFetchSize=1000", "root", "");
    conn.prepareStatement("set @@tidb_mem_quota_query = 8 << 20\n").execute();
    conn.prepareStatement("begin").execute();

    String sql = "SELECT * FROM test.sbtest1";
    PreparedStatement stmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    stmt.setFetchSize(1000);
    Instant beforeExecuteTime = Clock.systemUTC().instant();
    ResultSet rs = stmt.executeQuery();
    Instant afterExecuteTime = Clock.systemUTC().instant();
    System.out.println("It takes " + (Duration.between(beforeExecuteTime, afterExecuteTime).toString()) + " to EXECUTE QUERY");

    ResultSetMetaData metaData = rs.getMetaData();
    int columnCount = metaData.getColumnCount();
    int batchSize = stmt.getFetchSize();
    int rowCount = 0;
    int totalCount = 0;
    while (rs.next()) {
        rowCount++;
        totalCount++;
        Object[] columns = new Object[columnCount];
        for (int i = 1; i <= columnCount; i++) {
            columns[i - 1] = rs.getObject(i);
        }
        Row row = new Row(columns);
        if (rowCount == batchSize) {
            System.out.println("Fetched " + batchSize + " rows");
            System.out.println("allocated memory is " + (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));
            System.out.println("TotalCount " + totalCount);
            rowCount = 0;
        }
        if (totalCount == 4999999) {
            System.out.println("here");
        }
    }
    if (rowCount > 0) {
        System.out.println("allocated memory is " + (Runtime.getRuntime().totalMemory()-Runtime.getRuntime().freeMemory()));
        System.out.println("Fetched " + rowCount + " rows");
        System.out.println("TotalCount " + totalCount);
    }

    conn.prepareStatement("commit").execute();
}

Release note

Automatically spill the memory exceeding `tidb_mem_quota_query` into the disk

@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Jul 5, 2023
@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Jul 5, 2023
@YangKeao YangKeao changed the base branch from master to release-7.2 July 5, 2023 06:32
@YangKeao YangKeao requested a review from a team as a code owner July 5, 2023 06:32
@YangKeao YangKeao changed the base branch from release-7.2 to master July 5, 2023 06:32
@ti-chi-bot ti-chi-bot bot deleted a comment from ti-chi-bot Jul 5, 2023
@ti-chi-bot ti-chi-bot bot deleted a comment from ti-chi-bot Jul 5, 2023
@ti-chi-bot ti-chi-bot bot deleted a comment from ti-chi-bot Jul 5, 2023
@YangKeao
Copy link
Member Author

YangKeao commented Jul 5, 2023

/retest

@YangKeao YangKeao requested review from xhebox and removed request for a team July 5, 2023 06:36
@YangKeao YangKeao force-pushed the cursor-fetch-persist branch from 2a324dd to a701418 Compare July 5, 2023 06:57
@ti-chi-bot ti-chi-bot bot deleted a comment from ti-chi-bot Jul 5, 2023
@ti-chi-bot ti-chi-bot bot deleted a comment from ti-chi-bot Jul 5, 2023
@YangKeao YangKeao force-pushed the cursor-fetch-persist branch from b1477c0 to 3517e7a Compare July 5, 2023 07:48
server/conn.go Show resolved Hide resolved
@YangKeao YangKeao requested a review from xhebox July 5, 2023 13:54
@ti-chi-bot ti-chi-bot bot added the lgtm label Jul 6, 2023
@ti-chi-bot
Copy link

ti-chi-bot bot commented Jul 6, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hawkingrei, xhebox

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot removed the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Jul 6, 2023
@ti-chi-bot
Copy link

ti-chi-bot bot commented Jul 6, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-07-05 05:56:56.336695467 +0000 UTC m=+183448.270328885: ☑️ agreed by hawkingrei.
  • 2023-07-06 03:16:39.762632706 +0000 UTC m=+260231.696266127: ☑️ agreed by xhebox.

@ti-chi-bot ti-chi-bot bot merged commit 2ded221 into pingcap:master Jul 6, 2023
@YangKeao YangKeao added the needs-cherry-pick-release-7.1 Should cherry pick this PR to release-7.1 branch. label Jul 10, 2023
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.1: #45259.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jul 10, 2023
@bb7133 bb7133 added the needs-cherry-pick-release-6.5 Should cherry pick this PR to release-6.5 branch. label Jul 27, 2023
@bb7133
Copy link
Member

bb7133 commented Jul 27, 2023

/run-cherry-picker

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.5: #45602.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm needs-cherry-pick-release-6.5 Should cherry pick this PR to release-6.5 branch. needs-cherry-pick-release-7.1 Should cherry pick this PR to release-7.1 branch. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TiDB should track, limit (and maybe spill) the temporary memory used by CursorFetch
5 participants