forked from lotusdblabs/lotusdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflush.go
138 lines (128 loc) · 3.39 KB
/
flush.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package lotusdb
import (
"os"
"os/signal"
"syscall"
"time"
"github.com/flower-corp/lotusdb/index"
"github.com/flower-corp/lotusdb/logfile"
"github.com/flower-corp/lotusdb/logger"
)
func (cf *ColumnFamily) waitWritesMemSpace(size uint32) error {
cf.mu.Lock()
defer cf.mu.Unlock()
if !cf.activeMem.isFull(size) {
return nil
}
timer := time.NewTimer(cf.opts.MemSpaceWaitTimeout)
defer timer.Stop()
select {
case cf.flushChn <- cf.activeMem:
cf.immuMems = append(cf.immuMems, cf.activeMem)
// open a new active memtable.
var ioType = logfile.FileIO
if cf.opts.WalMMap {
ioType = logfile.MMap
}
memOpts := memOptions{
path: cf.opts.DirPath,
fid: cf.activeMem.logFileId() + 1,
fsize: int64(cf.opts.MemtableSize),
ioType: ioType,
memSize: cf.opts.MemtableSize,
bytesFlush: cf.opts.WalBytesFlush,
}
if table, err := openMemtable(memOpts); err != nil {
return err
} else {
cf.activeMem = table
}
case <-timer.C:
return ErrWaitMemSpaceTimeout
}
return nil
}
func (cf *ColumnFamily) listenAndFlush() {
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
for {
select {
case table := <-cf.flushChn:
var nodes []*index.IndexerNode
var deletedKeys [][]byte
iterateTable := func() error {
table.Lock()
defer table.Unlock()
iter := table.sklIter
// iterate and write data to bptree and value log(if any).
for iter.SeekToFirst(); iter.Valid(); iter.Next() {
key := iter.Key()
node := &index.IndexerNode{Key: key}
mv := decodeMemValue(iter.Value())
// delete invalid keys from indexer.
if mv.typ == byte(logfile.TypeDelete) || (mv.expiredAt != 0 && mv.expiredAt <= time.Now().Unix()) {
deletedKeys = append(deletedKeys, key)
} else {
valuePos, esize, err := cf.vlog.Write(&logfile.LogEntry{
Key: key,
Value: mv.value,
ExpiredAt: mv.expiredAt,
})
if err != nil {
return err
}
node.Meta = &index.IndexerMeta{
Fid: valuePos.Fid,
Offset: valuePos.Offset,
EntrySize: esize,
}
nodes = append(nodes, node)
}
}
return nil
}
if err := iterateTable(); err != nil {
logger.Errorf("listenAndFlush: handle iterate table err.%+v", err)
return
}
if err := cf.flushUpdateIndex(nodes, deletedKeys); err != nil {
logger.Errorf("listenAndFlush: update index err.%+v", err)
return
}
// delete wal after flush to indexer.
if err := table.deleteWal(); err != nil {
logger.Errorf("listenAndFlush: delete wal log file err.%+v", err)
}
cf.mu.Lock()
if len(cf.immuMems) > 1 {
cf.immuMems = cf.immuMems[1:]
} else {
cf.immuMems = cf.immuMems[:0]
}
cf.mu.Unlock()
// cf has closed or force quit.
case <-sig:
case <-cf.closedC:
return
}
}
}
func (cf *ColumnFamily) flushUpdateIndex(nodes []*index.IndexerNode, keys [][]byte) error {
cf.flushLock.Lock()
defer cf.flushLock.Unlock()
// must put and delete in batch.
writeOpts := index.WriteOptions{SendDiscard: true}
if _, err := cf.indexer.PutBatch(nodes, writeOpts); err != nil {
return err
}
if len(keys) > 0 {
if err := cf.indexer.DeleteBatch(keys, writeOpts); err != nil {
return err
}
}
// must fsync before delete wal.
if err := cf.indexer.Sync(); err != nil {
return err
}
return nil
}