Skip to content

Commit

Permalink
fix(backend): 支持跨业务污点池转移 & 修复es主机查询问题 #3959
Browse files Browse the repository at this point in the history
  • Loading branch information
iSecloud authored and zhangzhw8 committed Apr 12, 2024
1 parent 1a2e966 commit 839da61
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 33 deletions.
15 changes: 6 additions & 9 deletions dbm-ui/backend/db_dirty/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ def insert_dirty_machines(cls, bk_biz_id: int, bk_host_ids: List[Dict[str, Any]]
"condition": "AND",
"rules": [{"field": "bk_host_id", "operator": "in", "value": bk_host_ids}],
}
dirty_host_infos = CCApi.list_biz_hosts(
dirty_host_infos = CCApi.list_hosts_without_biz(
{
"bk_biz_id": bk_biz_id,
# 默认一次性录入的机器不会超过500
"page": {"start": 0, "limit": 500, "sort": "bk_host_id"},
"host_property_filter": host_property_filter,
Expand All @@ -80,15 +79,13 @@ def insert_dirty_machines(cls, bk_biz_id: int, bk_host_ids: List[Dict[str, Any]]
use_admin=True,
)["info"]

# 获取空闲机模块,资源池模块和污点池模块
system_manage_topo = SystemSettings.get_setting_value(key=SystemSettingsEnum.MANAGE_TOPO.value)
# 获取业务空闲机模块,资源池模块和污点池模块
idle_module = CcManage(bk_biz_id, "").get_biz_internal_module(bk_biz_id)[IDLE_HOST_MODULE]["bk_module_id"]
system_manage_topo = SystemSettings.get_setting_value(key=SystemSettingsEnum.MANAGE_TOPO.value)
resource_module, dirty_module = system_manage_topo["resource_module_id"], system_manage_topo["dirty_module_id"]
# 获取主机的拓扑信息
host_topo_infos = TopoHandler.query_host_set_module(bk_biz_id=bk_biz_id, bk_host_ids=bk_host_ids)[
"hosts_topo_info"
]
# 将污点机器信息转移至污点池模(如果污点机器不在空闲机/资源池,则放弃转移,认为已到正确拓扑)
# 获取主机的拓扑信息(注:这里不能带上业务信息,因为主机可能转移业务)
host_topo_infos = TopoHandler.query_host_set_module(bk_host_ids=bk_host_ids)["hosts_topo_info"]
# 将污点机器信息转移至DBA污点池模(如果污点机器不在空闲机/资源池,则放弃转移,认为已到正确拓扑)
transfer_host_ids = [
info["bk_host_id"]
for info in host_topo_infos
Expand Down
6 changes: 5 additions & 1 deletion dbm-ui/backend/db_dirty/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ class Meta:
swagger_schema_fields = {"example": DIRTY_MACHINE_LIST}


class DeleteDirtyMachineSerializer(serializers.Serializer):
class TransferDirtyMachineSerializer(serializers.Serializer):
bk_host_ids = serializers.ListField(child=serializers.IntegerField(), help_text=_("待转移的主机ID列表"))


class DeleteDirtyMachineSerializer(serializers.Serializer):
bk_host_ids = serializers.ListField(child=serializers.IntegerField(), help_text=_("待删除的污点池记录主机ID"))
21 changes: 19 additions & 2 deletions dbm-ui/backend/db_dirty/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
DeleteDirtyMachineSerializer,
QueryDirtyMachineResponseSerializer,
QueryDirtyMachineSerializer,
TransferDirtyMachineSerializer,
)
from backend.db_meta.models import AppCache
from backend.db_services.ipchooser.query.resource import ResourceQueryHelper
Expand Down Expand Up @@ -85,16 +86,32 @@ def query_operation_list(self, request):

@common_swagger_auto_schema(
operation_summary=_("将污点池主机转移至待回收模块"),
request_body=DeleteDirtyMachineSerializer(),
request_body=TransferDirtyMachineSerializer(),
tags=[SWAGGER_TAG],
)
@action(
detail=False,
methods=["POST"],
url_path="transfer_dirty_machines",
serializer_class=DeleteDirtyMachineSerializer,
serializer_class=TransferDirtyMachineSerializer,
)
def transfer_dirty_machines(self, request):
bk_host_ids = self.params_validate(self.get_serializer_class())["bk_host_ids"]
DBDirtyMachineHandler.transfer_dirty_machines(bk_host_ids)
return Response()

@common_swagger_auto_schema(
operation_summary=_("删除污点池记录"),
request_body=DeleteDirtyMachineSerializer(),
tags=[SWAGGER_TAG],
)
@action(
detail=False,
methods=["DELETE"],
url_path="delete_dirty_records",
serializer_class=DeleteDirtyMachineSerializer,
)
def delete_dirty_records(self, request):
bk_host_ids = self.params_validate(self.get_serializer_class())["bk_host_ids"]
DBDirtyMachineHandler.remove_dirty_machines(bk_host_ids)
return Response()
3 changes: 1 addition & 2 deletions dbm-ui/backend/db_services/bigdata/es/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from django.utils.translation import ugettext_lazy as _

from backend import env
from backend.db_meta.api.cluster.es.detail import scan_cluster
from backend.db_meta.enums import InstanceRole
from backend.db_meta.enums.cluster_type import ClusterType
Expand Down Expand Up @@ -45,7 +44,7 @@ def get_nodes(cls, bk_biz_id: int, cluster_id: int, role: str, keyword: str = No
machines = Machine.objects.filter(bk_host_id__in=storage_instances.values_list("machine", flat=True))

role_host_ids = list(machines.values_list("bk_host_id", flat=True))
return ResourceQueryHelper.search_cc_hosts(env.DBA_APP_BK_BIZ_ID, role_host_ids, keyword)
return ResourceQueryHelper.search_cc_hosts(role_host_ids, keyword)

@classmethod
def get_topo_graph(cls, bk_biz_id: int, cluster_id: int) -> dict:
Expand Down
3 changes: 1 addition & 2 deletions dbm-ui/backend/db_services/bigdata/influxdb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from django.db.models import F, Q, Value
from django.utils.translation import ugettext_lazy as _

from backend import env
from backend.db_meta.enums import ClusterType, InstanceRole
from backend.db_meta.models import Machine
from backend.db_meta.models.group import Group, GroupInstance
Expand Down Expand Up @@ -101,7 +100,7 @@ def list_instances(cls, bk_biz_id: int, query_params: Dict, limit: int, offset:
bk_host_ids = [instance["machine__bk_host_id"] for instance in instances]

# TODO: 页面上所有涉及到补充主机信息的地方需要调整bk_biz_id为dba业务(因为主机最终别转移到了dba业务)
host_infos = ResourceQueryHelper.search_cc_hosts(bk_biz_id=env.DBA_APP_BK_BIZ_ID, role_host_ids=bk_host_ids)
host_infos = ResourceQueryHelper.search_cc_hosts(role_host_ids=bk_host_ids)
host_id__host_map = {host["bk_host_id"]: host for host in host_infos}

# fill restart info
Expand Down
5 changes: 1 addition & 4 deletions dbm-ui/backend/db_services/bigdata/resources/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from django.db.models import Count, F
from django.utils.translation import ugettext_lazy as _

from backend import env
from backend.db_meta.models.cluster import Cluster
from backend.db_meta.models.instance import StorageInstance
from backend.db_proxy.models import ClusterExtension
Expand Down Expand Up @@ -154,9 +153,7 @@ def _to_nodes_list(

cloud_info = ResourceQueryHelper.search_cc_cloud(get_cache=True)
host_id__node_list_map = {node["machine__bk_host_id"]: node for node in node_list}
host_info_map = ResourceQueryHelper.search_cc_hosts(
env.DBA_APP_BK_BIZ_ID, list(host_id__node_list_map.keys()), keyword=None
)
host_info_map = ResourceQueryHelper.search_cc_hosts(list(host_id__node_list_map.keys()), keyword=None)
host_id__host_info_map = {host_info["bk_host_id"]: host_info for host_info in host_info_map}
for host_id, node in host_id__node_list_map.items():
node["bk_host_id"] = node.pop("machine__bk_host_id")
Expand Down
4 changes: 2 additions & 2 deletions dbm-ui/backend/db_services/ipchooser/handlers/topo_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ def query_host_topo_infos(
return {"total": len(hosts_topo_info), "hosts_topo_info": hosts_topo_info}

@classmethod
def query_host_set_module(cls, bk_biz_id: int, bk_host_ids: typing.List) -> typing.Dict:
def query_host_set_module(cls, bk_host_ids: typing.List) -> typing.Dict:
"""
根据过滤条件查询主机的集群ID和模块ID(不支持自定义拓扑主机的查询)
"""
host_topos = CCApi.find_host_biz_relations({"bk_biz_id": bk_biz_id, "bk_host_id": bk_host_ids}, use_admin=True)
host_topos = CCApi.find_host_biz_relations({"bk_host_id": bk_host_ids}, use_admin=True)
host_id__host_topos = defaultdict(lambda: defaultdict(list))
for host_info in host_topos:
host_id__host_topos[host_info["bk_host_id"]]["bk_set_ids"].append(host_info["bk_set_id"])
Expand Down
19 changes: 12 additions & 7 deletions dbm-ui/backend/db_services/ipchooser/query/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,8 @@ def _parse_host_filter(_host: str) -> Dict:

@staticmethod
def search_cc_hosts(
bk_biz_id: int,
role_host_ids: List[int],
bk_biz_id: int = None,
keyword: str = None,
set_filter: Union[str, List] = None,
module_filter: Union[str, List] = None,
Expand Down Expand Up @@ -456,22 +456,27 @@ def _split_keyword(_keyword: str) -> List:
],
}
)
list_biz_hosts_filter = {
"bk_biz_id": bk_biz_id,
list_hosts_filter = {
"fields": constants.CommonEnum.DEFAULT_HOST_FIELDS.value,
"page": {"start": 0, "limit": limit, "sort": "bk_host_innerip"},
"host_property_filter": {"condition": "AND", "rules": rules},
}
if bk_biz_id:
list_hosts_filter.update(bk_biz_id=bk_biz_id)

if set_filter_field:
list_biz_hosts_filter.update({set_filter_field: set_filter})
list_hosts_filter.update({set_filter_field: set_filter})

if module_filter_field:
list_biz_hosts_filter.update({module_filter_field: module_filter})
list_hosts_filter.update({module_filter_field: module_filter})

# 获取主机信息
resp = CCApi.list_biz_hosts(list_biz_hosts_filter, use_admin=True)
hosts = resp["info"]
if bk_biz_id:
resp = CCApi.list_biz_hosts(list_hosts_filter, use_admin=True)
else:
resp = CCApi.list_hosts_without_biz(list_hosts_filter, use_admin=True)

hosts = resp["info"]
ResourceQueryHelper.fill_agent_status(hosts)
ResourceQueryHelper.fill_cloud_name(hosts)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ def get_host_in_authorize(self, host_filter_meta: HostInAuthorizeFilterMeta):
elif not keyword_list or item["ip"] in keyword_list:
ip_whitelist.append({"ip": item["ip"]})

hosts = ResourceQueryHelper.search_cc_hosts(self.bk_biz_id, bk_host_ids, keyword=keyword)
hosts = ResourceQueryHelper.search_cc_hosts(
bk_biz_id=self.bk_biz_id, role_host_ids=bk_host_ids, keyword=keyword
)
return {"hosts": hosts, "ip_whitelist": ip_whitelist}

def authorize_apply(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from django.forms import model_to_dict
from django.utils.translation import ugettext_lazy as _

from backend import env
from backend.db_meta.api.cluster.rediscluster.handler import RedisClusterHandler
from backend.db_meta.api.cluster.tendiscache.handler import TendisCacheClusterHandler
from backend.db_meta.api.cluster.tendispluscluster.handler import TendisPlusClusterHandler
Expand Down Expand Up @@ -83,7 +82,7 @@ def get_nodes(cls, bk_biz_id: int, cluster_id: int, role: str, keyword: str = No
machines = Machine.objects.filter(ip__in=storage_instances.values_list("machine"))

role_host_ids = list(machines.values_list("bk_host_id", flat=True))
return ResourceQueryHelper.search_cc_hosts(env.DBA_APP_BK_BIZ_ID, role_host_ids, keyword)
return ResourceQueryHelper.search_cc_hosts(role_host_ids, keyword)

@classmethod
def _filter_cluster_hook(
Expand Down
2 changes: 1 addition & 1 deletion dbm-ui/backend/ticket/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def get_nodes(self, request, *args, **kwargs):
if not role_host_ids:
return Response([])

hosts = ResourceQueryHelper.search_cc_hosts(env.DBA_APP_BK_BIZ_ID, role_host_ids, keyword)
hosts = ResourceQueryHelper.search_cc_hosts(role_host_ids, keyword)

# 补充实例数,默认为1
for host in hosts:
Expand Down

0 comments on commit 839da61

Please sign in to comment.