forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.go
121 lines (102 loc) · 3.27 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
113
114
115
116
117
118
119
120
121
/*
Copyright 2017 Google Inc.
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.
*/
package framework
import (
"fmt"
"net"
"net/http"
"time"
"vitess.io/vitess/go/vt/vterrors"
"golang.org/x/net/context"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/vtgate/fakerpcvtgateconn"
"vitess.io/vitess/go/vt/vtgate/vtgateconn"
"vitess.io/vitess/go/vt/vttablet/tabletserver"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
querypb "vitess.io/vitess/go/vt/proto/query"
topodatapb "vitess.io/vitess/go/vt/proto/topodata"
)
var (
// Target is the target info for the server.
Target querypb.Target
// Server is the TabletServer for the framework.
Server *tabletserver.TabletServer
// ServerAddress is the http URL for the server.
ServerAddress string
// ResolveChan is the channel that sends dtids that are to be resolved.
ResolveChan = make(chan string, 1)
)
// StartServer starts the server and initializes
// all the global variables. This function should only be called
// once at the beginning of the test.
func StartServer(connParams, connAppDebugParams mysql.ConnParams, dbName string) error {
// Setup a fake vtgate server.
protocol := "resolveTest"
*vtgateconn.VtgateProtocol = protocol
vtgateconn.RegisterDialer(protocol, func(context.Context, string) (vtgateconn.Impl, error) {
return &txResolver{
FakeVTGateConn: fakerpcvtgateconn.FakeVTGateConn{},
}, nil
})
dbcfgs := dbconfigs.NewTestDBConfigs(connParams, connAppDebugParams, dbName)
config := tabletenv.DefaultQsConfig
config.EnableAutoCommit = true
config.StrictTableACL = true
config.TwoPCEnable = true
config.TwoPCAbandonAge = 1
config.TwoPCCoordinatorAddress = "fake"
config.EnableHotRowProtection = true
Target = querypb.Target{
Keyspace: "vttest",
Shard: "0",
TabletType: topodatapb.TabletType_MASTER,
}
Server = tabletserver.NewTabletServerWithNilTopoServer(config)
Server.Register()
err := Server.StartService(Target, dbcfgs)
if err != nil {
return vterrors.Wrap(err, "could not start service")
}
// Start http service.
ln, err := net.Listen("tcp", ":0")
if err != nil {
return vterrors.Wrap(err, "could not start listener")
}
ServerAddress = fmt.Sprintf("http://%s", ln.Addr().String())
go http.Serve(ln, nil)
for {
time.Sleep(10 * time.Millisecond)
response, err := http.Get(fmt.Sprintf("%s/debug/vars", ServerAddress))
if err == nil {
response.Body.Close()
break
}
}
return nil
}
// StopServer must be called once all the tests are done.
func StopServer() {
Server.StopService()
}
// txReolver transmits dtids to be resolved through ResolveChan.
type txResolver struct {
fakerpcvtgateconn.FakeVTGateConn
}
func (conn *txResolver) ResolveTransaction(ctx context.Context, dtid string) error {
select {
case ResolveChan <- dtid:
default:
}
return nil
}