forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvtctld_test.py
executable file
·236 lines (193 loc) · 9.21 KB
/
vtctld_test.py
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python
import json
import logging
import os
import socket
import unittest
import urllib2
import re
import environment
import tablet
import utils
from zk import zkocc
from vtctl import vtctl_client
# range "" - 80
shard_0_master = tablet.Tablet()
shard_0_replica = tablet.Tablet()
shard_0_spare = tablet.Tablet()
# range 80 - ""
shard_1_master = tablet.Tablet()
shard_1_replica = tablet.Tablet()
# not assigned
idle = tablet.Tablet()
scrap = tablet.Tablet()
# all tablets
tablets = [shard_0_master, shard_0_replica, shard_1_master, shard_1_replica,
idle, scrap, shard_0_spare]
# vtgate
vtgate_server = None
vtgate_port = None
def setUpModule():
try:
environment.topo_server().setup()
setup_procs = [t.init_mysql() for t in tablets]
utils.Vtctld().start()
utils.wait_procs(setup_procs)
except:
tearDownModule()
raise
def tearDownModule():
if utils.options.skip_teardown:
return
utils.vtgate_kill(vtgate_server)
teardown_procs = [t.teardown_mysql() for t in tablets]
utils.wait_procs(teardown_procs, raise_on_error=False)
environment.topo_server().teardown()
utils.kill_sub_processes()
utils.remove_tmp_files()
for t in tablets:
t.remove_tree()
class TestVtctld(unittest.TestCase):
@classmethod
def setUpClass(klass):
utils.run_vtctl(['CreateKeyspace', 'test_keyspace'])
utils.run_vtctl(['CreateKeyspace',
'--served_from', 'master:test_keyspace,replica:test_keyspace,rdonly:test_keyspace',
'redirected_keyspace'])
shard_0_master.init_tablet( 'master', 'test_keyspace', '-80')
shard_0_replica.init_tablet('spare', 'test_keyspace', '-80')
shard_0_spare.init_tablet( 'spare', 'test_keyspace', '-80')
shard_1_master.init_tablet( 'master', 'test_keyspace', '80-')
shard_1_replica.init_tablet('replica', 'test_keyspace', '80-')
idle.init_tablet('idle')
scrap.init_tablet('idle')
utils.run_vtctl(['RebuildKeyspaceGraph', 'test_keyspace'], auto_log=True)
utils.run_vtctl(['RebuildKeyspaceGraph', 'redirected_keyspace'],
auto_log=True)
# start running all the tablets
for t in [shard_0_master, shard_1_master, shard_1_replica]:
t.create_db('vt_test_keyspace')
t.start_vttablet(wait_for_state=None,
extra_args=utils.vtctld.process_args())
shard_0_replica.create_db('vt_test_keyspace')
shard_0_replica.start_vttablet(extra_args=utils.vtctld.process_args(),
target_tablet_type='replica',
wait_for_state=None)
for t in scrap, idle, shard_0_spare:
t.start_vttablet(wait_for_state=None,
extra_args=utils.vtctld.process_args())
# wait for the right states
for t in [shard_0_master, shard_1_master, shard_1_replica]:
t.wait_for_vttablet_state('SERVING')
for t in [scrap, idle, shard_0_replica, shard_0_spare]:
t.wait_for_vttablet_state('NOT_SERVING')
scrap.scrap()
for t in [shard_0_master, shard_0_replica, shard_0_spare,
shard_1_master, shard_1_replica, idle, scrap]:
t.reset_replication()
utils.run_vtctl(['ReparentShard', '-force', 'test_keyspace/-80',
shard_0_master.tablet_alias], auto_log=True)
utils.run_vtctl(['ReparentShard', '-force', 'test_keyspace/80-',
shard_1_master.tablet_alias], auto_log=True)
shard_0_replica.wait_for_vttablet_state('SERVING')
# run checks now before we start the tablets
utils.validate_topology()
# start a vtgate server too
global vtgate_server, vtgate_port
vtgate_server, vtgate_port = utils.vtgate_start(
cache_ttl='0s', extra_args=utils.vtctld.process_args())
def setUp(self):
self.data = utils.vtctld.dbtopo()
self.serving_data = utils.vtctld.serving_graph()
def _check_all_tablets(self, result):
lines = result.splitlines()
self.assertEqual(len(lines), len(tablets))
line_map = {}
for line in lines:
parts = line.split()
alias = parts[0]
line_map[alias] = parts
for tablet in tablets:
if not tablet.tablet_alias in line_map:
self.assertFalse('tablet %s is not in the result: %s' % (
tablet.tablet_alias, str(line_map)))
def test_vtctl(self):
# standalone RPC client to vtctld
out, err = utils.run_vtctl(['ListAllTablets', 'test_nj'],
mode=utils.VTCTL_VTCTLCLIENT)
self._check_all_tablets(out)
# vtctl querying the topology directly
out, err = utils.run_vtctl(['ListAllTablets', 'test_nj'],
mode=utils.VTCTL_VTCTL,
trap_output=True, auto_log=True)
self._check_all_tablets(out)
# python RPC client to vtctld
out, err = utils.run_vtctl(['ListAllTablets', 'test_nj'],
mode=utils.VTCTL_RPC)
self._check_all_tablets(out)
def test_assigned(self):
logging.debug("test_assigned: %s", str(self.data))
self.assertItemsEqual(self.data["Assigned"].keys(), ["test_keyspace"])
s0 = self.data["Assigned"]["test_keyspace"]['ShardNodes'][0]
self.assertItemsEqual(s0['Name'], "-80")
s1 = self.data["Assigned"]["test_keyspace"]['ShardNodes'][1]
self.assertItemsEqual(s1['Name'], "80-")
def test_not_assigned(self):
self.assertEqual(len(self.data["Idle"]), 1)
self.assertEqual(len(self.data["Scrap"]), 1)
def test_partial(self):
utils.pause("You can now run a browser and connect to http://%s:%u to manually check topology" % (socket.getfqdn(), utils.vtctld.port))
self.assertEqual(self.data["Partial"], True)
def test_explorer_redirects(self):
if environment.topo_server().flavor() != 'zookeeper':
logging.info('Skipping zookeeper tests in topology %s',
environment.topo_server().flavor())
return
base = 'http://localhost:%u' % utils.vtctld.port
self.assertEqual(urllib2.urlopen(base + '/explorers/redirect?type=keyspace&explorer=zk&keyspace=test_keyspace').geturl(),
base + '/zk/global/vt/keyspaces/test_keyspace')
self.assertEqual(urllib2.urlopen(base + '/explorers/redirect?type=shard&explorer=zk&keyspace=test_keyspace&shard=-80').geturl(),
base + '/zk/global/vt/keyspaces/test_keyspace/shards/-80')
self.assertEqual(urllib2.urlopen(base + '/explorers/redirect?type=tablet&explorer=zk&alias=%s' % shard_0_replica.tablet_alias).geturl(),
base + shard_0_replica.zk_tablet_path)
self.assertEqual(urllib2.urlopen(base + '/explorers/redirect?type=srv_keyspace&explorer=zk&keyspace=test_keyspace&cell=test_nj').geturl(),
base + '/zk/test_nj/vt/ns/test_keyspace')
self.assertEqual(urllib2.urlopen(base + '/explorers/redirect?type=srv_shard&explorer=zk&keyspace=test_keyspace&shard=-80&cell=test_nj').geturl(),
base + '/zk/test_nj/vt/ns/test_keyspace/-80')
self.assertEqual(urllib2.urlopen(base + '/explorers/redirect?type=srv_type&explorer=zk&keyspace=test_keyspace&shard=-80&tablet_type=replica&cell=test_nj').geturl(),
base + '/zk/test_nj/vt/ns/test_keyspace/-80/replica')
self.assertEqual(urllib2.urlopen(base + '/explorers/redirect?type=replication&explorer=zk&keyspace=test_keyspace&shard=-80&cell=test_nj').geturl(),
base + '/zk/test_nj/vt/replication/test_keyspace/-80')
def test_serving_graph(self):
self.assertItemsEqual(sorted(self.serving_data.keys()),
["redirected_keyspace", "test_keyspace"])
s0 = self.serving_data["test_keyspace"]['ShardNodes'][0]
self.assertItemsEqual(s0['Name'], "-80")
s1 = self.serving_data["test_keyspace"]['ShardNodes'][1]
self.assertItemsEqual(s1['Name'], "80-")
self.assertItemsEqual(sorted(s0['TabletNodes'].keys()),
["master", "replica"])
self.assertEqual(len(s0['TabletNodes']['master']), 1)
self.assertEqual(self.serving_data["redirected_keyspace"]['ServedFrom']['master'],
'test_keyspace')
def test_tablet_status(self):
# the vttablet that has a health check has a bit more, so using it
shard_0_replica_status = shard_0_replica.get_status()
self.assertTrue(re.search(r'Polling health information from.+MySQLReplicationLag', shard_0_replica_status))
self.assertIn('Alias: <a href="http://localhost:', shard_0_replica_status)
self.assertIn('</html>', shard_0_replica_status)
def test_vtgate(self):
# do a few vtgate topology queries to prime the cache
vtgate_client = zkocc.ZkOccConnection("localhost:%u" % vtgate_port,
"test_nj", 30.0)
vtgate_client.dial()
vtgate_client.get_srv_keyspace_names("test_nj")
vtgate_client.get_srv_keyspace("test_nj", "test_keyspace")
vtgate_client.get_end_points("test_nj", "test_keyspace", "-80", "master")
vtgate_client.close()
status = utils.get_status(vtgate_port)
self.assertIn('</html>', status) # end of page
self.assertIn('/serving_graph/test_nj">test_nj', status) # vtctld link
utils.pause("You can now run a browser and connect to http://%s:%u%s to manually check vtgate status page" % (socket.getfqdn(), vtgate_port, environment.status_url))
if __name__ == '__main__':
utils.main()