mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-25 00:40:29 +08:00
Start goroutine in AOF engine to log queued commands
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package aof
|
package aof
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/echovault/echovault/src/utils"
|
"github.com/echovault/echovault/src/utils"
|
||||||
"log"
|
"log"
|
||||||
@@ -20,18 +21,38 @@ type Opts struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Engine struct {
|
type Engine struct {
|
||||||
options Opts
|
options Opts
|
||||||
mut sync.Mutex
|
mut sync.Mutex
|
||||||
|
logChan chan []byte
|
||||||
|
logCount uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewAOFEngine(opts Opts) *Engine {
|
func NewAOFEngine(opts Opts) *Engine {
|
||||||
return &Engine{
|
return &Engine{
|
||||||
options: opts,
|
options: opts,
|
||||||
mut: sync.Mutex{},
|
mut: sync.Mutex{},
|
||||||
|
logChan: make(chan []byte, 4096),
|
||||||
|
logCount: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) LogCommand(command []byte, sync bool) error {
|
func (engine *Engine) Start(ctx context.Context) {
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
c := <-engine.logChan
|
||||||
|
if err := engine.LogCommand(c); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) QueueCommand(command []byte) {
|
||||||
|
engine.logChan <- command
|
||||||
|
}
|
||||||
|
|
||||||
|
func (engine *Engine) LogCommand(command []byte) error {
|
||||||
engine.mut.Lock()
|
engine.mut.Lock()
|
||||||
defer engine.mut.Unlock()
|
defer engine.mut.Unlock()
|
||||||
|
|
||||||
@@ -60,10 +81,8 @@ func (engine *Engine) LogCommand(command []byte, sync bool) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if sync {
|
if err = f.Sync(); err != nil {
|
||||||
if err = f.Sync(); err != nil {
|
log.Println(err)
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@@ -114,7 +133,7 @@ func (engine *Engine) RewriteLog() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) Restore() error {
|
func (engine *Engine) Restore(ctx context.Context) error {
|
||||||
// Open snapshot file.
|
// Open snapshot file.
|
||||||
// If snapshot file exists, set current state to the state in snapshot file.
|
// If snapshot file exists, set current state to the state in snapshot file.
|
||||||
// Open AOF file.
|
// Open AOF file.
|
||||||
|
|||||||
@@ -176,7 +176,7 @@ func (server *Server) handleConnection(ctx context.Context, conn net.Conn) {
|
|||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
if utils.IsWriteCommand(command, subCommand) {
|
if utils.IsWriteCommand(command, subCommand) {
|
||||||
// TODO: Queue successful write command instead of logging it directly
|
go server.AOFEngine.QueueCommand(message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
server.StateMutationInProgress.Store(false)
|
server.StateMutationInProgress.Store(false)
|
||||||
@@ -256,6 +256,10 @@ func (server *Server) Start(ctx context.Context) {
|
|||||||
StartRewriteAOF: server.StartRewriteAOF,
|
StartRewriteAOF: server.StartRewriteAOF,
|
||||||
FinishRewriteAOF: server.FinishRewriteAOF,
|
FinishRewriteAOF: server.FinishRewriteAOF,
|
||||||
})
|
})
|
||||||
|
if err := server.AOFEngine.Restore(ctx); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
server.AOFEngine.Start(ctx)
|
||||||
// Initialize and start standalone snapshot engine
|
// Initialize and start standalone snapshot engine
|
||||||
server.SnapshotEngine = snapshot.NewSnapshotEngine(snapshot.Opts{
|
server.SnapshotEngine = snapshot.NewSnapshotEngine(snapshot.Opts{
|
||||||
Config: conf,
|
Config: conf,
|
||||||
|
|||||||
Reference in New Issue
Block a user