-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient_msg.go
More file actions
executable file
·216 lines (177 loc) · 4.69 KB
/
client_msg.go
File metadata and controls
executable file
·216 lines (177 loc) · 4.69 KB
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
package fleetdb
import (
"encoding/gob"
"fmt"
"github.com/acharapko/fleetdb/log"
"github.com/acharapko/fleetdb/kv_store"
"github.com/acharapko/fleetdb/ids"
"github.com/acharapko/fleetdb/config"
)
func init() {
gob.Register(Request{})
gob.Register(Reply{})
gob.Register(Read{})
gob.Register(ReadReply{})
gob.Register(TransactionReply{})
gob.Register(Transaction{})
gob.Register(config.Config{})
}
/***************************
* Client-Replica Messages *
***************************/
// Request is bench reqeust with http response channel
type Request struct {
Command kv_store.Command
Timestamp int64
C chan Reply
}
// Reply replies to the current request
func (r *Request) Reply(reply Reply) {
r.C <- reply
}
func (r Request) String() string {
return fmt.Sprintf("Request {cmd=%v}", r.Command)
}
// Reply includes all info that might replies to back the client for the corresponding request
type Reply struct {
Command kv_store.Command
Value kv_store.Value
Timestamp int64
Err error
}
func (r Reply) String() string {
return fmt.Sprintf("Reply {cmd=%v}", r.Command)
}
// Read can be used as a special request that directly read the value of
// key without go through replication protocol in Replica
type Read struct {
CommandID ids.CommandID
Key kv_store.Key
}
func (r Read) String() string {
return fmt.Sprintf("Read {cid=%d, key=%d}", r.CommandID, r.Key)
}
// ReadReply cid and value of reading key
type ReadReply struct {
CommandID ids.CommandID
Value kv_store.Value
}
func (r ReadReply) String() string {
return fmt.Sprintf("ReadReply {cid=%d, val=%v}", r.CommandID, r.Value)
}
// Transaction contains arbitrary number of commands in one request
type Transaction struct {
TxID ids.TXID
CommandID ids.CommandID
Commands []kv_store.Command
CmdMeta []kv_store.TxCommandMeta
ClientID ids.ID
p3started bool
Timestamp int64
c chan TransactionReply
execChan chan TxExec
}
func NewInProgressTX(TxID ids.TXID, cmds []kv_store.Command, s []int) *Transaction {
cmdMetas := make([]kv_store.TxCommandMeta, len(cmds))
for i, slot := range s {
cmdMeta := kv_store.TxCommandMeta{false, false, slot}
cmdMetas[i] = cmdMeta
}
inprogress := Transaction{
TxID: TxID,
Commands: cmds,
CmdMeta:cmdMetas,
}
inprogress.MakeExecChannel(len(cmds))
return &inprogress
}
func (t *Transaction) MakeCommittedWaitingFlags(slots []int) {
cmdMetas := make([]kv_store.TxCommandMeta, len(slots))
for i := 0; i < len(slots); i++ {
cmdMeta := kv_store.TxCommandMeta{false, false, slots[i]}
cmdMetas[i] = cmdMeta
}
t.CmdMeta = cmdMetas
}
func (t Transaction) String() string {
return fmt.Sprintf("Transaction {client id=%s, tx id=%v, cmds=%v}", t.ClientID, t.TxID, t.Commands)
}
// Reply replies to the current request
func (r *Transaction) Reply(reply TransactionReply) {
if r.c != nil {
r.c <- reply
}
}
// Reply replies to the current request
func (r *Transaction) ReadyToExec(slot int, key kv_store.Key) {
if r.execChan != nil {
r.execChan <- TxExec{Slotnum: slot, Key: key}
}
}
func (r *Transaction) MakeExecChannel(numkeys int) {
//we have a buffered channel in case we are sending to it before the consumer is created
r.execChan = make(chan TxExec, numkeys)
}
func (r *Transaction) CloseExecChannel() {
close(r.execChan)
}
func (r *Transaction) GetExecChannel() chan TxExec {
return r.execChan
}
func (tx *Transaction) P3Sent() {
tx.p3started = true
}
func (tx *Transaction) CanSendP3() bool {
return !tx.p3started
}
func (tx *Transaction) AreAllCommitted() bool {
for _, meta := range tx.CmdMeta {
if !meta.CmdCommitted {
return false
}
}
return true
}
func (tx *Transaction) MarkCommitted(key kv_store.Key) {
log.Debugf("Marking Committed: len(meta) = %d\n", len(tx.CmdMeta))
for i, cmd := range tx.Commands {
if cmd.Key.B64() == key.B64() && !tx.CmdMeta[i].CmdCommitted {
tx.CmdMeta[i].CmdCommitted = true
return
}
}
}
func (tx *Transaction) AreAllWaiting() bool {
for _, meta := range tx.CmdMeta {
if !meta.CmdWaitingExec{
return false
}
}
return true
}
func (tx *Transaction) MarkWaiting(key kv_store.Key) {
log.Debugf("Marking Waiting key %v in TX %v\n", string(key), tx.TxID)
for i, cmd := range tx.Commands {
if cmd.Key.B64() == key.B64() && !tx.CmdMeta[i].CmdWaitingExec {
tx.CmdMeta[i].CmdWaitingExec = true
return
}
}
}
// TransactionReply is the result of transaction struct
type TransactionReply struct {
OK bool
CommandID ids.CommandID
LeaderID ids.ID
ClientID ids.ID
Commands []kv_store.Command
Timestamp int64
Err error
}
type TxExec struct {
Slotnum int
Key kv_store.Key
}
func (r TxExec) String() string {
return fmt.Sprintf("TxExec {key=%v, slotnum=%d}", r.Key, r.Slotnum)
}