forked from cockroachdb/cockroach
/
db.go
123 lines (108 loc) · 3.79 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
// 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.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package kv
import (
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/base"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/rpc"
"github.com/cockroachdb/cockroach/util"
"github.com/cockroachdb/cockroach/util/stop"
"github.com/gogo/protobuf/proto"
)
var allExternalMethods = [...]roachpb.Request{
roachpb.Get: &roachpb.GetRequest{},
roachpb.Put: &roachpb.PutRequest{},
roachpb.ConditionalPut: &roachpb.ConditionalPutRequest{},
roachpb.Increment: &roachpb.IncrementRequest{},
roachpb.Delete: &roachpb.DeleteRequest{},
roachpb.DeleteRange: &roachpb.DeleteRangeRequest{},
roachpb.Scan: &roachpb.ScanRequest{},
roachpb.ReverseScan: &roachpb.ReverseScanRequest{},
roachpb.BeginTransaction: &roachpb.BeginTransactionRequest{},
roachpb.EndTransaction: &roachpb.EndTransactionRequest{},
roachpb.AdminSplit: &roachpb.AdminSplitRequest{},
roachpb.AdminMerge: &roachpb.AdminMergeRequest{},
}
// A DBServer provides an HTTP server endpoint serving the key-value API.
// It accepts either JSON or serialized protobuf content types.
type DBServer struct {
context *base.Context
sender client.Sender
stopper *stop.Stopper
}
// NewDBServer allocates and returns a new DBServer.
func NewDBServer(ctx *base.Context, sender client.Sender, stopper *stop.Stopper) *DBServer {
return &DBServer{
context: ctx,
sender: sender,
stopper: stopper,
}
}
// RegisterRPC registers the RPC endpoints.
func (s *DBServer) RegisterRPC(rpcServer *rpc.Server) error {
const method = "Server.Batch"
return rpcServer.Register(method, s.executeCmd, &roachpb.BatchRequest{})
}
// executeCmd interprets the given message as a *roachpb.BatchRequest and sends it
// via the local sender.
func (s *DBServer) executeCmd(argsI proto.Message) (proto.Message, error) {
var br *roachpb.BatchResponse
var err error
f := func() {
ba := argsI.(*roachpb.BatchRequest)
if err = verifyRequest(ba); err != nil {
return
}
var pErr *roachpb.Error
br, pErr = s.sender.Send(context.TODO(), *ba)
if pErr != nil {
br = &roachpb.BatchResponse{}
}
if br.Error != nil {
panic(roachpb.ErrorUnexpectedlySet(s.sender, br))
}
br.Error = pErr
}
if !s.stopper.RunTask(f) {
err = util.Errorf("stopped")
}
return br, err
}
// verifyRequest checks for illegal inputs in request proto and
// returns an error indicating which, if any, were found.
func verifyRequest(ba *roachpb.BatchRequest) error {
for _, reqUnion := range ba.Requests {
req := reqUnion.GetInner()
if et, ok := req.(*roachpb.EndTransactionRequest); ok {
if err := verifyEndTransaction(et); err != nil {
return err
}
}
method := req.Method()
if int(method) >= len(allExternalMethods) || allExternalMethods[method] == nil {
return util.Errorf("Batch contains an internal request %s", method)
}
}
return nil
}
func verifyEndTransaction(req *roachpb.EndTransactionRequest) error {
if req.InternalCommitTrigger != nil {
return util.Errorf("EndTransaction request from external KV API contains commit trigger: %+v", req.InternalCommitTrigger)
}
return nil
}