-
Notifications
You must be signed in to change notification settings - Fork 0
/
logmanager_test.go
447 lines (422 loc) · 13.5 KB
/
logmanager_test.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package gostore
import (
"bytes"
"fmt"
"github.com/golang/protobuf/proto"
pb "github.com/mDibyo/gostore/pb"
"io/ioutil"
"os"
"reflect"
"testing"
)
// Variables and functions used in tests
var (
// Store keys and values
sampleKey1 = Key("key_1")
sampleKey2 = Key("key_2")
sampleKey3 = Key("key_3")
sampleKey4 = Key("key_4")
sampleKey5 = Key("key_5")
sampleValue1 = Value([]byte{1, 2, 3, 4, 5})
sampleValue2 = Value([]byte{2, 3, 4, 5, 6})
sampleValue3 = Value([]byte{3, 4, 5, 6, 7})
// logManager variables and functions
testLogDir string
newLogManagerForTest func(*testing.T) *logManager
)
func init() {
var err error
testLogDir, err = ioutil.TempDir("", "gostore_logs_")
if err != nil {
panic(fmt.Errorf("could not create temporary directory for tests: %v", err))
}
newLogManagerForTest = func(t *testing.T) *logManager {
lm, err := newLogManager(testLogDir)
if err != nil {
t.Fatalf("could not create log manager instance: %v", err)
}
return lm
}
}
func TestMain(m *testing.M) {
errcode := m.Run()
os.RemoveAll(testLogDir)
os.Exit(errcode)
}
func testLogEntry(t *testing.T, gotEntry, wantEntry *pb.LogEntry) {
if !reflect.DeepEqual(gotEntry, wantEntry) {
t.Errorf("did not get the expected log entry. expected=(%+v), actual=(%+v)", wantEntry, gotEntry)
}
}
func TestAddLogEntry(t *testing.T) {
nextLSN := 5
tests := []struct {
tid int64
entryType pb.LogEntry_LogEntryType
wantLenLogAfter int
wantNextLSNAfter int
}{
{
tid: 123,
entryType: pb.LogEntry_BEGIN,
wantLenLogAfter: 1,
wantNextLSNAfter: nextLSN + 1,
},
{
tid: 121,
entryType: pb.LogEntry_END,
wantLenLogAfter: 2,
wantNextLSNAfter: nextLSN + 2,
},
}
lm := *newLogManagerForTest(t)
lm.nextLSN = nextLSN
for _, test := range tests {
lm.addLogEntry(&pb.LogEntry{
Tid: proto.Int64(test.tid),
EntryType: test.entryType.Enum(),
})
if gotLenLogAfter := len(lm.log.Entry); gotLenLogAfter != test.wantLenLogAfter {
t.Errorf("did not get expected log length. expected=%d, actual=%d", test.wantLenLogAfter, gotLenLogAfter)
}
if gotNextLSNAfter := lm.nextLSN; gotNextLSNAfter != test.wantNextLSNAfter {
t.Errorf("did not get expected next LSN. expected=%d, actual=%d", test.wantNextLSNAfter, gotNextLSNAfter)
}
}
}
func TestBeginTransaction(t *testing.T) {
lm := *newLogManagerForTest(t)
tid := lm.nextTransactionID()
lm.beginTransaction(tid)
wantLogEntry := &pb.LogEntry{
Lsn: proto.Int64(0),
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_BEGIN.Enum(),
}
// Check log
if gotLenLogAfter := len(lm.log.Entry); gotLenLogAfter != 1 {
t.Errorf("did not get expected log length. expected=%d, actual=%d", 1, gotLenLogAfter)
}
testLogEntry(t, lm.log.Entry[0], wantLogEntry)
// Check currMutexes
if _, ok := lm.currMutexes[tid]; !ok {
t.Errorf("did not find TransactionID %d in current mutexes map as expected", tid)
}
}
func TestGetValue(t *testing.T) {
lm := *newLogManagerForTest(t)
smv := newStoreMapValue()
smv.value = CopyByteArray(sampleValue1)
lm.store[sampleKey1] = smv
tid := lm.nextTransactionID()
lm.beginTransaction(tid)
wantLenLogAfter := len(lm.log.Entry)
// Check value
if gotV, err := lm.getValue(tid, sampleKey1); err != nil {
t.Errorf("got an error while trying to get value: %v", err)
} else if !bytes.Equal(gotV, sampleValue1) {
t.Errorf("did not get back the correct value. expected=%v, actual=%v.", sampleValue1, gotV)
}
if _, err := lm.getValue(tid, sampleKey2); err == nil {
t.Errorf("did not get an error while trying to get value for non-existent key", err)
}
// Check log
if gotLenLogAfter := len(lm.log.Entry); gotLenLogAfter != wantLenLogAfter {
t.Errorf("did not get expected log length. expected=%d, actual=%d.", wantLenLogAfter, gotLenLogAfter)
}
// Check currMutexes
if cm, ok := lm.currMutexes[tid]; !ok {
t.Error("did not find transaction in current mutexes map as expected.")
} else if rw, ok := cm[sampleKey1]; !ok {
t.Error("did not find mutex for key in mutex map for transaction.")
} else if !rw.rLocked() {
t.Errorf("found that mutex for key was not read locked. mutex: %+v", rw)
}
}
func TestSetValue(t *testing.T) {
tests := []struct {
key Key
value Value
wantError bool
wantLogEntry *pb.LogEntry
}{
{ // Add new key
key: sampleKey1,
value: CopyByteArray(sampleValue1),
wantLogEntry: &pb.LogEntry{
EntryType: pb.LogEntry_UPDATE.Enum(),
Key: proto.String(string(sampleKey1)),
NewValue: CopyByteArray(sampleValue1),
},
},
{ // Change value for existing key
key: sampleKey2,
value: CopyByteArray(sampleValue2),
wantLogEntry: &pb.LogEntry{
EntryType: pb.LogEntry_UPDATE.Enum(),
Key: proto.String(string(sampleKey2)),
OldValue: CopyByteArray(sampleValue3),
NewValue: CopyByteArray(sampleValue2),
},
},
{
key: sampleKey3,
wantError: true,
},
}
lm := newLogManagerForTest(t)
smv := newStoreMapValue()
smv.value = CopyByteArray(sampleValue3)
lm.store[sampleKey2] = smv
for _, test := range tests {
tid := lm.nextTransactionID()
lm.beginTransaction(tid)
lenLogBefore := len(lm.log.Entry)
// Check setting
err := lm.setValue(tid, test.key, test.value)
if test.wantError {
if err == nil {
t.Errorf("did not get expected error for (key='%s', value=%v) while trying to set value.", test.key, test.value)
}
continue
} else {
if err != nil {
t.Errorf("got an unexpected error for (key='%s', value=%v) while trying to set value: %v.", test.key, test.value, err)
}
}
// Check storeMap
if gotSMV, ok := lm.store[test.key]; !ok {
t.Errorf("did not find value for key='%s' in storeMap.", test.key)
} else if !bytes.Equal(gotSMV.value, test.value) {
t.Errorf("did not get back the correct value. key='%s', expected=%v, actual=%v.", test.key, test.value, gotSMV.value)
}
// Check log
wantLogLenAfter := lenLogBefore + 1
gotLenLogAfter := len(lm.log.Entry)
if gotLenLogAfter != wantLogLenAfter {
t.Errorf("did not get expected log length. expected=%d, actual=%d.", wantLogLenAfter, gotLenLogAfter)
}
gotEntry := lm.log.Entry[gotLenLogAfter-1]
wantEntry := test.wantLogEntry
wantEntry.Lsn = gotEntry.Lsn
wantEntry.Tid = proto.Int64(int64(tid))
testLogEntry(t, gotEntry, wantEntry)
// Check currMutexes
if cm, ok := lm.currMutexes[tid]; !ok {
t.Error("did not find transaction in current mutexes map as expected.")
} else if rw, ok := cm[test.key]; !ok {
t.Errorf("did not find mutex for key='%s' in mutex map for transaction.", test.key)
} else if !rw.wLocked() {
t.Errorf("found that mutex for key='%s' was not write locked. mutex: %+v", test.key, rw)
}
}
}
func TestDeleteValue(t *testing.T) {
lm := newLogManagerForTest(t)
smv := newStoreMapValue()
smv.value = CopyByteArray(sampleValue1)
lm.store[sampleKey1] = smv
tid := lm.nextTransactionID()
lm.beginTransaction(tid)
lenLogBefore := len(lm.log.Entry)
// Check delete operation
if err := lm.deleteValue(tid, sampleKey1); err != nil {
t.Errorf("got an error while trying to delete value: %v", err)
}
if err := lm.deleteValue(tid, sampleKey2); err == nil {
t.Errorf("did not get expected error when deleting non-existant key")
}
// Check storeMap
if _, ok := lm.store[sampleKey1]; ok {
t.Errorf("found value for key after deletion in storeMap.", sampleKey1)
}
// Check log
wantLenLogAfter := lenLogBefore + 1
gotLenLogAfter := len(lm.log.Entry)
if gotLenLogAfter != wantLenLogAfter {
t.Errorf("did not get expected log length. expected=%d, actual=%d.", wantLenLogAfter, gotLenLogAfter)
}
gotEntry := lm.log.Entry[gotLenLogAfter-1]
wantEntry := &pb.LogEntry{
Lsn: gotEntry.Lsn,
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_UPDATE.Enum(),
Key: proto.String(string(sampleKey1)),
OldValue: CopyByteArray(sampleValue1),
}
testLogEntry(t, gotEntry, wantEntry)
// Check currMutexes
if cm, ok := lm.currMutexes[tid]; !ok {
t.Error("did not find transaction in current mutexes map as expected.")
} else if rw, ok := cm[sampleKey1]; !ok {
t.Error("did not find mutex for key in mutex map for transaction.")
} else if !rw.wLocked() {
t.Errorf("found that mutex was not write locked. mutex: %+v", rw)
}
}
func TestCommitTransaction(t *testing.T) {
tests := []struct {
key Key
value Value
wantNumEntries int
}{
{ // No operations
wantNumEntries: 3,
},
{ // Get operation
key: sampleKey4,
wantNumEntries: 3,
},
{ // Set operation
key: sampleKey5,
value: CopyByteArray(sampleValue2),
wantNumEntries: 4,
},
}
lm := newLogManagerForTest(t)
smv := newStoreMapValue()
smv.value = CopyByteArray(sampleValue1)
lm.store[sampleKey4] = smv
for _, test := range tests {
lenLogBefore := len(lm.log.Entry)
tid := lm.nextTransactionID()
lm.beginTransaction(tid)
if test.key != "" {
var err error
if test.value != nil {
err = lm.setValue(tid, test.key, test.value)
} else {
_, err = lm.getValue(tid, test.key)
}
if err != nil {
t.Errorf("got an error while getting/setting value for key='%s': %v", test.key, err)
}
}
// Check commit operation
if err := lm.commitTransaction(tid); err != nil {
t.Errorf("got an error while trying to commit transaction: %v", err)
}
// Check log
wantLenLogAfter := lenLogBefore + test.wantNumEntries
gotLenLogAfter := len(lm.log.Entry)
if gotLenLogAfter != wantLenLogAfter {
t.Errorf("did not get expected log length. expected=%d, actual=%d.", wantLenLogAfter, gotLenLogAfter)
}
gotLogEntry := lm.log.Entry[gotLenLogAfter-2]
wantLogEntry := &pb.LogEntry{
Lsn: gotLogEntry.Lsn,
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_COMMIT.Enum(),
}
testLogEntry(t, gotLogEntry, wantLogEntry)
gotLogEntry = lm.log.Entry[gotLenLogAfter-1]
wantLogEntry = &pb.LogEntry{
Lsn: gotLogEntry.Lsn,
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_END.Enum(),
}
testLogEntry(t, gotLogEntry, wantLogEntry)
if lm.nextLSNToFlush != lm.nextLSN {
t.Error("found that log was not flushed.")
}
// Check currMutexes
if _, ok := lm.currMutexes[tid]; ok {
t.Error("found transaction in current mutexes map.")
}
}
}
func TestAbortTransaction(t *testing.T) {
lm := newLogManagerForTest(t)
resetLogManager := func() {
smv := newStoreMapValue()
smv.value = CopyByteArray(sampleValue1)
lm.store[sampleKey1] = smv
}
checkCommon := func(tid TransactionID, wantLenLogAfter, numUndoRecords int) {
// Check abort operation
if err := lm.abortTransaction(tid); err != nil {
t.Errorf("got an error while trying to abort transaction: %v", err)
}
// Check log
gotLenLogAfter := len(lm.log.Entry)
if gotLenLogAfter != wantLenLogAfter {
t.Errorf("did not get expected log length. expected=%d, actual=%d.", wantLenLogAfter, gotLenLogAfter)
}
gotLogEntry := lm.log.Entry[gotLenLogAfter-2-numUndoRecords]
wantLogEntry := &pb.LogEntry{
Lsn: gotLogEntry.Lsn,
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_ABORT.Enum(),
}
testLogEntry(t, gotLogEntry, wantLogEntry)
gotLogEntry = lm.log.Entry[gotLenLogAfter-1]
wantLogEntry = &pb.LogEntry{
Lsn: gotLogEntry.Lsn,
Tid: proto.Int64(int64(tid)),
EntryType: pb.LogEntry_END.Enum(),
}
testLogEntry(t, gotLogEntry, wantLogEntry)
if lm.nextLSNToFlush != lm.nextLSN {
t.Error("found that log was not flushed.")
}
// Check currMutexes
if _, ok := lm.currMutexes[tid]; ok {
t.Error("found transaction in current mutexes map.")
}
}
checkStoreMapKey := func(k Key, v Value) {
gotSMV, ok := lm.store[k]
if v != nil { // key should exist
if !ok {
t.Errorf("did not find value for key='%s' in storeMap.", k)
} else if !bytes.Equal(gotSMV.value, v) {
t.Errorf("did not get back the correct value. key='%s', expected=%v, actual=%v.", k, v, gotSMV.value)
}
} else { // key should not exist
if ok {
t.Errorf("found value for key='%s' in storeMap: %v", k, gotSMV.value)
}
}
}
// No operations
resetLogManager()
lenLogBefore := len(lm.log.Entry)
tid := lm.nextTransactionID()
lm.beginTransaction(tid)
if _, err := lm.getValue(tid, sampleKey1); err != nil {
t.Errorf("got an error while getting value for key='%s': %v", sampleKey1, err)
}
checkCommon(tid, lenLogBefore+3, 0)
checkStoreMapKey(sampleKey1, sampleValue1)
// Set operation with existing key
resetLogManager()
lenLogBefore = len(lm.log.Entry)
tid = lm.nextTransactionID()
lm.beginTransaction(tid)
if err := lm.setValue(tid, sampleKey1, CopyByteArray(sampleValue2)); err != nil {
t.Errorf("got an error while setting value for key='%s': %v", sampleKey1, err)
}
checkCommon(tid, lenLogBefore+5, 1)
checkStoreMapKey(sampleKey1, sampleValue1)
// Set operation with new key
resetLogManager()
lenLogBefore = len(lm.log.Entry)
tid = lm.nextTransactionID()
lm.beginTransaction(tid)
if err := lm.setValue(tid, sampleKey2, CopyByteArray(sampleValue3)); err != nil {
t.Errorf("got an error while setting value for key='%s': %v", sampleKey2, err)
}
checkCommon(tid, lenLogBefore+5, 1)
checkStoreMapKey(sampleKey1, sampleValue1)
checkStoreMapKey(sampleKey2, nil)
// Delete operation (with existing key)
resetLogManager()
lenLogBefore = len(lm.log.Entry)
tid = lm.nextTransactionID()
lm.beginTransaction(tid)
if err := lm.deleteValue(tid, sampleKey1); err != nil {
t.Errorf("got an error while deleting key='%s': %v", sampleKey1, err)
}
checkCommon(tid, lenLogBefore+5, 1)
checkStoreMapKey(sampleKey1, sampleValue1)
}