-
Notifications
You must be signed in to change notification settings - Fork 1
/
inmemory.go
136 lines (122 loc) · 3.77 KB
/
inmemory.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
package paxos
import (
"github.com/LiuzhouChan/go-paxos/internal/settings"
"github.com/LiuzhouChan/go-paxos/paxospb"
)
var (
entrySliceSize = settings.Soft.InMemEntrySliceSize
minEntrySliceSize = settings.Soft.MinEntrySliceFreeSize
)
type inMemory struct {
entries []paxospb.Entry
markerInstanceID uint64
saveTo uint64
}
func newInMemory(lastInstanceID uint64) inMemory {
return inMemory{
markerInstanceID: lastInstanceID + 1,
saveTo: lastInstanceID,
}
}
func (im *inMemory) checkMarkerInstanceID() {
if len(im.entries) > 0 {
if im.entries[0].AcceptorState.InstanceID != im.markerInstanceID {
plog.Panicf("marker instance id %d, first instance id %d",
im.markerInstanceID, im.entries[0].AcceptorState.InstanceID)
}
}
}
func (im *inMemory) getEntries(low uint64, high uint64) []paxospb.Entry {
upperBound := im.markerInstanceID + uint64(len(im.entries))
if low > high || low < im.markerInstanceID {
plog.Panicf("invalid low value %d, high %d, marker instanace id %d",
low, high, im.markerInstanceID)
}
if high > upperBound {
plog.Panicf("invalid high value %d, upperBound %d", high, upperBound)
}
return im.entries[low-im.markerInstanceID : high-im.markerInstanceID]
}
func (im *inMemory) getLastInstanceID() (uint64, bool) {
if len(im.entries) > 0 {
return im.entries[len(im.entries)-1].AcceptorState.InstanceID, true
}
return 0, false
}
func (im *inMemory) commitUpdate(cu paxospb.UpdateCommit) {
if cu.AppliedTo > 0 {
im.saveLogTo(cu.AppliedTo)
}
}
func (im *inMemory) entriesToSave() []paxospb.Entry {
idx := im.saveTo + 1
if idx-im.markerInstanceID > uint64(len(im.entries)) {
plog.Infof("nothing to save %+v", im)
return []paxospb.Entry{}
}
return im.entries[idx-im.markerInstanceID:]
}
func (im *inMemory) saveLogTo(instanceID uint64) {
if instanceID < im.markerInstanceID {
return
}
if len(im.entries) == 0 {
return
}
if instanceID > im.entries[len(im.entries)-1].AcceptorState.InstanceID {
return
}
im.saveTo = instanceID
}
func (im *inMemory) appliedLogTo(instanceID uint64) {
if instanceID < im.markerInstanceID {
return
}
if len(im.entries) == 0 {
return
}
if instanceID > im.entries[len(im.entries)-1].AcceptorState.InstanceID {
return
}
newMarkerInstanceID := instanceID
im.entries = im.entries[newMarkerInstanceID-im.markerInstanceID:]
im.markerInstanceID = newMarkerInstanceID
im.resizeEntrySlice()
im.checkMarkerInstanceID()
}
func (im *inMemory) resizeEntrySlice() {
if cap(im.entries)-len(im.entries) < int(minEntrySliceSize) {
old := im.entries
im.entries = make([]paxospb.Entry, 0, entrySliceSize)
im.entries = append(im.entries, old...)
}
}
func (im *inMemory) merge(ents []paxospb.Entry) {
firstInstanceID := ents[0].AcceptorState.InstanceID
im.resizeEntrySlice()
if firstInstanceID == im.markerInstanceID+uint64(len(im.entries)) {
checkEntriesToAppend(im.entries, ents)
im.entries = append(im.entries, ents...)
} else if firstInstanceID <= im.markerInstanceID {
im.markerInstanceID = firstInstanceID
im.entries = newEntrySlice(ents)
im.saveTo = firstInstanceID - 1
} else {
existing := im.getEntries(im.markerInstanceID, firstInstanceID)
checkEntriesToAppend(existing, ents)
im.entries = make([]paxospb.Entry, 0, len(existing)+len(ents))
im.entries = append(im.entries, existing...)
im.entries = append(im.entries, ents...)
im.saveTo = min(im.saveTo, firstInstanceID-1)
}
im.checkMarkerInstanceID()
}
func checkEntriesToAppend(ents []paxospb.Entry, toAppend []paxospb.Entry) {
if len(ents) == 0 || len(toAppend) == 0 {
return
}
if ents[len(ents)-1].AcceptorState.InstanceID+1 != toAppend[0].AcceptorState.InstanceID {
plog.Panicf("found a hole, last %d, first to append %d",
ents[len(ents)-1].AcceptorState.InstanceID, toAppend[0].AcceptorState.InstanceID)
}
}