From 0868a978d1a0648d0df971dbe1d7274480146755 Mon Sep 17 00:00:00 2001 From: chyezh Date: Wed, 14 Aug 2024 12:04:52 +0800 Subject: [PATCH] fix: streaming service stat manager crash when pv stat is empty (#35439) issue: #33285 Signed-off-by: chyezh --- .../segment/stats/stats_manager.go | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/internal/streamingnode/server/wal/interceptors/segment/stats/stats_manager.go b/internal/streamingnode/server/wal/interceptors/segment/stats/stats_manager.go index f571717728e9c..3fdd80bc9ea03 100644 --- a/internal/streamingnode/server/wal/interceptors/segment/stats/stats_manager.go +++ b/internal/streamingnode/server/wal/interceptors/segment/stats/stats_manager.go @@ -77,7 +77,13 @@ func (m *StatsManager) AllocRows(segmentID int64, insert InsertMetrics) bool { // update the total stats if inserted. if inserted { m.totalStats.Collect(insert) + if _, ok := m.pchannelStats[info.PChannel]; !ok { + m.pchannelStats[info.PChannel] = &InsertMetrics{} + } m.pchannelStats[info.PChannel].Collect(insert) + if _, ok := m.vchannelStats[info.VChannel]; !ok { + m.vchannelStats[info.VChannel] = &InsertMetrics{} + } m.vchannelStats[info.VChannel].Collect(insert) return true } @@ -128,17 +134,21 @@ func (m *StatsManager) UnregisterSealedSegment(segmentID int64) *SegmentStats { } stats := m.segmentStats[segmentID] - m.pchannelStats[info.PChannel].Subtract(stats.Insert) - m.vchannelStats[info.VChannel].Subtract(stats.Insert) - m.totalStats.Collect(stats.Insert) + m.totalStats.Subtract(stats.Insert) delete(m.segmentStats, segmentID) delete(m.segmentIndex, segmentID) - if m.pchannelStats[info.PChannel].BinarySize == 0 { - delete(m.pchannelStats, info.PChannel) + if _, ok := m.pchannelStats[info.PChannel]; ok { + m.pchannelStats[info.PChannel].Subtract(stats.Insert) + if m.pchannelStats[info.PChannel].BinarySize == 0 { + delete(m.pchannelStats, info.PChannel) + } } - if m.vchannelStats[info.VChannel].BinarySize == 0 { - delete(m.vchannelStats, info.VChannel) + if _, ok := m.vchannelStats[info.VChannel]; ok { + m.vchannelStats[info.VChannel].Subtract(stats.Insert) + if m.vchannelStats[info.VChannel].BinarySize == 0 { + delete(m.vchannelStats, info.VChannel) + } } return stats }