/
rpcClient.go
86 lines (68 loc) · 1.54 KB
/
rpcClient.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
package worker
import (
"context"
"encoding/json"
"time"
"github.com/BWbwchen/MapReduce/rpc"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
func WorkerRegister(w *rpc.WorkerInfo) int {
conn, err := grpc.Dial(MasterIP, grpc.WithInsecure())
if err != nil {
panic(err)
}
defer conn.Close()
c := rpc.NewMasterClient(conn)
log.Trace("New Master Client")
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
log.Trace("With Time out")
defer cancel()
log.Trace("Start RPC call")
r, err := c.WorkerRegister(ctx, w)
log.Trace("End RPC call")
if err != nil {
panic(err)
}
if r.Result == false {
panic("Register Error")
}
return int(r.Id)
}
func UpdateIMDInfo(u *rpc.IMDInfo) bool {
conn, err := grpc.Dial(MasterIP, grpc.WithInsecure())
if err != nil {
log.Panic(err)
}
defer conn.Close()
c := rpc.NewMasterClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.UpdateIMDInfo(ctx, u)
if err != nil {
log.Panic(err)
}
if r.Result == false {
log.Panic("Update IMD Info Error")
}
return r.Result
}
func GetIMDData(ip string, filename string) []KV {
conn, err := grpc.Dial(ip, grpc.WithInsecure())
if err != nil {
panic(err)
}
defer conn.Close()
c := rpc.NewWorkerClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.GetIMDData(ctx, &rpc.IMDLoc{
Filename: filename,
})
var ret []KV
err = json.Unmarshal([]byte(r.Kvs), &ret)
if err != nil {
panic(err)
}
return ret
}