|
|
@ -308,9 +308,11 @@ func (w *SegmentWAL) Truncate(mint int64, p Postings) error {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "create compaction segment")
|
|
|
|
return errors.Wrap(err, "create compaction segment")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
csf := newSegmentFile(f)
|
|
|
|
var (
|
|
|
|
|
|
|
|
csf = newSegmentFile(f)
|
|
|
|
activeSeries := []RefSeries{}
|
|
|
|
crc32 = newCRC32()
|
|
|
|
|
|
|
|
activeSeries = []RefSeries{}
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
Loop:
|
|
|
|
Loop:
|
|
|
|
for r.next() {
|
|
|
|
for r.next() {
|
|
|
@ -337,7 +339,7 @@ Loop:
|
|
|
|
buf := w.getBuffer()
|
|
|
|
buf := w.getBuffer()
|
|
|
|
flag = w.encodeSeries(buf, activeSeries)
|
|
|
|
flag = w.encodeSeries(buf, activeSeries)
|
|
|
|
|
|
|
|
|
|
|
|
_, err = w.writeTo(csf, WALEntrySeries, flag, buf.get())
|
|
|
|
_, err = w.writeTo(csf, crc32, WALEntrySeries, flag, buf.get())
|
|
|
|
w.putBuffer(buf)
|
|
|
|
w.putBuffer(buf)
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
@ -355,20 +357,17 @@ Loop:
|
|
|
|
if err := csf.Truncate(off); err != nil {
|
|
|
|
if err := csf.Truncate(off); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := csf.Close(); err != nil {
|
|
|
|
csf.Sync()
|
|
|
|
return errors.Wrap(err, "close tmp file")
|
|
|
|
csf.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := renameFile(csf.Name(), candidates[0].Name()); err != nil {
|
|
|
|
if err := renameFile(csf.Name(), candidates[0].Name()); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for _, f := range candidates[1:] {
|
|
|
|
for _, f := range candidates[1:] {
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
|
|
|
|
return errors.Wrap(err, "close obsolete WAL segment file")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.RemoveAll(f.Name()); err != nil {
|
|
|
|
if err := os.RemoveAll(f.Name()); err != nil {
|
|
|
|
return errors.Wrap(err, "delete WAL segment file")
|
|
|
|
return errors.Wrap(err, "delete WAL segment file")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := w.dirFile.Sync(); err != nil {
|
|
|
|
if err := w.dirFile.Sync(); err != nil {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -381,9 +380,7 @@ Loop:
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We don't need it to be open.
|
|
|
|
// We don't need it to be open.
|
|
|
|
if err := csf.Close(); err != nil {
|
|
|
|
csf.Close()
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
w.mtx.Lock()
|
|
|
|
w.mtx.Lock()
|
|
|
|
w.files = append([]*segmentFile{csf}, w.files[len(candidates):]...)
|
|
|
|
w.files = append([]*segmentFile{csf}, w.files[len(candidates):]...)
|
|
|
@ -674,19 +671,19 @@ func (w *SegmentWAL) write(t WALEntryType, flag uint8, buf []byte) error {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n, err := w.writeTo(w.cur, t, flag, buf)
|
|
|
|
n, err := w.writeTo(w.cur, w.crc32, t, flag, buf)
|
|
|
|
|
|
|
|
|
|
|
|
w.curN += int64(n)
|
|
|
|
w.curN += int64(n)
|
|
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (w *SegmentWAL) writeTo(wr io.Writer, t WALEntryType, flag uint8, buf []byte) (int, error) {
|
|
|
|
func (w *SegmentWAL) writeTo(wr io.Writer, crc32 hash.Hash, t WALEntryType, flag uint8, buf []byte) (int, error) {
|
|
|
|
if len(buf) == 0 {
|
|
|
|
if len(buf) == 0 {
|
|
|
|
return 0, nil
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.crc32.Reset()
|
|
|
|
crc32.Reset()
|
|
|
|
wr = io.MultiWriter(w.crc32, wr)
|
|
|
|
wr = io.MultiWriter(crc32, wr)
|
|
|
|
|
|
|
|
|
|
|
|
var b [6]byte
|
|
|
|
var b [6]byte
|
|
|
|
b[0] = byte(t)
|
|
|
|
b[0] = byte(t)
|
|
|
@ -702,7 +699,7 @@ func (w *SegmentWAL) writeTo(wr io.Writer, t WALEntryType, flag uint8, buf []byt
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return n1 + n2, err
|
|
|
|
return n1 + n2, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n3, err := wr.Write(w.crc32.Sum(b[:0]))
|
|
|
|
n3, err := wr.Write(crc32.Sum(b[:0]))
|
|
|
|
|
|
|
|
|
|
|
|
return n1 + n2 + n3, err
|
|
|
|
return n1 + n2 + n3, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|