Skip to content

Commit

Permalink
fix(backend): 优化监控告警组后台同步任务 #2656
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzhw8 committed Dec 18, 2023
1 parent c09f34c commit fa3a339
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check_hard_code_ip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ jobs:
echo "good job!"
else
echo "Hard code ip founded! Please remove it."
echo ${RESULT}
echo -e "${RESULT}"
exit 1
fi
4 changes: 2 additions & 2 deletions .github/workflows/python_code_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ jobs:
if [ "$FLAKE8_RESULT" = "0" ]; then
echo "flake8 test passed"
else
echo ${FLAKE8_RESULT}
echo -e "${FLAKE8_RESULT}"
echo "flake8 test failed, please check if you have install pre-commit"
exit 1
fi
- name: Format with black
run: |
BLACK_RESULT=$(black dbm-ui/backend 2>&1)
if [[ $BLACK_RESULT =~ "reformatted" ]]; then
echo ${BLACK_RESULT}
echo -e "${BLACK_RESULT}"
echo "black test failed, please check if you have install pre-commit"
exit 1
else
Expand Down
8 changes: 3 additions & 5 deletions dbm-ui/backend/db_periodic_task/local_tasks/check_checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@
from backend.components import BKLogApi
from backend.db_meta.enums import ClusterType, InstanceInnerRole
from backend.db_meta.models import Cluster
from backend.db_report.models import ChecksumCheckReport
from backend.db_periodic_task.local_tasks.register import register_periodic_task
from backend.db_periodic_task.utils import TimeUnit, calculate_countdown
from backend.db_report.models import ChecksumCheckReport, ChecksumInstance
from backend.utils.time import datetime2str

from ...db_report.models.checksum_check_report import ChecksumInstance
from ..util import TimeUnit, calculate_countdown
from .register import register_periodic_task

logger = logging.getLogger("celery")


Expand Down
86 changes: 38 additions & 48 deletions dbm-ui/backend/db_periodic_task/local_tasks/db_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import logging
import os

from blueapps.core.celery.celery import app
from celery.schedules import crontab
from django.core.cache import cache

Expand All @@ -23,61 +24,50 @@
from backend.db_monitor.exceptions import BkMonitorSaveAlarmException
from backend.db_monitor.models import CollectInstance, DispatchGroup, MonitorPolicy, NoticeGroup
from backend.db_monitor.tasks import update_app_policy

from .register import register_periodic_task

# from django.utils.crypto import get_random_string

from backend.db_periodic_task.local_tasks.register import register_periodic_task
from backend.db_periodic_task.utils import TimeUnit, calculate_countdown

logger = logging.getLogger("celery")


@register_periodic_task(run_every=crontab(minute="*/2"))
@register_periodic_task(run_every=crontab(minute="*/5"))
def update_local_notice_group():
"""同步告警组"""

now = datetime.datetime.now()
logger.info("[local_notice_group] start update local group at: %s", now)

dbas = DBAdministrator.objects.all()
updated_groups, created_groups = 0, 0

for dba in dbas:
receiver_users = dba.users or DEFAULT_DB_ADMINISTRATORS
dba_ids = DBAdministrator.objects.values_list("id", flat=True)
count = len(dba_ids)
for index, dba_id in enumerate(dba_ids):
countdown = calculate_countdown(count=count, index=index, duration=5 * TimeUnit.MINUTE)
logger.info("dba_id({}) update notice group will be run after {} seconds.".format(dba_id, countdown))
update_dba_notice_group.apply_async(kwargs={"dba_id": dba_id}, countdown=countdown)


@app.task
def update_dba_notice_group(dba_id: int):
dba = DBAdministrator.objects.get(id=dba_id)
receiver_users = dba.users or DEFAULT_DB_ADMINISTRATORS
try:
group_name = f"{dba.get_db_type_display()}_DBA"
group_receivers = [{"id": user, "type": "user"} for user in receiver_users]
logger.info("[local_notice_group] update_or_create notice group: %s", group_name)
try:
group_name = f"{dba.get_db_type_display()}_DBA"
group_receivers = [{"id": user, "type": "user"} for user in receiver_users]
logger.info("[local_notice_group] update_or_create notice group: %s", group_name)
try:
group = NoticeGroup.objects.get(bk_biz_id=dba.bk_biz_id, db_type=dba.db_type, is_built_in=True)
except NoticeGroup.DoesNotExist:
NoticeGroup.objects.create(
name=group_name,
receivers=group_receivers,
details={"alert_notice": DEFAULT_ALERT_NOTICE},
bk_biz_id=dba.bk_biz_id,
db_type=dba.db_type,
is_built_in=True,
)
created_groups += 1
else:
group.name = group_name
group.receivers = group_receivers
if not group.details:
group.details = {"alert_notice": DEFAULT_ALERT_NOTICE}
group.save(update_fields=["name", "receivers", "details"])
updated_groups += 1

except Exception as e:
logger.error("[local_notice_group] update_or_create notice group error: %s", e)
continue

logger.info(
"[local_notice_group] finish update local group end: %s, create_cnt: %s, update_cnt: %s",
datetime.datetime.now() - now,
created_groups,
updated_groups,
)
group = NoticeGroup.objects.get(bk_biz_id=dba.bk_biz_id, db_type=dba.db_type, is_built_in=True)
except NoticeGroup.DoesNotExist:
NoticeGroup.objects.create(
name=group_name,
receivers=group_receivers,
details={"alert_notice": DEFAULT_ALERT_NOTICE},
bk_biz_id=dba.bk_biz_id,
db_type=dba.db_type,
is_built_in=True,
)
else:
group.name = group_name
group.receivers = group_receivers
if not group.details:
group.details = {"alert_notice": DEFAULT_ALERT_NOTICE}
group.save(update_fields=["name", "receivers", "details"])
except Exception as e:
logger.error("[local_notice_group] update_or_create notice group error: %s", e)


@register_periodic_task(run_every=crontab(minute="*/5"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
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.
"""
from __future__ import absolute_import, unicode_literals

import math
import random
from typing import Optional
Expand Down
1 change: 1 addition & 0 deletions dbm-ui/backend/env/bklog.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

# 自定义日志保留天数
BKLOG_MYSQL_BACKUP_RESULT_RETENTION = get_type_env(key="BKLOG_MYSQL_BACKUP_RESULT_RETENTION", _type=int)
BKLOG_MYSQL_DBBACKUP_RESULT_RETENTION = get_type_env(key="BKLOG_MYSQL_DBBACKUP_RESULT_RETENTION", _type=int)
BKLOG_MYSQL_BINLOG_RESULT_RETENTION = get_type_env(key="BKLOG_MYSQL_BINLOG_RESULT_RETENTION", _type=int)
BKLOG_REDIS_BINGLOG_BACKUP_RETENTION = get_type_env(key="BKLOG_REDIS_BINGLOG_BACKUP_RETENTION", _type=int)
BKLOG_REDIS_FULLBACKUP_RETENTION = get_type_env(key="BKLOG_REDIS_FULLBACKUP_RETENTION", _type=int)
Expand Down
10 changes: 10 additions & 0 deletions dbm-ui/backend/tests/db_periodic_task/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
72 changes: 72 additions & 0 deletions dbm-ui/backend/tests/db_periodic_task/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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 logging
import math
import random
import statistics
from typing import List, Set

import pytest

from backend.db_periodic_task import utils
from backend.utils import basic

pytestmark = pytest.mark.django_db
logger = logging.getLogger("test")


class TestCountDownPolicyCompare:
def test_compare_policy(self):
queue_lengths = [int(random.random() * 2 * utils.DURATION * math.pow(10, exp)) for exp in range(-1, 3)]
countdown_funcs = [
utils.calculate_countdown__mod,
utils.calculate_countdown__random,
utils.calculate_countdown__mix,
]
for countdown_func in countdown_funcs:
variance_list = []
for queue_length in queue_lengths:
hit_per_second: List[int] = [0] * utils.DURATION
for idx in range(queue_length):
countdown = countdown_func(count=queue_length, index=idx, duration=utils.DURATION)
hit_per_second[countdown] += 1

hit_per_interval = []
for chunk in basic.chunk_lists(lst=hit_per_second, n=max(1, int(utils.DURATION / 15))):
hit_per_interval.append(sum(chunk))
print(f"queue_length -> {queue_length}, hit_per_interval -> {hit_per_interval}")
variance_list.append(statistics.variance(hit_per_interval))

print(f"{countdown_func.__name__}, variance_list -> {variance_list}")


class TestPeriodicTask:
def test_calculate_countdown(self):
def _get_countdown_set(_count: int) -> Set[int]:
_countdown_set = set()
for idx in range(_count):
_countdown_set.add(utils.calculate_countdown(count=_count, index=idx))
return _countdown_set

def _test_one_count():
assert utils.calculate_countdown(count=1, index=0) == 0

def _test_count_less_than_duration():
count = random.randint(1, utils.DURATION)
_get_countdown_set(_count=count)

def _test_count_more_than_duration():
count = random.randint(utils.DURATION + 1, 2 * utils.DURATION)
_get_countdown_set(_count=count)

_test_one_count()
_test_count_less_than_duration()
_test_count_more_than_duration()
10 changes: 10 additions & 0 deletions dbm-ui/backend/tests/dbm_init/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
10 changes: 10 additions & 0 deletions dbm-ui/backend/tests/flow/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
10 changes: 10 additions & 0 deletions dbm-ui/backend/tests/flow/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
10 changes: 10 additions & 0 deletions dbm-ui/backend/tests/flow/components/collections/mysql/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
10 changes: 10 additions & 0 deletions dbm-ui/backend/tests/mysql/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-DB管理系统(BlueKing-BK-DBM) available.
Copyright (C) 2017-2023 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
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.
"""
4 changes: 2 additions & 2 deletions helm-charts/bk-dbm/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ dependencies:
description: A Helm chart for bk-dbm
name: bk-dbm
type: application
version: 1.3.0-alpha.24
appVersion: 1.3.0-alpha.24
version: 1.3.0-alpha.25
appVersion: 1.3.0-alpha.25
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/db-simulation/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 0.0.1-alpha.43
appVersion: 0.0.1-alpha.44
description: A Helm chart for Kubernetes
name: db-simulation
type: application
Expand Down
2 changes: 1 addition & 1 deletion helm-charts/bk-dbm/charts/dbm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
apiVersion: v2
appVersion: 1.3.0-alpha.186
appVersion: 1.3.0-alpha.198
description: A Helm chart for dbm
name: dbm
type: application
Expand Down

0 comments on commit fa3a339

Please sign in to comment.