Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hot fix/UUID having extr chars #3134

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 47 additions & 9 deletions volttron/platform/control/control_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,59 @@
from volttron.platform import jsonapi
from volttron.platform.agent.utils import is_secure_mode
import os
from typing import List

_stdout = sys.stdout
_stderr = sys.stderr


def _calc_min_uuid_length(agents):
n = 0
for agent1 in agents:
for agent2 in agents:
if agent1 is agent2:
continue
common_len = len(os.path.commonprefix([agent1.uuid, agent2.uuid]))
if common_len > n:
n = common_len
return n + 1
agent_ids = [agent.uuid for agent in agents]
return _calc_min_unique_uuid_length(agent_ids)


def _calc_min_unique_uuid_length(uuids: List[str]) -> int:
"""
Helper function to calculate unique uuids with minial char numbers.
Mechanism: start common_len at 1,
collect a pool of uuids represent by first common_len of its uuid,
increase the number of common_len until the pool of uuids with first common_len has unique members.
EXAMPLE:
agent_ids = ['9b171ba5-f69f-4895-a69e-b51cf4d78150',
'2c7c8405-49c8-48eb-86ff-236ebe39da6e',
'd73a71c8-000f-46aa-8e5f-c4436cf847c3',
'da2c1f0d-6ce5-4095-843c-3abc861d5199',
'f5de325c-e723-41e5-9ceb-fae722a40eb6']
_calc_min_unique_uuid_length(agent_ids)
>> 2
EXAMPLE:
agent_ids = ['9b171ba5-f69f-4895-a69e-b51cf4d78150',
'd77c8405-49c8-48eb-86ff-236ebe39da6e',
'd73a71c8-000f-46aa-8e5f-c4436cf847c3',
'da2c1f0d-6ce5-4095-843c-3abc861d5199',
'da3c1f0d-6ce5-4095-843c-3abc861d5199',
'f5de325c-e723-41e5-9ceb-fae722a40eb6']
_calc_min_unique_uuid_length(agent_ids)
>> 3
agent_ids = ['9b171ba5-f69f-4895-a69e-b51cf4d78150',
'd77c8405-49c8-48eb-86ff-236ebe39da6e',
'f5de325c-e723-41e5-9ceb-fae722a40eb6']
_calc_min_unique_uuid_length(agent_ids)
>> 1
agent_ids = ['9b171ba5-f69f-4895-a69e-b51cf4d78150',
'9b171ba5-f69f-4895-a69e-b51cf4d78151',
]
_calc_min_unique_uuid_length(agent_ids)
>> 36
"""
common_len = 1
while common_len <= len(uuids[0]): # assuming all uuids have the same length
head_uuids = [uuid[:common_len] for uuid in uuids]
if len(head_uuids) == len(set(head_uuids)):
break
common_len += 1
# print(head_uuids)
return common_len


def _list_agents(aip):
Expand Down
28 changes: 28 additions & 0 deletions volttrontesting/platform/control_tests/test_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from volttron.platform import get_examples
from volttron.platform.jsonrpc import RemoteError
from volttron.platform.control.control_utils import _calc_min_unique_uuid_length
import sys

@pytest.mark.timeout(600)
Expand Down Expand Up @@ -233,3 +234,30 @@ def main(argv=sys.argv):
gevent.sleep(1)
wait_time += 1
assert crashed and restarted


def test__calc_min_unique_uuid_length():
agent_ids = ['9b171ba5-f69f-4895-a69e-b51cf4d78150',
'2c7c8405-49c8-48eb-86ff-236ebe39da6e',
'd73a71c8-000f-46aa-8e5f-c4436cf847c3',
'da2c1f0d-6ce5-4095-843c-3abc861d5199',
'f5de325c-e723-41e5-9ceb-fae722a40eb6']
assert _calc_min_unique_uuid_length(agent_ids) == 2

agent_ids = ['9b171ba5-f69f-4895-a69e-b51cf4d78150',
'd77c8405-49c8-48eb-86ff-236ebe39da6e',
'd73a71c8-000f-46aa-8e5f-c4436cf847c3',
'da2c1f0d-6ce5-4095-843c-3abc861d5199',
'da3c1f0d-6ce5-4095-843c-3abc861d5199',
'f5de325c-e723-41e5-9ceb-fae722a40eb6']
assert _calc_min_unique_uuid_length(agent_ids) == 3

agent_ids = ['9b171ba5-f69f-4895-a69e-b51cf4d78150',
'd77c8405-49c8-48eb-86ff-236ebe39da6e',
'f5de325c-e723-41e5-9ceb-fae722a40eb6']
assert _calc_min_unique_uuid_length(agent_ids) == 1

agent_ids = ['9b171ba5-f69f-4895-a69e-b51cf4d78150',
'9b171ba5-f69f-4895-a69e-b51cf4d78151',
]
assert _calc_min_unique_uuid_length(agent_ids) == 36
Loading