Skip to content

Commit

Permalink
optimization: 灰度回滚逻辑优化 (closed TencentBlueKing#1893)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyyalt committed Oct 27, 2023
1 parent e8adce0 commit 615e38e
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions apps/core/gray/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,27 @@ def update_host_ap_by_host_ids(

# 切换接入点
update_kwargs: typing.Dict[str, typing.Any] = {"updated_at": timezone.now()}
need_update_host_id: typing.List[int] = [host_node["bk_host_id"] for host_node in partial_host_nodes]
gse_v1_ap_id_is_none_host_count: int = 0
# 若需要更新gse_v1_ap_id使用F方式,顺序不可以变
if rollback:
# 先更新gse_v1_ap_id 为None的主机更新成映射对应的v1_ap_id(需要保证映射为一对一关系)
gse_v1_ap_id_is_none_host_ids: typing.List[int] = list(
node_man_models.Host.objects.filter(
bk_biz_id__in=bk_biz_ids,
bk_host_id__in=need_update_host_id,
gse_v1_ap_id=None,
).values_list("bk_host_id", flat=True)
)

gse_v1_ap_id_is_none_host_count: int = node_man_models.Host.objects.filter(
bk_host_id__in=gse_v1_ap_id_is_none_host_ids
).update(ap_id=v1_ap_id)

need_update_host_id: typing.List[int] = list(
set(need_update_host_id) - set(gse_v1_ap_id_is_none_host_ids)
)
# 更新gse_v1_ap_id不为None的主机
update_kwargs.update(
ap_id=F("gse_v1_ap_id"),
gse_v1_ap_id=None,
Expand All @@ -175,11 +194,12 @@ def update_host_ap_by_host_ids(
)

update_count: int = node_man_models.Host.objects.filter(
bk_biz_id__in=bk_biz_ids, bk_host_id__in=[host_node["bk_host_id"] for host_node in partial_host_nodes]
bk_biz_id__in=bk_biz_ids, bk_host_id__in=need_update_host_id
).update(**update_kwargs)

logger.info(
f"[update_host_ap_by_host_ids][rollback={rollback}] Update count -> {update_count}, "
f"[update_host_ap_by_host_ids][rollback={rollback}] "
f"Update count -> {update_count + gse_v1_ap_id_is_none_host_count}, "
f"expect count -> {len(partial_host_nodes)}"
)

Expand Down Expand Up @@ -309,13 +329,12 @@ def rollback(cls, validated_data: typing.Dict[str, typing.List[typing.Any]]):
is_biz_gray: bool = cls.is_biz_gray(validated_data)

with atomic():
if is_biz_gray:
# 需要先回滚管控区域,确保业务移除灰度列表前能正常判定管控区域所属关系
# 更新管控区域接入点
cls.update_cloud_ap_id(validated_data, rollback=True)
# 需要先回滚管控区域,确保业务移除灰度列表前能正常判定管控区域所属关系
# 更新管控区域接入点
cls.update_cloud_ap_id(validated_data, rollback=True)

# 更新灰度业务范围
cls.update_gray_scope_list(validated_data, rollback=True)
# 更新灰度业务范围, 无论是按业务还是ip回滚都去掉业务灰度标记
cls.update_gray_scope_list(validated_data, rollback=True)

# 更新主机ap
update_result: typing.Dict[str, typing.Any] = cls.update_host_ap(validated_data, is_biz_gray, rollback=True)
Expand Down

0 comments on commit 615e38e

Please sign in to comment.