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

ddl_notifier: implement serialization and deserialization for SchemaChangeEvent #56089

Merged

Conversation

fzzf678
Copy link
Contributor

@fzzf678 fzzf678 commented Sep 14, 2024

What problem does this PR solve?

Issue Number: ref #55722

Problem Summary:

What changed and how does it work?

Add Marshal and UnMarshal method for SchemaChangeEvent.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

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

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/needs-tests-checked size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 14, 2024
Copy link

tiprow bot commented Sep 14, 2024

Hi @fzzf678. 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-sigs/prow repository.

Copy link

codecov bot commented Sep 14, 2024

Codecov Report

Attention: Patch coverage is 93.60000% with 8 lines in your changes missing coverage. Please review.

Project coverage is 56.7927%. Comparing base (b9a7d35) to head (be7d271).
Report is 15 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #56089         +/-   ##
=================================================
- Coverage   72.8966%   56.7927%   -16.1040%     
=================================================
  Files          1609       1762        +153     
  Lines        447141     635609     +188468     
=================================================
+ Hits         325951     360980      +35029     
- Misses       101132     249795     +148663     
- Partials      20058      24834       +4776     
Flag Coverage Δ
integration 39.3827% <76.8000%> (?)
unit 72.0719% <92.0000%> (+0.0641%) ⬆️

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

Components Coverage Δ
dumpling 52.9567% <ø> (ø)
parser ∅ <ø> (∅)
br 63.0302% <ø> (+17.2388%) ⬆️

@@ -33,7 +35,7 @@ func TestPublishToTableStore(t *testing.T) {
CREATE TABLE ddl_notifier (
ddl_job_id BIGINT,
multi_schema_change_seq BIGINT COMMENT '-1 if the schema change does not belong to a multi-schema change DDL. 0 or positive numbers representing the sub-job index of a multi-schema change DDL',
schema_change JSON COMMENT 'SchemaChange at rest',
schema_change LONGBLOB COMMENT 'SchemaChange at rest',
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the reason to change the column type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried the JSON type and use row.GetJSON() and row.GetBytes() get the serialized data, but it can't be deserialized. I took a look at other system tables like tidb_global_task and tidb_background_subtask, they use LONGBLOB type save the meta and row.GetBytes() get the serialized data and I did so. Is it my way of operating the JSON type is wrong?

Copy link
Contributor

@lance6716 lance6716 Sep 18, 2024

Choose a reason for hiding this comment

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

diff --git a/pkg/ddl/notifier/publish_testkit_test.go b/pkg/ddl/notifier/publish_testkit_test.go
index 30f0c1170f..d5e01cbabf 100644
--- a/pkg/ddl/notifier/publish_testkit_test.go
+++ b/pkg/ddl/notifier/publish_testkit_test.go
@@ -35,7 +35,7 @@ func TestPublishToTableStore(t *testing.T) {
 CREATE TABLE ddl_notifier (
        ddl_job_id BIGINT,
        multi_schema_change_seq BIGINT COMMENT '-1 if the schema change does not belong to a multi-schema change DDL. 0 or positive numbers representing the sub-job index of a multi-schema change DDL',
-       schema_change LONGBLOB COMMENT 'SchemaChange at rest',
+       schema_change JSON COMMENT 'SchemaChange at rest',
        processed_by_flag BIGINT UNSIGNED DEFAULT 0 COMMENT 'flag to mark which subscriber has processed the event',
        PRIMARY KEY(ddl_job_id, multi_schema_change_seq)
 )
diff --git a/pkg/ddl/notifier/store.go b/pkg/ddl/notifier/store.go
index c652fd0052..f259277e0b 100644
--- a/pkg/ddl/notifier/store.go
+++ b/pkg/ddl/notifier/store.go
@@ -94,7 +94,12 @@ func (t *tableStore) List(ctx context.Context, se *sess.Session, limit int) ([]*
        ret := make([]*schemaChange, 0, len(rows))
        for _, row := range rows {
                event := SchemaChangeEvent{}
-               err = json.Unmarshal(row.GetBytes(2), &event)
+               binaryJSON := row.GetJSON(2)
+               jsonStr, err := binaryJSON.MarshalJSON()
+               if err != nil {
+                       return nil, errors.Trace(err)
+               }
+               err = json.Unmarshal(jsonStr, &event)
                if err != nil {
                        return nil, errors.Trace(err)
                }

However I don't have strong opinion about the column type. JSON type can add JSON syntax check in the SQL layer and readable SELECT output, LONGTEXT or LONGBLOB has readable SELECT output. LONGBLOB has no advantage. Maybe wait other reviewers' opinions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried again and found that the JSON type in table is not equal to our internal json type in code, we need a conversion to use the JSON type in table like this:

j, err := row.GetJSON(2).MarshalJSON()
err = json.Unmarshal(j, &event)

https://github.com/pingcap/tidb/blob/master/pkg/types/json_binary.go#L151-L180

@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Sep 18, 2024
Copy link
Contributor

@lance6716 lance6716 left a comment

Choose a reason for hiding this comment

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

rest lgtm

pkg/ddl/notifier/events.go Outdated Show resolved Hide resolved
pkg/ddl/notifier/publish_testkit_test.go Outdated Show resolved Hide resolved
@@ -33,7 +35,7 @@ func TestPublishToTableStore(t *testing.T) {
CREATE TABLE ddl_notifier (
ddl_job_id BIGINT,
multi_schema_change_seq BIGINT COMMENT '-1 if the schema change does not belong to a multi-schema change DDL. 0 or positive numbers representing the sub-job index of a multi-schema change DDL',
schema_change JSON COMMENT 'SchemaChange at rest',
schema_change LONGBLOB COMMENT 'SchemaChangeEvent at rest',
Copy link
Contributor

@lance6716 lance6716 Sep 18, 2024

Choose a reason for hiding this comment

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

Because other reviewers and me has no comment, I think you can choose the type as you like. Please /unhold after you choose it.

pkg/ddl/notifier/events.go Outdated Show resolved Hide resolved
@lance6716
Copy link
Contributor

/hold

@ti-chi-bot ti-chi-bot bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 18, 2024
Copy link

ti-chi-bot bot commented Sep 18, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: CbcWestwolf, lance6716

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

Copy link

ti-chi-bot bot commented Sep 18, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-09-18 03:46:04.802907408 +0000 UTC m=+1019234.543331347: ☑️ agreed by CbcWestwolf.
  • 2024-09-18 06:24:21.987593871 +0000 UTC m=+1028731.728017810: ☑️ agreed by lance6716.

@fzzf678
Copy link
Contributor Author

fzzf678 commented Sep 18, 2024

/retest

@fzzf678
Copy link
Contributor Author

fzzf678 commented Sep 18, 2024

/unhold

@ti-chi-bot ti-chi-bot bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 18, 2024
Copy link

tiprow bot commented Sep 18, 2024

@fzzf678: 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-sigs/prow repository.

@ti-chi-bot ti-chi-bot bot merged commit 7ce6ec3 into pingcap:master Sep 18, 2024
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants