Skip to content

Commit

Permalink
Merge pull request #38 from whdalsrnt/master
Browse files Browse the repository at this point in the history
refactor: refactor load balancing policy of plugins
  • Loading branch information
whdalsrnt authored Jan 8, 2025
2 parents 4045928 + b3b0ef5 commit 4f5fe9d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 44 deletions.
16 changes: 13 additions & 3 deletions src/spaceone/plugin/manager/plugin_manager/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
86 changes: 46 additions & 40 deletions src/spaceone/plugin/model/installed_plugin_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
9 changes: 8 additions & 1 deletion src/spaceone/plugin/service/plugin_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down

0 comments on commit 4f5fe9d

Please sign in to comment.