| 1 |
|
| 2 |
package dumptsm |
| 3 |
|
| 4 |
import ( |
| 5 |
"encoding/binary" |
| 6 |
"flag" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
"strconv" |
| 11 |
"strings" |
| 12 |
"text/tabwriter" |
| 13 |
"time" |
| 14 |
|
| 15 |
"github.com/influxdata/influxdb/tsdb/engine/tsm1" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
type Command struct { |
| 20 |
|
| 21 |
Stderr io.Writer |
| 22 |
Stdout io.Writer |
| 23 |
|
| 24 |
dumpIndex bool |
| 25 |
dumpBlocks bool |
| 26 |
dumpAll bool |
| 27 |
filterKey string |
| 28 |
path string |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
func NewCommand() *Command { |
| 33 |
return &Command{ |
| 34 |
Stderr: os.Stderr, |
| 35 |
Stdout: os.Stdout, |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
func (cmd *Command) Run(args ...string) error { |
| 41 |
fs := flag.NewFlagSet("file", flag.ExitOnError) |
| 42 |
fs.BoolVar(&cmd.dumpIndex, "index", false, "Dump raw index data") |
| 43 |
fs.BoolVar(&cmd.dumpBlocks, "blocks", false, "Dump raw block data") |
| 44 |
fs.BoolVar(&cmd.dumpAll, "all", false, "Dump all data. Caution: This may print a lot of information") |
| 45 |
fs.StringVar(&cmd.filterKey, "filter-key", "", "Only display index and block data match this key substring") |
| 46 |
|
| 47 |
fs.SetOutput(cmd.Stdout) |
| 48 |
fs.Usage = cmd.printUsage |
| 49 |
|
| 50 |
if err := fs.Parse(args); err != nil { |
| 51 |
return err |
| 52 |
} |
| 53 |
|
| 54 |
if fs.Arg(0) == "" { |
| 55 |
fmt.Printf("TSM file not specified\n\n") |
| 56 |
fs.Usage() |
| 57 |
return nil |
| 58 |
} |
| 59 |
cmd.path = fs.Args()[0] |
| 60 |
cmd.dumpBlocks = cmd.dumpBlocks || cmd.dumpAll || cmd.filterKey != "" |
| 61 |
cmd.dumpIndex = cmd.dumpIndex || cmd.dumpAll || cmd.filterKey != "" |
| 62 |
return cmd.dump() |
| 63 |
} |
| 64 |
|
| 65 |
func (cmd *Command) dump() error { |
| 66 |
var errors []error |
| 67 |
|
| 68 |
f, err := os.Open(cmd.path) |
| 69 |
if err != nil { |
| 70 |
return err |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
stat, err := f.Stat() |
| 75 |
if err != nil { |
| 76 |
return err |
| 77 |
} |
| 78 |
b := make([]byte, 8) |
| 79 |
|
| 80 |
r, err := tsm1.NewTSMReader(f) |
| 81 |
if err != nil { |
| 82 |
return fmt.Errorf("Error opening TSM files: %s", err.Error()) |
| 83 |
} |
| 84 |
defer r.Close() |
| 85 |
|
| 86 |
minTime, maxTime := r.TimeRange() |
| 87 |
keyCount := r.KeyCount() |
| 88 |
|
| 89 |
blockStats := &blockStats{} |
| 90 |
|
| 91 |
println("Summary:") |
| 92 |
fmt.Printf(" File: %s\n", cmd.path) |
| 93 |
fmt.Printf(" Time Range: %s - %s\n", |
| 94 |
time.Unix(0, minTime).UTC().Format(time.RFC3339Nano), |
| 95 |
time.Unix(0, maxTime).UTC().Format(time.RFC3339Nano), |
| 96 |
) |
| 97 |
fmt.Printf(" Duration: %s ", time.Unix(0, maxTime).Sub(time.Unix(0, minTime))) |
| 98 |
fmt.Printf(" Series: %d ", keyCount) |
| 99 |
fmt.Printf(" File Size: %d\n", stat.Size()) |
| 100 |
println() |
| 101 |
|
| 102 |
tw := tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) |
| 103 |
|
| 104 |
if cmd.dumpIndex { |
| 105 |
println("Index:") |
| 106 |
tw.Flush() |
| 107 |
println() |
| 108 |
|
| 109 |
fmt.Fprintln(tw, " "+strings.Join([]string{"Pos", "Min Time", "Max Time", "Ofs", "Size", "Key", "Field"}, "\t")) |
| 110 |
var pos int |
| 111 |
for i := 0; i < keyCount; i++ { |
| 112 |
key, _ := r.KeyAt(i) |
| 113 |
for _, e := range r.Entries(key) { |
| 114 |
pos++ |
| 115 |
split := strings.Split(string(key), "#!~#") |
| 116 |
|
| 117 |
|
| 118 |
measurement := split[0] |
| 119 |
field := split[1] |
| 120 |
|
| 121 |
if cmd.filterKey != "" && !strings.Contains(string(key), cmd.filterKey) { |
| 122 |
continue |
| 123 |
} |
| 124 |
fmt.Fprintln(tw, " "+strings.Join([]string{ |
| 125 |
strconv.FormatInt(int64(pos), 10), |
| 126 |
time.Unix(0, e.MinTime).UTC().Format(time.RFC3339Nano), |
| 127 |
time.Unix(0, e.MaxTime).UTC().Format(time.RFC3339Nano), |
| 128 |
strconv.FormatInt(int64(e.Offset), 10), |
| 129 |
strconv.FormatInt(int64(e.Size), 10), |
| 130 |
measurement, |
| 131 |
field, |
| 132 |
}, "\t")) |
| 133 |
tw.Flush() |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
tw = tabwriter.NewWriter(cmd.Stdout, 8, 8, 1, '\t', 0) |
| 139 |
fmt.Fprintln(tw, " "+strings.Join([]string{"Blk", "Chk", "Ofs", "Len", "Type", "Min Time", "Points", "Enc [T/V]", "Len [T/V]"}, "\t")) |
| 140 |
|
| 141 |
|
| 142 |
i := int64(5) |
| 143 |
var blockCount, pointCount, blockSize int64 |
| 144 |
indexSize := r.IndexSize() |
| 145 |
|
| 146 |
|
| 147 |
for j := 0; j < keyCount; j++ { |
| 148 |
key, _ := r.KeyAt(j) |
| 149 |
for _, e := range r.Entries(key) { |
| 150 |
|
| 151 |
f.Seek(int64(e.Offset), 0) |
| 152 |
f.Read(b[:4]) |
| 153 |
|
| 154 |
chksum := binary.BigEndian.Uint32(b[:4]) |
| 155 |
|
| 156 |
buf := make([]byte, e.Size-4) |
| 157 |
f.Read(buf) |
| 158 |
|
| 159 |
blockSize += int64(e.Size) |
| 160 |
|
| 161 |
if cmd.filterKey != "" && !strings.Contains(string(key), cmd.filterKey) { |
| 162 |
i += blockSize |
| 163 |
blockCount++ |
| 164 |
continue |
| 165 |
} |
| 166 |
|
| 167 |
blockType := buf[0] |
| 168 |
|
| 169 |
encoded := buf[1:] |
| 170 |
|
| 171 |
var v []tsm1.Value |
| 172 |
v, err := tsm1.DecodeBlock(buf, v) |
| 173 |
if err != nil { |
| 174 |
return err |
| 175 |
} |
| 176 |
startTime := time.Unix(0, v[0].UnixNano()) |
| 177 |
|
| 178 |
pointCount += int64(len(v)) |
| 179 |
|
| 180 |
|
| 181 |
tsLen, j := binary.Uvarint(encoded) |
| 182 |
|
| 183 |
|
| 184 |
ts := encoded[int(j) : int(j)+int(tsLen)] |
| 185 |
|
| 186 |
|
| 187 |
values := encoded[int(j)+int(tsLen):] |
| 188 |
|
| 189 |
tsEncoding := timeEnc[int(ts[0]>>4)] |
| 190 |
vEncoding := encDescs[int(blockType+1)][values[0]>>4] |
| 191 |
|
| 192 |
typeDesc := blockTypes[blockType] |
| 193 |
|
| 194 |
blockStats.inc(0, ts[0]>>4) |
| 195 |
blockStats.inc(int(blockType+1), values[0]>>4) |
| 196 |
blockStats.size(len(buf)) |
| 197 |
|
| 198 |
if cmd.dumpBlocks { |
| 199 |
fmt.Fprintln(tw, " "+strings.Join([]string{ |
| 200 |
strconv.FormatInt(blockCount, 10), |
| 201 |
strconv.FormatUint(uint64(chksum), 10), |
| 202 |
strconv.FormatInt(i, 10), |
| 203 |
strconv.FormatInt(int64(len(buf)), 10), |
| 204 |
typeDesc, |
| 205 |
startTime.UTC().Format(time.RFC3339Nano), |
| 206 |
strconv.FormatInt(int64(len(v)), 10), |
| 207 |
fmt.Sprintf("%s/%s", tsEncoding, vEncoding), |
| 208 |
fmt.Sprintf("%d/%d", len(ts), len(values)), |
| 209 |
}, "\t")) |
| 210 |
} |
| 211 |
|
| 212 |
i += blockSize |
| 213 |
blockCount++ |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
if cmd.dumpBlocks { |
| 218 |
println("Blocks:") |
| 219 |
tw.Flush() |
| 220 |
println() |
| 221 |
} |
| 222 |
|
| 223 |
var blockSizeAvg int64 |
| 224 |
if blockCount > 0 { |
| 225 |
blockSizeAvg = blockSize / blockCount |
| 226 |
} |
| 227 |
fmt.Printf("Statistics\n") |
| 228 |
fmt.Printf(" Blocks:\n") |
| 229 |
fmt.Printf(" Total: %d Size: %d Min: %d Max: %d Avg: %d\n", |
| 230 |
blockCount, blockSize, blockStats.min, blockStats.max, blockSizeAvg) |
| 231 |
fmt.Printf(" Index:\n") |
| 232 |
fmt.Printf(" Total: %d Size: %d\n", blockCount, indexSize) |
| 233 |
fmt.Printf(" Points:\n") |
| 234 |
fmt.Printf(" Total: %d", pointCount) |
| 235 |
println() |
| 236 |
|
| 237 |
println(" Encoding:") |
| 238 |
for i, counts := range blockStats.counts { |
| 239 |
if len(counts) == 0 { |
| 240 |
continue |
| 241 |
} |
| 242 |
fmt.Printf(" %s: ", strings.Title(fieldType[i])) |
| 243 |
for j, v := range counts { |
| 244 |
fmt.Printf("\t%s: %d (%d%%) ", encDescs[i][j], v, int(float64(v)/float64(blockCount)*100)) |
| 245 |
} |
| 246 |
println() |
| 247 |
} |
| 248 |
fmt.Printf(" Compression:\n") |
| 249 |
fmt.Printf(" Per block: %0.2f bytes/point\n", float64(blockSize)/float64(pointCount)) |
| 250 |
fmt.Printf(" Total: %0.2f bytes/point\n", float64(stat.Size())/float64(pointCount)) |
| 251 |
|
| 252 |
if len(errors) > 0 { |
| 253 |
println() |
| 254 |
fmt.Printf("Errors (%d):\n", len(errors)) |
| 255 |
for _, err := range errors { |
| 256 |
fmt.Printf(" * %v\n", err) |
| 257 |
} |
| 258 |
println() |
| 259 |
return fmt.Errorf("error count %d", len(errors)) |
| 260 |
} |
| 261 |
return nil |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
func (cmd *Command) printUsage() { |
| 266 |
usage := `Dumps low-level details about tsm1 files. |
| 267 |
|
| 268 |
Usage: influx_inspect dumptsm [flags] <path |
| 269 |
|
| 270 |
-index |
| 271 |
Dump raw index data |
| 272 |
-blocks |
| 273 |
Dump raw block data |
| 274 |
-all |
| 275 |
Dump all data. Caution: This may print a lot of information |
| 276 |
-filter-key <name> |
| 277 |
Only display index and block data match this key substring |
| 278 |
` |
| 279 |
|
| 280 |
fmt.Fprintf(cmd.Stdout, usage) |
| 281 |
} |
| 282 |
|
| 283 |
var ( |
| 284 |
fieldType = []string{ |
| 285 |
"timestamp", "float", "int", "bool", "string", "unsigned", |
| 286 |
} |
| 287 |
blockTypes = []string{ |
| 288 |
"float64", "int64", "bool", "string", "unsigned", |
| 289 |
} |
| 290 |
timeEnc = []string{ |
| 291 |
"none", "s8b", "rle", |
| 292 |
} |
| 293 |
floatEnc = []string{ |
| 294 |
"none", "gor", |
| 295 |
} |
| 296 |
intEnc = []string{ |
| 297 |
"none", "s8b", "rle", |
| 298 |
} |
| 299 |
boolEnc = []string{ |
| 300 |
"none", "bp", |
| 301 |
} |
| 302 |
stringEnc = []string{ |
| 303 |
"none", "snpy", |
| 304 |
} |
| 305 |
unsignedEnc = []string{ |
| 306 |
"none", "s8b", "rle", |
| 307 |
} |
| 308 |
encDescs = [][]string{ |
| 309 |
timeEnc, floatEnc, intEnc, boolEnc, stringEnc, unsignedEnc, |
| 310 |
} |
| 311 |
) |
| 312 |
|
| 313 |
type blockStats struct { |
| 314 |
min, max int |
| 315 |
counts [][]int |
| 316 |
} |
| 317 |
|
| 318 |
func (b *blockStats) inc(typ int, enc byte) { |
| 319 |
for len(b.counts) <= typ { |
| 320 |
b.counts = append(b.counts, []int{}) |
| 321 |
} |
| 322 |
for len(b.counts[typ]) <= int(enc) { |
| 323 |
b.counts[typ] = append(b.counts[typ], 0) |
| 324 |
} |
| 325 |
b.counts[typ][enc]++ |
| 326 |
} |
| 327 |
|
| 328 |
func (b *blockStats) size(sz int) { |
| 329 |
if b.min == 0 || sz < b.min { |
| 330 |
b.min = sz |
| 331 |
} |
| 332 |
if b.min == 0 || sz > b.max { |
| 333 |
b.max = sz |
| 334 |
} |
| 335 |
} |