forked from galaxydi/go-loghub
-
Notifications
You must be signed in to change notification settings - Fork 113
/
client_consumer.go
203 lines (182 loc) · 5.31 KB
/
client_consumer.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
package sls
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"github.com/go-kit/kit/log/level"
)
// ConsumerGroup type define
type ConsumerGroup struct {
ConsumerGroupName string `json:"consumerGroup"`
Timeout int `json:"timeout"` // timeout seconds
InOrder bool `json:"order"`
}
// ConsumerGroupCheckPoint type define
type ConsumerGroupCheckPoint struct {
ShardID int `json:"shard"`
CheckPoint string `json:"checkpoint"`
UpdateTime int64 `json:"updateTime"`
Consumer string `json:"consumer"`
}
// CreateConsumerGroup ...
func (c *Client) CreateConsumerGroup(project, logstore string, cg ConsumerGroup) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
body, err := json.Marshal(cg)
uri := fmt.Sprintf("/logstores/%v/consumergroups", logstore)
_, err = c.request(project, "POST", uri, h, body)
if err != nil {
return NewClientError(err)
}
return nil
}
// UpdateConsumerGroup ...
func (c *Client) UpdateConsumerGroup(project, logstore string, cg ConsumerGroup) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
body, err := json.Marshal(cg)
uri := fmt.Sprintf("/logstores/%v/consumergroups/%v", logstore, cg.ConsumerGroupName)
_, err = c.request(project, "PUT", uri, h, body)
if err != nil {
return NewClientError(err)
}
return nil
}
// DeleteConsumerGroup ...
func (c *Client) DeleteConsumerGroup(project, logstore string, cgName string) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/logstores/%v/consumergroups/%v", logstore, cgName)
_, err = c.request(project, "DELETE", uri, h, nil)
if err != nil {
return NewClientError(err)
}
return nil
}
// ListConsumerGroup ...
func (c *Client) ListConsumerGroup(project, logstore string) (cgList []*ConsumerGroup, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/logstores/%v/consumergroups", logstore)
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, NewClientError(err)
}
defer r.Body.Close()
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, NewClientError(err)
}
if r.StatusCode != http.StatusOK {
errMsg := &Error{}
err = json.Unmarshal(buf, errMsg)
if err != nil {
err = fmt.Errorf("failed to split shards")
if IsDebugLevelMatched(5) {
dump, _ := httputil.DumpResponse(r, true)
level.Error(Logger).Log("msg", string(dump))
}
return nil, NewClientError(err)
}
return nil, errMsg
}
type getConsumerGroup struct {
ConsumerGroupName string `json:"name"` // for getConsumerGroup, this is "name"
Timeout int `json:"timeout"` // timeout seconds
InOrder bool `json:"order"`
}
var cgListRaw []*getConsumerGroup
err = json.Unmarshal(buf, &cgListRaw)
for _, rawCg := range cgListRaw {
cgList = append(cgList, &ConsumerGroup{
ConsumerGroupName: rawCg.ConsumerGroupName,
Timeout: rawCg.Timeout,
InOrder: rawCg.InOrder,
})
}
return
}
// HeartBeat ...
func (c *Client) HeartBeat(project, logstore string, cgName, consumer string, heartBeatShardIDs []int) (shardIDs []int, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
if heartBeatShardIDs == nil {
heartBeatShardIDs = []int{}
}
body, err := json.Marshal(heartBeatShardIDs)
if err != nil {
return nil, NewClientError(err)
}
urlVal := url.Values{}
urlVal.Add("type", "heartbeat")
urlVal.Add("consumer", consumer)
uri := fmt.Sprintf("/logstores/%v/consumergroups/%v?%v", logstore, cgName, urlVal.Encode())
r, err := c.request(project, "POST", uri, h, body)
if err != nil {
return nil, NewClientError(err)
}
defer r.Body.Close()
buf, _ := ioutil.ReadAll(r.Body)
var shards []int
json.Unmarshal(buf, &shards)
for _, v := range shards {
shardIDs = append(shardIDs, v)
}
return shardIDs, nil
}
// UpdateCheckpoint ...
func (c *Client) UpdateCheckpoint(project, logstore string, cgName string, consumer string, shardID int, checkpoint string, forceSuccess bool) (err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
"Content-Type": "application/json",
}
b := map[string]interface{}{
"shard": shardID,
"checkpoint": checkpoint,
}
body, err := json.Marshal(b)
if err != nil {
return NewClientError(err)
}
urlVal := url.Values{}
urlVal.Add("type", "checkpoint")
urlVal.Add("consumer", consumer)
if forceSuccess {
urlVal.Add("forceSuccess", "true")
} else {
urlVal.Add("forceSuccess", "false")
}
// fmt.Println(urlVal.Encode())
uri := fmt.Sprintf("/logstores/%v/consumergroups/%v?%v", logstore, cgName, urlVal.Encode())
_, err = c.request(project, "POST", uri, h, body)
if err != nil {
return NewClientError(err)
}
return nil
}
// GetCheckpoint ...
func (c *Client) GetCheckpoint(project, logstore string, cgName string) (checkPointList []*ConsumerGroupCheckPoint, err error) {
h := map[string]string{
"x-log-bodyrawsize": "0",
}
uri := fmt.Sprintf("/logstores/%v/consumergroups/%v", logstore, cgName)
r, err := c.request(project, "GET", uri, h, nil)
if err != nil {
return nil, NewClientError(err)
}
defer r.Body.Close()
buf, _ := ioutil.ReadAll(r.Body)
err = json.Unmarshal(buf, &checkPointList)
return
}