-
Notifications
You must be signed in to change notification settings - Fork 644
/
db_server.go
216 lines (179 loc) · 7.01 KB
/
db_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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package rpcdb
import (
"context"
"encoding/json"
"errors"
"sync"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/ava-labs/avalanchego/database"
rpcdbpb "github.com/ava-labs/avalanchego/proto/pb/rpcdb"
)
var errUnknownIterator = errors.New("unknown iterator")
// DatabaseServer is a database that is managed over RPC.
type DatabaseServer struct {
rpcdbpb.UnsafeDatabaseServer
db database.Database
// batchLock protects [batches] from concurrent modifications. Note that
// [batchLock] does not protect the actual Batch. Batches are documented as
// not being safe for concurrent use. Therefore, it is up to the client to
// respect this invariant.
batchLock sync.Mutex
batches map[int64]database.Batch
// iteratorLock protects [nextIteratorID] and [iterators] from concurrent
// modifications. Similarly to [batchLock], [iteratorLock] does not protect
// the actual Iterator. Iterators are documented as not being safe for
// concurrent use. Therefore, it is up to the client to respect this
// invariant.
iteratorLock sync.RWMutex
nextIteratorID uint64
iterators map[uint64]database.Iterator
}
// NewServer returns a database instance that is managed remotely
func NewServer(db database.Database) *DatabaseServer {
return &DatabaseServer{
db: db,
batches: make(map[int64]database.Batch),
iterators: make(map[uint64]database.Iterator),
}
}
// Has delegates the Has call to the managed database and returns the result
func (db *DatabaseServer) Has(_ context.Context, req *rpcdbpb.HasRequest) (*rpcdbpb.HasResponse, error) {
has, err := db.db.Has(req.Key)
return &rpcdbpb.HasResponse{
Has: has,
Err: errorToErrCode[err],
}, errorToRPCError(err)
}
// Get delegates the Get call to the managed database and returns the result
func (db *DatabaseServer) Get(_ context.Context, req *rpcdbpb.GetRequest) (*rpcdbpb.GetResponse, error) {
value, err := db.db.Get(req.Key)
return &rpcdbpb.GetResponse{
Value: value,
Err: errorToErrCode[err],
}, errorToRPCError(err)
}
// Put delegates the Put call to the managed database and returns the result
func (db *DatabaseServer) Put(_ context.Context, req *rpcdbpb.PutRequest) (*rpcdbpb.PutResponse, error) {
err := db.db.Put(req.Key, req.Value)
return &rpcdbpb.PutResponse{Err: errorToErrCode[err]}, errorToRPCError(err)
}
// Delete delegates the Delete call to the managed database and returns the
// result
func (db *DatabaseServer) Delete(_ context.Context, req *rpcdbpb.DeleteRequest) (*rpcdbpb.DeleteResponse, error) {
err := db.db.Delete(req.Key)
return &rpcdbpb.DeleteResponse{Err: errorToErrCode[err]}, errorToRPCError(err)
}
// Compact delegates the Compact call to the managed database and returns the
// result
func (db *DatabaseServer) Compact(_ context.Context, req *rpcdbpb.CompactRequest) (*rpcdbpb.CompactResponse, error) {
err := db.db.Compact(req.Start, req.Limit)
return &rpcdbpb.CompactResponse{Err: errorToErrCode[err]}, errorToRPCError(err)
}
// Close delegates the Close call to the managed database and returns the result
func (db *DatabaseServer) Close(context.Context, *rpcdbpb.CloseRequest) (*rpcdbpb.CloseResponse, error) {
err := db.db.Close()
return &rpcdbpb.CloseResponse{Err: errorToErrCode[err]}, errorToRPCError(err)
}
// HealthCheck performs a heath check against the underlying database.
func (db *DatabaseServer) HealthCheck(context.Context, *emptypb.Empty) (*rpcdbpb.HealthCheckResponse, error) {
health, err := db.db.HealthCheck()
if err != nil {
return &rpcdbpb.HealthCheckResponse{}, err
}
details, err := json.Marshal(health)
return &rpcdbpb.HealthCheckResponse{
Details: details,
}, err
}
// WriteBatch takes in a set of key-value pairs and atomically writes them to
// the internal database
func (db *DatabaseServer) WriteBatch(_ context.Context, req *rpcdbpb.WriteBatchRequest) (*rpcdbpb.WriteBatchResponse, error) {
db.batchLock.Lock()
batch, exists := db.batches[req.Id]
if !exists {
batch = db.db.NewBatch()
db.batches[req.Id] = batch
}
db.batchLock.Unlock()
for _, put := range req.Puts {
if err := batch.Put(put.Key, put.Value); err != nil {
// Because we are reporting an error, we free the allocated batch.
delete(db.batches, req.Id)
return &rpcdbpb.WriteBatchResponse{Err: errorToErrCode[err]}, errorToRPCError(err)
}
}
for _, del := range req.Deletes {
if err := batch.Delete(del.Key); err != nil {
// Because we are reporting an error, we free the allocated batch.
delete(db.batches, req.Id)
return &rpcdbpb.WriteBatchResponse{Err: errorToErrCode[err]}, errorToRPCError(err)
}
}
if req.Continues {
return &rpcdbpb.WriteBatchResponse{}, nil
}
db.batchLock.Lock()
delete(db.batches, req.Id)
db.batchLock.Unlock()
err := batch.Write()
return &rpcdbpb.WriteBatchResponse{Err: errorToErrCode[err]}, errorToRPCError(err)
}
// NewIteratorWithStartAndPrefix allocates an iterator and returns the iterator
// ID
func (db *DatabaseServer) NewIteratorWithStartAndPrefix(_ context.Context, req *rpcdbpb.NewIteratorWithStartAndPrefixRequest) (*rpcdbpb.NewIteratorWithStartAndPrefixResponse, error) {
it := db.db.NewIteratorWithStartAndPrefix(req.Start, req.Prefix)
db.iteratorLock.Lock()
defer db.iteratorLock.Unlock()
id := db.nextIteratorID
db.iterators[id] = it
db.nextIteratorID++
return &rpcdbpb.NewIteratorWithStartAndPrefixResponse{Id: id}, nil
}
// IteratorNext attempts to call next on the requested iterator
func (db *DatabaseServer) IteratorNext(_ context.Context, req *rpcdbpb.IteratorNextRequest) (*rpcdbpb.IteratorNextResponse, error) {
db.iteratorLock.RLock()
it, exists := db.iterators[req.Id]
db.iteratorLock.RUnlock()
if !exists {
return nil, errUnknownIterator
}
size := 0
data := []*rpcdbpb.PutRequest(nil)
for size < maxBatchSize && it.Next() {
key := it.Key()
value := it.Value()
size += len(key) + len(value)
data = append(data, &rpcdbpb.PutRequest{
Key: key,
Value: value,
})
}
return &rpcdbpb.IteratorNextResponse{Data: data}, nil
}
// IteratorError attempts to report any errors that occurred during iteration
func (db *DatabaseServer) IteratorError(_ context.Context, req *rpcdbpb.IteratorErrorRequest) (*rpcdbpb.IteratorErrorResponse, error) {
db.iteratorLock.RLock()
it, exists := db.iterators[req.Id]
db.iteratorLock.RUnlock()
if !exists {
return nil, errUnknownIterator
}
err := it.Error()
return &rpcdbpb.IteratorErrorResponse{Err: errorToErrCode[err]}, errorToRPCError(err)
}
// IteratorRelease attempts to release the resources allocated to an iterator
func (db *DatabaseServer) IteratorRelease(_ context.Context, req *rpcdbpb.IteratorReleaseRequest) (*rpcdbpb.IteratorReleaseResponse, error) {
db.iteratorLock.Lock()
it, exists := db.iterators[req.Id]
if !exists {
db.iteratorLock.Unlock()
return &rpcdbpb.IteratorReleaseResponse{Err: 0}, nil
}
delete(db.iterators, req.Id)
db.iteratorLock.Unlock()
err := it.Error()
it.Release()
return &rpcdbpb.IteratorReleaseResponse{Err: errorToErrCode[err]}, errorToRPCError(err)
}