forked from cockroachdb/cockroach
/
server.go
112 lines (97 loc) · 3.02 KB
/
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
// Copyright 2016 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: Radu Berinde (radu@cockroachlabs.com)
package distsql
import (
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/client"
"github.com/cockroachdb/cockroach/roachpb"
"github.com/cockroachdb/cockroach/sql/parser"
"github.com/pkg/errors"
)
// ServerContext encompasses the configuration required to create a
// DistSQLServer.
type ServerContext struct {
DB *client.DB
}
// ServerImpl implements the server for the distributed SQL APIs.
type ServerImpl struct {
ctx ServerContext
evalCtx parser.EvalContext
}
var _ DistSQLServer = &ServerImpl{}
// NewServer instantiates a DistSQLServer.
func NewServer(ctx ServerContext) *ServerImpl {
ds := &ServerImpl{
ctx: ctx,
evalCtx: parser.EvalContext{
ReCache: parser.NewRegexpCache(512),
},
}
return ds
}
func (ds *ServerImpl) setupTxn(
ctx context.Context,
txnProto *roachpb.Transaction,
) *client.Txn {
txn := client.NewTxn(ctx, *ds.ctx.DB)
// TODO(radu): we should sanity check some of these fields
txn.Proto = *txnProto
return txn
}
// SetupSimpleFlow sets up a simple flow, connecting the simple response output
// stream to the given RowReceiver. The flow is not started.
func (ds *ServerImpl) SetupSimpleFlow(
ctx context.Context, req *SetupFlowsRequest, output RowReceiver,
) (*Flow, error) {
f := &Flow{evalCtx: &ds.evalCtx}
f.txn = ds.setupTxn(ctx, &req.Txn)
f.simpleFlowConsumer = output
flow := req.Flows[0]
// TODO(radu): for now we expect exactly one processor (a table reader).
if len(flow.Processors) != 1 {
return nil, errors.Errorf("only single-processor flows supported")
}
_, err := f.setupProcessor(&flow.Processors[0])
if err != nil {
return nil, err
}
return f, nil
}
// RunSimpleFlow is part of the DistSQLServer interface.
func (ds *ServerImpl) RunSimpleFlow(
req *SetupFlowsRequest, stream DistSQL_RunSimpleFlowServer,
) error {
if len(req.Flows) != 1 {
return errors.Errorf("expected exactly one flow, got %d", len(req.Flows))
}
// Set up the outgoing mailbox for the stream.
mbox := newOutbox(stream)
f, err := ds.SetupSimpleFlow(stream.Context(), req, mbox)
if err != nil {
return err
}
// TODO(radu): this stuff should probably be run through a stopper.
mbox.start(&f.waitGroup)
f.Start()
f.Wait()
return mbox.err
}
// SetupFlows is part of the DistSQLServer interface.
func (ds *ServerImpl) SetupFlows(ctx context.Context, req *SetupFlowsRequest) (
*SimpleResponse, error,
) {
return nil, errors.Errorf("not implemented")
}