-
Notifications
You must be signed in to change notification settings - Fork 106
/
meta_storage.go
401 lines (359 loc) · 10 KB
/
meta_storage.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package nsqd
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path"
"strconv"
"sync"
"time"
"github.com/absolute8511/bolt"
"github.com/spaolacci/murmur3"
)
const (
metaDataBucket = "meta_data_bucket"
)
var errMetaNotFound = errors.New("meta not found")
type IMetaStorage interface {
PersistReader(key string, fsync bool, confirmed diskQueueEndInfo, queueEndInfo diskQueueEndInfo) error
RetrieveReader(key string) (diskQueueEndInfo, diskQueueEndInfo, error)
Remove(key string)
PersistWriter(key string, fsync bool, wend diskQueueEndInfo) error
RetrieveWriter(key string) (diskQueueEndInfo, error)
Sync()
Close()
}
type fileMetaStorage struct {
metaLock sync.Mutex
}
func checkMetaFileEnd(f *os.File) error {
endFlag := make([]byte, len(diskMagicEndBytes))
n, err := f.Read(endFlag)
if err != nil {
if err == io.EOF {
// old meta file
} else {
nsqLog.Errorf("reader meta end error, need fix: %v", err.Error())
return errInvalidMetaFileData
}
} else if !bytes.Equal(endFlag[:n], diskMagicEndBytes) {
nsqLog.Errorf("reader meta end data invalid: %v need fix: %v", n, endFlag)
return errInvalidMetaFileData
}
return nil
}
func (fs *fileMetaStorage) Sync() {
}
func (fs *fileMetaStorage) Close() {
}
// retrieveMetaData initializes state from the filesystem
func (fs *fileMetaStorage) RetrieveReader(fileName string) (diskQueueEndInfo, diskQueueEndInfo, error) {
var f *os.File
var err error
var confirmed diskQueueEndInfo
var queueEnd diskQueueEndInfo
f, err = os.OpenFile(fileName, os.O_RDONLY, 0644)
if err != nil {
return confirmed, queueEnd, err
}
defer f.Close()
_, err = fmt.Fscanf(f, "%d\n%d\n%d,%d,%d\n%d,%d,%d\n",
&confirmed.totalMsgCnt,
&queueEnd.totalMsgCnt,
&confirmed.EndOffset.FileNum, &confirmed.EndOffset.Pos, &confirmed.virtualEnd,
&queueEnd.EndOffset.FileNum, &queueEnd.EndOffset.Pos, &queueEnd.virtualEnd)
if err != nil {
nsqLog.Infof("fscanf new meta file err : %v", err)
return confirmed, queueEnd, err
}
err = checkMetaFileEnd(f)
if err != nil {
nsqLog.Errorf("reader (%v) meta invalid, need fix: %v", fileName, err.Error())
}
return confirmed, queueEnd, err
}
func preWriteMetaEnd(f *os.File) error {
// error can be ignored since we just make sure end with non-magic
f.Seek(-1*int64(len(diskMagicEndBytes)), os.SEEK_END)
_, err := f.Write(make([]byte, len(diskMagicEndBytes)))
if err != nil {
return err
}
_, err = f.Seek(0, os.SEEK_SET)
return err
}
func writeMeta(f *os.File, perr error, confirmed diskQueueEndInfo, queueEndInfo diskQueueEndInfo) (int, error) {
if perr != nil {
return 0, perr
}
n, err := fmt.Fprintf(f, "%d\n%d\n%d,%d,%d\n%d,%d,%d\n",
confirmed.TotalMsgCnt(),
queueEndInfo.totalMsgCnt,
confirmed.EndOffset.FileNum, confirmed.EndOffset.Pos, confirmed.Offset(),
queueEndInfo.EndOffset.FileNum, queueEndInfo.EndOffset.Pos, queueEndInfo.Offset())
return n, err
}
func writeMetaEnd(f *os.File, perr error, pos int) (int, error) {
if perr != nil {
return 0, perr
}
// write magic end
n, err := f.Write(diskMagicEndBytes)
pos += n
f.Truncate(int64(pos))
return n, err
}
func (fs *fileMetaStorage) Remove(fileName string) {
fs.metaLock.Lock()
defer fs.metaLock.Unlock()
os.Remove(fileName)
}
// persistMetaData atomically writes state to the filesystem
func (fs *fileMetaStorage) PersistReader(fileName string, fsync bool, confirmed diskQueueEndInfo, queueEndInfo diskQueueEndInfo) error {
var f *os.File
var err error
var n int
pos := 0
s := time.Now()
fs.metaLock.Lock()
defer fs.metaLock.Unlock()
f, err = os.OpenFile(fileName, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
cost1 := time.Since(s)
err = preWriteMetaEnd(f)
if testCrash {
return errors.New("test crash")
}
cost2 := time.Since(s)
n, err = writeMeta(f, err, confirmed, queueEndInfo)
pos += n
_, err = writeMetaEnd(f, err, pos)
if err != nil {
nsqLog.Errorf("reader (%v) meta write failed, need fix: %v", fileName, err.Error())
} else if fsync {
f.Sync()
}
f.Close()
cost3 := time.Since(s)
if cost3 >= slowCost {
nsqLog.Logf("reader (%v) meta persist cost: %v,%v,%v", fileName, cost1, cost2, cost3)
}
return err
}
func (fs *fileMetaStorage) RetrieveWriter(fileName string) (diskQueueEndInfo, error) {
var queueEnd diskQueueEndInfo
return queueEnd, nil
}
func (fs *fileMetaStorage) PersistWriter(fileName string, fsync bool, queueEndInfo diskQueueEndInfo) error {
return nil
}
type dbMetaData struct {
EndOffset diskQueueOffset
VirtualEnd BackendOffset
TotalMsgCnt int64
}
type dbMetaStorage struct {
dataPath string
db *bolt.DB
fileMeta *fileMetaStorage
}
func NewDBMetaStorage(p string) (*dbMetaStorage, error) {
ro := &bolt.Options{
Timeout: time.Second,
ReadOnly: false,
FreelistType: bolt.FreelistMapType,
}
os.MkdirAll(p, 0755)
db, err := bolt.Open(path.Join(p, "meta.db"), 0644, ro)
if err != nil {
nsqLog.LogErrorf("failed to init bolt db meta storage: %v , %v ", p, err)
return nil, err
}
db.NoSync = true
err = db.Update(func(tx *bolt.Tx) error {
_, errC := tx.CreateBucketIfNotExists([]byte(metaDataBucket))
return errC
})
if err != nil {
nsqLog.LogErrorf("failed to init bucket: %v , %v ", p, err)
return nil, err
}
return &dbMetaStorage{
db: db,
dataPath: p,
fileMeta: &fileMetaStorage{},
}, nil
}
func (dbs *dbMetaStorage) PersistReader(key string, fsync bool, confirmed diskQueueEndInfo, queueEndInfo diskQueueEndInfo) error {
var metaConfirmed dbMetaData
metaConfirmed.EndOffset = confirmed.EndOffset
metaConfirmed.VirtualEnd = confirmed.virtualEnd
metaConfirmed.TotalMsgCnt = confirmed.totalMsgCnt
var metaQueueEnd dbMetaData
metaQueueEnd.EndOffset = queueEndInfo.EndOffset
metaQueueEnd.TotalMsgCnt = queueEndInfo.totalMsgCnt
metaQueueEnd.VirtualEnd = queueEndInfo.virtualEnd
err := dbs.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(metaDataBucket))
newV, _ := json.Marshal(metaConfirmed)
ck := []byte(key + "-confirmed")
oldV := b.Get(ck)
exists := oldV != nil
if exists && bytes.Equal(oldV, newV) {
} else {
err := b.Put(ck, newV)
if err != nil {
return err
}
}
ek := []byte(key + "-end")
newV, _ = json.Marshal(metaQueueEnd)
oldV = b.Get(ek)
exists = oldV != nil
if exists && bytes.Equal(oldV, newV) {
} else {
err := b.Put(ek, newV)
if err != nil {
return err
}
}
return nil
})
if fsync && err == nil {
dbs.Sync()
}
if err != nil {
nsqLog.LogErrorf("failed to save meta key %v from db: %v , %v ", key, dbs.dataPath, err)
}
return err
}
func (dbs *dbMetaStorage) Remove(key string) {
dbs.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(metaDataBucket))
ck := []byte(key + "-confirmed")
b.Delete(ck)
ek := []byte(key + "-end")
b.Delete(ek)
return nil
})
dbs.fileMeta.Remove(key)
}
func (dbs *dbMetaStorage) RetrieveReader(key string) (diskQueueEndInfo, diskQueueEndInfo, error) {
var confirmed diskQueueEndInfo
var queueEnd diskQueueEndInfo
var metaConfirmed dbMetaData
var metaEnd dbMetaData
fallback := false
err := dbs.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(metaDataBucket))
ck := []byte(key + "-confirmed")
ek := []byte(key + "-end")
v := b.Get(ck)
v2 := b.Get(ek)
if v == nil && v2 == nil {
fallback = true
return errMetaNotFound
}
err := json.Unmarshal(v, &metaConfirmed)
if err != nil {
return err
}
err = json.Unmarshal(v2, &metaEnd)
if err != nil {
return err
}
return nil
})
if err != nil {
nsqLog.Warningf("failed to read meta key %v from db: %v , %v ", key, dbs.dataPath, err)
}
if fallback || err != nil {
nsqLog.Logf("fallback to read meta key %v from file meta", key)
return dbs.fileMeta.RetrieveReader(key)
}
confirmed.EndOffset = metaConfirmed.EndOffset
confirmed.totalMsgCnt = metaConfirmed.TotalMsgCnt
confirmed.virtualEnd = metaConfirmed.VirtualEnd
queueEnd.EndOffset = metaEnd.EndOffset
queueEnd.totalMsgCnt = metaEnd.TotalMsgCnt
queueEnd.virtualEnd = metaEnd.VirtualEnd
return confirmed, queueEnd, err
}
func (dbs *dbMetaStorage) PersistWriter(key string, fsync bool, queueEndInfo diskQueueEndInfo) error {
return nil
}
func (dbs *dbMetaStorage) RetrieveWriter(key string) (diskQueueEndInfo, error) {
var queueEnd diskQueueEndInfo
return queueEnd, nil
}
func (dbs *dbMetaStorage) Sync() {
if dbs.db != nil {
dbs.db.Sync()
}
dbs.fileMeta.Sync()
}
func (dbs *dbMetaStorage) Close() {
if dbs.db != nil {
dbs.db.Sync()
dbs.db.Close()
}
dbs.fileMeta.Close()
}
type shardedDBMetaStorage struct {
dataPath string
dbShards [8]*dbMetaStorage
}
func NewShardedDBMetaStorage(p string) (*shardedDBMetaStorage, error) {
d := &shardedDBMetaStorage{
dataPath: p,
}
var err error
for i := 0; i < len(d.dbShards); i++ {
d.dbShards[i], err = NewDBMetaStorage(path.Join(p, strconv.Itoa(i)))
if err != nil {
return nil, err
}
}
return d, nil
}
func (ss *shardedDBMetaStorage) RetrieveReader(key string) (diskQueueEndInfo, diskQueueEndInfo, error) {
h := int(murmur3.Sum32([]byte(key))) % len(ss.dbShards)
d := ss.dbShards[h]
return d.RetrieveReader(key)
}
func (ss *shardedDBMetaStorage) Remove(key string) {
h := int(murmur3.Sum32([]byte(key))) % len(ss.dbShards)
d := ss.dbShards[h]
d.Remove(key)
}
func (ss *shardedDBMetaStorage) PersistReader(key string, fsync bool, confirmed diskQueueEndInfo, queueEndInfo diskQueueEndInfo) error {
h := int(murmur3.Sum32([]byte(key))) % len(ss.dbShards)
d := ss.dbShards[h]
return d.PersistReader(key, fsync, confirmed, queueEndInfo)
}
func (ss *shardedDBMetaStorage) PersistWriter(key string, fsync bool, queueEndInfo diskQueueEndInfo) error {
h := int(murmur3.Sum32([]byte(key))) % len(ss.dbShards)
d := ss.dbShards[h]
return d.PersistWriter(key, fsync, queueEndInfo)
}
func (ss *shardedDBMetaStorage) RetrieveWriter(key string) (diskQueueEndInfo, error) {
h := int(murmur3.Sum32([]byte(key))) % len(ss.dbShards)
d := ss.dbShards[h]
return d.RetrieveWriter(key)
}
func (ss *shardedDBMetaStorage) Sync() {
for _, d := range ss.dbShards {
d.Sync()
}
}
func (ss *shardedDBMetaStorage) Close() {
ss.Sync()
for _, d := range ss.dbShards {
d.Close()
}
}