-
Notifications
You must be signed in to change notification settings - Fork 0
/
shard_client.go
166 lines (157 loc) · 3.64 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
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 {
ReadIndex(ctx context.Context) (err error)
Apply(ctx context.Context, cmd []byte) (value uint64, data []byte, err error)
Commit(ctx context.Context, cmd []byte) (err error)
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 shardClient
type shardClient struct {
manager *shardClientManager
shardID uint64
retries int
}
func newShardClient(manager *shardClientManager, shardID uint64, opts ...ShardClientOption) (c *shardClient, err error) {
c = &shardClient{
manager: manager,
shardID: shardID,
}
for _, fn := range opts {
if err = fn(c); err != nil {
return
}
}
return
}
func (c *shardClient) ReadIndex(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 *shardClient) Apply(ctx context.Context, cmd []byte) (value uint64, data []byte, err error) {
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 *shardClient) Commit(ctx context.Context, cmd []byte) (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.Commit(ctx, c.shardID, cmd)
if err == nil {
break
}
}
return
}
func (c *shardClient) 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
}
}
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 *shardClient) 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
}
}
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
}