forked from crask/kafka-pusher
/
worker.go
208 lines (176 loc) · 6.28 KB
/
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
package main
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
"github.com/crask/kafka/consumergroup"
"github.com/golang/glog"
"gopkg.in/Shopify/sarama.v1"
)
type Worker struct {
Callback *WorkerCallback
Topics []string
Zookeeper []string
ZkPath string
Consumer *consumergroup.ConsumerGroup
}
type WorkerCallback struct {
Url string
RetryTimes int
Timeout time.Duration
BypassFailed bool
FailedSleep time.Duration
}
func NewWorker() *Worker {
return &Worker{}
}
func (this *Worker) Init(config *CallbackItemConfig) error {
this.Callback = &WorkerCallback{
Url: config.Url,
RetryTimes: config.RetryTimes,
Timeout: config.Timeout,
BypassFailed: config.BypassFailed,
FailedSleep: config.FailedSleep,
}
this.Topics = config.Topics
this.Zookeeper = config.Zookeepers
this.ZkPath = config.ZkPath
this.Consumer = nil
cgConfig := consumergroup.NewConfig()
cgConfig.Offsets.ProcessingTimeout = 10 * time.Second
cgConfig.Offsets.CommitInterval = time.Duration(commitInterval) * time.Second
cgConfig.Offsets.Initial = sarama.OffsetNewest
// Random Sleeping to avoid burst commit onto ZK.
r := rand.Intn(commitInterval)
time.Sleep(time.Duration(r))
if len(this.ZkPath) > 0 {
cgConfig.Zookeeper.Chroot = this.ZkPath
}
cgName := this.getGroupName()
consumer, err := consumergroup.JoinConsumerGroup(cgName, this.Topics, this.Zookeeper, cgConfig)
if err != nil {
glog.Errorf("Failed to join consumer group for url[%v], %v", this.Callback.Url, err.Error())
return err
} else {
glog.V(1).Infof("Join consumer group for url[%s] with UUID[%s]", this.Callback.Url, cgName)
}
glog.Infoln(consumer)
this.Consumer = consumer
return nil
}
func (this *Worker) getGroupName() string {
m := md5.New()
m.Write([]byte(this.Callback.Url))
s := hex.EncodeToString(m.Sum(nil))
return s
}
func (this *Worker) Work() {
consumer := this.Consumer
go func() {
for err := range consumer.Errors() {
glog.Errorln("Error working consumers", err)
}
}()
eventCount := 0
offsets := make(map[string]map[int32]int64)
for message := range consumer.Messages() {
if offsets[message.Topic] == nil {
offsets[message.Topic] = make(map[int32]int64)
}
eventCount += 1
if offsets[message.Topic][message.Partition] != 0 && offsets[message.Topic][message.Partition] != message.Offset-1 {
glog.Errorf("Unexpected offset on %s:%d. Expected %d, found %d, diff %d.\n", message.Topic, message.Partition, offsets[message.Topic][message.Partition]+1, message.Offset, message.Offset-offsets[message.Topic][message.Partition]+1)
}
msg := CreateMsg(message)
glog.V(2).Infof("received message,[topic:%s][partition:%d][offset:%d]", msg.Topic, msg.Partition, msg.Offset)
deliverySuccessed := false
retry_times := 0
for {
for !deliverySuccessed && retry_times < this.Callback.RetryTimes {
deliverySuccessed, _ = this.delivery(msg, retry_times)
if !deliverySuccessed {
retry_times++
} else {
break
}
}
if deliverySuccessed {
break
}
if this.Callback.BypassFailed {
glog.Errorf("tried to delivery message [url:%s][topic:%s][partition:%d][offset:%d] for %d times and all failed. BypassFailed is :%t ,will not retry", this.Callback.Url, msg.Topic, msg.Partition, msg.Offset, retry_times, this.Callback.BypassFailed)
break
} else {
glog.Errorf("tried to delivery message [url:%s][topic:%s][partition:%d][offset:%d] for %d times and all failed. BypassFailed is :%t ,sleep %s to retry", this.Callback.Url, msg.Topic, msg.Partition, msg.Offset, retry_times, this.Callback.BypassFailed, this.Callback.FailedSleep)
time.Sleep(this.Callback.FailedSleep)
retry_times = 0
}
}
offsets[message.Topic][message.Partition] = message.Offset
consumer.CommitUpto(message)
glog.Infof("commited message,[topic:%s][partition:%d][offset:%d][url:%s]", msg.Topic, msg.Partition, msg.Offset, this.Callback.Url)
}
}
func (this *Worker) delivery(msg *Msg, retry_times int) (success bool, err error) {
glog.V(2).Infof("delivery message,[url:%s][retry_times:%d][topic:%s][partition:%d][offset:%d]", this.Callback.Url, retry_times, msg.Topic, msg.Partition, msg.Offset)
client := &http.Client{}
client.Timeout = this.Callback.Timeout
type RMSG struct {
Topic string `json:"Topic"`
PartitionKey string `json:"PartitionKey"`
TimeStamp int `json:"TimeStamp"`
Data string `json:"Data"`
LogId string `json:"LogId"`
ContentType string `json:"ContentType"`
}
var rmsg RMSG
json.Unmarshal(msg.Value, &rmsg)
req, _ := http.NewRequest("POST", this.Callback.Url, ioutil.NopCloser(strings.NewReader(rmsg.Data)))
if rmsg.ContentType == "" {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; param=value")
} else {
req.Header.Set("Content-Type", rmsg.ContentType)
}
req.Header.Set("User-Agent", "Taiji pusher consumer(go)/v"+VERSION)
req.Header.Set("X-Retry-Times", fmt.Sprintf("%d", retry_times))
req.Header.Set("X-Kmq-Topic", msg.Topic)
req.Header.Set("X-Kmq-Partition", fmt.Sprintf("%d", msg.Partition))
req.Header.Set("X-Kmq-Partition-Key", rmsg.PartitionKey)
req.Header.Set("X-Kmq-Offset", fmt.Sprintf("%d", msg.Offset))
req.Header.Set("X-Kmq-Logid", fmt.Sprintf("%s", rmsg.LogId))
req.Header.Set("Meilishuo", "uid:0;ip:0.0.0.0;v:0;master:0")
req.Header.Set("X-Kmq-Timestamp", fmt.Sprintf("%d", rmsg.TimeStamp))
resp, err := client.Do(req)
suc := true
if nil == err {
defer resp.Body.Close()
suc = (resp.StatusCode == 200)
if suc == false {
rbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
rbody = []byte{}
}
glog.Errorf("delivery failed,[retry_times:%d][topic:%s][partition:%d][offset:%d][msg:%s][url:%s][http_code:%d][response_body:%s]",
retry_times, msg.Topic, msg.Partition, msg.Offset, rmsg.Data, this.Callback.Url, resp.StatusCode, rbody)
}
} else {
glog.Errorf("delivery failed,[retry_times:%d][topic:%s][partition:%d][offset:%d][msg:%s][url:%s][error:%s]",
retry_times, msg.Topic, msg.Partition, msg.Offset, rmsg.Data, this.Callback.Url, err.Error())
suc = false
}
return suc, err
}
func (this *Worker) Closed() bool {
return this.Consumer.Closed()
}
func (this *Worker) Close() {
if err := this.Consumer.Close(); err != nil {
glog.Errorln("Error closing consumers", err)
}
}