forked from coreos/torus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
148 lines (129 loc) · 3.34 KB
/
grpc.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
package grpc
import (
"net"
"net/url"
"strings"
"time"
"google.golang.org/grpc"
"golang.org/x/net/context"
"github.com/coreos/torus"
"github.com/coreos/torus/distributor/protocols"
"github.com/coreos/torus/models"
)
const defaultPort = "40000"
func init() {
protocols.RegisterRPCListener("http", grpcRPCListener)
protocols.RegisterRPCDialer("http", grpcRPCDialer)
}
func grpcRPCListener(url *url.URL, hdl protocols.RPC, gmd torus.GlobalMetadata) (protocols.RPCServer, error) {
out := &handler{
handle: hdl,
}
h := url.Host
if !strings.Contains(h, ":") {
h = net.JoinHostPort(h, defaultPort)
}
lis, err := net.Listen("tcp", h)
if err != nil {
return nil, err
}
out.grpc = grpc.NewServer()
models.RegisterTorusStorageServer(out.grpc, out)
go out.grpc.Serve(lis)
return out, nil
}
func grpcRPCDialer(url *url.URL, timeout time.Duration, gmd torus.GlobalMetadata) (protocols.RPC, error) {
h := url.Host
if !strings.Contains(h, ":") {
h = net.JoinHostPort(h, defaultPort)
}
conn, err := grpc.Dial(h, grpc.WithInsecure(), grpc.WithTimeout(timeout))
if err != nil {
return nil, err
}
return &client{
conn: conn,
handler: models.NewTorusStorageClient(conn),
}, nil
}
type client struct {
conn *grpc.ClientConn
handler models.TorusStorageClient
}
func (c *client) Close() error {
return c.conn.Close()
}
func (c *client) PutBlock(ctx context.Context, ref torus.BlockRef, data []byte) error {
_, err := c.handler.PutBlock(ctx, &models.PutBlockRequest{
Refs: []*models.BlockRef{
ref.ToProto(),
},
Blocks: [][]byte{
data,
},
})
return err
}
func (c *client) Block(ctx context.Context, ref torus.BlockRef) ([]byte, error) {
resp, err := c.handler.Block(ctx, &models.BlockRequest{
BlockRef: ref.ToProto(),
})
if err != nil {
return nil, err
}
return resp.Data, nil
}
func (c *client) RebalanceCheck(ctx context.Context, refs []torus.BlockRef) ([]bool, error) {
req := &models.RebalanceCheckRequest{}
for _, x := range refs {
req.BlockRefs = append(req.BlockRefs, x.ToProto())
}
resp, err := c.handler.RebalanceCheck(ctx, req)
if err != nil {
return nil, err
}
return resp.Valid, nil
}
func (c *client) WriteBuf(ctx context.Context, ref torus.BlockRef) ([]byte, error) {
panic("unimplemented")
}
type handler struct {
handle protocols.RPC
grpc *grpc.Server
}
func (h *handler) Block(ctx context.Context, req *models.BlockRequest) (*models.BlockResponse, error) {
data, err := h.handle.Block(ctx, torus.BlockFromProto(req.BlockRef))
if err != nil {
return nil, err
}
return &models.BlockResponse{
Ok: true,
Data: data,
}, nil
}
func (h *handler) PutBlock(ctx context.Context, req *models.PutBlockRequest) (*models.PutResponse, error) {
for i, ref := range req.Refs {
err := h.handle.PutBlock(ctx, torus.BlockFromProto(ref), req.Blocks[i])
if err != nil {
return nil, err
}
}
return &models.PutResponse{Ok: true}, nil
}
func (h *handler) RebalanceCheck(ctx context.Context, req *models.RebalanceCheckRequest) (*models.RebalanceCheckResponse, error) {
check := make([]torus.BlockRef, len(req.BlockRefs))
for i, x := range req.BlockRefs {
check[i] = torus.BlockFromProto(x)
}
out, err := h.handle.RebalanceCheck(ctx, check)
if err != nil {
return nil, err
}
return &models.RebalanceCheckResponse{
Valid: out,
}, nil
}
func (h *handler) Close() error {
h.grpc.Stop()
return nil
}