-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.go
192 lines (175 loc) · 5.38 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
package client
import (
"bytes"
"context"
"dfs/proto"
"dfs/utils"
"fmt"
"io"
"log"
"time"
"google.golang.org/grpc"
)
var (
config = utils.GetConfig()
address = config.NameNodeHost + config.NameNodePort
datanodePort = config.DataNodePort
blockSize = config.BlockSize
)
func getGrpcClientConn(address string) (*grpc.ClientConn, *proto.DfsClient, *context.CancelFunc, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
conn, err := grpc.DialContext(ctx, address, grpc.WithInsecure(), grpc.WithBlock())
// conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect to %v error %v", address, err)
}
client := proto.NewDfsClient(conn)
return conn, &client, &cancel, err
}
// Read a file
// returns bytes of the file
// todo add offset
func Read(fileName string) []byte {
fileLocationArr := getFileLocation(fileName, proto.FileName_READ)
blockList := fileLocationArr.FileBlocksList
file := make([]byte, 0)
for _, blockReplicas := range blockList {
replicaList := blockReplicas.BlockReplicaList
for _, block := range replicaList {
// add retry here with arr
tempBlock := readBlock(block.BlockName, block.IpAddr)
file = append(file, tempBlock...)
break
}
}
return file
}
func readBlock(chunkName string, ipAddr string) []byte {
conn, client, cancel1, _ := getGrpcClientConn(ipAddr + datanodePort)
defer (*cancel1)()
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
fileStream, err := (*client).GetBlock(ctx, &proto.FileName{FileName: chunkName})
if err != nil {
log.Fatalf("error getting block %v", err)
}
chunkData := bytes.Buffer{}
for {
res, err := fileStream.Recv()
if err == io.EOF {
return chunkData.Bytes()
}
if err != nil {
log.Fatal("cannot receive response: ", err)
}
chunkData.Write(res.GetContent())
}
}
func getFileLocation(fileName string, mode proto.FileName_Mode) *proto.FileLocationArr {
conn, client, _, _ := getGrpcClientConn(address)
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
fileLocationArr, err := (*client).GetFileLocation(ctx, &proto.FileName{FileName: fileName, Mode: mode})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
return fileLocationArr
}
func writeBlock(chunkName string, ipAddr string, data []byte, blockReplicaList *proto.BlockReplicaList) error {
conn, client, _, _ := getGrpcClientConn(ipAddr + datanodePort)
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
writeBlockClient, err := (*client).WriteBlock(ctx)
if err != nil {
return err
}
sentDataLength := 0
chunkSize := 50
err = writeBlockClient.Send(&proto.FileWriteStream{BlockReplicaList: blockReplicaList})
if err != nil {
return err
}
for sentDataLength < len(data) {
max := (sentDataLength + chunkSize)
if max > len(data) {
max = len(data)
}
chunk := data[sentDataLength:max]
_ = writeBlockClient.Send(&proto.FileWriteStream{File: &proto.File{Content: chunk}})
sentDataLength = chunkSize + sentDataLength
}
blockStatus, error := writeBlockClient.CloseAndRecv()
fmt.Println(blockStatus)
if error != nil {
return error
}
return nil
}
// Write a new file
// returns true if successful
// returns false if error
func Write(fileName string, data []byte) bool {
for len(data) > 0 {
fileLocation := getFileLocation(fileName, proto.FileName_WRITE)
blockReplicas := fileLocation.FileBlocksList[0]
blockCapacity := blockSize - blockReplicas.BlockReplicaList[0].BlockSize
limit := int64(len(data))
if blockCapacity > int64(len(data)) {
limit = blockCapacity
}
writeBlock(fileName, blockReplicas.BlockReplicaList[0].IpAddr, data[0:limit], blockReplicas)
data = data[limit:len(data)]
}
return false
}
// Append to a existing file
// returns true if successful
// returns false if error
func Append(fileName string) bool {
return false
}
// sends heartbeat to namenode to check if its alive and
// renews the file lease
func renewLock(file string) {
conn, dfsClient, cancel1, _ := getGrpcClientConn(address + datanodePort)
defer (*cancel1)()
defer conn.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
res, err := (*dfsClient).RenewLock(ctx, &proto.FileName{FileName: file})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
if res.GetSuccess() {
log.Printf("renewed lease")
} else {
log.Printf("was not able to renew lease")
}
}
func createFileNameNode(fileName string) *proto.FileLocationArr {
conn, dfsClient, cancel1, _ := getGrpcClientConn(address + datanodePort)
defer conn.Close()
defer (*cancel1)()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
fileLocationArr, err := (*dfsClient).GetFileLocation(ctx, &proto.FileName{FileName: fileName})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
return fileLocationArr
}
// CreateFile creates a file
func CreateFile(file string) error {
fileLocation := createFileNameNode(file)
fileBlocks := fileLocation.FileBlocksList
blockReplicas := fileBlocks[0]
writeBlock(file, blockReplicas.BlockReplicaList[0].IpAddr, make([]byte, 0), blockReplicas)
for _, replica := range blockReplicas.BlockReplicaList {
fmt.Println(replica.IpAddr, "IpAddr")
fmt.Println(replica.BlockName, "BlockName")
}
return nil
}