forked from TarsCloud/TarsGo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
propertyf.go
447 lines (384 loc) · 8.31 KB
/
propertyf.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package tars
import (
"reflect"
"sort"
"strconv"
"strings"
"sync"
"time"
"tars/protocol/res/propertyf"
"tars/util/tools"
)
//ReportMethod is the interface for all kinds of report methods.
type ReportMethod interface {
Desc() string
Set(int)
Get() string
clear()
}
//Sum report methods.
type Sum struct {
data int
mlock *sync.Mutex
}
//NewSum new and init the sum report methods.
func NewSum() *Sum {
return &Sum{
data: 0,
mlock: new(sync.Mutex)}
}
//Desc return the descriptor string for the sum method.
func (s *Sum) Desc() string {
return "Sum"
}
//Get gets the result of the sum report method.
func (s *Sum) Get() (out string) {
s.mlock.Lock()
defer s.mlock.Unlock()
out = strconv.Itoa(s.data)
s.clear()
return
}
func (s *Sum) clear() {
s.data = 0
}
//Set sets a value tho the sum method.
func (s *Sum) Set(in int) {
s.mlock.Lock()
defer s.mlock.Unlock()
s.data += in
}
//Avg for counting average for the report value.
type Avg struct {
count int
sum int
mlock *sync.Mutex
}
//NewAvg new and init the average struct.
func NewAvg() *Avg {
return &Avg{
count: 0,
sum: 0,
mlock: new(sync.Mutex),
}
}
//Desc return the Desc methods for the Avg struct.
func (a *Avg) Desc() string {
return "Avg"
}
//Get gets the result of the average counting.
func (a *Avg) Get() (out string) {
a.mlock.Lock()
defer a.mlock.Unlock()
if a.count == 0 {
out = "0"
return
}
out = strconv.FormatFloat(float64(a.sum)/float64(a.count), 'f', -1, 64)
a.clear()
return
}
//Set sets the value for the average counting.
func (a *Avg) Set(in int) {
a.mlock.Lock()
defer a.mlock.Unlock()
a.count++
a.sum += in
}
func (a *Avg) clear() {
a.count = 0
a.sum = 0
}
//Max struct is for counting the Max value for the reporting value.
type Max struct {
data int
mlock *sync.Mutex
}
//NewMax new and init the Max struct.
func NewMax() *Max {
return &Max{
data: -9999999,
mlock: new(sync.Mutex)}
}
//Set sets a value for counting max.
func (m *Max) Set(in int) {
m.mlock.Lock()
defer m.mlock.Unlock()
if in > m.data {
m.data = in
}
}
//Get gets the max value.
func (m *Max) Get() (out string) {
m.mlock.Lock()
defer m.mlock.Unlock()
out = strconv.Itoa(m.data)
m.clear()
return
}
func (m *Max) clear() {
m.data = -9999999
}
//Desc return the descrition for the Max struct.
func (m *Max) Desc() string {
return "Max"
}
//Min is the struct for counting the min value.
type Min struct {
data int
mlock *sync.Mutex
}
//NewMin new and init the min struct.
func NewMin() *Min {
return &Min{
data: 0,
mlock: new(sync.Mutex),
}
}
//Desc return the descrition string of the Min strcut.
func (m *Min) Desc() string {
return "Min"
}
//Set sets a value for counting min value.
func (m *Min) Set(in int) {
m.mlock.Lock()
defer m.mlock.Unlock()
if m.data == 0 || (m.data > in && in != 0) {
m.data = in
}
}
//Get get the min value for the Min struct.
func (m *Min) Get() (out string) {
m.mlock.Lock()
defer m.mlock.Unlock()
out = strconv.Itoa(m.data)
m.clear()
return
}
func (m *Min) clear() {
m.data = 0
}
//Distr is used for counting the distribution of the reporting values.
type Distr struct {
dataRange []int
result []int
mlock *sync.Mutex
}
//Desc return the descrition string of the Distr struct.
func (d *Distr) Desc() string {
return "Distr"
}
//NewDistr new and int the Distr
func NewDistr(in []int) (d *Distr) {
d = new(Distr)
d.mlock = new(sync.Mutex)
s := tools.UniqueInts(in)
sort.Ints(s)
d.dataRange = s
d.result = make([]int, len(d.dataRange))
return d
}
//Set sets the value for counting distribution.
func (d *Distr) Set(in int) {
d.mlock.Lock()
defer d.mlock.Unlock()
index := tools.UpperBound(d.dataRange, in)
d.result[index]++
}
//Get get the distribution of the reporting values.
func (d *Distr) Get() string {
d.mlock.Lock()
defer d.mlock.Unlock()
var s string
for i := range d.dataRange {
if i != 0 {
s += ","
}
s = s + strconv.Itoa(d.dataRange[i]) + "|" + strconv.Itoa(d.result[i])
}
d.clear()
return s
}
func (d *Distr) clear() {
for i := range d.result {
d.result[i] = 0
}
}
//Count is for counting the total of reporting
type Count struct {
mlock *sync.Mutex
data int
}
//NewCount new and init the counting struct.
func NewCount() *Count {
return &Count{
data: 0,
mlock: new(sync.Mutex),
}
}
//Set sets the value for counting.
func (c *Count) Set(in int) {
c.mlock.Lock()
defer c.mlock.Unlock()
c.data++
}
//Get gets the total times of the reporting values.
func (c *Count) Get() (out string) {
c.mlock.Lock()
defer c.mlock.Unlock()
out = strconv.Itoa(c.data)
c.clear()
return
}
func (c *Count) clear() {
c.data = 0
}
//Desc return the Count string.
func (c *Count) Desc() string {
return "Count"
}
//PropertyReportHelper is helper struct for property report.
type PropertyReportHelper struct {
reportPtrs []*PropertyReport
mlock *sync.Mutex
comm *Communicator
pf *propertyf.PropertyF
node string
}
//ProHelper is global.
var ProHelper *PropertyReportHelper
var proOnce sync.Once
//ReportToServer report to the remote propertyreport server.
func (p *PropertyReportHelper) ReportToServer() {
p.mlock.Lock()
defer p.mlock.Unlock()
cfg := GetServerConfig()
var head propertyf.StatPropMsgHead
statMsg := make(map[propertyf.StatPropMsgHead]propertyf.StatPropMsgBody)
head.IPropertyVer = 2
if cfg != nil {
if cfg.Enableset {
setList := strings.Split(cfg.Setdivision, ".")
head.ModuleName = cfg.App + "." + cfg.Server + "." + setList[0] + setList[1] + setList[2]
head.SetName = setList[0]
head.SetArea = setList[1]
head.SetID = setList[2]
} else {
head.ModuleName = cfg.App + "." + cfg.Server
}
} else {
return
}
head.Ip = cfg.LocalIP
for _, v := range p.reportPtrs {
head.PropertyName = v.key
var body propertyf.StatPropMsgBody
body.VInfo = make([]propertyf.StatPropInfo, 0)
for _, m := range v.reportMethods {
var info propertyf.StatPropInfo
bflag := false
desc := m.Desc()
result := m.Get()
//todo: use interface method IsDefault() bool
switch desc {
case "Sum":
if result != "0" {
bflag = true
}
case "Avg":
if result != "0" {
bflag = true
}
case "Distr":
if result != "" {
bflag = true
}
case "Max":
if result != "-9999999" {
bflag = true
}
case "Min":
if result != "0" {
bflag = true
}
case "Count":
if result != "0" {
bflag = true
}
default:
bflag = true
}
if !bflag {
continue
}
info.Policy = desc
info.Value = result
body.VInfo = append(body.VInfo, info)
}
statMsg[head] = body
_, err := p.pf.ReportPropMsg(statMsg)
if err != nil {
TLOG.Error("Send to property server Error", reflect.TypeOf(err), err)
}
}
}
//Init inits the PropertyReportHelper
func (p *PropertyReportHelper) Init(comm *Communicator, node string) {
p.node = node
p.mlock = new(sync.Mutex)
p.comm = comm
p.pf = new(propertyf.PropertyF)
p.reportPtrs = make([]*PropertyReport, 0)
p.comm.StringToProxy(p.node, p.pf)
}
func initProReport() {
if GetClientConfig() == nil {
return
}
comm := NewCommunicator()
comm.SetProperty("netthread", 1)
ProHelper = new(PropertyReportHelper)
ProHelper.Init(comm, GetClientConfig().property)
go ProHelper.Run()
}
//AddToReport adds the user's PropertyReport to the PropertyReportHelper
func (p *PropertyReportHelper) AddToReport(pr *PropertyReport) {
p.reportPtrs = append(p.reportPtrs, pr)
}
//Run start the properting report goroutine.
func (p *PropertyReportHelper) Run() {
//todo , get report interval from config
loop := time.NewTicker(PropertyReportInterval)
for range loop.C {
p.ReportToServer()
}
}
//PropertyReport struct.
type PropertyReport struct {
key string
reportMethods []ReportMethod
}
//CreatePropertyReport creats the property report instance with the key.
func CreatePropertyReport(key string, argvs ...ReportMethod) (ptr *PropertyReport) {
proOnce.Do(initProReport)
for _, v := range ProHelper.reportPtrs {
if v.key == key {
ptr = v
return
}
}
ptr = new(PropertyReport)
ptr.reportMethods = make([]ReportMethod, 0)
for _, v := range argvs {
ptr.reportMethods = append(ptr.reportMethods, v)
}
ptr.key = key
ProHelper.reportPtrs = append(ProHelper.reportPtrs, ptr)
return
}
//Report reports a value.
func (p *PropertyReport) Report(in int) {
for _, v := range p.reportMethods {
v.Set(in)
}
}