-
Notifications
You must be signed in to change notification settings - Fork 31
/
metadata.go
301 lines (259 loc) · 8.55 KB
/
metadata.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package bigqueue
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"syscall"
"github.com/grandecola/mmap"
)
const (
cMetadataVersion = 1
cMetadataFileName = "metadata.dat"
// size of file without any consumer information.
cMetadataSize = 56
)
var (
// ErrIncompatibleVersion is returned when file format is older/newer.
ErrIncompatibleVersion = errors.New("incompatible format of the code and data")
)
// metadata stores head, tail and config parameters for a bigqueue.
type metadata struct {
aa *mmap.File
co map[string]int64
file string
size int64
}
// newMetadata creates/reads metadata file for a bigqueue.
func newMetadata(dataDir string, arenaSize int) (*metadata, error) {
metaPath := filepath.Join(dataDir, cMetadataFileName)
info, err := os.Stat(metaPath)
switch {
case err != nil && !os.IsNotExist(err):
return nil, fmt.Errorf("error in reading metadata file :: %w", err)
case err != nil && os.IsNotExist(err):
return createFile(metaPath)
default:
return loadFile(metaPath, info.Size())
}
}
// loadFile loads the file and builds the struct for metadata.
func loadFile(metaPath string, size int64) (*metadata, error) {
aa, err := newArena(metaPath, int(size))
if err != nil {
return nil, fmt.Errorf("error in creating arena for metadata file :: %w", err)
}
md := &metadata{
aa: aa,
co: make(map[string]int64),
file: metaPath,
size: size,
}
if md.getVersion() != cMetadataVersion {
return nil, ErrIncompatibleVersion
}
base := int64(cMetadataSize)
for i := 0; i < md.getNumConsumers(); i++ {
name := md.getConsumerName(base)
md.co[name] = base
base += int64(len(name)) + 24
}
return md, nil
}
// createFile creates a new metadata file.
func createFile(metaPath string) (*metadata, error) {
aa, err := newArena(metaPath, cMetadataSize)
if err != nil {
return nil, fmt.Errorf("error in creating arena for metadata file :: %w", err)
}
md := &metadata{
aa: aa,
co: make(map[string]int64),
file: metaPath,
size: cMetadataSize,
}
md.putVersion()
return md, nil
}
// getVersion reads the value of data format version.
//
// <-------- version ------->
// +------------+------------+
// | byte 00-03 | byte 04-07 |
// +------------+------------+
//
func (m *metadata) getVersion() int {
return int(m.aa.ReadUint64At(0))
}
// putVersion writes the metadata format version.
func (m *metadata) putVersion() {
m.aa.WriteUint64At(cMetadataVersion, 0)
}
// getHead reads the value of head of the queue from the metadata.
// head points to the first element that is still not deleted yet.
// Head of a bigqueue can be identified using:
// 1. Arena ID
// 2. Position (offset) in the arena
//
// <------- head aid ------> <------- head pos ------>
// +------------+------------+------------+------------+
// | byte 08-11 | byte 12-15 | byte 16-19 | byte 20-23 |
// +------------+------------+------------+------------+
//
func (m *metadata) getHead() (int, int) {
return int(m.aa.ReadUint64At(8)), int(m.aa.ReadUint64At(16))
}
// putHead stores the value of head in the metadata.
// func (m *metadata) putHead(aid, pos int) {
// m.aa.WriteUint64At(uint64(aid), 8)
// m.aa.WriteUint64At(uint64(pos), 16)
// }
// getTail reads the values of tail of the queue from the metadata arena.
// Tail of a bigqueue, similar to head, can be identified using:
// 1. Arena ID
// 2. Position (offset) in the arena
//
// <------- tail aid ------> <------- tail pos ------>
// +------------+------------+------------+------------+
// | byte 24-27 | byte 28-31 | byte 32-35 | byte 36-39 |
// +------------+------------+------------+------------+
func (m *metadata) getTail() (int, int) {
return int(m.aa.ReadUint64At(24)), int(m.aa.ReadUint64At(32))
}
// putTail stores the value of tail in the metadata arena.
func (m *metadata) putTail(aid, pos int) {
m.aa.WriteUint64At(uint64(aid), 24)
m.aa.WriteUint64At(uint64(pos), 32)
}
// getArenaSize reads the value of arena size from metadata file.
//
// <------ arena size ----->
// +------------+------------+
// | byte 40-43 | byte 44-47 |
// +------------+------------+
//
func (m *metadata) getArenaSize() int {
return int(m.aa.ReadUint64At(40))
}
// putArenaSize stores the value of arena size in the metadata.
func (m *metadata) putArenaSize(size int) {
m.aa.WriteUint64At(uint64(size), 40)
}
// getNumConsumers reads the value of # of consumers from metadata file.
//
// <---- # of consumers --->
// +------------+------------+
// | byte 48-51 | byte 52-55 |
// +------------+------------+
//
func (m *metadata) getNumConsumers() int {
return int(m.aa.ReadUint64At(48))
}
// putNumConsumers stores the value of # of consumers in the metadata.
func (m *metadata) putNumConsumers(size int) {
m.aa.WriteUint64At(uint64(size), 48)
}
/*
* Now, we store all the conusmer information in the metadata file.
* We store 4 things for a given consumer (in this order) -
* 1. Consumer name length (8 bytes)
* 2. Head arena id (8 bytes)
* 3. Head position in the arena (8 bytes)
* 4. Name of the consumer (length)
*/
// getConsumerLength reads the length of the consumer name for
// the consumer stored at a given base offset in metadata file.
//
// <------------- consumer length ----------->
// +--------------------+----------------------+
// | byte base - base+3 | byte base+4 - base+7 |
// +--------------------+----------------------+
//
func (m *metadata) getConsumerLength(base int64) int {
return int(m.aa.ReadUint64At(base))
}
// putConsumerLength writes the length of the consumer name into the metadata file.
func (m *metadata) putConsumerLength(base int64, length int) {
m.aa.WriteUint64At(uint64(length), base)
}
// getConsumerHead reads the head position (aid+offset) for
// the consumer stored at a given base offset in metadata file.
//
// <-------------- consumer head AID -------------> <-------------- consumer head pos -------------->
// +-----------------------+------------------------+------------------------+------------------------+
// | byte base+8 - base+11 | byte base+12 - base+15 | byte base+16 - base+19 | byte base+20 - base+23 |
// +-----------------------+------------------------+------------------------+------------------------+
//
func (m *metadata) getConsumerHead(base int64) (int, int) {
return int(m.aa.ReadUint64At(base + 8)), int(m.aa.ReadUint64At(base + 16))
}
// putConsumerHead writes the head position of the consumer into the metadata file.
func (m *metadata) putConsumerHead(base int64, aid, pos int) {
m.aa.WriteUint64At(uint64(aid), base+8)
m.aa.WriteUint64At(uint64(pos), base+16)
}
// getConsumerName reads the name of the consumer stored at a given offset in metadata.
func (m *metadata) getConsumerName(base int64) string {
sb := &strings.Builder{}
sb.Grow(m.getConsumerLength(base))
_ = m.aa.ReadStringAt(sb, base+24)
return sb.String()
}
// putConsumerName writes the name of the consumer in the metadata file.
// name is stored at offset 24 until it can be fully stored in the file.
func (m *metadata) putConsumerName(base int64, name string) {
m.aa.WriteStringAt(name, base+24)
}
// putConsumer writes the consumer in the metadata file.
func (m *metadata) putConsumer(base int64, name string) {
m.putConsumerLength(base, len(name))
aid, offset := m.getHead()
m.putConsumerHead(base, aid, offset)
m.putConsumerName(base, name)
m.putNumConsumers(m.getNumConsumers() + 1)
m.co[name] = base
}
// getConsumer either finds an existing consumer with the given name or initializes
// a new consumer with the given name and stores it into the metadata file.
func (m *metadata) getConsumer(name string) (int64, error) {
if b, ok := m.co[name]; ok {
return b, nil
}
oldsize := m.size
newsize := m.size + 24 + int64(len(name))
if err := m.extendFile(newsize); err != nil {
return 0, err
}
m.size = newsize
m.putConsumer(oldsize, name)
return oldsize, nil
}
// flush writes the memory state of the metadata arena on to disk.
func (m *metadata) flush() error {
return m.aa.Flush(syscall.MS_SYNC)
}
// close releases all the resources currently used by the metadata.
func (m *metadata) close() error {
if err := m.flush(); err != nil {
return err
}
return m.aa.Unmap()
}
// extendFile extends the metadata file to given size.
func (m *metadata) extendFile(size int64) error {
if err := m.close(); err != nil {
return err
}
// extend the file
if err := os.Truncate(m.file, size); err != nil {
return fmt.Errorf("error in extending metadata file :: %w", err)
}
// remap the arena with bigger size
aa, err := newArena(m.file, int(size))
if err != nil {
return fmt.Errorf("error in remapping metadata file :: %w", err)
}
m.aa = aa
return nil
}