1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-07 21:47:02 +09:00

Add streampool msg size

This commit is contained in:
mcrakhman 2024-08-15 08:28:51 +02:00
parent 2989b11f3c
commit 7e9839336c
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
5 changed files with 41 additions and 5 deletions

View file

@ -62,6 +62,10 @@ func (h *HeadUpdate) MsgSize() uint64 {
return byteSize + uint64(len(h.Meta.PeerId)) + uint64(len(h.Meta.ObjectId)) + uint64(len(h.Meta.SpaceId))
}
func (h *HeadUpdate) Size() int {
return int(h.MsgSize())
}
func (h *HeadUpdate) SetPeerId(peerId string) {
h.Meta.PeerId = peerId
}

View file

@ -93,7 +93,7 @@ func (s *syncService) Run(ctx context.Context) (err error) {
func (s *syncService) Close(ctx context.Context) (err error) {
err = s.receiveQueue.Close()
if s.commonMetric != nil {
s.commonMetric.UnregisterSyncMetric(s.spaceId, s.metric)
s.commonMetric.UnregisterSyncMetric(s.spaceId)
}
s.manager.Close()
return

View file

@ -31,7 +31,9 @@ type Metric interface {
WrapDRPCHandler(h drpc.Handler) drpc.Handler
RequestLog(ctx context.Context, rpc string, fields ...zap.Field)
RegisterSyncMetric(spaceId string, syncMetric SyncMetric)
UnregisterSyncMetric(spaceId string, syncMetric SyncMetric)
UnregisterSyncMetric(spaceId string)
RegisterStreamPoolSyncMetric(mtr StreamPoolMetric)
UnregisterStreamPoolSyncMetric()
app.ComponentRunnable
}
@ -43,18 +45,31 @@ type metric struct {
appField zap.Field
mx sync.Mutex
syncMetrics map[string]SyncMetric
streamPoolMetric StreamPoolMetric
lastCachedState SyncMetricState
lastCachedDate time.Time
lastCachedTimeout time.Duration
}
func (m *metric) RegisterStreamPoolSyncMetric(mtr StreamPoolMetric) {
m.mx.Lock()
defer m.mx.Unlock()
m.streamPoolMetric = mtr
}
func (m *metric) UnregisterStreamPoolSyncMetric() {
m.mx.Lock()
defer m.mx.Unlock()
m.streamPoolMetric = nil
}
func (m *metric) RegisterSyncMetric(spaceId string, syncMetric SyncMetric) {
m.mx.Lock()
defer m.mx.Unlock()
m.syncMetrics[spaceId] = syncMetric
}
func (m *metric) UnregisterSyncMetric(spaceId string, syncMetric SyncMetric) {
func (m *metric) UnregisterSyncMetric(spaceId string) {
m.mx.Lock()
defer m.mx.Unlock()
delete(m.syncMetrics, spaceId)

View file

@ -10,6 +10,10 @@ type SyncMetric interface {
SyncMetricState() SyncMetricState
}
type StreamPoolMetric interface {
OutgoingMsg() (count uint32, size uint64)
}
type SyncMetricState struct {
IncomingMsgCount uint32
IncomingMsgSize uint64
@ -28,13 +32,11 @@ type SyncMetricState struct {
func (st *SyncMetricState) Append(other SyncMetricState) {
st.IncomingMsgSize += other.IncomingMsgSize
st.OutgoingMsgSize += other.OutgoingMsgSize
st.IncomingReqSize += other.IncomingReqSize
st.OutgoingReqSize += other.OutgoingReqSize
st.ReceivedRespSize += other.ReceivedRespSize
st.SentRespSize += other.SentRespSize
st.IncomingMsgCount += other.IncomingMsgCount
st.OutgoingMsgCount += other.OutgoingMsgCount
st.IncomingReqCount += other.IncomingReqCount
st.OutgoingReqCount += other.OutgoingReqCount
st.ReceivedRespCount += other.ReceivedRespCount
@ -58,6 +60,9 @@ func (m *metric) getLastCached() SyncMetricState {
for _, mtr := range allMetrics {
lastCached.Append(mtr.SyncMetricState())
}
if m.streamPoolMetric != nil {
lastCached.OutgoingMsgCount, lastCached.OutgoingMsgSize = m.streamPoolMetric.OutgoingMsg()
}
m.mx.Lock()
defer m.mx.Unlock()
m.lastCachedState = lastCached

View file

@ -81,6 +81,16 @@ type streamPool struct {
lastStreamId uint32
}
func (s *streamPool) OutgoingMsg() (count uint32, size uint64) {
s.mu.Lock()
defer s.mu.Unlock()
for _, st := range s.streams {
count += uint32(st.stats.msgCount.Load())
size += uint64(st.stats.totalSize.Load())
}
return
}
func (s *streamPool) Init(a *app.App) (err error) {
s.metric, _ = a.Component(metric.CName).(metric.Metric)
s.handler = a.MustComponent(streamhandler.CName).(streamhandler.StreamHandler)
@ -92,6 +102,7 @@ func (s *streamPool) Init(a *app.App) (err error) {
s.streamConfig = a.MustComponent("config").(configGetter).GetStreamConfig()
s.statService.AddProvider(s)
if s.metric != nil {
s.metric.RegisterStreamPoolSyncMetric(s)
registerMetrics(s.metric.Registry(), s, "")
}
return nil
@ -440,6 +451,7 @@ func (s *streamPool) removeStream(streamId uint32) {
func (s *streamPool) Close(ctx context.Context) (err error) {
s.statService.RemoveProvider(s)
s.metric.UnregisterStreamPoolSyncMetric()
return s.dial.Close()
}