| 1 |
|
| 2 |
package dumptsmwal |
| 3 |
|
| 4 |
import ( |
| 5 |
"flag" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"log" |
| 9 |
"os" |
| 10 |
"path/filepath" |
| 11 |
"sort" |
| 12 |
|
| 13 |
"github.com/influxdata/influxdb/tsdb/engine/tsm1" |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
type Command struct { |
| 18 |
|
| 19 |
Stderr io.Writer |
| 20 |
Stdout io.Writer |
| 21 |
|
| 22 |
showDuplicates bool |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
func NewCommand() *Command { |
| 27 |
return &Command{ |
| 28 |
Stderr: os.Stderr, |
| 29 |
Stdout: os.Stdout, |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
func (cmd *Command) Run(args ...string) (err error) { |
| 35 |
fs := flag.NewFlagSet("dumptsmwal", flag.ExitOnError) |
| 36 |
fs.SetOutput(cmd.Stdout) |
| 37 |
fs.BoolVar(&cmd.showDuplicates, "show-duplicates", false, "prints keys with out-of-order or duplicate values") |
| 38 |
fs.Usage = cmd.printUsage |
| 39 |
if err := fs.Parse(args); err != nil { |
| 40 |
return err |
| 41 |
} else if fs.NArg() == 0 { |
| 42 |
fmt.Printf("path required\n\n") |
| 43 |
fs.Usage() |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
for _, path := range fs.Args() { |
| 49 |
if err := cmd.process(path); err != nil { |
| 50 |
return err |
| 51 |
} |
| 52 |
} |
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
func (cmd *Command) process(path string) error { |
| 57 |
if filepath.Ext(path) != "."+tsm1.WALFileExtension { |
| 58 |
log.Printf("invalid wal filename, skipping %s", path) |
| 59 |
return nil |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
minTimestampByKey := make(map[string]int64) |
| 64 |
duplicateKeys := make(map[string]struct{}) |
| 65 |
|
| 66 |
|
| 67 |
f, err := os.Open(path) |
| 68 |
if err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
defer f.Close() |
| 72 |
r := tsm1.NewWALSegmentReader(f) |
| 73 |
|
| 74 |
|
| 75 |
for r.Next() { |
| 76 |
entry, err := r.Read() |
| 77 |
if err != nil { |
| 78 |
return fmt.Errorf("cannot read entry: %s", err) |
| 79 |
} |
| 80 |
|
| 81 |
switch entry := entry.(type) { |
| 82 |
case *tsm1.WriteWALEntry: |
| 83 |
if !cmd.showDuplicates { |
| 84 |
fmt.Printf("[write] sz=%d\n", entry.MarshalSize()) |
| 85 |
} |
| 86 |
|
| 87 |
keys := make([]string, 0, len(entry.Values)) |
| 88 |
for k := range entry.Values { |
| 89 |
keys = append(keys, k) |
| 90 |
} |
| 91 |
sort.Strings(keys) |
| 92 |
|
| 93 |
for _, k := range keys { |
| 94 |
for _, v := range entry.Values[k] { |
| 95 |
t := v.UnixNano() |
| 96 |
|
| 97 |
|
| 98 |
if min, ok := minTimestampByKey[k]; ok && t <= min { |
| 99 |
duplicateKeys[k] = struct{}{} |
| 100 |
} |
| 101 |
minTimestampByKey[k] = t |
| 102 |
|
| 103 |
|
| 104 |
if cmd.showDuplicates { |
| 105 |
continue |
| 106 |
} |
| 107 |
|
| 108 |
switch v := v.(type) { |
| 109 |
case tsm1.IntegerValue: |
| 110 |
fmt.Printf("%s %vi %d\n", k, v.Value(), t) |
| 111 |
case tsm1.UnsignedValue: |
| 112 |
fmt.Printf("%s %vu %d\n", k, v.Value(), t) |
| 113 |
case tsm1.FloatValue: |
| 114 |
fmt.Printf("%s %v %d\n", k, v.Value(), t) |
| 115 |
case tsm1.BooleanValue: |
| 116 |
fmt.Printf("%s %v %d\n", k, v.Value(), t) |
| 117 |
case tsm1.StringValue: |
| 118 |
fmt.Printf("%s %q %d\n", k, v.Value(), t) |
| 119 |
default: |
| 120 |
fmt.Printf("%s EMPTY\n", k) |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
case *tsm1.DeleteWALEntry: |
| 126 |
fmt.Printf("[delete] sz=%d\n", entry.MarshalSize()) |
| 127 |
for _, k := range entry.Keys { |
| 128 |
fmt.Printf("%s\n", string(k)) |
| 129 |
} |
| 130 |
|
| 131 |
case *tsm1.DeleteRangeWALEntry: |
| 132 |
fmt.Printf("[delete-range] min=%d max=%d sz=%d\n", entry.Min, entry.Max, entry.MarshalSize()) |
| 133 |
for _, k := range entry.Keys { |
| 134 |
fmt.Printf("%s\n", string(k)) |
| 135 |
} |
| 136 |
|
| 137 |
default: |
| 138 |
return fmt.Errorf("invalid wal entry: %#v", entry) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
if cmd.showDuplicates { |
| 144 |
keys := make([]string, 0, len(duplicateKeys)) |
| 145 |
for k := range duplicateKeys { |
| 146 |
keys = append(keys, k) |
| 147 |
} |
| 148 |
sort.Strings(keys) |
| 149 |
|
| 150 |
for _, k := range keys { |
| 151 |
fmt.Println(k) |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return nil |
| 156 |
} |
| 157 |
|
| 158 |
func (cmd *Command) printUsage() { |
| 159 |
fmt.Print(`Dumps all entries from one or more TSM WAL files. |
| 160 |
|
| 161 |
Usage: influx_inspect dumptsmwal path...`) |
| 162 |
} |