/
logmanager.go
378 lines (326 loc) · 9.34 KB
/
logmanager.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
/*
Package gostore implements a simple single-node log-based key-value
store. It supports multiple concurrent transactions through a set of
locks on values.
*/
package gostore
import (
"flag"
"fmt"
"github.com/golang/protobuf/proto"
pb "github.com/mDibyo/gostore/pb"
"io/ioutil"
"math/rand"
"sync"
"time"
)
// Key represents a key in the store
type Key string
// Value represents the value for a key in the key store
type Value []byte
type storeMapValue struct {
value Value
// RWMutex attributes
lock sync.RWMutex
// ValueAccessor attributes
rAccessorChan chan *valueAccessor
wAccessorChan chan *valueAccessor
ping chan struct{}
}
func newStoreMapValue() *storeMapValue {
return &storeMapValue{
rAccessorChan: make(chan *valueAccessor),
wAccessorChan: make(chan *valueAccessor),
ping: make(chan struct{}),
}
}
// TransactionID is used to uniquely identify/represent a transaction.
type TransactionID int64
type storeMap map[Key]*storeMapValue
func (sm storeMap) storeMapValue(k Key, addIfNotExist bool) (smv *storeMapValue, err error) {
smv, ok := sm[k]
if ok {
return
}
if !addIfNotExist {
return smv, fmt.Errorf("key %s does not exist.", k)
}
smv = newStoreMapValue()
sm[k] = smv
return
}
type currentMutexesMap map[Key]*rwMutexWrapper
func (cm currentMutexesMap) getWrappedRWMutex(k Key, smv *storeMapValue) *rwMutexWrapper {
if rw, ok := cm[k]; ok {
return rw
}
_rw := wrapRWMutex(&smv.lock)
cm[k] = &_rw
return &_rw
}
var logFileFmt = "%012d_%012d.log"
type logManager struct {
log pb.Log // the log of transaction operations
logDir string // the directory in which log is stored
logLock sync.Mutex // lock to synchronize access to the log
nextLSN int // the LSN for the next log entry
nextLSNToFlush int // the LSN of the next log entry to be flushed
currMutexes map[TransactionID]currentMutexesMap // the mutexes held currently by running transactions
store storeMap // the master copy of the current state of the store
}
func newLogManager(ld string) (lm *logManager, err error) {
lm = &logManager{}
lm.logDir = ld
if lm.logDir == "" {
lm.logDir = "./data"
}
lm.currMutexes = make(map[TransactionID]currentMutexesMap)
lm.store = make(storeMap)
// Retrieve old logs if they exist
err = lm.retrieveLog()
// Replay log over storeMap
for _, e := range lm.log.Entry {
tid := TransactionID(*e.Tid)
switch *e.EntryType {
case pb.LogEntry_BEGIN:
lm.currMutexes[tid] = make(currentMutexesMap)
case pb.LogEntry_UPDATE:
fallthrough
case pb.LogEntry_UNDO:
lm.updateStoreMapValue(lm.currMutexes[tid], Key(*e.Key), Value(CopyByteArray(e.NewValue)))
case pb.LogEntry_COMMIT:
case pb.LogEntry_ABORT:
case pb.LogEntry_END:
for _, rw := range lm.currMutexes[tid] {
rw.unlock()
}
delete(lm.currMutexes, tid)
}
}
// Abort incomplete transactions
for tid, _ := range lm.currMutexes {
lm.abortTransaction(tid)
}
return
}
func (lm *logManager) addLogEntry(e *pb.LogEntry) {
lm.logLock.Lock()
defer lm.logLock.Unlock()
entries := &lm.log.Entry
e.Lsn = proto.Int64(int64(lm.nextLSN))
*entries = append(*entries, e)
lm.nextLSN++
}
func (lm *logManager) retrieveLog() (err error) {
files, err := ioutil.ReadDir(lm.logDir)
if err != nil {
return fmt.Errorf("could not retrieve old logs: %v", err)
}
for _, file := range files {
if !file.IsDir() {
var startLSN, endLSN = -1, -1
_, err = fmt.Sscanf(file.Name(), logFileFmt, &startLSN, &endLSN)
if err != nil {
continue
}
if startLSN != lm.nextLSN || endLSN < startLSN {
err = fmt.Errorf("log file %s was not in the expected format", file.Name())
break
}
filename := fmt.Sprintf("%s/%s", lm.logDir, file.Name())
data, err := ioutil.ReadFile(filename)
if err != nil {
err = fmt.Errorf("could not read log file %s: %v", filename, err)
break
}
if err = proto.UnmarshalMerge(data, &lm.log); err != nil {
err = fmt.Errorf("could not unmarshal log file %s: %v", filename, err)
break
}
lm.nextLSN = len(lm.log.Entry)
if nextLSN := endLSN + 1; nextLSN != lm.nextLSN {
err = fmt.Errorf("log file %s did not have the right number of entries", filename)
break
}
}
}
lm.nextLSNToFlush = lm.nextLSN
return err
}
func (lm *logManager) flushLog() error {
lm.logLock.Lock()
defer lm.logLock.Unlock()
entries := lm.log.GetEntry()
logToFlush := &pb.Log{
Entry: entries[lm.nextLSNToFlush:],
}
data, err := proto.Marshal(logToFlush)
if err != nil {
return fmt.Errorf("error while marshalling log to be flushed: %v", err)
}
filename := fmt.Sprintf(logFileFmt, lm.nextLSNToFlush, lm.nextLSN-1)
if err := ioutil.WriteFile(fmt.Sprintf("%s/%s", lm.logDir, filename), data, 0644); err != nil {
return fmt.Errorf("error while writing out log: %v", err)
}
lm.nextLSNToFlush = lm.nextLSN
return nil
}
func (lm *logManager) nextTransactionID() TransactionID {
return TransactionID(rand.Int63())
}
func (lm *logManager) beginTransaction(tid TransactionID) {
lm.currMutexes[tid] = make(currentMutexesMap)
lm.addLogEntry(&pb.LogEntry{
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_BEGIN.Enum(),
})
}
func (lm *logManager) getValue(tid TransactionID, k Key) (Value, error) {
cm, ok := lm.currMutexes[tid]
if !ok {
return nil, fmt.Errorf("transaction with ID %d is not currently running", tid)
}
smv, err := lm.store.storeMapValue(k, false)
if err != nil {
return nil, fmt.Errorf("could not retrieve value: %v", err)
}
rw := cm.getWrappedRWMutex(k, smv)
rw.rLock()
return smv.value, nil
}
func (lm *logManager) updateStoreMapValue(cm currentMutexesMap, k Key, v Value) (oldValue, newValue []byte, err error) {
smv, err := lm.store.storeMapValue(k, true)
if err != nil {
return nil, nil, fmt.Errorf("could not retrieve value: %v", err)
}
rw := cm.getWrappedRWMutex(k, smv)
rw.wLock()
if smv.value != nil {
oldValue = CopyByteArray(smv.value)
}
if v != nil {
smv.value = v
newValue = CopyByteArray(v)
} else {
delete(lm.store, k)
}
return
}
func (lm *logManager) updateValue(tid TransactionID, k Key, v Value) error {
cm, ok := lm.currMutexes[tid]
if !ok {
return fmt.Errorf("transaction with ID %d is not currently running.", tid)
}
oldValue, newValue, err := lm.updateStoreMapValue(cm, k, v)
if err != nil {
return err
}
// Write log entry
lm.addLogEntry(&pb.LogEntry{
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_UPDATE.Enum(),
Key: proto.String(string(k)),
OldValue: oldValue,
NewValue: newValue,
})
return nil
}
func (lm *logManager) setValue(tid TransactionID, k Key, v Value) error {
if v == nil {
return fmt.Errorf("value is nil.")
}
return lm.updateValue(tid, k, v)
}
func (lm *logManager) deleteValue(tid TransactionID, k Key) error {
_, err := lm.store.storeMapValue(k, false)
if err != nil {
return err
}
return lm.updateValue(tid, k, nil)
}
func (lm *logManager) commitTransaction(tid TransactionID) error {
cm, ok := lm.currMutexes[tid]
if !ok {
return fmt.Errorf("transaction with ID %d is not currently running", tid)
}
// Write out COMMIT and END log entries
lm.addLogEntry(&pb.LogEntry{
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_COMMIT.Enum(),
})
lm.addLogEntry(&pb.LogEntry{
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_END.Enum(),
})
// Flush out log
if err := lm.flushLog(); err != nil {
return fmt.Errorf("error while flushing log: %v", err)
}
// Release all locks and remove from current transactions
for _, rw := range cm {
rw.unlock()
}
delete(lm.currMutexes, tid)
return nil
}
func (lm *logManager) abortTransaction(tid TransactionID) (err error) {
cm, ok := lm.currMutexes[tid]
if !ok {
err = fmt.Errorf("transaction with ID %d is not currently running", tid)
return
}
// Write out ABORT entry
lm.addLogEntry(&pb.LogEntry{
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_ABORT.Enum(),
})
// Undo updates (and write log entries)
entries := &lm.log.Entry
iterateEntries := (*entries)[:]
iterate:
for i := len(iterateEntries) - 1; i >= 0; i-- {
e := iterateEntries[i]
if *e.Tid == int64(tid) {
switch *e.EntryType {
case pb.LogEntry_UPDATE: // Undo UPDATE records
oldValue, newValue, err := lm.updateStoreMapValue(cm, Key(*e.Key), Value(e.OldValue))
if err != nil {
return err
}
lm.addLogEntry(&pb.LogEntry{
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_UNDO.Enum(),
Key: e.Key,
OldValue: oldValue, // e.NewValue
NewValue: newValue, // e.OldValue
UndoLsn: e.Lsn,
})
case pb.LogEntry_BEGIN: // Stop when BEGIN record is reached
break iterate
}
}
}
lm.addLogEntry(&pb.LogEntry{
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_END.Enum(),
})
// Flush out log
lm.flushLog()
// Release all locks and remove from current transactions
for _, rw := range cm {
rw.unlock()
}
delete(lm.currMutexes, tid)
return
}
var lmInstance logManager
func init() {
rand.Seed(time.Now().UnixNano())
logDir := flag.String("logDir", "", "the directory in which log files will be stored")
flag.Parse()
if lmInstancePtr, err := newLogManager(*logDir); err != nil {
panic(err)
} else {
lmInstance = *lmInstancePtr
}
}