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

executor: fix panic issue when handle HashAggExec.Close() #46662

Merged
merged 5 commits into from
Sep 8, 2023

Conversation

solotzg
Copy link
Contributor

@solotzg solotzg commented Sep 5, 2023

What problem does this PR solve?

Issue Number: close #41778

Problem Summary:

func (e *HashAggExec) Close() error may be invoked multiple times and this part should guarantee idempotent

tidb/executor/aggregate.go

Lines 267 to 291 in 6f1bdf7

if e.parallelExecInitialized {
// `Close` may be called after `Open` without calling `Next` in test.
if !e.prepared {
close(e.inputCh)
for _, ch := range e.partialOutputChs {
close(ch)
}
for _, ch := range e.partialInputChs {
close(ch)
}
close(e.finalOutputCh)
}
close(e.finishCh)
for _, ch := range e.partialOutputChs {
channel.Clear(ch)
}
for _, ch := range e.partialInputChs {
channel.Clear(ch)
}
channel.Clear(e.finalOutputCh)
e.executed = false
if e.memTracker != nil {
e.memTracker.ReplaceBytesUsed(0)
}
}

What is changed and how it works?

  • rename parallelExecInitialized to parallelExecValid
  • set parallelExecValid to false after relevant channel closed.

The result becomes like ERROR 1815 (HY000): expression isnull(cast(test.ta.a1, var_string(4294967295))) cannot be pushed down because func (pc *PbConverter) encodeDatum has not supported json

func (pc *PbConverter) encodeDatum(ft *types.FieldType, d types.Datum) (tipb.ExprType, []byte, bool) {
var (
tp tipb.ExprType
val []byte
)
switch d.Kind() {
case types.KindNull:
tp = tipb.ExprType_Null
case types.KindInt64:
tp = tipb.ExprType_Int64
val = codec.EncodeInt(nil, d.GetInt64())
case types.KindUint64:
tp = tipb.ExprType_Uint64
val = codec.EncodeUint(nil, d.GetUint64())
case types.KindString, types.KindBinaryLiteral:
tp = tipb.ExprType_String
val = d.GetBytes()
case types.KindMysqlBit:
tp = tipb.ExprType_MysqlBit
val = d.GetBytes()
case types.KindBytes:
tp = tipb.ExprType_Bytes
val = d.GetBytes()
case types.KindFloat32:
tp = tipb.ExprType_Float32
val = codec.EncodeFloat(nil, d.GetFloat64())
case types.KindFloat64:
tp = tipb.ExprType_Float64
val = codec.EncodeFloat(nil, d.GetFloat64())
case types.KindMysqlDuration:
tp = tipb.ExprType_MysqlDuration
val = codec.EncodeInt(nil, int64(d.GetMysqlDuration().Duration))
case types.KindMysqlDecimal:
tp = tipb.ExprType_MysqlDecimal
var err error
val, err = codec.EncodeDecimal(nil, d.GetMysqlDecimal(), d.Length(), d.Frac())
if err != nil {
logutil.BgLogger().Error("encode decimal", zap.Error(err))
return tp, nil, false
}
case types.KindMysqlTime:
if pc.client.IsRequestTypeSupported(kv.ReqTypeDAG, int64(tipb.ExprType_MysqlTime)) {
tp = tipb.ExprType_MysqlTime
val, err := codec.EncodeMySQLTime(pc.sc, d.GetMysqlTime(), ft.GetType(), nil)
if err != nil {
logutil.BgLogger().Error("encode mysql time", zap.Error(err))
return tp, nil, false
}
return tp, val, true
}
return tp, nil, false
case types.KindMysqlEnum:
tp = tipb.ExprType_MysqlEnum
val = codec.EncodeUint(nil, d.GetUint64())
default:
return tp, nil, false
}
return tp, val, true
}

Tests

Reproduction:

CREATE database if not exists test;
use test;
CREATE TABLE `ta` (
    `a1` json DEFAULT NULL,
    `a2` decimal(31, 1) DEFAULT '-229239137389541701499094015080.1'
);
CREATE TABLE `tb` (
    `b1` smallint(6) DEFAULT '-11385',
    `b2` decimal(63, 14) DEFAULT '-6197127648752447138876497216172307937317445669286.98661563645110',
    KEY `76114f1d` (`b1`)
) ENGINE = InnoDB DEFAULT CHARSET = ascii COLLATE = ascii_bin COMMENT = 'be783ecc' PARTITION BY HASH (`b1`) PARTITIONS 3;
CREATE TABLE `tc` (
    `c1` text DEFAULT NULL,
    `c2` float NOT NULL DEFAULT '1.8132474',
    PRIMARY KEY (`c2`)
    /*T![clustered_index] CLUSTERED */
);
insert into `ta`
values (NULL, 1228.0);
insert into `ta`
values ('"json string1"', 623.8);
insert into `ta`
values (NULL, 1337.0);
insert into `ta`
values ('-44', 24.0);
insert into `ta`
values ('"json string1"', 882543.0);
insert into `ta`
values ('"json string1"', 24.0);
insert into `ta`
values ('323232323.32323235', 24.0);
insert into `ta`
values ('-44', 24.0);
insert into `ta`
values ('"json string1"', 24.0);
insert into `ta`
values ('"json string1"', 24.0);
select count(*)
from `ta`
where not (
        `ta`.`a1` in (
            select `b2`
            from `tb`
            where not (
                    `ta`.`a1` in (
                        select `c1`
                        from `tc`
                        where `ta`.`a2` in (
                                select `b2`
                                from `tb`
                                where not (
                                        `ta`.`a2` between 61151 and 260381
                                    )
                                    and IsNull(`ta`.`a1`)
                            )
                    )
                )
        )
    );

Result:

mysql> select count(*) from `ta` where not (         `ta`.`a1` in (             select `b2`             from `tb`             where not (
     `ta`.`a1` in (                         select `c1`                         from `tc`                         where `ta`.`a2` in (
              select `b2`                                 from `tb`                                 where not (                                         `ta`.`a2` between 61151 and 260381                                     )                                     and IsNull(`ta`.`a1`)
     )                     )                 )         )     );
ERROR 1815 (HY000): expression isnull(cast(test.ta.a1, var_string(4294967295))) cannot be pushed down

Check List

Tests

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

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

Signed-off-by: TONG, Zhigao <tongzhigao@pingcap.com>
@ti-chi-bot ti-chi-bot bot added do-not-merge/invalid-title do-not-merge/needs-linked-issue do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 5, 2023
@tiprow
Copy link

tiprow bot commented Sep 5, 2023

Hi @solotzg. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@codecov
Copy link

codecov bot commented Sep 5, 2023

Codecov Report

Merging #46662 (a82f27b) into master (5981cc5) will increase coverage by 0.1687%.
Report is 22 commits behind head on master.
The diff coverage is 100.0000%.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #46662        +/-   ##
================================================
+ Coverage   73.2845%   73.4532%   +0.1687%     
================================================
  Files          1319       1343        +24     
  Lines        395898     408581     +12683     
================================================
+ Hits         290132     300116      +9984     
- Misses        87278      90029      +2751     
+ Partials      18488      18436        -52     
Flag Coverage Δ
integration 28.2342% <100.0000%> (?)
unit 74.0434% <100.0000%> (+0.7589%) ⬆️

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

Components Coverage Δ
dumpling 54.0444% <ø> (ø)
parser 86.9780% <ø> (+1.9867%) ⬆️
br 49.4073% <ø> (-3.0561%) ⬇️
📢 Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pingcap).

@solotzg solotzg changed the title [WIP] fix 41778 executor: fix panic issue when handle HashAggExec.Close() Sep 5, 2023
@ti-chi-bot ti-chi-bot bot removed do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. do-not-merge/invalid-title labels Sep 5, 2023
@@ -327,7 +328,7 @@ func (e *HashAggExec) initForParallelExec(_ sessionctx.Context) {
e.finalWorkers[i].finalResultHolderCh <- exec.NewFirstChunk(e)
}

e.parallelExecInitialized = true
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 write a test case for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Signed-off-by: TONG, Zhigao <tongzhigao@pingcap.com>
@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Sep 7, 2023
@solotzg
Copy link
Contributor Author

solotzg commented Sep 7, 2023

/cc @wshwsh12

@ti-chi-bot ti-chi-bot bot requested a review from wshwsh12 September 7, 2023 05:22
2
Signed-off-by: TONG, Zhigao <tongzhigao@pingcap.com>
Signed-off-by: TONG, Zhigao <tongzhigao@pingcap.com>
@ti-chi-bot ti-chi-bot bot added needs-cherry-pick-release-5.3 Type: Need cherry pick to release-5.3 needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. needs-cherry-pick-release-6.1 Should cherry pick this PR to release-6.1 branch. 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. and removed do-not-merge/needs-triage-completed labels Sep 8, 2023
@ti-chi-bot ti-chi-bot bot added the approved label Sep 8, 2023
@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Sep 8, 2023
@ti-chi-bot
Copy link

ti-chi-bot bot commented Sep 8, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wshwsh12, XuHuaiyu

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 added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 8, 2023
@ti-chi-bot
Copy link

ti-chi-bot bot commented Sep 8, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-09-08 03:14:16.848092221 +0000 UTC m=+2674421.397108208: ☑️ agreed by wshwsh12.
  • 2023-09-08 07:57:43.374419243 +0000 UTC m=+2691427.923435215: ☑️ agreed by XuHuaiyu.

@solotzg
Copy link
Contributor Author

solotzg commented Sep 8, 2023

/retest

@tiprow
Copy link

tiprow bot commented Sep 8, 2023

@solotzg: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ti-chi-bot ti-chi-bot bot merged commit 1d55a3c into pingcap:master Sep 8, 2023
@ti-chi-bot
Copy link
Member

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

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Sep 8, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

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

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Sep 8, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

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

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Sep 8, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

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

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Sep 8, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

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

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Sep 8, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@solotzg solotzg deleted the fix-41778 branch September 11, 2023 02:16
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-5.3 Type: Need cherry pick to release-5.3 needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. needs-cherry-pick-release-6.1 Should cherry pick this PR to release-6.1 branch. 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-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

close of closed channel in (*HashAggExec).Close
5 participants