Capture all

Today, Aaron L. shares the tale of an innocent little network mapping program that killed itself with its own thoroughness:

I was hired to take over development on a network topology mapper that came from an acquisition. The product did not work except in small test environments. Every customer demo was a failure.

The code below was used to determine if two ports on two different switches are connected. This process was repeated for every switch in the network. As the number of switches, ports, and MAC addresses increased the run time of the product went up exponentially and typically crashed with an array index out of bounds exception. The code below is neatly presented, the actual code took me over a day of repeatedly saying "WTF?" before I realized the original programmer had no idea what a Map or Set or List was. But after eliminating the arrays the flawed matching algorithm was still there and so shortly all of the acquired code was thrown away and the mapper was re-written from scratch with more efficient ways of connecting switches.


public class Switch {
    Array[] allMACs = new Array[numMACs];
    Array[] portIndexes = new Array[numPorts];
    Array[] ports = new Array[numPorts];

    public void load() {
        // load allMACs by reading switch via SNMP
        // psuedo code to avoid lots of irrelevant SNMP code
        int portCounter = 0;
        int macCounter = 0;
        for each port {
            ports[portCounter] = port;
            portIndexes[portCounter] = macCounter;
            for each MAC on port {
                 allMACs[macCounter++] = MAC;
            }
        }
    }

    public Array[] getMACsForPort(int port) {
        int startIndex;
        int endIndex;
        for (int ictr = 0; ictr < ports.length; ictr++) {
            if (ports[ictr] == port) {
                startIndex = portIndexes[ictr];
                endIndex = portIndexes[ictr + 1];
            }
        }
        Array[] portMACS = new Array[endIndex - startIndex];
        int pctr = 0;
        for (int ictr = startIndex; ictr < endIndex - 1; ictr++) {
            portMACS[pctr++] = allMACs[ictr];
        }
        return(portMACS);
    }
}

...
for every switch in the network {
    for every other switch in the network {
        for every port on switch {
            Array[] switchPortMACs = Switch.getMACsForPort(port);
            for every port on other switch {
                Array[] otherSwitchPortMACs = OtherSwitch.getMACsForPort(other port);
                if (intersect switchPortMACs with otherSwitchPortMACs == true) {
                   connect switch.port with otherSwitch.port;
                }
            }
        }
    }
}

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!