问题

在跑 OpenStack functional 功能测试的时候有两个用例过不去。

  • nova.tests.functional.db.test_resource_provider.ResourceClassTestCase.test_create_duplicate_id_retry
  • nova.tests.functional.db.test_resource_provider.ResourceClassTestCase.test_create_duplicate_id_retry_failing

调试定位到问题代码:

# /opt/stack/queens/nova/nova/objects/resource_provider.py

    def create(self):
if 'id' in self:
raise exception.ObjectActionError(action='create',
reason='already created')
if 'name' not in self:
raise exception.ObjectActionError(action='create',
reason='name is required')
if self.name in fields.ResourceClass.STANDARD:
raise exception.ResourceClassExists(resource_class=self.name) if not self.name.startswith(fields.ResourceClass.CUSTOM_NAMESPACE):
raise exception.ObjectActionError(
action='create',
reason='name must start with ' +
fields.ResourceClass.CUSTOM_NAMESPACE) updates = self.obj_get_changes()
# There is the possibility of a race when adding resource classes, as
# the ID is generated locally. This loop catches that exception, and
# retries until either it succeeds, or a different exception is
# encountered.
retries = self.RESOURCE_CREATE_RETRY_COUNT
while retries:
retries -= 1
try:
rc = self._create_in_db(self._context, updates)
self._from_db_object(self._context, self, rc)
break
except db_exc.DBDuplicateEntry as e:
# NOTE: e.columns 为空,所以直接出发后续的异常
if 'id' in e.columns:
# Race condition for ID creation; try again
continue
# The duplication is on the other unique column, 'name'. So do
# not retry; raise the exception immediately.
raise exception.ResourceClassExists(resource_class=self.name)
else:
# We have no idea how common it will be in practice for the retry
# limit to be exceeded. We set it high in the hope that we never
# hit this point, but added this log message so we know that this
# specific situation occurred.
LOG.warning("Exceeded retry limit on ID generation while "
"creating ResourceClass %(name)s",
{'name': self.name})
msg = _("creating resource class %s") % self.name
raise exception.MaxDBRetriesExceeded(action=msg)

继续看 db_exc.DBDuplicateEntry 的实现:

# /usr/lib/python2.7/site-packages/oslo_db/exception.py
class DBDuplicateEntry(DBError):
"""Duplicate entry at unique column error. Raised when made an attempt to write to a unique column the same entry as
existing one. :attr: `columns` available on an instance of the exception
and could be used at error handling:: try:
instance_type_ref.save()
except DBDuplicateEntry as e:
if 'colname' in e.columns:
# Handle error. :kwarg columns: a list of unique columns have been attempted to write a
duplicate entry.
:type columns: list
:kwarg value: a value which has been attempted to write. The value will
be None, if we can't extract it for a particular database backend. Only
MySQL and PostgreSQL 9.x are supported right now.
"""
def __init__(self, columns=None, inner_exception=None, value=None):
# 正常情况下,触发 DBDuplicateEntry 会将冲突的 columns 返回,让开发者得以方便的作出进一步判断
self.columns = columns or []
self.value = value
super(DBDuplicateEntry, self).__init__(inner_exception)

定位到生成冲突 columns 的地方:

# /opt/stack/queens/nova/.tox/functional/lib/python2.7/site-packages/oslo_db/sqlalchemy/exc_filters.py

@filters("sqlite", sqla_exc.IntegrityError,
(r"^.*columns?(?P<columns>[^)]+)(is|are)\s+not\s+unique$",
r"^.*UNIQUE\s+constraint\s+failed:\s+(?P<columns>.+)$",
r"^.*PRIMARY\s+KEY\s+must\s+be\s+unique.*$"))
def _sqlite_dupe_key_error(integrity_error, match, engine_name, is_disconnect):
"""Filter for SQLite duplicate key error. note(boris-42): In current versions of DB backends unique constraint
violation messages follow the structure: sqlite:
1 column - (IntegrityError) column c1 is not unique
N columns - (IntegrityError) column c1, c2, ..., N are not unique sqlite since 3.7.16:
1 column - (IntegrityError) UNIQUE constraint failed: tbl.k1
N columns - (IntegrityError) UNIQUE constraint failed: tbl.k1, tbl.k2 sqlite since 3.8.2:
(IntegrityError) PRIMARY KEY must be unique """
columns = []
# NOTE(ochuprykov): We can get here by last filter in which there are no
# groups. Trying to access the substring that matched by
# the group will lead to IndexError. In this case just
# pass empty list to exception.DBDuplicateEntry
try:
columns = match.group('columns')
columns = [c.split('.')[-1] for c in columns.strip().split(", ")]
except IndexError:
pass raise exception.DBDuplicateEntry(columns, integrity_error)

没有生产冲突 columns 的原因是:「底层 db engine 返回的 string match 不符合上述的匹配规范」。e.g.

2013-05-20 错误:('(sqlite3.IntegrityError) PRIMARY KEY must be unique',)
2019-04-16 正确:('(sqlite3.IntegrityError) UNIQUE constraint failed: resource_classes.id',)

这是一个 SQLite3 版本不匹配导致的问题,但在 Nova 项目中却没有明确的指定 SQLite3 的版本,所以只能手动的修复这一问题。

解决

手动编译升级 SQLite3 的版本:

wget https://www.sqlite.org/2019/sqlite-autoconf-3280000.tar.gz
tar -xvf sqlite-autoconf-3280000.tar.gz
cd sqlite-autoconf-3280000
mkdir /opt/sqlite3
./configure --prefix=/opt/sqlite3
make && make install

升级完 SQLite3 依旧没有直接解决上述问题,这里主要涉及到一个 Python 如何调用 C so 库的问题,这也是解决这个问题的精髓所在。

  • 首先我们找到 SQLite3 Python 客户端(API)的位置
$ python -c "import sqlite3; print(sqlite3.__file__)"
/usr/lib64/python2.7/sqlite3/__init__.pyc
  • 查看 SQLite3 API 实现并找到 so 库导入语句
# /usr/lib64/python2.7/sqlite3/dbapi2.py

from _sqlite3 import *
  • 查找 _sqlite3 so 库的位置
$ python -c 'import _sqlite3; print(_sqlite3)'
<module '_sqlite3' from '/opt/stack/queens/nova/.tox/functional/lib64/python2.7/lib-dynload/_sqlite3.so'>
  • 查看 _sqlite3 so 库内含的动态函数库
$ ldd /opt/stack/queens/nova/.tox/functional/lib64/python2.7/lib-dynload/_sqlite3.so
linux-vdso.so.1 => (0x00007ffc4defb000)
libsqlite3.so.0 => /lib64/libsqlite3.so.0 (0x00007f708ba42000)
libpython2.7.so.1.0 => /lib64/libpython2.7.so.1.0 (0x00007f708b676000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f708b45a000)
libc.so.6 => /lib64/libc.so.6 (0x00007f708b08d000)
libz.so.1 => /lib64/libz.so.1 (0x00007f708ae77000)
libm.so.6 => /lib64/libm.so.6 (0x00007f708ab75000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f708a971000)
libutil.so.1 => /lib64/libutil.so.1 (0x00007f708a76e000)
/lib64/ld-linux-x86-64.so.2 (0x00007f708bf62000)
  • 凭直觉,我们首先关注 libsqlite3.so.0 函数库
$ ls -alh /lib64/libsqlite3.so.0
lrwxrwxrwx. 1 root root 19 May 14 05:13 /lib64/libsqlite3.so.0 -> libsqlite3.so.0.8.6 $ ls -alh /lib64/libsqlite3.so.0.8.6
-rwxr-xr-x. 1 root root 5.1M Jun 4 05:51 /lib64/libsqlite3.so.0.8.6

至此,我们可以想到之所以升级了 SQLite3 的版本但依旧没有解决问题的原因是「Python 程序中调用的动态函数库依旧没有被更新」。所以我们只需要使用新安装的 so 文件替换掉就的就可以解决了。

mv /usr/lib64/libsqlite3.so.0.8.6 /usr/lib64/libsqlite3.so.0.8.6.bk
cp /opt/sqlite3/lib/libsqlite3.so.0.8.6 /usr/lib64/libsqlite3.so.0.8.6

最后

最后贴上 SQLite3 的修改 commit:

This issue is involved this commit, and introduced by version-3.8.2

...
commit eb743f01b125bebd8736ceb2873b69f27721b0ae
Author: D. Richard Hipp <drh@hwaci.com>
Date: Tue Nov 5 13:33:55 2013 +0000 Standardize the error messages generated by constraint failures to a format
of "$TYPE constraint failed: $DETAIL". This involves many changes to the
expected output of test cases.
...

解决这一问题主要的收获是 Python 程序和 C 程序之间的调用关键,如果两者之间并非是通过 TCP 协议来通信,而是通过 so 库文件来调用的话,那么我们需要注意 C 程序在 Linux 操作系统上的文件安装方式。并非单纯的升级了 C 程序就会立马在 Python 程序上生效,还要注意两者之间的桥梁(调用库文件)是否也一同升级了

SQLite 版本引发的 Python 程序调用问题的更多相关文章

  1. python程序调用C/C++代码

    这篇用来记录在些模拟Canoe生成CAN数据桢工具时遇到的问题, 生成CAN数据桢,主要分为两个关注点: 1.如何从can信号名获取到can信号的ID长度以及信号的起始位,并将信号值按照一定的规则填写 ...

  2. Python程序调用摄像头实现人脸识别

    使用简单代码实现摄像头进行在线人脸识别 import cv2 import sys import logging as log import datetime as dt from time impo ...

  3. python笔记:#002#第一个python程序

    第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3​​.x 版本简介 执行 Python 程序的三种方式 解释器 -- python / python ...

  4. 第一个 Python 程序

    ## 目标 * 第一个 `HelloPython` 程序* `Python 2.x` 与 `3​​.x` 版本简介* 执行 `Python` 程序的三种方式 * 解释器 —— `python` / ` ...

  5. 解释器、环境变量、如何运行python程序、变量先定义后引用

    python解释器的介绍.解释器的安装.环境变量的添加为什么加环境变量.如何调取不同的解释器版本实现多版本共存.python程序如何运行的.python的变量定义 一.python解释器: 用来翻译语 ...

  6. PYTHON- 操作系统和python程序

    操作系统基础 应用程序的启动:(重点!!!) python解释器安装,多版本共存 执行python程序的两种方式 运行一个python程序经历的三个阶段(重要) python 的内存管理 ====== ...

  7. 人生苦短之---第一个Python程序

    第一个 Python 程序 目标 第一个 HelloPython 程序 Python 2.x 与 3​​.x 版本简介 执行 Python 程序的三种方式 解释器 —— python / python ...

  8. 在Python程序中调用Java代码的实现

    <原创不易,转载请标明出处:https://www.cnblogs.com/bandaobudaoweng/p/10785766.html> 前言 开发Python程序,需求中需要用到Ja ...

  9. Java调用Python程序

    最近,需要在应用中,需要使用Java程序去调用Python代码,主要有两种实现方式. 一.使用Jython架包 因为调用的Python代码中需要使用Json对象,开始使用jython2.5.2,发现不 ...

随机推荐

  1. Hadoop_03_Hadoop分布式集群搭建

    一:Hadoop集群简介: Hadoop 集群具体来说包含两个集群:HDFS集群和YARN集群,两者逻辑上分离,但物理上常在一起: HDFS集群:负责海量数据的存储,集群中的角色主要有: NameNo ...

  2. 洛谷 P2939 [USACO09FEB]改造路Revamping Trails

    题意翻译 约翰一共有N)个牧场.由M条布满尘埃的小径连接.小径可 以双向通行.每天早上约翰从牧场1出发到牧场N去给奶牛检查身体. 通过每条小径都需要消耗一定的时间.约翰打算升级其中K条小径,使之成为高 ...

  3. 第六章 组件 60 组件切换-应用切换动画和mode方式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  4. 论文参考文献中的M R J意义

    1 期刊作者.题名[J].刊名,出版年,卷(期):起止页码 2 专著作者.书名[M].版本(第一版不著录).出版地:出版者,出版年.起止页码 3 论文集作者.题名[C].//编者.论文集名.出版地:出 ...

  5. Python numpy.ZIP 安装问题

    今天在python上安装numpy,按照网上教程,安装pip,然后命令行直接:pip install numpy  .但是一直因为资源问题下载失败. 后来下载了一个numpy-1.11.2.zip 安 ...

  6. MySQL 将数据文件分布到不同的磁盘

    https://blog.csdn.net/john_chang11/article/details/51783632 [root@test1 temp]# vi /etc/my.cnf [mysql ...

  7. hive中的 lateral view

    lateral view用于和split, explode等UDTF一起使用,它能够将一列数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合. 一个简单的例子,假设我们有一张表pageAds,它有 ...

  8. Oracle 查询对应表所有字段名称,可排除不想要的字段

    由于Oracle varchar类型长度限制为4000 ,以下方法仅支持查询字段拼接后长度小于等于4000 第一种方法 select wm_concat(column_name) from user_ ...

  9. TensorFlow - 深度学习破解验证码 实验

    TensorFlow - 深度学习破解验证码 简介:验证码主要用于防刷,传统的验证码识别算法一般需要把验证码分割为单个字符,然后逐个识别,如果字符之间相互重叠,传统的算法就然并卵了,本文采用cnn对验 ...

  10. mongodb多条件分页查询的三种方法(转)

    一.使用limit和skip进行分页查询 public List<User> pageList(int pageNum ,int pageSize){ List<User> u ...