Debian Codegraph @

cmd/influx-tools/export/exporter.go

raw symbols SCIP
1 package export
2
3 import (
4 "context"
5 "fmt"
6 "io"
7 "sort"
8 "text/tabwriter"
9 "time"
10
11 "github.com/influxdata/influxdb/cmd/influx-tools/internal/format"
12 "github.com/influxdata/influxdb/cmd/influx-tools/internal/storage"
13 "github.com/influxdata/influxdb/cmd/influx-tools/server"
14 "github.com/influxdata/influxdb/models"
15 "github.com/influxdata/influxdb/services/meta"
16 "github.com/influxdata/influxdb/tsdb"
17 )
18
19 type exporterConfig struct {
20 Database string
21 RP string
22 ShardDuration time.Duration
23 Min, Max uint64
24 }
25
26 type exporter struct {
27 metaClient server.MetaClient
28 tsdbStore *tsdb.Store
29 store *storage.Store
30
31 min, max uint64
32 db, rp string
33 d time.Duration
34 sourceGroups []meta.ShardGroupInfo
35 targetGroups []meta.ShardGroupInfo
36
37 // source data time range
38 startDate time.Time
39 endDate time.Time
40 }
41
42 func newExporter(server server.Interface, cfg *exporterConfig) (*exporter, error) {
43 client := server.MetaClient()
44
45 dbi := client.Database(cfg.Database)
46 if dbi == nil {
47 return nil, fmt.Errorf("database '%s' does not exist", cfg.Database)
48 }
49
50 if cfg.RP == "" {
51 // select default RP
52 cfg.RP = dbi.DefaultRetentionPolicy
53 }
54
55 rpi, err := client.RetentionPolicy(cfg.Database, cfg.RP)
56 if rpi == nil || err != nil {
57 return nil, fmt.Errorf("retention policy '%s' does not exist", cfg.RP)
58 }
59
60 store := tsdb.NewStore(server.TSDBConfig().Dir)
61 if server.Logger() != nil {
62 store.WithLogger(server.Logger())
63 }
64 store.EngineOptions.Config = server.TSDBConfig()
65 store.EngineOptions.EngineVersion = server.TSDBConfig().Engine
66 store.EngineOptions.IndexVersion = server.TSDBConfig().Index
67 store.EngineOptions.DatabaseFilter = func(database string) bool {
68 return database == cfg.Database
69 }
70 store.EngineOptions.RetentionPolicyFilter = func(_, rp string) bool {
71 return rp == cfg.RP
72 }
73 store.EngineOptions.ShardFilter = func(_, _ string, _ uint64) bool {
74 return false
75 }
76
77 return &exporter{
78 metaClient: client,
79 tsdbStore: store,
80 store: &storage.Store{TSDBStore: store},
81 min: cfg.Min,
82 max: cfg.Max,
83 db: cfg.Database,
84 rp: cfg.RP,
85 d: cfg.ShardDuration,
86 }, nil
87 }
88
89 func (e *exporter) Open() (err error) {
90 err = e.tsdbStore.Open()
91 if err != nil {
92 return err
93 }
94
95 err = e.loadShardGroups()
96 if err != nil {
97 return err
98 }
99
100 e.targetGroups = planShardGroups(e.sourceGroups, e.startDate, e.endDate, e.d)
101 if e.max >= uint64(len(e.targetGroups)) {
102 e.max = uint64(len(e.targetGroups) - 1)
103 }
104 if e.min > e.max {
105 return fmt.Errorf("invalid shard group range %d to %d", e.min, e.max)
106 }
107
108 e.targetGroups = e.targetGroups[e.min : e.max+1]
109
110 return nil
111 }
112
113 func (e *exporter) PrintPlan(w io.Writer) {
114 fmt.Fprintf(w, "Source data from: %s -> %s\n\n", e.startDate, e.endDate)
115 fmt.Fprintf(w, "Converting source from %d shard group(s) to %d shard groups:\n\n", len(e.sourceGroups), len(e.targetGroups))
116 e.printShardGroups(w, 0, e.sourceGroups)
117 fmt.Fprintln(w)
118 e.printShardGroups(w, int(e.min), e.targetGroups)
119 }
120
121 func (e *exporter) printShardGroups(w io.Writer, base int, target []meta.ShardGroupInfo) {
122 tw := tabwriter.NewWriter(w, 10, 8, 1, '\t', 0)
123 fmt.Fprintln(tw, "Seq #\tID\tStart\tEnd")
124 for i := 0; i < len(target); i++ {
125 g := target[i]
126 fmt.Fprintf(tw, "%d\t%d\t%s\t%s\n", i+base, g.ID, g.StartTime, g.EndTime)
127 }
128 tw.Flush()
129 }
130
131 func (e *exporter) SourceTimeRange() (time.Time, time.Time) { return e.startDate, e.endDate }
132 func (e *exporter) SourceShardGroups() []meta.ShardGroupInfo { return e.sourceGroups }
133 func (e *exporter) TargetShardGroups() []meta.ShardGroupInfo { return e.targetGroups }
134
135 func (e *exporter) loadShardGroups() error {
136 min := time.Unix(0, models.MinNanoTime)
137 max := time.Unix(0, models.MaxNanoTime)
138
139 groups, err := e.metaClient.ShardGroupsByTimeRange(e.db, e.rp, min, max)
140 if err != nil {
141 return err
142 }
143
144 if len(groups) == 0 {
145 return nil
146 }
147
148 sort.Sort(meta.ShardGroupInfos(groups))
149 e.sourceGroups = groups
150 e.startDate = groups[0].StartTime
151 e.endDate = groups[len(groups)-1].EndTime
152
153 return nil
154 }
155
156 func (e *exporter) shardsGroupsByTimeRange(min, max time.Time) []meta.ShardGroupInfo {
157 groups := make([]meta.ShardGroupInfo, 0, len(e.sourceGroups))
158 for _, g := range e.sourceGroups {
159 if !g.Overlaps(min, max) {
160 continue
161 }
162 groups = append(groups, g)
163 }
164 return groups
165 }
166
167 func (e *exporter) WriteTo(w format.Writer) error {
168 for _, g := range e.targetGroups {
169 min, max := g.StartTime, g.EndTime
170 rs, err := e.read(min, max.Add(-1))
171 if err != nil || rs == nil {
172 return err
173 }
174
175 format.WriteBucket(w, min.UnixNano(), max.UnixNano(), rs)
176 rs.Close()
177 }
178 return nil
179 }
180
181 // Read creates a ResultSet that reads all points with a timestamp ts, such that start ≤ ts < end.
182 func (e *exporter) read(min, max time.Time) (*storage.ResultSet, error) {
183 shards, err := e.getShards(min, max)
184 if err != nil {
185 return nil, err
186 }
187
188 req := storage.ReadRequest{
189 Database: e.db,
190 RP: e.rp,
191 Shards: shards,
192 Start: min.UnixNano(),
193 End: max.UnixNano(),
194 }
195
196 return e.store.Read(context.Background(), &req)
197 }
198
199 func (e *exporter) Close() error {
200 return e.tsdbStore.Close()
201 }
202
203 func (e *exporter) getShards(min, max time.Time) ([]*tsdb.Shard, error) {
204 groups := e.shardsGroupsByTimeRange(min, max)
205 var ids []uint64
206 for _, g := range groups {
207 for _, s := range g.Shards {
208 ids = append(ids, s.ID)
209 }
210 }
211
212 shards := e.tsdbStore.Shards(ids)
213 if len(shards) == len(ids) {
214 return shards, nil
215 }
216
217 return e.openStoreWithShardsIDs(ids)
218 }
219
220 func (e *exporter) openStoreWithShardsIDs(ids []uint64) ([]*tsdb.Shard, error) {
221 e.tsdbStore.Close()
222 e.tsdbStore.EngineOptions.ShardFilter = func(_, _ string, id uint64) bool {
223 for i := range ids {
224 if id == ids[i] {
225 return true
226 }
227 }
228 return false
229 }
230 if err := e.tsdbStore.Open(); err != nil {
231 return nil, err
232 }
233 return e.tsdbStore.Shards(ids), nil
234 }