-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.go
157 lines (130 loc) · 2.94 KB
/
structs.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
package pegasus
import (
"log"
"os"
"strconv"
"sync"
"time"
"6.824/labrpc"
"6.824/raft"
)
const (
LEADER_WAIT = raft.HB_WAIT_MIN // wait for these many ms before requerying for a new leader.
PERIODIC_GET_WAIT = 500
FAKE_CLIENT_ID = -111
FAKE_REQUEST_ID = -112
)
const (
OK = "OK"
ErrNoKey = "ErrNoKey"
ErrWrongLeader = "ErrWrongLeader"
ErrNotFinishedYet = "ErrNotFinishedYet"
ErrLogOverwritten = "ErrLogOverwritten"
)
type Clerk struct {
servers []*labrpc.ClientEnd
pegasusServers []*KVServer
currentLeader int
client_id int64
}
type KVServer struct {
mu sync.Mutex
me int
rf *raft.Raft
applyCh chan raft.ApplyMsg
dead int32 // set by Kill()
maxraftstate int // snapshot if log grows this big
counter int
stateMachine map[string]string
consumerCond sync.Cond
producerCond sync.Cond
consumed bool
lastAppliedIndex int
lastAppliedId int64
lastAppliedKeyValue PegasusCommand
requests map[int64]*Request // map from client_id to request_id
duplicate map[int64]bool
}
type Op string
const (
GetVal Op = "GetVal"
PutVal Op = "PutVal"
AppendVal Op = "AppendVal"
)
type Topic string
const (
CK_SETUP Topic = "CK_SETUP"
CK_UPDATE_LEADER Topic = "CK_UPDATE_LEADER"
CK_GETPUTAPPEND Topic = "CK_GETPUTAPPEND"
CK_PER_GET Topic = "CK_PER_GET"
KV_SETUP Topic = "KV_SETUP"
KV_APPLYCH Topic = "KV_APPLYCH"
KV_GET Topic = "KV_GET"
KV_PUTAPPEND Topic = "KV_PUTAPPEND"
)
type Err string
type OpArgs struct {
Key string
Value string
Op Op
RequestId int64
ClientId int64
}
type OpReply struct {
Err Err
Value string // used if Op is get
}
type FindLeaderArgs struct{}
type FindLeaderReply struct {
IsLeader bool
}
type PegasusCommand struct {
RequestId int64
Op Op
Key string
Value string
}
type Request struct {
Id int64
Result Result
Index int
}
type Result struct {
isFinished bool
Value string
}
// Returns the level of verbosity from stdargs.
func getVerbosity() int {
v := os.Getenv("PEGASUS_VERBOSE")
level := 0
if v != "" {
var err error
level, err = strconv.Atoi(v)
if err != nil {
log.Fatalf("Invalid verbosity %v", v)
}
}
return level
}
var debugStart time.Time
var debugVerbosity int
// Sets format for the default logger.
func init() {
debugVerbosity = getVerbosity()
debugStart = time.Now()
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
}
// Logs a message with a specific topic.
func (ck *Clerk) logMsg(topic Topic, msg string) {
if debugVerbosity >= 1 {
time := time.Since(debugStart).Milliseconds()
log.Printf("%v %v %v %v\n", time, topic, ck.client_id, msg)
}
}
// Logs a message with a specific topic.
func (kv *KVServer) logMsg(topic Topic, msg string) {
if debugVerbosity >= 1 {
time := time.Since(debugStart).Milliseconds()
log.Printf("%v %v K%v %v\n", time, topic, kv.me, msg)
}
}