jldb/mdb/follower.go

69 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-10-13 09:43:27 +00:00
package mdb
/*
type follower struct {
Stop chan struct{}
Done *sync.WaitGroup
WALRootDir string
SeqNum uint64 // Current max applied sequence number.
ApplyChanges func(rec *cswal.Record) error
seqNum uint64 // Current max applied sequence number.
}
func (f *follower) Run() {
defer f.Done.Done()
f.seqNum = f.SeqNum
for {
f.runOnce()
select {
case <-f.Stop:
return
default:
// Something went wrong.
time.Sleep(time.Second)
}
}
}
func (f *follower) runOnce() {
it, err := cswal.NewIterator(f.WALRootDir, f.seqNum+1)
if err != nil {
f.logf("Failed to get WAL iterator: %v", errs.FmtDetails(err))
return
}
defer it.Close()
for {
hasNext := it.Next(time.Second)
select {
case <-f.Stop:
return
default:
}
if !hasNext {
if it.Error() != nil {
f.logf("Iteration error: %v", errs.FmtDetails(it.Error()))
return
}
continue
}
rec := it.Record()
if err := f.ApplyChanges(rec); err != nil {
f.logf("Failed to apply changes: %s", errs.FmtDetails(err))
return
}
f.seqNum = rec.SeqNum
}
}
func (f *follower) logf(pattern string, args ...interface{}) {
log.Printf("[FOLLOWER] "+pattern, args...)
}
*/