Skip to content

Commit 8ce963f

Browse files
authored
fix: shorten lock time (#6968)
1 parent b3aabb6 commit 8ce963f

File tree

1 file changed

+36
-27
lines changed

1 file changed

+36
-27
lines changed

src/frontend/src/instance.rs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -376,33 +376,35 @@ impl Instance {
376376
ctx: QueryContextRef,
377377
) -> server_error::Result<bool> {
378378
let db_string = ctx.get_db_string();
379-
let cache = self
380-
.otlp_metrics_table_legacy_cache
381-
.entry(db_string)
382-
.or_default();
383-
384-
// check cache
385-
let hit_cache = names
386-
.iter()
387-
.filter_map(|name| cache.get(*name))
388-
.collect::<Vec<_>>();
389-
if !hit_cache.is_empty() {
390-
let hit_legacy = hit_cache.iter().any(|en| *en.value());
391-
let hit_prom = hit_cache.iter().any(|en| !*en.value());
392-
393-
// hit but have true and false, means both legacy and new mode are used
394-
// we cannot handle this case, so return error
395-
// add doc links in err msg later
396-
ensure!(!(hit_legacy && hit_prom), OtlpMetricModeIncompatibleSnafu);
379+
// fast cache check
380+
{
381+
let cache = self
382+
.otlp_metrics_table_legacy_cache
383+
.entry(db_string.clone())
384+
.or_default();
397385

398-
let flag = hit_legacy;
399-
// set cache for all names
400-
names.iter().for_each(|name| {
401-
if !cache.contains_key(*name) {
402-
cache.insert(name.to_string(), flag);
403-
}
404-
});
405-
return Ok(flag);
386+
let hit_cache = names
387+
.iter()
388+
.filter_map(|name| cache.get(*name))
389+
.collect::<Vec<_>>();
390+
if !hit_cache.is_empty() {
391+
let hit_legacy = hit_cache.iter().any(|en| *en.value());
392+
let hit_prom = hit_cache.iter().any(|en| !*en.value());
393+
394+
// hit but have true and false, means both legacy and new mode are used
395+
// we cannot handle this case, so return error
396+
// add doc links in err msg later
397+
ensure!(!(hit_legacy && hit_prom), OtlpMetricModeIncompatibleSnafu);
398+
399+
let flag = hit_legacy;
400+
// set cache for all names
401+
names.iter().for_each(|name| {
402+
if !cache.contains_key(*name) {
403+
cache.insert(name.to_string(), flag);
404+
}
405+
});
406+
return Ok(flag);
407+
}
406408
}
407409

408410
let catalog = ctx.current_catalog();
@@ -430,7 +432,10 @@ impl Instance {
430432

431433
// means no existing table is found, use new mode
432434
if table_ids.is_empty() {
433-
// set cache
435+
let cache = self
436+
.otlp_metrics_table_legacy_cache
437+
.entry(db_string)
438+
.or_default();
434439
names.iter().for_each(|name| {
435440
cache.insert(name.to_string(), false);
436441
});
@@ -455,6 +460,10 @@ impl Instance {
455460
.unwrap_or(&OTLP_LEGACY_DEFAULT_VALUE)
456461
})
457462
.collect::<Vec<_>>();
463+
let cache = self
464+
.otlp_metrics_table_legacy_cache
465+
.entry(db_string)
466+
.or_default();
458467
if !options.is_empty() {
459468
// check value consistency
460469
let has_prom = options.iter().any(|opt| *opt == OTLP_METRIC_COMPAT_PROM);

0 commit comments

Comments
 (0)