示例#1
0
void GridManager::Update(uint32 diff)
{
    for (auto machine : _machines)
        machine->Update(diff);

    for (auto pMachine : _priorityMachines)
        pMachine->Update(diff);

    if (!_waitingJobs.empty())
    {
        Job* job = _waitingJobs.front();
        if (job->GetPriority() == 0)
        {
            std::vector<Machine*> machineVector; // TODO: Every time a job is added this sorted vector is being rebuilt; change that
            machineVector.reserve(_machines.size());

            // put machines in the map into a vector so we can sort them
            std::transform(_machines.begin(), _machines.end(), std::back_inserter(machineVector),
                [](MachineSet::value_type val) { return val; });

            // sort of load balancing, better machines get assigned jobs first
            std::sort(machineVector.begin(), machineVector.end(), [](Machine* m1, Machine* m2) {
                double score1 = ((m1->GetMaxJobs() - m1->GetNumberOfCurrentJobs()) + m1->GetAvailableDiskSpace() + m1->GetAvailableRAM());
                double score2 = ((m2->GetMaxJobs() - m2->GetNumberOfCurrentJobs()) + m2->GetAvailableDiskSpace() + m2->GetAvailableRAM());

                return score1 > score2;
            });

            for (auto& mach : machineVector)
            {
                if (mach->AddJob(job))
                {
                    sLog(Console)->Log("Job %s added to machine %s", job->GetName().c_str(), mach->GetName().c_str());
                    _waitingJobs.pop();
                }
            }
        }
        else
        {
            std::vector<PriorityMachine*> machineVector; // TODO: Every time a job is added this sorted vector is being rebuilt; change that
            machineVector.reserve(_priorityMachines.size());

            // put machines in the map into a vector so we can sort them
            std::transform(_priorityMachines.begin(), _priorityMachines.end(), std::back_inserter(machineVector),
                [](PriorityMachineSet::value_type val) { return val; });

            // sort of load balancing, better machines get assigned jobs first
            std::sort(machineVector.begin(), machineVector.end(), [](PriorityMachine* m1, PriorityMachine* m2) {
                double score1 = ((m1->GetMaxJobs() - m1->GetNumberOfCurrentJobs()) + m1->GetAvailableDiskSpace() + m1->GetAvailableRAM());
                double score2 = ((m2->GetMaxJobs() - m2->GetNumberOfCurrentJobs()) + m2->GetAvailableDiskSpace() + m2->GetAvailableRAM());

                return score1 > score2;
            });

            for (auto& mach : machineVector)
            {
                if (mach->AddJob(job))
                {
                    sLog(Console)->Log("Job %s added to machine %s", job->GetName().c_str(), mach->GetName().c_str());
                    _waitingJobs.pop();
                }
            }
        }
    }

    _idleUsers.Update(diff);
}