Skip to content
Snippets Groups Projects
Commit acbd989b authored by Maël Madon's avatar Maël Madon
Browse files

refac: new class UserSubmitQueue, use of class Queue was misleading. Issue#13

parent e4c85e93
Branches
Tags
1 merge request!14Submission queue for replay users
Pipeline #5514 passed with warnings
......@@ -206,6 +206,14 @@ bool JobComparator::operator()(Job j1, Job j2) const
return JobComparator::operator()(&j1, &j2);
}
bool JobSubmissionOrder::operator()(const Job *j1, const Job *j2) const
{
if (j1->submission_time == j2->submission_time)
return jobcmp(j1, j2);
else
return j1->submission_time < j2->submission_time;
}
bool SessionComparator::operator()(const Session *s1, const Session *s2) const
{
PPK_ASSERT_ERROR(
......@@ -214,6 +222,72 @@ bool SessionComparator::operator()(const Session *s1, const Session *s2) const
return s1->start_time > s2->start_time;
}
UserSubmitQueue::UserSubmitQueue(
const std::string user_name, const std::string input_json)
{
/* Parse the JSON input... */
Document doc = Parser::parse_input_json(input_json);
string error_prefix = "Invalid JSON file '" + input_json + "'";
PPK_ASSERT_ERROR(
doc.IsObject(), "%s: not a JSON object", error_prefix.c_str());
PPK_ASSERT_ERROR(doc.HasMember("jobs"), "%s: the 'jobs' array is missing",
error_prefix.c_str());
const Value &jobs = doc["jobs"];
PPK_ASSERT_ERROR(jobs.IsArray(), "%s: the 'jobs' member is not an array",
error_prefix.c_str());
for (SizeType i = 0; i < jobs.Size();
i++)
{
const Value &job_json_description = jobs[i];
Job *j = Parser::job_from_json_object(user_name, job_json_description);
_jobs.push_back(j);
}
/* Sort the list by submission time */
_jobs.sort(submission_order);
}
UserSubmitQueue::~UserSubmitQueue(){
for (auto j : _jobs){
delete j;
}
}
void UserSubmitQueue::insert_sorted(const Job *j)
{
Job *job = new Job;
*job = *job;
auto start = _jobs.begin();
auto end = _jobs.end();
while(start!=end && submission_order((*start), job)) {
++start;
}
_jobs.insert(start,job);
}
const Job *UserSubmitQueue::top() const
{
return _jobs.front();
}
const Job *UserSubmitQueue::pop()
{
Job *j = _jobs.front();
_jobs.pop_front();
return j;
}
bool UserSubmitQueue::is_empty() const
{
return _jobs.size() == 0;
}
// Mainly copy-paste from the 'load_from_json' methods from the classes
// Workload, Jobs and Profiles in batsim
Document Parser::parse_input_json(const string input_json)
......@@ -243,35 +317,6 @@ Document Parser::parse_input_json(const string input_json)
return doc;
}
Queue *Parser::job_queue_from_input_json(
string user_name, const string input_json)
{
Queue *job_trace = new Queue(new FCFSOrder());
Document doc = Parser::parse_input_json(input_json);
string error_prefix = "Invalid JSON file '" + input_json + "'";
PPK_ASSERT_ERROR(
doc.IsObject(), "%s: not a JSON object", error_prefix.c_str());
PPK_ASSERT_ERROR(doc.HasMember("jobs"), "%s: the 'jobs' array is missing",
error_prefix.c_str());
const Value &jobs = doc["jobs"];
PPK_ASSERT_ERROR(jobs.IsArray(), "%s: the 'jobs' member is not an array",
error_prefix.c_str());
for (SizeType i = 0; i < jobs.Size();
i++) // Uses SizeType instead of size_t
{
const Value &job_json_description = jobs[i];
auto j = Parser::job_from_json_object(user_name, job_json_description);
job_trace->insert_job(j);
}
return job_trace;
}
Session *Parser::session_graph_from_SABjson(
const string user_name, const string input_json)
{
......
......@@ -56,6 +56,12 @@ struct JobComparator
bool operator()(Job j1, Job j2) const;
};
struct JobSubmissionOrder
{
JobComparator jobcmp;
bool operator()(const Job *j1, const Job *j2) const;
};
struct Profile
{
std::string name;
......@@ -86,6 +92,7 @@ struct SessionComparator
bool operator()(const Session *s1, const Session *s2) const;
};
/* Map of all jobs, for keeping track in the scheduler */
class Workload
{
public:
......@@ -116,7 +123,29 @@ private:
int _job_number = 0;
};
class Queue;
/* List of jobs to submit for users, sorted by submission time */
class UserSubmitQueue
{
public:
UserSubmitQueue(
const std::string user_name, const std::string input_json);
~UserSubmitQueue();
/* Insert the job in the sorted queue */
void insert_sorted(const Job *job);
/* Give a pointer to the first elem */
const Job *top() const;
/* Remove and give a pointer to the first elem */
const Job *pop();
bool is_empty() const;
private:
std::list<Job *> _jobs;
JobSubmissionOrder submission_order;
};
/**
* Useful parsers
......
......@@ -91,14 +91,14 @@ public:
Queue(SortableJobOrder * order);
~Queue();
/* Insert the job at the beginning of the queue, setting its release_date
* to the current date. */
void append_job(const Job * job, SortableJobOrder::UpdateInformation * update_info);
/* Insert the job at the beginning of the queue */
/* Insert the job at the beginning of the queue, setting its release_date
* to the job's submission date. */
void insert_job(const Job * job);
/* Insert the job at the right place in the queue.
* Caution: this function assumes that the queue is already sorted! */
void insert_sorted_job(const Job *job);
std::list<SortableJob *>::iterator remove_job(const Job * job);
std::list<SortableJob *>::iterator remove_job(std::list<SortableJob *>::iterator job_it);
void sort_queue(SortableJobOrder::UpdateInformation * update_info, SortableJobOrder::CompareInformation * compare_info = nullptr);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment