Commit 4518599e authored by Millian Poquet's avatar Millian Poquet
Browse files

[code] JsonProtocolWriter : Batsim->Sched

parent 0a2ed331
......@@ -2,10 +2,34 @@
#include <xbt.h>
void JsonProtocolWriter::append_submit_job(const std::string &job_id,
#include <rapidjson/stringbuffer.h>
#include <rapidjson/writer.h>
using namespace rapidjson;
using namespace std;
JsonProtocolWriter::JsonProtocolWriter() :
_alloc(_doc.GetAllocator())
{
_doc.SetObject();
}
JsonProtocolWriter::~JsonProtocolWriter()
{
}
void JsonProtocolWriter::append_nop(double date)
{
xbt_assert(date >= _last_date, "Date inconsistency");
_last_date = date;
_is_empty = false;
}
void JsonProtocolWriter::append_submit_job(const string &job_id,
double date,
const std::string &job_description,
const std::string &profile_description,
const string &job_description,
const string &profile_description,
bool acknowledge_submission)
{
xbt_assert(false, "Unimplemented!");
......@@ -16,10 +40,10 @@ void JsonProtocolWriter::append_submit_job(const std::string &job_id,
(void) acknowledge_submission;
}
void JsonProtocolWriter::append_execute_job(const std::string &job_id,
void JsonProtocolWriter::append_execute_job(const string &job_id,
const MachineRange &allocated_resources,
double date,
const std::string &executor_to_allocated_resource_mapping)
const string &executor_to_allocated_resource_mapping)
{
xbt_assert(false, "Unimplemented!");
(void) job_id;
......@@ -28,7 +52,7 @@ void JsonProtocolWriter::append_execute_job(const std::string &job_id,
(void) executor_to_allocated_resource_mapping;
}
void JsonProtocolWriter::append_reject_job(const std::string &job_id,
void JsonProtocolWriter::append_reject_job(const string &job_id,
double date)
{
xbt_assert(false, "Unimplemented!");
......@@ -36,7 +60,7 @@ void JsonProtocolWriter::append_reject_job(const std::string &job_id,
(void) date;
}
void JsonProtocolWriter::append_kill_job(const std::vector<std::string> &job_ids,
void JsonProtocolWriter::append_kill_job(const vector<string> &job_ids,
double date)
{
xbt_assert(false, "Unimplemented!");
......@@ -45,7 +69,7 @@ void JsonProtocolWriter::append_kill_job(const std::vector<std::string> &job_ids
}
void JsonProtocolWriter::append_set_resource_state(MachineRange resources,
const std::string &new_state,
const string &new_state,
double date)
{
xbt_assert(false, "Unimplemented!");
......@@ -86,65 +110,238 @@ void JsonProtocolWriter::append_query_request(void *anything,
void JsonProtocolWriter::append_simulation_starts(double date)
{
xbt_assert(false, "Unimplemented. TODO!!!");
(void) date;
/* {
"timestamp": 0.0,
"type": "SIMULATION_STARTS",
"data": {}
} */
xbt_assert(date >= _last_date, "Date inconsistency");
_last_date = date;
Value event(rapidjson::kObjectType);
event.AddMember("timestamp", Value().SetDouble(date), _alloc);
event.AddMember("type", Value().SetString("SIMULATION_STARTS"), _alloc);
event.AddMember("data", Value().SetObject(), _alloc);
_events.PushBack(event, _alloc);
}
void JsonProtocolWriter::append_simulation_ends(double date)
{
xbt_assert(false, "Unimplemented. TODO!!!");
(void) date;
/* {
"timestamp": 0.0,
"type": "SIMULATION_ENDS",
"data": {}
} */
xbt_assert(date >= _last_date, "Date inconsistency");
_last_date = date;
Value event(rapidjson::kObjectType);
event.AddMember("timestamp", Value().SetDouble(date), _alloc);
event.AddMember("type", Value().SetString("SIMULATION_ENDS"), _alloc);
event.AddMember("data", Value().SetObject(), _alloc);
_events.PushBack(event, _alloc);
}
void JsonProtocolWriter::append_job_submitted(std::vector<std::string> job_ids,
void JsonProtocolWriter::append_job_submitted(const vector<string> & job_ids,
double date)
{
xbt_assert(false, "Unimplemented. TODO!!!");
(void) job_ids;
(void) date;
/* {
"timestamp": 10.0,
"type": "JOB_SUBMITTED",
"data": {
"job_ids": ["w0!1", "w0!2"]
}
} */
xbt_assert(date >= _last_date, "Date inconsistency");
_last_date = date;
Value event(rapidjson::kObjectType);
event.AddMember("timestamp", Value().SetDouble(date), _alloc);
event.AddMember("type", Value().SetString("JOB_SUBMITTED"), _alloc);
Value jobs(rapidjson::kArrayType);
jobs.Reserve(job_ids.size(), _alloc);
for (const string & job_id : job_ids)
jobs.PushBack(Value().SetString(job_id.c_str(), _alloc), _alloc);
event.AddMember("data", Value().SetObject().AddMember("job_ids", jobs, _alloc), _alloc);
_events.PushBack(event, _alloc);
}
void JsonProtocolWriter::append_job_completed(const std::string &job_id,
void JsonProtocolWriter::append_job_completed(const string & job_id,
const string & job_status,
double date)
{
xbt_assert(false, "Unimplemented. TODO!!!");
(void) job_id;
(void) date;
/* {
"timestamp": 10.0,
"type": "JOB_COMPLETED",
"data": {"job_id": "w0!1", "status": "SUCCESS"}
} */
const static vector<string> accepted_statuses = {"SUCCESS", "TIMEOUT"};
xbt_assert(date >= _last_date, "Date inconsistency");
xbt_assert(std::find(accepted_statuses.begin(), accepted_statuses.end(), job_status) != accepted_statuses.end(),
"Unsupported job status '%s'!", job_status.c_str());
_last_date = date;
Value data(rapidjson::kObjectType);
data.AddMember("job_id", Value().SetString(job_id.c_str(), _alloc), _alloc);
data.AddMember("status", Value().SetString(job_status.c_str(), _alloc), _alloc);
Value event(rapidjson::kObjectType);
event.AddMember("timestamp", Value().SetDouble(date), _alloc);
event.AddMember("type", Value().SetString("JOB_COMPLETED"), _alloc);
event.AddMember("data", data, _alloc);
_events.PushBack(event, _alloc);
}
void JsonProtocolWriter::append_job_killed(std::vector<std::string> job_ids,
void JsonProtocolWriter::append_job_killed(const vector<string> & job_ids,
double date)
{
xbt_assert(false, "Unimplemented. TODO!!!");
(void) job_ids;
(void) date;
/* {
"timestamp": 10.0,
"type": "JOB_KILLED",
"data": {"job_ids": ["w0!1", "w0!2"]}
} */
xbt_assert(date >= _last_date, "Date inconsistency");
_last_date = date;
Value event(rapidjson::kObjectType);
event.AddMember("timestamp", Value().SetDouble(date), _alloc);
event.AddMember("type", Value().SetString("JOB_KILLED"), _alloc);
Value jobs(rapidjson::kArrayType);
jobs.Reserve(job_ids.size(), _alloc);
for (const string & job_id : job_ids)
jobs.PushBack(Value().SetString(job_id.c_str(), _alloc), _alloc);
event.AddMember("data", Value().SetObject().AddMember("job_ids", jobs, _alloc), _alloc);
_events.PushBack(event, _alloc);
}
void JsonProtocolWriter::append_resource_state_changed(const MachineRange &resources,
const std::string &new_state,
void JsonProtocolWriter::append_resource_state_changed(const MachineRange & resources,
const string & new_state,
double date)
{
xbt_assert(false, "Unimplemented. TODO!!!");
(void) resources;
(void) new_state;
(void) date;
/* {
"timestamp": 10.0,
"type": "RESOURCE_STATE_CHANGED",
"data": {"resources": "1 2 3-5", "state": "42"}
} */
xbt_assert(date >= _last_date, "Date inconsistency");
_last_date = date;
Value data(rapidjson::kObjectType);
data.AddMember("resources",
Value().SetString(resources.to_string_hyphen(" ", "-").c_str(), _alloc), _alloc);
data.AddMember("state", Value().SetString(new_state.c_str(), _alloc), _alloc);
Value event(rapidjson::kObjectType);
event.AddMember("timestamp", Value().SetDouble(date), _alloc);
event.AddMember("type", Value().SetString("RESOURCE_STATE_CHANGED"), _alloc);
event.AddMember("data", data, _alloc);
_events.PushBack(event, _alloc);
}
void JsonProtocolWriter::append_query_reply(void *anything,
double date)
void JsonProtocolWriter::append_query_reply_energy(double consumed_energy,
double date)
{
xbt_assert(false, "Unimplemented. TODO!!!");
(void) anything;
(void) date;
/* {
"timestamp": 10.0,
"type": "QUERY_REPLY",
"data": {"energy_consumed": "12500" }
} */
xbt_assert(date >= _last_date, "Date inconsistency");
_last_date = date;
Value event(rapidjson::kObjectType);
event.AddMember("timestamp", Value().SetDouble(date), _alloc);
event.AddMember("type", Value().SetString("QUERY_REPLY"), _alloc);
event.AddMember("data", Value().SetObject().AddMember("energy_consumed", Value().SetDouble(consumed_energy), _alloc), _alloc);
_events.PushBack(event, _alloc);
}
void JsonProtocolWriter::clear()
{
xbt_assert(false, "Unimplemented. TODO!!!");
_is_empty = true;
_doc.RemoveAllMembers();
_events.SetArray();
}
std::string JsonProtocolWriter::generate_current_message(double date)
string JsonProtocolWriter::generate_current_message(double date)
{
xbt_assert(false, "Unimplemented. TODO!!!");
(void) date;
xbt_assert(date >= _last_date, "Date inconsistency");
xbt_assert(_events.IsArray(),
"Successive calls to JsonProtocolWriter::generate_current_message without calling "
"the clear() method is not supported");
// Generating the content
_doc.AddMember("now", Value().SetDouble(date), _alloc);
_doc.AddMember("events", _events, _alloc);
// Dumping the content to a buffer
StringBuffer buffer;
Writer<rapidjson::StringBuffer> writer(buffer);
_doc.Accept(writer);
// Returning the buffer as a string
return string(buffer.GetString());
}
bool test_json_writer()
{
AbstractProtocolWriter * proto_writer = new JsonProtocolWriter;
printf("EMPTY content:\n%s\n", proto_writer->generate_current_message(0).c_str());
proto_writer->clear();
proto_writer->append_nop(0);
printf("NOP content:\n%s\n", proto_writer->generate_current_message(42).c_str());
proto_writer->clear();
proto_writer->append_simulation_starts(10);
printf("SIM_START content:\n%s\n", proto_writer->generate_current_message(42).c_str());
proto_writer->clear();
proto_writer->append_simulation_ends(10);
printf("SIM_END content:\n%s\n", proto_writer->generate_current_message(42).c_str());
proto_writer->clear();
proto_writer->append_job_submitted({"w0!j0", "w0!j1"}, 10);
printf("JOB_SUBMITTED content:\n%s\n", proto_writer->generate_current_message(42).c_str());
proto_writer->clear();
proto_writer->append_job_completed("w0!j0", "SUCCESS", 10);
printf("JOB_COMPLETED content:\n%s\n", proto_writer->generate_current_message(42).c_str());
proto_writer->clear();
proto_writer->append_job_killed({"w0!j0", "w0!j1"}, 10);
printf("JOB_KILLED content:\n%s\n", proto_writer->generate_current_message(42).c_str());
proto_writer->clear();
proto_writer->append_resource_state_changed(MachineRange::from_string_hyphen("1,3-5"), "42", 10);
printf("RESOURCE_STATE_CHANGED content:\n%s\n", proto_writer->generate_current_message(42).c_str());
proto_writer->clear();
proto_writer->append_query_reply_energy(65535, 10);
printf("QUERY_REPLY (energy) content:\n%s\n", proto_writer->generate_current_message(42).c_str());
proto_writer->clear();
delete proto_writer;
return true;
}
......@@ -3,6 +3,8 @@
#include <vector>
#include <string>
#include <rapidjson/document.h>
#include "machine_range.hpp"
/**
......@@ -11,6 +13,18 @@
class AbstractProtocolWriter
{
public:
/**
* @brief Destructor
*/
virtual ~AbstractProtocolWriter() {};
// Bidirectional messages
/**
* @brief Appends a NOP message.
* @param[in] date The event date. Must be greater than or equal to the previous event.
*/
virtual void append_nop(double date) = 0;
// Messages from the Scheduler to Batsim
/**
* @brief Appends a SUBMIT_JOB event.
......@@ -124,15 +138,17 @@ public:
* @param[in] job_ids The identifiers of the submitted jobs.
* @param[in] date The event date. Must be greater than or equal to the previous event.
*/
virtual void append_job_submitted(std::vector<std::string> job_ids,
virtual void append_job_submitted(const std::vector<std::string> & job_ids,
double date) = 0;
/**
* @brief Appends a JOB_COMPLETED event.
* @param[in] job_id The identifier of the job that has completed.
* @param[in] job_status The job status
* @param[in] date The event date. Must be greater than or equal to the previous event.
*/
virtual void append_job_completed(const std::string & job_id,
const std::string & job_status,
double date) = 0;
/**
......@@ -140,7 +156,7 @@ public:
* @param[in] job_ids The identifiers of the jobs that have been killed.
* @param[in] date The event date. Must be greater than or equal to the previous event.
*/
virtual void append_job_killed(std::vector<std::string> job_ids,
virtual void append_job_killed(const std::vector<std::string> & job_ids,
double date) = 0;
/**
......@@ -154,12 +170,12 @@ public:
double date) = 0;
/**
* @brief Appends a QUERY_REPLY event.
* @param[in] anything ...
* @brief Appends a QUERY_REPLY (energy) event.
* @param[in] consumed_energy The total consumed energy in joules
* @param[in] date The event date. Must be greater than or equal to the previous event.
*/
virtual void append_query_reply(void * anything,
double date) = 0;
virtual void append_query_reply_energy(double consumed_energy,
double date) = 0;
// Management functions
/**
......@@ -174,6 +190,12 @@ public:
* @return A string representation of the events added since the last call to clear.
*/
virtual std::string generate_current_message(double date) = 0;
/**
* @brief Returns whether the Writer has content
* @return Whether the Writer has content
*/
virtual bool is_empty() = 0;
};
/**
......@@ -182,6 +204,24 @@ public:
class JsonProtocolWriter : public AbstractProtocolWriter
{
public:
/**
* @brief Creates an empty JsonProtocolWriter
*/
JsonProtocolWriter();
/**
* @brief Destroys a JsonProtocolWriter
*/
~JsonProtocolWriter();
// Bidirectional messages
/**
* @brief Appends a NOP message.
* @param[in] date The event date. Must be greater than or equal to the previous event.
*/
void append_nop(double date);
// Messages from the Scheduler to Batsim
/**
* @brief Appends a SUBMIT_JOB event.
......@@ -295,15 +335,17 @@ public:
* @param[in] job_ids The identifiers of the submitted jobs.
* @param[in] date The event date. Must be greater than or equal to the previous event.
*/
void append_job_submitted(std::vector<std::string> job_ids,
void append_job_submitted(const std::vector<std::string> & job_ids,
double date);
/**
* @brief Appends a JOB_COMPLETED event.
* @param[in] job_id The identifier of the job that has completed.
* @param[in] job_status The job status
* @param[in] date The event date. Must be greater than or equal to the previous event.
*/
void append_job_completed(const std::string & job_id,
const std::string & job_status,
double date);
/**
......@@ -311,7 +353,7 @@ public:
* @param[in] job_ids The identifiers of the jobs that have been killed.
* @param[in] date The event date. Must be greater than or equal to the previous event.
*/
void append_job_killed(std::vector<std::string> job_ids,
void append_job_killed(const std::vector<std::string> & job_ids,
double date);
/**
......@@ -325,12 +367,12 @@ public:
double date);
/**
* @brief Appends a QUERY_REPLY event.
* @param[in] anything ...
* @brief Appends a QUERY_REPLY (energy) event.
* @param[in] consumed_energy The total consumed energy in joules
* @param[in] date The event date. Must be greater than or equal to the previous event.
*/
void append_query_reply(void * anything,
double date);
void append_query_reply_energy(double consumed_energy,
double date);
// Management functions
/**
......@@ -345,4 +387,24 @@ public:
* @return A string representation of the events added since the last call to clear.
*/
std::string generate_current_message(double date);
/**
* @brief Returns whether the Writer has content
* @return Whether the Writer has content
*/
bool is_empty() { return _is_empty; }
private:
bool _is_empty = true;
double _last_date = -1;
rapidjson::Document _doc;
rapidjson::Document::AllocatorType & _alloc;
rapidjson::Value _events = rapidjson::Value(rapidjson::kArrayType);
};
/**
* @brief Tests whether the JsonProtocolWriter behaves correctly
* @return Whether the JsonProtocolWriter behaves as expected
*/
bool test_json_writer();
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment