forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
133 lines (118 loc) · 3.42 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
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2015, 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 framework
import (
"fmt"
"net"
"net/http"
"os"
"path"
"time"
"github.com/youtube/vitess/go/sqldb"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/mysqlctl"
"github.com/youtube/vitess/go/vt/proto/query"
"github.com/youtube/vitess/go/vt/proto/topodata"
"github.com/youtube/vitess/go/vt/tabletserver"
"github.com/youtube/vitess/go/vt/vttest"
)
var (
// BaseConfig is the base config of the default server.
BaseConfig tabletserver.Config
// Target is the target info for the default server.
Target query.Target
// DefaultServer is the default server.
DefaultServer *tabletserver.TabletServer
// ServerAddress is the http URL for the default server.
ServerAddress string
)
// StartDefaultServer starts the default server and initializes
// all the global variables. This function should only be called
// once at the beginning of the test.
func StartDefaultServer(connParams sqldb.ConnParams) error {
dbcfgs := dbconfigs.DBConfigs{
App: dbconfigs.DBConfig{
ConnParams: connParams,
Keyspace: "vttest",
Shard: "0",
EnableRowcache: true,
EnableInvalidator: false,
},
}
mysqld := mysqlctl.NewMysqld(
"Dba",
"App",
&mysqlctl.Mycnf{},
&dbcfgs.Dba,
&dbcfgs.App.ConnParams,
&dbcfgs.Repl)
BaseConfig = tabletserver.DefaultQsConfig
BaseConfig.RowCache.Binary = vttest.MemcachedPath()
BaseConfig.RowCache.Socket = path.Join(os.TempDir(), "memcache.sock")
BaseConfig.RowCache.Connections = 100
BaseConfig.EnableAutoCommit = true
BaseConfig.StrictTableAcl = true
Target = query.Target{
Keyspace: "vttest",
Shard: "0",
TabletType: topodata.TabletType_MASTER,
}
DefaultServer = tabletserver.NewTabletServer(BaseConfig)
DefaultServer.Register()
err := DefaultServer.StartService(&Target, &dbcfgs, nil, mysqld)
if err != nil {
return fmt.Errorf("could not start service: %v\n", err)
}
// Start http service.
ln, err := net.Listen("tcp", ":0")
if err != nil {
return fmt.Errorf("could not start listener: %v\n", err)
}
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
}
// StopDefaultServer must be called once all the tests are done.
func StopDefaultServer() {
DefaultServer.StopService()
}
// NewServer starts a new unregistered TabletServer.
// You have to end it with StopService once testing is done.
func NewServer(config tabletserver.Config, connParams sqldb.ConnParams, enableRowcache bool) (*tabletserver.TabletServer, error) {
dbcfgs := dbconfigs.DBConfigs{
App: dbconfigs.DBConfig{
ConnParams: connParams,
Keyspace: "vttest",
Shard: "0",
EnableRowcache: enableRowcache,
EnableInvalidator: false,
},
}
mysqld := mysqlctl.NewMysqld(
"Dba",
"App",
&mysqlctl.Mycnf{},
&dbcfgs.Dba,
&dbcfgs.App.ConnParams,
&dbcfgs.Repl)
Target = query.Target{
Keyspace: "vttest",
Shard: "0",
TabletType: topodata.TabletType_MASTER,
}
server := tabletserver.NewTabletServer(config)
err := server.StartService(&Target, &dbcfgs, nil, mysqld)
if err != nil {
return nil, err
}
return server, nil
}