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

*: remove mock.NewContext() usage when building table meta in production code #56348

Merged
merged 3 commits into from
Sep 29, 2024

Conversation

lcwangchao
Copy link
Collaborator

@lcwangchao lcwangchao commented Sep 26, 2024

What problem does this PR solve?

Issue Number: ref #53388

What changed and how does it work?

This PR removes the mock.Context usage in production codes when building table meta, including:

  • The codes that build system table meta.
  • The codes in lightning to build table meta from create table statement.
  • The codes in cmd/importer

Check List

Tests

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

Use the below test to output the system table's meta in json format:

func TestDumpInitTableInfo(t *testing.T) {
	_, do := testkit.CreateMockStoreAndDomain(t)
	var sb strings.Builder
	is := do.InfoSchema()
	dbNames := is.AllSchemaNames()
	slices.SortFunc(dbNames, func(a, b pmodel.CIStr) int {
		return strings.Compare(a.L, b.L)
	})
	for _, db := range dbNames {
		sb.WriteString("database: " + db.O + "\n")
		infos, err := is.SchemaTableInfos(context.TODO(), db)
		require.NoError(t, err)
		slices.SortFunc(infos, func(a, b *model.TableInfo) int {
			return strings.Compare(a.Name.L, b.Name.L)
		})
		for _, info := range infos {
			// set UpdateTS to 0 to make the output stable.
			info.UpdateTS = 0
			if db.L == "metrics_schema" {
				// metrics_schema's ID is not stable, so we set it to 0.
				info.ID = 0
			}
			str, err := json.MarshalIndent(info, "", "\t")
			require.NoError(t, err)
			sb.WriteString(string(str))
			sb.WriteString("\n")
		}
	}
	require.NoError(t, os.WriteFile("/tmp/meta.json", []byte(sb.String()), 0644))
}

And try to diff the output file in master and this PR, there is no diff. So the behavior is the same when building system table meta.

  • 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. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. sig/planner SIG: Planner labels Sep 26, 2024
Copy link

tiprow bot commented Sep 26, 2024

Hi @lcwangchao. 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.

@@ -62,6 +64,7 @@ func Init() {
c.ID = int64(i) + 1
}
meta.DBID = dbID
meta.State = model.StatePublic
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think the master code lost this line to set the table state to public... Though it still works well without this state, I still fix it to make the meta complete.

Copy link

codecov bot commented Sep 26, 2024

Codecov Report

Attention: Patch coverage is 88.88889% with 2 lines in your changes missing coverage. Please review.

Project coverage is 76.1041%. Comparing base (65d740f) to head (8abc07a).
Report is 1 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #56348        +/-   ##
================================================
+ Coverage   73.3813%   76.1041%   +2.7228%     
================================================
  Files          1623       1645        +22     
  Lines        447183     455176      +7993     
================================================
+ Hits         328149     346408     +18259     
+ Misses        98920      87175     -11745     
- Partials      20114      21593      +1479     
Flag Coverage Δ
integration 51.8855% <88.2352%> (?)
unit 72.5331% <100.0000%> (+0.0537%) ⬆️

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

Components Coverage Δ
dumpling 52.9478% <ø> (ø)
parser ∅ <ø> (∅)
br 63.0417% <ø> (+17.5108%) ⬆️

@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Sep 26, 2024
)

// GetTableInfo returns table information.
func GetTableInfo(ctx context.Context, db dbutil.QueryExecutor, schemaName string, tableName string) (*model.TableInfo, error) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Remove GetTableInfo because it is not used by any code...

Copy link
Contributor

Choose a reason for hiding this comment

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

used by tiflow😓

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Sep 26, 2024

@lcwangchao: 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.

createTableStmt, ok := astNode.(*ast.CreateTableStmt)
if !ok {
return nil, errors.New("cannot transfer the parsed SQL as an CREATE TABLE statement")
}
info, err := ddl.MockTableInfo(sctx, createTableStmt, tableID)
info, err := ddl.BuildTableInfoFromAST(metabuild.NewNonStrictContext(), createTableStmt)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Seems newTableInfo always use 0 as table it and it is not actually used when importing data:

theTableInfo, err := newTableInfo(createTblSQL, 0)

PTAL @lance6716

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Sep 29, 2024
@@ -237,7 +237,8 @@ func parseTable(t *table, stmt *ast.CreateTableStmt) error {
t.name = stmt.Table.Name.L
t.columns = make([]*column, 0, len(stmt.Cols))

mockTbl, err := ddl.MockTableInfo(mock.NewContext(), stmt, 1)
mockTbl, err := ddl.BuildTableInfoFromAST(metabuild.NewNonStrictContext(), stmt)
mockTbl.ID = 1
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 this for?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It seems like an old tool to import data to tidb. But I think no one uses it now...

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 29, 2024
Copy link

ti-chi-bot bot commented Sep 29, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-09-29 02:23:49.062555934 +0000 UTC m=+148784.482768945: ☑️ agreed by lance6716.
  • 2024-09-29 02:41:22.269389494 +0000 UTC m=+149837.689602505: ☑️ agreed by D3Hunter.

@ti-chi-bot ti-chi-bot bot added the approved label Sep 29, 2024
Copy link
Member

@YangKeao YangKeao left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

ti-chi-bot bot commented Sep 29, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: D3Hunter, hawkingrei, lance6716, qw4990, tangenta, YangKeao

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

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Sep 29, 2024

@lcwangchao: 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.

@lance6716
Copy link
Contributor

/retest

Copy link

tiprow bot commented Sep 29, 2024

@lance6716: 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.

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Sep 29, 2024

@lcwangchao: 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.

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Sep 29, 2024

@lcwangchao: 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.

@lcwangchao
Copy link
Collaborator Author

/retest

Copy link

tiprow bot commented Sep 29, 2024

@lcwangchao: 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 8f0baf4 into pingcap:master Sep 29, 2024
23 checks passed
@lcwangchao lcwangchao deleted the removemockctxintablebuild branch September 29, 2024 15:20
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. sig/planner SIG: Planner 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.

7 participants