| 1 |
|
| 2 |
package dumptsi |
| 3 |
|
| 4 |
import ( |
| 5 |
"errors" |
| 6 |
"flag" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
"path/filepath" |
| 11 |
"regexp" |
| 12 |
"text/tabwriter" |
| 13 |
|
| 14 |
"github.com/influxdata/influxdb/logger" |
| 15 |
"github.com/influxdata/influxdb/models" |
| 16 |
"github.com/influxdata/influxdb/tsdb" |
| 17 |
"github.com/influxdata/influxdb/tsdb/index/tsi1" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
type Command struct { |
| 22 |
|
| 23 |
Stderr io.Writer |
| 24 |
Stdout io.Writer |
| 25 |
|
| 26 |
seriesFilePath string |
| 27 |
paths []string |
| 28 |
|
| 29 |
showSeries bool |
| 30 |
showMeasurements bool |
| 31 |
showTagKeys bool |
| 32 |
showTagValues bool |
| 33 |
showTagValueSeries bool |
| 34 |
|
| 35 |
measurementFilter *regexp.Regexp |
| 36 |
tagKeyFilter *regexp.Regexp |
| 37 |
tagValueFilter *regexp.Regexp |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
func NewCommand() *Command { |
| 42 |
return &Command{ |
| 43 |
Stderr: os.Stderr, |
| 44 |
Stdout: os.Stdout, |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func (cmd *Command) Run(args ...string) error { |
| 50 |
var measurementFilter, tagKeyFilter, tagValueFilter string |
| 51 |
fs := flag.NewFlagSet("dumptsi", flag.ExitOnError) |
| 52 |
fs.StringVar(&cmd.seriesFilePath, "series-file", "", "Path to series file") |
| 53 |
fs.BoolVar(&cmd.showSeries, "series", false, "Show raw series data") |
| 54 |
fs.BoolVar(&cmd.showMeasurements, "measurements", false, "Show raw measurement data") |
| 55 |
fs.BoolVar(&cmd.showTagKeys, "tag-keys", false, "Show raw tag key data") |
| 56 |
fs.BoolVar(&cmd.showTagValues, "tag-values", false, "Show raw tag value data") |
| 57 |
fs.BoolVar(&cmd.showTagValueSeries, "tag-value-series", false, "Show raw series data for each value") |
| 58 |
fs.StringVar(&measurementFilter, "measurement-filter", "", "Regex measurement filter") |
| 59 |
fs.StringVar(&tagKeyFilter, "tag-key-filter", "", "Regex tag key filter") |
| 60 |
fs.StringVar(&tagValueFilter, "tag-value-filter", "", "Regex tag value filter") |
| 61 |
fs.SetOutput(cmd.Stdout) |
| 62 |
fs.Usage = cmd.printUsage |
| 63 |
if err := fs.Parse(args); err != nil { |
| 64 |
return err |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
if measurementFilter != "" { |
| 69 |
re, err := regexp.Compile(measurementFilter) |
| 70 |
if err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
cmd.measurementFilter = re |
| 74 |
} |
| 75 |
if tagKeyFilter != "" { |
| 76 |
re, err := regexp.Compile(tagKeyFilter) |
| 77 |
if err != nil { |
| 78 |
return err |
| 79 |
} |
| 80 |
cmd.tagKeyFilter = re |
| 81 |
} |
| 82 |
if tagValueFilter != "" { |
| 83 |
re, err := regexp.Compile(tagValueFilter) |
| 84 |
if err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
cmd.tagValueFilter = re |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
if cmd.seriesFilePath == "" { |
| 92 |
return errors.New("series file path required") |
| 93 |
} |
| 94 |
|
| 95 |
cmd.paths = fs.Args() |
| 96 |
if len(cmd.paths) == 0 { |
| 97 |
fmt.Printf("at least one path required\n\n") |
| 98 |
fs.Usage() |
| 99 |
return nil |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
if cmd.showTagValueSeries { |
| 104 |
cmd.showTagValues = true |
| 105 |
} |
| 106 |
if cmd.showTagValues { |
| 107 |
cmd.showTagKeys = true |
| 108 |
} |
| 109 |
if cmd.showTagKeys { |
| 110 |
cmd.showMeasurements = true |
| 111 |
} |
| 112 |
|
| 113 |
return cmd.run() |
| 114 |
} |
| 115 |
|
| 116 |
func (cmd *Command) run() error { |
| 117 |
sfile := tsdb.NewSeriesFile(cmd.seriesFilePath) |
| 118 |
sfile.Logger = logger.New(os.Stderr) |
| 119 |
if err := sfile.Open(); err != nil { |
| 120 |
return err |
| 121 |
} |
| 122 |
defer sfile.Close() |
| 123 |
|
| 124 |
|
| 125 |
idx, fs, err := cmd.readFileSet(sfile) |
| 126 |
if err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
|
| 130 |
if cmd.showSeries { |
| 131 |
if err := cmd.printSeries(sfile); err != nil { |
| 132 |
return err |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
if fs != nil { |
| 138 |
defer fs.Release() |
| 139 |
defer fs.Close() |
| 140 |
if cmd.showSeries || cmd.showMeasurements { |
| 141 |
return cmd.printMeasurements(sfile, fs) |
| 142 |
} |
| 143 |
return cmd.printFileSummaries(fs) |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
defer idx.Close() |
| 148 |
for i := 0; i < int(idx.PartitionN); i++ { |
| 149 |
if err := func() error { |
| 150 |
fs, err := idx.PartitionAt(i).RetainFileSet() |
| 151 |
if err != nil { |
| 152 |
return err |
| 153 |
} |
| 154 |
defer fs.Release() |
| 155 |
|
| 156 |
if cmd.showSeries || cmd.showMeasurements { |
| 157 |
return cmd.printMeasurements(sfile, fs) |
| 158 |
} |
| 159 |
return cmd.printFileSummaries(fs) |
| 160 |
}(); err != nil { |
| 161 |
return err |
| 162 |
} |
| 163 |
} |
| 164 |
return nil |
| 165 |
} |
| 166 |
|
| 167 |
func (cmd *Command) readFileSet(sfile *tsdb.SeriesFile) (*tsi1.Index, *tsi1.FileSet, error) { |
| 168 |
|
| 169 |
if len(cmd.paths) == 1 { |
| 170 |
fi, err := os.Stat(cmd.paths[0]) |
| 171 |
if err != nil { |
| 172 |
return nil, nil, err |
| 173 |
} else if fi.IsDir() { |
| 174 |
|
| 175 |
if ok, err := tsi1.IsIndexDir(cmd.paths[0]); err != nil { |
| 176 |
return nil, nil, err |
| 177 |
} else if !ok { |
| 178 |
return nil, nil, fmt.Errorf("Not an index directory: %q", cmd.paths[0]) |
| 179 |
} |
| 180 |
|
| 181 |
idx := tsi1.NewIndex(sfile, |
| 182 |
"", |
| 183 |
tsi1.WithPath(cmd.paths[0]), |
| 184 |
tsi1.DisableCompactions(), |
| 185 |
) |
| 186 |
if err := idx.Open(); err != nil { |
| 187 |
return nil, nil, err |
| 188 |
} |
| 189 |
return idx, nil, nil |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
var files []tsi1.File |
| 195 |
for _, path := range cmd.paths { |
| 196 |
switch ext := filepath.Ext(path); ext { |
| 197 |
case tsi1.LogFileExt: |
| 198 |
f := tsi1.NewLogFile(sfile, path) |
| 199 |
if err := f.Open(); err != nil { |
| 200 |
return nil, nil, err |
| 201 |
} |
| 202 |
files = append(files, f) |
| 203 |
|
| 204 |
case tsi1.IndexFileExt: |
| 205 |
f := tsi1.NewIndexFile(sfile) |
| 206 |
f.SetPath(path) |
| 207 |
if err := f.Open(); err != nil { |
| 208 |
return nil, nil, err |
| 209 |
} |
| 210 |
files = append(files, f) |
| 211 |
|
| 212 |
default: |
| 213 |
return nil, nil, fmt.Errorf("unexpected file extension: %s", ext) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
fs, err := tsi1.NewFileSet(nil, sfile, files) |
| 218 |
if err != nil { |
| 219 |
return nil, nil, err |
| 220 |
} |
| 221 |
fs.Retain() |
| 222 |
|
| 223 |
return nil, fs, nil |
| 224 |
} |
| 225 |
|
| 226 |
func (cmd *Command) printSeries(sfile *tsdb.SeriesFile) error { |
| 227 |
if !cmd.showSeries { |
| 228 |
return nil |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
tw := tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) |
| 233 |
fmt.Fprintln(tw, "Series\t") |
| 234 |
|
| 235 |
|
| 236 |
itr := sfile.SeriesIDIterator() |
| 237 |
for { |
| 238 |
e, err := itr.Next() |
| 239 |
if err != nil { |
| 240 |
return err |
| 241 |
} else if e.SeriesID == 0 { |
| 242 |
break |
| 243 |
} |
| 244 |
name, tags := tsdb.ParseSeriesKey(sfile.SeriesKey(e.SeriesID)) |
| 245 |
|
| 246 |
if !cmd.matchSeries(name, tags) { |
| 247 |
continue |
| 248 |
} |
| 249 |
|
| 250 |
deleted := sfile.IsDeleted(e.SeriesID) |
| 251 |
|
| 252 |
fmt.Fprintf(tw, "%s%s\t%v\n", name, tags.HashKey(), deletedString(deleted)) |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
if err := tw.Flush(); err != nil { |
| 257 |
return err |
| 258 |
} |
| 259 |
fmt.Fprint(cmd.Stdout, "\n\n") |
| 260 |
|
| 261 |
return nil |
| 262 |
} |
| 263 |
|
| 264 |
func (cmd *Command) printMeasurements(sfile *tsdb.SeriesFile, fs *tsi1.FileSet) error { |
| 265 |
if !cmd.showMeasurements { |
| 266 |
return nil |
| 267 |
} |
| 268 |
|
| 269 |
tw := tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) |
| 270 |
fmt.Fprintln(tw, "Measurement\t") |
| 271 |
|
| 272 |
|
| 273 |
if itr := fs.MeasurementIterator(); itr != nil { |
| 274 |
for e := itr.Next(); e != nil; e = itr.Next() { |
| 275 |
if cmd.measurementFilter != nil && !cmd.measurementFilter.Match(e.Name()) { |
| 276 |
continue |
| 277 |
} |
| 278 |
|
| 279 |
fmt.Fprintf(tw, "%s\t%v\n", e.Name(), deletedString(e.Deleted())) |
| 280 |
if err := tw.Flush(); err != nil { |
| 281 |
return err |
| 282 |
} |
| 283 |
|
| 284 |
if err := cmd.printTagKeys(sfile, fs, e.Name()); err != nil { |
| 285 |
return err |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
fmt.Fprint(cmd.Stdout, "\n\n") |
| 291 |
|
| 292 |
return nil |
| 293 |
} |
| 294 |
|
| 295 |
func (cmd *Command) printTagKeys(sfile *tsdb.SeriesFile, fs *tsi1.FileSet, name []byte) error { |
| 296 |
if !cmd.showTagKeys { |
| 297 |
return nil |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
tw := tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) |
| 302 |
itr := fs.TagKeyIterator(name) |
| 303 |
for e := itr.Next(); e != nil; e = itr.Next() { |
| 304 |
if cmd.tagKeyFilter != nil && !cmd.tagKeyFilter.Match(e.Key()) { |
| 305 |
continue |
| 306 |
} |
| 307 |
|
| 308 |
fmt.Fprintf(tw, " %s\t%v\n", e.Key(), deletedString(e.Deleted())) |
| 309 |
if err := tw.Flush(); err != nil { |
| 310 |
return err |
| 311 |
} |
| 312 |
|
| 313 |
if err := cmd.printTagValues(sfile, fs, name, e.Key()); err != nil { |
| 314 |
return err |
| 315 |
} |
| 316 |
} |
| 317 |
fmt.Fprint(cmd.Stdout, "\n") |
| 318 |
|
| 319 |
return nil |
| 320 |
} |
| 321 |
|
| 322 |
func (cmd *Command) printTagValues(sfile *tsdb.SeriesFile, fs *tsi1.FileSet, name, key []byte) error { |
| 323 |
if !cmd.showTagValues { |
| 324 |
return nil |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
tw := tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) |
| 329 |
itr := fs.TagValueIterator(name, key) |
| 330 |
for e := itr.Next(); e != nil; e = itr.Next() { |
| 331 |
if cmd.tagValueFilter != nil && !cmd.tagValueFilter.Match(e.Value()) { |
| 332 |
continue |
| 333 |
} |
| 334 |
|
| 335 |
fmt.Fprintf(tw, " %s\t%v\n", e.Value(), deletedString(e.Deleted())) |
| 336 |
if err := tw.Flush(); err != nil { |
| 337 |
return err |
| 338 |
} |
| 339 |
|
| 340 |
if err := cmd.printTagValueSeries(sfile, fs, name, key, e.Value()); err != nil { |
| 341 |
return err |
| 342 |
} |
| 343 |
} |
| 344 |
fmt.Fprint(cmd.Stdout, "\n") |
| 345 |
|
| 346 |
return nil |
| 347 |
} |
| 348 |
|
| 349 |
func (cmd *Command) printTagValueSeries(sfile *tsdb.SeriesFile, fs *tsi1.FileSet, name, key, value []byte) error { |
| 350 |
if !cmd.showTagValueSeries { |
| 351 |
return nil |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
tw := tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) |
| 356 |
itr := fs.TagValueSeriesIDIterator(name, key, value) |
| 357 |
for { |
| 358 |
e, err := itr.Next() |
| 359 |
if err != nil { |
| 360 |
return err |
| 361 |
} else if e.SeriesID == 0 { |
| 362 |
break |
| 363 |
} |
| 364 |
|
| 365 |
name, tags := tsdb.ParseSeriesKey(sfile.SeriesKey(e.SeriesID)) |
| 366 |
|
| 367 |
if !cmd.matchSeries(name, tags) { |
| 368 |
continue |
| 369 |
} |
| 370 |
|
| 371 |
fmt.Fprintf(tw, " %s%s\n", name, tags.HashKey()) |
| 372 |
if err := tw.Flush(); err != nil { |
| 373 |
return err |
| 374 |
} |
| 375 |
} |
| 376 |
fmt.Fprint(cmd.Stdout, "\n") |
| 377 |
|
| 378 |
return nil |
| 379 |
} |
| 380 |
|
| 381 |
func (cmd *Command) printFileSummaries(fs *tsi1.FileSet) error { |
| 382 |
for _, f := range fs.Files() { |
| 383 |
switch f := f.(type) { |
| 384 |
case *tsi1.LogFile: |
| 385 |
if err := cmd.printLogFileSummary(f); err != nil { |
| 386 |
return err |
| 387 |
} |
| 388 |
case *tsi1.IndexFile: |
| 389 |
if err := cmd.printIndexFileSummary(f); err != nil { |
| 390 |
return err |
| 391 |
} |
| 392 |
default: |
| 393 |
panic("unreachable") |
| 394 |
} |
| 395 |
fmt.Fprintln(cmd.Stdout, "") |
| 396 |
} |
| 397 |
return nil |
| 398 |
} |
| 399 |
|
| 400 |
func (cmd *Command) printLogFileSummary(f *tsi1.LogFile) error { |
| 401 |
fmt.Fprintf(cmd.Stdout, "[LOG FILE] %s\n", filepath.Base(f.Path())) |
| 402 |
tw := tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) |
| 403 |
fmt.Fprintf(tw, "Series:\t%d\n", f.SeriesN()) |
| 404 |
fmt.Fprintf(tw, "Measurements:\t%d\n", f.MeasurementN()) |
| 405 |
fmt.Fprintf(tw, "Tag Keys:\t%d\n", f.TagKeyN()) |
| 406 |
fmt.Fprintf(tw, "Tag Values:\t%d\n", f.TagValueN()) |
| 407 |
return tw.Flush() |
| 408 |
} |
| 409 |
|
| 410 |
func (cmd *Command) printIndexFileSummary(f *tsi1.IndexFile) error { |
| 411 |
fmt.Fprintf(cmd.Stdout, "[INDEX FILE] %s\n", filepath.Base(f.Path())) |
| 412 |
|
| 413 |
|
| 414 |
var measurementN, measurementSeriesN, measurementSeriesSize uint64 |
| 415 |
var keyN uint64 |
| 416 |
var valueN, valueSeriesN, valueSeriesSize uint64 |
| 417 |
|
| 418 |
if mitr := f.MeasurementIterator(); mitr != nil { |
| 419 |
for me, _ := mitr.Next().(*tsi1.MeasurementBlockElem); me != nil; me, _ = mitr.Next().(*tsi1.MeasurementBlockElem) { |
| 420 |
kitr := f.TagKeyIterator(me.Name()) |
| 421 |
for ke, _ := kitr.Next().(*tsi1.TagBlockKeyElem); ke != nil; ke, _ = kitr.Next().(*tsi1.TagBlockKeyElem) { |
| 422 |
vitr := f.TagValueIterator(me.Name(), ke.Key()) |
| 423 |
for ve, _ := vitr.Next().(*tsi1.TagBlockValueElem); ve != nil; ve, _ = vitr.Next().(*tsi1.TagBlockValueElem) { |
| 424 |
valueN++ |
| 425 |
valueSeriesN += uint64(ve.SeriesN()) |
| 426 |
valueSeriesSize += uint64(len(ve.SeriesData())) |
| 427 |
} |
| 428 |
keyN++ |
| 429 |
} |
| 430 |
measurementN++ |
| 431 |
measurementSeriesN += uint64(me.SeriesN()) |
| 432 |
measurementSeriesSize += uint64(len(me.SeriesData())) |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
tw := tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) |
| 438 |
fmt.Fprintf(tw, "Measurements:\t%d\n", measurementN) |
| 439 |
fmt.Fprintf(tw, " Series data size:\t%d (%s)\n", measurementSeriesSize, formatSize(measurementSeriesSize)) |
| 440 |
fmt.Fprintf(tw, " Bytes per series:\t%.01fb\n", float64(measurementSeriesSize)/float64(measurementSeriesN)) |
| 441 |
fmt.Fprintf(tw, "Tag Keys:\t%d\n", keyN) |
| 442 |
fmt.Fprintf(tw, "Tag Values:\t%d\n", valueN) |
| 443 |
fmt.Fprintf(tw, " Series:\t%d\n", valueSeriesN) |
| 444 |
fmt.Fprintf(tw, " Series data size:\t%d (%s)\n", valueSeriesSize, formatSize(valueSeriesSize)) |
| 445 |
fmt.Fprintf(tw, " Bytes per series:\t%.01fb\n", float64(valueSeriesSize)/float64(valueSeriesN)) |
| 446 |
return tw.Flush() |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
func (cmd *Command) matchSeries(name []byte, tags models.Tags) bool { |
| 451 |
|
| 452 |
if cmd.measurementFilter != nil && !cmd.measurementFilter.Match(name) { |
| 453 |
return false |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
if cmd.tagKeyFilter != nil || cmd.tagValueFilter != nil { |
| 458 |
var matched bool |
| 459 |
for _, tag := range tags { |
| 460 |
if (cmd.tagKeyFilter == nil || cmd.tagKeyFilter.Match(tag.Key)) && (cmd.tagValueFilter == nil || cmd.tagValueFilter.Match(tag.Value)) { |
| 461 |
matched = true |
| 462 |
break |
| 463 |
} |
| 464 |
} |
| 465 |
if !matched { |
| 466 |
return false |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
return true |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
func (cmd *Command) printUsage() { |
| 475 |
usage := `Dumps low-level details about tsi1 files. |
| 476 |
|
| 477 |
Usage: influx_inspect dumptsi [flags] path... |
| 478 |
|
| 479 |
-series |
| 480 |
Dump raw series data |
| 481 |
-measurements |
| 482 |
Dump raw measurement data |
| 483 |
-tag-keys |
| 484 |
Dump raw tag keys |
| 485 |
-tag-values |
| 486 |
Dump raw tag values |
| 487 |
-tag-value-series |
| 488 |
Dump raw series for each tag value |
| 489 |
-measurement-filter REGEXP |
| 490 |
Filters data by measurement regular expression |
| 491 |
-series-file PATH |
| 492 |
Path to the "_series" directory under the database data directory. |
| 493 |
Required. |
| 494 |
-tag-key-filter REGEXP |
| 495 |
Filters data by tag key regular expression |
| 496 |
-tag-value-filter REGEXP |
| 497 |
Filters data by tag value regular expression |
| 498 |
|
| 499 |
One or more paths are required. Path must specify either a TSI index directory |
| 500 |
or it should specify one or more .tsi/.tsl files. If no flags are specified |
| 501 |
then summary stats are provided for each file. |
| 502 |
` |
| 503 |
|
| 504 |
fmt.Fprintf(cmd.Stdout, usage) |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
func deletedString(v bool) string { |
| 509 |
if v { |
| 510 |
return "(deleted)" |
| 511 |
} |
| 512 |
return "" |
| 513 |
} |
| 514 |
|
| 515 |
func formatSize(v uint64) string { |
| 516 |
denom := uint64(1) |
| 517 |
var uom string |
| 518 |
for _, uom = range []string{"b", "kb", "mb", "gb", "tb"} { |
| 519 |
if denom*1024 > v { |
| 520 |
break |
| 521 |
} |
| 522 |
denom *= 1024 |
| 523 |
} |
| 524 |
return fmt.Sprintf("%0.01f%s", float64(v)/float64(denom), uom) |
| 525 |
} |