74
74
import java .util .Optional ;
75
75
import java .util .Set ;
76
76
77
- /** NereidsSqlCacheManager */
77
+ /**
78
+ * NereidsSqlCacheManager
79
+ */
78
80
public class NereidsSqlCacheManager {
79
- // key: <user>:<sql>
81
+ // key: <ctl.db>:< user>:<sql>
80
82
// value: SqlCacheContext
81
83
private volatile Cache <String , SqlCacheContext > sqlCaches ;
82
84
@@ -110,7 +112,7 @@ private static Cache<String, SqlCacheContext> buildSqlCaches(int sqlCacheNum, lo
110
112
// auto evict cache when jvm memory too low
111
113
.softValues ();
112
114
if (sqlCacheNum > 0 ) {
113
- cacheBuilder = cacheBuilder .maximumSize (sqlCacheNum );
115
+ cacheBuilder .maximumSize (sqlCacheNum );
114
116
}
115
117
if (expireAfterAccessSeconds > 0 ) {
116
118
cacheBuilder = cacheBuilder .expireAfterAccess (Duration .ofSeconds (expireAfterAccessSeconds ));
@@ -119,25 +121,28 @@ private static Cache<String, SqlCacheContext> buildSqlCaches(int sqlCacheNum, lo
119
121
return cacheBuilder .build ();
120
122
}
121
123
122
- /** tryAddFeCache */
124
+ /**
125
+ * tryAddFeCache
126
+ */
123
127
public void tryAddFeSqlCache (ConnectContext connectContext , String sql ) {
124
128
Optional <SqlCacheContext > sqlCacheContextOpt = connectContext .getStatementContext ().getSqlCacheContext ();
125
129
if (!sqlCacheContextOpt .isPresent ()) {
126
130
return ;
127
131
}
128
132
129
133
SqlCacheContext sqlCacheContext = sqlCacheContextOpt .get ();
130
- UserIdentity currentUserIdentity = connectContext .getCurrentUserIdentity ();
131
134
String key = sqlCacheContext .getCacheKeyType () == CacheKeyType .SQL
132
- ? currentUserIdentity . toString () + ":" + normalizeSql (sql . trim ( ))
133
- : currentUserIdentity . toString () + ":" + DebugUtil .printId (sqlCacheContext .getOrComputeCacheKeyMd5 ());
135
+ ? generateCacheKey ( connectContext , normalizeSql (sql ))
136
+ : generateCacheKey ( connectContext , DebugUtil .printId (sqlCacheContext .getOrComputeCacheKeyMd5 () ));
134
137
if (sqlCaches .getIfPresent (key ) == null && sqlCacheContext .getOrComputeCacheKeyMd5 () != null
135
138
&& sqlCacheContext .getResultSetInFe ().isPresent ()) {
136
139
sqlCaches .put (key , sqlCacheContext );
137
140
}
138
141
}
139
142
140
- /** tryAddBeCache */
143
+ /**
144
+ * tryAddBeCache
145
+ */
141
146
public void tryAddBeCache (ConnectContext connectContext , String sql , CacheAnalyzer analyzer ) {
142
147
Optional <SqlCacheContext > sqlCacheContextOpt = connectContext .getStatementContext ().getSqlCacheContext ();
143
148
if (!sqlCacheContextOpt .isPresent ()) {
@@ -147,10 +152,9 @@ public void tryAddBeCache(ConnectContext connectContext, String sql, CacheAnalyz
147
152
return ;
148
153
}
149
154
SqlCacheContext sqlCacheContext = sqlCacheContextOpt .get ();
150
- UserIdentity currentUserIdentity = connectContext .getCurrentUserIdentity ();
151
155
String key = sqlCacheContext .getCacheKeyType () == CacheKeyType .SQL
152
- ? currentUserIdentity . toString () + ":" + normalizeSql (sql . trim ( ))
153
- : currentUserIdentity . toString () + ":" + DebugUtil .printId (sqlCacheContext .getOrComputeCacheKeyMd5 ());
156
+ ? generateCacheKey ( connectContext , normalizeSql (sql ))
157
+ : generateCacheKey ( connectContext , DebugUtil .printId (sqlCacheContext .getOrComputeCacheKeyMd5 () ));
154
158
if (sqlCaches .getIfPresent (key ) == null && sqlCacheContext .getOrComputeCacheKeyMd5 () != null ) {
155
159
SqlCache cache = (SqlCache ) analyzer .getCache ();
156
160
sqlCacheContext .setSumOfPartitionNum (cache .getSumOfPartitionNum ());
@@ -167,23 +171,23 @@ public void tryAddBeCache(ConnectContext connectContext, String sql, CacheAnalyz
167
171
}
168
172
}
169
173
170
- /** tryParseSql */
174
+ /**
175
+ * tryParseSql
176
+ */
171
177
public Optional <LogicalSqlCache > tryParseSql (ConnectContext connectContext , String sql ) {
172
- UserIdentity currentUserIdentity = connectContext .getCurrentUserIdentity ();
173
- String key = currentUserIdentity + ":" + normalizeSql (sql .trim ());
178
+ String key = generateCacheKey (connectContext , normalizeSql (sql .trim ()));
174
179
SqlCacheContext sqlCacheContext = sqlCaches .getIfPresent (key );
175
180
if (sqlCacheContext == null ) {
176
181
return Optional .empty ();
177
182
}
178
183
179
184
// LOG.info("Total size: " + GraphLayout.parseInstance(sqlCacheContext).totalSize());
180
-
185
+ UserIdentity currentUserIdentity = connectContext . getCurrentUserIdentity ();
181
186
List <Variable > currentVariables = resolveUserVariables (sqlCacheContext );
182
187
if (usedVariablesChanged (currentVariables , sqlCacheContext )) {
183
188
String md5 = DebugUtil .printId (
184
189
sqlCacheContext .doComputeCacheKeyMd5 (Utils .fastToImmutableSet (currentVariables )));
185
-
186
- String md5CacheKey = currentUserIdentity + ":" + md5 ;
190
+ String md5CacheKey = generateCacheKey (connectContext , md5 );
187
191
SqlCacheContext sqlCacheContextWithVariable = sqlCaches .getIfPresent (md5CacheKey );
188
192
189
193
// already exist cache in the fe, but the variable is different to this query,
@@ -203,6 +207,15 @@ public Optional<LogicalSqlCache> tryParseSql(ConnectContext connectContext, Stri
203
207
}
204
208
}
205
209
210
+ private String generateCacheKey (ConnectContext connectContext , String sqlOrMd5 ) {
211
+ CatalogIf <?> currentCatalog = connectContext .getCurrentCatalog ();
212
+ String currentCatalogName = currentCatalog != null ? currentCatalog .getName () : "" ;
213
+ String currentDatabase = connectContext .getDatabase ();
214
+ String currentDatabaseName = currentDatabase != null ? currentDatabase : "" ;
215
+ return currentCatalogName + "." + currentDatabaseName + ":" + connectContext .getCurrentUserIdentity ().toString ()
216
+ + ":" + sqlOrMd5 ;
217
+ }
218
+
206
219
private String normalizeSql (String sql ) {
207
220
return NereidsParser .removeCommentAndTrimBlank (sql );
208
221
}
@@ -402,7 +415,7 @@ private boolean usedVariablesChanged(List<Variable> currentVariables, SqlCacheCo
402
415
Variable cachedVariable = cachedUsedVariables .get (i );
403
416
if (!Objects .equals (currentVariable , cachedVariable )
404
417
|| cachedVariable .getRealExpression ().anyMatch (
405
- expr -> !((ExpressionTrait ) expr ).isDeterministic ())) {
418
+ expr -> !((ExpressionTrait ) expr ).isDeterministic ())) {
406
419
return true ;
407
420
}
408
421
}
0 commit comments