I've consolidated the action of bringing up (and down)
a keyspace into keyspace_util. I think many tests can
be simplified if we used this. For now, I've only rewritten
vtgatev3_test to use it.
This commit is contained in:
Sugu Sougoumarane 2015-01-17 21:29:44 -08:00
Родитель 7580bc6841
Коммит 65b688e12d
3 изменённых файлов: 158 добавлений и 100 удалений

80
test/keyspace_util.py Normal file
Просмотреть файл

@ -0,0 +1,80 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# 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.
"""
This module allows you to bring up and tear down keyspaces.
"""
import os
import environment
import tablet
import utils
class TestEnv(object):
def __init__(self):
self.tablet_map={}
def launch(self, keyspace, shards=None, replica_count=0, rdonly_count=0, ddls=None):
self.tablets=[]
utils.run_vtctl(['CreateKeyspace', keyspace])
if not shards or shards[0] == "0":
shards = ["0"]
else:
utils.run_vtctl(['SetKeyspaceShardingInfo', '-force', keyspace, 'keyspace_id', 'uint64'])
for shard in shards:
procs = []
procs.append(self._start_tablet(keyspace, shard, "master", None))
for i in xrange(replica_count):
procs.append(self._start_tablet(keyspace, shard, "replica", i))
for i in xrange(rdonly_count):
procs.append(self._start_tablet(keyspace, shard, "rdonly", i))
utils.wait_procs(procs)
utils.run_vtctl(['RebuildKeyspaceGraph', keyspace], auto_log=True)
for t in self.tablets:
t.create_db('vt_' + keyspace)
t.start_vttablet(
wait_for_state=None,
extra_args=['-queryserver-config-schema-reload-time', '1'],
)
for t in self.tablets:
t.wait_for_vttablet_state('SERVING')
for t in self.tablets:
if t.tablet_type == "master":
utils.run_vtctl(['ReparentShard', '-force', keyspace+'/'+t.shard, t.tablet_alias], auto_log=True)
# Force read-write even if there are no replicas.
utils.run_vtctl(['SetReadWrite', t.tablet_alias], auto_log=True)
utils.run_vtctl(['RebuildKeyspaceGraph', keyspace], auto_log=True)
for ddl in ddls:
fname = os.path.join(environment.tmproot, "ddl.sql")
with open(fname, "w") as f:
f.write(ddl)
utils.run_vtctl(['ApplySchemaKeyspace', '-simple', '-sql-file', fname, keyspace])
def teardown(self):
all_tablets = self.tablet_map.values()
tablet.kill_tablets(all_tablets)
teardown_procs = [t.teardown_mysql() for t in all_tablets]
utils.wait_procs(teardown_procs, raise_on_error=False)
for t in all_tablets:
t.remove_tree()
def _start_tablet(self, keyspace, shard, tablet_type, index):
t = tablet.Tablet()
self.tablets.append(t)
if tablet_type == "master":
key = "%s.%s.%s" %(keyspace, shard, tablet_type)
else:
key = "%s.%s.%s.%s" %(keyspace, shard, tablet_type, index)
self.tablet_map[key] = t
proc = t.init_mysql()
t.init_tablet(tablet_type, keyspace=keyspace, shard=shard)
return proc

Просмотреть файл

@ -196,6 +196,7 @@ def run(cmd, trap_output=False, raise_on_error=True, **kargs):
stdout, stderr = proc.communicate()
if proc.returncode:
if raise_on_error:
pause("cmd fail: %s, pausing..." % (args))
raise TestError('cmd fail:', args, stdout, stderr)
else:
logging.debug('cmd fail: %s %s %s', str(args), stdout, stderr)
@ -320,6 +321,12 @@ def wait_for_vars(name, port, var=None):
break
timeout = wait_step('waiting for /debug/vars of %s' % name, timeout)
def apply_vschema(vschema):
fname = os.path.join(environment.tmproot, "vschema.json")
with open(fname, "w") as f:
f.write(vschema)
run_vtctl(['ApplyVSchema', "-vschema_file", fname])
# vtgate helpers, assuming it always restarts on the same port
def vtgate_start(vtport=None, cell='test_nj', retry_delay=1, retry_count=1,
topo_impl=None, tablet_bson_encrypted=False, cache_ttl='1s',

Просмотреть файл

@ -13,6 +13,7 @@ import unittest
import urllib
import environment
import keyspace_util
import tablet
import utils
@ -20,17 +21,13 @@ from vtdb import cursorv3
from vtdb import dbexceptions
from vtdb import vtgatev3
conn_class = vtgatev3
shard_0_master = tablet.Tablet()
shard_1_master = tablet.Tablet()
lookup_master = tablet.Tablet()
vtgate_server = None
vtgate_port = None
USER_KEYSACE = 'user_keyspace'
LOOKUP_KEYSPACE = 'lookup_keyspace'
keyspace_env = None
create_vt_user = '''create table vt_user (
id bigint,
@ -83,7 +80,7 @@ primary key (music_id)
schema = '''{
"Keyspaces": {
"user_keyspace": {
"user": {
"Sharded": true,
"Vindexes": {
"user_index": {
@ -175,7 +172,7 @@ schema = '''{
"vt_music_extra": "vt_music_extra"
}
},
"lookup_keyspace": {
"lookup": {
"Sharded": false,
"Tables": {
"vt_user_idx": "",
@ -191,92 +188,66 @@ json.loads(schema)
def setUpModule():
global keyspace_env
global shard_0_master
global shard_1_master
global lookup_master
global vtgate_server
global vtgate_port
logging.debug("in setUpModule")
try:
environment.topo_server().setup()
logging.debug("Setting up tablets")
keyspace_env = keyspace_util.TestEnv()
keyspace_env.launch(
"user",
shards=["-80", "80-"],
ddls=[
create_vt_user,
create_vt_user2,
create_vt_user_extra,
create_vt_music,
create_vt_music_extra,
],
)
keyspace_env.launch(
"lookup",
ddls=[
create_vt_user_idx,
create_music_user_map,
create_name_user2_map,
],
)
shard_0_master = keyspace_env.tablet_map["user.-80.master"]
shard_1_master = keyspace_env.tablet_map["user.80-.master"]
lookup_master = keyspace_env.tablet_map["lookup.0.master"]
# start mysql instance external to the test
setup_procs = [shard_0_master.init_mysql(),
shard_1_master.init_mysql(),
lookup_master.init_mysql(),
]
utils.wait_procs(setup_procs)
setup_tablets()
utils.apply_vschema(schema)
vtgate_server, vtgate_port = utils.vtgate_start()
except:
tearDownModule()
raise
def tearDownModule():
global vtgate_server
logging.debug("in tearDownModule")
if utils.options.skip_teardown:
return
logging.debug("Tearing down the servers and setup")
utils.vtgate_kill(vtgate_server)
tablet.kill_tablets([shard_0_master, shard_1_master, lookup_master])
teardown_procs = [shard_0_master.teardown_mysql(),
shard_1_master.teardown_mysql(),
lookup_master.teardown_mysql(),
]
utils.wait_procs(teardown_procs, raise_on_error=False)
keyspace_env.teardown()
environment.topo_server().teardown()
utils.kill_sub_processes()
utils.remove_tmp_files()
shard_0_master.remove_tree()
shard_1_master.remove_tree()
lookup_master.remove_tree()
def setup_tablets():
global vtgate_server
global vtgate_port
# Start up a master mysql and vttablet
logging.debug("Setting up tablets")
utils.run_vtctl(['CreateKeyspace', USER_KEYSACE])
utils.run_vtctl(['CreateKeyspace', LOOKUP_KEYSPACE])
utils.run_vtctl(['SetKeyspaceShardingInfo', '-force', USER_KEYSACE,
'keyspace_id', 'uint64'])
shard_0_master.init_tablet('master', keyspace=USER_KEYSACE, shard='-80')
shard_1_master.init_tablet('master', keyspace=USER_KEYSACE, shard='80-')
lookup_master.init_tablet('master', keyspace=LOOKUP_KEYSPACE, shard='0')
for t in [shard_0_master, shard_1_master]:
t.create_db('vt_user_keyspace')
t.mquery('vt_user_keyspace', create_vt_user)
t.mquery('vt_user_keyspace', create_vt_user2)
t.mquery('vt_user_keyspace', create_vt_user_extra)
t.mquery('vt_user_keyspace', create_vt_music)
t.mquery('vt_user_keyspace', create_vt_music_extra)
t.start_vttablet(wait_for_state='SERVING')
utils.run_vtctl(['SetReadWrite', t.tablet_alias])
lookup_master.create_db('vt_lookup_keyspace')
lookup_master.mquery('vt_lookup_keyspace', create_vt_user_idx)
lookup_master.mquery('vt_lookup_keyspace', create_music_user_map)
lookup_master.mquery('vt_lookup_keyspace', create_name_user2_map)
lookup_master.start_vttablet(wait_for_state='SERVING')
utils.run_vtctl(['SetReadWrite', lookup_master.tablet_alias])
utils.run_vtctl(['RebuildKeyspaceGraph', USER_KEYSACE], auto_log=True)
utils.run_vtctl(['RebuildKeyspaceGraph', LOOKUP_KEYSPACE], auto_log=True)
fname = os.path.join(environment.tmproot, "vschema.json")
with open(fname, "w") as f:
f.write(schema)
utils.run_vtctl(['ApplyVSchema', "-vschema_file", fname])
vtgate_server, vtgate_port = utils.vtgate_start()
def get_connection(user=None, password=None):
global vtgate_port
timeout = 10.0
conn = None
vtgate_addrs = {"_vt": ["localhost:%s" % (vtgate_port),]}
conn = conn_class.connect(vtgate_addrs, timeout,
return vtgatev3.connect("localhost:%s" % (vtgate_port), timeout,
user=user, password=password)
return conn
class TestVTGateFunctions(unittest.TestCase):
@ -319,11 +290,11 @@ class TestVTGateFunctions(unittest.TestCase):
vtgate_conn.commit()
# Verify values in db
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_user")
result = shard_0_master.mquery("vt_user", "select * from vt_user")
self.assertEqual(result, ((1L, 'test 1'), (2L, 'test 2'), (3L, 'test 3')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_user")
result = shard_1_master.mquery("vt_user", "select * from vt_user")
self.assertEqual(result, ((4L, 'test 4'), (6L, 'test 6'), (7L, 'test 7')))
result = lookup_master.mquery("vt_lookup_keyspace", "select * from vt_user_idx")
result = lookup_master.mquery("vt_lookup", "select * from vt_user_idx")
self.assertEqual(result, ((1L,), (2L,), (3L,), (4L,), (6L,), (7L,)))
# Test IN clause
@ -373,9 +344,9 @@ class TestVTGateFunctions(unittest.TestCase):
"master")
self.assertEqual(result, ([], 1L, 0L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_user")
result = shard_0_master.mquery("vt_user", "select * from vt_user")
self.assertEqual(result, ((1L, 'test one'), (2L, 'test 2'), (3L, 'test 3')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_user")
result = shard_1_master.mquery("vt_user", "select * from vt_user")
self.assertEqual(result, ((4L, 'test four'), (6L, 'test 6'), (7L, 'test 7')))
# Test deletes
@ -391,11 +362,11 @@ class TestVTGateFunctions(unittest.TestCase):
"master")
self.assertEqual(result, ([], 1L, 0L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_user")
result = shard_0_master.mquery("vt_user", "select * from vt_user")
self.assertEqual(result, ((2L, 'test 2'), (3L, 'test 3')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_user")
result = shard_1_master.mquery("vt_user", "select * from vt_user")
self.assertEqual(result, ((6L, 'test 6'), (7L, 'test 7')))
result = lookup_master.mquery("vt_lookup_keyspace", "select * from vt_user_idx")
result = lookup_master.mquery("vt_lookup", "select * from vt_user_idx")
self.assertEqual(result, ((2L,), (3L,), (6L,), (7L,)))
def test_user2(self):
@ -418,11 +389,11 @@ class TestVTGateFunctions(unittest.TestCase):
'master')
self.assertEqual(result, ([], 1L, 0L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_user2")
result = shard_0_master.mquery("vt_user", "select * from vt_user2")
self.assertEqual(result, ((1L, 'name1'), (2L, 'name2')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_user2")
result = shard_1_master.mquery("vt_user", "select * from vt_user2")
self.assertEqual(result, ((7L, 'name1'),))
result = lookup_master.mquery("vt_lookup_keyspace", "select * from name_user2_map")
result = lookup_master.mquery("vt_lookup", "select * from name_user2_map")
self.assertEqual(result, (('name1', 1L), ('name1', 7L), ('name2', 2L)))
# Test select by id
@ -455,11 +426,11 @@ class TestVTGateFunctions(unittest.TestCase):
"master")
self.assertEqual(result, ([], 1L, 0L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_user2")
result = shard_0_master.mquery("vt_user", "select * from vt_user2")
self.assertEqual(result, ())
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_user2")
result = shard_1_master.mquery("vt_user", "select * from vt_user2")
self.assertEqual(result, ((7L, 'name1'),))
result = lookup_master.mquery("vt_lookup_keyspace", "select * from name_user2_map")
result = lookup_master.mquery("vt_lookup", "select * from name_user2_map")
self.assertEqual(result, (('name1', 7L),))
def test_user_extra(self):
@ -479,9 +450,9 @@ class TestVTGateFunctions(unittest.TestCase):
i = x+1
result = vtgate_conn._execute("select * from vt_user_extra where user_id = :user_id", {'user_id': i}, 'master')
self.assertEqual(result, ([(i, "test %s" % i)], 1L, 0, [('user_id', 8L), ('email', 253L)]))
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_user_extra")
result = shard_0_master.mquery("vt_user", "select * from vt_user_extra")
self.assertEqual(result, ((1L, 'test 1'), (2L, 'test 2'), (3L, 'test 3')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_user_extra")
result = shard_1_master.mquery("vt_user", "select * from vt_user_extra")
self.assertEqual(result, ((4L, 'test 4'),))
vtgate_conn.begin()
@ -496,9 +467,9 @@ class TestVTGateFunctions(unittest.TestCase):
"master")
self.assertEqual(result, ([], 1L, 0L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_user_extra")
result = shard_0_master.mquery("vt_user", "select * from vt_user_extra")
self.assertEqual(result, ((1L, 'test one'), (2L, 'test 2'), (3L, 'test 3')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_user_extra")
result = shard_1_master.mquery("vt_user", "select * from vt_user_extra")
self.assertEqual(result, ((4L, 'test four'),))
vtgate_conn.begin()
@ -513,9 +484,9 @@ class TestVTGateFunctions(unittest.TestCase):
"master")
self.assertEqual(result, ([], 1L, 0L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_user_extra")
result = shard_0_master.mquery("vt_user", "select * from vt_user_extra")
self.assertEqual(result, ((2L, 'test 2'), (3L, 'test 3')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_user_extra")
result = shard_1_master.mquery("vt_user", "select * from vt_user_extra")
self.assertEqual(result, ())
def test_music(self):
@ -552,11 +523,11 @@ class TestVTGateFunctions(unittest.TestCase):
'master')
self.assertEqual(result, ([], 1L, 8L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_music")
result = shard_0_master.mquery("vt_user", "select * from vt_music")
self.assertEqual(result, ((1L, 1L, 'test 1'), (2L, 2L, 'test 2'), (3L, 3L, 'test 3'), (5L, 6L, 'test 6')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_music")
result = shard_1_master.mquery("vt_user", "select * from vt_music")
self.assertEqual(result, ((4L, 4L, 'test 4'), (6L, 7L, 'test 7'), (6L, 8L, 'test 8')))
result = lookup_master.mquery("vt_lookup_keyspace", "select * from music_user_map")
result = lookup_master.mquery("vt_lookup", "select * from music_user_map")
self.assertEqual(result, ((1L, 1L), (2L, 2L), (3L, 3L), (4L, 4L), (6L, 5L), (7L, 6L), (8L, 6L)))
vtgate_conn.begin()
@ -571,9 +542,9 @@ class TestVTGateFunctions(unittest.TestCase):
"master")
self.assertEqual(result, ([], 1L, 0L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_music")
result = shard_0_master.mquery("vt_user", "select * from vt_music")
self.assertEqual(result, ((1L, 1L, 'test 1'), (2L, 2L, 'test 2'), (3L, 3L, 'test 3'), (5L, 6L, 'test six')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_music")
result = shard_1_master.mquery("vt_user", "select * from vt_music")
self.assertEqual(result, ((4L, 4L, 'test 4'), (6L, 7L, 'test seven'), (6L, 8L, 'test 8')))
vtgate_conn.begin()
@ -588,11 +559,11 @@ class TestVTGateFunctions(unittest.TestCase):
"master")
self.assertEqual(result, ([], 2L, 0L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_music")
result = shard_0_master.mquery("vt_user", "select * from vt_music")
self.assertEqual(result, ((1L, 1L, 'test 1'), (2L, 2L, 'test 2'), (5L, 6L, 'test six')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_music")
result = shard_1_master.mquery("vt_user", "select * from vt_music")
self.assertEqual(result, ((4L, 4L, 'test 4'),))
result = lookup_master.mquery("vt_lookup_keyspace", "select * from music_user_map")
result = lookup_master.mquery("vt_lookup", "select * from music_user_map")
self.assertEqual(result, ((1L, 1L), (2L, 2L), (4L, 4L), (6L, 5L)))
def test_music_extra(self):
@ -612,9 +583,9 @@ class TestVTGateFunctions(unittest.TestCase):
vtgate_conn.commit()
result = vtgate_conn._execute("select * from vt_music_extra where music_id = :music_id", {'music_id': 6}, 'master')
self.assertEqual(result, ([(6L, 5L, "test 6")], 1, 0, [('music_id', 8L), ('user_id', 8L), ('artist', 253L)]))
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_music_extra")
result = shard_0_master.mquery("vt_user", "select * from vt_music_extra")
self.assertEqual(result, ((1L, 1L, 'test 1'), (6L, 5L, 'test 6')))
result = shard_1_master.mquery("vt_user_keyspace", "select * from vt_music_extra")
result = shard_1_master.mquery("vt_user", "select * from vt_music_extra")
self.assertEqual(result, ())
vtgate_conn.begin()
@ -629,7 +600,7 @@ class TestVTGateFunctions(unittest.TestCase):
"master")
self.assertEqual(result, ([], 0L, 0L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_music_extra")
result = shard_0_master.mquery("vt_user", "select * from vt_music_extra")
self.assertEqual(result, ((1L, 1L, 'test 1'), (6L, 5L, 'test six')))
vtgate_conn.begin()
@ -644,7 +615,7 @@ class TestVTGateFunctions(unittest.TestCase):
"master")
self.assertEqual(result, ([], 0L, 0L, []))
vtgate_conn.commit()
result = shard_0_master.mquery("vt_user_keyspace", "select * from vt_music_extra")
result = shard_0_master.mquery("vt_user", "select * from vt_music_extra")
self.assertEqual(result, ((1L, 1L, 'test 1'),))
def test_insert_value_required(self):