Use buffered writer when writing tombstones

When deleting many series, the many small writes flood the disks
and consume a lot of CPU time.
pull/8204/head
Jason Wilder 2017-03-21 22:23:04 -06:00
parent 6232d5e56d
commit a78da51b7c
1 changed files with 17 additions and 5 deletions

View File

@ -69,6 +69,12 @@ func (t *Tombstoner) AddRange(keys []string, min, max int64) error {
return nil return nil
} }
if cap(tombstones) < len(tombstones)+len(keys) {
ts := make([]Tombstone, len(tombstones), len(tombstones)+len(keys))
copy(ts, tombstones)
tombstones = ts
}
for _, k := range keys { for _, k := range keys {
tombstones = append(tombstones, Tombstone{ tombstones = append(tombstones, Tombstone{
Key: k, Key: k,
@ -173,30 +179,36 @@ func (t *Tombstoner) writeTombstone(tombstones []Tombstone) error {
var b [8]byte var b [8]byte
bw := bufio.NewWriterSize(tmp, 1024*1024)
binary.BigEndian.PutUint32(b[:4], v2header) binary.BigEndian.PutUint32(b[:4], v2header)
if _, err := tmp.Write(b[:4]); err != nil { if _, err := bw.Write(b[:4]); err != nil {
return err return err
} }
for _, t := range tombstones { for _, t := range tombstones {
binary.BigEndian.PutUint32(b[:4], uint32(len(t.Key))) binary.BigEndian.PutUint32(b[:4], uint32(len(t.Key)))
if _, err := tmp.Write(b[:4]); err != nil { if _, err := bw.Write(b[:4]); err != nil {
return err return err
} }
if _, err := tmp.Write([]byte(t.Key)); err != nil { if _, err := bw.WriteString(t.Key); err != nil {
return err return err
} }
binary.BigEndian.PutUint64(b[:], uint64(t.Min)) binary.BigEndian.PutUint64(b[:], uint64(t.Min))
if _, err := tmp.Write(b[:]); err != nil { if _, err := bw.Write(b[:]); err != nil {
return err return err
} }
binary.BigEndian.PutUint64(b[:], uint64(t.Max)) binary.BigEndian.PutUint64(b[:], uint64(t.Max))
if _, err := tmp.Write(b[:]); err != nil { if _, err := bw.Write(b[:]); err != nil {
return err return err
} }
} }
if err := bw.Flush(); err != nil {
return err
}
// fsync the file to flush the write // fsync the file to flush the write
if err := tmp.Sync(); err != nil { if err := tmp.Sync(); err != nil {
return err return err