-
Notifications
You must be signed in to change notification settings - Fork 662
/
shared_memory_server.go
306 lines (255 loc) · 6.54 KB
/
shared_memory_server.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gsharedmemory
import (
"context"
"sync"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
sharedmemorypb "github.com/ava-labs/avalanchego/proto/pb/sharedmemory"
)
var _ sharedmemorypb.SharedMemoryServer = &Server{}
// Server is shared memory that is managed over RPC.
type Server struct {
sharedmemorypb.UnsafeSharedMemoryServer
sm atomic.SharedMemory
db database.Database
getsLock sync.Mutex
gets map[int64]*getRequest
indexedLock sync.Mutex
indexed map[int64]*indexedRequest
applyLock sync.Mutex
apply map[int64]*applyRequest
}
// NewServer returns shared memory connected to remote shared memory
func NewServer(sm atomic.SharedMemory, db database.Database) *Server {
return &Server{
sm: sm,
db: db,
gets: make(map[int64]*getRequest),
indexed: make(map[int64]*indexedRequest),
apply: make(map[int64]*applyRequest),
}
}
type getRequest struct {
peerChainID ids.ID
keys [][]byte
executed bool
remainingValues [][]byte
}
func (s *Server) Get(
_ context.Context,
req *sharedmemorypb.GetRequest,
) (*sharedmemorypb.GetResponse, error) {
s.getsLock.Lock()
defer s.getsLock.Unlock()
get, exists := s.gets[req.Id]
if !exists {
peerChainID, err := ids.ToID(req.PeerChainId)
if err != nil {
return nil, err
}
get = &getRequest{
peerChainID: peerChainID,
keys: make([][]byte, 0, len(req.Keys)),
}
}
get.keys = append(get.keys, req.Keys...)
if req.Continues {
s.gets[req.Id] = get
return &sharedmemorypb.GetResponse{}, nil
}
if !get.executed {
values, err := s.sm.Get(get.peerChainID, get.keys)
if err != nil {
delete(s.gets, req.Id)
return nil, err
}
get.executed = true
get.remainingValues = values
}
currentSize := 0
resp := &sharedmemorypb.GetResponse{}
for i, value := range get.remainingValues {
sizeChange := baseElementSize + len(value)
if newSize := currentSize + sizeChange; newSize > maxBatchSize && i > 0 {
break
}
currentSize += sizeChange
resp.Values = append(resp.Values, value)
}
get.remainingValues = get.remainingValues[len(resp.Values):]
resp.Continues = len(get.remainingValues) > 0
if resp.Continues {
s.gets[req.Id] = get
} else {
delete(s.gets, req.Id)
}
return resp, nil
}
type indexedRequest struct {
peerChainID ids.ID
traits [][]byte
startTrait []byte
startKey []byte
limit int
executed bool
remainingValues [][]byte
lastTrait []byte
lastKey []byte
}
func (s *Server) Indexed(
_ context.Context,
req *sharedmemorypb.IndexedRequest,
) (*sharedmemorypb.IndexedResponse, error) {
s.indexedLock.Lock()
defer s.indexedLock.Unlock()
indexed, exists := s.indexed[req.Id]
if !exists {
peerChainID, err := ids.ToID(req.PeerChainId)
if err != nil {
return nil, err
}
indexed = &indexedRequest{
peerChainID: peerChainID,
traits: make([][]byte, 0, len(req.Traits)),
startTrait: req.StartTrait,
startKey: req.StartKey,
limit: int(req.Limit),
}
}
indexed.traits = append(indexed.traits, req.Traits...)
if req.Continues {
s.indexed[req.Id] = indexed
return &sharedmemorypb.IndexedResponse{}, nil
}
if !indexed.executed {
values, lastTrait, lastKey, err := s.sm.Indexed(
indexed.peerChainID,
indexed.traits,
indexed.startTrait,
indexed.startKey,
indexed.limit,
)
if err != nil {
delete(s.indexed, req.Id)
return nil, err
}
indexed.executed = true
indexed.remainingValues = values
indexed.lastTrait = lastTrait
indexed.lastKey = lastKey
}
currentSize := 0
resp := &sharedmemorypb.IndexedResponse{
LastTrait: indexed.lastTrait,
LastKey: indexed.lastKey,
}
for i, value := range indexed.remainingValues {
sizeChange := baseElementSize + len(value)
if newSize := currentSize + sizeChange; newSize > maxBatchSize && i > 0 {
break
}
currentSize += sizeChange
resp.Values = append(resp.Values, value)
}
indexed.remainingValues = indexed.remainingValues[len(resp.Values):]
resp.Continues = len(indexed.remainingValues) > 0
if resp.Continues {
indexed.lastTrait = nil
indexed.lastKey = nil
s.indexed[req.Id] = indexed
} else {
delete(s.indexed, req.Id)
}
return resp, nil
}
type applyRequest struct {
requests map[ids.ID]*atomic.Requests
batches map[int64]database.Batch
}
func (s *Server) Apply(
_ context.Context,
req *sharedmemorypb.ApplyRequest,
) (*sharedmemorypb.ApplyResponse, error) {
s.applyLock.Lock()
defer s.applyLock.Unlock()
apply, exists := s.apply[req.Id]
if !exists {
apply = &applyRequest{
requests: make(map[ids.ID]*atomic.Requests),
batches: make(map[int64]database.Batch),
}
}
if err := s.parseRequests(apply.requests, req.Requests); err != nil {
delete(s.apply, req.Id)
return nil, err
}
if err := s.parseBatches(apply.batches, req.Batches); err != nil {
delete(s.apply, req.Id)
return nil, err
}
if req.Continues {
s.apply[req.Id] = apply
return &sharedmemorypb.ApplyResponse{}, nil
}
delete(s.apply, req.Id)
batches := make([]database.Batch, len(apply.batches))
i := 0
for _, batch := range apply.batches {
batches[i] = batch
i++
}
return &sharedmemorypb.ApplyResponse{}, s.sm.Apply(apply.requests, batches...)
}
func (s *Server) parseRequests(
requests map[ids.ID]*atomic.Requests,
rawRequests []*sharedmemorypb.AtomicRequest,
) error {
for _, value := range rawRequests {
peerChainID, err := ids.ToID(value.PeerChainId)
if err != nil {
return err
}
req, ok := requests[peerChainID]
if !ok {
req = &atomic.Requests{
PutRequests: make([]*atomic.Element, 0, len(value.PutRequests)),
}
requests[peerChainID] = req
}
req.RemoveRequests = append(req.RemoveRequests, value.RemoveRequests...)
for _, v := range value.PutRequests {
req.PutRequests = append(req.PutRequests, &atomic.Element{
Key: v.Key,
Value: v.Value,
Traits: v.Traits,
})
}
}
return nil
}
func (s *Server) parseBatches(
batches map[int64]database.Batch,
rawBatches []*sharedmemorypb.Batch,
) error {
for _, reqBatch := range rawBatches {
batch, exists := batches[reqBatch.Id]
if !exists {
batch = s.db.NewBatch()
batches[reqBatch.Id] = batch
}
for _, putReq := range reqBatch.Puts {
if err := batch.Put(putReq.Key, putReq.Value); err != nil {
return err
}
}
for _, deleteReq := range reqBatch.Deletes {
if err := batch.Delete(deleteReq.Key); err != nil {
return err
}
}
}
return nil
}