-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
348 lines (310 loc) · 9.23 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package main
import (
"errors"
"fmt"
"net/rpc"
"strings"
"time"
)
// DEFINITIONS =================================================
const CHUNK_SIZE_MB = 64
const CHUNK_SIZE = CHUNK_SIZE_MB * 1024 * 1024
type handle uint64
type bufferID struct {
Handle handle
Timestamp time.Time
}
// RPC Arguments and Returns
type RegisterArgs struct {
ChunkserverAddr string
Handles map[handle]int // handle : version
}
type RegisterReturn int //placeholder
type GetChunkArgs struct {
FileName string
ChunkIndex int
}
type GetChunkReturn struct {
Handle handle
Chunkservers []string
Expire time.Time
}
type ReadArgs struct {
Handle handle
Offset int64 // precision required by os seek function
Length int
expire time.Time
}
type ReadReturn []byte
type DataPushArg struct {
Handle handle
Data []byte
}
type DataPushReturn struct {
DataBufferID bufferID // will be -1 when unsuccessful
}
type PrimaryApplyAppendArg struct {
Replicas []string
AppendBufferIDs []bufferID
expire time.Time
}
type PrimaryApplyAppendReturn int // offset
type PrimaryApplyWriteArg struct {
Replicas []string
WriteBufferIDs []bufferID
ChunkOffset int64
ChunkIndex int
expire time.Time
}
type PrimaryApplyWriteReturn int // placeholder
type CreateArgs string // absolute path
type CreateReturn int // placeholder
type DelArgs string // absolute path
type DelReturn int // placeholder
// PUBLIC API ==================================================
type LocationsKey struct {
filename string
index int
}
type GFSClient struct {
locations map[LocationsKey]GetChunkReturn
master string
}
func Init(master string) (*GFSClient, error) {
if len(strings.Split(master, ":")) != 2 {
return nil, errors.New("invalid master address")
}
gfsClient := GFSClient{map[LocationsKey]GetChunkReturn{}, master}
return &gfsClient, nil
}
func (c *GFSClient) Read(fileName string, readStart int64, readLength int) (ReadReturn, error) {
currChunkIndex := int(readStart / CHUNK_SIZE)
currByteIndex := readStart % CHUNK_SIZE
ret := []byte{}
for readLength > 0 {
remaining := int(CHUNK_SIZE - currByteIndex)
if remaining <= readLength {
// when more to read in next chunk -> process partially
//fmt.Printf("currChunkIndex: %d; currByteIndex: %d; data Length (partial): %d\n", currChunkIndex, currByteIndex, remaining)
chunkret, err := c.chunkRead(fileName, currChunkIndex, currByteIndex, remaining)
if err != nil {
return nil, err
}
ret = append(ret, []byte(chunkret)...)
readLength -= int(remaining)
currChunkIndex += 1
currByteIndex = 0
} else {
// when all read can be done in current chunk -> process the entire data
//fmt.Printf("currChunkIndex: %d; currByteIndex: %d; data Length: %d\n", currChunkIndex, currByteIndex, readLength)
chunkret, err := c.chunkRead(fileName, currChunkIndex, currByteIndex, readLength)
if err != nil {
return nil, err
}
ret = append(ret, []byte(chunkret)...)
readLength = 0
}
}
return ret, nil
}
// GFS client code that appends to the end of file.
func (c *GFSClient) RecordAppend(fileName string, data []byte) (int, error) {
// Get chunk handle and list of replicas from master if necessary
client, err := rpc.Dial("tcp", c.master)
if err != nil {
return -1, err
}
getChunkArgs := GetChunkArgs{
FileName: fileName,
ChunkIndex: -1,
}
getChunkReturn := GetChunkReturn{}
err = client.Call("MasterServer.GetChunkHandleAndLocations", getChunkArgs, &getChunkReturn)
if err != nil {
return -1, err
}
// push data and prepare append
var applyAppendArg PrimaryApplyAppendArg
applyAppendArg.AppendBufferIDs, err = push(getChunkReturn.Chunkservers, getChunkReturn.Handle, data)
if err != nil {
return -1, err
}
// apply append
applyAppendArg.Replicas = getChunkReturn.Chunkservers
applyAppendArg.expire = getChunkReturn.Expire
var applyAppendReturn PrimaryApplyAppendReturn
primary, err := rpc.Dial("tcp", getChunkReturn.Chunkservers[0])
if err != nil {
return -1, err
}
err = primary.Call("ChunkServer.PrimaryApplyAppend", applyAppendArg, &applyAppendReturn)
if err != nil {
return -1, err
}
return int(applyAppendReturn) + int(getChunkReturn.Handle)*64*1024*1024, nil
}
// Write data to fileName starting at writeStart
// NOTE: file starts with byte 0
func (c *GFSClient) Write(fileName string, writeStart int64, data []byte) error {
currChunkIndex := int(writeStart / CHUNK_SIZE)
currByteIndex := writeStart % CHUNK_SIZE
for len(data) > 0 {
remaining := CHUNK_SIZE - currByteIndex
if remaining <= int64(len(data)) {
// when remaining space is not sufficient to hold entire data -> process partially
c.chunkWrite(fileName, currChunkIndex, currByteIndex, data[:remaining], c.master)
data = data[remaining:]
currChunkIndex += 1
currByteIndex = 0
} else {
// when remaining space can hold entire data -> process the entire data
c.chunkWrite(fileName, currChunkIndex, currByteIndex, data, c.master)
data = data[:0]
}
}
return nil
}
func (c *GFSClient) Create(filename string) error {
// TODO Reuse master rpc client
client, err := rpc.Dial("tcp", c.master)
if err != nil {
return err
}
arg := CreateArgs(filename)
ret := CreateReturn(0)
err = client.Call("MasterServer.Create", arg, &ret)
if err != nil {
return err
}
return nil
}
func (c *GFSClient) Delete(filename string) error {
// TODO Reuse master rpc client
client, err := rpc.Dial("tcp", c.master)
if err != nil {
return err
}
arg := DelArgs(filename)
ret := DelReturn(0)
err = client.Call("MasterServer.Delete", arg, &ret)
if err != nil {
return err
}
return nil
}
// INTERNAL FUNCTIONS ==========================================
// GFS client code that pushes data to each replica.
// Returns an array of buffer id and error (nil when success)
func push(replicas []string, handle handle, data []byte) ([]bufferID, error) {
// prepare arguments and return
appendArgs := DataPushArg{
Handle: handle,
Data: data,
}
var pushReturn DataPushReturn
var bufferIDs []bufferID
// iterate all replicas to push data. return when any error happened
for _, replica := range replicas {
client, err := rpc.Dial("tcp", replica)
if err != nil {
return nil, err
}
err = client.Call("ChunkServer.DataPush", appendArgs, &pushReturn)
if err != nil {
return nil, err
}
//fmt.Printf("INFO: Data of %d bytes pushed to server %s\n", len(data), replica)
bufferIDs = append(bufferIDs, pushReturn.DataBufferID)
}
return bufferIDs, nil
}
// Write data to chunk index from startByte to endByte
func (c *GFSClient) chunkWrite(fileName string, index int, startByte int64, data []byte, master string) error {
var getChunkReturn GetChunkReturn
if val, ok := c.locations[LocationsKey{fileName, index}]; ok && val.Expire.After(time.Now()) {
//fmt.Println("Location known && not expired")
getChunkReturn = val
} else {
// Get chunk handle and list of replicas from master
client, err := rpc.Dial("tcp", master)
if err != nil {
return err
}
getChunkArgs := GetChunkArgs{
FileName: fileName,
ChunkIndex: index,
}
getChunkReturn = GetChunkReturn{}
err = client.Call("MasterServer.GetChunkHandleAndLocations", getChunkArgs, &getChunkReturn)
if err != nil {
return err
}
c.locations[LocationsKey{fileName, index}] = getChunkReturn
}
// push data and prepare write
var applyWriteArg PrimaryApplyWriteArg
var err error
applyWriteArg.WriteBufferIDs, err = push(getChunkReturn.Chunkservers, getChunkReturn.Handle, data)
if err != nil {
return err
}
// write data
applyWriteArg.Replicas = getChunkReturn.Chunkservers
applyWriteArg.ChunkIndex = int(index)
applyWriteArg.ChunkOffset = int64(startByte)
applyWriteArg.expire = getChunkReturn.Expire
var applyWriteReturn PrimaryApplyWriteReturn
primary, err := rpc.Dial("tcp", getChunkReturn.Chunkservers[0])
if err != nil {
return err
}
err = primary.Call("ChunkServer.PrimaryApplyWrite", applyWriteArg, &applyWriteReturn)
if err != nil {
return err
}
return nil
}
// GFS client code that reads file starting at readStart byte for readLength bytes
// Note: Read starts at byte 0
func (c *GFSClient) chunkRead(fileName string, index int, startByte int64, readLen int) (ReadReturn, error) {
var getChunkReturn GetChunkReturn
if val, ok := c.locations[LocationsKey{fileName, index}]; ok && val.Expire.After(time.Now()) {
// location already known and not expired yet
getChunkReturn = val
} else {
// Get chunk handle and list of replicas from master
client, err := rpc.Dial("tcp", c.master)
if err != nil {
return nil, err
}
getChunkArgs := GetChunkArgs{
FileName: fileName,
ChunkIndex: index,
}
getChunkReturn = GetChunkReturn{}
err = client.Call("MasterServer.GetChunkHandleAndLocations", getChunkArgs, &getChunkReturn)
if err != nil {
return nil, err
}
// store requested location
c.locations[LocationsKey{fileName, index}] = getChunkReturn
}
// Call chunkserver for read
client, err := rpc.Dial("tcp", getChunkReturn.Chunkservers[0])
if err != nil {
return nil, err
}
readArgs := ReadArgs{
Handle: getChunkReturn.Handle,
Offset: startByte,
Length: readLen,
expire: getChunkReturn.Expire,
}
readReturn := []byte{}
err = client.Call("ChunkServer.Read", readArgs, &readReturn)
if err != nil {
return nil, err
} else {
return readReturn, nil
}
}