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

*: track the memory usage in Insert/Update/Delete executors #34097

Merged
merged 23 commits into from
Jun 22, 2022

Conversation

ekexium
Copy link
Contributor

@ekexium ekexium commented Apr 19, 2022

Signed-off-by: ekexium ekexium@gmail.com

What problem does this PR solve?

Issue Number: close #34096

Problem Summary:

Track the memory usage in update and delete executors.

What is changed and how it works?

Introduce a new generic type MemAwareMap.

goos: darwin
goarch: arm64
pkg: github.com/pingcap/tidb/util/set
BenchmarkMemAwareIntMap
BenchmarkMemAwareIntMap/MemAwareIntMap_1
BenchmarkMemAwareIntMap/MemAwareIntMap_1-10         	850669917	        13.95 ns/op
BenchmarkMemAwareIntMap/MemAwareIntMap_100
BenchmarkMemAwareIntMap/MemAwareIntMap_100-10       	 2034136	      5815 ns/op
BenchmarkMemAwareIntMap/MemAwareIntMap_10000
BenchmarkMemAwareIntMap/MemAwareIntMap_10000-10     	   19460	    610353 ns/op
BenchmarkMemAwareIntMap/MemAwareIntMap_1000000
BenchmarkMemAwareIntMap/MemAwareIntMap_1000000-10   	      94	 122998138 ns/op
BenchmarkNativeIntMap
BenchmarkNativeIntMap/NativeIntMap_1
BenchmarkNativeIntMap/NativeIntMap_1-10             	920470398	        12.93 ns/op
BenchmarkNativeIntMap/NativeIntMap_100
BenchmarkNativeIntMap/NativeIntMap_100-10           	 2082813	      5751 ns/op
BenchmarkNativeIntMap/NativeIntMap_10000
BenchmarkNativeIntMap/NativeIntMap_10000-10         	   20052	    606626 ns/op
BenchmarkNativeIntMap/NativeIntMap_1000000
BenchmarkNativeIntMap/NativeIntMap_1000000-10       	      92	 117214526 ns/op
goos: darwin
goarch: arm64
pkg: github.com/pingcap/tidb/kv
BenchmarkMemAwareHandleMap
BenchmarkMemAwareHandleMap/MemAwareIntMap_1
BenchmarkMemAwareHandleMap/MemAwareIntMap_1-10         	44301274	        27.02 ns/op
BenchmarkMemAwareHandleMap/MemAwareIntMap_100
BenchmarkMemAwareHandleMap/MemAwareIntMap_100-10       	  132794	      8956 ns/op
BenchmarkMemAwareHandleMap/MemAwareIntMap_10000
BenchmarkMemAwareHandleMap/MemAwareIntMap_10000-10     	    1173	   1011384 ns/op
BenchmarkMemAwareHandleMap/MemAwareIntMap_1000000
BenchmarkMemAwareHandleMap/MemAwareIntMap_1000000-10   	       5	 218250775 ns/op
BenchmarkNativeHandleMap
BenchmarkNativeHandleMap/NativeIntMap_1
BenchmarkNativeHandleMap/NativeIntMap_1-10             	44798058	        26.50 ns/op
BenchmarkNativeHandleMap/NativeIntMap_100
BenchmarkNativeHandleMap/NativeIntMap_100-10           	  133512	      8903 ns/op
BenchmarkNativeHandleMap/NativeIntMap_10000
BenchmarkNativeHandleMap/NativeIntMap_10000-10         	    1166	   1009782 ns/op
BenchmarkNativeHandleMap/NativeIntMap_1000000
BenchmarkNativeHandleMap/NativeIntMap_1000000-10       	       5	 238954458 ns/op

Check List

Tests

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

I manually tested delete and update.

explain analyze delete t_3, t_4 from t_3, t_4 where t_3.vip_level = t_4.vip_level

image

+---------------------------+----------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------+----------+---------+
| id                        | estRows  | actRows | task      | access object | execution info                                                                                                                                                                                                                                                                                                            | operator info                                                                | memory   | disk    |
+---------------------------+----------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------+----------+---------+
| Delete_6                  | N/A      | 0       | root      |               | time:9.97s, loops:1                                                                                                                                                                                                                                                                                                       | N/A                                                                          | 454.2 MB | N/A     |
| └─HashJoin_9              | 12487.50 | 2002100 | root      |               | time:161.3ms, loops:1958, build_hash_table:{total:1.88ms, fetch:1.83ms, build:46.8µs}, probe:{concurrency:5, total:38.6s, max:7.76s, probe:37.9s, fetch:675.7ms}                                                                                                                                                          | inner join, equal:[eq(credit_card.t_3.vip_level, credit_card.t_4.vip_level)] | 187.4 KB | 0 Bytes |
|   ├─TableReader_16(Build) | 9990.00  | 10      | root      |               | time:1.43ms, loops:2, cop_task: {num: 1, max: 1.64ms, proc_keys: 10, rpc_num: 1, rpc_time: 1.59ms, copr_cache_hit_ratio: 0.00}                                                                                                                                                                                            | data:Selection_15                                                            | 2.53 KB  | N/A     |
|   │ └─Selection_15        | 9990.00  | 10      | cop[tikv] |               | tikv_task:{time:0s, loops:1}, scan_detail: {total_process_keys: 10, total_process_keys_size: 1980, total_keys: 91, rocksdb: {delete_skipped_count: 0, key_skipped_count: 90, block: {cache_hit_count: 7, read_count: 1, read_byte: 1.15 KB}}}                                                                             | not(isnull(credit_card.t_4.vip_level))                                       | N/A      | N/A     |
|   │   └─TableFullScan_14  | 10000.00 | 10      | cop[tikv] | table:t_4     | tikv_task:{time:0s, loops:1}                                                                                                                                                                                                                                                                                              | keep order:false, stats:pseudo                                               | N/A      | N/A     |
|   └─TableReader_13(Probe) | 9990.00  | 200210  | root      |               | time:137.7ms, loops:198, cop_task: {num: 8, max: 630.6ms, min: 1.7ms, avg: 255.5ms, p95: 630.6ms, max_proc_keys: 175563, p95_proc_keys: 175563, tot_proc: 1.88s, rpc_num: 8, rpc_time: 2.04s, copr_cache_hit_ratio: 0.00}                                                                                                 | data:Selection_12                                                            | 42.3 MB  | N/A     |
|     └─Selection_12        | 9990.00  | 200210  | cop[tikv] |               | tikv_task:{proc max:416ms, min:0s, p80:379ms, p95:416ms, iters:211, tasks:8}, scan_detail: {total_process_keys: 200210, total_process_keys_size: 39864813, total_keys: 3376831, rocksdb: {delete_skipped_count: 200210, key_skipped_count: 3577033, block: {cache_hit_count: 9221, read_count: 450, read_byte: 6.98 MB}}} | not(isnull(credit_card.t_3.vip_level))                                       | N/A      | N/A     |
|       └─TableFullScan_11  | 10000.00 | 200210  | cop[tikv] | table:t_3     | tikv_task:{proc max:416ms, min:0s, p80:379ms, p95:416ms, iters:211, tasks:8}                                                                                                                                                                                                                                              | keep order:false, stats:pseudo                                               | N/A      | N/A     |
+---------------------------+----------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------+----------+---------+

update `T_CUSTOMER` set purse_num = purse_num + 1 where vip_level = '0'

image

mysql> explain analyze update `T_CUSTOMER` set purse_num = purse_num + 1 where vip_level = '0'
    -> ;
+---------------------------+--------------+-----------+-----------+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------+----------+------+
| id                        | estRows      | actRows   | task      | access object    | execution info                                                                                                                                                                                                                                                                                                                          | operator info                             | memory   | disk |
+---------------------------+--------------+-----------+-----------+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------+----------+------+
| Update_4                  | N/A          | 0         | root      |                  | time:5m49.4s, loops:1,                                                                                                                                                                                                                                                                                                                  | N/A                                       | 384.3 MB | N/A  |
| └─TableReader_8       | 19874190.00  | 20002899  | root      |                  | time:5.55s, loops:19470, cop_task: {num: 237, max: 1.86s, min: 3.02ms, avg: 866.3ms, p95: 1.55s, max_proc_keys: 542441, p95_proc_keys: 542252, tot_proc: 3m9.4s, tot_wait: 429ms, rpc_num: 237, rpc_time: 3m25.3s, copr_cache_hit_ratio: 0.00}                                                                                          | data:Selection_7                          | 337.7 MB | N/A  |
|   └─Selection_7       | 19874190.00  | 20002899  | cop[tikv] |                  | tikv_task:{proc max:1.59s, min:0s, p80:1.04s, p95:1.32s, iters:98765, tasks:237}, scan_detail: {total_process_keys: 100000000, total_process_keys_size: 19913721288, total_keys: 100000237, rocksdb: {delete_skipped_count: 0, key_skipped_count: 100000000, block: {cache_hit_count: 225344, read_count: 100338, read_byte: 2.63 GB}}} | eq(credit_card.t_customer.vip_level, "0") | N/A      | N/A  |
|     └─TableFullScan_6 | 100000000.00 | 100000000 | cop[tikv] | table:T_CUSTOMER | tikv_task:{proc max:1.53s, min:0s, p80:996ms, p95:1.28s, iters:98765, tasks:237}                                                                                                                                                                                                                                                        | keep order:false                          | N/A      | N/A  |
+---------------------------+--------------+-----------+-----------+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------+----------+------+
4 rows in set (5 min 49.35 sec)

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
Copy link
Member

ti-chi-bot commented Apr 19, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • XuHuaiyu
  • wshwsh12

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@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. labels Apr 19, 2022
Signed-off-by: ekexium <ekexium@gmail.com>
@sre-bot
Copy link
Contributor

sre-bot commented Apr 19, 2022

@ekexium ekexium force-pushed the update-memtrack branch 2 times, most recently from e3e9a91 to 3efd06e Compare April 21, 2022 12:53
Signed-off-by: ekexium <ekexium@gmail.com>
@ekexium
Copy link
Contributor Author

ekexium commented Apr 28, 2022

/run-check_dev

@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label May 11, 2022
kv/key.go Outdated Show resolved Hide resolved
kv/key.go Outdated Show resolved Hide resolved
util/set/mem_aware_map.go Outdated Show resolved Hide resolved
Signed-off-by: ekexium <ekexium@gmail.com>
@ekexium
Copy link
Contributor Author

ekexium commented May 24, 2022

/run-check_dev

1 similar comment
@ekexium
Copy link
Contributor Author

ekexium commented May 24, 2022

/run-check_dev

Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
Signed-off-by: ekexium <ekexium@gmail.com>
@ekexium ekexium changed the title *: track the memory usage of handle maps in update exec *: track the memory usage in Insert/Update/Delete executors May 27, 2022
Signed-off-by: ekexium <ekexium@gmail.com>

fix tblRowMap

Signed-off-by: ekexium <ekexium@gmail.com>

try

Signed-off-by: ekexium <ekexium@gmail.com>

try again

Signed-off-by: ekexium <ekexium@gmail.com>

try again

Signed-off-by: ekexium <ekexium@gmail.com>
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jun 1, 2022
@ekexium
Copy link
Contributor Author

ekexium commented Jun 1, 2022

Please take another look @wshwsh12

hawkingrei and others added 2 commits June 9, 2022 16:49
Signed-off-by: ekexium <ekexium@fastmail.com>
executor/delete.go Show resolved Hide resolved
executor/delete.go Outdated Show resolved Hide resolved
executor/update.go Outdated Show resolved Hide resolved
executor/update.go Outdated Show resolved Hide resolved
kv/key.go Show resolved Hide resolved
kv/key.go Outdated Show resolved Hide resolved
util/set/mem_aware_map.go Show resolved Hide resolved
util/hack/hack.go Outdated Show resolved Hide resolved
Signed-off-by: ekexium <ekexium@fastmail.com>
@wshwsh12 wshwsh12 self-requested a review June 10, 2022 07:54
Signed-off-by: ekexium <ekexium@fastmail.com>
@ti-chi-bot ti-chi-bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 13, 2022
Copy link
Contributor

@wshwsh12 wshwsh12 left a comment

Choose a reason for hiding this comment

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

LGTM

util/set/mem_aware_map.go Outdated Show resolved Hide resolved
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jun 13, 2022
Signed-off-by: ekexium <ekexium@fastmail.com>
// See the License for the specific language governing permissions and
// limitations under the License.

package set
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add an individual package like map or tiMap to put these files?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Map and set are highly related. And map is reserved so I think leaving them in set is fine.

kv/key.go Outdated

// ExtraMemSize implements the Handle interface.
func (ch *CommonHandle) ExtraMemSize() uint64 {
return uint64(len(ch.encoded) + len(ch.colEndOffsets)*2)
Copy link
Contributor

Choose a reason for hiding this comment

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

1.cap(ch.encoded) and cap(ch.colEndOffsets)?
2. Why do we need to *2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

colEndOffsets is an array of uint16.

kv/key.go Outdated Show resolved Hide resolved
kv/key.go Outdated
@@ -158,9 +159,15 @@ type Handle interface {
// String implements the fmt.Stringer interface.
String() string
// MemUsage returns the memory usage of a handle.
MemUsage() int64
MemUsage() uint64
// ExtraMemSize returns the dynamic size of memory occupied by the handle, e.g. slices.
Copy link
Contributor

Choose a reason for hiding this comment

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

What does dynamic mean? How do we use this func?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

dynamic means the part of memory is not a fixed number, i.e. slices or pointers. Do you have any suggestions on improving the comment?
We use it with the mem-aware map, since the map only counts the actual size of the keys and values, but doesn't contain the objects that are referenced by pointers.

return x
}

func BenchmarkMemAwareIntMap(b *testing.B) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This benchmark may be not convincing, I think we'd better add a microbenchmark for the MemAwareHandleMap.

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've updated results in the description.

Signed-off-by: ekexium <ekexium@fastmail.com>
Signed-off-by: ekexium <ekexium@fastmail.com>
@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jun 22, 2022
@XuHuaiyu
Copy link
Contributor

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 6b154a9

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jun 22, 2022
@ti-chi-bot ti-chi-bot merged commit 95e13af into pingcap:master Jun 22, 2022
@sre-bot
Copy link
Contributor

sre-bot commented Jun 22, 2022

TiDB MergeCI notify

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci/integration-cdc-test 🟢 all 35 tests passed 31 min Existing passed
idc-jenkins-ci-tidb/integration-common-test 🟢 all 11 tests passed 15 min Existing passed
idc-jenkins-ci-tidb/common-test 🟢 all 12 tests passed 9 min 38 sec Existing passed
idc-jenkins-ci-tidb/integration-ddl-test 🟢 all 6 tests passed 8 min 21 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-2 🟢 all 28 tests passed 6 min 9 sec Existing passed
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 5 min 27 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 5 min 16 sec Existing passed
idc-jenkins-ci-tidb/integration-compatibility-test 🟢 all 1 tests passed 3 min 44 sec Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 3 min 21 sec Existing passed
idc-jenkins-ci-tidb/plugin-test 🟢 build success, plugin test success 4min Existing passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The memory consumed by UpdateExec.updatedRowKeys and UpdateExec.mergedRowData is not tracked
6 participants