-
Notifications
You must be signed in to change notification settings - Fork 32
/
arenamanager.go
184 lines (151 loc) · 3.87 KB
/
arenamanager.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
package bigqueue
import (
"fmt"
"path"
"strconv"
"syscall"
"github.com/grandecola/mmap"
)
const (
cArenaFileSuffix = "_arena.dat"
)
// arenaManager manages all the arenas for a bigqueue
type arenaManager struct {
dir string
conf *bqConfig
md *metadata
baseAid int
arenas []*mmap.File
inMem int
fullPath []byte
}
// newArenaManager returns a pointer to new arenaManager.
func newArenaManager(dir string, conf *bqConfig, md *metadata) (*arenaManager, error) {
headAid, _ := md.getHead()
tailAid, _ := md.getTail()
numArenas := tailAid + 1 - headAid
arenas := make([]*mmap.File, numArenas)
am := &arenaManager{
dir: path.Clean(dir),
conf: conf,
md: md,
baseAid: headAid,
arenas: arenas,
}
// we load the tail arena into memory
if err := am.loadArena(tailAid); err != nil {
return nil, err
}
return am, nil
}
// getArena returns arena for a given arena ID
func (m *arenaManager) getArena(aid int) (*mmap.File, error) {
relAid := aid - m.baseAid
if relAid == len(m.arenas) {
m.arenas = append(m.arenas, nil)
}
aa := m.arenas[relAid]
if aa != nil {
return aa, nil
}
// before we get a new arena into memory, we need to ensure that after fetching
// a new arena into memory, we do not cross the provided memory limit.
if err := m.ensureEnoughMem(); err != nil {
return nil, err
}
// now, get arena into memory
if err := m.loadArena(aid); err != nil {
return nil, err
}
return m.arenas[aid], nil
}
// ensureEnoughMem ensures that at least 1 new arena can be brought into memory.
func (m *arenaManager) ensureEnoughMem() error {
// if no limit on # of arenas, no need for eviction.
if m.conf.maxInMemArenas == 0 {
return nil
}
// Check whether an eviction is needed to begin with.
if m.inMem < m.conf.maxInMemArenas {
return nil
}
// Start evicting from the arena just before the last arena that we have.
// If message size > arena size, last arena may not always be the tail arena.
// We always ensure that head and tail arenas are not evicted from memory.
// Simply iterate from the last arena until enough memory is
// available for a new arena to be loaded into memory
tailAid, _ := m.md.getTail()
curAid := m.baseAid + len(m.arenas)
for m.conf.maxInMemArenas-m.inMem <= 0 {
curAid--
if curAid < 0 {
panic("not enough memory to hold arenas in memory")
}
if curAid == tailAid {
continue
}
if err := m.unloadArena(curAid); err != nil {
return err
}
}
return nil
}
// loadArena will fetch the arena into memory.
func (m *arenaManager) loadArena(aid int) error {
if m.arenas[aid-m.baseAid] != nil {
return nil
}
m.fullPath = append(m.fullPath[:0], m.dir...)
m.fullPath = append(m.fullPath, '/')
m.fullPath = strconv.AppendInt(m.fullPath, int64(aid), 10)
m.fullPath = append(m.fullPath, cArenaFileSuffix...)
aa, err := newArena(string(m.fullPath), m.conf.arenaSize)
if err != nil {
return err
}
m.inMem++
m.arenas[aid-m.baseAid] = aa
return nil
}
// unloadArena will remove the arena from memory.
func (m *arenaManager) unloadArena(aid int) error {
if m.arenas[aid-m.baseAid] == nil {
return nil
}
if err := m.arenas[aid-m.baseAid].Unmap(); err != nil {
return fmt.Errorf("error in unmap :: %w", err)
}
m.inMem--
m.arenas[aid-m.baseAid] = nil
return nil
}
func (m *arenaManager) flush() error {
for _, aa := range m.arenas {
if aa == nil {
continue
}
if err := aa.Flush(syscall.MS_SYNC); err != nil {
return fmt.Errorf("error in flushing arena file :: %w", err)
}
}
if err := m.md.flush(); err != nil {
return err
}
return nil
}
// close unmaps all the arenas managed by arenaManager.
func (m *arenaManager) close() error {
var retErr error
for _, aa := range m.arenas {
if aa == nil {
continue
}
if err := aa.Unmap(); err != nil {
retErr = err
}
}
if retErr != nil {
return fmt.Errorf("error in unmap :: %w", retErr)
}
return nil
}