-
Notifications
You must be signed in to change notification settings - Fork 2
/
diskqueue.go
365 lines (298 loc) · 7.52 KB
/
diskqueue.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
package diskqueue
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"io"
"log"
"math/rand"
"os"
"path"
"sync"
"sync/atomic"
)
//Diskqueue A Disk queue structure
type Diskqueue struct {
//Inherite from sync RWMutex
sync.RWMutex
//Initialize parameter
name string
filePath string
//meata data related
depth int64
readFileNum int64
writeFileNum int64
//File and Buffer operation
readFile *os.File
writeFile *os.File
reader *bufio.Reader
writeBuf bytes.Buffer
//working channel
readChan chan []byte
writeChan chan []byte
writeResponseChan chan error
emptyChan chan int
emptyResponseChan chan error
exitChan chan int
exitSyncChan chan int
}
// WorkQueue :Interface of Diskqueue
type WorkQueue interface {
Put([]byte) error
ReadChan() chan []byte
Close() error
//Delete() error
Depth() int64
Empty() error
}
// NewDiskqueue :A new a instance of diskqueue to retrive data
func NewDiskqueue(name string, path string) WorkQueue {
dq := Diskqueue{
name: name,
filePath: path,
readChan: make(chan []byte),
writeChan: make(chan []byte),
writeResponseChan: make(chan error),
emptyChan: make(chan int),
emptyResponseChan: make(chan error),
exitChan: make(chan int),
exitSyncChan: make(chan int),
}
err := dq.readMetaDataFile()
if err != nil && !os.IsNotExist(err) {
//if error is meta file not exist, log it down
log.Println("err on init read meta data file")
}
err = dq.writeMetaDataFile()
if err != nil {
log.Println("err on init write meta data file")
}
//Start to working loop
go dq.inLoop()
return &dq
}
// Empty :Cleanup queue and files
func (d *Diskqueue) Empty() error {
d.RLock()
defer d.RUnlock()
d.emptyChan <- 1
return <-d.emptyResponseChan
}
// Depth :Return the depth from this queue (return total length this queue, include all data in files)
func (d *Diskqueue) Depth() int64 {
return atomic.LoadInt64(&d.depth)
}
// Close :this diskQueue
func (d *Diskqueue) Close() error {
d.exitChan <- 1
return nil
}
// Put :data to diskqueue
func (d *Diskqueue) Put(data []byte) error {
log.Println("Enter put data:", string(data))
d.RLock()
defer d.RUnlock()
log.Println("put after lock")
d.writeChan <- data
return <-d.writeResponseChan
}
// ReadChan : Read data from disk queue
// this is expected to be an *unbuffered* channel
func (d *Diskqueue) ReadChan() chan []byte {
return d.readChan
}
func (d *Diskqueue) metaDataFileName() string {
return fmt.Sprintf(path.Join(d.filePath, "%s.diskqueue.meta.dat"), d.name)
}
func (d *Diskqueue) fileName(filenum int64) string {
return fmt.Sprintf(path.Join(d.filePath, "%s.diskqueue.%06d.dat"), d.name, filenum)
}
func (d *Diskqueue) writeMetaDataFile() error {
var f *os.File
var err error
metaFile := d.metaDataFileName()
tmpFile := fmt.Sprintf("%s.%d.tmp", d.filePath, rand.Int())
log.Println("temp meta file:", tmpFile)
//Opeb temp meta tata file to write first, avoid any conflict
f, err = os.OpenFile(tmpFile, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
_, err = fmt.Fprintf(f, "%d\n%d\n%d\n",
atomic.LoadInt64(&d.depth),
d.readFileNum,
d.writeFileNum)
if err != nil {
f.Close()
return err
}
f.Sync()
f.Close()
//Done, replace to real meta data file
return os.Rename(tmpFile, metaFile)
}
func (d *Diskqueue) readMetaDataFile() error {
var f *os.File
var err error
metaFile := d.metaDataFileName()
f, err = os.OpenFile(metaFile, os.O_RDONLY, 0600)
if err != nil {
f.Close()
return err
}
var depth int64
_, err = fmt.Fscanf(f, "%d\n%d\n%d\n",
&depth,
&d.readFileNum,
&d.writeFileNum)
atomic.StoreInt64(&d.depth, depth)
return nil
}
// readDataFromFile perform low level way to retrieval data from file
func (d *Diskqueue) readDataFromFile() ([]byte, error) {
var err error
var dataLength int32
log.Println("read file:", d.fileName(d.readFileNum))
if d.readFile == nil {
//never open file before
d.readFile, err = os.OpenFile(d.fileName(d.readFileNum), os.O_RDONLY, 0600)
if err != nil {
log.Println("Open read file err")
return nil, err
}
d.reader = bufio.NewReader(d.readFile)
}
//TODO handle seek and size control
err = binary.Read(d.reader, binary.BigEndian, &dataLength)
if err != nil {
log.Println("Read read file binary err")
d.readFile.Close()
d.readFile = nil
return nil, err
}
log.Println("Read find data size:", dataLength)
readBuf := make([]byte, dataLength)
_, err = io.ReadFull(d.reader, readBuf)
if err != nil {
log.Println("Read read to buffer error")
d.readFile.Close()
d.readFile = nil
return nil, err
}
//TODO. check message size
d.readFile.Close()
d.readFile = nil
return readBuf, nil
}
// writeDataToFile perform low level data write to file using buffer
func (d *Diskqueue) writeDataToFile(data []byte) error {
var err error
if d.writeFile == nil {
//never open file before
d.writeFile, err = os.OpenFile(d.fileName(d.writeFileNum), os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
log.Println("write data file err:", err)
return err
}
}
log.Println("Write data:", string(data), " to file:", d.fileName(d.writeFileNum))
dataLength := int32(len(data))
//TODO. check message size
d.writeBuf.Reset()
err = binary.Write(&d.writeBuf, binary.BigEndian, dataLength)
if err != nil {
return err
}
_, err = d.writeBuf.Write(data)
if err != nil {
return err
}
log.Println("Write buf:", d.writeBuf, " data:", string(d.writeBuf.Bytes()), " ori:", string(data))
_, err = d.writeFile.Write(d.writeBuf.Bytes())
if err != nil {
d.writeFile.Close()
d.writeFile = nil
return err
}
log.Println("Write data to buffer done")
//TODO handle seek and size control
d.writeFile.Close()
d.writeFile = nil
d.writeFileNum++
return nil
}
func (d *Diskqueue) moveReaderForward() {
os.Remove(d.fileName(d.readFileNum))
d.readFileNum++
d.writeMetaDataFile()
}
func (d *Diskqueue) removeAllFiles() error {
os.RemoveAll(d.metaDataFileName())
return os.RemoveAll(d.filePath)
}
func (d *Diskqueue) sync() error {
if d.writeFile != nil {
err := d.writeFile.Sync()
if err != nil {
d.writeFile.Close()
d.writeFile = nil
return err
}
}
return d.writeMetaDataFile()
}
// Major working thread to handle concurrency
func (d *Diskqueue) inLoop() {
var err error
var dataRead []byte //data for readChan
var count int64 //To store total data write
var readDataChan chan []byte
for {
var fileSync bool
if count > 5 {
count = 0
fileSync = true
}
if fileSync {
err = d.sync()
if err != nil {
log.Println("File sync error:", err)
}
}
//When first data put the read can work normally, otherwise keep loop
if d.readFileNum < d.writeFileNum {
dataRead, err = d.readDataFromFile()
if err != nil {
//TODO handle read error
log.Panic("Read err:", err)
continue
} else {
log.Println("Got read data:", dataRead)
}
readDataChan = d.readChan
} else {
readDataChan = nil
}
select {
case <-d.emptyChan:
// Handle empty command thread
d.emptyResponseChan <- d.removeAllFiles()
case readDataChan <- dataRead:
// Handle read case
// the Go channel spec dictates that nil channel operations (read or write)
// in a select are skipped, we set r to d.readChan only when there is data to read
d.moveReaderForward()
log.Println("Read move next..")
case dataWrite := <-d.writeChan:
// Handle write case
count++
d.writeResponseChan <- d.writeDataToFile(dataWrite)
case <-d.exitChan:
// Handle exist case
goto exit
}
}
exit:
d.exitSyncChan <- 1
}