diff --git a/omniadvisor/README.md b/omniadvisor/README.md index 6600b178517049309e59812c53a74b94f707e30b..5c28773eb68f3923a5feb6bff8bfa5ce2810a242 100755 --- a/omniadvisor/README.md +++ b/omniadvisor/README.md @@ -4,13 +4,12 @@ - ① 进入`omniadvisor/src/`目录下。 - ② 顺序执行以下命令:`python init.py makemigrations`和`python init.py migrate`,初始化数据库 - ③ 执行命令`python init.py createsuperuser`创建超级用户,并按指示设置用户名和密码。 -- ④ 修改`omniadvisor/src/server/engine/settings.py`中的 `ALLOWED_HOSTS`,在列表中添加当前节点IP地址 2 启动历史数据库服务 完成数据库初始化后,即可启动历史数据库,并通过Web端查看数据库内数据 -- ① 执行命令`python init.py runserver 0.0.0.0:8000`启动数据库。 -- ② 登录Web管理页面:`localhost:8000/admin/`,`localhost `替换实际的IP地址,并通过初始化过程中的用户名和密码登录。 +- ① 执行命令`python init.py runserver 0.0.0.0:8000`启动数据库,其中`0.0.0.0`应替换为实际节点IP。 +- ② 登录Web管理页面:`localhost:8000/admin/`,`localhost`替换实际的IP地址,并通过初始化过程中的用户名和密码登录。 3 负载劫持 首先要对用户负载进行劫持,获取用户负载及相关信息,以便后续能够调优。使能负载劫持,步骤如下: diff --git a/omniadvisor/src/omniadvisor/interface/config_tuning.py b/omniadvisor/src/omniadvisor/interface/config_tuning.py index 6509be5c28c88df149c01e7e096a70fb79b8fa27..8142f730960355fa11909e9677d3be0f33674ffe 100644 --- a/omniadvisor/src/omniadvisor/interface/config_tuning.py +++ b/omniadvisor/src/omniadvisor/interface/config_tuning.py @@ -10,9 +10,14 @@ from omniadvisor.repository.model.load import Load from omniadvisor.repository.load_repository import LoadRepository from omniadvisor.repository.tuning_record_repository import TuningRecordRepository from omniadvisor.service.retest_service import retest -from omniadvisor.service.tuning_result.tuning_result import remove_tuning_result -from omniadvisor.service.tuning_result.tuning_result_history import get_tuning_result_history, \ +from omniadvisor.service.tuning_result.tuning_result import ( + get_tuning_result, + remove_tuning_result +) +from omniadvisor.service.tuning_result.tuning_result_history import ( + get_tuning_result_history, get_other_tuning_result_history +) from omniadvisor.utils.logger import global_logger @@ -77,10 +82,14 @@ def unified_tuning(load, retest_way: str, tuning_method: str): 'information. Remove tuning results.') remove_tuning_result(load, next_config) raise - tuning_result_history = get_tuning_result_history(load) + tuning_result = get_tuning_result(load=load, config=next_config) + tuning_result_history = get_tuning_result_history(load=load) # 更新最优配置 - if tuning_result_history.best_config and tuning_result_history.best_config != next_config: - LoadRepository.update_best_config(load, tuning_result_history.best_config) + tuning_result_history.refresh_best_config() + global_logger.info(f'Current tuning result: {tuning_result.runtime}, best tuning result ever: ' + f'{tuning_result_history.best_tuning_result.runtime}, best tuning speedup: ' + f'{tuning_result_history.boost_percentage} %') + else: # 更新待测试配置即可 global_logger.info(f"The way of retest is hijacking, update the config from tuning to test config.") diff --git a/omniadvisor/src/omniadvisor/interface/hijack_recommend.py b/omniadvisor/src/omniadvisor/interface/hijack_recommend.py index 59d71373be9655c8c6ab524ea1b331b3eb5883bc..269abed4b14dbf3ba68febfea8ae3daf84f8301a 100644 --- a/omniadvisor/src/omniadvisor/interface/hijack_recommend.py +++ b/omniadvisor/src/omniadvisor/interface/hijack_recommend.py @@ -6,6 +6,7 @@ from omniadvisor.repository.model.load import Load from omniadvisor.repository.load_repository import LoadRepository from omniadvisor.repository.tuning_record_repository import TuningRecordRepository from omniadvisor.service.tuning_result.tuning_result import get_tuning_result +from omniadvisor.service.tuning_result.tuning_result_history import get_tuning_result_history from omniadvisor.utils.logger import global_logger from omniadvisor.service.spark_service.spark_run import spark_run @@ -76,18 +77,12 @@ def _process_load_config(load: Load): LoadRepository.update_test_config(load, {}) # 测试配置执行失败 elif tuning_result.status == OA_CONF.ExecStatus.success: - # 获取当前best_config的平均测试性能 - best_tuning_result = get_tuning_result(load, load.best_config) - # 若调优性能优于最佳性能 刷新当前的best_config - if tuning_result.runtime < best_tuning_result.runtime: - boost_percentage = round( - (best_tuning_result.runtime - tuning_result.runtime) / best_tuning_result.runtime, 2 - ) - global_logger.info(f"Tuning_runtime = {tuning_result.runtime} is " - f"quicker than best_runtime = {best_tuning_result.runtime}, " - f"boost percentage = {boost_percentage}" - f"found a better spark config") - LoadRepository.update_best_config(load, load.test_config) + tuning_result_history = get_tuning_result_history(load=load) + # 更新最优配置 + tuning_result_history.refresh_best_config() + global_logger.info(f'Current tuning result: {tuning_result.runtime}, best tuning result ever: ' + f'{tuning_result_history.best_tuning_result.runtime}, best tuning speedup: ' + f'{tuning_result_history.boost_percentage} %') # 当前test_config达到调优轮次后 置空test_config LoadRepository.update_test_config(load, {}) diff --git a/omniadvisor/src/omniadvisor/service/tuning_result/tuning_result_history.py b/omniadvisor/src/omniadvisor/service/tuning_result/tuning_result_history.py index 429ebecfea7118204a2d96fccb9af363c13d76ca..f8b1d640738653b1563065ec1a728b11f3091d72 100644 --- a/omniadvisor/src/omniadvisor/service/tuning_result/tuning_result_history.py +++ b/omniadvisor/src/omniadvisor/service/tuning_result/tuning_result_history.py @@ -1,5 +1,7 @@ from typing import List +from common.constant import OA_CONF +from omniadvisor.repository.load_repository import LoadRepository from omniadvisor.repository.exam_record_repository import ExamRecordRepository from omniadvisor.repository.load_prefetch_repository import LoadPrefetchRepository from omniadvisor.repository.model.load import Load @@ -81,11 +83,26 @@ class TuningResultHistory: return list(map(lambda tuning_result: tuning_result.to_tuning(), self._tuning_results)) @property - def best_config(self): - if self.tuning_history: - return min(self.tuning_history, key=lambda x: x.runtime).config + def user_tuning_result(self): + for tuning_result in self._tuning_results: + if tuning_result.method == OA_CONF.TuningMethod.user: + return tuning_result + return None + + @property + def best_tuning_result(self): + if self._tuning_results: + return min(self._tuning_results, key=lambda x: x.runtime) else: - return {} + return None + + @property + def boost_percentage(self): + if not self.user_tuning_result: + return 0.0 + return round( + (self.user_tuning_result.runtime - self.best_tuning_result.runtime) / self.user_tuning_result.runtime, 4 + ) * 100 @property def latest_tuning_result(self): @@ -111,3 +128,14 @@ class TuningResultHistory: else: data[tuning_result.method] = 1 return data + + def refresh_best_config(self): + """ + 更新负载中的最优配置字段 + :return: + """ + if self.best_tuning_result and self.best_tuning_result.config != self._load.best_config: + LoadRepository.update_best_config( + load=self._load, + best_config=self.best_tuning_result.config + ) diff --git a/omniadvisor/src/server/engine/settings.py b/omniadvisor/src/server/engine/settings.py index 38b99e65306830294658327502a56c66303fc563..e100d21c736eecbb80cc7a0b3bc0679aecbb2242 100644 --- a/omniadvisor/src/server/engine/settings.py +++ b/omniadvisor/src/server/engine/settings.py @@ -25,7 +25,7 @@ SECRET_KEY = 'django-insecure-no@z!_@jyd1w6!497ewxgm3h2n-0^oaz@9go9w5b%y_9%tv2c% # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = ['*'] # Application definition diff --git a/omniadvisor/tests/omniadvisor/interface/test_config_tuning.py b/omniadvisor/tests/omniadvisor/interface/test_config_tuning.py index e7919e68a9a3e6eb208c91b49869a318e1403623..83dfe32ff561c28dd16b1aa2779756c6639becfc 100644 --- a/omniadvisor/tests/omniadvisor/interface/test_config_tuning.py +++ b/omniadvisor/tests/omniadvisor/interface/test_config_tuning.py @@ -1,4 +1,3 @@ -import logging import sys from unittest.mock import patch, MagicMock @@ -27,16 +26,18 @@ class TestTuning: with patch('omniadvisor.repository.load_repository.LoadRepository.query_by_id'), \ patch('omniadvisor.repository.tuning_record_repository.TuningRecordRepository.create'), \ patch('omniadvisor.service.retest_service.spark_run') as mock_spark_run, \ - patch('omniadvisor.interface.config_tuning.get_tuning_result_history'), \ - patch('omniadvisor.repository.load_repository.LoadRepository.update_best_config') as mock_update_best, \ + patch('omniadvisor.interface.config_tuning.get_tuning_result'), \ + patch('omniadvisor.interface.config_tuning.get_tuning_result_history') as mock_get_history, \ patch('algo.iterative.tuning.SmacAppendTuning.tune') as mock_smac_tuning: mock_exam_record = MagicMock() mock_smac_tuning.return_value = self.tune_return_val mock_exam_record.status = OA_CONF.ExecStatus.success spark_output = self.empty_str mock_spark_run.return_value = mock_exam_record, spark_output + mock_tuning_history = MagicMock() + mock_get_history.return_value = mock_tuning_history unified_tuning(load=self.load, retest_way=OA_CONF.RetestWay.backend, tuning_method=self.tuning_method) - mock_update_best.assert_called_once() + mock_tuning_history.refresh_best_config.assert_called_once() mock_smac_tuning.assert_called_once() assert mock_spark_run.call_count == OA_CONF.tuning_retest_times @@ -47,6 +48,7 @@ class TestTuning: """ with patch('omniadvisor.repository.load_repository.LoadRepository.query_by_id'), \ patch('omniadvisor.repository.tuning_record_repository.TuningRecordRepository.create'), \ + patch('omniadvisor.interface.config_tuning.get_tuning_result'), \ patch('omniadvisor.service.retest_service.spark_run') as mock_spark_run, \ patch('omniadvisor.interface.config_tuning.get_tuning_result_history') as mock_tuning_result_history, \ patch('omniadvisor.service.retest_service.get_tuning_result') as mock_get_tuning_result, \ diff --git a/omniadvisor/tests/omniadvisor/service/tuning_result/test_tuning_result_history.py b/omniadvisor/tests/omniadvisor/service/tuning_result/test_tuning_result_history.py index 97cb5c6544d59af645246543c0fdde82e49e7519..68609f3cb280abc630fdeaa97b0b5fb75117c7c3 100644 --- a/omniadvisor/tests/omniadvisor/service/tuning_result/test_tuning_result_history.py +++ b/omniadvisor/tests/omniadvisor/service/tuning_result/test_tuning_result_history.py @@ -71,4 +71,4 @@ class TestTuningResultHistory: (tuning_record_mock.config, tuning_result_mock.runtime), ({"param2": "value2"}, 5) ] - assert tuning_result_history.best_config == {"param2": "value2"} \ No newline at end of file + assert tuning_result_history.best_tuning_result.config == {"param2": "value2"} \ No newline at end of file