forked from fujiwara/kinesis-tailf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkinesis.go
224 lines (198 loc) · 4.39 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
package ktail
import (
"bufio"
"context"
"crypto/md5"
"log"
"math/big"
"os"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/kinesis"
"github.com/aws/aws-sdk-go-v2/service/kinesis/types"
"github.com/fujiwara/kinesis-tailf/kpl"
)
var (
flushInterval = 100 * time.Millisecond
iterateInterval = time.Second
LF = []byte{'\n'}
maxEmptyIterates = 100
)
type IterateParams struct {
StreamName string
ShardID string
StartTimestamp time.Time
EndTimestamp time.Time
}
type isOverFunc func(time.Time, ...bool) bool
//go:generate protoc --go_out=kpl --go_opt=paths=source_relative ./kpl.proto
type App struct {
kinesis *kinesis.Client
cfg aws.Config
StreamName string
AppendLF bool
}
func New(cfg aws.Config, name string) *App {
return &App{
kinesis: kinesis.NewFromConfig(cfg),
cfg: cfg,
StreamName: name,
}
}
func (app *App) Run(ctx context.Context, shardKey string, startTs, endTs time.Time) error {
shardIds, err := app.determinShardIds(ctx, shardKey)
if err != nil {
return err
}
var wg, wgW sync.WaitGroup
ch := make(chan []byte, 1000)
ctxC, cancel := context.WithCancel(ctx)
wgW.Add(1)
go app.writer(ctxC, ch, &wgW)
for _, id := range shardIds {
wg.Add(1)
go func(id string) {
param := IterateParams{
ShardID: id,
StartTimestamp: startTs,
EndTimestamp: endTs,
}
err := app.iterate(ctx, param, ch)
if err != nil {
log.Println(err)
}
wg.Done()
}(id)
}
wg.Wait()
cancel()
close(ch)
wgW.Wait()
return nil
}
func (app *App) iterate(ctx context.Context, p IterateParams, ch chan []byte) error {
in := &kinesis.GetShardIteratorInput{
ShardId: aws.String(p.ShardID),
StreamName: aws.String(app.StreamName),
}
if p.StartTimestamp.IsZero() {
in.ShardIteratorType = types.ShardIteratorTypeLatest
} else {
in.ShardIteratorType = types.ShardIteratorTypeAtTimestamp
in.Timestamp = &(p.StartTimestamp)
}
var isOver isOverFunc
var emptyHits int
if p.EndTimestamp.IsZero() {
isOver = func(_ time.Time, _ ...bool) bool {
return false
}
} else {
isOver = func(t time.Time, empty ...bool) bool {
if len(empty) > 0 && empty[0] {
emptyHits++
return maxEmptyIterates <= emptyHits
}
return p.EndTimestamp.Before(t)
}
}
r, err := app.kinesis.GetShardIterator(ctx, in)
if err != nil {
return err
}
itr := r.ShardIterator
for {
rr, err := app.kinesis.GetRecords(ctx, &kinesis.GetRecordsInput{
Limit: aws.Int32(1000),
ShardIterator: itr,
})
if err != nil {
return err
}
itr = rr.NextShardIterator
for _, record := range rr.Records {
if isOver(*record.ApproximateArrivalTimestamp) {
return nil
}
ar, err := kpl.Unmarshal(record.Data)
if err == nil {
for _, r := range ar.Records {
ch <- r.Data
}
} else {
ch <- record.Data
}
}
if len(rr.Records) == 0 {
if isOver(time.Now(), true) {
return nil
}
time.Sleep(iterateInterval)
}
}
}
func (app *App) writer(ctx context.Context, ch chan []byte, wg *sync.WaitGroup) {
defer wg.Done()
var mu sync.Mutex
w := bufio.NewWriter(os.Stdout)
defer w.Flush()
// run periodical flusher
ticker := time.NewTicker(flushInterval)
go func() {
for {
select {
case <-ticker.C:
mu.Lock()
w.Flush()
mu.Unlock()
case <-ctx.Done():
return
}
}
}()
for {
b, ok := <-ch
if !ok {
// channel closed
return
}
mu.Lock()
w.Write(b)
if app.AppendLF {
w.Write(LF)
}
mu.Unlock()
}
}
func toHashKey(s string) *big.Int {
b := md5.Sum([]byte(s))
return big.NewInt(0).SetBytes(b[:])
}
func (app *App) determinShardIds(ctx context.Context, shardKey string) ([]string, error) {
var shardIds []string
sd, err := app.kinesis.DescribeStream(ctx, &kinesis.DescribeStreamInput{
StreamName: aws.String(app.StreamName),
})
if err != nil {
return shardIds, err
}
if shardKey == "" {
// all shards
for _, s := range sd.StreamDescription.Shards {
shardIds = append(shardIds, *s.ShardId)
}
return shardIds, nil
}
hashKey := toHashKey(shardKey)
for _, s := range sd.StreamDescription.Shards {
start, end := big.NewInt(0), big.NewInt(0)
start.SetString(*s.HashKeyRange.StartingHashKey, 10)
end.SetString(*s.HashKeyRange.EndingHashKey, 10)
if start.Cmp(hashKey) <= 0 && hashKey.Cmp(end) <= 0 {
shardIds = append(shardIds, *s.ShardId)
break
}
}
return shardIds, nil
}