記一次奇葩的排錯(cuò)經(jīng)歷(刪除cell)

慎重挪捕!慎重烧栋!照這個(gè)方法刪除cell后并鸵,會(huì)導(dǎo)致已存在的虛擬機(jī)無法獲取詳情!

此文后續(xù) http://www.reibang.com/p/48e1451cf7dc

自從為了配置高可用集群捌蚊,給OpenStack裝了雙控制節(jié)點(diǎn)后集畅,就遇到了一個(gè)奇怪的現(xiàn)象:

cell0.png

所有計(jì)算節(jié)點(diǎn)都出現(xiàn)了兩次!

當(dāng)時(shí)想排查錯(cuò)誤缅糟,但一來沒頭緒挺智,二來任務(wù)緊急,淺淺嘗試一下就放棄排查了窗宦。好在也不影響使用赦颇,就這么用了下去,直到有一天赴涵!

直到有一天媒怯,我運(yùn)行了如下命令:

nova-manage cell_v2 discover_hosts --verbose

看到如下輸出時(shí),突然產(chǎn)生疑惑:

cell1.png

為什么出現(xiàn)了兩次cell髓窜?

好像隱隱預(yù)感到了什么扇苞!

連忙輸入如下命令查看一下:

nova-manage cell_v2 list_cells

見下圖:

cell2.png

果然,有一個(gè)兄弟跟 “正統(tǒng)”的cell1 長(zhǎng)得很像!

它應(yīng)該就是罪魁禍?zhǔn)琢耍?/p>

那么這個(gè)Name是None的兄弟寄纵,是怎么混進(jìn)來的鳖敷?

我一看就明白了,原來它曾經(jīng)也是一個(gè)正統(tǒng)的cell1程拭,畢竟它的數(shù)據(jù)庫配置中還@了曾經(jīng)的controller定踱,可是改朝換代后,原來的單控制節(jié)點(diǎn) controller 由虛擬IP對(duì)應(yīng)的hostname controllerv 代替恃鞋。在配置第二個(gè)控制節(jié)點(diǎn)時(shí)屋吨,我又創(chuàng)了一個(gè)cell1,導(dǎo)致原來的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

結(jié)果還是:

cell4.png

好了绷杜,原來這個(gè)force是假的 https://bugs.launchpad.net/nova/+bug/1721179

不行直秆,我要把你連根拔起:

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

結(jié)果:

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'}]

雖然出錯(cuò)了歇竟!但看到最后一段挥唠,我知道故事已經(jīng)要走到結(jié)局了。

破局的關(guān)鍵就在于這張圖:

cell6.png

簡(jiǎn)單的說焕议,就是我想在 nova_api 數(shù)據(jù)庫的 cell_mappings 表中刪除 uuid88d1334b-8794-4481-9207-aa12dae132ad 的這位仁兄宝磨。

但這位仁兄在cell_mappings 表中的 idinstance_mappings 表中的 cell_id 有“裙帶關(guān)系” !刪不掉盅安!

哦原來是上頭有人唤锉,那就簡(jiǎn)單了!

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

同步下數(shù)據(jù)庫籽慢,重啟下nova服務(wù):

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

又回到最初的起點(diǎn):

openstack compute service list
cell11.png

干凈了!

雖然好像并沒什么用猫胁,但也是時(shí)候?qū)W習(xí)一下nova中關(guān)于cell的知識(shí)了……

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末箱亿,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子弃秆,更是在濱河造成了極大的恐慌届惋,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件菠赚,死亡現(xiàn)場(chǎng)離奇詭異脑豹,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)衡查,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門瘩欺,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拌牲,你說我怎么就攤上這事俱饿。” “怎么了塌忽?”我有些...
    開封第一講書人閱讀 165,747評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵拍埠,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我土居,道長(zhǎng)枣购,這世上最難降的妖魔是什么嬉探? 我笑而不...
    開封第一講書人閱讀 58,939評(píng)論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮坷虑,結(jié)果婚禮上甲馋,老公的妹妹穿的比我還像新娘。我一直安慰自己迄损,他們只是感情好定躏,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著芹敌,像睡著了一般痊远。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上氏捞,一...
    開封第一講書人閱讀 51,737評(píng)論 1 305
  • 那天碧聪,我揣著相機(jī)與錄音,去河邊找鬼液茎。 笑死逞姿,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的捆等。 我是一名探鬼主播滞造,決...
    沈念sama閱讀 40,448評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼栋烤!你這毒婦竟也來了砰盐?” 一聲冷哼從身側(cè)響起眯杏,我...
    開封第一講書人閱讀 39,352評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后滑蚯,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體噪服,經(jīng)...
    沈念sama閱讀 45,834評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡唾那,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評(píng)論 3 338
  • 正文 我和宋清朗相戀三年埠帕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片话侄。...
    茶點(diǎn)故事閱讀 40,133評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡疆虚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出满葛,到底是詐尸還是另有隱情径簿,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評(píng)論 5 346
  • 正文 年R本政府宣布嘀韧,位于F島的核電站篇亭,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏锄贷。R本人自食惡果不足惜译蒂,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評(píng)論 3 331
  • 文/蒙蒙 一曼月、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧柔昼,春花似錦哑芹、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至乙嘀,卻和暖如春末购,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背虎谢。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評(píng)論 1 272
  • 我被黑心中介騙來泰國打工盟榴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人婴噩。 一個(gè)月前我還...
    沈念sama閱讀 48,398評(píng)論 3 373
  • 正文 我出身青樓擎场,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國和親几莽。 傳聞我的和親對(duì)象是個(gè)殘疾皇子迅办,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容