forked from lni/dragonboat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
143 lines (129 loc) · 3.08 KB
/
session.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
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rsm
import (
"encoding/binary"
"encoding/json"
"io"
"github.com/lni/dragonboat/internal/utils/cache/biogo/store/llrb"
)
// RaftClientID is the type used as client id in sessions.
type RaftClientID uint64
// RaftSeriesID is the type used as series id in sessions.
type RaftSeriesID uint64
// Compare implements the llrb.Comparable interface.
func (a *RaftClientID) Compare(b llrb.Comparable) int {
bk := b.(*RaftClientID)
aval := *a
bval := *bk
switch {
case aval < bval:
return -1
case aval > bval:
return 1
default:
return 0
}
}
// Session is the session object maintained on the raft side.
type Session struct {
ClientID RaftClientID
RespondedUpTo RaftSeriesID
History map[RaftSeriesID]uint64
}
func newSession(id RaftClientID) *Session {
return &Session{
ClientID: id,
History: make(map[RaftSeriesID]uint64),
}
}
func (s *Session) getResponse(id RaftSeriesID) (uint64, bool) {
v, ok := s.History[id]
return v, ok
}
func (s *Session) addResponse(id RaftSeriesID, resp uint64) {
_, ok := s.History[id]
if !ok {
s.History[id] = resp
} else {
panic("adding a duplicated response")
}
}
func (s *Session) clearTo(to RaftSeriesID) {
if to <= s.RespondedUpTo {
return
}
if to == s.RespondedUpTo+1 {
delete(s.History, to)
s.RespondedUpTo = to
return
}
s.RespondedUpTo = to
for k := range s.History {
if k <= to {
delete(s.History, k)
}
}
}
func (s *Session) hasResponded(id RaftSeriesID) bool {
return id <= s.RespondedUpTo
}
func (s *Session) save(writer io.Writer) (uint64, error) {
data, err := json.Marshal(s)
if err != nil {
panic(err)
}
sz := len(data)
lenbuf := make([]byte, 8)
binary.LittleEndian.PutUint64(lenbuf, uint64(sz))
n, err := writer.Write(lenbuf)
if err != nil {
return 0, err
}
if n != len(lenbuf) {
return 0, io.ErrShortWrite
}
n, err = writer.Write(data)
if err != nil {
return 0, err
}
if n != len(data) {
return 0, io.ErrShortWrite
}
return uint64(len(data) + 8), nil
}
func createSessionFromSnapshot(reader io.Reader) (*Session, error) {
lenbuf := make([]byte, 8)
n, err := io.ReadFull(reader, lenbuf)
if err != nil {
return nil, err
}
if n != len(lenbuf) {
return nil, io.ErrUnexpectedEOF
}
sz := binary.LittleEndian.Uint64(lenbuf)
data := make([]byte, sz)
n, err = io.ReadFull(reader, data)
if err != nil {
return nil, err
}
if n != len(data) {
return nil, io.ErrUnexpectedEOF
}
s := newSession(0)
if err := json.Unmarshal(data, s); err != nil {
panic(err)
}
return s, nil
}