| 1 |
// Package bz1 reads data from bz1 shards. |
| 2 |
package bz1 // import "github.com/influxdata/influxdb/cmd/influx_tsm/bz1" |
| 3 |
|
| 4 |
import ( |
| 5 |
"bytes" |
| 6 |
"encoding/binary" |
| 7 |
"encoding/json" |
| 8 |
"fmt" |
| 9 |
"math" |
| 10 |
"sort" |
| 11 |
"time" |
| 12 |
|
| 13 |
"github.com/boltdb/bolt" |
| 14 |
"github.com/golang/snappy" |
| 15 |
"github.com/influxdata/influxdb/cmd/influx_tsm/stats" |
| 16 |
"github.com/influxdata/influxdb/cmd/influx_tsm/tsdb" |
| 17 |
"github.com/influxdata/influxdb/tsdb/engine/tsm1" |
| 18 |
) |
| 19 |
|
| 20 |
// DefaultChunkSize is the size of chunks read from the bz1 shard |
| 21 |
const DefaultChunkSize = 1000 |
| 22 |
|
| 23 |
// Reader is used to read all data from a bz1 shard. |
| 24 |
type Reader struct { |
| 25 |
path string |
| 26 |
db *bolt.DB |
| 27 |
tx *bolt.Tx |
| 28 |
|
| 29 |
cursors []*cursor |
| 30 |
currCursor int |
| 31 |
|
| 32 |
keyBuf string |
| 33 |
values []tsm1.Value |
| 34 |
valuePos int |
| 35 |
|
| 36 |
fields map[string]*tsdb.MeasurementFields |
| 37 |
codecs map[string]*tsdb.FieldCodec |
| 38 |
|
| 39 |
stats *stats.Stats |
| 40 |
} |
| 41 |
|
| 42 |
// NewReader returns a reader for the bz1 shard at path. |
| 43 |
func NewReader(path string, stats *stats.Stats, chunkSize int) *Reader { |
| 44 |
r := &Reader{ |
| 45 |
path: path, |
| 46 |
fields: make(map[string]*tsdb.MeasurementFields), |
| 47 |
codecs: make(map[string]*tsdb.FieldCodec), |
| 48 |
stats: stats, |
| 49 |
} |
| 50 |
|
| 51 |
if chunkSize <= 0 { |
| 52 |
chunkSize = DefaultChunkSize |
| 53 |
} |
| 54 |
|
| 55 |
r.values = make([]tsm1.Value, chunkSize) |
| 56 |
|
| 57 |
return r |
| 58 |
} |
| 59 |
|
| 60 |
// Open opens the reader. |
| 61 |
func (r *Reader) Open() error { |
| 62 |
// Open underlying storage. |
| 63 |
db, err := bolt.Open(r.path, 0666, &bolt.Options{Timeout: 1 * time.Second}) |
| 64 |
if err != nil { |
| 65 |
return err |
| 66 |
} |
| 67 |
r.db = db |
| 68 |
|
| 69 |
seriesSet := make(map[string]bool) |
| 70 |
|
| 71 |
if err := r.db.View(func(tx *bolt.Tx) error { |
| 72 |
var data []byte |
| 73 |
|
| 74 |
meta := tx.Bucket([]byte("meta")) |
| 75 |
if meta == nil { |
| 76 |
// No data in this shard. |
| 77 |
return nil |
| 78 |
} |
| 79 |
|
| 80 |
pointsBucket := tx.Bucket([]byte("points")) |
| 81 |
if pointsBucket == nil { |
| 82 |
return nil |
| 83 |
} |
| 84 |
|
| 85 |
if err := pointsBucket.ForEach(func(key, _ []byte) error { |
| 86 |
seriesSet[string(key)] = true |
| 87 |
return nil |
| 88 |
}); err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
|
| 92 |
buf := meta.Get([]byte("fields")) |
| 93 |
if buf == nil { |
| 94 |
// No data in this shard. |
| 95 |
return nil |
| 96 |
} |
| 97 |
|
| 98 |
data, err = snappy.Decode(nil, buf) |
| 99 |
if err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
if err := json.Unmarshal(data, &r.fields); err != nil { |
| 103 |
return err |
| 104 |
} |
| 105 |
return nil |
| 106 |
}); err != nil { |
| 107 |
return err |
| 108 |
} |
| 109 |
|
| 110 |
// Build the codec for each measurement. |
| 111 |
for k, v := range r.fields { |
| 112 |
r.codecs[k] = tsdb.NewFieldCodec(v.Fields) |
| 113 |
} |
| 114 |
|
| 115 |
r.tx, err = r.db.Begin(false) |
| 116 |
if err != nil { |
| 117 |
return err |
| 118 |
} |
| 119 |
|
| 120 |
// Create cursor for each field of each series. |
| 121 |
for s := range seriesSet { |
| 122 |
measurement := tsdb.MeasurementFromSeriesKey(s) |
| 123 |
fields := r.fields[measurement] |
| 124 |
if fields == nil { |
| 125 |
r.stats.IncrFiltered() |
| 126 |
continue |
| 127 |
} |
| 128 |
for _, f := range fields.Fields { |
| 129 |
c := newCursor(r.tx, s, f.Name, r.codecs[measurement]) |
| 130 |
if c == nil { |
| 131 |
continue |
| 132 |
} |
| 133 |
c.SeekTo(0) |
| 134 |
r.cursors = append(r.cursors, c) |
| 135 |
} |
| 136 |
} |
| 137 |
sort.Sort(cursors(r.cursors)) |
| 138 |
|
| 139 |
return nil |
| 140 |
} |
| 141 |
|
| 142 |
// Next returns whether there is any more data to be read. |
| 143 |
func (r *Reader) Next() bool { |
| 144 |
r.valuePos = 0 |
| 145 |
OUTER: |
| 146 |
for { |
| 147 |
if r.currCursor >= len(r.cursors) { |
| 148 |
// All cursors drained. No more data remains. |
| 149 |
return false |
| 150 |
} |
| 151 |
|
| 152 |
cc := r.cursors[r.currCursor] |
| 153 |
r.keyBuf = tsm1.SeriesFieldKey(cc.series, cc.field) |
| 154 |
|
| 155 |
for { |
| 156 |
k, v := cc.Next() |
| 157 |
if k == -1 { |
| 158 |
// Go to next cursor and try again. |
| 159 |
r.currCursor++ |
| 160 |
if r.valuePos == 0 { |
| 161 |
// The previous cursor had no data. Instead of returning |
| 162 |
// just go immediately to the next cursor. |
| 163 |
continue OUTER |
| 164 |
} |
| 165 |
// There is some data available. Indicate that it should be read. |
| 166 |
return true |
| 167 |
} |
| 168 |
|
| 169 |
if f, ok := v.(float64); ok { |
| 170 |
if math.IsInf(f, 0) { |
| 171 |
r.stats.AddPointsRead(1) |
| 172 |
r.stats.IncrInf() |
| 173 |
continue |
| 174 |
} |
| 175 |
|
| 176 |
if math.IsNaN(f) { |
| 177 |
r.stats.AddPointsRead(1) |
| 178 |
r.stats.IncrNaN() |
| 179 |
continue |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
r.values[r.valuePos] = tsm1.NewValue(k, v) |
| 184 |
r.valuePos++ |
| 185 |
|
| 186 |
if r.valuePos >= len(r.values) { |
| 187 |
return true |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// Read returns the next chunk of data in the shard, converted to tsm1 values. Data is |
| 194 |
// emitted completely for every field, in every series, before the next field is processed. |
| 195 |
// Data from Read() adheres to the requirements for writing to tsm1 shards |
| 196 |
func (r *Reader) Read() (string, []tsm1.Value, error) { |
| 197 |
return r.keyBuf, r.values[:r.valuePos], nil |
| 198 |
} |
| 199 |
|
| 200 |
// Close closes the reader. |
| 201 |
func (r *Reader) Close() error { |
| 202 |
r.tx.Rollback() |
| 203 |
return r.db.Close() |
| 204 |
} |
| 205 |
|
| 206 |
// cursor provides ordered iteration across a series. |
| 207 |
type cursor struct { |
| 208 |
cursor *bolt.Cursor |
| 209 |
buf []byte // uncompressed buffer |
| 210 |
off int // buffer offset |
| 211 |
fieldIndices []int |
| 212 |
index int |
| 213 |
|
| 214 |
series string |
| 215 |
field string |
| 216 |
dec *tsdb.FieldCodec |
| 217 |
|
| 218 |
keyBuf int64 |
| 219 |
valBuf interface{} |
| 220 |
} |
| 221 |
|
| 222 |
// newCursor returns an instance of a bz1 cursor. |
| 223 |
func newCursor(tx *bolt.Tx, series string, field string, dec *tsdb.FieldCodec) *cursor { |
| 224 |
// Retrieve points bucket. Ignore if there is no bucket. |
| 225 |
b := tx.Bucket([]byte("points")).Bucket([]byte(series)) |
| 226 |
if b == nil { |
| 227 |
return nil |
| 228 |
} |
| 229 |
|
| 230 |
return &cursor{ |
| 231 |
cursor: b.Cursor(), |
| 232 |
series: series, |
| 233 |
field: field, |
| 234 |
dec: dec, |
| 235 |
keyBuf: -2, |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
// Seek moves the cursor to a position. |
| 240 |
func (c *cursor) SeekTo(seek int64) { |
| 241 |
var seekBytes [8]byte |
| 242 |
binary.BigEndian.PutUint64(seekBytes[:], uint64(seek)) |
| 243 |
|
| 244 |
// Move cursor to appropriate block and set to buffer. |
| 245 |
k, v := c.cursor.Seek(seekBytes[:]) |
| 246 |
if v == nil { // get the last block, it might have this time |
| 247 |
_, v = c.cursor.Last() |
| 248 |
} else if seek < int64(binary.BigEndian.Uint64(k)) { // the seek key is less than this block, go back one and check |
| 249 |
_, v = c.cursor.Prev() |
| 250 |
|
| 251 |
// if the previous block max time is less than the seek value, reset to where we were originally |
| 252 |
if v == nil || seek > int64(binary.BigEndian.Uint64(v[0:8])) { |
| 253 |
_, v = c.cursor.Seek(seekBytes[:]) |
| 254 |
} |
| 255 |
} |
| 256 |
c.setBuf(v) |
| 257 |
|
| 258 |
// Read current block up to seek position. |
| 259 |
c.seekBuf(seekBytes[:]) |
| 260 |
|
| 261 |
// Return current entry. |
| 262 |
c.keyBuf, c.valBuf = c.read() |
| 263 |
} |
| 264 |
|
| 265 |
// seekBuf moves the cursor to a position within the current buffer. |
| 266 |
func (c *cursor) seekBuf(seek []byte) (key, value []byte) { |
| 267 |
for { |
| 268 |
// Slice off the current entry. |
| 269 |
buf := c.buf[c.off:] |
| 270 |
|
| 271 |
// Exit if current entry's timestamp is on or after the seek. |
| 272 |
if len(buf) == 0 { |
| 273 |
return |
| 274 |
} |
| 275 |
|
| 276 |
if bytes.Compare(buf[0:8], seek) != -1 { |
| 277 |
return |
| 278 |
} |
| 279 |
|
| 280 |
c.off += entryHeaderSize + entryDataSize(buf) |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
// Next returns the next key/value pair from the cursor. If there are no values |
| 285 |
// remaining, -1 is returned. |
| 286 |
func (c *cursor) Next() (int64, interface{}) { |
| 287 |
for { |
| 288 |
k, v := func() (int64, interface{}) { |
| 289 |
if c.keyBuf != -2 { |
| 290 |
k, v := c.keyBuf, c.valBuf |
| 291 |
c.keyBuf = -2 |
| 292 |
return k, v |
| 293 |
} |
| 294 |
|
| 295 |
// Ignore if there is no buffer. |
| 296 |
if len(c.buf) == 0 { |
| 297 |
return -1, nil |
| 298 |
} |
| 299 |
|
| 300 |
// Move forward to next entry. |
| 301 |
c.off += entryHeaderSize + entryDataSize(c.buf[c.off:]) |
| 302 |
|
| 303 |
// If no items left then read first item from next block. |
| 304 |
if c.off >= len(c.buf) { |
| 305 |
_, v := c.cursor.Next() |
| 306 |
c.setBuf(v) |
| 307 |
} |
| 308 |
|
| 309 |
return c.read() |
| 310 |
}() |
| 311 |
|
| 312 |
if k != -1 && v == nil { |
| 313 |
// There is a point in the series at the next timestamp, |
| 314 |
// but not for this cursor's field. Go to the next point. |
| 315 |
continue |
| 316 |
} |
| 317 |
return k, v |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
// setBuf saves a compressed block to the buffer. |
| 322 |
func (c *cursor) setBuf(block []byte) { |
| 323 |
// Clear if the block is empty. |
| 324 |
if len(block) == 0 { |
| 325 |
c.buf, c.off, c.fieldIndices, c.index = c.buf[0:0], 0, c.fieldIndices[0:0], 0 |
| 326 |
return |
| 327 |
} |
| 328 |
|
| 329 |
// Otherwise decode block into buffer. |
| 330 |
// Skip over the first 8 bytes since they are the max timestamp. |
| 331 |
buf, err := snappy.Decode(nil, block[8:]) |
| 332 |
if err != nil { |
| 333 |
c.buf = c.buf[0:0] |
| 334 |
fmt.Printf("block decode error: %s\n", err) |
| 335 |
} |
| 336 |
|
| 337 |
c.buf, c.off = buf, 0 |
| 338 |
} |
| 339 |
|
| 340 |
// read reads the current key and value from the current block. |
| 341 |
func (c *cursor) read() (key int64, value interface{}) { |
| 342 |
// Return nil if the offset is at the end of the buffer. |
| 343 |
if c.off >= len(c.buf) { |
| 344 |
return -1, nil |
| 345 |
} |
| 346 |
|
| 347 |
// Otherwise read the current entry. |
| 348 |
buf := c.buf[c.off:] |
| 349 |
dataSize := entryDataSize(buf) |
| 350 |
|
| 351 |
return tsdb.DecodeKeyValue(c.field, c.dec, buf[0:8], buf[entryHeaderSize:entryHeaderSize+dataSize]) |
| 352 |
} |
| 353 |
|
| 354 |
// Sort bz1 cursors in correct order for writing to TSM files. |
| 355 |
|
| 356 |
type cursors []*cursor |
| 357 |
|
| 358 |
func (a cursors) Len() int { return len(a) } |
| 359 |
func (a cursors) Swap(i, j int) { a[i], a[j] = a[j], a[i] } |
| 360 |
func (a cursors) Less(i, j int) bool { |
| 361 |
if a[i].series == a[j].series { |
| 362 |
return a[i].field < a[j].field |
| 363 |
} |
| 364 |
return a[i].series < a[j].series |
| 365 |
} |
| 366 |
|
| 367 |
// entryHeaderSize is the number of bytes required for the header. |
| 368 |
const entryHeaderSize = 8 + 4 |
| 369 |
|
| 370 |
// entryDataSize returns the size of an entry's data field, in bytes. |
| 371 |
func entryDataSize(v []byte) int { return int(binary.BigEndian.Uint32(v[8:12])) } |