-
Notifications
You must be signed in to change notification settings - Fork 1
/
logreader.go
171 lines (156 loc) · 4.34 KB
/
logreader.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
package logdb
import (
"fmt"
"sync"
ipaxos "github.com/LiuzhouChan/go-paxos/internal/paxos"
"github.com/LiuzhouChan/go-paxos/internal/utils/logutil"
"github.com/LiuzhouChan/go-paxos/paxosio"
"github.com/LiuzhouChan/go-paxos/paxospb"
)
// LogReader is the struct used to manage logs that have already been persisted
// into LogDB. This implementation is influenced by CockroachDB's
// replicaRaftStorage.
type LogReader struct {
sync.Mutex
groupID uint64
nodeID uint64
logdb paxosio.ILogDB
state paxospb.State
markerInstanceID uint64
length uint64
}
// NewLogReader creates and returns a new LogReader instance.
func NewLogReader(groupID uint64, nodeID uint64, logdb paxosio.ILogDB) *LogReader {
l := &LogReader{
logdb: logdb,
groupID: groupID,
nodeID: nodeID,
length: 1,
markerInstanceID: 0,
}
return l
}
func (lr *LogReader) describe() string {
return fmt.Sprintf("logreader %s instance id %d length %d",
logutil.DescribeNode(lr.groupID, lr.nodeID), lr.markerInstanceID, lr.length)
}
//NodeState ...
func (lr *LogReader) NodeState() paxospb.State {
lr.Lock()
defer lr.Unlock()
return lr.state
}
//Entries ...
func (lr *LogReader) Entries(low, high uint64) ([]paxospb.Entry, error) {
lr.Lock()
defer lr.Unlock()
return lr.entriesLocked(low, high)
}
func (lr *LogReader) entriesLocked(low, high uint64) ([]paxospb.Entry, error) {
if low > high {
return nil, fmt.Errorf("high %d < low %d", high, low)
}
if low <= lr.markerInstanceID {
return nil, ipaxos.ErrCompacted
}
if high > lr.lastInstanceID()+1 {
plog.Errorf("low %d high %d, last instance id %d", low, high, lr.lastInstanceID())
return nil, ipaxos.ErrUnavailable
}
ents, err := lr.logdb.IterateEntries(lr.groupID, lr.nodeID, low, high)
if err != nil {
return nil, err
}
if uint64(len(ents)) == high-low {
return ents, nil
}
if len(ents) > 0 {
if ents[0].AcceptorState.InstanceID > low {
return nil, ipaxos.ErrCompacted
}
expected := ents[len(ents)-1].AcceptorState.InstanceID + 1
if lr.lastInstanceID() <= expected {
plog.Errorf("%s, %v, low %d high %d, expected %d last instance id %d",
lr.describe(), ipaxos.ErrUnavailable, low, high, expected, lr.lastInstanceID())
return nil, ipaxos.ErrUnavailable
}
return nil, fmt.Errorf("gap found between [%d:%d] at %d", low, high, expected)
}
plog.Warningf("failed to get anything from logreader")
return nil, ipaxos.ErrUnavailable
}
// GetRange returns the index range of all logs managed by the LogReader
// instance.
func (lr *LogReader) GetRange() (uint64, uint64) {
lr.Lock()
defer lr.Unlock()
return lr.firstInstanceID(), lr.lastInstanceID()
}
func (lr *LogReader) firstInstanceID() uint64 {
return lr.markerInstanceID + 1
}
func (lr *LogReader) lastInstanceID() uint64 {
return lr.markerInstanceID + lr.length - 1
}
// SetRange updates the LogReader to reflect what is available in it.
func (lr *LogReader) SetRange(firstInstanceID, length uint64) {
if length == 0 {
return
}
lr.Lock()
defer lr.Unlock()
first := lr.firstInstanceID()
last := firstInstanceID + length - 1
if last < first {
return
}
if first > firstInstanceID {
cut := first - firstInstanceID
firstInstanceID = first
length -= cut
}
offset := firstInstanceID - lr.markerInstanceID
switch {
case lr.length > offset:
lr.length = offset + length
case lr.length == offset:
lr.length = lr.length + length
default:
panic("missing log entry")
}
}
// SetState sets the persistent state.
func (lr *LogReader) SetState(s paxospb.State) {
lr.Lock()
defer lr.Unlock()
lr.state = s
}
// Append ...
func (lr *LogReader) Append(entries []paxospb.Entry) error {
if len(entries) == 0 {
return nil
}
if len(entries) > 0 {
if entries[0].AcceptorState.InstanceID+uint64(len(entries))-1 !=
entries[len(entries)-1].AcceptorState.InstanceID {
panic("gap in entries")
}
}
lr.SetRange(entries[0].AcceptorState.InstanceID, uint64(len(entries)))
return nil
}
// Compact compacts paxos log entries up to index
func (lr *LogReader) Compact(instanceID uint64) error {
lr.Lock()
defer lr.Unlock()
if instanceID < lr.markerInstanceID {
return ipaxos.ErrCompacted
}
if instanceID > lr.lastInstanceID() {
return ipaxos.ErrUnavailable
}
i := instanceID - lr.markerInstanceID
lr.length = lr.length - i
lr.markerInstanceID = instanceID
return nil
}