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

localbackend: fix resource leak when err on new local backend (#53664) #55066

Merged

Conversation

ti-chi-bot
Copy link
Member

This is an automated cherry-pick of #53664

What problem does this PR solve?

Issue Number: close #53659

Problem Summary:

What changed and how does it work?

  • close them on error

Check List

Tests

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

i think it's too much to add so many failpoints to just test resource closed on err, so i write the test here

apply this diff to the code

diff --git a/pkg/lightning/backend/local/local.go b/pkg/lightning/backend/local/local.go
index f232d71d79..1d91833fa8 100644
--- a/pkg/lightning/backend/local/local.go
+++ b/pkg/lightning/backend/local/local.go
@@ -560,12 +560,14 @@ func NewBackend(
 		// the interface `ScatterRegions` may time out.
 		pd.WithCustomTimeoutOption(60*time.Second),
 	)
+	failpoint.InjectCall("failedToCreatePDClient", &err)
 	if err != nil {
 		return nil, common.NormalizeOrWrapErr(common.ErrCreatePDClient, err)
 	}
 
 	// The following copies tikv.NewTxnClient without creating yet another pdClient.
 	spkv, err = tikvclient.NewEtcdSafePointKV(strings.Split(config.PDAddr, ","), tls.TLSConfig())
+	failpoint.InjectCall("failedToCreateSafePointKV", &err)
 	if err != nil {
 		return nil, common.ErrCreateKVClient.Wrap(err).GenWithStackByArgs()
 	}
@@ -582,6 +584,7 @@ func NewBackend(
 	tikvCodec := pdCliForTiKV.GetCodec()
 	rpcCli = tikvclient.NewRPCClient(tikvclient.WithSecurity(tls.ToTiKVSecurityConfig()), tikvclient.WithCodec(tikvCodec))
 	tikvCli, err = tikvclient.NewKVStore("lightning-local-backend", pdCliForTiKV, spkv, rpcCli)
+	failpoint.InjectCall("failedToCreateKVStore", &err)
 	if err != nil {
 		return nil, common.ErrCreateKVClient.Wrap(err).GenWithStackByArgs()
 	}
@@ -594,6 +597,7 @@ func NewBackend(
 	importClientFactory = newImportClientFactoryImpl(splitCli, tls, config.MaxConnPerStore, config.ConnCompressType)
 
 	multiIngestSupported, err = checkMultiIngestSupport(ctx, pdCli, importClientFactory)
+	failpoint.InjectCall("failedToCheckMultiIngestSupport", &err)
 	if err != nil {
 		return nil, common.ErrCheckMultiIngest.Wrap(err).GenWithStackByArgs()
 	}

then in real-tikv-test which have routine leak check, add this:

func TestLocalBackendCleanOnErr(t *testing.T) {
	for _, fp := range []string{
		"failedToCreatePDClient",
		"failedToCreateSafePointKV",
		"failedToCreateKVStore",
		"failedToCheckMultiIngestSupport",
	} {
		t.Run(fp, func(t *testing.T) {
			fpName := "github.com/pingcap/tidb/pkg/lightning/backend/local/" + fp
			testfailpoint.EnableCall(t, fpName,
				func(errP *error) {
					*errP = fmt.Errorf("mock error" + fp)
				},
			)
			t.Cleanup(func() {
				require.NoError(t, failpoint.Disable(fpName))
			})
			_, err := local.NewBackend(context.Background(), &common.TLS{}, local.BackendConfig{
				PDAddr:                 "127.0.0.1:2379",
				RegionSplitBatchSize:   10,
				RegionSplitConcurrency: 1,
				MaxConnPerStore:        1,
				ConnCompressType:       config.CompressionNone,
			}, nil)
			require.ErrorContains(t, err, "mock error"+fp)
		})
	}
}
  • 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

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot ti-chi-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. type/cherry-pick-for-release-8.1 This PR is cherry-picked to release-8.1 from a source PR. labels Jul 30, 2024
@ti-chi-bot ti-chi-bot added the cherry-pick-approved Cherry pick PR approved by release team. label Jul 30, 2024
@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Jul 30, 2024
@D3Hunter
Copy link
Contributor

@lance6716

Copy link

codecov bot commented Jul 30, 2024

Codecov Report

Attention: Patch coverage is 16.94915% with 49 lines in your changes missing coverage. Please review.

Please upload report for BASE (release-8.1@cfd1d3a). Learn more about missing BASE report.

Additional details and impacted files
@@               Coverage Diff                @@
##             release-8.1     #55066   +/-   ##
================================================
  Coverage               ?   71.1775%           
================================================
  Files                  ?       1465           
  Lines                  ?     421973           
  Branches               ?          0           
================================================
  Hits                   ?     300350           
  Misses                 ?     101115           
  Partials               ?      20508           
Flag Coverage Δ
unit 71.1775% <16.9491%> (?)

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

Components Coverage Δ
dumpling 53.9957% <0.0000%> (?)
parser ∅ <0.0000%> (?)
br 41.0760% <0.0000%> (?)

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

ti-chi-bot bot commented Jul 30, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-07-30 10:35:23.035721916 +0000 UTC m=+265639.315769985: ☑️ agreed by D3Hunter.
  • 2024-07-30 13:00:27.642297612 +0000 UTC m=+274343.922345673: ☑️ agreed by lance6716.

Copy link

ti-chi-bot bot commented Jul 31, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: D3Hunter, lance6716, Leavrth

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 the approved label Jul 31, 2024
@ti-chi-bot ti-chi-bot bot merged commit 04f4e2b into pingcap:release-8.1 Jul 31, 2024
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved cherry-pick-approved Cherry pick PR approved by release team. lgtm 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. type/cherry-pick-for-release-8.1 This PR is cherry-picked to release-8.1 from a source PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants