44import json
55import logging
66import optparse
7+ import sys
78import time
89from vtproto import topodata_pb2
910from vttest import sharding_utils
10- import sandbox_utils
1111import vtctl_sandbox
1212
1313
@@ -23,24 +23,29 @@ def initial_reparent(keyspace, master_cell, num_shards, namespace, timeout_s):
2323 """Performs the first reparent."""
2424 successfully_reparented = []
2525 master_tablets = {}
26+ start_time = time .time ()
27+ logging .info ('Finding tablets to reparent to.' )
2628 while len (master_tablets ) < num_shards :
29+ if time .time () - start_time > timeout_s :
30+ logging .fatal ('Timed out waiting to find a replica tablet' )
31+ return 1
2732 for shard_name in sharding_utils .get_shard_names (num_shards ):
28- shard_name = sandbox_utils .fix_shard_name (shard_name )
33+ if shard_name in master_tablets :
34+ continue
2935 tablets = vtctl_sandbox .execute_vtctl_command (
30- ['ListShardTablets' , '%s/%s' % (
31- keyspace , sandbox_utils .fix_shard_name (shard_name ))],
36+ ['ListShardTablets' , '%s/%s' % (keyspace , shard_name )],
3237 namespace = namespace )[0 ].split ('\n ' )
3338 tablets = [x .split (' ' ) for x in tablets if x ]
3439 potential_masters = [
3540 x [0 ] for x in tablets if x [3 ] == 'replica'
3641 and x [0 ].split ('-' )[0 ] == master_cell ]
3742 if potential_masters :
3843 master_tablets [shard_name ] = potential_masters [0 ]
44+ logging .info (
45+ '%s selected for shard %s' , potential_masters [0 ], shard_name )
3946
40- start_time = time .time ()
4147 while time .time () - start_time < timeout_s :
4248 for shard_name in sharding_utils .get_shard_names (num_shards ):
43- shard_name = sandbox_utils .fix_shard_name (shard_name )
4449 master_tablet_id = master_tablets [shard_name ]
4550 if is_master (master_tablet_id , namespace ):
4651 logging .info ('Tablet %s is the master of %s/%s.' ,
@@ -55,8 +60,9 @@ def initial_reparent(keyspace, master_cell, num_shards, namespace, timeout_s):
5560 master_tablet_id ], namespace = namespace , timeout_s = 5 )
5661 if len (successfully_reparented ) == num_shards :
5762 logging .info ('Done with initial reparent.' )
58- return
63+ return 0
5964 logging .fatal ('Timed out waiting for initial reparent.' )
65+ return 1
6066
6167
6268def main ():
@@ -73,9 +79,9 @@ def main():
7379 logging .getLogger ().setLevel (logging .INFO )
7480
7581 options , _ = parser .parse_args ()
76- initial_reparent (options .keyspace , options .master_cell ,
77- options .shard_count , options .namespace ,
78- options .timeout )
82+ sys . exit ( initial_reparent (options .keyspace , options .master_cell ,
83+ options .shard_count , options .namespace ,
84+ options .timeout ) )
7985
8086
8187if __name__ == '__main__' :
0 commit comments