-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_client.go
166 lines (150 loc) · 3.75 KB
/
host_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
package zongzi
import (
"context"
"time"
"github.com/benbjohnson/clock"
"google.golang.org/grpc"
"github.com/logbn/zongzi/internal"
)
type hostClient struct {
agent *Agent
clock clock.Clock
host Host
}
func newhostClient(a *Agent, host Host) hostClient {
return hostClient{
agent: a,
clock: clock.New(),
host: host,
}
}
func (c *hostClient) Ping(ctx context.Context) (t time.Duration, err error) {
if c.host.ID == c.agent.hostID() {
return
}
start := c.clock.Now()
_, err = c.agent.grpcClientPool.get(c.host.ApiAddress).Ping(ctx, &internal.PingRequest{})
t = c.clock.Since(start)
return
}
func (c *hostClient) ReadIndex(ctx context.Context, shardID uint64) (err error) {
if c.host.ID == c.agent.hostID() {
return
}
_, err = c.agent.grpcClientPool.get(c.host.ApiAddress).Index(ctx, &internal.IndexRequest{ShardId: shardID})
return
}
func (c *hostClient) Apply(ctx context.Context, shardID uint64, cmd []byte) (value uint64, data []byte, err error) {
var res *internal.ApplyResponse
if c.host.ID == c.agent.hostID() {
c.agent.log.Debugf(`gRPC hostClient Apply Local: %s`, string(cmd))
res, err = c.agent.grpcServer.Apply(ctx, &internal.ApplyRequest{
ShardId: shardID,
Data: cmd,
})
} else {
c.agent.log.Debugf(`gRPC hostClient Apply Remote: %s`, string(cmd))
res, err = c.agent.grpcClientPool.get(c.host.ApiAddress).Apply(ctx, &internal.ApplyRequest{
ShardId: shardID,
Data: cmd,
})
}
if err != nil {
return
}
value = res.Value
data = res.Data
return
}
func (c *hostClient) Commit(ctx context.Context, shardID uint64, cmd []byte) (err error) {
if c.host.ID == c.agent.hostID() {
c.agent.log.Debugf(`gRPC hostClient Commit Local: %s`, string(cmd))
_, err = c.agent.grpcServer.Commit(ctx, &internal.CommitRequest{
ShardId: shardID,
Data: cmd,
})
} else {
c.agent.log.Debugf(`gRPC hostClient Commit Remote: %s`, string(cmd))
_, err = c.agent.grpcClientPool.get(c.host.ApiAddress).Commit(ctx, &internal.CommitRequest{
ShardId: shardID,
Data: cmd,
})
}
if err != nil {
return
}
return
}
func (c *hostClient) Read(ctx context.Context, shardID uint64, query []byte, stale bool) (value uint64, data []byte, err error) {
var res *internal.ReadResponse
if c.host.ID == c.agent.hostID() {
res, err = c.agent.grpcServer.Read(ctx, &internal.ReadRequest{
ShardId: shardID,
Stale: stale,
Data: query,
})
} else {
res, err = c.agent.grpcClientPool.get(c.host.ApiAddress).Read(ctx, &internal.ReadRequest{
ShardId: shardID,
Stale: stale,
Data: query,
})
}
if err != nil {
return
}
value = res.Value
data = res.Data
return
}
type watchServer struct {
grpc.ServerStream
ctx context.Context
results chan<- *Result
}
func newWatchServer(ctx context.Context, results chan<- *Result) *watchServer {
return &watchServer{
ctx: ctx,
results: results,
}
}
func (s *watchServer) Context() context.Context {
return s.ctx
}
func (s *watchServer) Send(res *internal.WatchResponse) error {
s.results <- &Result{
Value: res.Value,
Data: res.Data,
}
return nil
}
func (c *hostClient) Watch(ctx context.Context, shardID uint64, query []byte, results chan<- *Result, stale bool) (err error) {
var client internal.Internal_WatchClient
if c.host.ID == c.agent.hostID() {
err = c.agent.grpcServer.Watch(&internal.WatchRequest{
ShardId: shardID,
Stale: stale,
Data: query,
}, newWatchServer(ctx, results))
} else {
client, err = c.agent.grpcClientPool.get(c.host.ApiAddress).Watch(ctx, &internal.WatchRequest{
ShardId: shardID,
Stale: stale,
Data: query,
})
for {
res, err := client.Recv()
if err != nil {
break
}
results <- &Result{
Value: res.Value,
Data: res.Data,
}
}
}
if err != nil {
return
}
return
}