mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 05:57:03 +09:00
Add streampool msg size
This commit is contained in:
parent
2989b11f3c
commit
7e9839336c
5 changed files with 41 additions and 5 deletions
|
@ -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))
|
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) {
|
func (h *HeadUpdate) SetPeerId(peerId string) {
|
||||||
h.Meta.PeerId = peerId
|
h.Meta.PeerId = peerId
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,7 +93,7 @@ func (s *syncService) Run(ctx context.Context) (err error) {
|
||||||
func (s *syncService) Close(ctx context.Context) (err error) {
|
func (s *syncService) Close(ctx context.Context) (err error) {
|
||||||
err = s.receiveQueue.Close()
|
err = s.receiveQueue.Close()
|
||||||
if s.commonMetric != nil {
|
if s.commonMetric != nil {
|
||||||
s.commonMetric.UnregisterSyncMetric(s.spaceId, s.metric)
|
s.commonMetric.UnregisterSyncMetric(s.spaceId)
|
||||||
}
|
}
|
||||||
s.manager.Close()
|
s.manager.Close()
|
||||||
return
|
return
|
||||||
|
|
|
@ -31,7 +31,9 @@ type Metric interface {
|
||||||
WrapDRPCHandler(h drpc.Handler) drpc.Handler
|
WrapDRPCHandler(h drpc.Handler) drpc.Handler
|
||||||
RequestLog(ctx context.Context, rpc string, fields ...zap.Field)
|
RequestLog(ctx context.Context, rpc string, fields ...zap.Field)
|
||||||
RegisterSyncMetric(spaceId string, syncMetric SyncMetric)
|
RegisterSyncMetric(spaceId string, syncMetric SyncMetric)
|
||||||
UnregisterSyncMetric(spaceId string, syncMetric SyncMetric)
|
UnregisterSyncMetric(spaceId string)
|
||||||
|
RegisterStreamPoolSyncMetric(mtr StreamPoolMetric)
|
||||||
|
UnregisterStreamPoolSyncMetric()
|
||||||
app.ComponentRunnable
|
app.ComponentRunnable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,18 +45,31 @@ type metric struct {
|
||||||
appField zap.Field
|
appField zap.Field
|
||||||
mx sync.Mutex
|
mx sync.Mutex
|
||||||
syncMetrics map[string]SyncMetric
|
syncMetrics map[string]SyncMetric
|
||||||
|
streamPoolMetric StreamPoolMetric
|
||||||
lastCachedState SyncMetricState
|
lastCachedState SyncMetricState
|
||||||
lastCachedDate time.Time
|
lastCachedDate time.Time
|
||||||
lastCachedTimeout time.Duration
|
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) {
|
func (m *metric) RegisterSyncMetric(spaceId string, syncMetric SyncMetric) {
|
||||||
m.mx.Lock()
|
m.mx.Lock()
|
||||||
defer m.mx.Unlock()
|
defer m.mx.Unlock()
|
||||||
m.syncMetrics[spaceId] = syncMetric
|
m.syncMetrics[spaceId] = syncMetric
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *metric) UnregisterSyncMetric(spaceId string, syncMetric SyncMetric) {
|
func (m *metric) UnregisterSyncMetric(spaceId string) {
|
||||||
m.mx.Lock()
|
m.mx.Lock()
|
||||||
defer m.mx.Unlock()
|
defer m.mx.Unlock()
|
||||||
delete(m.syncMetrics, spaceId)
|
delete(m.syncMetrics, spaceId)
|
||||||
|
|
|
@ -10,6 +10,10 @@ type SyncMetric interface {
|
||||||
SyncMetricState() SyncMetricState
|
SyncMetricState() SyncMetricState
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StreamPoolMetric interface {
|
||||||
|
OutgoingMsg() (count uint32, size uint64)
|
||||||
|
}
|
||||||
|
|
||||||
type SyncMetricState struct {
|
type SyncMetricState struct {
|
||||||
IncomingMsgCount uint32
|
IncomingMsgCount uint32
|
||||||
IncomingMsgSize uint64
|
IncomingMsgSize uint64
|
||||||
|
@ -28,13 +32,11 @@ type SyncMetricState struct {
|
||||||
|
|
||||||
func (st *SyncMetricState) Append(other SyncMetricState) {
|
func (st *SyncMetricState) Append(other SyncMetricState) {
|
||||||
st.IncomingMsgSize += other.IncomingMsgSize
|
st.IncomingMsgSize += other.IncomingMsgSize
|
||||||
st.OutgoingMsgSize += other.OutgoingMsgSize
|
|
||||||
st.IncomingReqSize += other.IncomingReqSize
|
st.IncomingReqSize += other.IncomingReqSize
|
||||||
st.OutgoingReqSize += other.OutgoingReqSize
|
st.OutgoingReqSize += other.OutgoingReqSize
|
||||||
st.ReceivedRespSize += other.ReceivedRespSize
|
st.ReceivedRespSize += other.ReceivedRespSize
|
||||||
st.SentRespSize += other.SentRespSize
|
st.SentRespSize += other.SentRespSize
|
||||||
st.IncomingMsgCount += other.IncomingMsgCount
|
st.IncomingMsgCount += other.IncomingMsgCount
|
||||||
st.OutgoingMsgCount += other.OutgoingMsgCount
|
|
||||||
st.IncomingReqCount += other.IncomingReqCount
|
st.IncomingReqCount += other.IncomingReqCount
|
||||||
st.OutgoingReqCount += other.OutgoingReqCount
|
st.OutgoingReqCount += other.OutgoingReqCount
|
||||||
st.ReceivedRespCount += other.ReceivedRespCount
|
st.ReceivedRespCount += other.ReceivedRespCount
|
||||||
|
@ -58,6 +60,9 @@ func (m *metric) getLastCached() SyncMetricState {
|
||||||
for _, mtr := range allMetrics {
|
for _, mtr := range allMetrics {
|
||||||
lastCached.Append(mtr.SyncMetricState())
|
lastCached.Append(mtr.SyncMetricState())
|
||||||
}
|
}
|
||||||
|
if m.streamPoolMetric != nil {
|
||||||
|
lastCached.OutgoingMsgCount, lastCached.OutgoingMsgSize = m.streamPoolMetric.OutgoingMsg()
|
||||||
|
}
|
||||||
m.mx.Lock()
|
m.mx.Lock()
|
||||||
defer m.mx.Unlock()
|
defer m.mx.Unlock()
|
||||||
m.lastCachedState = lastCached
|
m.lastCachedState = lastCached
|
||||||
|
|
|
@ -81,6 +81,16 @@ type streamPool struct {
|
||||||
lastStreamId uint32
|
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) {
|
func (s *streamPool) Init(a *app.App) (err error) {
|
||||||
s.metric, _ = a.Component(metric.CName).(metric.Metric)
|
s.metric, _ = a.Component(metric.CName).(metric.Metric)
|
||||||
s.handler = a.MustComponent(streamhandler.CName).(streamhandler.StreamHandler)
|
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.streamConfig = a.MustComponent("config").(configGetter).GetStreamConfig()
|
||||||
s.statService.AddProvider(s)
|
s.statService.AddProvider(s)
|
||||||
if s.metric != nil {
|
if s.metric != nil {
|
||||||
|
s.metric.RegisterStreamPoolSyncMetric(s)
|
||||||
registerMetrics(s.metric.Registry(), s, "")
|
registerMetrics(s.metric.Registry(), s, "")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -440,6 +451,7 @@ func (s *streamPool) removeStream(streamId uint32) {
|
||||||
|
|
||||||
func (s *streamPool) Close(ctx context.Context) (err error) {
|
func (s *streamPool) Close(ctx context.Context) (err error) {
|
||||||
s.statService.RemoveProvider(s)
|
s.statService.RemoveProvider(s)
|
||||||
|
s.metric.UnregisterStreamPoolSyncMetric()
|
||||||
return s.dial.Close()
|
return s.dial.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue