Skip to content

Commit 4f5fe9d

Browse files
author
Jongmin Kim
authored
Merge pull request #38 from whdalsrnt/master
refactor: refactor load balancing policy of plugins
2 parents 4045928 + b3b0ef5 commit 4f5fe9d

File tree

3 files changed

+67
-44
lines changed

3 files changed

+67
-44
lines changed

src/spaceone/plugin/manager/plugin_manager/__init__.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,19 @@ def update_plugin_state(
193193
if len(endpoints) == 0:
194194
endpoints = [endpoint]
195195

196-
return plugin_vo.update(
197-
{"state": state, "endpoint": endpoint, "endpoints": endpoints}
198-
)
196+
endpoints = sorted(endpoints)
197+
198+
if plugin_vo.endpoints != endpoints:
199+
return plugin_vo.update(
200+
{
201+
"state": state,
202+
"endpoint": endpoint,
203+
"endpoints": endpoints,
204+
"current_index": 0,
205+
}
206+
)
207+
else:
208+
return plugin_vo.update({"state": state, "endpoint": endpoint})
199209

200210
def make_reprovision(self, supervisor_id, plugin_id, version):
201211
def _rollback(old_data: dict):

src/spaceone/plugin/model/installed_plugin_model.py

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,75 @@
44

55
from spaceone.core.model.mongo_model import MongoModel
66
from spaceone.plugin.model.supervisor_model import Supervisor
7-
from spaceone.plugin.manager.plugin_manager.plugin_state import PROVISIONING, ACTIVE, ERROR, RE_PROVISIONING
7+
from spaceone.plugin.manager.plugin_manager.plugin_state import (
8+
PROVISIONING,
9+
ACTIVE,
10+
ERROR,
11+
RE_PROVISIONING,
12+
)
813

9-
__all__ = ['InstalledPlugin']
14+
__all__ = ["InstalledPlugin"]
1015

1116

1217
class InstalledPlugin(MongoModel):
1318
# TODO: check plugin_id max length
14-
plugin_id = StringField(max_length=255, required=True, null=False, unique_with=['version', 'supervisor_id'])
19+
plugin_id = StringField(
20+
max_length=255,
21+
required=True,
22+
null=False,
23+
unique_with=["version", "supervisor_id"],
24+
)
1525
supervisor_id = StringField(max_length=255, required=True, null=False)
16-
supervisor = ReferenceField('Supervisor', reverse_delete_rule=CASCADE, required=True, null=False)
26+
supervisor = ReferenceField(
27+
"Supervisor", reverse_delete_rule=CASCADE, required=True, null=False
28+
)
1729
name = StringField(max_length=255)
1830
image = StringField(max_length=255)
1931
version = StringField(max_length=255)
20-
state = StringField(max_length=40,
21-
default=PROVISIONING,
22-
choices=(PROVISIONING, ACTIVE, ERROR, RE_PROVISIONING))
32+
state = StringField(
33+
max_length=40,
34+
default=PROVISIONING,
35+
choices=(PROVISIONING, ACTIVE, ERROR, RE_PROVISIONING),
36+
)
2337
endpoint = StringField(max_length=255)
2438
endpoints = ListField(StringField(max_length=255))
39+
current_index = IntField(default=0)
2540
domain_id = StringField(max_length=40, required=True, null=False)
2641
created_at = DateTimeField(auto_now_add=True)
2742
updated_at = DateTimeField(auto_now_add=True)
2843
endpoint_called_at = DateTimeField(default=None, null=True)
2944

3045
meta = {
31-
'updatable_fields': [
32-
'name',
33-
'updated_at',
34-
'state',
35-
'endpoint',
36-
'endpoints',
37-
'endpoint_called_at'
46+
"updatable_fields": [
47+
"name",
48+
"updated_at",
49+
"state",
50+
"endpoint",
51+
"endpoints",
52+
"current_index",
53+
"endpoint_called_at",
3854
],
39-
'minimal_fields': [
40-
'plugin_id',
41-
'version',
42-
'state',
43-
'endpoint',
44-
'endpoints'
55+
"minimal_fields": ["plugin_id", "version", "state", "endpoint", "endpoints"],
56+
"change_query_keys": {"hostname": "supervisor.hostname"},
57+
"reference_query_keys": {"supervisor": Supervisor},
58+
"ordering": ["name"],
59+
"indexes": [
60+
"plugin_id",
61+
"supervisor_id",
62+
"supervisor",
63+
"domain_id",
64+
"name",
65+
"image",
66+
"version",
67+
"state",
68+
"endpoint_called_at",
4569
],
46-
'change_query_keys': {
47-
'hostname': 'supervisor.hostname'
48-
},
49-
'reference_query_keys': {
50-
'supervisor': Supervisor
51-
},
52-
'ordering': ['name'],
53-
'indexes': [
54-
'plugin_id',
55-
'supervisor_id',
56-
'supervisor',
57-
'domain_id',
58-
'name',
59-
'image',
60-
'version',
61-
'state',
62-
'endpoint_called_at'
63-
]
6470
}
6571

6672
def update(self, data):
67-
data['updated_at'] = datetime.datetime.now()
73+
data["updated_at"] = datetime.datetime.now()
6874
return super().update(data)
6975

7076
def update_endpoint_called_at(self):
71-
data = {'endpoint_called_at': datetime.datetime.now()}
77+
data = {"endpoint_called_at": datetime.datetime.now()}
7278
return super().update(data)

src/spaceone/plugin/service/plugin_service.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,14 @@ def _select_endpoint(self, plugin_ref, updated_version=None):
251251
endpoints = installed_plugin.endpoints
252252
if endpoints:
253253
_LOGGER.debug(f"[_select_endpoint] {endpoints}")
254-
endpoint = self._select_one(endpoints)
254+
# endpoint = self._select_one(endpoints)
255+
256+
installed_plugin = installed_plugin.increment("current_index")
257+
258+
if installed_plugin.current_index >= len(endpoints):
259+
installed_plugin = installed_plugin.update({"current_index": 0})
260+
261+
endpoint = endpoints[installed_plugin.current_index]
255262

256263
endpoint_info = {"endpoint": endpoint}
257264

0 commit comments

Comments
 (0)