kytos / pathfinder

Kytos main path finder Network Application (NApp)
https://napps.kytos.io/kytos/pathfinder
MIT License
1 stars 21 forks source link

KytosGraph adds excessive metadata to its links #64

Closed MarvinTorres closed 4 years ago

MarvinTorres commented 4 years ago

https://github.com/kytos/pathfinder/blob/eb43bbefef45083cfdae460be223019ca3b8264a/graph.py#L42-L65

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:

_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