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

KytosGraph adds excessive metadata to its links #64

Closed
MarvinTorres opened this issue Aug 14, 2020 · 0 comments · Fixed by #65
Closed

KytosGraph adds excessive metadata to its links #64

MarvinTorres opened this issue Aug 14, 2020 · 0 comments · Fixed by #65

Comments

@MarvinTorres
Copy link
Contributor

MarvinTorres commented Aug 14, 2020

pathfinder/graph.py

Lines 42 to 65 in eb43bbe

def update_links(self, links):
"""Update all links inside the graph."""
keys = []
for link in links.values():
if link.is_active():
self.graph.add_edge(link.endpoint_a.id, link.endpoint_b.id)
for key, value in link.metadata.items():
keys.extend(key)
endpoint_a = link.endpoint_a.id
endpoint_b = link.endpoint_b.id
self.graph[endpoint_a][endpoint_b][key] = value
self._set_default_metadata(keys)
def _set_default_metadata(self, keys):
"""Set metadata to all links.
Set the value to zero for inexistent metadata in a link to make those
irrelevant in pathfinding.
"""
for key in keys:
for endpoint_a, endpoint_b in self.graph.edges:
if key not in self.graph[endpoint_a][endpoint_b]:
self.graph[endpoint_a][endpoint_b][key] = 0

Description

KytosGraph's update_links method receives links from a Topology input parameter called Links, uses each link in the Topology to find the same link in its internal NetworkX graph, and updates that link's metadata in the NetworkX graph.

However, this method does not work properly. If it is tasked to update link a with the following metadata:

  • "bandwidth": 30
  • "delay": 20

_set_default_metadata will add unintended single-letter attributes ("b", "a", "n", ... "d", "e", "l", ...) to it on top of the intended attributes ("bandwidth", "delay").

This is the result of keys being incorrectly set up before being used as a summary of attributes that exist in a KytosGraph. _set_default_metadata uses it to set zeroes to unused attributes. So if it is ["b", "a", "n", "d", "w", "i", "d", "t", "h"] instead of ["bandwidth"], _set_default_metadata will add seven (7) attributes to each link, instead of one (1).

These unused attributes will add memory overhead that will never be utilized by the end-user, making them garbage data.

Steps to reproduce

Run the following code in an online compiler.

def update_links(**links):
        """Update all links inside the graph."""
        keys = []
        for link in links.values():
                for key, value in link.metadata.items():
                    keys.extend(key)

        print(list(u for u in keys))

class Link():
  def __init__(self, metadata):
    self.metadata = metadata

link_a = Link({"one": 1, "hi": 38})
link_b = Link({"two": 2})
link_c = Link({"three": 3})

update_links(a=link_a, b=link_b, c=link_c)

Error Output

Expected: ['one', 'hi', 'two', 'three']
Actual: ['o', 'n', 'e', 'h', 'i', 't', 'w', 'o', 't', 'h', 'r', 'e', 'e']

Cause
This is likely caused by the structure of strings in Python and extend's implementation.

If extend's argument was "apple", then key's elements become ['a', 'p', 'p', 'l', 'e']. This is equivalent to using keys.extend(t for t in "apple")

But if extend's argument was a generator, such as `k for k in ', then key's elements become ['apple']. This is equivalent to using 'keys.extend(t for t in ["apple"])

It is possible that extend uses its argument as the tail end of a generator.

Motivation

Although update_links updates the links correctly, and _set_default_metadata does not touch the attributes in links that update_links updated, users might wonder why a link has a bunch of single-letter attributes with zeroes. In addition, keys may be used again in the future, so it is best to fix this issue as soon as possible before it causes trouble.

System Details

  • OS: Ubuntu 18.04
MarvinTorres added a commit to MarvinTorres/pathfinder that referenced this issue Aug 14, 2020
@MarvinTorres MarvinTorres changed the title KytosGraph updates links with incorrect metadata KytosGraph adds garbage metadata to its links Aug 15, 2020
MarvinTorres added a commit to MarvinTorres/pathfinder that referenced this issue Aug 19, 2020
  - This hopefully ensures that kytos#64 will not reoccur.
@MarvinTorres MarvinTorres changed the title KytosGraph adds garbage metadata to its links KytosGraph adds excessive metadata to its links Aug 19, 2020
hdiogenes pushed a commit that referenced this issue Sep 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant