-
Notifications
You must be signed in to change notification settings - Fork 17
/
dcp_stats_log.go
215 lines (171 loc) · 4 KB
/
dcp_stats_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package consumer
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/couchbase/eventing/logging"
)
type state string
var (
StateInit = state("StreamInit")
StateStreamEnd = state("StreamEnd")
// Caller should provide the request that its making and not this value
StateStreamRequest = state("StreamRequest")
)
var (
maxBatchSize = 20
)
type LogKey int8
const (
LogState LogKey = iota
StreamResponse
)
type DcpStatsLog interface {
AddDcpLog(vb uint16, key LogKey, value string)
DeletePartition(vb uint16)
}
type ResponseStat struct {
Ts string `json:"response_ts"`
Count int `json:"count"`
}
func NewResponseStat() *ResponseStat {
return &ResponseStat{}
}
type DcpVbStats struct {
dirty bool
CurrentState state `json:"state"`
Ts string `json:"ts"`
LastRequest string `json:"last_request"`
StreamResponse map[string]*ResponseStat `json:"response,omitempty"`
}
func NewDcpVbStat() *DcpVbStats {
s := &DcpVbStats{
dirty: true,
CurrentState: StateInit,
Ts: time.Now().Format(logging.LogTimeFormat),
StreamResponse: make(map[string]*ResponseStat),
}
return s
}
func (stat *DcpVbStats) logMessage() (string, bool) {
if !stat.dirty {
return "", false
}
stat.dirty = false
data, err := json.Marshal(stat)
if err != nil {
logging.Errorf("Error in parsing stat struct: %v err: %v", stat, err)
return "", false
}
return string(data), true
}
func (stat *DcpVbStats) AddStat(key LogKey, value string) {
switch key {
case LogState:
nextState := state(value)
switch nextState {
case StateInit, StateStreamEnd:
// Ignore. Only log last stream request
default:
nextState = StateStreamRequest
stat.LastRequest = value
stat.dirty = true
}
if stat.CurrentState == nextState {
return
}
// If state changed to stream request then clear previous response map
if nextState == StateStreamRequest {
stat.StreamResponse = make(map[string]*ResponseStat)
}
stat.CurrentState = nextState
stat.Ts = time.Now().Format(logging.LogTimeFormat)
case StreamResponse:
rs, ok := stat.StreamResponse[value]
if !ok {
rs = NewResponseStat()
stat.StreamResponse[value] = rs
}
rs.Ts = time.Now().Format(logging.LogTimeFormat)
rs.Count++
}
stat.dirty = true
}
type dcpStatsLogs struct {
sync.RWMutex
stats map[uint16]*DcpVbStats
stopChannel chan struct{}
pollingInterval time.Duration
logPrefix string
}
func NewDcpStatsLog(pollingInterval time.Duration, logSuffix string, stopChannel chan struct{}) DcpStatsLog {
dsl := &dcpStatsLogs{
stats: make(map[uint16]*DcpVbStats),
stopChannel: stopChannel,
pollingInterval: pollingInterval,
logPrefix: fmt.Sprintf("DcpStatsLog [%s] ", logSuffix),
}
go dsl.periodicLogger()
return dsl
}
func (dsl *dcpStatsLogs) periodicLogger() {
poll := time.NewTicker(dsl.pollingInterval)
defer poll.Stop()
for {
select {
case <-poll.C:
dsl.log()
case <-dsl.stopChannel:
dsl.log()
return
}
}
}
func (dsl *dcpStatsLogs) log() {
dsl.RLock()
defer dsl.RUnlock()
s, first := "", true
count := 0
for vb, stat := range dsl.stats {
log, ok := stat.logMessage()
if !ok {
continue
}
count++
if first {
s = fmt.Sprintf("{\"vb_%d\" : %s", vb, log)
first = false
continue
}
s = fmt.Sprintf("%s, \"vb_%d\" : %s", s, vb, log)
if count == maxBatchSize {
count, first = 0, true
logging.Infof("%s %s}", dsl.logPrefix, s)
s = ""
}
}
if first {
return
}
logging.Infof("%s %s}", dsl.logPrefix, s)
}
func (dsl *dcpStatsLogs) AddDcpLog(vb uint16, key LogKey, value string) {
dsl.Lock()
defer dsl.Unlock()
stat := dsl.getOrAddStats(vb)
stat.AddStat(key, value)
}
func (dsl *dcpStatsLogs) DeletePartition(vb uint16) {
dsl.Lock()
defer dsl.Unlock()
delete(dsl.stats, vb)
}
func (dsl *dcpStatsLogs) getOrAddStats(vb uint16) *DcpVbStats {
stat, ok := dsl.stats[vb]
if !ok {
stat = NewDcpVbStat()
dsl.stats[vb] = stat
}
return stat
}