fix bugs on platform windows to pass all test case.

This commit is contained in:
ranbo chen 2017-10-31 20:07:29 +08:00 committed by Your Name
parent 6ca5e52b69
commit b82b2d0c1b
7 changed files with 50 additions and 9 deletions

View File

@ -170,7 +170,13 @@ func (w *chunkWriter) finalizeTail() error {
if err := tf.Truncate(off); err != nil {
return err
}
return tf.Close()
if err := tf.Close(); err != nil {
return err
}
// close dir file (if not windows platform will fail on rename)
return w.dirFile.Close()
}
func (w *chunkWriter) cut() error {

View File

@ -426,12 +426,22 @@ func (c *LeveledCompactor) write(dest string, meta *BlockMeta, blocks ...BlockRe
if err != nil {
return errors.Wrap(err, "open temporary block dir")
}
defer df.Close()
defer func() {
if df != nil {
df.Close()
}
}()
if err := fileutil.Fsync(df); err != nil {
return errors.Wrap(err, "sync temporary dir file")
}
// close temp dir before rename block dir(for windows platform)
if err = df.Close(); err != nil {
return errors.Wrap(err, "close temporary dir")
}
df = nil
// Block successfully written, make visible and remove old ones.
if err := renameFile(tmp, dir); err != nil {
return errors.Wrap(err, "rename block dir")

View File

@ -21,8 +21,7 @@ import (
func mmap(f *os.File, sz int) ([]byte, error) {
low, high := uint32(sz), uint32(sz>>32)
h, errno := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, low, high, nil)
h, errno := syscall.CreateFileMapping(syscall.Handle(f.Fd()), nil, syscall.PAGE_READONLY, high, low, nil)
if h == 0 {
return nil, os.NewSyscallError("CreateFileMapping", errno)
}

View File

@ -153,6 +153,8 @@ func newIndexWriter(dir string) (*indexWriter, error) {
if err != nil {
return nil, err
}
defer df.Close() // close for flatform windows
f, err := os.OpenFile(filepath.Join(dir, indexFilename), os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
return nil, err

View File

@ -49,7 +49,11 @@ func writeTombstoneFile(dir string, tr tombstoneReader) error {
if err != nil {
return err
}
defer f.Close()
defer func() {
if f != nil {
f.Close()
}
}()
buf := encbuf{b: make([]byte, 3*binary.MaxVarintLen64)}
buf.reset()
@ -82,6 +86,10 @@ func writeTombstoneFile(dir string, tr tombstoneReader) error {
return err
}
if err = f.Close(); err != nil {
return err
}
f = nil
return renameFile(tmp, path)
}

21
wal.go
View File

@ -384,7 +384,7 @@ func (w *SegmentWAL) Truncate(mint int64, keep func(uint64) bool) error {
w.putBuffer(buf)
if err != nil {
return err
return errors.Wrap(err, "write to compaction segment")
}
}
if r.Err() != nil {
@ -401,14 +401,15 @@ func (w *SegmentWAL) Truncate(mint int64, keep func(uint64) bool) error {
csf.Sync()
csf.Close()
candidates[0].Close() // need close before remove on platform windows
if err := renameFile(csf.Name(), candidates[0].Name()); err != nil {
return err
return errors.Wrap(err, "rename compaction segment")
}
for _, f := range candidates[1:] {
f.Close() // need close before remove on platform windows
if err := os.RemoveAll(f.Name()); err != nil {
return errors.Wrap(err, "delete WAL segment file")
}
f.Close()
}
if err := w.dirFile.Sync(); err != nil {
return err
@ -522,6 +523,15 @@ func (w *SegmentWAL) openSegmentFile(name string) (*os.File, error) {
}
metab := make([]byte, 8)
// If there is an error, we need close f for platform windows before gc.
// Otherwise, file op may fail.
hasError := true
defer func() {
if hasError {
f.Close()
}
}()
if n, err := f.Read(metab); err != nil {
return nil, errors.Wrapf(err, "validate meta %q", f.Name())
} else if n != 8 {
@ -534,6 +544,7 @@ func (w *SegmentWAL) openSegmentFile(name string) (*os.File, error) {
if metab[4] != WALFormatDefault {
return nil, errors.Errorf("unknown WAL segment format %d in %q", metab[4], f.Name())
}
hasError = false
return f, nil
}
@ -702,6 +713,10 @@ func (w *SegmentWAL) Close() error {
if hf := w.head(); hf != nil {
return errors.Wrapf(hf.Close(), "closing WAL head %s", hf.Name())
}
if w.dirFile != nil {
w.dirFile.Close()
}
return nil
}

View File

@ -271,8 +271,9 @@ func TestWALRestoreCorrupted_invalidSegment(t *testing.T) {
Ok(t, err)
f, err := wal.createSegmentFile(dir + "/000001")
Ok(t, err)
_, err = wal.createSegmentFile(dir + "/000002")
f2, err := wal.createSegmentFile(dir + "/000002")
Ok(t, err)
Ok(t, f2.Close()) // close for windows!
// Make header of second segment invalid.
_, err = f.WriteAt([]byte{1, 2, 3, 4}, 0)