diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/file/TsFileMetrics.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/file/TsFileMetrics.java index 189c5eb0f9c8..543e631364a2 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/file/TsFileMetrics.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/file/TsFileMetrics.java @@ -38,6 +38,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; @@ -206,17 +207,19 @@ public Gauge getOrCreateGlobalTsFileCountGauge( } public void deleteGlobalTsFileCountGauge(String orderStr, String database, String regionId) { - metricService - .get() - .remove( - MetricType.GAUGE, - FILE_GLOBAL_COUNT, - Tag.NAME.toString(), - orderStr, - Tag.DATABASE.toString(), - database, - Tag.REGION.toString(), - regionId); + // This metric may not exist + Optional.ofNullable(this.metricService.get()) + .ifPresent( + service -> + service.remove( + MetricType.GAUGE, + FILE_GLOBAL_COUNT, + Tag.NAME.toString(), + orderStr, + Tag.DATABASE.toString(), + database, + Tag.REGION.toString(), + regionId)); } private void updateGlobalTsFileSizeMap( @@ -267,17 +270,19 @@ public Gauge getOrCreateGlobalTsFileSizeGauge(String orderStr, String database, } public void deleteGlobalTsFileSizeGauge(String orderStr, String database, String regionId) { - metricService - .get() - .remove( - MetricType.GAUGE, - FILE_GLOBAL_SIZE, - Tag.NAME.toString(), - orderStr, - Tag.DATABASE.toString(), - database, - Tag.REGION.toString(), - regionId); + // This metric may not exist + Optional.ofNullable(this.metricService.get()) + .ifPresent( + service -> + service.remove( + MetricType.GAUGE, + FILE_GLOBAL_SIZE, + Tag.NAME.toString(), + orderStr, + Tag.DATABASE.toString(), + database, + Tag.REGION.toString(), + regionId)); } // endregion diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/StorageEngine.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/StorageEngine.java index 21bdf6578d68..eddac28e128d 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/StorageEngine.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/StorageEngine.java @@ -55,6 +55,7 @@ import org.apache.iotdb.db.queryengine.plan.analyze.cache.schema.DataNodeTTLCache; import org.apache.iotdb.db.queryengine.plan.planner.plan.node.load.LoadTsFilePieceNode; import org.apache.iotdb.db.queryengine.plan.scheduler.load.LoadTsFileScheduler; +import org.apache.iotdb.db.service.metrics.FileMetrics; import org.apache.iotdb.db.service.metrics.WritingMetrics; import org.apache.iotdb.db.storageengine.buffer.BloomFilterCache; import org.apache.iotdb.db.storageengine.buffer.ChunkCache; @@ -750,9 +751,6 @@ public void deleteDataRegion(DataRegionId regionId) { deletingDataRegionMap.computeIfAbsent(regionId, k -> dataRegionMap.remove(regionId)); if (region != null) { region.markDeleted(); - WRITING_METRICS.removeDataRegionMemoryCostMetrics(regionId); - WRITING_METRICS.removeFlushingMemTableStatusMetrics(regionId); - WRITING_METRICS.removeActiveMemtableCounterMetrics(regionId); try { region.abortCompaction(); region.syncDeleteDataFiles(); @@ -789,6 +787,10 @@ public void deleteDataRegion(DataRegionId regionId) { default: break; } + WRITING_METRICS.removeDataRegionMemoryCostMetrics(regionId); + WRITING_METRICS.removeFlushingMemTableStatusMetrics(regionId); + WRITING_METRICS.removeActiveMemtableCounterMetrics(regionId); + FileMetrics.getInstance().deleteRegion(region.getDatabaseName(), region.getDataRegionId()); } catch (Exception e) { LOGGER.error( "Error occurs when deleting data region {}-{}", diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java index ff8eda308d47..92c68cd22f89 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/DataRegion.java @@ -3710,8 +3710,6 @@ public void waitForDeleted() { if (!deleted) { deletedCondition.await(); } - FileMetrics.getInstance().deleteRegion(databaseName, dataRegionId); - MetricService.getInstance().removeMetricSet(metrics); } catch (InterruptedException e) { logger.error("Interrupted When waiting for data region deleted."); Thread.currentThread().interrupt(); @@ -3726,6 +3724,7 @@ public void markDeleted() { try { deleted = true; releaseDirectBufferMemory(); + MetricService.getInstance().removeMetricSet(metrics); deletedCondition.signalAll(); } finally { writeUnlock();