Debian Codegraph @

cmd/influx-tools/export/command.go

raw symbols SCIP
1 package export
2
3 import (
4 "compress/gzip"
5 "errors"
6 "flag"
7 "fmt"
8 "io"
9 "math"
10 "os"
11 "strconv"
12 "strings"
13 "time"
14
15 "github.com/influxdata/influxdb/cmd/influx-tools/internal/format"
16 "github.com/influxdata/influxdb/cmd/influx-tools/internal/format/binary"
17 "github.com/influxdata/influxdb/cmd/influx-tools/internal/format/line"
18 "github.com/influxdata/influxdb/cmd/influx-tools/internal/format/text"
19 "github.com/influxdata/influxdb/cmd/influx-tools/server"
20 "go.uber.org/zap"
21 )
22
23 var (
24 _ line.Writer
25 _ binary.Writer
26 )
27
28 // Command represents the program execution for "store query".
29 type Command struct {
30 // Standard input/output, overridden for testing.
31 Stderr io.Writer
32 Stdout io.Writer
33 Logger *zap.Logger
34 server server.Interface
35
36 conflicts io.WriteCloser
37
38 configPath string
39 database string
40 rp string
41 shardDuration time.Duration
42 format string
43 r rangeValue
44 conflictPath string
45 ignore bool
46 print bool
47 }
48
49 // NewCommand returns a new instance of the export Command.
50 func NewCommand(server server.Interface) *Command {
51 return &Command{
52 Stderr: os.Stderr,
53 Stdout: os.Stdout,
54 server: server,
55 }
56 }
57
58 // Run executes the export command using the specified args.
59 func (cmd *Command) Run(args []string) (err error) {
60 err = cmd.parseFlags(args)
61 if err != nil {
62 return err
63 }
64
65 err = cmd.server.Open(cmd.configPath)
66 if err != nil {
67 return err
68 }
69 defer cmd.server.Close()
70
71 e, err := cmd.openExporter()
72 if err != nil {
73 return err
74 }
75 defer e.Close()
76
77 e.PrintPlan(cmd.Stderr)
78
79 if cmd.print {
80 return nil
81 }
82
83 if !cmd.ignore {
84 if f, err := os.Create(cmd.conflictPath); err != nil {
85 return err
86 } else {
87 cmd.conflicts = gzip.NewWriter(f)
88 defer func() {
89 cmd.conflicts.Close()
90 f.Close()
91 }()
92 }
93 }
94
95 var wr format.Writer
96 switch cmd.format {
97 case "line":
98 wr = line.NewWriter(cmd.Stdout)
99 case "binary":
100 wr = binary.NewWriter(cmd.Stdout, cmd.database, cmd.rp, cmd.shardDuration)
101 case "series":
102 wr = text.NewWriter(cmd.Stdout, text.Series)
103 case "values":
104 wr = text.NewWriter(cmd.Stdout, text.Values)
105 case "discard":
106 wr = format.Discard
107 }
108 defer func() {
109 err = wr.Close()
110 }()
111
112 if cmd.conflicts != nil {
113 wr = format.NewConflictWriter(wr, line.NewWriter(cmd.conflicts))
114 } else {
115 wr = format.NewConflictWriter(wr, format.DevNull)
116 }
117
118 return e.WriteTo(wr)
119 }
120
121 func (cmd *Command) openExporter() (*exporter, error) {
122 cfg := &exporterConfig{Database: cmd.database, RP: cmd.rp, ShardDuration: cmd.shardDuration, Min: cmd.r.Min(), Max: cmd.r.Max()}
123 e, err := newExporter(cmd.server, cfg)
124 if err != nil {
125 return nil, err
126 }
127
128 return e, e.Open()
129 }
130
131 func (cmd *Command) parseFlags(args []string) error {
132 fs := flag.NewFlagSet("export", flag.ContinueOnError)
133 fs.StringVar(&cmd.configPath, "config", "", "Config file")
134 fs.StringVar(&cmd.database, "database", "", "Database name")
135 fs.StringVar(&cmd.rp, "rp", "", "Retention policy name")
136 fs.StringVar(&cmd.format, "format", "line", "Output format (line, binary)")
137 fs.StringVar(&cmd.conflictPath, "conflict-path", "", "File name for writing field conflicts using line protocol and gzipped")
138 fs.BoolVar(&cmd.ignore, "no-conflict-path", false, "Disable writing field conflicts to a file")
139 fs.Var(&cmd.r, "range", "Range of target shards to export (default: all)")
140 fs.BoolVar(&cmd.print, "print-only", false, "Print plan to stderr and exit")
141 fs.DurationVar(&cmd.shardDuration, "duration", time.Hour*24*7, "Target shard duration")
142
143 if err := fs.Parse(args); err != nil {
144 return err
145 }
146
147 if cmd.database == "" {
148 return errors.New("database is required")
149 }
150
151 switch cmd.format {
152 case "line", "binary", "series", "values", "discard":
153 default:
154 return fmt.Errorf("invalid format '%s'", cmd.format)
155 }
156
157 if cmd.conflictPath == "" && !cmd.ignore {
158 return errors.New("missing conflict-path")
159 }
160
161 return nil
162 }
163
164 type rangeValue struct {
165 min, max uint64
166 set bool
167 }
168
169 func (rv *rangeValue) Min() uint64 { return rv.min }
170
171 func (rv *rangeValue) Max() uint64 {
172 if !rv.set {
173 return math.MaxUint64
174 }
175 return rv.max
176 }
177
178 func (rv *rangeValue) String() string {
179 if rv.Min() == rv.Max() {
180 return fmt.Sprint(rv.min)
181 }
182 return fmt.Sprintf("[%d,%d]", rv.Min(), rv.Max())
183 }
184
185 func (rv *rangeValue) Set(v string) (err error) {
186 p := strings.Split(v, "-")
187 switch {
188 case len(p) == 1:
189 rv.min, err = strconv.ParseUint(p[0], 10, 64)
190 if err != nil {
191 return fmt.Errorf("range error: invalid number %s", v)
192 }
193 rv.max = rv.min
194 case len(p) == 2:
195 rv.min, err = strconv.ParseUint(p[0], 10, 64)
196 if err != nil {
197 return fmt.Errorf("range error: min value %q is not a positive number", p[0])
198 }
199 rv.max = math.MaxUint64
200 if len(p[1]) > 0 {
201 rv.max, err = strconv.ParseUint(p[1], 10, 64)
202 if err != nil {
203 return fmt.Errorf("range error: max value %q is not empty or a positive number", p[1])
204 }
205 }
206 default:
207 return fmt.Errorf("range error: %q is not a valid range", v)
208 }
209
210 if rv.min > rv.max {
211 return errors.New("range error: min > max")
212 }
213
214 rv.set = true
215
216 return nil
217 }