-
Notifications
You must be signed in to change notification settings - Fork 670
/
shared_memory_client.go
326 lines (285 loc) · 7.8 KB
/
shared_memory_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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gsharedmemory
import (
"context"
stdatomic "sync/atomic"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/chains/atomic/gsharedmemory/gsharedmemoryproto"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
)
const (
maxBatchSize = 64 * 1024 // 64 KiB
// baseElementSize is an approximation of the protobuf encoding overhead per
// element
baseElementSize = 8 // bytes
)
var (
_ atomic.SharedMemory = &Client{}
)
// Client is atomic.SharedMemory that talks over RPC.
type Client struct {
client gsharedmemoryproto.SharedMemoryClient
uniqueID int64
}
// NewClient returns shared memory connected to remote shared memory
func NewClient(client gsharedmemoryproto.SharedMemoryClient) *Client {
return &Client{client: client}
}
func (c *Client) Put(peerChainID ids.ID, elems []*atomic.Element, rawBatches ...database.Batch) error {
req := gsharedmemoryproto.PutRequest{
PeerChainID: peerChainID[:],
Elems: make([]*gsharedmemoryproto.Element, 0, len(elems)),
Id: stdatomic.AddInt64(&c.uniqueID, 1),
Continues: true,
}
currentSize := 0
for _, elem := range elems {
sizeChange := baseElementSize + len(elem.Key) + len(elem.Value)
for _, trait := range elem.Traits {
sizeChange += len(trait)
}
if newSize := currentSize + sizeChange; newSize > maxBatchSize {
if _, err := c.client.Put(context.Background(), &req); err != nil {
return err
}
currentSize = 0
req.PeerChainID = nil
req.Elems = req.Elems[:0]
}
currentSize += sizeChange
req.Elems = append(req.Elems, &gsharedmemoryproto.Element{
Key: elem.Key,
Value: elem.Value,
Traits: elem.Traits,
})
}
batchGroups, err := c.makeBatches(rawBatches, currentSize)
if err != nil {
return err
}
for i, batches := range batchGroups {
req.Batches = batches
req.Continues = i < len(batchGroups)-1
if _, err := c.client.Put(context.Background(), &req); err != nil {
return err
}
req.PeerChainID = nil
req.Elems = nil
}
if len(batchGroups) == 0 {
req.Continues = false
if _, err := c.client.Put(context.Background(), &req); err != nil {
return err
}
}
return nil
}
func (c *Client) Get(peerChainID ids.ID, keys [][]byte) ([][]byte, error) {
req := &gsharedmemoryproto.GetRequest{
PeerChainID: peerChainID[:],
Id: stdatomic.AddInt64(&c.uniqueID, 1),
Continues: true,
}
currentSize := 0
prevIndex := 0
for i, key := range keys {
sizeChange := baseElementSize + len(key)
if newSize := currentSize + sizeChange; newSize > maxBatchSize {
_, err := c.client.Get(context.Background(), req)
if err != nil {
return nil, err
}
currentSize = 0
prevIndex = i
req.PeerChainID = nil
}
currentSize += sizeChange
req.Keys = keys[prevIndex : i+1]
}
req.Continues = false
resp, err := c.client.Get(context.Background(), req)
if err != nil {
return nil, err
}
values := resp.Values
req.PeerChainID = nil
req.Keys = nil
for resp.Continues {
resp, err = c.client.Get(context.Background(), req)
if err != nil {
return nil, err
}
values = append(values, resp.Values...)
}
return values, nil
}
func (c *Client) Indexed(
peerChainID ids.ID,
traits [][]byte,
startTrait,
startKey []byte,
limit int,
) (
[][]byte,
[]byte,
[]byte,
error,
) {
req := &gsharedmemoryproto.IndexedRequest{
PeerChainID: peerChainID[:],
StartTrait: startTrait,
StartKey: startKey,
Limit: int32(limit),
Id: stdatomic.AddInt64(&c.uniqueID, 1),
Continues: true,
}
currentSize := 0
prevIndex := 0
for i, trait := range traits {
sizeChange := baseElementSize + len(trait)
if newSize := currentSize + sizeChange; newSize > maxBatchSize {
_, err := c.client.Indexed(context.Background(), req)
if err != nil {
return nil, nil, nil, err
}
currentSize = 0
prevIndex = i
req.PeerChainID = nil
req.StartTrait = nil
req.StartKey = nil
req.Limit = 0
}
currentSize += sizeChange
req.Traits = traits[prevIndex : i+1]
}
req.Continues = false
resp, err := c.client.Indexed(context.Background(), req)
if err != nil {
return nil, nil, nil, err
}
lastTrait := resp.LastTrait
lastKey := resp.LastKey
values := resp.Values
req.PeerChainID = nil
req.Traits = nil
req.StartTrait = nil
req.StartKey = nil
req.Limit = 0
for resp.Continues {
resp, err = c.client.Indexed(context.Background(), req)
if err != nil {
return nil, nil, nil, err
}
values = append(values, resp.Values...)
}
return values, lastTrait, lastKey, nil
}
func (c *Client) Remove(peerChainID ids.ID, keys [][]byte, rawBatches ...database.Batch) error {
req := gsharedmemoryproto.RemoveRequest{
PeerChainID: peerChainID[:],
Id: stdatomic.AddInt64(&c.uniqueID, 1),
Continues: true,
}
currentSize := 0
prevIndex := 0
for i, key := range keys {
sizeChange := baseElementSize + len(key)
if newSize := currentSize + sizeChange; newSize > maxBatchSize {
if _, err := c.client.Remove(context.Background(), &req); err != nil {
return err
}
currentSize = 0
prevIndex = i
req.PeerChainID = nil
}
currentSize += sizeChange
req.Keys = keys[prevIndex : i+1]
}
batchGroups, err := c.makeBatches(rawBatches, currentSize)
if err != nil {
return err
}
for i, batches := range batchGroups {
req.Batches = batches
req.Continues = i < len(batchGroups)-1
if _, err := c.client.Remove(context.Background(), &req); err != nil {
return err
}
req.PeerChainID = nil
req.Keys = nil
}
if len(batchGroups) == 0 {
req.Continues = false
if _, err := c.client.Remove(context.Background(), &req); err != nil {
return err
}
}
return nil
}
func (c *Client) makeBatches(rawBatches []database.Batch, currentSize int) ([][]*gsharedmemoryproto.Batch, error) {
batchGroups := [][]*gsharedmemoryproto.Batch(nil)
currentBatchGroup := []*gsharedmemoryproto.Batch(nil)
currentBatch := &gsharedmemoryproto.Batch{
Id: stdatomic.AddInt64(&c.uniqueID, 1),
}
for _, batch := range rawBatches {
batch := batch.Inner()
fb := filteredBatch{
writes: make(map[string][]byte),
deletes: make(map[string]struct{}),
}
if err := batch.Replay(&fb); err != nil {
return nil, err
}
puts := fb.PutRequests()
for _, p := range puts {
sizeChange := baseElementSize + len(p.Key) + len(p.Value)
if newSize := currentSize + sizeChange; newSize > maxBatchSize {
if len(currentBatch.Deletes)+len(currentBatch.Puts) > 0 {
currentBatchGroup = append(currentBatchGroup, currentBatch)
}
if len(currentBatchGroup) > 0 {
batchGroups = append(batchGroups, currentBatchGroup)
}
currentSize = 0
currentBatchGroup = nil
currentBatch = &gsharedmemoryproto.Batch{
Id: currentBatch.Id,
}
}
currentSize += sizeChange
currentBatch.Puts = append(currentBatch.Puts, p)
}
deletes := fb.DeleteRequests()
for _, d := range deletes {
sizeChange := baseElementSize + len(d.Key)
if newSize := currentSize + sizeChange; newSize > maxBatchSize {
if len(currentBatch.Deletes)+len(currentBatch.Puts) > 0 {
currentBatchGroup = append(currentBatchGroup, currentBatch)
}
if len(currentBatchGroup) > 0 {
batchGroups = append(batchGroups, currentBatchGroup)
}
currentSize = 0
currentBatchGroup = nil
currentBatch = &gsharedmemoryproto.Batch{
Id: currentBatch.Id,
}
}
currentSize += sizeChange
currentBatch.Deletes = append(currentBatch.Deletes, d)
}
if len(currentBatch.Deletes)+len(currentBatch.Puts) > 0 {
currentBatchGroup = append(currentBatchGroup, currentBatch)
}
currentBatch = &gsharedmemoryproto.Batch{
Id: stdatomic.AddInt64(&c.uniqueID, 1),
}
}
if len(currentBatchGroup) > 0 {
batchGroups = append(batchGroups, currentBatchGroup)
}
return batchGroups, nil
}