예제 #1
0
    QString getAssignmentChain(NetPath path, NetworkOverseer const& network)
    {
        if ( path.size() == 0 )
            return QString("none");

        QStringList result;
        for ( NetPath::iterator i = path.begin(); i != path.end(); i++)
        {
            NetworkingElement * ne = *i;
            if ( ne->isSwitch() ) 
            {
                uint uid = network.getIdByElement(ne);
                result << QString().setNum(uid);
            }
        }

        NetPath::iterator first = path.begin(), last = path.end();
        if ( (*first)->isLink() && (*(--last))->isLink() )
        {
            Link * begin = static_cast<Link *>(*first);
            Link * end = static_cast<Link *>(*last);
            uint firstComp = network.getIdByElement(begin->getLinkedComputationalElement());
            uint lastComp = network.getIdByElement(end->getLinkedComputationalElement());
            result.prepend(QString().setNum(firstComp));
            result.append(QString().setNum(lastComp));
        
        } 
        return result.join(";");
    }
예제 #2
0
    bool isReplica(Assignment * a, Element * element, NetPath& path)
    {
        if ( path.size() == 0 || !element->isStore() )
           return false;

        Element* first = static_cast<Link*>(path[0])->getLinkedComputationalElement();
        Element* last = static_cast<Link*>(path[path.size()-1])->getLinkedComputationalElement();
        if ( first->isStore() && a->isReplicaOnStore(static_cast<Storage*>(element), 
                 static_cast<Store*>(first)) )
           return true;

        if ( last->isStore() && a->isReplicaOnStore(static_cast<Storage*>(element),
                 static_cast<Store*>(last)) )
           return true;

        return false;
    }
NetPath VirtualLinkRouter::routeKShortestPathsALL(VirtualLink * virtualLink, Network * network, std::vector<NetPath> * pathStorage)
{   
    // links and switches that would be removed from the network,
    // they should be restored after algorithm's finish
    Links removedLinks;
    Switches removedSwitches;

    // first, create the graph with decreased capacities
    decreaseCapacities(virtualLink, network, &removedLinks, &removedSwitches);

    // Yen's algorithm
    NetPath shortest = searchPathDejkstra(virtualLink, network, K_SHORTEST_PATHS);
    if ( shortest.size() == 0 )
    {
        restoreCapacities(virtualLink, network, &removedLinks, &removedSwitches);
        return NetPath(); // no path found!
    }
    pathStorage->push_back(shortest);

    Links& links = network->getLinks();

    unsigned pathsFound = 1;
    long pathWeight = calculateKShortestPathWeight(shortest);
    bool isNewPathFound = true;
    while ( isNewPathFound && pathsFound < Criteria::kShortestPathDepth() )
    {
        NetPath candidate = shortest;
        NetPath::iterator it = shortest.begin();
        NetPath::iterator itEnd = shortest.end();
        isNewPathFound = false;
        bool isNewCandidateFound = false;

        Link * linkToRemove = NULL;
        for ( ; it != itEnd; ++it )
        {
            if ( (*it)->isLink() && links.find(static_cast<Link*>(*it)) != links.end() )
            {
                // removing link and trying dejkstra
                links.erase(links.find(static_cast<Link*>(*it)));
                NetPath newPath = searchPathDejkstra(virtualLink, network, K_SHORTEST_PATHS);
                if ( newPath.size() == shortest.size() )
                {
                    isNewPathFound = true;
                    pathStorage->push_back(newPath);
                    ++pathsFound;
                    long weight = calculateKShortestPathWeight(newPath);
                    if ( weight > pathWeight )
                    {
                        isNewCandidateFound = true;
                        linkToRemove = static_cast<Link*>(*it);
                        pathWeight = weight;
                        candidate = newPath;
                        if ( pathsFound == Criteria::kShortestPathDepth() )
                            break;
                    }
                    if ( linkToRemove == NULL )
                        linkToRemove = static_cast<Link*>(*it); // to avoid the situation with removing NULL-link
                }
                links.insert(static_cast<Link*>(*it)); // inserting link again
            }
        }

        if ( isNewCandidateFound )
            shortest = candidate;

        if ( isNewPathFound && pathsFound != Criteria::kShortestPathDepth() )
        {
            // restoring the capacity of link being removed
            linkToRemove->RemoveAssignment(virtualLink);
            links.erase(links.find(linkToRemove));
            removedLinks.insert(linkToRemove);
        }
    }

    // restoring removed capacities
    restoreCapacities(virtualLink, network, &removedLinks, &removedSwitches);

    return shortest;
}
예제 #4
0
Algorithm::Result FirstFitAlgorithm::schedule()
{
    for (Requests::iterator i = requests.begin(); i != requests.end(); i++)
    {
        Request * request = *i;
        Assignment * assignment = new Assignment(request);
        Algorithm::Result result = SUCCESS;

        Nodes & vms = request->getVirtualMachines();
        for (Nodes::iterator vmi = vms.begin(); vmi != vms.end(); vmi++)
        {
            Node * vm = *vmi;
            Nodes & nodes = network->getNodes();
            Nodes::iterator n;
            for (n = nodes.begin(); n != nodes.end(); n++)
            {
                Node * node = *n;
                if ( node->isAssignmentPossible(*vm) )
                {
                    node->assign(*vm);
                    assignment->AddAssignment(vm, node);
                    break;
                }  
            }
            
            if ( n == nodes.end() )
            {
                result = FAILURE;
                break;
            }
        }

        Stores & storages = request->getStorages();
        for (Stores::iterator s = storages.begin(); s != storages.end(); s++)
        {
            Store * storage = *s;
            Stores & stores = network->getStores();
            Stores::iterator si;
            for ( si = stores.begin(); si != stores.end(); si++)
            {
                Store * store = *si;
                if ( store->isAssignmentPossible(*storage) )
                {
                    store->assign(*storage);
                    assignment->AddAssignment(storage, store);
                    break;
                }
            }

            if ( si == storages.end() )
            {
                result = FAILURE;
                break;
            }
        }

        Links & vlinks = request->getVirtualLinks();
        for (Links::iterator l = vlinks.begin(); l != vlinks.end(); l++)
        {
            Link * dummy = createDummyLink(*l, assignment);

            if ( dummy == 0 )
            {
                result = FAILURE;
                break;
            }

            NetPath netPath = VirtualLinkRouter::routeDejkstra(dummy, network);
            delete dummy;

            if ( netPath.size() == 0 )
            {
                result = FAILURE;
                break;
            } 

            for ( NetPath::iterator i = netPath.begin(); i != netPath.end(); i++)
            {
               (*i)->assign(**l);
            }
            assignment->AddAssignment(*l, netPath);
        }

        if ( result == FAILURE )
            delete assignment;
        else
            assignments.insert(assignment);
    }

    printf("Assigned total of %d from %d requests", assignments.size(), requests.size());
    if ( assignments.size() == requests.size() )
        return SUCCESS;
    else if ( assignments.size() != 0 )
        return PARTIAL;
    else
        return FAILURE;
}