From 28277b5a654032e8485c52e1eac3f078161ddd2e Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Tue, 30 Aug 2016 13:40:47 -0700 Subject: [PATCH] wal: use page buffered writer for writing records Forces torn writes to only happen on sector boundaries. Fixes #6271 --- wal/encoder.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/wal/encoder.go b/wal/encoder.go index fdeceaf80..edbd1785a 100644 --- a/wal/encoder.go +++ b/wal/encoder.go @@ -15,19 +15,24 @@ package wal import ( - "bufio" "encoding/binary" "hash" "io" "sync" "github.com/coreos/etcd/pkg/crc" + "github.com/coreos/etcd/pkg/ioutil" "github.com/coreos/etcd/wal/walpb" ) +// walPageBytes is the alignment for flushing records to the backing Writer. +// It should be a multiple of the minimum sector size so that WAL repair can +// safely between torn writes and ordinary data corruption. +const walPageBytes = 8 * minSectorSize + type encoder struct { mu sync.Mutex - bw *bufio.Writer + bw *ioutil.PageWriter crc hash.Hash32 buf []byte @@ -36,7 +41,7 @@ type encoder struct { func newEncoder(w io.Writer, prevCrc uint32) *encoder { return &encoder{ - bw: bufio.NewWriter(w), + bw: ioutil.NewPageWriter(w, walPageBytes), crc: crc.New(prevCrc, crcTable), // 1MB buffer buf: make([]byte, 1024*1024),