forked from aliyun/aliyun-log-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shard_worker.go
214 lines (195 loc) · 7.66 KB
/
shard_worker.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
package consumerLibrary
import (
"github.com/aliyun/aliyun-log-go-sdk"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"sync"
"time"
)
var shutDownLock sync.RWMutex
var consumeStatusLock sync.RWMutex
var consumerTaskLock sync.RWMutex
type ShardConsumerWorker struct {
client *ConsumerClient
consumerCheckPointTracker *ConsumerCheckPointTracker
consumerShutDownFlag bool
lastFetchLogGroupList *sls.LogGroupList
nextFetchCursor string
lastFetchGroupCount int
lastFetchtime int64
consumerStatus string
process func(shard int, logGroup *sls.LogGroupList) string
shardId int
tempCheckPoint string
isCurrentDone bool
logger log.Logger
lastFetchTimeForForceFlushCpt int64
isFlushCheckpointDone bool
rollBackCheckpoint string
}
func (consumer *ShardConsumerWorker) setConsumerStatus(status string) {
consumeStatusLock.Lock()
defer consumeStatusLock.Unlock()
consumer.consumerStatus = status
}
func (consumer *ShardConsumerWorker) getConsumerStatus() string {
consumeStatusLock.RLock()
defer consumeStatusLock.RUnlock()
return consumer.consumerStatus
}
func initShardConsumerWorker(shardId int, consumerClient *ConsumerClient, do func(shard int, logGroup *sls.LogGroupList) string, logger log.Logger) *ShardConsumerWorker {
shardConsumeWorker := &ShardConsumerWorker{
consumerShutDownFlag: false,
process: do,
consumerCheckPointTracker: initConsumerCheckpointTracker(shardId, consumerClient, logger),
client: consumerClient,
consumerStatus: INITIALIZING,
shardId: shardId,
lastFetchtime: 0,
isCurrentDone: true,
isFlushCheckpointDone: true,
logger: logger,
lastFetchTimeForForceFlushCpt: 0,
rollBackCheckpoint: "",
}
return shardConsumeWorker
}
func (consumer *ShardConsumerWorker) consume() {
if consumer.consumerShutDownFlag {
consumer.setIsFlushCheckpointDoneToFalse()
go func() {
// If the data is not consumed, save the tempCheckPoint to the server
if consumer.getConsumerStatus() == PULL_PROCESSING_DONE {
consumer.consumerCheckPointTracker.tempCheckPoint = consumer.tempCheckPoint
} else if consumer.getConsumerStatus() == CONSUME_PROCESSING {
level.Info(consumer.logger).Log("msg", "Consumption is in progress, waiting for consumption to be completed")
consumer.setIsFlushCheckpointDoneToTrue()
return
}
err := consumer.consumerCheckPointTracker.flushCheckPoint()
if err != nil {
level.Warn(consumer.logger).Log("msg", "Flush checkpoint error,prepare for retry", "error message:", err)
} else {
consumer.setConsumerStatus(SHUTDOWN_COMPLETE)
level.Info(consumer.logger).Log("msg", "shardworker are shut down complete", "shardWorkerId", consumer.shardId)
}
consumer.setIsFlushCheckpointDoneToTrue()
}()
} else if consumer.getConsumerStatus() == INITIALIZING {
consumer.setConsumerIsCurrentDoneToFalse()
go func() {
cursor, err := consumer.consumerInitializeTask()
if err != nil {
consumer.setConsumerStatus(INITIALIZING)
} else {
consumer.nextFetchCursor = cursor
consumer.setConsumerStatus(INITIALIZING_DONE)
}
consumer.setConsumerIsCurrentDoneToTrue()
}()
} else if consumer.getConsumerStatus() == INITIALIZING_DONE || consumer.getConsumerStatus() == CONSUME_PROCESSING_DONE {
consumer.setConsumerIsCurrentDoneToFalse()
consumer.setConsumerStatus(PULL_PROCESSING)
go func() {
var isGenerateFetchTask = true
// throttling control, similar as Java's SDK
if consumer.lastFetchGroupCount < 100 {
// The time used here is in milliseconds.
isGenerateFetchTask = (time.Now().UnixNano()/1e6 - consumer.lastFetchtime) > 500
} else if consumer.lastFetchGroupCount < 500 {
isGenerateFetchTask = (time.Now().UnixNano()/1e6 - consumer.lastFetchtime) > 200
} else if consumer.lastFetchGroupCount < 1000 {
isGenerateFetchTask = (time.Now().UnixNano()/1e6 - consumer.lastFetchtime) > 50
}
if isGenerateFetchTask {
consumer.lastFetchtime = time.Now().UnixNano() / 1e6
// Set the logback cursor. If the logs are not consumed, save the logback cursor to the server.
consumer.tempCheckPoint = consumer.nextFetchCursor
logGroupList, nextCursor, err := consumer.consumerFetchTask()
if err != nil {
consumer.setConsumerStatus(INITIALIZING_DONE)
} else {
consumer.lastFetchLogGroupList = logGroupList
consumer.nextFetchCursor = nextCursor
consumer.consumerCheckPointTracker.setMemoryCheckPoint(consumer.nextFetchCursor)
consumer.lastFetchGroupCount = GetLogGroupCount(consumer.lastFetchLogGroupList)
level.Debug(consumer.logger).Log("shardId", consumer.shardId, "fetch log count", GetLogCount(consumer.lastFetchLogGroupList))
if consumer.lastFetchGroupCount == 0 {
consumer.lastFetchLogGroupList = nil
} else {
consumer.lastFetchTimeForForceFlushCpt = time.Now().Unix()
}
if time.Now().Unix()-consumer.lastFetchTimeForForceFlushCpt > 30 {
err := consumer.consumerCheckPointTracker.flushCheckPoint()
if err != nil {
level.Warn(consumer.logger).Log("msg", "Failed to save the final checkpoint", "error:", err)
} else {
consumer.lastFetchTimeForForceFlushCpt = 0
}
}
consumer.setConsumerStatus(PULL_PROCESSING_DONE)
}
} else {
level.Debug(consumer.logger).Log("msg", "Pull Log Current Limitation and Re-Pull Log")
consumer.setConsumerStatus(INITIALIZING_DONE)
}
consumer.setConsumerIsCurrentDoneToTrue()
}()
} else if consumer.getConsumerStatus() == PULL_PROCESSING_DONE {
consumer.setConsumerIsCurrentDoneToFalse()
consumer.setConsumerStatus(CONSUME_PROCESSING)
go func() {
rollBackCheckpoint := consumer.consumerProcessTask()
if rollBackCheckpoint != "" {
consumer.nextFetchCursor = rollBackCheckpoint
level.Info(consumer.logger).Log("msg", "Checkpoints set for users have been reset", "shardWorkerId", consumer.shardId, "rollBackCheckpoint", rollBackCheckpoint)
}
consumer.lastFetchLogGroupList = nil
consumer.setConsumerStatus(CONSUME_PROCESSING_DONE)
consumer.setConsumerIsCurrentDoneToTrue()
}()
}
}
func (consumer *ShardConsumerWorker) consumerShutDown() {
consumer.consumerShutDownFlag = true
if !consumer.isShutDownComplete() {
if consumer.getIsFlushCheckpointDoneStatus() == true {
consumer.consume()
} else {
return
}
}
}
func (consumer *ShardConsumerWorker) isShutDownComplete() bool {
return consumer.getConsumerStatus() == SHUTDOWN_COMPLETE
}
func (consumer *ShardConsumerWorker) setConsumerIsCurrentDoneToFalse() {
consumerTaskLock.Lock()
defer consumerTaskLock.Unlock()
consumer.isCurrentDone = false
}
func (consumer *ShardConsumerWorker) setConsumerIsCurrentDoneToTrue() {
consumerTaskLock.Lock()
defer consumerTaskLock.Unlock()
consumer.isCurrentDone = true
}
func (consumer *ShardConsumerWorker) getConsumerIsCurrentDoneStatus() bool {
consumerTaskLock.RLock()
defer consumerTaskLock.RUnlock()
return consumer.isCurrentDone
}
func (consumer *ShardConsumerWorker) setIsFlushCheckpointDoneToFalse() {
shutDownLock.Lock()
defer shutDownLock.Unlock()
consumer.isFlushCheckpointDone = false
}
func (consumer *ShardConsumerWorker) setIsFlushCheckpointDoneToTrue() {
shutDownLock.Lock()
defer shutDownLock.Unlock()
consumer.isFlushCheckpointDone = true
}
func (consumer *ShardConsumerWorker) getIsFlushCheckpointDoneStatus() bool {
shutDownLock.RLock()
defer shutDownLock.RUnlock()
return consumer.isFlushCheckpointDone
}