/
command.go
144 lines (121 loc) · 3.57 KB
/
command.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
package importer
import (
"errors"
"flag"
"io"
"os"
"time"
"github.com/influxdata/influxdb/cmd/influx_tools/internal/errlist"
"github.com/influxdata/influxdb/cmd/influx_tools/internal/format/binary"
"github.com/influxdata/influxdb/cmd/influx_tools/server"
"github.com/influxdata/influxdb/services/meta"
"github.com/influxdata/influxdb/tsdb/engine/tsm1"
"go.uber.org/zap"
)
// Command represents the program execution for "store query".
type Command struct {
// Standard input/output, overridden for testing.
Stderr io.Writer
Stdin io.Reader
Logger *zap.Logger
server server.Interface
configPath string
database string
retentionPolicy string
replication int
duration time.Duration
shardDuration time.Duration
buildTSI bool
replace bool
}
// NewCommand returns a new instance of Command.
func NewCommand(server server.Interface) *Command {
return &Command{
Stderr: os.Stderr,
Stdin: os.Stdin,
server: server,
}
}
// Run executes the import command using the specified args.
func (cmd *Command) Run(args []string) (err error) {
err = cmd.parseFlags(args)
if err != nil {
return err
}
err = cmd.server.Open(cmd.configPath)
if err != nil {
return err
}
i := newImporter(cmd.server, cmd.database, cmd.retentionPolicy, cmd.replace, cmd.buildTSI, cmd.Logger)
reader := binary.NewReader(cmd.Stdin)
_, err = reader.ReadHeader()
if err != nil {
return err
}
rp := &meta.RetentionPolicySpec{Name: cmd.retentionPolicy, ShardGroupDuration: cmd.shardDuration}
if cmd.duration >= time.Hour {
rp.Duration = &cmd.duration
}
if cmd.replication > 0 {
rp.ReplicaN = &cmd.replication
}
err = i.CreateDatabase(rp)
if err != nil {
return err
}
var bh *binary.BucketHeader
for bh, err = reader.NextBucket(); (bh != nil) && (err == nil); bh, err = reader.NextBucket() {
err = importShard(reader, i, bh.Start, bh.End)
if err != nil {
return err
}
}
return err
}
func importShard(reader *binary.Reader, i *importer, start int64, end int64) error {
err := i.StartShardGroup(start, end)
if err != nil {
return err
}
el := errlist.NewErrorList()
var sh *binary.SeriesHeader
var next bool
for sh, err = reader.NextSeries(); (sh != nil) && (err == nil); sh, err = reader.NextSeries() {
i.AddSeries(sh.SeriesKey)
pr := reader.Points()
seriesFieldKey := tsm1.SeriesFieldKeyBytes(string(sh.SeriesKey), string(sh.Field))
for next, err = pr.Next(); next && (err == nil); next, err = pr.Next() {
err = i.Write(seriesFieldKey, pr.Values())
if err != nil {
break
}
}
if err != nil {
break
}
}
el.Add(err)
el.Add(i.CloseShardGroup())
return el.Err()
}
func (cmd *Command) parseFlags(args []string) error {
fs := flag.NewFlagSet("import", flag.ContinueOnError)
fs.StringVar(&cmd.configPath, "config", "", "Config file")
fs.StringVar(&cmd.database, "database", "", "Database name")
fs.StringVar(&cmd.retentionPolicy, "rp", "", "Retention policy")
fs.IntVar(&cmd.replication, "replication", 0, "Retention policy replication")
fs.DurationVar(&cmd.duration, "duration", time.Hour*0, "Retention policy duration")
fs.DurationVar(&cmd.shardDuration, "shard-duration", time.Hour*24*7, "Retention policy shard duration")
fs.BoolVar(&cmd.buildTSI, "build-tsi", false, "Build the on disk TSI")
fs.BoolVar(&cmd.replace, "replace", false, "Enables replacing an existing retention policy")
if err := fs.Parse(args); err != nil {
return err
}
if cmd.database == "" {
return errors.New("database is required")
}
if cmd.retentionPolicy == "" {
return errors.New("retention policy is required")
}
return nil
}