/
spur.go
301 lines (251 loc) · 9.27 KB
/
spur.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
291
292
293
294
295
296
297
298
299
// spur.go
package main
import (
"bufio"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/bobappleyard/readline"
"gopkg.in/alecthomas/kingpin.v2"
"io"
"log"
"os"
"path/filepath"
"strings"
"time"
)
var (
app *kingpin.Application
verbose bool
region, stream, partition, shardID string
iType string
iteratorType = &iType
// Prompt for Commands
interactive *kingpin.CmdClause
// Generate data.
gen *kingpin.CmdClause
genLog bool
genFile *kingpin.CmdClause
file *os.File
genItr *kingpin.CmdClause
numberOfIterations int
testString string
genPrompt *kingpin.CmdClause
// Read data.
read *kingpin.CmdClause
showEmptyReads bool
tail bool
sleepMilli int
streamGroup *KinesisStreamGroup
)
func init() {
app = kingpin.New("spur", "A command-line AWS Kinesis application.")
app.Flag("verbose", "Describe what is happening, as it happens.").Short('v').BoolVar(&verbose)
app.Flag("region", "Find the kinsesis stream in this AWS region.").Default("us-west-1").StringVar(®ion)
app.Flag("stream", "Use this Kinesis stream name").Default("JDR_TestStream_1").StringVar(&stream)
app.Flag("partition", "Identify as this Kinesis stream partition").Default("PARTITION").StringVar(&partition)
app.Flag("shard-id", "The Shard to read on the kinesis stream.").Default("shardId-000000000001").StringVar(&shardID)
// TODO: Accommodate the other two ShardIterator types (two flags?, Arg, Flag?)
// ShardIteratorType
// - "AT_SEQUENCE_NUMBER" start reading at a particular sequence numner
// - "AFTER_SEQUENCE_NUMBER" start reading right after the position indicated by the sequence number.
// - "TIME_HORIZON" start reading at the last untrimmed record (oldest).
// - "LATEST" start reading just after hte most recent record in the shard.
app.Flag("iterator-type", "Where to start reading the stream.").Default("LATEST").EnumVar(&iteratorType, "TRIM_HORIZON", "LATEST")
interactive = app.Command("interactive", "Prompt for commands.")
gen = app.Command("gen", "Put data into the Kinesis stream. This is inefficiently done a record at a time, no batching.")
gen.Flag("log", "Generate a log style prefix for each message including the current time. Default on, use --no-log to turn it off.").BoolVar(&genLog)
genFile = gen.Command("file", "Put data into the Kinesis stream from a file or stdin.")
genFile.Arg("file-name", "Name of file for reading newline separeted records, each record is sent to the Kinesis stream.").OpenFileVar(&file, os.O_RDONLY, 0666)
genItr = gen.Command("iterate", "Generate <iterations> log entires to the stream, using <test-string>.")
genItr.Arg("iterations", "Number of test string entries to send to the kinesis stream.").Required().IntVar(&numberOfIterations)
genItr.Arg("test-string", "String to send to the kinsesis stream.").Default("this is a test string").StringVar(&testString)
genPrompt = gen.Command("prompt", "Prompt for strings to send to the kinesis stream.")
read = app.Command("read", "Read from a kinesis stream.")
read.Flag("tail", "Continue waiting for records to read from the stream, will set latest unless -all specificed").Short('t').BoolVar(&tail)
read.Flag("sleep", "Delay in milliseconds for sleep between polls in tail mode.").Default("500").IntVar(&sleepMilli)
read.Flag("log-empty-reads", "Print out the empty reads and delay stats. This will happen with verbose as well.").BoolVar(&showEmptyReads)
kingpin.CommandLine.Help = `A command-line AWS Kinesis application.
Spur reads from the environment or ~/.aws/credentials for AWS credentials in the usual way. Unfortunately
it doesn't read out the ~/.aws/configuration file for other informaiton (e.g. region).
`
}
func main() {
// Parse the command line to fool with flags and get the command we'll execeute.
command := kingpin.MustParse(app.Parse(os.Args[1:]))
// Flag messiness.
shardIteratorType := *iteratorType
if tail {
// read = true;
shardIteratorType = "LATEST"
}
// The AWS library doesn't read configuariton information
// out of .aws/config, just the credentials from .aws/credentials.
if aws.DefaultConfig.Region == "" {
aws.DefaultConfig.Region = region
}
// Say Hello
if verbose {
// fmt.Println(aws.DefaultConfig.Credentials.Get())
fmt.Println("\nOpening up kinesies stream:", stream)
fmt.Println("To partition:", partition)
fmt.Println("In region:", region)
}
// List of commands as parsed matched against functions to execute the commands.
commandMap := map[string]func(*KinesisStream){
// interactive.FullCommand(): doInteractive,
genFile.FullCommand(): doPutFile,
genItr.FullCommand(): doIterate,
genPrompt.FullCommand(): doPrompt,
read.FullCommand(): doRead,
}
// Set up Kinesis.
kinesisStream := NewStream(aws.DefaultConfig, stream, partition, shardIteratorType, shardID)
// Execute the command.
if interactive.FullCommand() == command {
streamGroup, err := NewStreamGroup(aws.DefaultConfig)
streamGroup.CurrentStream = kinesisStream
if err != nil {
log.Fatal(err)
}
doInteractive(streamGroup)
} else {
commandMap[command](kinesisStream)
}
}
func doPutFile(s *KinesisStream) {
if file == nil {
if verbose {
fmt.Println("Reading from stdin, and putting the lines into the Kinesis stream.")
}
file = os.Stdin
} else {
if verbose {
// This is pretty close to disgusting.
absolutePath, _ := filepath.Abs(filepath.Join(filepath.Dir(file.Name()), file.Name()))
fmt.Printf("Putting file \"%s\" into the Kinesis stream.\n", absolutePath)
}
defer file.Close()
}
scanner := bufio.NewScanner(file)
i := 0
for scanner.Scan() {
resp, err := s.PutLogLine(scanner.Text())
if err != nil {
printAWSError(err)
}
if err = scanner.Err(); err != nil {
log.Fatal(err)
}
if verbose {
fmt.Printf("Put line %d\n", i)
fmt.Printf("Resp: %s\n", resp)
}
i++
}
}
// Generate data by iterating a test string out to the stream.
func doIterate(s *KinesisStream) {
if verbose {
fmt.Printf("Will push %v enties into the stream.\n", numberOfIterations)
fmt.Printf("Using the string: %s\n", testString)
}
for i := 0; i < numberOfIterations; i++ {
line := fmt.Sprintf("%s %d", testString, i)
resp, err := s.PutLogLine(line)
if err != nil {
log.Fatal(err)
}
if verbose {
if i%100 == 0 {
fmt.Printf("%d iteration %s\n", i, resp)
}
}
}
}
func doPrompt(s *KinesisStream) {
moreToRead := true
for moreToRead {
line, err := readline.String("Send to Kinesis, <crtl-d> to end: ")
if err == io.EOF {
moreToRead = false
} else if err != nil {
log.Fatal(err)
} else {
resp, err := s.PutLogLine(strings.TrimRight(line, "\n"))
if err != nil {
log.Fatal(err)
}
if verbose {
fmt.Println("Response:", awsutil.StringValue(resp))
}
readline.AddHistory(line)
}
}
}
// Read string and print them fromt he stream.
func doRead(s *KinesisStream) {
if verbose {
fmt.Println("\nReading from shard: ", s.ShardID)
fmt.Println("With iterator type:", s.ShardIteratorType)
}
var msecBehind int64 = 0
var lastDelay int64 = 0
emptyReads := 0
s.ReadReset()
for moreData := true; moreData; {
output, err := s.GetRecords()
if err != nil {
printAWSError(err)
log.Fatal(err)
}
// Keep reading until we're caught up or catchup and sleep if we're tailling.
msecBehind = *output.MillisBehindLatest
if msecBehind <= 0 {
if tail {
time.Sleep(time.Duration(sleepMilli) * time.Millisecond)
} else {
moreData = false
}
}
// Share what you got.
if showEmptyReads || verbose {
if len(output.Records) > 0 {
if emptyReads != 0 {
fmt.Println(emptyReads, "empty responses (no records) and delay is now:",
fmtMilliseconds(msecBehind), "behind the tip of the stream.")
}
if verbose {
fmt.Println("Got ", len(output.Records), " data records.")
fmt.Println("The response is ", fmtMilliseconds(msecBehind), " behind the tip of the stream.")
}
emptyReads = 0
} else {
if emptyReads == 0 {
if lastDelay != msecBehind {
lastDelay = msecBehind
fmt.Printf("The response is %s behind the top of the stream.\n",
fmtMilliseconds(msecBehind))
}
}
emptyReads++
}
}
for i, record := range output.Records {
if verbose {
fmt.Println("Data record: ", i+1)
fmt.Println("Partition: ", *record.PartitionKey)
fmt.Println("SequenceNumber: ", *record.SequenceNumber)
fmt.Printf("Data: ")
}
fmt.Println(string(record.Data))
}
}
}
func doInteractive(g *KinesisStreamGroup) {
// why can't I declare this inline in the promptLoop call?
xICommand := func(line string) (err error) {return DoICommand(line, g)}
prompt := g.CurrentStream.Name + "(" + g.Region + ") >"
err := promptLoop(prompt, xICommand)
if err != nil {fmt.Printf("Error - %s.\n", err)}
}