Debian Codegraph @

cmd/influx-tools/importer/command.go

raw symbols SCIP
1 package importer
2
3 import (
4 "errors"
5 "flag"
6 "io"
7 "os"
8 "time"
9
10 "github.com/influxdata/influxdb/cmd/influx-tools/internal/errlist"
11
12 "github.com/influxdata/influxdb/cmd/influx-tools/internal/format/binary"
13 "github.com/influxdata/influxdb/cmd/influx-tools/server"
14 "github.com/influxdata/influxdb/services/meta"
15 "github.com/influxdata/influxdb/tsdb/engine/tsm1"
16 "go.uber.org/zap"
17 )
18
19 // Command represents the program execution for "store query".
20 type Command struct {
21 // Standard input/output, overridden for testing.
22 Stderr io.Writer
23 Stdin io.Reader
24 Logger *zap.Logger
25 server server.Interface
26
27 configPath string
28 database string
29 retentionPolicy string
30 replication int
31 duration time.Duration
32 shardDuration time.Duration
33 buildTSI bool
34 replace bool
35 }
36
37 // NewCommand returns a new instance of Command.
38 func NewCommand(server server.Interface) *Command {
39 return &Command{
40 Stderr: os.Stderr,
41 Stdin: os.Stdin,
42 server: server,
43 }
44 }
45
46 // Run executes the import command using the specified args.
47 func (cmd *Command) Run(args []string) (err error) {
48 err = cmd.parseFlags(args)
49 if err != nil {
50 return err
51 }
52
53 err = cmd.server.Open(cmd.configPath)
54 if err != nil {
55 return err
56 }
57
58 i := newImporter(cmd.server, cmd.database, cmd.retentionPolicy, cmd.replace, cmd.buildTSI, cmd.Logger)
59
60 reader := binary.NewReader(cmd.Stdin)
61 _, err = reader.ReadHeader()
62 if err != nil {
63 return err
64 }
65
66 rp := &meta.RetentionPolicySpec{Name: cmd.retentionPolicy, ShardGroupDuration: cmd.shardDuration}
67 if cmd.duration >= time.Hour {
68 rp.Duration = &cmd.duration
69 }
70 if cmd.replication > 0 {
71 rp.ReplicaN = &cmd.replication
72 }
73 err = i.CreateDatabase(rp)
74 if err != nil {
75 return err
76 }
77
78 var bh *binary.BucketHeader
79 for bh, err = reader.NextBucket(); (bh != nil) && (err == nil); bh, err = reader.NextBucket() {
80 err = importShard(reader, i, bh.Start, bh.End)
81 if err != nil {
82 return err
83 }
84 }
85
86 return err
87 }
88
89 func importShard(reader *binary.Reader, i *importer, start int64, end int64) error {
90 err := i.StartShardGroup(start, end)
91 if err != nil {
92 return err
93 }
94
95 el := errlist.NewErrorList()
96 var sh *binary.SeriesHeader
97 var next bool
98 for sh, err = reader.NextSeries(); (sh != nil) && (err == nil); sh, err = reader.NextSeries() {
99 i.AddSeries(sh.SeriesKey)
100 pr := reader.Points()
101 seriesFieldKey := tsm1.SeriesFieldKeyBytes(string(sh.SeriesKey), string(sh.Field))
102
103 for next, err = pr.Next(); next && (err == nil); next, err = pr.Next() {
104 err = i.Write(seriesFieldKey, pr.Values())
105 if err != nil {
106 break
107 }
108 }
109 if err != nil {
110 break
111 }
112 }
113
114 el.Add(err)
115 el.Add(i.CloseShardGroup())
116
117 return el.Err()
118 }
119
120 func (cmd *Command) parseFlags(args []string) error {
121 fs := flag.NewFlagSet("import", flag.ContinueOnError)
122 fs.StringVar(&cmd.configPath, "config", "", "Config file")
123 fs.StringVar(&cmd.database, "database", "", "Database name")
124 fs.StringVar(&cmd.retentionPolicy, "rp", "", "Retention policy")
125 fs.IntVar(&cmd.replication, "replication", 0, "Retention policy replication")
126 fs.DurationVar(&cmd.duration, "duration", time.Hour*0, "Retention policy duration")
127 fs.DurationVar(&cmd.shardDuration, "shard-duration", time.Hour*24*7, "Retention policy shard duration")
128 fs.BoolVar(&cmd.buildTSI, "build-tsi", false, "Build the on disk TSI")
129 fs.BoolVar(&cmd.replace, "replace", false, "Enables replacing an existing retention policy")
130
131 if err := fs.Parse(args); err != nil {
132 return err
133 }
134
135 if cmd.database == "" {
136 return errors.New("database is required")
137 }
138
139 if cmd.retentionPolicy == "" {
140 return errors.New("retention policy is required")
141 }
142
143 return nil
144 }