Flask错误处理程序,回滚mysql异常

时间:2016-07-19 08:23:41

标签: python mysql transactions sqlalchemy flask-sqlalchemy

我有一个Flask应用程序,并且有一个端点/资源使用candidate_device作为orm在FlaskSqlAlchemy表中插入记录。我遇到的问题是,当我在jenkins上运行测试时,jenkins在48个并行线程/进程pytest -n 48 some_service/tests中运行这些测试。大约有13个测试调用所提到的资源并在candidate_device表中创建新记录。并非所有时间,但有时我的测试由于此错误而失败

{"asciTime":"2016-07-19 07:59:43,093", "fileName":"app.py", "logRecordCreationTime":"1468915183.093476", "functionName":"log_exception", "levelNo":"40", "lineNo":"1423", "time":"93", "levelName":"ERROR", "message":"Exception on /v1/candidates/1641/devices [POST]" }#012Traceback (most recent call last):#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/flask_restful/__init__.py", line 268, in error_router#012 return self.handle_error(e)#012 File "/root/base_service_container/candidate_service/common/talent_api.py", line 33, in handle_error#012 app_name, url, user_id, user_email = get_request_info(current_app)#012 File "/root/base_service_container/candidate_service/common/error_handling.py", line 175, in get_request_info#012 user_id = request.user.id#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 237, in __get__#012 return self.impl.get(instance_state(instance), dict_)#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 573, in get#012 value = state._load_expired(state, passive)#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/state.py", line 480, in _load_expired#012 self.manager.deferred_scalar_loader(self, toload)#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 664, in load_scalar_attributes#012 only_load_props=attribute_names)#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 219, in load_on_ident#012 return q.one()#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2473, in one#012 ret = list(self)#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2516, in __iter__#012 return self._execute_and_instances(context)#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2529, in _execute_and_instances#012 close_with_result=True)#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2520, in _connection_from_session#012 **kw)#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 882, in connection#012 execution_options=execution_options)#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 887, in _connection_for_bind#012 engine, execution_options)#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 313, in _connection_for_bind#012 self._assert_active()#012 File "/root/.pyenv/versions/2.7.9/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 214, in _assert_active#012 % self._rollback_exception#012InvalidRequestError: 
This Session's transaction has been rolled back due to a previous exception during 
flush. To begin a new transaction with this Session, first issue Session.rollback().
Original exception was: (_mysql_exceptions.OperationalError) (1205, 'Lock wait timeout exceeded; try restarting transaction') 
[SQL: u'INSERT INTO candidate_device (one_signal_device_id, candidate_id, registered_at_datetime) VALUES (%s, %s, %s)'] [parameters: ('b702183d-4541-4ec0-a977-82d2a18ff681', 1641L, datetime.datetime(2016, 7, 19, 7, 58, 52, 49878))]

Mian错误就是这个

Original exception was: (_mysql_exceptions.OperationalError) (1205, 'Lock wait timeout exceeded; try restarting transaction')

所以我,为什么不添加错误处理程序来捕获此类异常和回滚,以便其他代码不会因先前的异常而失败。

我的想法完全错了。所以我只想问你们哪些人有办法解决这些问题,因为我找不到使用某些SqlAlchemy配置等来修复此错误的解决方案。

非常感谢您的帮助。提前致谢

2 个答案:

答案 0 :(得分:1)

48太高了。早期版本的MySQL在实际放慢速度之前可以处理大约8个版本。最新版本可能能够处理48,但只能使用精心挑选的查询。

innodb_lock_wait_timeout默认为50秒。这听起来像48真的绊倒了。那INSERT应该只需要几分之一秒。

我建议你将48降低到CPU数量或16,以较小者为准。然后小心地抬起它,看看你是否可以提高速度。

如果您想讨论特定查询,请提供

SHOW CREATE TABLE candidate_device;
SHOW VARIABLES LIKE 'innodb%';

有很多方法可以加快INSERTs的速度。您可以通过批量处理轻松获得10倍的改进。这对您的申请是否可行?

答案 1 :(得分:1)

您的问题必须是关于连接限制。根据您的查询执行时间长短,您可以超过此数字(相同的查询并不总是使用相同的时间,这就是有时失败的原因,有时它们不会。)

首先执行此sql查询:

show variables like "max_connections";

此数字是mysql服务器允许的同时连接数。更多的连接需要更多的内存,所以明智地选择它。

如何增加mysql连接限制?很简单,只需转到您的配置文件(通常在/etc/my.cnf,但这取决于您的安装)和属性

max_connections = 200

请注意,虽然您只启动了48个进程,但使用的连接会更多。 SqlAlchemy使用连接池,因此每个进程都将使用多个连接。但同样,sqlalchemy的确切行为会降低您的配置。