记一次奇葩的排错经历(删除cell)

慎重!慎重!照这个方法删除cell后,会导致已存在的虚拟机无法获取详情!

此文后续 https://www.jianshu.com/p/48e1451cf7dc

自从为了配置高可用集群,给OpenStack装了双控制节点后,就遇到了一个奇怪的现象:

cell0.png

所有计算节点都出现了两次!

当时想排查错误,但一来没头绪,二来任务紧急,浅浅尝试一下就放弃排查了。好在也不影响使用,就这么用了下去,直到有一天!

直到有一天,我运行了如下命令:

nova-manage cell_v2 discover_hosts --verbose

看到如下输出时,突然产生疑惑:

cell1.png

为什么出现了两次cell?

好像隐隐预感到了什么!

连忙输入如下命令查看一下:

nova-manage cell_v2 list_cells

见下图:

cell2.png

果然,有一个兄弟跟 “正统”的cell1 长得很像!

它应该就是罪魁祸首了!

那么这个Name是None的兄弟,是怎么混进来的?

我一看就明白了,原来它曾经也是一个正统的cell1,毕竟它的数据库配置中还@了曾经的controller,可是改朝换代后,原来的单控制节点 controller 由虚拟IP对应的hostname controllerv 代替。在配置第二个控制节点时,我又创了一个cell1,导致原来的cell1变成了无名无姓的None

既然你没有用了,就把你删掉吧,于是我:

nova-manage cell_v2 delete_cell --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad

可是:

cell3.png

意思是你根深蒂固,我除不了你了呗?

不行,我又:

nova-manage cell_v2 delete_cell --force --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad

结果还是:

cell4.png

好了,原来这个force是假的 https://bugs.launchpad.net/nova/+bug/1721179

不行,我要把你连根拔起:

nova-manage cell_v2 delete_host --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad --host compute1

结果:

cell5.png

好了好了,惹不起!只能祭出终极武器了,改一下源码。

来到 /usr/lib/python2.7/dist-packages/nova/cmd/manage.py

找到 delete_cell 方法:

@args('--force', action='store_true', default=False,
          help=_('Delete hosts that belong to the cell as well.'))
    @args('--cell_uuid', metavar='<cell_uuid>', dest='cell_uuid',
          required=True, help=_('The uuid of the cell to delete.'))
    def delete_cell(self, cell_uuid, force=False):
        """Delete an empty cell by the given uuid.
        This command will return a non-zero exit code in the following cases.
        * The cell is not found by uuid.
        * It has hosts and force is False.
        * It has instance mappings.
        If force is True and the cell has host, hosts are deleted as well.
        Returns 0 in the following cases.
        * The empty cell is found and deleted successfully.
        * The cell has hosts and force is True and the cell and the hosts are
          deleted successfully.
        """
        ctxt = context.get_admin_context()
        # Find the CellMapping given the uuid.
        try:
            cell_mapping = objects.CellMapping.get_by_uuid(ctxt, cell_uuid)
        except exception.CellMappingNotFound:
            print(_('Cell with uuid %s was not found.') % cell_uuid)
            return 1

        # Check to see if there are any HostMappings for this cell.
        host_mappings = objects.HostMappingList.get_by_cell_id(
            ctxt, cell_mapping.id)
        nodes = []
        if host_mappings:
            if not force:
                print(_('There are existing hosts mapped to cell with uuid '
                        '%s.') % cell_uuid)
                return 2
            # We query for the compute nodes in the cell,
            # so that they can be unmapped.
            with context.target_cell(ctxt, cell_mapping) as cctxt:
                nodes = objects.ComputeNodeList.get_all(cctxt)

        # Check to see if there are any InstanceMappings for this cell.
        instance_mappings = objects.InstanceMappingList.get_by_cell_id(
            ctxt, cell_mapping.id)
        if instance_mappings:
            print(_('There are existing instances mapped to cell with '
                    'uuid %s.') % cell_uuid)
            return 3

        # Unmap the compute nodes so that they can be discovered
        # again in future, if needed.
        for node in nodes:
            node.mapped = 0
            node.save()

        # Delete hosts mapped to the cell.
        for host_mapping in host_mappings:
            host_mapping.destroy()

        # There are no hosts or instances mapped to the cell so delete it.
        cell_mapping.destroy()
        return 0

return 2return 3 的部分注释掉!

直奔主题,即最后一句cell_mapping.destroy()

好了,再删一下:

root@controller:~# nova-manage cell_v2 delete_cell --force --cell_uuid 88d1334b-8794-4481-9207-aa12dae132ad
An error has occurred:
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/nova/cmd/manage.py", line 1868, in main
    ret = fn(*fn_args, **fn_kwargs)
  File "/usr/lib/python2.7/dist-packages/nova/cmd/manage.py", line 1713, in delete_cell
    cell_mapping.destroy()
  File "/usr/lib/python2.7/dist-packages/oslo_versionedobjects/base.py", line 226, in wrapper
    return fn(self, *args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/nova/objects/cell_mapping.py", line 114, in destroy
    self._destroy_in_db(self._context, self.uuid)
  File "/usr/lib/python2.7/dist-packages/oslo_db/sqlalchemy/enginefacade.py", line 979, in wrapper
    return fn(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/nova/objects/cell_mapping.py", line 108, in _destroy_in_db
    uuid=uuid).delete()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 3212, in delete
    delete_op.exec_()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 1179, in exec_
    self._do_exec()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 1363, in _do_exec
    mapper=self.mapper)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1107, in execute
    bind, close_with_result=True).execute(clause, params or {})
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 945, in execute
    return meth(self, multiparams, params)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/sql/elements.py", line 263, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1053, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
    context)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1398, in _handle_dbapi_exception
    util.raise_from_cause(newraise, exc_info)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 470, in do_execute
    cursor.execute(statement, parameters)
  File "/usr/lib/python2.7/dist-packages/pymysql/cursors.py", line 166, in execute
    result = self._query(query)
  File "/usr/lib/python2.7/dist-packages/pymysql/cursors.py", line 322, in _query
    conn.query(q)
  File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 856, in query
    self._affected_rows = self._read_query_result(unbuffered=unbuffered)
  File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 1057, in _read_query_result
    result.read()
  File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 1340, in read
    first_packet = self.connection._read_packet()
  File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 1014, in _read_packet
    packet.check_error()
  File "/usr/lib/python2.7/dist-packages/pymysql/connections.py", line 393, in check_error
    err.raise_mysql_exception(self._data)
  File "/usr/lib/python2.7/dist-packages/pymysql/err.py", line 107, in raise_mysql_exception
    raise errorclass(errno, errval)
DBReferenceError: (pymysql.err.IntegrityError) (1451, u'Cannot delete or update a parent row: a foreign key constraint fails (`nova_api`.`instance_mappings`, CONSTRAINT `instance_mappings_ibfk_1` FOREIGN KEY (`cell_id`) REFERENCES `cell_mappings` (`id`))') [SQL: u'DELETE FROM cell_mappings WHERE cell_mappings.uuid = %(uuid_1)s'] [parameters: {u'uuid_1': u'88d1334b-8794-4481-9207-aa12dae132ad'}]

虽然出错了!但看到最后一段,我知道故事已经要走到结局了。

破局的关键就在于这张图:

cell6.png

简单的说,就是我想在 nova_api 数据库的 cell_mappings 表中删除 uuid88d1334b-8794-4481-9207-aa12dae132ad 的这位仁兄。

但这位仁兄在cell_mappings 表中的 idinstance_mappings 表中的 cell_id 有“裙带关系” !删不掉!

哦原来是上头有人,那就简单了!

mysql -uroot -p
use nova_api;

找一下这位仁兄:

select * from cell_mappings where cell_mappings.uuid = '88d1334b-8794-4481-9207-aa12dae132ad';
cell7.png

找到了,id 是 2 !

然后,删!

delete from instance_mappings where cell_id = '2';
cell8.png

再删!

delete from cell_mappings where cell_mappings.uuid = '88d1334b-8794-4481-9207-aa12dae132ad';
cell9.png

好了,赶紧看一下:

nova-manage cell_v2 list_cells
cell10.png

同步下数据库,重启下nova服务:

nova-manage api_db sync
service nova-api restart
service nova-consoleauth restart
service nova-scheduler restart
service nova-conductor restart
service nova-novncproxy restart

又回到最初的起点:

openstack compute service list
cell11.png

干净了!

虽然好像并没什么用,但也是时候学习一下nova中关于cell的知识了……

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,258评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,335评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,225评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,126评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,140评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,098评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,018评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,857评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,298评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,518评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,678评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,400评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,993评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,638评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,801评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,661评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,558评论 2 352

推荐阅读更多精彩内容