Prewrite optimization flag

This commit is contained in:
tidwall 2018-11-10 16:16:04 -07:00
parent 74a47309ec
commit 12b47b39ce
2 changed files with 17 additions and 11 deletions

View File

@ -9,6 +9,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/tidwall/buntdb" "github.com/tidwall/buntdb"
@ -152,6 +153,7 @@ func (server *Server) writeAOF(args []string, d *commandDetailsT) error {
} }
if server.aof != nil { if server.aof != nil {
atomic.StoreInt32(&server.aofdirty, 1) // prewrite optimization flag
n := len(server.aofbuf) n := len(server.aofbuf)
server.aofbuf = redcon.AppendArray(server.aofbuf, len(args)) server.aofbuf = redcon.AppendArray(server.aofbuf, len(args))
for _, arg := range args { for _, arg := range args {

View File

@ -94,6 +94,7 @@ type Server struct {
mu sync.RWMutex mu sync.RWMutex
aof *os.File // active aof file aof *os.File // active aof file
aofdirty int32 // mark the aofbuf as having data
aofbuf []byte // prewrite buffer aofbuf []byte // prewrite buffer
aofsz int // active size of the aof file aofsz int // active size of the aof file
qdb *buntdb.DB // hook queue log qdb *buntdb.DB // hook queue log
@ -473,9 +474,12 @@ func (server *Server) evioServe() error {
} }
events.PreWrite = func() { events.PreWrite = func() {
if atomic.LoadInt32(&server.aofdirty) != 0 {
server.mu.Lock() server.mu.Lock()
defer server.mu.Unlock() defer server.mu.Unlock()
server.flushAOF() server.flushAOF()
atomic.StoreInt32(&server.aofdirty, 1)
}
} }
return evio.Serve(events, fmt.Sprintf("%s:%d", server.host, server.port)) return evio.Serve(events, fmt.Sprintf("%s:%d", server.host, server.port))