-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshard_client.go
205 lines (195 loc) · 4.54 KB
/
shard_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
package zongzi
import (
"context"
)
// ShardClient can be used to interact with a shard regardless of its placement in the cluster
// Requests will be forwarded to the appropriate host based on ping
type ShardClient interface {
Apply(ctx context.Context, cmd []byte) (value uint64, data []byte, err error)
Commit(ctx context.Context, cmd []byte) (err error)
Index(ctx context.Context) (err error)
Leader() (uint64, uint64)
Read(ctx context.Context, query []byte, stale bool) (value uint64, data []byte, err error)
Watch(ctx context.Context, query []byte, results chan<- *Result, stale bool) (err error)
}
// The shard client
type client struct {
manager *clientManager
shardID uint64
retries int
writeToLeader bool
}
func newClient(manager *clientManager, shardID uint64, opts ...ClientOption) (c *client, err error) {
c = &client{
manager: manager,
shardID: shardID,
}
for _, fn := range opts {
if err = fn(c); err != nil {
return
}
}
return
}
func (c *client) Index(ctx context.Context) (err error) {
c.manager.mutex.RLock()
list, ok := c.manager.clientMember[c.shardID]
c.manager.mutex.RUnlock()
if !ok {
err = ErrShardNotReady
return
}
el := list.Front()
for ; el != nil; el = el.Next() {
err = el.Value.ReadIndex(ctx, c.shardID)
if err == nil {
break
}
}
return
}
func (c *client) Leader() (replicaID, term uint64) {
c.manager.mutex.RLock()
leader, ok := c.manager.clientLeader[c.shardID]
c.manager.mutex.RUnlock()
if ok {
replicaID = leader.replicaID
term = leader.term
}
return
}
func (c *client) Apply(ctx context.Context, cmd []byte) (value uint64, data []byte, err error) {
if c.writeToLeader {
c.manager.mutex.RLock()
leader, ok := c.manager.clientLeader[c.shardID]
c.manager.mutex.RUnlock()
if ok {
return leader.client.Apply(ctx, c.shardID, cmd)
}
}
c.manager.mutex.RLock()
list, ok := c.manager.clientMember[c.shardID]
c.manager.mutex.RUnlock()
if !ok {
err = ErrShardNotReady
return
}
for el := list.Front(); el != nil; el = el.Next() {
value, data, err = el.Value.Apply(ctx, c.shardID, cmd)
if err == nil {
break
}
}
return
}
func (c *client) Commit(ctx context.Context, cmd []byte) (err error) {
if c.writeToLeader {
c.manager.mutex.RLock()
leader, ok := c.manager.clientLeader[c.shardID]
c.manager.mutex.RUnlock()
if ok {
return leader.client.Commit(ctx, c.shardID, cmd)
}
}
c.manager.mutex.RLock()
list, ok := c.manager.clientMember[c.shardID]
c.manager.mutex.RUnlock()
if !ok {
err = ErrShardNotReady
return
}
el := list.Front()
for ; el != nil; el = el.Next() {
err = el.Value.Commit(ctx, c.shardID, cmd)
if err == nil {
break
}
}
return
}
func (c *client) Read(ctx context.Context, query []byte, stale bool) (value uint64, data []byte, err error) {
var run bool
if stale {
c.manager.mutex.RLock()
list, ok := c.manager.clientMember[c.shardID]
c.manager.mutex.RUnlock()
if !ok {
err = ErrShardNotReady
return
}
el := list.Front()
for ; el != nil; el = el.Next() {
run = true
value, data, err = el.Value.Read(ctx, c.shardID, query, stale)
if err == nil {
break
}
}
if run && err == nil {
return
}
}
if c.writeToLeader {
if leader, ok := c.manager.clientLeader[c.shardID]; ok {
return leader.client.Read(ctx, c.shardID, query, stale)
}
}
c.manager.mutex.RLock()
list, ok := c.manager.clientMember[c.shardID]
c.manager.mutex.RUnlock()
if !ok {
err = ErrShardNotReady
return
}
el := list.Front()
for ; el != nil; el = el.Next() {
value, data, err = el.Value.Read(ctx, c.shardID, query, stale)
if err == nil {
break
}
}
return
}
func (c *client) Watch(ctx context.Context, query []byte, results chan<- *Result, stale bool) (err error) {
var run bool
if stale {
c.manager.mutex.RLock()
list, ok := c.manager.clientMember[c.shardID]
c.manager.mutex.RUnlock()
if !ok {
err = ErrShardNotReady
return
}
el := list.Front()
for ; el != nil; el = el.Next() {
run = true
err = el.Value.Watch(ctx, c.shardID, query, results, stale)
if err == nil {
break
}
}
if run && err == nil {
return
}
}
if c.writeToLeader {
if leader, ok := c.manager.clientLeader[c.shardID]; ok {
return leader.client.Watch(ctx, c.shardID, query, results, stale)
}
}
c.manager.mutex.RLock()
list, ok := c.manager.clientMember[c.shardID]
c.manager.mutex.RUnlock()
if !ok {
err = ErrShardNotReady
return
}
el := list.Front()
for ; el != nil; el = el.Next() {
err = el.Value.Watch(ctx, c.shardID, query, results, stale)
if err == nil {
break
}
}
return
}