From 12b47b39cecf4ea9301f57745c5206de132a67fd Mon Sep 17 00:00:00 2001 From: tidwall Date: Sat, 10 Nov 2018 16:16:04 -0700 Subject: [PATCH] Prewrite optimization flag --- internal/server/aof.go | 2 ++ internal/server/server.go | 26 +++++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/internal/server/aof.go b/internal/server/aof.go index ef127b0c..3d038347 100644 --- a/internal/server/aof.go +++ b/internal/server/aof.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" "github.com/tidwall/buntdb" @@ -152,6 +153,7 @@ func (server *Server) writeAOF(args []string, d *commandDetailsT) error { } if server.aof != nil { + atomic.StoreInt32(&server.aofdirty, 1) // prewrite optimization flag n := len(server.aofbuf) server.aofbuf = redcon.AppendArray(server.aofbuf, len(args)) for _, arg := range args { diff --git a/internal/server/server.go b/internal/server/server.go index fdd0cb09..af82aa7e 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -92,14 +92,15 @@ type Server struct { exlistmu sync.RWMutex exlist []exitem - mu sync.RWMutex - aof *os.File // active aof file - aofbuf []byte // prewrite buffer - aofsz int // active size of the aof file - qdb *buntdb.DB // hook queue log - qidx uint64 // hook queue log last idx - cols ds.BTree // data collections - expires map[string]map[string]time.Time // synced with cols + mu sync.RWMutex + aof *os.File // active aof file + aofdirty int32 // mark the aofbuf as having data + aofbuf []byte // prewrite buffer + aofsz int // active size of the aof file + qdb *buntdb.DB // hook queue log + qidx uint64 // hook queue log last idx + cols ds.BTree // data collections + expires map[string]map[string]time.Time // synced with cols follows map[*bytes.Buffer]bool fcond *sync.Cond @@ -473,9 +474,12 @@ func (server *Server) evioServe() error { } events.PreWrite = func() { - server.mu.Lock() - defer server.mu.Unlock() - server.flushAOF() + if atomic.LoadInt32(&server.aofdirty) != 0 { + server.mu.Lock() + defer server.mu.Unlock() + server.flushAOF() + atomic.StoreInt32(&server.aofdirty, 1) + } } return evio.Serve(events, fmt.Sprintf("%s:%d", server.host, server.port))