forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
359 lines (313 loc) · 10.1 KB
/
main.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package main
import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"sync"
"text/tabwriter"
"time"
"github.com/influxdb/influxdb/cmd/influx_tsm/b1"
"github.com/influxdb/influxdb/cmd/influx_tsm/bz1"
"github.com/influxdb/influxdb/cmd/influx_tsm/tsdb"
)
type ShardReader interface {
KeyIterator
Open() error
Close() error
}
const (
backupExt = "bak"
tsmExt = "tsm"
)
var description = fmt.Sprintf(`
Convert a database from b1 or bz1 format to tsm1 format.
This tool will backup any directory before conversion. It is up to the
end-user to delete the backup on the disk, once the end-user is happy
with the converted data. Backups are named by suffixing the database
name with '.%s'. The backups will be ignored by the system since they
are not registered with the cluster.
To restore a backup, delete the tsm1 version, rename the backup directory
restart the node.`, backupExt)
var dataPath string
var ds string
var tsmSz uint64
var parallel bool
var disBack bool
const maxTSMSz = 2 * 1000 * 1000 * 1000
func init() {
flag.StringVar(&ds, "dbs", "", "Comma-delimited list of databases to convert. Default is to convert all databases.")
flag.Uint64Var(&tsmSz, "sz", maxTSMSz, "Maximum size of individual TSM files.")
flag.BoolVar(¶llel, "parallel", false, "Perform parallel conversion.")
flag.BoolVar(&disBack, "nobackup", false, "Disable database backups. Not recommended.")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] <data-path> \n", os.Args[0])
fmt.Fprintf(os.Stderr, "%s\n\n", description)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
}
}
func main() {
pg := NewParallelGroup(1)
flag.Parse()
if len(flag.Args()) < 1 {
fmt.Fprintf(os.Stderr, "No data directory specified\n")
os.Exit(1)
}
dataPath = flag.Args()[0]
if tsmSz > maxTSMSz {
fmt.Fprintf(os.Stderr, "Maximum TSM file size is %d\n", maxTSMSz)
os.Exit(1)
}
// Check if specific directories were requested.
reqDs := strings.Split(ds, ",")
if len(reqDs) == 1 && reqDs[0] == "" {
reqDs = nil
}
// Determine the list of databases
dbs, err := ioutil.ReadDir(dataPath)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to access data directory at %s: %s\n", dataPath, err.Error())
os.Exit(1)
}
fmt.Println() // Cleanly separate output from start of program.
// Dump summary of what is about to happen.
fmt.Println("b1 and bz1 shard conversion.")
fmt.Println("-----------------------------------")
fmt.Println("Data directory is: ", dataPath)
fmt.Println("Databases specified: ", allDBs(reqDs))
fmt.Println("Database backups enabled:", yesno(!disBack))
fmt.Println("Parallel mode enabled: ", yesno(parallel))
fmt.Println()
// Get the list of shards for conversion.
var shards []*tsdb.ShardInfo
for _, db := range dbs {
if strings.HasSuffix(db.Name(), backupExt) {
fmt.Printf("Skipping %s as it looks like a backup.\n", db.Name())
continue
}
d := tsdb.NewDatabase(filepath.Join(dataPath, db.Name()))
shs, err := d.Shards()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to access shards for database %s: %s\n", d.Name(), err.Error())
os.Exit(1)
}
shards = append(shards, shs...)
}
sort.Sort(tsdb.ShardInfos(shards))
usl := len(shards)
shards = tsdb.ShardInfos(shards).FilterFormat(tsdb.TSM1).ExclusiveDatabases(reqDs)
sl := len(shards)
// Anything to convert?
fmt.Printf("\n%d shard(s) detected, %d non-TSM shards detected.\n", usl, sl)
if len(shards) == 0 {
fmt.Printf("Nothing to do.\n")
os.Exit(0)
}
// Display list of convertible shards.
fmt.Println()
w := new(tabwriter.Writer)
w.Init(os.Stdout, 0, 8, 1, '\t', 0)
fmt.Fprintln(w, "Database\tRetention\tPath\tEngine\tSize")
for _, si := range shards {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n", si.Database, si.RetentionPolicy, si.FullPath(dataPath), si.FormatAsString(), si.Size)
}
w.Flush()
// Get confirmation from user.
fmt.Printf("\nThese shards will be converted. Proceed? y/N: ")
liner := bufio.NewReader(os.Stdin)
yn, err := liner.ReadString('\n')
if err != nil {
fmt.Fprintf(os.Stderr, "failed to read response: %s", err.Error())
os.Exit(1)
}
yn = strings.TrimRight(strings.ToLower(yn), "\n")
if yn != "y" {
fmt.Println("Conversion aborted.")
os.Exit(1)
}
fmt.Println("Conversion starting....")
// Backup each directory.
conversionStart := time.Now()
if !disBack {
databases := tsdb.ShardInfos(shards).Databases()
fmt.Printf("Backing up %d databases...\n", len(databases))
if parallel {
pg = NewParallelGroup(len(databases))
}
for _, db := range databases {
pg.Request()
go func(db string) {
defer pg.Release()
start := time.Now()
err := backupDatabase(filepath.Join(dataPath, db))
if err != nil {
fmt.Fprintf(os.Stderr, "Backup of database %s failed: %s\n", db, err.Error())
os.Exit(1)
}
fmt.Printf("Database %s backed up (%v)\n", db, time.Now().Sub(start))
}(db)
}
pg.Wait()
} else {
fmt.Println("Database backup disabled.")
}
// Convert each shard.
if parallel {
pg = NewParallelGroup(len(shards))
}
for _, si := range shards {
pg.Request()
go func(si *tsdb.ShardInfo) {
defer pg.Release()
start := time.Now()
if err := convertShard(si); err != nil {
fmt.Fprintf(os.Stderr, "Failed to convert %s: %s\n", si.FullPath(dataPath), err.Error())
os.Exit(1)
}
fmt.Printf("Conversion of %s successful (%s)\n", si.FullPath(dataPath), time.Now().Sub(start))
}(si)
}
pg.Wait()
// Dump stats.
preSize := tsdb.ShardInfos(shards).Size()
postSize := TsmBytesWritten
fmt.Printf("\nSummary statistics\n========================================\n")
fmt.Printf("Databases converted: %d\n", len(tsdb.ShardInfos(shards).Databases()))
fmt.Printf("Shards converted: %d\n", len(shards))
fmt.Printf("TSM files created: %d\n", TsmFilesCreated)
fmt.Printf("Points read: %d\n", PointsRead)
fmt.Printf("Points written: %d\n", PointsWritten)
fmt.Printf("NaN filtered: %d\n", NanFiltered)
fmt.Printf("Inf filtered: %d\n", InfFiltered)
fmt.Printf("Points without fields filtered: %d\n", b1.NoFieldsFiltered+bz1.NoFieldsFiltered)
fmt.Printf("Disk usage pre-conversion (bytes): %d\n", preSize)
fmt.Printf("Disk usage post-conversion (bytes): %d\n", postSize)
fmt.Printf("Reduction factor: %d%%\n", (100*preSize-postSize)/preSize)
fmt.Printf("Bytes per TSM point: %.2f\n", float64(postSize)/float64(PointsWritten))
fmt.Printf("Total conversion time: %v\n", time.Now().Sub(conversionStart))
fmt.Println()
}
// backupDatabase backs up the database at src.
func backupDatabase(src string) error {
dest := filepath.Join(src + "." + backupExt)
if _, err := os.Stat(dest); !os.IsNotExist(err) {
return fmt.Errorf("backup of %s already exists", src)
}
return copyDir(dest, src)
}
// copyDir copies the directory at src to dest. If dest does not exist it
// will be created. It is up to the caller to ensure the paths don't overlap.
func copyDir(dest, src string) error {
copyFile := func(path string, info os.FileInfo, err error) error {
// Strip the src from the path and replace with dest.
toPath := strings.Replace(path, src, dest, 1)
// Copy it.
if info.IsDir() {
if err := os.MkdirAll(toPath, info.Mode()); err != nil {
return err
}
} else {
err := func() error {
in, err := os.Open(path)
if err != nil {
return err
}
defer in.Close()
out, err := os.OpenFile(toPath, os.O_CREATE|os.O_WRONLY, info.Mode())
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}()
if err != nil {
return err
}
}
return nil
}
return filepath.Walk(src, copyFile)
}
// convertShard converts the shard in-place.
func convertShard(si *tsdb.ShardInfo) error {
src := si.FullPath(dataPath)
dst := fmt.Sprintf("%s.%s", src, tsmExt)
var reader ShardReader
switch si.Format {
case tsdb.BZ1:
reader = bz1.NewReader(src)
case tsdb.B1:
reader = b1.NewReader(src)
default:
return fmt.Errorf("Unsupported shard format: %s", si.FormatAsString())
}
defer reader.Close()
// Open the shard, and create a converter.
if err := reader.Open(); err != nil {
return fmt.Errorf("Failed to open %s for conversion: %s", src, err.Error())
}
converter := NewConverter(dst, uint32(tsmSz))
// Perform the conversion.
if err := converter.Process(reader); err != nil {
return fmt.Errorf("Conversion of %s failed: %s", src, err.Error())
}
// Delete source shard, and rename new tsm1 shard.
if err := reader.Close(); err != nil {
return fmt.Errorf("Conversion of %s failed due to close: %s", src, err.Error())
}
if err := os.RemoveAll(si.FullPath(dataPath)); err != nil {
return fmt.Errorf("Deletion of %s failed: %s", src, err.Error())
}
if err := os.Rename(dst, src); err != nil {
return fmt.Errorf("Rename of %s to %s failed: %s", dst, src, err.Error())
}
return nil
}
// ParallelGroup allows the maximum parrallelism of a set of operations to be controlled.
type ParallelGroup struct {
c chan struct{}
wg sync.WaitGroup
}
// NewParallelGroup returns a group which allows n operations to run in parallel. A value of 0
// means no operations will ever run.
func NewParallelGroup(n int) *ParallelGroup {
return &ParallelGroup{
c: make(chan struct{}, n),
}
}
// Request requests permission to start an operation. It will block unless and until
// the parallel requirements would not be violated.
func (p *ParallelGroup) Request() {
p.wg.Add(1)
p.c <- struct{}{}
}
// Release informs the group that a previoulsy requested operation has completed.
func (p *ParallelGroup) Release() {
<-p.c
p.wg.Done()
}
// Wait blocks until the ParallelGroup has no unreleased operations.
func (p *ParallelGroup) Wait() {
p.wg.Wait()
}
// yesno returns "yes" for true, "no" for false.
func yesno(b bool) string {
if b {
return "yes"
}
return "no"
}
// allDBs returns "all" if all databases are requested for conversion.
func allDBs(dbs []string) string {
if dbs == nil {
return "all"
}
return fmt.Sprintf("%v", dbs)
}