forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gateway.go
106 lines (83 loc) · 4.78 KB
/
gateway.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
// Copyright 2016, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package gateway contains the routing layer of vtgate. A Gateway can take
// a query targeted to a keyspace/shard/tablet_type and send it off.
package gateway
import (
"flag"
log "github.com/golang/glog"
"golang.org/x/net/context"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/vt/discovery"
"github.com/youtube/vitess/go/vt/tabletserver/querytypes"
"github.com/youtube/vitess/go/vt/topo"
querypb "github.com/youtube/vitess/go/vt/proto/query"
topodatapb "github.com/youtube/vitess/go/vt/proto/topodata"
)
// This file contains the Gateway interface definition, and the
// implementations registry.
var (
// implementation controls the implementation of Gateway.
implementation = flag.String("gateway_implementation", "discoverygateway", "The implementation of gateway")
)
// A Gateway is the query processing module for each shard,
// which is used by ScatterConn.
type Gateway interface {
// Execute executes the non-streaming query for the specified keyspace, shard, and tablet type.
Execute(ctx context.Context, keyspace, shard string, tabletType topodatapb.TabletType, query string, bindVars map[string]interface{}, transactionID int64) (*sqltypes.Result, error)
// ExecuteBatch executes a group of queries for the specified keyspace, shard, and tablet type.
ExecuteBatch(ctx context.Context, keyspace, shard string, tabletType topodatapb.TabletType, queries []querytypes.BoundQuery, asTransaction bool, transactionID int64) ([]sqltypes.Result, error)
// StreamExecute executes a streaming query for the specified keyspace, shard, and tablet type.
StreamExecute(ctx context.Context, keyspace, shard string, tabletType topodatapb.TabletType, query string, bindVars map[string]interface{}) (sqltypes.ResultStream, error)
// Begin starts a transaction for the specified keyspace, shard, and tablet type.
// It returns the transaction ID.
Begin(ctx context.Context, keyspace string, shard string, tabletType topodatapb.TabletType) (int64, error)
// Commit commits the current transaction for the specified keyspace, shard, and tablet type.
Commit(ctx context.Context, keyspace, shard string, tabletType topodatapb.TabletType, transactionID int64) error
// Rollback rolls back the current transaction for the specified keyspace, shard, and tablet type.
Rollback(ctx context.Context, keyspace, shard string, tabletType topodatapb.TabletType, transactionID int64) error
// BeginExecute executes a begin and the non-streaming query
// for the specified keyspace, shard, and tablet type.
BeginExecute(ctx context.Context, keyspace, shard string, tabletType topodatapb.TabletType, query string, bindVars map[string]interface{}) (*sqltypes.Result, int64, error)
// BeginExecuteBatch executes a begin and a group of queries
// for the specified keyspace, shard, and tablet type.
BeginExecuteBatch(ctx context.Context, keyspace, shard string, tabletType topodatapb.TabletType, queries []querytypes.BoundQuery, asTransaction bool) ([]sqltypes.Result, int64, error)
// SplitQuery splits a query into sub-queries for the specified keyspace, shard, and tablet type.
SplitQuery(ctx context.Context, keyspace, shard string, tabletType topodatapb.TabletType, sql string, bindVariables map[string]interface{}, splitColumn string, splitCount int64) ([]querytypes.QuerySplit, error)
// SplitQuery splits a query into sub-queries for the specified keyspace, shard, and tablet type.
// TODO(erez): Rename to SplitQuery after migration to SplitQuery V2.
SplitQueryV2(
ctx context.Context,
keyspace,
shard string,
tabletType topodatapb.TabletType,
sql string,
bindVariables map[string]interface{},
splitColumns []string,
splitCount int64,
numRowsPerQueryPart int64,
algorithm querypb.SplitQueryRequest_Algorithm) ([]querytypes.QuerySplit, error)
// Close shuts down underlying connections.
Close(ctx context.Context) error
// CacheStatus returns a list of TabletCacheStatus per tablet.
CacheStatus() TabletCacheStatusList
}
// Creator is the factory method which can create the actual gateway object.
type Creator func(hc discovery.HealthCheck, topoServer topo.Server, serv topo.SrvTopoServer, cell string, retryCount int, tabletTypesToWait []topodatapb.TabletType) Gateway
var creators = make(map[string]Creator)
// RegisterCreator registers a Creator with given name.
func RegisterCreator(name string, gc Creator) {
if _, ok := creators[name]; ok {
log.Fatalf("Gateway %s already exists", name)
}
creators[name] = gc
}
// GetCreator returns the Creator specified by the gateway_implementation flag.
func GetCreator() Creator {
gc, ok := creators[*implementation]
if !ok {
log.Fatalf("No gateway registered as %s", *implementation)
}
return gc
}