-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkinesis.go
290 lines (240 loc) · 7.21 KB
/
kinesis.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
package kinetic
import (
"errors"
"net"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
awsKinesis "github.com/aws/aws-sdk-go/service/kinesis"
"github.com/aws/aws-sdk-go/service/kinesis/kinesisiface"
)
const (
atSequenceNumber = iota
afterSequenceNumber
trimHorizon
latest
statusCreating = iota
statusDeleting
statusActive
staticUpdating
kinesisWritesPerSec int = 1000
kinesisReadsPerSec int = 2
// Timeout TODO
)
var (
// HTTPTimeout is the timeout for http for requests.
HTTPTimeout = 60 * time.Second
// DialTimeout is the timeout for the tpc dial.
DialTimeout = 5 * time.Second
// HandShakeTimeout is the timeout for the tpc handshake.
HandShakeTimeout = 5 * time.Second
)
// ErrNilShardIterator is an error for when we get back a nil shard iterator
var ErrNilShardIterator = errors.New("Nil shard iterator")
// ErrNilShardStatus is an error for when we get back a nil stream status
var ErrNilShardStatus = errors.New("Nil stream status")
// Empty is an empty struct. It is mostly used for counting semaphore purposes
type Empty struct{}
var (
conf = getConfig()
// ShardIterTypes are the types of iterators to use within Kinesis
ShardIterTypes shardIteratorTypes = map[int]string{
atSequenceNumber: "AT_SEQUENCE_NUMBER",
afterSequenceNumber: "AFTER_SEQUENCE_NUMBER",
trimHorizon: "TRIM_HORIZON",
latest: "LATEST",
}
streamStatuses streamStatusTypes = map[int]string{
statusCreating: "CREATING",
statusDeleting: "DELETING",
statusActive: "ACTIVE",
staticUpdating: "UPDATING",
}
)
type msgFn func([]byte, *sync.WaitGroup)
type shardIteratorTypes map[int]string
type streamStatusTypes map[int]string
type kinesis struct {
stream string
shard string
endPoint string
shardIteratorType string
shardIterator string
sequenceNumber string
sequenceNumberMu sync.Mutex
client kinesisiface.KinesisAPI
msgCount int64
errCount int64
startTime *time.Time
}
func (k *kinesis) initWithStartTime(stream, shard, shardIteratorType, accessKey, secretKey, region string, endpoint string, startTime *time.Time) (*kinesis, error) {
httpClient := &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: DialTimeout,
}).Dial,
TLSHandshakeTimeout: HandShakeTimeout},
Timeout: HTTPTimeout,
}
sess, err := authenticate(accessKey, secretKey)
awsConf := aws.NewConfig().WithRegion(region).WithHTTPClient(httpClient)
if conf.Debug.Verbose {
awsConf = awsConf.WithLogLevel(aws.LogDebugWithRequestRetries | aws.LogDebugWithRequestErrors)
}
if k.endPoint != "" {
awsConf = awsConf.WithEndpoint(k.endPoint)
} else if endpoint != "" {
awsConf = awsConf.WithEndpoint(endpoint)
}
client := awsKinesis.New(sess, awsConf)
k = &kinesis{
stream: stream,
shard: shard,
shardIteratorType: "AT_TIMESTAMP",
client: client,
startTime: startTime,
}
if err != nil {
return k, err
}
err = k.initShardIterator()
return k, err
}
func (k *kinesis) init(stream, shard, shardIteratorType, accessKey, secretKey, region string, endpoint string) (*kinesis, error) {
httpClient := &http.Client{
Transport: &http.Transport{
Dial: (&net.Dialer{
Timeout: DialTimeout,
}).Dial,
TLSHandshakeTimeout: HandShakeTimeout},
Timeout: HTTPTimeout,
}
sess, err := authenticate(accessKey, secretKey)
awsConf := aws.NewConfig().WithRegion(region).WithHTTPClient(httpClient)
if conf.Debug.Verbose {
awsConf = awsConf.WithLogLevel(aws.LogDebugWithRequestRetries | aws.LogDebugWithRequestErrors)
}
if k.endPoint != "" {
awsConf = awsConf.WithEndpoint(k.endPoint)
} else if endpoint != "" {
awsConf = awsConf.WithEndpoint(endpoint)
}
client := awsKinesis.New(sess, awsConf)
k = &kinesis{
stream: stream,
shard: shard,
shardIteratorType: shardIteratorType,
client: client,
}
if err != nil {
return k, err
}
err = k.initShardIterator()
return k, err
}
func (k *kinesis) initShardIterator() error {
var awsSeqNumber *string
var err error
var resp *awsKinesis.GetShardIteratorOutput
if k.shardIteratorType == "AT_TIMESTAMP" && k.sequenceNumber == "" {
resp, err = k.client.GetShardIterator(&awsKinesis.GetShardIteratorInput{
ShardId: aws.String(k.shard),
ShardIteratorType: &k.shardIteratorType,
StreamName: aws.String(k.stream),
Timestamp: k.startTime,
})
k.shardIteratorType = ShardIterTypes[atSequenceNumber]
} else {
if k.sequenceNumber != "" {
awsSeqNumber = aws.String(k.sequenceNumber)
k.shardIteratorType = ShardIterTypes[atSequenceNumber]
}
resp, err = k.client.GetShardIterator(&awsKinesis.GetShardIteratorInput{
ShardId: aws.String(k.shard), // Required
ShardIteratorType: aws.String(k.shardIteratorType), // Required
StreamName: aws.String(k.stream), // Required
StartingSequenceNumber: awsSeqNumber,
})
}
if err != nil {
return err
}
if resp.ShardIterator != nil {
return k.setShardIterator(*resp.ShardIterator)
}
return ErrNilShardIterator
}
func (k *kinesis) setSequenceNumber(sequenceNum string) {
if sequenceNum == "" || len(sequenceNum) == 0 {
return
}
k.sequenceNumberMu.Lock()
k.sequenceNumber = sequenceNum
k.sequenceNumberMu.Unlock()
}
func (k *kinesis) setShardIterator(shardIter string) error {
if shardIter == "" || len(shardIter) == 0 {
return errors.New("Attempted to set shard iterator with empty value")
}
k.shardIterator = shardIter
return nil
}
func (k *kinesis) checkActive() (bool, error) {
status, err := k.client.DescribeStream(&awsKinesis.DescribeStreamInput{
StreamName: aws.String(k.stream), // Required
})
if err != nil {
return false, err
}
if status.StreamDescription.StreamStatus == nil {
return false, ErrNilShardStatus
}
if streamStatuses[statusActive] == *status.StreamDescription.StreamStatus {
return true, nil
}
return false, nil
}
func (k *kinesis) newClient(endpoint, stream string) (*awsKinesis.Kinesis, error) {
k.endPoint = endpoint
conf := &aws.Config{}
conf = conf.WithCredentials(
credentials.NewStaticCredentials("BAD_ACCESS_KEY", "BAD_SECRET_KEY", "BAD_TOKEN"),
).WithEndpoint(endpoint).WithRegion("us-east-1").WithDisableSSL(true).WithMaxRetries(3)
// fake region
sess, err := session.NewSessionWithOptions(session.Options{Config: *conf})
return awsKinesis.New(sess, conf), err
}
func (k *kinesis) refreshClient(accessKey, secretKey, region string) error {
sess, err := authenticate(accessKey, secretKey)
conf := aws.NewConfig().WithRegion(region).WithEndpoint(k.endPoint)
if err != nil {
return err
}
k.client = awsKinesis.New(sess, conf)
return nil
}
func (k *kinesis) decMsgCount() {
atomic.AddInt64(&k.msgCount, -1)
}
func (k *kinesis) incMsgCount() {
atomic.AddInt64(&k.msgCount, 1)
}
func (k *kinesis) getMsgCount() int64 {
return atomic.LoadInt64(&k.msgCount)
}
func (k *kinesis) decErrCount() {
atomic.AddInt64(&k.errCount, -1)
}
func (k *kinesis) incErrCount() {
atomic.AddInt64(&k.errCount, 1)
}
func (k *kinesis) getErrCount() int64 {
return atomic.LoadInt64(&k.errCount)
}
func getLock(sem chan Empty) {
sem <- Empty{}
}