From b3b0ef5cd25bfcd562057b598c4348ba0c0f614a Mon Sep 17 00:00:00 2001 From: Jongmin Kim Date: Wed, 8 Jan 2025 22:48:06 +0900 Subject: [PATCH] refactor: refactor load balancing policy of plugins --- .../plugin/manager/plugin_manager/__init__.py | 16 +++- .../plugin/model/installed_plugin_model.py | 86 ++++++++++--------- src/spaceone/plugin/service/plugin_service.py | 9 +- 3 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/spaceone/plugin/manager/plugin_manager/__init__.py b/src/spaceone/plugin/manager/plugin_manager/__init__.py index 9b6597e..83a9c8c 100644 --- a/src/spaceone/plugin/manager/plugin_manager/__init__.py +++ b/src/spaceone/plugin/manager/plugin_manager/__init__.py @@ -193,9 +193,19 @@ def update_plugin_state( if len(endpoints) == 0: endpoints = [endpoint] - return plugin_vo.update( - {"state": state, "endpoint": endpoint, "endpoints": endpoints} - ) + endpoints = sorted(endpoints) + + if plugin_vo.endpoints != endpoints: + return plugin_vo.update( + { + "state": state, + "endpoint": endpoint, + "endpoints": endpoints, + "current_index": 0, + } + ) + else: + return plugin_vo.update({"state": state, "endpoint": endpoint}) def make_reprovision(self, supervisor_id, plugin_id, version): def _rollback(old_data: dict): diff --git a/src/spaceone/plugin/model/installed_plugin_model.py b/src/spaceone/plugin/model/installed_plugin_model.py index b51e8b2..87024df 100644 --- a/src/spaceone/plugin/model/installed_plugin_model.py +++ b/src/spaceone/plugin/model/installed_plugin_model.py @@ -4,69 +4,75 @@ from spaceone.core.model.mongo_model import MongoModel from spaceone.plugin.model.supervisor_model import Supervisor -from spaceone.plugin.manager.plugin_manager.plugin_state import PROVISIONING, ACTIVE, ERROR, RE_PROVISIONING +from spaceone.plugin.manager.plugin_manager.plugin_state import ( + PROVISIONING, + ACTIVE, + ERROR, + RE_PROVISIONING, +) -__all__ = ['InstalledPlugin'] +__all__ = ["InstalledPlugin"] class InstalledPlugin(MongoModel): # TODO: check plugin_id max length - plugin_id = StringField(max_length=255, required=True, null=False, unique_with=['version', 'supervisor_id']) + plugin_id = StringField( + max_length=255, + required=True, + null=False, + unique_with=["version", "supervisor_id"], + ) supervisor_id = StringField(max_length=255, required=True, null=False) - supervisor = ReferenceField('Supervisor', reverse_delete_rule=CASCADE, required=True, null=False) + supervisor = ReferenceField( + "Supervisor", reverse_delete_rule=CASCADE, required=True, null=False + ) name = StringField(max_length=255) image = StringField(max_length=255) version = StringField(max_length=255) - state = StringField(max_length=40, - default=PROVISIONING, - choices=(PROVISIONING, ACTIVE, ERROR, RE_PROVISIONING)) + state = StringField( + max_length=40, + default=PROVISIONING, + choices=(PROVISIONING, ACTIVE, ERROR, RE_PROVISIONING), + ) endpoint = StringField(max_length=255) endpoints = ListField(StringField(max_length=255)) + current_index = IntField(default=0) domain_id = StringField(max_length=40, required=True, null=False) created_at = DateTimeField(auto_now_add=True) updated_at = DateTimeField(auto_now_add=True) endpoint_called_at = DateTimeField(default=None, null=True) meta = { - 'updatable_fields': [ - 'name', - 'updated_at', - 'state', - 'endpoint', - 'endpoints', - 'endpoint_called_at' + "updatable_fields": [ + "name", + "updated_at", + "state", + "endpoint", + "endpoints", + "current_index", + "endpoint_called_at", ], - 'minimal_fields': [ - 'plugin_id', - 'version', - 'state', - 'endpoint', - 'endpoints' + "minimal_fields": ["plugin_id", "version", "state", "endpoint", "endpoints"], + "change_query_keys": {"hostname": "supervisor.hostname"}, + "reference_query_keys": {"supervisor": Supervisor}, + "ordering": ["name"], + "indexes": [ + "plugin_id", + "supervisor_id", + "supervisor", + "domain_id", + "name", + "image", + "version", + "state", + "endpoint_called_at", ], - 'change_query_keys': { - 'hostname': 'supervisor.hostname' - }, - 'reference_query_keys': { - 'supervisor': Supervisor - }, - 'ordering': ['name'], - 'indexes': [ - 'plugin_id', - 'supervisor_id', - 'supervisor', - 'domain_id', - 'name', - 'image', - 'version', - 'state', - 'endpoint_called_at' - ] } def update(self, data): - data['updated_at'] = datetime.datetime.now() + data["updated_at"] = datetime.datetime.now() return super().update(data) def update_endpoint_called_at(self): - data = {'endpoint_called_at': datetime.datetime.now()} + data = {"endpoint_called_at": datetime.datetime.now()} return super().update(data) diff --git a/src/spaceone/plugin/service/plugin_service.py b/src/spaceone/plugin/service/plugin_service.py index 7fac2d5..f0d192c 100644 --- a/src/spaceone/plugin/service/plugin_service.py +++ b/src/spaceone/plugin/service/plugin_service.py @@ -251,7 +251,14 @@ def _select_endpoint(self, plugin_ref, updated_version=None): endpoints = installed_plugin.endpoints if endpoints: _LOGGER.debug(f"[_select_endpoint] {endpoints}") - endpoint = self._select_one(endpoints) + # endpoint = self._select_one(endpoints) + + installed_plugin = installed_plugin.increment("current_index") + + if installed_plugin.current_index >= len(endpoints): + installed_plugin = installed_plugin.update({"current_index": 0}) + + endpoint = endpoints[installed_plugin.current_index] endpoint_info = {"endpoint": endpoint}