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

Lightning: Memory Leak on Large Source Files #39331

Closed
dsdashun opened this issue Nov 23, 2022 · 4 comments · Fixed by #39332
Closed

Lightning: Memory Leak on Large Source Files #39331

dsdashun opened this issue Nov 23, 2022 · 4 comments · Fixed by #39332
Assignees
Labels
affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 component/lightning This issue is related to Lightning of TiDB. severity/major type/bug The issue is confirmed as a bug.

Comments

@dsdashun
Copy link
Contributor

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

  1. Prepare a large CSV file as the data source
  2. Create the Lightning task with strict-mode set to false
  3. Observe the memory usage of Lightning

2. What did you expect to see? (Required)

The memory usage should be stable, even if the source data is large, because Lightning is streaming-processing the data.

3. What did you see instead (Required)

The memory of Lightning process is increasing continuously. I suspect there might be memory leaks.

4. What is your TiDB version? (Required)

latest version

@dsdashun dsdashun added the type/bug The issue is confirmed as a bug. label Nov 23, 2022
@dsdashun
Copy link
Contributor Author

/component lightning

@ti-chi-bot ti-chi-bot added the component/lightning This issue is related to Lightning of TiDB. label Nov 23, 2022
@dsdashun
Copy link
Contributor Author

/severity major

@ti-chi-bot ti-chi-bot added severity/major may-affects-4.0 This bug maybe affects 4.0.x versions. may-affects-5.0 This bug maybe affects 5.0.x versions. may-affects-5.1 This bug maybe affects 5.1.x versions. may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.0 may-affects-6.1 may-affects-6.2 may-affects-6.3 may-affects-6.4 labels Nov 23, 2022
@dsdashun
Copy link
Contributor Author

Captured the heap profile:
70b532d9-d298-45cc-a840-f3b39e103d2c
ec91d092-1bad-45c4-8d0e-08238ea2644a

@dsdashun
Copy link
Contributor Author

I've got the memory leak point. It's in here:

if len(mb.availableBufs) > 0 && mb.availableBufs[0].cap >= size {

When the kvMemBuf tries to allocate buffer, it only checks the first byte buffer's capacity in the available byte buffer pool. If the capacity is less than the allocating size, it'll create a new byte buffer, which will allocate a new pieces of memory using manual.New().
Since all the byte buffers are recycled by putting back into the available byte buffer pool, this means, each time an allocation with a size bigger than the first byte buffer's capacity, a new byte buffer will be generated with extra memory allocated, and those memory will not be freed until the chunk restore is finished.
However, when strict-mode is set to false, the large file is treated as a single chunk. The chunk restore process cannot finish in a short period of time, and the byte buffers are accumulating more and more, resulting in memory leak and potential OOM.

@dsdashun dsdashun mentioned this issue Nov 23, 2022
12 tasks
@dsdashun dsdashun removed may-affects-4.0 This bug maybe affects 4.0.x versions. may-affects-5.1 This bug maybe affects 5.1.x versions. may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-5.0 This bug maybe affects 5.0.x versions. may-affects-6.0 may-affects-6.1 may-affects-6.2 may-affects-6.3 labels Nov 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 component/lightning This issue is related to Lightning of TiDB. severity/major type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants