-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.local.go
123 lines (101 loc) · 2.48 KB
/
store.local.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
package aggregateIncrWrite
import (
"context"
"errors"
"hash/crc32"
"sync"
"time"
)
const incrBufferFull = "buffer full"
func NewLocalStore() AggregateStoreInterface{
return &storeLocal{stopChan: make(chan bool), wait: &sync.WaitGroup{}}
}
type storeLocal struct {
*Config
buffers []chan *incrItem
stopChan chan bool
batchAggChan chan aggItem
stoped bool
wait *sync.WaitGroup
}
func(a *storeLocal) incr(ctx context.Context, id string, val int64)( err error) {
if a.stoped {
return errors.New("has closesed, incr failed")
}
item := &incrItem{id: id, delta: val}
buffer := a.dispatch(ctx, item)
select {
case <- ctx.Done():
return ctx.Err()
case buffer <- item:
return nil
default:
return errors.New(incrBufferFull)
}
}
func (a *storeLocal) dispatch(ctx context.Context, item *incrItem) chan *incrItem{
sum := crc32.ChecksumIEEE([]byte(item.id))
index := sum % uint32(len(a.buffers))
return a.buffers[index]
}
func(a *storeLocal) stop(ctx context.Context) (err error) {
a.stoped = true
close(a.stopChan)
a.wait.Wait()
return
}
func(a *storeLocal) start(c *Config){
a.Config = c
a.buffers = make([]chan *incrItem, a.Config.getConcurrency())
a.batchAggChan = make(chan aggItem, a.Config.getBufferNum() * 100)
interval := int(a.Config.getInterval())
intervalPice := interval/a.Config.getConcurrency()
for i := 0; i < a.Config.getConcurrency(); i ++ {
buffer := make(chan *incrItem, a.Config.getBufferNum())
a.buffers[i] = buffer
a.wait.Add(1)
delayInterval := interval
go func() {
time.Sleep(time.Duration(delayInterval)) // 均匀打散
a.aggregating(buffer)
}()
interval -= intervalPice
}
return
}
func (a *storeLocal) batchAgg() chan aggItem {
return a.batchAggChan
}
func (a *storeLocal) aggregating(buffer chan *incrItem) {
pool := make(aggItem)
// 让tick的启动时间分散
ticker := time.Tick(a.Config.getInterval())
defer a.wait.Done()
for {
select {
case <- a.stopChan:
a.stoped = true
for {
select {
case item := <- buffer:
pool[item.id] += item.delta
default:
a.batchAggChan <- pool
a.Config.getLogger().Info("aggregating exit")
return
}
}
case item, ok := <- buffer:
if !ok {
break
}
pool[item.id] += item.delta
a.Config.getMetric().MetricIncrCount(item.delta)
case <-ticker:
a.Config.getLogger().Info("lcoal run save")
a.batchAggChan <- pool
a.Config.getMetric().MetricBatchCount(int64(len(pool)))
pool = make(aggItem)
}
}
}