forked from acharapko/fleetdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
executable file
·312 lines (274 loc) · 7.58 KB
/
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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package fleetdb
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"strconv"
"sync"
"time"
"github.com/acharapko/fleetdb/log"
"github.com/acharapko/fleetdb/kv_store"
"github.com/acharapko/fleetdb/ids"
"github.com/acharapko/fleetdb/config"
)
// Client main access point of bench lib
type Client struct {
ID ids.ID // client id use the same id as servers in local site
N int
addrs map[ids.ID]string
http map[ids.ID]string
cid ids.CommandID
txNum int
tempCmds []kv_store.Command
results map[ids.CommandID]bool
sync.RWMutex
sync.WaitGroup
}
// NewClient creates a new Client from config
func NewClient() *Client {
config := config.Instance
ids.GetID()
fmt.Printf("Starting Client %v\n", ids.GetID())
c := new(Client)
c.ID = ids.GetID()
c.N = len(config.Addrs)
c.addrs = config.Addrs
c.http = config.HTTPAddrs
c.txNum = 0
c.results = make(map[ids.CommandID]bool, config.BufferSize)
return c
}
func (c *Client) getNodeID(key kv_store.Key) ids.ID {
//TODO: select random node in the zone
id := ids.NewID(c.ID.Zone(), 1)
return id
}
// RESTGet access server's REST API with url = http://ip:port/key
func (c *Client) RESTGet(key kv_store.Key, table string) kv_store.Value {
c.cid++
id := c.getNodeID(key)
url := c.http[id] + "/" + table + "/" + string(key)
log.Debugf("RESTGET %s cid=%d, url=%s\n", string(key), c.cid, url)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Errorln(err)
return nil
}
req.Header.Set("id", fmt.Sprintf("%v", c.ID))
req.Header.Set("cid", strconv.FormatUint(uint64(c.cid), 10))
req.Header.Set("timestamp", strconv.FormatInt(time.Now().UnixNano(), 10))
rep, err := http.DefaultClient.Do(req)
if err != nil {
log.Errorln(err)
return nil
}
defer rep.Body.Close()
if rep.StatusCode == http.StatusOK {
b, _ := ioutil.ReadAll(rep.Body)
log.Debugf("type=%s key=%v value=%x", "get", string(key), kv_store.Value(b))
return kv_store.Value(b)
}
dump, _ := httputil.DumpResponse(rep, true)
log.Debugf("%q", dump)
return nil
}
// RESTDelete access server's REST API with url = http://ip:port/key
func (c *Client) RESTDelete(key kv_store.Key, table string) bool {
c.cid++
id := c.getNodeID(key)
url := c.http[id] + "/" + table + "/" + string(key)
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
log.Errorln(err)
return false
}
req.Header.Set("id", fmt.Sprintf("%v", c.ID))
req.Header.Set("cid", strconv.FormatUint(uint64(c.cid), 10))
req.Header.Set("timestamp", strconv.FormatInt(time.Now().UnixNano(), 10))
rep, err := http.DefaultClient.Do(req)
if err != nil {
log.Errorln(err)
return false
}
defer rep.Body.Close()
if rep.StatusCode == http.StatusOK {
log.Debugf("type=%s key=%v", "delete", key)
return true
}
dump, _ := httputil.DumpResponse(rep, true)
log.Debugf("%q", dump)
return false
}
// RESTPut access server's REST API with url = http://ip:port/key and request body of value
func (c *Client) RESTPut(key kv_store.Key, value kv_store.Value, table string) {
c.cid++
id := c.getNodeID(key)
url := c.http[id] + "/" + table + "/" + string(key)
log.Debugf("RESTPUT %s cid=%d, url=%s\n", string(key), c.cid, url)
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(value))
if err != nil {
log.Errorln(err)
return
}
req.Header.Set("id", fmt.Sprintf("%v", c.ID))
req.Header.Set("cid", fmt.Sprintf("%v", c.cid))
req.Header.Set("timestamp", fmt.Sprintf("%d", time.Now().UnixNano()))
rep, err := http.DefaultClient.Do(req)
if err != nil {
log.Errorln(err)
return
}
defer rep.Body.Close()
if rep.StatusCode == http.StatusOK {
log.Debugf("type=%s key=%v value=%x", "put", string(key), value)
} else {
dump, _ := httputil.DumpResponse(rep, true)
log.Debugf("%q", dump)
}
}
// Get post json get request to server url
func (c *Client) Get(key kv_store.Key, table string) kv_store.Value {
return c.RESTGet(key, table)
}
// Put post json request
func (c *Client) Put(key kv_store.Key, value kv_store.Value, table string) {
c.RESTPut(key, value, table)
}
// Delete post json request
func (c *Client) Delete(key kv_store.Key, table string) {
c.RESTPut(key, nil, table)
}
// GetAsync do Get request in goroutine
func (c *Client) GetAsync(key kv_store.Key, table string) {
c.Add(1)
c.Lock()
c.results[c.cid+1] = false
c.Unlock()
go c.Get(key, table)
}
// PutAsync do Put request in goroutine
func (c *Client) PutAsync(key kv_store.Key, value kv_store.Value, table string) {
c.Add(1)
c.Lock()
c.results[c.cid+1] = false
c.Unlock()
go c.Put(key, value, table)
}
// Put post json request
func (c *Client) PutTx(keys []kv_store.Key, values []kv_store.Value, tables []string) bool {
c.txNum++
cmds := make([]kv_store.Command, len(keys))
cntr := 0
for i, k := range keys {
c.cid++
cmd := kv_store.Command{tables[i], k, values[i],c.ID,c.cid, kv_store.PUT }
cmds[cntr] = cmd
cntr++
}
return c.JSONTX(cmds)
}
func (c *Client) PrepTx() {
c.txNum++
c.tempCmds = make([]kv_store.Command, 0)
}
// Add put for future TX
func (c *Client) AddTxPut(key kv_store.Key, value kv_store.Value, table string) {
c.cid++
cmd := kv_store.Command{table,key, value,c.ID,c.cid, kv_store.PUT }
c.tempCmds = append(c.tempCmds, cmd)
}
func (c *Client) AddTxDelete(key kv_store.Key, table string) {
c.cid++
cmd := kv_store.Command{table,key, nil,c.ID,c.cid, kv_store.DELETE }
c.tempCmds = append(c.tempCmds, cmd)
}
// Add get for future TX
func (c *Client) AddTxGet(key kv_store.Key, table string) {
c.cid++
cmd := kv_store.Command{table,key, nil,c.ID,c.cid, kv_store.GET }
c.tempCmds = append(c.tempCmds, cmd)
}
func (c *Client) SendTX() bool {
return c.JSONTX(c.tempCmds)
}
func (c *Client) JSONGet(key kv_store.Key, table string) kv_store.Value {
c.cid++
cmd := kv_store.Command{ table,key, nil, c.ID, c.cid, kv_store.GET}
req := new(Request)
req.Command = cmd
req.Timestamp = time.Now().UnixNano()
id := c.getNodeID(key)
url := c.http[id]
data, err := json.Marshal(*req)
rep, err := http.Post(url, "json", bytes.NewBuffer(data))
if err != nil {
log.Errorln(err)
return nil
}
defer rep.Body.Close()
if rep.StatusCode == http.StatusOK {
b, _ := ioutil.ReadAll(rep.Body)
return kv_store.Value(b)
}
return nil
}
func (c *Client) JSONPut(key kv_store.Key, value kv_store.Value, table string) {
c.cid++
cmd := kv_store.Command{table, key, value, c.ID,c.cid, kv_store.PUT}
req := new(Request)
req.Command = cmd
req.Timestamp = time.Now().UnixNano()
id := c.getNodeID(key)
url := c.http[id]
data, err := json.Marshal(*req)
rep, err := http.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
log.Errorln(err)
return
}
defer rep.Body.Close()
dump, _ := httputil.DumpResponse(rep, true)
log.Debugln(rep.Status)
log.Debugf("%q", dump)
}
func (c *Client) JSONTX(commands []kv_store.Command) bool {
c.cid++
tx := new(Transaction)
tx.ClientID = c.ID
tx.CommandID = c.cid
for _, cmd := range commands {
tx.Commands = append(tx.Commands, cmd)
}
url := c.http[c.getNodeID(commands[0].Key)]
log.Debugf("TX: %v\n", tx)
data, err := json.Marshal(*tx)
rep, err := http.Post(url, "application/json", bytes.NewBuffer(data))
if rep != nil {
log.Debugf("TX ok=%s", rep.Header["Ok"][0] )
defer rep.Body.Close()
if rep.Header["Ok"][0] == "true" {
return true
}
}
if err != nil {
log.Errorln(err)
return false
}
log.Debugln(rep.Status)
return false
}
// RequestDone returns the total number of succeed async reqeusts
func (c *Client) RequestDone() int {
sum := 0
for _, succeed := range c.results {
if succeed {
sum++
}
}
return sum
}
func (c *Client) Start() {}
func (c *Client) Stop() {}