forked from gwatts/dyndump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_dump.go
233 lines (201 loc) · 4.95 KB
/
cmd_dump.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
// Copyright 2016 Gareth Watts
// Licensed under an MIT license
// See the LICENSE file for details
package main
import (
"errors"
"fmt"
"io"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/Foyer-Inc/dyndump/dyndump"
"gopkg.in/cheggaaa/pb.v1"
)
const (
s3ObjectNotFound = "NoSuchKey"
)
type writers struct {
io.Writer
fileWriter io.WriteCloser
s3Writer *dyndump.S3Writer
s3RunErr chan error
}
func (w *writers) Close() error {
if w.fileWriter != nil {
if w, ok := w.Writer.(io.Closer); ok {
w.Close()
}
}
if w.s3Writer != nil {
if err := w.s3Writer.Close(); err != nil {
return err
}
return <-w.s3RunErr
}
return nil
}
func (w *writers) Abort() {
if w.fileWriter != nil {
if w, ok := w.Writer.(io.Closer); ok {
w.Close()
}
}
if w.s3Writer != nil {
w.s3Writer.Abort()
<-w.s3RunErr
}
}
type dumper struct {
f *dyndump.Fetcher
abortChan chan struct{}
tableBytes int64
startTime time.Time
dyn *dynamodb.DynamoDB
tableInfo *dynamodb.TableDescription
// options
tableName *string
consistentRead *bool
filename *string
stdout *bool
maxItems *int
parallel *int
readCapacity *int
s3BucketName *string
s3Prefix *string
}
func (d *dumper) openS3Writer() (*dyndump.S3Writer, error) {
// check if already exists
svc := s3.New(session.New())
r := dyndump.S3Reader{
S3: svc,
Bucket: *d.s3BucketName,
PathPrefix: *d.s3Prefix,
}
md, err := r.Metadata()
if err == nil {
// no error; successfully pulled existing metadata
return nil, fmt.Errorf("backup already exists for path prefix=%q table_name=%q",
*d.s3Prefix, md.TableName)
}
if aerr, ok := err.(awserr.Error); !ok || (ok && aerr.Code() != s3ObjectNotFound) {
return nil, err
}
// metadata wasn't found; ok to continue
md = dyndump.Metadata{
TableName: *d.tableName,
TableARN: aws.StringValue(d.tableInfo.TableArn),
}
return dyndump.NewS3Writer(svc, *d.s3BucketName, *d.s3Prefix, md), nil
}
func (d *dumper) openWriters() *writers {
var fout io.Writer
ws := new(writers)
if *d.stdout {
fout = os.Stdout
} else if *d.filename != "" {
var err error
fout, err = os.Create(*d.filename)
if err != nil {
fail("Failed to open file for write: %s", err)
}
}
if *d.s3BucketName != "" {
if *d.s3Prefix == "" {
fail("s3-prefix not set")
}
w, err := d.openS3Writer()
if err != nil {
fail("Failed: %v", err)
}
ws.s3Writer = w
ws.s3Writer.MaxParallel = *d.parallel // match fetcher parallelism
ws.s3RunErr = make(chan error)
if fout != nil {
// stream to both
ws.Writer = io.MultiWriter(fout, ws.s3Writer)
} else {
ws.Writer = ws.s3Writer
}
go func() { ws.s3RunErr <- ws.s3Writer.Run() }()
return ws
}
if fout == nil {
fail("Either s3-bucket & s3-prefix, or filename must be set")
}
// no s3
ws.Writer = fout
return ws
}
func (d *dumper) init() error {
d.dyn = dynamodb.New(session.New())
resp, err := d.dyn.DescribeTable(&dynamodb.DescribeTableInput{
TableName: d.tableName,
})
if err != nil {
return err
}
d.tableInfo = resp.Table
return nil
}
func (d *dumper) start(infoWriter io.Writer) (done chan error, err error) {
out := d.openWriters()
w := dyndump.NewSimpleEncoder(out)
fmt.Fprintf(infoWriter, "Beginning scan: table=%q readCapacity=%d parallel=%d itemCount=%d totalSize=%s\n",
*d.tableName, *d.readCapacity, *d.parallel,
aws.Int64Value(d.tableInfo.ItemCount), fmtBytes(aws.Int64Value(d.tableInfo.TableSizeBytes)))
d.f = &dyndump.Fetcher{
Dyn: d.dyn,
TableName: *d.tableName,
ConsistentRead: *d.consistentRead,
MaxParallel: *d.parallel,
MaxItems: int64(*d.maxItems),
ReadCapacity: float64(*d.readCapacity),
Writer: w,
}
done = make(chan error)
d.abortChan = make(chan struct{}, 1)
d.startTime = time.Now()
go func() {
rerr := make(chan error)
go func() { rerr <- d.f.Run() }()
select {
case <-d.abortChan:
d.f.Stop()
<-rerr
out.Abort()
done <- errors.New("Aborted")
case err := <-rerr:
if err != nil {
out.Abort()
done <- err
} else {
done <- out.Close()
}
}
}()
return done, nil
}
func (d *dumper) newProgressBar() *pb.ProgressBar {
bar := pb.New64(aws.Int64Value(d.tableInfo.TableSizeBytes))
bar.ShowSpeed = true
bar.SetUnits(pb.U_BYTES)
return bar
}
func (d *dumper) updateProgress(bar *pb.ProgressBar) {
bar.Set64(d.f.Stats().BytesRead)
}
func (d *dumper) abort() {
d.abortChan <- struct{}{}
}
func (d *dumper) printFinalStats(w io.Writer) {
finalStats := d.f.Stats()
deltaSeconds := float64(time.Since(d.startTime) / time.Second)
fmt.Fprintf(w, "Avg items/sec: %.2f\n", float64(finalStats.ItemsRead)/deltaSeconds)
fmt.Fprintf(w, "Avg capacity/sec: %.2f\n", finalStats.CapacityUsed/deltaSeconds)
fmt.Fprintln(w, "Total items read: ", finalStats.ItemsRead)
}