def test_topo_read_threshold(self): before_topo_rtt = vtdb_logger.get_logger().get_topo_rtt() # Check original state. keyspace_obj = topology.get_keyspace('test_keyspace') self.assertNotEqual(keyspace_obj, None, "test_keyspace should be not None") self.assertEqual(keyspace_obj.sharding_col_type, keyrange_constants.KIT_UINT64, "ShardingColumnType be %s" % keyrange_constants.KIT_UINT64) # Change the keyspace object. utils.run_vtctl(['SetKeyspaceShardingInfo', '-force', 'test_keyspace', 'keyspace_id', keyrange_constants.KIT_BYTES]) utils.run_vtctl(['RebuildKeyspaceGraph', 'test_keyspace'], auto_log=True) # sleep throttle interval and check values again. # the keyspace should have changed and also caused a rtt to topo server. time.sleep(self.keyspace_fetch_throttle) topology.refresh_keyspace(self.vtgate_client, 'test_keyspace') keyspace_obj = topology.get_keyspace('test_keyspace') after_1st_clear = vtdb_logger.get_logger().get_topo_rtt() self.assertEqual(after_1st_clear - before_topo_rtt, 1, "One additional round-trips to topo server") self.assertEqual(keyspace_obj.sharding_col_type, keyrange_constants.KIT_BYTES, "ShardingColumnType be %s" % keyrange_constants.KIT_BYTES) # Refresh without sleeping for throttle time shouldn't cause additional rtt. topology.refresh_keyspace(self.vtgate_client, 'test_keyspace') keyspace_obj = topology.get_keyspace('test_keyspace') after_2nd_clear = vtdb_logger.get_logger().get_topo_rtt() self.assertEqual(after_2nd_clear - after_1st_clear, 0, "No additional round-trips to topo server")
def test_topo_read_threshold(self): before_topo_rtt = get_topo_rtt() # Check original state. keyspace_obj = topology.get_keyspace("test_keyspace") self.assertNotEqual(keyspace_obj, None, "test_keyspace should be not None") self.assertEqual( keyspace_obj.sharding_col_type, keyrange_constants.KIT_UINT64, "ShardingColumnType be %s" % keyrange_constants.KIT_UINT64, ) # Change the keyspace object. utils.run_vtctl( ["SetKeyspaceShardingInfo", "-force", "test_keyspace", "keyspace_id", keyrange_constants.KIT_BYTES] ) utils.run_vtctl(["RebuildKeyspaceGraph", "test_keyspace"], auto_log=True) # sleep throttle interval and check values again. # the keyspace should have changed and also caused a rtt to topo server. time.sleep(self.keyspace_fetch_throttle) topology.refresh_keyspace(self.vtgate_client, "test_keyspace") keyspace_obj = topology.get_keyspace("test_keyspace") after_1st_clear = get_topo_rtt() self.assertEqual(after_1st_clear - before_topo_rtt, 1, "One additional round-trips to topo server") self.assertEqual( keyspace_obj.sharding_col_type, keyrange_constants.KIT_BYTES, "ShardingColumnType be %s" % keyrange_constants.KIT_BYTES, ) # Refresh without sleeping for throttle time shouldn't cause additional rtt. topology.refresh_keyspace(self.vtgate_client, "test_keyspace") keyspace_obj = topology.get_keyspace("test_keyspace") after_2nd_clear = get_topo_rtt() self.assertEqual(after_2nd_clear - after_1st_clear, 0, "No additional round-trips to topo server")
def get_db_params_for_tablet_conn(topo_client, keyspace_name, shard, db_type, timeout, encrypted, user, password): db_params_list = [] encrypted_service = 'vts' if encrypted: service = encrypted_service else: service = 'vt' db_key = "%s.%s.%s:%s" % (keyspace_name, shard, db_type, service) # This will read the cached keyspace. keyspace_object = topology.get_keyspace(keyspace_name) # Handle vertical split by checking 'ServedFrom' field. new_keyspace = None served_from = keyspace_object.served_from if served_from is not None: new_keyspace = served_from.get(db_type, None) if new_keyspace is not None: keyspace_name = new_keyspace try: end_points_data = topo_client.get_end_points('local', keyspace_name, shard, db_type) except zkocc.ZkOccError as e: vtdb_logger.get_logger().topo_zkocc_error('do data', db_key, e) return [] except Exception as e: vtdb_logger.get_logger().topo_exception('failed to get or parse topo data', db_key, e) return [] end_points_list = [] host_port_list = [] encrypted_host_port_list = [] if 'Entries' not in end_points_data: vtdb_logger.get_logger().topo_exception('topo server returned: ' + str(end_points_data), db_key, e) raise Exception('zkocc returned: %s' % str(end_points_data)) for entry in end_points_data['Entries']: if service in entry['NamedPortMap']: host_port = (entry['Host'], entry['NamedPortMap'][service], service == 'vts') host_port_list.append(host_port) if encrypted and encrypted_service in entry['NamedPortMap']: host_port = (entry['Host'], entry['NamedPortMap'][encrypted_service], True) encrypted_host_port_list.append(host_port) if encrypted and len(encrypted_host_port_list) > 0: random.shuffle(encrypted_host_port_list) end_points_list = encrypted_host_port_list else: random.shuffle(host_port_list) end_points_list = host_port_list for host, port, encrypted in end_points_list: vt_params = VTConnParams(keyspace_name, shard, db_type, "%s:%s" % (host, port), timeout, encrypted, user, password).__dict__ db_params_list.append(vt_params) return db_params_list
def test_topo_read_threshold(self): before_topo_rtt = vtdb_logger.get_logger().get_topo_rtt() # Check original state. keyspace_obj = topology.get_keyspace('test_keyspace') self.assertNotEqual( keyspace_obj, None, 'test_keyspace should be not None') self.assertEqual( keyspace_obj.sharding_col_type, keyrange_constants.KIT_UINT64, 'ShardingColumnType be %s' % keyrange_constants.KIT_UINT64) # Change the keyspace object. utils.run_vtctl(['SetKeyspaceShardingInfo', '-force', 'test_keyspace', 'keyspace_id', keyrange_constants.KIT_BYTES]) utils.run_vtctl(['RebuildKeyspaceGraph', 'test_keyspace'], auto_log=True) # sleep throttle interval and check values again. # the keyspace should have changed and also caused a rtt to topo server. time.sleep(self.keyspace_fetch_throttle) topology.refresh_keyspace(self.vtgate_client, 'test_keyspace') keyspace_obj = topology.get_keyspace('test_keyspace') after_1st_clear = vtdb_logger.get_logger().get_topo_rtt() self.assertEqual( after_1st_clear - before_topo_rtt, 1, 'One additional round-trips to topo server') self.assertEqual( keyspace_obj.sharding_col_type, keyrange_constants.KIT_BYTES, 'ShardingColumnType be %s' % keyrange_constants.KIT_BYTES) # Refresh without sleeping for throttle time shouldn't cause # additional rtt. topology.refresh_keyspace(self.vtgate_client, 'test_keyspace') keyspace_obj = topology.get_keyspace('test_keyspace') after_2nd_clear = vtdb_logger.get_logger().get_topo_rtt() self.assertEqual( after_2nd_clear - after_1st_clear, 0, 'No additional round-trips to topo server')