-
Notifications
You must be signed in to change notification settings - Fork 0
/
discard.go
261 lines (235 loc) · 5.82 KB
/
discard.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package lazydb
import (
"encoding/binary"
"errors"
"fmt"
"github.com/billsjc123/LazyDB/iocontroller"
"github.com/billsjc123/LazyDB/logfile"
"io"
"log"
"path/filepath"
"sort"
"sync"
)
const (
discardRecordSize = 12
discardFileSize int64 = 2 << 12
discardFileName = "discard"
)
var (
ErrDiscardNoSpace = errors.New("not enough space can be allocated for the discard file")
)
// format of discard file` record:
// +-------+--------------+----------------+ +-------+--------------+----------------+
// | fid | total size | discarded size | | fid | total size | discarded size |
// +-------+--------------+----------------+ +-------+--------------+----------------+
// 0-------4--------------8---------------12 12------16------------20----------------24
type discard struct {
sync.Mutex
once *sync.Once
file iocontroller.IOController
valChan chan *Value
freeList []int64 // contains file offset that can be allocated
location map[uint32]int64 // offset of each fid
}
// initDiscard returns a new
func newDiscard(path, name string, buffersize int) (*discard, error) {
fname := filepath.Join(path, name)
file, err := iocontroller.NewFileIOController(fname, discardFileSize)
if err != nil {
return nil, err
}
freeList := make([]int64, 0)
location := map[uint32]int64{}
var offset int64
for {
buf := make([]byte, discardRecordSize)
if _, err := file.Read(buf, offset); err != nil {
if err == io.EOF || err == logfile.ErrLogEndOfFile {
break
}
return nil, err
}
fid := binary.LittleEndian.Uint32(buf[:4])
total := binary.LittleEndian.Uint32(buf[4:8])
if fid == 0 && total == 0 {
freeList = append(freeList, offset)
} else {
location[fid] = offset
}
offset += discardRecordSize
}
d := &discard{
once: new(sync.Once),
file: file,
valChan: make(chan *Value, buffersize),
freeList: freeList,
location: location,
}
go d.listenUpdate()
return d, nil
}
// listenUpdate listens to valChan, and close discard file when channel is closed
func (d *discard) listenUpdate() {
for {
select {
case val, ok := <-d.valChan:
if !ok {
if err := d.file.Close(); err != nil {
log.Fatalf("close discard file err: %v", err)
}
return
}
d.incrDiscard(val.fid, val.entrySize)
}
}
}
func (d *discard) closeChan() {
d.once.Do(func() {
close(d.valChan)
})
}
// remove a discard entry when a logfile is deleted
func (d *discard) removeDiscard(fid uint32) {
if fid == 0 {
return
}
d.Lock()
defer d.Unlock()
offset, ok := d.location[fid]
if !ok {
return
}
buf := make([]byte, discardRecordSize)
if _, err := d.file.Write(buf, offset); err != nil {
log.Fatalf("remove discard err:%v", err)
return
}
delete(d.location, fid)
d.freeList = append(d.freeList, offset)
}
func (d *discard) incrDiscard(fid uint32, delta int) {
if delta <= 0 {
return
}
d.Lock()
defer d.Unlock()
offset, err := d.alloc(fid)
if err != nil {
log.Fatalf("discard file allocate err: %+v", err)
return
}
buf := make([]byte, 4)
offset += 8
if _, err := d.file.Read(buf, offset); err != nil {
log.Fatalf("incr value in discard err: %+v", err)
return
}
v := binary.LittleEndian.Uint32(buf[:4])
binary.LittleEndian.PutUint32(buf, v+uint32(delta))
if _, err = d.file.Write(buf, offset); err != nil {
log.Fatalf("incr value in discard err: %+v", err)
return
}
}
func (d *discard) alloc(fid uint32) (int64, error) {
if offset, ok := d.location[fid]; ok {
return offset, nil
}
if len(d.freeList) == 0 {
return 0, ErrDiscardNoSpace
}
offset := d.freeList[len(d.freeList)-1]
d.freeList = d.freeList[:len(d.freeList)-1]
d.location[fid] = offset
return offset, nil
}
func (d *discard) setTotal(fid, total uint32) {
d.Lock()
defer d.Unlock()
if fid == 0 || total == 0 {
return
}
// totalSize already been set
if _, ok := d.location[fid]; ok {
return
}
offset, err := d.alloc(fid)
if err != nil {
log.Fatalf("discard file allocate err: %+v", err)
return
}
buf := make([]byte, 8)
binary.LittleEndian.PutUint32(buf[:4], fid)
binary.LittleEndian.PutUint32(buf[4:8], total)
if _, err = d.file.Write(buf, offset); err != nil {
log.Fatalf("set total in discard err: %+v", err)
return
}
}
// CCL means compaction cnadidate list.
// iterate and find the file with most discarded data,
// there are 682 records at most, no need to worry about the performance.
func (d *discard) getCCL(activeFid uint32, ratio float64) ([]uint32, error) {
var offset int64
ccl := make([]uint32, 0)
d.Lock()
defer d.Unlock()
for {
if offset == 8172 {
fmt.Print("")
}
buf := make([]byte, discardRecordSize)
if _, err := d.file.Read(buf, offset); err != nil {
if err == logfile.ErrLogEndOfFile || err == io.EOF {
break
}
return nil, err
}
offset += discardRecordSize
fid := binary.LittleEndian.Uint32(buf[:4])
totalSize := binary.LittleEndian.Uint32(buf[4:8])
v := binary.LittleEndian.Uint32(buf[8:12])
if fid == activeFid {
continue
}
var curRatio float64
if totalSize != 0 && fid != 0 {
curRatio = float64(v) / float64(totalSize)
}
if curRatio > ratio {
ccl = append(ccl, fid)
}
}
// older log file will be merge firstly
sort.Slice(ccl, func(i, j int) bool {
return ccl[i] < ccl[j]
})
return ccl, nil
}
func (d *discard) sync() error {
return d.file.Sync()
}
func (d *discard) close() error {
return d.file.Close()
}
func (d *discard) clear(fid uint32) {
d.Lock()
defer d.Unlock()
// re-initialize
offset, err := d.alloc(fid)
if err != nil {
log.Fatalf("discard file allocate err: %+v", err)
return
}
buf := make([]byte, discardRecordSize)
if _, err := d.file.Write(buf, offset); err != nil {
log.Fatalf("incr value in discard err:%v", err)
return
}
// release free space of discard file
if offset, ok := d.location[fid]; ok {
d.freeList = append(d.freeList, offset)
delete(d.location, fid)
}
}