Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(backend): 删除query_clusters方法 #9115 #9116

Open
wants to merge 1 commit into
base: v1.5.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 2 additions & 97 deletions dbm-ui/backend/db_services/mysql/cluster/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,117 +8,22 @@
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
import copy
from collections import defaultdict
from typing import Any, Dict, List

from django.db.models import Q, Value
from django.db.models.query import QuerySet
from django.forms import model_to_dict

from backend.db_meta.enums import ClusterType, InstanceInnerRole, InstanceRole, TenDBClusterSpiderRole
from backend.db_meta.models import Cluster, DBModule, ProxyInstance, StorageInstance
from backend.db_meta.enums import ClusterType, InstanceRole, TenDBClusterSpiderRole
from backend.db_meta.models import Cluster, ProxyInstance, StorageInstance
from backend.db_services.dbbase.cluster.handlers import ClusterServiceHandler as BaseClusterServiceHandler
from backend.db_services.dbbase.dataclass import DBInstance
from backend.db_services.mysql.dataclass import ClusterFilter


class ClusterServiceHandler(BaseClusterServiceHandler):
def __init__(self, bk_biz_id: int):
self.bk_biz_id = bk_biz_id

def query_clusters(self, cluster_filters: List[ClusterFilter]) -> List[Dict[str, Any]]:
"""
# TODO: Deprecated, 这个方法将被移除,请不要调用
根据过滤条件查询集群,默认是精确匹配
"""

def _fill_mysql_instance_info(_cluster: Cluster, _cluster_info: Dict):
_cluster_info["masters"] = []
_cluster_info["slaves"] = []
_cluster_info["repeaters"] = []
# 录入proxy instance的信息和storage instance的信息
_cluster_info["proxies"] = [inst.simple_desc for inst in _cluster.proxyinstance_set.all()]
for inst in _cluster.storageinstance_set.all():
role = (
"masters" if _cluster.cluster_type == ClusterType.TenDBSingle else f"{inst.instance_inner_role}s"
)
_cluster_info[role].append(inst.simple_desc)

def _fill_spider_instance_info(_cluster: Cluster, _cluster_info: Dict):
# 更新remote db/remote dr的信息
_cluster_info.update(
{
"remote_db": [
m.simple_desc
for m in _cluster.storageinstance_set.all()
if m.instance_inner_role == InstanceInnerRole.MASTER
],
"remote_dr": [
m.simple_desc
for m in _cluster.storageinstance_set.all()
if m.instance_inner_role == InstanceInnerRole.SLAVE
],
}
)
# 更新spider角色信息
_cluster_info.update(
{
role: [
inst.simple_desc
for inst in _cluster.proxyinstance_set.all()
if inst.tendbclusterspiderext.spider_role == role
]
for role in TenDBClusterSpiderRole.get_values()
}
)
# 增加spider_ctl角色信息
_cluster_info["spider_ctl"] = copy.deepcopy(_cluster_info["spider_master"])
for instance in _cluster_info["spider_ctl"]:
instance["port"] = instance["admin_port"]
instance["instance_address"] = f"{instance['ip']}:{instance['port']}"

filter_conditions = Q()
for cluster_filter in cluster_filters:
filter_conditions |= Q(**cluster_filter.export_filter_conditions())
# 限制业务和集群类型只能是mysql & tendbcluster
filter_conditions &= Q(
cluster_type__in=[ClusterType.TenDBSingle, ClusterType.TenDBHA, ClusterType.TenDBCluster],
bk_biz_id=self.bk_biz_id,
)

clusters: QuerySet = (
Cluster.objects.prefetch_related("storageinstance_set", "proxyinstance_set")
.filter(filter_conditions)
.distinct()
)
cluster_db_module_ids: List[int] = [cluster.db_module_id for cluster in clusters]
db_module_names: Dict[int, str] = {
module.db_module_id: module.alias_name
for module in DBModule.objects.filter(db_module_id__in=cluster_db_module_ids)
}

filter_cluster_list = []
for cluster in clusters:
cluster_info = self._format_cluster_field(model_to_dict(cluster))
cluster_info["db_module_name"] = db_module_names.get(cluster_info["db_module_id"], "")

# 添加实例信息
cluster_info["instance_count"] = (
cluster.storageinstance_set.all().count() + cluster.proxyinstance_set.all().count()
)
if cluster.cluster_type == ClusterType.TenDBCluster:
cluster_info["instance_count"] += cluster.proxyinstance_set.filter(
tendbclusterspiderext__spider_role=TenDBClusterSpiderRole.SPIDER_MASTER
).count()
_fill_spider_instance_info(cluster, cluster_info)
else:
_fill_mysql_instance_info(cluster, cluster_info)

filter_cluster_list.append(cluster_info)

return filter_cluster_list

def get_remote_pairs(self, cluster_ids: List[int]):
"""
根据tendbcluster集群查询remote db/remote dr
Expand Down
12 changes: 0 additions & 12 deletions dbm-ui/backend/db_services/mysql/cluster/mock_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@
{"bk_host_id": 2, "cluster_info": {}, "related_clusters": []},
]

QUERY_CLUSTERS_REQUEST_DATA = {
"cluster_filters": [
{"immute_domain": "test.db1"},
{"immute_domain": "test.db2"},
]
}

QUERY_CLUSTERS_RESPONSE_DATA = [
{"id": 1, "immute_domain": "test.db1", "...": "..."},
{"id": 2, "immute_domain": "test.db2", "...": "..."},
]

GET_INTERSECTED_SLAVE_MACHINES_RESPONSE_DATA = [
{"ip": "1.1.1.1", "bk_host_id": 1, "bk_cloud_id": 0, "bk_biz_id": 2000522222},
{"ip": "1.1.1.2", "bk_host_id": 2, "bk_cloud_id": 0, "bk_biz_id": 2000522222},
Expand Down
22 changes: 0 additions & 22 deletions dbm-ui/backend/db_services/mysql/cluster/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,9 @@
GET_TENDB_MACHINE_INSTANCE_PAIR_RESPONSE_DATA,
GET_TENDB_RELATED_MACHINES_RESPONSE_DATA,
GET_TENDB_REMOTE_PAIRS_RESPONSE_DATA,
QUERY_CLUSTERS_REQUEST_DATA,
QUERY_CLUSTERS_RESPONSE_DATA,
)


class QueryClustersRequestSerializer(serializers.Serializer):
class FilterSerializer(serializers.Serializer):
"""集群过滤条件,后续有补充可以在这里添加"""

bk_biz_id = serializers.IntegerField(help_text=_("业务ID"), required=False)
id = serializers.IntegerField(help_text=_("集群ID"), required=False)
immute_domain = serializers.CharField(help_text=_("集群域名"), required=False)
cluster_type = serializers.CharField(help_text=_("集群类型"), required=False)

cluster_filters = serializers.ListSerializer(help_text=_("集群过滤条件列表"), child=FilterSerializer())

class Meta:
swagger_schema_fields = {"example": QUERY_CLUSTERS_REQUEST_DATA}


class QueryClustersResponseSerializer(serializers.Serializer):
class Meta:
swagger_schema_fields = {"example": QUERY_CLUSTERS_RESPONSE_DATA}


class GetTendbRemoteMachinesSerializer(serializers.Serializer):
cluster_ids = serializers.ListField(help_text=_("集群ID列表"), child=serializers.IntegerField())

Expand Down
16 changes: 0 additions & 16 deletions dbm-ui/backend/db_services/mysql/cluster/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@
GetMachineInstancePairSerializer,
GetTendbRemotePairsResponseSerializer,
GetTendbRemotePairsSerializer,
QueryClustersRequestSerializer,
QueryClustersResponseSerializer,
)
from backend.db_services.mysql.dataclass import ClusterFilter
from backend.iam_app.handlers.drf_perm.base import DBManagePermission

SWAGGER_TAG = "db_services/mysql/cluster"
Expand All @@ -37,19 +34,6 @@ class ClusterViewSet(BaseClusterViewSet):
action_permission_map = {}
default_permission_class = [DBManagePermission()]

@common_swagger_auto_schema(
operation_summary=_("通过过滤条件批量查询集群"),
request_body=QueryClustersRequestSerializer(),
tags=[SWAGGER_TAG],
responses={status.HTTP_200_OK: QueryClustersResponseSerializer()},
)
@action(methods=["POST"], detail=False, serializer_class=QueryClustersRequestSerializer)
def query_clusters(self, request, bk_biz_id):
# TODO: Deprecated, 这个视图方法将被移除,请不要调用
validated_data = self.params_validate(self.get_serializer_class())
cluster_filters = [ClusterFilter.from_dict(filter_dict) for filter_dict in validated_data["cluster_filters"]]
return Response(ClusterServiceHandler(bk_biz_id).query_clusters(cluster_filters=cluster_filters))

@common_swagger_auto_schema(
operation_summary=_("查询tendbcluster集群的remote_db/remote_dr"),
request_body=GetTendbRemotePairsSerializer(),
Expand Down