-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_client.go
168 lines (135 loc) · 3.56 KB
/
test_client.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
package kv
import (
"context"
"fmt"
"strings"
"time"
pb "github.com/SuhasHebbar/CS739-P2/proto"
"golang.org/x/exp/slog"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type TestClient struct {
LeaderId int
Clients map[int32]pb.RaftRpcClient
Config *Config
deadline time.Duration
}
func NewTestClient() *TestClient {
c := TestClient{Clients: map[int32]pb.RaftRpcClient{}}
config := GetConfig()
c.Config = &config
for k, url := range c.Config.Peers {
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
conn, err := grpc.Dial(url, opts...)
if err != nil {
slog.Error("Failed to dial", "err", err)
panic(err)
}
client := pb.NewRaftRpcClient(conn)
c.Clients[k] = client
}
c.deadline = 10 * time.Hour
return &c
}
func (c *TestClient) HandleGet(keystr string, skipQuorum bool, partitionedIds []int, isPartitionedServer bool) {
key := pb.Key{Key: keystr}
var response *pb.Response
var err error
for i := 0; i < len(c.Config.Peers); i++ {
ctx, cancel := context.WithTimeout(context.Background(), c.deadline)
defer cancel()
clientId := (c.LeaderId + i) % len(c.Config.Peers)
fmt.Println("Trying leaderId", clientId)
if skipQuorum {
response, err = c.Clients[int32(clientId)].FastGet(ctx, &key)
} else {
response, err = c.Clients[int32(clientId)].Get(ctx, &key)
}
Debugf("response %v, err %v", response, err)
if response == nil {
continue
}
if !response.IsLeader {
continue
}
c.LeaderId = clientId
break
}
if err != nil || (response != nil && !response.IsLeader) {
slog.Debug("err %v", err)
return
}
if response.Ok {
fmt.Println(response.Response)
} else {
if response.Response == NON_EXISTENT_KEY_MSG {
fmt.Println("<Value does not exist>")
} else {
fmt.Println(err)
}
}
}
func (c *TestClient) HandleSet(arguments string, partitionedIds []int, isPartitionedServer bool) {
key, value, valid := strings.Cut(arguments, " ")
if !valid {
fmt.Println("Something went wrong")
return
}
kvPair := pb.KeyValuePair{Key: key, Value: value}
var response *pb.Response
var err error
for i := 0; i < len(c.Config.Peers); i++ {
ctx, cancel := context.WithTimeout(context.Background(), c.deadline)
defer cancel()
clientId := (c.LeaderId + i) % len(c.Config.Peers)
fmt.Println("Trying leaderId", clientId)
response, err = c.Clients[int32(clientId)].Set(ctx, &kvPair)
Debugf("response %v, err %v", response, err)
if response == nil {
continue
}
if !response.IsLeader {
continue
}
c.LeaderId = clientId
break
}
if err != nil || (response != nil && !response.IsLeader) {
slog.Debug("err %v", err)
return
}
fmt.Println("OK")
}
func (c *TestClient) HandleDelete(arguments string, partitionedIds []int) {
key := pb.Key{Key: arguments}
var response *pb.Response
var err error
for i := 0; i < len(c.Config.Peers); i++ {
ctx, cancel := context.WithTimeout(context.Background(), c.deadline)
defer cancel()
clientId := (c.LeaderId + i) % len(c.Config.Peers)
fmt.Println("Trying leaderId", clientId)
response, err = c.Clients[int32(c.LeaderId)].Delete(ctx, &key)
Debugf("response %v, err %v", response, err)
if response == nil {
continue
}
if !response.IsLeader {
continue
}
c.LeaderId = clientId
break
}
if response != nil && !response.IsLeader {
slog.Debug("Not leader")
return
}
if response.Ok == true {
fmt.Printf("Deleted %v\n", arguments)
} else if response.Response == NON_EXISTENT_KEY_MSG {
fmt.Println("<Value does not exist>")
} else {
fmt.Println("Someting went wrong!")
}
}