Skip to content

Commit 1cd6694

Browse files
committed
add support for --store=type=mem,size=val when start a server
This commit sets `--store=type=mem` (storing database in memory) as the default option for storage. We also allow users to customize the proportion of available memory allocated to the test server.
1 parent 0bcb536 commit 1cd6694

File tree

2 files changed

+78
-12
lines changed

2 files changed

+78
-12
lines changed

testserver/testserver.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ import (
5656
"testing"
5757
"time"
5858

59+
"github.com/cockroachdb/cockroach-go/v2/testserver/version"
5960
// Import postgres driver.
6061
_ "github.com/lib/pq"
61-
62-
"github.com/cockroachdb/cockroach-go/v2/testserver/version"
6362
)
6463

6564
var customBinaryFlag = flag.String("cockroach-binary", "", "Use specified cockroach binary")
@@ -77,6 +76,9 @@ const (
7776
firstTenantID = 2
7877
)
7978

79+
// By default, we allocate 20% of available memory to the test server.
80+
const defaultStoreMemSize = 0.2
81+
8082
// TestServer is a helper to run a real cockroach node.
8183
type TestServer interface {
8284
// Start starts the server.
@@ -166,8 +168,10 @@ func NewDBForTestWithDatabase(
166168
type TestServerOpt func(args *testServerArgs)
167169

168170
type testServerArgs struct {
169-
secure bool
170-
rootPW string // if nonempty, set as pw for root
171+
secure bool
172+
rootPW string // if nonempty, set as pw for root
173+
storeOnDisk bool // to save database in disk
174+
storeMemSize float64 // the proportion of available memory allocated to test server
171175
}
172176

173177
// SecureOpt is a TestServer option that can be passed to NewTestServer to
@@ -178,6 +182,27 @@ func SecureOpt() TestServerOpt {
178182
}
179183
}
180184

185+
// StoreOnDiskOpt is a TestServer option that can be passed to NewTestServer
186+
// to enable storing database in memory.
187+
func StoreOnDiskOpt() TestServerOpt {
188+
return func(args *testServerArgs) {
189+
args.storeOnDisk = true
190+
}
191+
}
192+
193+
// SetStoreMemSizeOpt is a TestServer option that can be passed to NewTestServer
194+
// to set the proportion of available memory that is allocated
195+
// to the test server.
196+
func SetStoreMemSizeOpt(memSize float64) TestServerOpt {
197+
return func(args *testServerArgs) {
198+
if memSize > 0 {
199+
args.storeMemSize = memSize
200+
} else {
201+
args.storeMemSize = defaultStoreMemSize
202+
}
203+
}
204+
}
205+
181206
// RootPasswordOpt is a TestServer option that, when passed to NewTestServer,
182207
// sets the given password for the root user (and returns a URL using it from
183208
// PGURL(). This avoids having to use client certs.
@@ -200,6 +225,7 @@ const (
200225
// found in your path.
201226
func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
202227
serverArgs := &testServerArgs{}
228+
serverArgs.storeMemSize = defaultStoreMemSize
203229
for _, applyOptToArgs := range opts {
204230
applyOptToArgs(serverArgs)
205231
}
@@ -236,6 +262,9 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
236262
}
237263
return path, nil
238264
}
265+
// TODO(janexing): Make sure the log is written to logDir instead of shown in console.
266+
// Should be done once issue #109 is solved:
267+
// https://github.com/cockroachdb/cockroach-go/issues/109
239268
logDir, err := mkDir(logsDirName)
240269
if err != nil {
241270
return nil, err
@@ -292,6 +321,13 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
292321
startCmd = "start"
293322
}
294323

324+
var storeArg string
325+
if serverArgs.storeOnDisk {
326+
storeArg = "--store=path=" + baseDir
327+
} else {
328+
storeArg = fmt.Sprintf("--store=type=mem,size=%.2f", serverArgs.storeMemSize)
329+
}
330+
295331
args := []string{
296332
cockroachBinary,
297333
startCmd,
@@ -300,7 +336,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
300336
"--host=localhost",
301337
"--port=0",
302338
"--http-port=0",
303-
"--store=" + baseDir,
339+
storeArg,
304340
"--listening-url-file=" + listeningURLFile,
305341
}
306342

testserver/testserver_test.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424

2525
const noPW = ""
2626

27+
const defStoreMemSize = 0.2
28+
2729
func TestRunServer(t *testing.T) {
2830
const testPW = "foobar"
2931
for _, tc := range []struct {
@@ -34,6 +36,12 @@ func TestRunServer(t *testing.T) {
3436
name: "Insecure",
3537
instantiation: func(t *testing.T) (*sql.DB, func()) { return testserver.NewDBForTest(t) },
3638
},
39+
{
40+
name: "InsecureWithCustomizedMemSize",
41+
instantiation: func(t *testing.T) (*sql.DB, func()) {
42+
return testserver.NewDBForTest(t, testserver.SetStoreMemSizeOpt(0.3))
43+
},
44+
},
3745
{
3846
name: "SecureClientCert",
3947
instantiation: func(t *testing.T) (*sql.DB, func()) { return testserver.NewDBForTest(t, testserver.SecureOpt()) },
@@ -44,34 +52,46 @@ func TestRunServer(t *testing.T) {
4452
return testserver.NewDBForTest(t, testserver.SecureOpt(), testserver.RootPasswordOpt(testPW))
4553
},
4654
},
55+
{
56+
name: "InsecureTenantStoreOnDisk",
57+
instantiation: func(t *testing.T) (*sql.DB, func()) {
58+
return testserver.NewDBForTest(t, testserver.StoreOnDiskOpt())
59+
},
60+
},
61+
{
62+
name: "SecureTenantStoreOnDisk",
63+
instantiation: func(t *testing.T) (*sql.DB, func()) {
64+
return testserver.NewDBForTest(t, testserver.SecureOpt(), testserver.StoreOnDiskOpt())
65+
},
66+
},
4767
{
4868
name: "InsecureTenant",
4969
instantiation: func(t *testing.T) (*sql.DB, func()) {
50-
return newTenantDBForTest(t, false /* secure */, false /* proxy */, noPW)
70+
return newTenantDBForTest(t, false /* secure */, false /* proxy */, noPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
5171
},
5272
},
5373
{
5474
name: "SecureTenant",
5575
instantiation: func(t *testing.T) (*sql.DB, func()) {
56-
return newTenantDBForTest(t, true /* secure */, false /* proxy */, noPW)
76+
return newTenantDBForTest(t, true /* secure */, false /* proxy */, noPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
5777
},
5878
},
5979
{
6080
name: "SecureTenantCustomPassword",
6181
instantiation: func(t *testing.T) (*sql.DB, func()) {
62-
return newTenantDBForTest(t, true /* secure */, false /* proxy */, testPW)
82+
return newTenantDBForTest(t, true /* secure */, false /* proxy */, testPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
6383
},
6484
},
6585
{
6686
name: "SecureTenantThroughProxy",
6787
instantiation: func(t *testing.T) (*sql.DB, func()) {
68-
return newTenantDBForTest(t, true /* secure */, true /* proxy */, noPW)
88+
return newTenantDBForTest(t, true /* secure */, true /* proxy */, noPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
6989
},
7090
},
7191
{
7292
name: "SecureTenantThroughProxyCustomPassword",
7393
instantiation: func(t *testing.T) (*sql.DB, func()) {
74-
return newTenantDBForTest(t, true /* secure */, true /* proxy */, testPW)
94+
return newTenantDBForTest(t, true /* secure */, true /* proxy */, testPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
7595
},
7696
},
7797
} {
@@ -105,15 +125,25 @@ type tenantInterface interface {
105125
// newTenantDBForTest is a testing helper function that starts a TestServer
106126
// process and a SQL tenant process pointed at this TestServer. A sql connection
107127
// to the tenant and a cleanup function are returned.
108-
func newTenantDBForTest(t *testing.T, secure bool, proxy bool, pw string) (*sql.DB, func()) {
128+
func newTenantDBForTest(
129+
t *testing.T, secure bool, proxy bool, pw string, diskStore bool, storeMemSize float64,
130+
) (*sql.DB, func()) {
109131
t.Helper()
110132
var opts []testserver.TestServerOpt
111133
if secure {
112134
opts = append(opts, testserver.SecureOpt())
113135
}
136+
if diskStore {
137+
opts = append(opts, testserver.StoreOnDiskOpt())
138+
}
114139
if pw != "" {
115140
opts = append(opts, testserver.RootPasswordOpt(pw))
116141
}
142+
if storeMemSize >= 0 {
143+
opts = append(opts, testserver.SetStoreMemSizeOpt(storeMemSize))
144+
} else {
145+
t.Fatal("Percentage memory size for data storage cannot be nagative")
146+
}
117147
ts, err := testserver.NewTestServer(opts...)
118148
if err != nil {
119149
t.Fatal(err)
@@ -138,7 +168,7 @@ func newTenantDBForTest(t *testing.T, secure bool, proxy bool, pw string) (*sql.
138168
}
139169

140170
func TestTenant(t *testing.T) {
141-
db, stop := newTenantDBForTest(t, false /* secure */, false /* proxy */, noPW)
171+
db, stop := newTenantDBForTest(t, false /* secure */, false /* proxy */, noPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
142172
defer stop()
143173
if _, err := db.Exec("SELECT 1"); err != nil {
144174
t.Fatal(err)

0 commit comments

Comments
 (0)