Skip to content

Commit 3962b55

Browse files
committed
[feature](nereids) HBO: persist pinned statistics to internal db table
### What problem does this PR solve? Problem Summary: 手动注入(HBO SET/DELETE STATISTICS)的 pinned 统计此前只存于 单 FE 内存,FE 重启即丢失。本次将 pinned 条目落盘到 internal db 表 `__internal_schema.hbo_statistics`,由 `Config.hbo_persist_pinned_to_internal_db` (default false,hot-mutable)控制: - 模型:内存(memory)仍权威,读侧只走 HboPlanStatisticsManager 的 Caffeine LRU; SET/DELETE 同步 write-through(尽力而为,失败仅记 WARN 不阻断命令); 表在首次使用时由 `CREATE TABLE IF NOT EXISTS` 自动创建,FE 重启后经 `ensurePinnedLoaded()` 惰性全量加载(hboPinnedLoaded 标志 + 失败重试)。 - 存储列:UNIQUE KEY(fingerprint varchar(64));row_count bigint(rows 为保留字)、 node_type varchar(1024) NULL、struct_info varchar(65533) NULL(超长截断)、 create_time_ms bigint;HASH 1 bucket、replication_num=1,与 column_statistics 等共用 __internal_schema(FeConstants.enableInternalSchemaDb 门控)。 - SQL 经 StatisticsUtil.execUpdate/execStatisticQuery 以 internal context 执行, 字符串字面量统一单引号转义。 - 验证:手工全链路 SET -> 内库行存在 -> FE 重启后 explain 翻转仍生效(日志 "loaded N hbo pinned statistics entries from internal table")-> DELETE 清行恢复; 新回归 hbo_persist_internal_db_test 热开配置断言 SET 写行 / DELETE 清行并在 finally 还原;既有 hbo_structinfo_inject_test 仍通过。 ### Release note None(实验特性内部行为,默认关闭) ### Check List (For Author) - Test: FE 构建通过(checkstyle 0 违规);回归 hbo_structinfo_inject_test 与 hbo_persist_internal_db_test 均 Test 1 suites, failed 0;手工验证重启恢复 - Behavior changed: No(默认 false 保持原内存行为) - Does this need documentation: No
1 parent 0d5ca4d commit 3962b55

5 files changed

Lines changed: 225 additions & 2 deletions

File tree

‎fe/fe-common/src/main/java/org/apache/doris/common/Config.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,6 +2329,17 @@ public class Config extends ConfigBase {
23292329
+ "recently used entry. Takes effect at FE start (not hot-mutable).")
23302330
public static int hbo_pinned_stats_cache_num = 5000;
23312331

2332+
/**
2333+
* Persist manually injected (pinned) hbo statistics into the internal database table
2334+
* __internal_schema.hbo_statistics, so that they survive FE restarts. In-memory pinned
2335+
* statistics stay authoritative; changes are written through synchronously (best effort)
2336+
* and the table is loaded lazily on first use.
2337+
*/
2338+
@ConfField(mutable = true, description = "The default setting is false. When true, HBO SET/DELETE STATISTICS "
2339+
+ "entries are persisted into __internal_schema.hbo_statistics and reloaded after FE "
2340+
+ "restart.")
2341+
public static boolean hbo_persist_pinned_to_internal_db = false;
2342+
23322343
/**
23332344
* Maximum number of events to poll in each RPC.
23342345
*/

‎fe/fe-core/src/main/java/org/apache/doris/nereids/stats/HboPlanStatisticsManager.java‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
package org.apache.doris.nereids.stats;
1919

2020
import org.apache.doris.common.Config;
21+
import org.apache.doris.common.FeConstants;
2122

2223
import com.github.benmanes.caffeine.cache.Cache;
2324
import com.github.benmanes.caffeine.cache.Caffeine;
25+
import org.apache.logging.log4j.LogManager;
26+
import org.apache.logging.log4j.Logger;
2427

2528
import java.util.Collections;
2629
import java.util.Map;
@@ -35,11 +38,16 @@
3538
* and are never overwritten or evicted by the automatic profile-based collection.
3639
*/
3740
public class HboPlanStatisticsManager {
41+
private static final Logger LOG = LogManager.getLogger(HboPlanStatisticsManager.class);
42+
3843
private HboPlanStatisticsProvider hboPlanStatisticsProvider;
3944
private HboPlanInfoProvider hboPlanInfoProvider;
4045
private final Cache<String, PinnedHboStatistics> pinnedPlanStatistics = Caffeine.newBuilder()
4146
.maximumSize(Math.max(Config.hbo_pinned_stats_cache_num, 0))
4247
.build();
48+
// whether pinned statistics have been loaded from the internal table (only when persistence
49+
// is enabled); single-threaded planner/command access makes this flag safe enough
50+
private volatile boolean hboPinnedLoaded = false;
4351

4452
public HboPlanStatisticsManager() {
4553
hboPlanStatisticsProvider = new MemoryHboPlanStatisticsProvider();
@@ -71,12 +79,16 @@ public void putPinnedPlanStatistics(String fingerprint, long rows, String nodeTy
7179
public void putPinnedPlanStatistics(String fingerprint, long rows, String nodeType, String structCanonical) {
7280
// LRU bounded by Config.hbo_pinned_stats_cache_num; pinned entries are otherwise never
7381
// expired automatically and are only removed by HBO DELETE STATISTICS or eviction
82+
long createTimeMs = System.currentTimeMillis();
7483
pinnedPlanStatistics.put(fingerprint,
75-
new PinnedHboStatistics(fingerprint, rows, nodeType, structCanonical,
76-
System.currentTimeMillis()));
84+
new PinnedHboStatistics(fingerprint, rows, nodeType, structCanonical, createTimeMs));
85+
if (Config.hbo_persist_pinned_to_internal_db && FeConstants.enableInternalSchemaDb) {
86+
HboStatisticsStore.persist(fingerprint, rows, nodeType, structCanonical, createTimeMs);
87+
}
7788
}
7889

7990
public Optional<PinnedHboStatistics> getPinnedPlanStatistics(String fingerprint) {
91+
ensurePinnedLoaded();
8092
return Optional.ofNullable(pinnedPlanStatistics.getIfPresent(fingerprint));
8193
}
8294

@@ -86,12 +98,35 @@ public Optional<PinnedHboStatistics> getPinnedPlanStatistics(String fingerprint)
8698
*/
8799
public void removePinnedPlanStatistics(String fingerprint) {
88100
pinnedPlanStatistics.invalidate(fingerprint);
101+
if (Config.hbo_persist_pinned_to_internal_db && FeConstants.enableInternalSchemaDb) {
102+
HboStatisticsStore.delete(fingerprint);
103+
}
89104
}
90105

91106
public Map<String, PinnedHboStatistics> getAllPinnedPlanStatistics() {
107+
ensurePinnedLoaded();
92108
return Collections.unmodifiableMap(pinnedPlanStatistics.asMap());
93109
}
94110

111+
private void ensurePinnedLoaded() {
112+
if (hboPinnedLoaded || !Config.hbo_persist_pinned_to_internal_db
113+
|| !FeConstants.enableInternalSchemaDb) {
114+
return;
115+
}
116+
try {
117+
for (PinnedHboStatistics pinned : HboStatisticsStore.loadAll()) {
118+
pinnedPlanStatistics.put(pinned.getFingerprint(), pinned);
119+
}
120+
hboPinnedLoaded = true;
121+
LOG.info("loaded {} hbo pinned statistics entries from internal table",
122+
pinnedPlanStatistics.asMap().size());
123+
} catch (Throwable t) {
124+
// table may not be ready yet (e.g. internal schema initialization in progress);
125+
// retry on the next access
126+
LOG.warn("failed to load hbo pinned statistics from internal table", t);
127+
}
128+
}
129+
95130
/**
96131
* A manually injected hbo statistics entry.
97132
*/
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.nereids.stats;
19+
20+
import org.apache.doris.common.FeConstants;
21+
import org.apache.doris.statistics.ResultRow;
22+
import org.apache.doris.statistics.util.StatisticsUtil;
23+
24+
import org.apache.logging.log4j.LogManager;
25+
import org.apache.logging.log4j.Logger;
26+
27+
import java.util.ArrayList;
28+
import java.util.List;
29+
30+
/**
31+
* Persistence of manually injected (pinned) hbo statistics into the internal database
32+
* ({@code __internal_schema.hbo_statistics}), controlled by
33+
* {@code Config.hbo_persist_pinned_to_internal_db}.
34+
*
35+
* <p>The in-memory {@link HboPlanStatisticsManager} stays authoritative on the read path;
36+
* SET/DELETE are written through synchronously (best effort, failures only logged) and a FE
37+
* loads the whole table into memory lazily on first use, so pinned entries survive a FE
38+
* restart. All FEs share the table through the backend storage.
39+
*/
40+
public class HboStatisticsStore {
41+
private static final Logger LOG = LogManager.getLogger(HboStatisticsStore.class);
42+
43+
private static final String INTERNAL_DB = FeConstants.INTERNAL_DB_NAME;
44+
private static final String TABLE = "hbo_statistics";
45+
private static final String FULL_QUALIFIED = "internal." + INTERNAL_DB + "." + TABLE;
46+
private static final int STRUCT_MAX_LEN = 65533;
47+
48+
private static final String DDL = "CREATE TABLE IF NOT EXISTS `internal`.`" + INTERNAL_DB + "`.`" + TABLE
49+
+ "` (\n"
50+
+ " `fingerprint` varchar(64) NOT NULL COMMENT \"\",\n"
51+
+ " `row_count` bigint NOT NULL COMMENT \"\",\n"
52+
+ " `node_type` varchar(1024) NULL COMMENT \"\",\n"
53+
+ " `struct_info` varchar(" + STRUCT_MAX_LEN + ") NULL COMMENT \"\",\n"
54+
+ " `create_time_ms` bigint NOT NULL COMMENT \"\"\n"
55+
+ ") ENGINE = olap\n"
56+
+ "UNIQUE KEY(`fingerprint`)\n"
57+
+ "COMMENT \"Doris internal hbo pinned statistics table, DO NOT MODIFY IT\"\n"
58+
+ "DISTRIBUTED BY HASH(`fingerprint`)\n"
59+
+ "BUCKETS 1\n"
60+
+ "PROPERTIES (\"replication_num\" = \"1\")";
61+
62+
private HboStatisticsStore() {
63+
}
64+
65+
/** Create the table if it does not exist. */
66+
public static void ensureTable() throws Exception {
67+
StatisticsUtil.execUpdate(DDL);
68+
}
69+
70+
/** Upsert one pinned entry (UNIQUE KEY fingerprint replaces on conflict). */
71+
public static void persist(String fingerprint, long rows, String nodeType, String structCanonical,
72+
long createTimeMs) {
73+
try {
74+
ensureTable();
75+
String struct = structCanonical == null ? "" : structCanonical;
76+
if (struct.length() > STRUCT_MAX_LEN) {
77+
struct = struct.substring(0, STRUCT_MAX_LEN);
78+
}
79+
String sql = "INSERT INTO " + FULL_QUALIFIED
80+
+ " (`fingerprint`, `row_count`, `node_type`, `struct_info`, `create_time_ms`) VALUES ('"
81+
+ escape(fingerprint) + "', " + rows + ", '" + escape(nodeType) + "', '"
82+
+ escape(struct) + "', " + createTimeMs + ")";
83+
StatisticsUtil.execUpdate(sql);
84+
} catch (Throwable t) {
85+
LOG.warn("failed to persist hbo pinned statistics for fingerprint {}", fingerprint, t);
86+
}
87+
}
88+
89+
/** Remove a pinned entry. */
90+
public static void delete(String fingerprint) {
91+
try {
92+
ensureTable();
93+
String sql = "DELETE FROM " + FULL_QUALIFIED + " WHERE `fingerprint` = '" + escape(fingerprint) + "'";
94+
StatisticsUtil.execUpdate(sql);
95+
} catch (Throwable t) {
96+
LOG.warn("failed to delete hbo pinned statistics for fingerprint {}", fingerprint, t);
97+
}
98+
}
99+
100+
/** Load all pinned entries into memory. Returns empty list when the table is not present. */
101+
public static List<HboPlanStatisticsManager.PinnedHboStatistics> loadAll() {
102+
List<HboPlanStatisticsManager.PinnedHboStatistics> result = new ArrayList<>();
103+
try {
104+
ensureTable();
105+
List<ResultRow> rows = StatisticsUtil.execStatisticQuery(
106+
"SELECT `fingerprint`, `row_count`, `node_type`, `struct_info`, `create_time_ms` FROM "
107+
+ FULL_QUALIFIED);
108+
for (ResultRow row : rows) {
109+
try {
110+
result.add(new HboPlanStatisticsManager.PinnedHboStatistics(
111+
row.get(0),
112+
Long.parseLong(row.get(1)),
113+
row.get(2) == null ? "" : row.get(2),
114+
row.get(3) == null ? "" : row.get(3),
115+
Long.parseLong(row.get(4))));
116+
} catch (NumberFormatException e) {
117+
LOG.warn("skip malformed hbo pinned statistics row {}", row, e);
118+
}
119+
}
120+
} catch (Throwable t) {
121+
LOG.warn("failed to load hbo pinned statistics from internal table", t);
122+
}
123+
return result;
124+
}
125+
126+
private static String escape(String value) {
127+
return value == null ? "" : value.replace("'", "''");
128+
}
129+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- This file is automatically generated. You should know what you did if you want to edit this
2+
-- !set_persisted --
3+
0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef 123456
4+
5+
-- !delete_cleared --
6+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
suite("hbo_persist_internal_db_test", "nonConcurrent") {
19+
// Persistence of pinned hbo statistics into __internal_schema.hbo_statistics is gated by
20+
// Config.hbo_persist_pinned_to_internal_db (default false, hot-mutable). Flip it on for this
21+
// suite and restore the previous value afterwards so other suites keep the default
22+
// in-memory-only behavior. The in-memory pinned entry is authoritative: SET writes through
23+
// synchronously and DELETE removes the row again.
24+
def prevPersist = (sql """ ADMIN SHOW FRONTEND CONFIG LIKE 'hbo_persist_pinned_to_internal_db'; """)[0][1].toString()
25+
sql """ ADMIN SET FRONTEND CONFIG ("hbo_persist_pinned_to_internal_db" = "true"); """
26+
def fingerprint = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
27+
def tableName = "__internal_schema.hbo_statistics"
28+
try {
29+
// SET persists the pinned entry into the internal table synchronously
30+
sql """ HBO SET STATISTICS '${fingerprint}' = 123456; """
31+
qt_set_persisted """ SELECT fingerprint, row_count, node_type, struct_info FROM ${tableName}
32+
WHERE fingerprint = '${fingerprint}'; """
33+
34+
// DELETE removes the row from the internal table
35+
sql """ HBO DELETE STATISTICS '${fingerprint}'; """
36+
qt_delete_cleared """ SELECT fingerprint, row_count, node_type, struct_info FROM ${tableName}
37+
WHERE fingerprint = '${fingerprint}'; """
38+
} finally {
39+
sql """ HBO DELETE STATISTICS '${fingerprint}'; """
40+
sql """ ADMIN SET FRONTEND CONFIG ("hbo_persist_pinned_to_internal_db" = "${prevPersist}"); """
41+
}
42+
}

0 commit comments

Comments
 (0)