Skip to content

[fix](mtmv) reuse Gson instance in MTMVTask.getTvfInfo to avoid Master FE CPU spike - #66792

Open
xy720 wants to merge 1 commit into
apache:masterfrom
xy720:fix-mtmv-tvf-gson-cpu-1
Open

[fix](mtmv) reuse Gson instance in MTMVTask.getTvfInfo to avoid Master FE CPU spike#66792
xy720 wants to merge 1 commit into
apache:masterfrom
xy720:fix-mtmv-tvf-gson-cpu-1

Conversation

@xy720

@xy720 xy720 commented Aug 14, 2026

Copy link
Copy Markdown
Member

What problem does this PR solve?

Problem Summary:

Related PR: (#28566)

防止在集群有大量物化视图刷新任务的场景下,tasks('type'='mv') TVF 调用 将 Master Cpu 打满。
复用Gson对象可以防止getAdapter的重复调用

200个刷新任务(包括pending)+ 30s 一次的tasks('type'='mv') TVF 调用,32cFE效果就可能达到如下

Clipboard_Screenshot_1786726026 Clipboard_Screenshot_1786727546

Release note

None

Check List (For Author)

  • Test

    • Regression test
    • Unit Test
    • Manual test (add detailed scripts or steps below)
    • No need to test or manual test. Explain why:
      • This is a refactor/code format and no logic has been changed.
      • Previous test can cover this change.
      • No code files have been changed.
      • Other reason
  • Behavior changed:

    • No.
    • Yes.
  • Does this need documentation?

    • No.
    • Yes.

Check List (For Reviewer who merge this PR)

  • Confirm the release note
  • Confirm test cases
  • Confirm document
  • Add branch pick label

@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@xy720

xy720 commented Aug 14, 2026

Copy link
Copy Markdown
Member Author

run buildall

@xy720

xy720 commented Aug 14, 2026

Copy link
Copy Markdown
Member Author

/review

@xy720 xy720 changed the title [fix](mtmv) reuse Gson instance in MTMVTask.getTvfInfo to avoid Master FE CPU spike- #66791 [fix](mtmv) reuse Gson instance in MTMVTask.getTvfInfo to avoid Master FE CPU spike Aug 14, 2026
@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-H: Total hot run time: 17593 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit a72b0346d353b7ea6ea5a232480ffc47099651f1, data reload: false

------ Round 1 ----------------------------------
============================================
q1	17548	3041	3037	3037
q2	1932	229	152	152
q3	10431	888	521	521
q4	4669	247	203	203
q5	7673	553	386	386
q6	134	134	93	93
q7	530	516	382	382
q8	9230	917	927	917
q9	3451	2374	2364	2364
q10	6511	880	727	727
q11	441	254	243	243
q12	681	392	323	323
q13	17860	1887	1566	1566
q14	163	157	140	140
q15	q16	441	407	368	368
q17	788	739	786	739
q18	3166	2329	2255	2255
q19	1205	927	763	763
q20	654	529	439	439
q21	5490	1741	1874	1741
q22	333	282	234	234
Total cold run time: 93331 ms
Total hot run time: 17593 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	3426	3339	3325	3325
q2	217	227	167	167
q3	2310	2340	2189	2189
q4	1196	1180	893	893
q5	2201	2182	2155	2155
q6	173	121	86	86
q7	1081	947	842	842
q8	1610	1403	1395	1395
q9	3092	3080	3074	3074
q10	1867	1798	1613	1613
q11	352	266	248	248
q12	458	434	344	344
q13	1828	1838	1571	1571
q14	177	194	158	158
q15	q16	400	406	361	361
q17	1036	1023	1024	1023
q18	5030	4487	4819	4487
q19	893	842	830	830
q20	964	949	842	842
q21	3923	3141	3273	3141
q22	474	395	318	318
Total cold run time: 32708 ms
Total hot run time: 29062 ms

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review opinion: looks good. I found no blocking issue and no inline issue to raise.

Critical checkpoint conclusions:

  • Goal and proof: tasks('type'='mv') calls MTMVTask.getTvfInfo once per visible task. This change removes up to three Gson constructions per emitted task row and reuses the resulting reflective adapters, directly addressing the reported Master FE CPU hotspot. Existing MTMVTaskTest and test_task_mtmv.groovy exercise getTvfInfo / the MV tasks TVF, although this PR adds no targeted concurrency test or performance benchmark.
  • Scope and clarity: the patch is minimal and focused: one private static final serializer plus the three replacements in the sole MTMV task-row producer. The default new Gson() configuration and each existing null branch remain unchanged, so observable JSON formatting and null behavior are preserved.
  • Concurrency: concurrent metadata RPC workers can share this instance. Gson 2.10.1 explicitly supports cross-thread instance reuse; its implementation uses thread-local in-progress adapter construction, a concurrent adapter cache, and a separate writer for every toJson call. The serializers reached here are stateless. completedPartitions is a CopyOnWriteArrayList, and the change introduces no new task-state race, lock, lock-order, or deadlock risk. See the Gson 2.10.1 implementation.
  • Lifecycle and memory: JVM class initialization safely publishes the static final instance. The adapter cache is bounded by a small fixed population of context/list/string/enum types, retains no task instances, and has no static-initialization cycle.
  • Configuration, compatibility, and parallel paths: no config, schema, thrift, storage, persisted-state, function-symbol, or FE/BE variable change is involved, so rolling-upgrade behavior is unchanged. All three plain-Gson allocations in this MTMV hot path were replaced; other task types use separate row producers and are not part of this workload.
  • Conditions, errors, and observability: the existing null conditions and error behavior are untouched. The patch creates no new failure path, distributed operation, or state transition requiring extra logging, metrics, or error context.
  • Transactions and data writes: not applicable; the change only serializes task metadata for display and does not alter transactions, edit logs, failover behavior, or data writes.
  • Tests and results: no test or result file is changed. Existing coverage reaches the affected API, but does not quantify allocation/CPU improvement. I did not run builds or tests because this runner's review instructions prohibit them; correctness was checked from the full call path and Gson 2.10.1's concurrency contract and implementation.
  • Performance and remaining issues: repeated constructor/factory-list allocation and repeated adapter discovery are removed without adding synchronization or per-row work. I found no additional correctness, lifecycle, compatibility, performance, or coverage defect requiring a review comment.

User focus: no additional review focus was provided; the complete PR was reviewed.

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 85966 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
TPC-DS sf100 test result on commit a72b0346d353b7ea6ea5a232480ffc47099651f1, data reload: false

query5	4258	406	326	326
query6	412	169	165	165
query7	4858	446	261	261
query8	287	120	123	120
query9	8691	2898	2898	2898
query10	405	256	235	235
query11	5378	1067	918	918
query12	119	75	71	71
query13	1228	463	329	329
query14	5940	1992	1865	1865
query14_1	1780	1758	1754	1754
query15	169	123	113	113
query16	903	361	362	361
query17	776	427	349	349
query18	2322	335	232	232
query19	163	133	104	104
query20	70	68	68	68
query21	206	122	104	104
query22	5398	5339	5202	5202
query23	7465	6907	6786	6786
query23_1	6849	6824	6467	6467
query24	7286	1088	802	802
query24_1	790	806	800	800
query25	452	312	270	270
query26	1256	248	169	169
query27	2731	454	284	284
query28	4626	1510	1520	1510
query29	940	453	354	354
query30	290	180	157	157
query31	974	665	607	607
query32	104	51	52	51
query33	469	219	176	176
query34	978	847	493	493
query35	417	405	343	343
query36	551	542	531	531
query37	127	85	77	77
query38	1019	850	813	813
query39	528	551	539	539
query39_1	505	508	530	508
query40	232	129	117	117
query41	59	58	57	57
query42	78	76	76	76
query43	247	254	217	217
query44	1017	588	584	584
query45	112	105	100	100
query46	812	876	542	542
query47	986	1008	977	977
query48	331	336	234	234
query49	542	254	205	205
query50	826	338	266	266
query51	8115	8106	8058	8058
query52	75	72	60	60
query53	203	218	169	169
query54	231	176	180	176
query55	77	57	54	54
query56	209	216	216	216
query57	663	660	618	618
query58	242	201	185	185
query59	1144	1103	1004	1004
query60	249	224	197	197
query61	108	110	108	108
query62	357	206	172	172
query63	185	156	161	156
query64	2614	674	533	533
query65	1604	1560	1572	1560
query66	1799	308	241	241
query67	10160	9912	9881	9881
query68	2727	1268	768	768
query69	356	222	191	191
query70	652	599	574	574
query71	287	250	255	250
query72	2292	1788	1538	1538
query73	651	631	332	332
query74	1580	1224	1144	1144
query75	1238	1161	1018	1018
query76	2279	761	543	543
query77	248	255	204	204
query78	5227	4863	4546	4546
query79	1875	809	596	596
query80	1156	384	347	347
query81	479	194	174	174
query82	638	129	109	109
query83	317	255	232	232
query84	308	126	102	102
query85	840	452	385	385
query86	390	171	169	169
query87	1012	975	906	906
query88	2803	2133	2152	2133
query89	319	224	206	206
query90	1870	141	147	141
query91	152	138	122	122
query92	48	45	44	44
query93	1414	1210	834	834
query94	618	265	214	214
query95	644	428	339	339
query96	804	588	286	286
query97	1062	1073	1012	1012
query98	144	140	132	132
query99	412	354	312	312
Total cold run time: 179314 ms
Total hot run time: 85966 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
ClickBench: Total hot run time: 14.81 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit a72b0346d353b7ea6ea5a232480ffc47099651f1, data reload: false

query1	0.01	0.01	0.01
query2	0.08	0.04	0.04
query3	0.25	0.12	0.10
query4	1.60	0.10	0.08
query5	0.17	0.15	0.17
query6	1.25	0.70	0.69
query7	0.03	0.00	0.01
query8	0.04	0.03	0.03
query9	0.29	0.21	0.22
query10	0.35	0.34	0.36
query11	0.16	0.12	0.11
query12	0.14	0.12	0.12
query13	0.31	0.30	0.30
query14	0.48	0.47	0.47
query15	0.36	0.36	0.35
query16	0.23	0.23	0.24
query17	0.64	0.76	0.69
query18	0.18	0.17	0.16
query19	1.21	1.21	1.14
query20	0.02	0.01	0.01
query21	15.47	0.15	0.11
query22	5.06	0.05	0.05
query23	16.16	0.25	0.10
query24	3.00	0.32	0.26
query25	0.10	0.04	0.04
query26	0.73	0.17	0.12
query27	0.04	0.04	0.03
query28	3.66	0.60	0.27
query29	12.43	3.19	2.55
query30	0.26	0.11	0.13
query31	2.77	0.38	0.17
query32	3.53	0.32	0.23
query33	1.40	1.42	1.52
query34	15.37	2.27	1.82
query35	1.75	1.79	1.76
query36	0.46	0.29	0.30
query37	0.06	0.04	0.05
query38	0.05	0.03	0.03
query39	0.04	0.02	0.03
query40	0.12	0.08	0.08
query41	0.09	0.03	0.03
query42	0.03	0.03	0.03
query43	0.03	0.02	0.02
Total cold run time: 90.41 s
Total hot run time: 14.81 s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants