forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 5
/
db.go
324 lines (292 loc) · 11.8 KB
/
db.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
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License. See the AUTHORS file
// for names of contributors.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package kv
import (
"bytes"
"encoding/gob"
"net"
"reflect"
"time"
"github.com/cockroachdb/cockroach/gossip"
"github.com/cockroachdb/cockroach/rpc"
"github.com/cockroachdb/cockroach/storage"
"github.com/cockroachdb/cockroach/util"
)
// A DB interface provides asynchronous methods to access a key value store.
type DB interface {
Contains(args *storage.ContainsRequest) <-chan *storage.ContainsResponse
Get(args *storage.GetRequest) <-chan *storage.GetResponse
Put(args *storage.PutRequest) <-chan *storage.PutResponse
Increment(args *storage.IncrementRequest) <-chan *storage.IncrementResponse
Delete(args *storage.DeleteRequest) <-chan *storage.DeleteResponse
DeleteRange(args *storage.DeleteRangeRequest) <-chan *storage.DeleteRangeResponse
Scan(args *storage.ScanRequest) <-chan *storage.ScanResponse
EndTransaction(args *storage.EndTransactionRequest) <-chan *storage.EndTransactionResponse
AccumulateTS(args *storage.AccumulateTSRequest) <-chan *storage.AccumulateTSResponse
ReapQueue(args *storage.ReapQueueRequest) <-chan *storage.ReapQueueResponse
EnqueueUpdate(args *storage.EnqueueUpdateRequest) <-chan *storage.EnqueueUpdateResponse
EnqueueMessage(args *storage.EnqueueMessageRequest) <-chan *storage.EnqueueMessageResponse
}
// GetI fetches the value at the specified key and deserializes it
// into "value". Returns true on success or false if the key was not
// found. The timestamp of the write is returned as the second return
// value. The first result parameter is "ok": true if a value was
// found for the requested key; false otherwise. An error is returned
// on error fetching from underlying storage or deserializing value.
func GetI(db DB, key storage.Key, value interface{}) (bool, int64, error) {
gr := <-db.Get(&storage.GetRequest{Key: key})
if gr.Error != nil {
return false, 0, gr.Error
}
if len(gr.Value.Bytes) == 0 {
return false, 0, nil
}
if err := gob.NewDecoder(bytes.NewBuffer(gr.Value.Bytes)).Decode(value); err != nil {
return true, gr.Value.Timestamp, err
}
return true, gr.Value.Timestamp, nil
}
// PutI sets the given key to the serialized byte string of the value
// provided. Uses current time and default expiration.
func PutI(db DB, key storage.Key, value interface{}) error {
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(value); err != nil {
return err
}
pr := <-db.Put(&storage.PutRequest{
Key: key,
Value: storage.Value{
Bytes: buf.Bytes(),
Timestamp: time.Now().UnixNano(),
},
})
return pr.Error
}
// BootstrapRangeLocations sets meta1 and meta2 values for KeyMax,
// using the provided replica.
func BootstrapRangeLocations(db DB, replica storage.Replica) error {
locations := storage.RangeLocations{
Replicas: []storage.Replica{replica},
// TODO(spencer): uncomment when we have hrsht's change.
//StartKey: storage.KeyMin,
}
// Write meta1.
if err := PutI(db, storage.MakeKey(storage.KeyMeta1Prefix, storage.KeyMax), locations); err != nil {
return err
}
// Write meta2.
if err := PutI(db, storage.MakeKey(storage.KeyMeta2Prefix, storage.KeyMax), locations); err != nil {
return err
}
return nil
}
// UpdateRangeLocations updates the range locations metadata for the
// range specified by the meta parameter. This always involves a write
// to "meta2", and may require a write to "meta1", in the event that
// meta.EndKey is a "meta2" key (prefixed by KeyMeta2Prefix).
func UpdateRangeLocations(db DB, meta storage.RangeMetadata, locations storage.RangeLocations) error {
// TODO(spencer): a lot more work here to actually implement this.
// Write meta2.
if err := PutI(db, storage.MakeKey(storage.KeyMeta2Prefix, meta.EndKey), locations); err != nil {
return err
}
return nil
}
// A DistDB provides methods to access Cockroach's monolithic,
// distributed key value store. Each method invocation triggers a
// lookup or lookups to find replica metadata for implicated key
// ranges.
type DistDB struct {
// gossip provides up-to-date information about the start of the
// key range, used to find the replica metadata for arbitrary key
// ranges.
gossip *gossip.Gossip
// rangeCache caches replica metadata for key ranges. The cache is
// filled while servicing read and write requests to the key value
// store.
rangeCache util.LRUCache
}
// NewDB returns a key-value datastore client which connects to the
// Cockroach cluster via the supplied gossip instance.
func NewDB(gossip *gossip.Gossip) *DistDB {
return &DistDB{gossip: gossip}
}
func (db *DistDB) nodeIDToAddr(nodeID int32) (net.Addr, error) {
nodeIDKey := gossip.MakeNodeIDGossipKey(nodeID)
info, err := db.gossip.GetInfo(nodeIDKey)
if info == nil || err != nil {
return nil, util.Errorf("Unable to lookup address for node: %v. Error: %v", nodeID, err)
}
return info.(net.Addr), nil
}
func (db *DistDB) lookupMetadata(metadataKey storage.Key, replicas []storage.Replica) (*storage.RangeLocations, error) {
replica := storage.ChooseRandomReplica(replicas)
if replica == nil {
return nil, util.Errorf("No replica to choose for metadata key: %q", metadataKey)
}
addr, err := db.nodeIDToAddr(replica.NodeID)
if err != nil {
// TODO(harshit): May be retry a different replica.
return nil, err
}
client := rpc.NewClient(addr)
arg := &storage.InternalRangeLookupRequest{
RequestHeader: storage.RequestHeader{
Replica: *replica,
},
Key: metadataKey,
}
var reply storage.InternalRangeLookupResponse
err = client.Call("Node.InternalRangeLookup", arg, &reply)
if err != nil {
return nil, err
}
if reply.Error != nil {
return nil, reply.Error
}
return &reply.Locations, nil
}
// TODO(harshit): Consider caching returned metadata info.
func (db *DistDB) lookupMeta1(key storage.Key) (*storage.RangeLocations, error) {
info, err := db.gossip.GetInfo(gossip.KeyFirstRangeMetadata)
if err != nil {
return nil, err
}
metadataKey := storage.MakeKey(storage.KeyMeta1Prefix, key)
return db.lookupMetadata(metadataKey, info.(storage.RangeLocations).Replicas)
}
func (db *DistDB) lookupMeta2(key storage.Key) (*storage.RangeLocations, error) {
meta1Val, err := db.lookupMeta1(key)
if err != nil {
return nil, err
}
metadataKey := storage.MakeKey(storage.KeyMeta2Prefix, key)
return db.lookupMetadata(metadataKey, meta1Val.Replicas)
}
// getNode gets an RPC client to the node where the requested
// key is located. The range cache may be updated. The bi-level range
// metadata for the cluster is consulted in the event that the local
// cache doesn't contain range metadata corresponding to the specified
// key.
func (db *DistDB) getNode(key storage.Key) (*rpc.Client, *storage.Replica, error) {
meta2Val, err := db.lookupMeta2(key)
if err != nil {
return nil, nil, err
}
replica := storage.ChooseRandomReplica(meta2Val.Replicas)
if replica == nil {
return nil, nil, util.Errorf("No node found for key: %q", key)
}
addr, err := db.nodeIDToAddr(replica.NodeID)
if err != nil {
// TODO(harshit): May be retry a different replica.
return nil, nil, err
}
return rpc.NewClient(addr), replica, nil
}
// sendRPC sends the specified RPC asynchronously and returns a
// channel which receives the reply struct when the call is
// complete. Returns a channel of the same type as "reply".
func (db *DistDB) sendRPC(key storage.Key, method string, args, reply interface{}) interface{} {
chanVal := reflect.MakeChan(reflect.ChanOf(reflect.BothDir, reflect.TypeOf(reply)), 1)
go func() {
replyVal := reflect.ValueOf(reply)
node, replica, err := db.getNode(key)
if err == nil {
argsHeader := args.(*storage.RequestHeader)
argsHeader.Replica = *replica
err = node.Call(method, args, reply)
}
if err != nil {
// TODO(spencer): check error here; we need to clear this
// segment of range cache and retry getNode() if the range
// wasn't found.
reflect.Indirect(replyVal).FieldByName("Error").Set(reflect.ValueOf(err))
}
chanVal.Send(replyVal)
}()
return chanVal.Interface()
}
// Contains checks for the existence of a key.
func (db *DistDB) Contains(args *storage.ContainsRequest) <-chan *storage.ContainsResponse {
return db.sendRPC(args.Key, "Node.Contains",
args, &storage.ContainsResponse{}).(chan *storage.ContainsResponse)
}
// Get .
func (db *DistDB) Get(args *storage.GetRequest) <-chan *storage.GetResponse {
return db.sendRPC(args.Key, "Node.Get",
args, &storage.GetResponse{}).(chan *storage.GetResponse)
}
// Put .
func (db *DistDB) Put(args *storage.PutRequest) <-chan *storage.PutResponse {
return db.sendRPC(args.Key, "Node.Put",
args, &storage.PutResponse{}).(chan *storage.PutResponse)
}
// Increment .
func (db *DistDB) Increment(args *storage.IncrementRequest) <-chan *storage.IncrementResponse {
return db.sendRPC(args.Key, "Node.Increment",
args, &storage.IncrementResponse{}).(chan *storage.IncrementResponse)
}
// Delete .
func (db *DistDB) Delete(args *storage.DeleteRequest) <-chan *storage.DeleteResponse {
return db.sendRPC(args.Key, "Node.Delete",
args, &storage.DeleteResponse{}).(chan *storage.DeleteResponse)
}
// DeleteRange .
func (db *DistDB) DeleteRange(args *storage.DeleteRangeRequest) <-chan *storage.DeleteRangeResponse {
// TODO(spencer): range of keys.
return db.sendRPC(args.StartKey, "Node.DeleteRange",
args, &storage.DeleteRangeResponse{}).(chan *storage.DeleteRangeResponse)
}
// Scan .
func (db *DistDB) Scan(args *storage.ScanRequest) <-chan *storage.ScanResponse {
// TODO(spencer): range of keys.
return nil
}
// EndTransaction .
func (db *DistDB) EndTransaction(args *storage.EndTransactionRequest) <-chan *storage.EndTransactionResponse {
// TODO(spencer): multiple keys here...
return db.sendRPC(args.Keys[0], "Node.EndTransaction",
args, &storage.EndTransactionResponse{}).(chan *storage.EndTransactionResponse)
}
// AccumulateTS is used to efficiently accumulate a time series of
// int64 quantities representing discrete subtimes. For example, a
// key/value might represent a minute of data. Each would contain 60
// int64 counts, each representing a second.
func (db *DistDB) AccumulateTS(args *storage.AccumulateTSRequest) <-chan *storage.AccumulateTSResponse {
return db.sendRPC(args.Key, "Node.AccumulateTS",
args, &storage.AccumulateTSResponse{}).(chan *storage.AccumulateTSResponse)
}
// ReapQueue scans and deletes messages from a recipient message
// queue. ReapQueueRequest invocations must be part of an extant
// transaction or they fail. Returns the reaped queue messsages, up to
// the requested maximum. If fewer than the maximum were returned,
// then the queue is empty.
func (db *DistDB) ReapQueue(args *storage.ReapQueueRequest) <-chan *storage.ReapQueueResponse {
return db.sendRPC(args.Inbox, "Node.ReapQueue",
args, &storage.ReapQueueResponse{}).(chan *storage.ReapQueueResponse)
}
// EnqueueUpdate enqueues an update for eventual execution.
func (db *DistDB) EnqueueUpdate(args *storage.EnqueueUpdateRequest) <-chan *storage.EnqueueUpdateResponse {
// TODO(spencer): queued updates go to system-reserved keys.
return nil
}
// EnqueueMessage enqueues a message for delivery to an inbox.
func (db *DistDB) EnqueueMessage(args *storage.EnqueueMessageRequest) <-chan *storage.EnqueueMessageResponse {
return db.sendRPC(args.Inbox, "Node.EnqueueMessage",
args, &storage.EnqueueMessageResponse{}).(chan *storage.EnqueueMessageResponse)
}