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

Merged
merged 3 commits into from
May 30, 2024

Conversation

D3Hunter
Copy link
Contributor

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

@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. labels May 29, 2024
Copy link

tiprow bot commented May 29, 2024

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

@@ -112,7 +112,7 @@ func PaginateScanRegion(
var batch []*RegionInfo
batch, err = client.ScanRegions(ctx, scanStartKey, endKey, limit)
if err != nil {
err = errors.Annotatef(berrors.ErrPDBatchScanRegion, "scan regions from start-key:%s, err: %s",
err = errors.Annotatef(berrors.ErrPDBatchScanRegion.Wrap(err), "scan regions from start-key:%s, err: %s",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

make sure we can see context cancel as a error, not a string

Copy link

codecov bot commented May 29, 2024

Codecov Report

Attention: Patch coverage is 76.19048% with 15 lines in your changes are missing coverage. Please review.

Project coverage is 75.0389%. Comparing base (2b68ccf) to head (61bfa6a).
Report is 5 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #53664        +/-   ##
================================================
+ Coverage   74.4613%   75.0389%   +0.5775%     
================================================
  Files          1506       1528        +22     
  Lines        357521     439893     +82372     
================================================
+ Hits         266215     330091     +63876     
- Misses        71899      89150     +17251     
- Partials      19407      20652      +1245     
Flag Coverage Δ
integration 51.4650% <71.4285%> (?)
unit 71.4981% <17.4603%> (-1.8443%) ⬇️

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

Components Coverage Δ
dumpling 53.9957% <ø> (-2.3014%) ⬇️
parser ∅ <ø> (∅)
br 56.7480% <100.0000%> (+13.1993%) ⬆️

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label May 30, 2024
Copy link
Contributor

@GMHDBJD GMHDBJD left a comment

Choose a reason for hiding this comment

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

LGTM

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

ti-chi-bot bot commented May 30, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-05-30 02:25:09.60171102 +0000 UTC m=+2916063.358846592: ☑️ agreed by lance6716.
  • 2024-05-30 03:17:54.340401591 +0000 UTC m=+2919228.097537157: ☑️ agreed by GMHDBJD.

Copy link

ti-chi-bot bot commented May 30, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: GMHDBJD, 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 May 30, 2024
@ti-chi-bot ti-chi-bot bot merged commit 29bf008 into pingcap:master May 30, 2024
36 of 37 checks passed
@D3Hunter D3Hunter deleted the fix53659 branch May 30, 2024 04:18
@GMHDBJD GMHDBJD added the needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. label Jul 15, 2024
@GMHDBJD
Copy link
Contributor

GMHDBJD commented Jul 15, 2024

/run-cherry-pick

@GMHDBJD
Copy link
Contributor

GMHDBJD commented Jul 15, 2024

/run-cherrypick

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jul 15, 2024
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.5: #54618.

@ti-chi-bot ti-chi-bot added the needs-cherry-pick-release-8.1 Should cherry pick this PR to release-8.1 branch. label Jul 30, 2024
ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Jul 30, 2024
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-8.1: #55066.

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-7.5 Should cherry pick this PR to release-7.5 branch. needs-cherry-pick-release-8.1 Should cherry pick this PR to release-8.1 branch. 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

client leak or directory locked when met error when creating local backend
5 participants