protocol.hpp 21 KB
Newer Older
1
2
#pragma once

Millian Poquet's avatar
Millian Poquet committed
3
#include <functional>
4
5
#include <vector>
#include <string>
Millian Poquet's avatar
Millian Poquet committed
6
#include <map>
7

8
9
#include <rapidjson/document.h>

10
#include "machine_range.hpp"
Millian Poquet's avatar
Millian Poquet committed
11
#include "ipp.hpp"
12

Millian Poquet's avatar
Millian Poquet committed
13
14
struct BatsimContext;

15
16
17
18
19
20
/**
 * @brief Does the interface between protocol semantics and message representation.
 */
class AbstractProtocolWriter
{
public:
21
22
23
    /**
     * @brief Destructor
     */
Millian Poquet's avatar
Millian Poquet committed
24
    virtual ~AbstractProtocolWriter() {}
25
26
27
28
29
30
31
32

    // 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;

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    // Messages from the Scheduler to Batsim
    /**
     * @brief Appends a SUBMIT_JOB event.
     * @details The job_description and profile_descriptions are either both given or both empty.
     *          If they are given, the job and profile information is sent within the protocol.
     *          Otherwise, it is sent by another channel (probably redis).
     * @param[in] job_id The job identifier. It must not conflict with existing job identifiers
     *            within Batism.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     * @param[in] job_description The job description string. Can be empty.
     * @param[in] profile_description The profile description string. Can be empty.
     * @param[in] acknowledge_submission Whether Batsim should acknowledge the upcoming job
     *            submission or not.
     */
    virtual void append_submit_job(const std::string & job_id,
                                   double date,
                                   const std::string & job_description = "",
                                   const std::string & profile_description = "",
                                   bool acknowledge_submission = false) = 0;

    /**
54
     * @brief Appends an EXECUTE_JOB event.
55
56
57
58
59
60
     * @param[in] job_id The job identifier. It must be known by Batsim.
     * @param[in] allocated_resources The resources on which the job should be executed.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     * @param[in] executor_to_allocated_resource_mapping Optional.
     *            Allows to give a custom mapping from executors to allocated resources.
     *            By default, the number of allocated resources must equals
61
     *            the job size, and executor i is launched on allocated resource i.
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
     */
    virtual void append_execute_job(const std::string & job_id,
                                    const MachineRange & allocated_resources,
                                    double date,
                                    const std::string & executor_to_allocated_resource_mapping = "") = 0;

    /**
     * @brief Appends a REJECT_JOB event.
     * @param[in] job_id The job identifier. Must be known by Batsim. Must be in the SUBMITTED state.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    virtual void append_reject_job(const std::string & job_id,
                                   double date) = 0;

    /**
     * @brief Appends a KILL_JOB event.
     * @param[in] job_ids The job identifiers of the jobs to kill. Must be known by Batsim.
     *                    Must be in the RUNNING state (COMPLETED jobs are ignored).
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    virtual void append_kill_job(const std::vector<std::string> & job_ids,
                                 double date) = 0;

    /**
     * @brief Appends a SET_RESOURCE_STATE event.
     * @param[in] resources The resources whose state must be set.
     * @param[in] new_state The state the machines should be set into.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    virtual void append_set_resource_state(MachineRange resources,
                                           const std::string & new_state,
                                           double date)  = 0;

    /**
     * @brief Appends a CALL_ME_LATER event.
     * @param[in] future_date The date at which the decision process shall be called.
     *            Must be greater than date.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    virtual void append_call_me_later(double future_date,
                                      double date) = 0;

    /**
     * @brief Appends a SUBMITER_MAY_SUBMIT_JOBS event.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    virtual void append_submitter_may_submit_jobs(double date) = 0;

    /**
     * @brief Appends a SCHEDULER_FINISHED_SUBMITTING_JOBS event.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    virtual void append_scheduler_finished_submitting_jobs(double date) = 0;

    /**
     * @brief Appends a QUERY_REQUEST event.
     * @param[in] anything ...
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    virtual void append_query_request(void * anything,
                                      double date) = 0;





    // Messages from Batsim to the Scheduler
    /**
130
     * @brief Appends a SIMULATION_BEGINS event.
131
132
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
133
    virtual void append_simulation_begins(double date) = 0;
134
135
136
137
138
139
140
141
142
143
144
145

    /**
     * @brief Appends a SIMULATION_ENDS event.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    virtual void append_simulation_ends(double date) = 0;

    /**
     * @brief Appends a JOB_SUBMITTED event.
     * @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.
     */
146
    virtual void append_job_submitted(const std::vector<std::string> & job_ids,
147
148
149
150
151
                                      double date) = 0;

    /**
     * @brief Appends a JOB_COMPLETED event.
     * @param[in] job_id The identifier of the job that has completed.
152
     * @param[in] job_status The job status
153
154
155
     * @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,
156
                                      const std::string & job_status,
157
158
159
160
161
162
163
                                      double date) = 0;

    /**
     * @brief Appends a JOB_KILLED event.
     * @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.
     */
164
    virtual void append_job_killed(const std::vector<std::string> & job_ids,
165
166
167
168
169
170
171
172
173
174
175
176
177
                                   double date) = 0;

    /**
     * @brief Appends a RESOURCE_STATE_CHANGED event.
     * @param[in] resources The resources whose state has changed.
     * @param[in] new_state The state the machines are now in.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    virtual void append_resource_state_changed(const MachineRange & resources,
                                               const std::string & new_state,
                                               double date) = 0;

    /**
178
179
     * @brief Appends a QUERY_REPLY (energy) event.
     * @param[in] consumed_energy The total consumed energy in joules
180
181
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
182
183
    virtual void append_query_reply_energy(double consumed_energy,
                                           double date) = 0;
184
185
186

    // Management functions
    /**
187
     * @brief Clears inner content. Should called directly after generate_current_message.
188
189
190
191
192
193
194
195
196
197
     */
    virtual void clear() = 0;

    /**
     * @brief Generates a string representation of the message containing all the events since the
     *        last call to clear.
     * @param[in] date The message date. Must be greater than or equal to the inner events dates.
     * @return A string representation of the events added since the last call to clear.
     */
    virtual std::string generate_current_message(double date) = 0;
198
199
200
201
202
203

    /**
     * @brief Returns whether the Writer has content
     * @return Whether the Writer has content
     */
    virtual bool is_empty() = 0;
204
205
};

206
207
208
/**
 * @brief The JSON implementation of the AbstractProtocolWriter
 */
209
210
211
class JsonProtocolWriter : public AbstractProtocolWriter
{
public:
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
    /**
     * @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);

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    // Messages from the Scheduler to Batsim
    /**
     * @brief Appends a SUBMIT_JOB event.
     * @details The job_description and profile_descriptions are either both given or both empty.
     *          If they are given, the job and profile information is sent within the protocol.
     *          Otherwise, it is sent by another channel (probably redis).
     * @param[in] job_id The job identifier. It must not conflict with existing job identifiers
     *            within Batism.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     * @param[in] job_description The job description string. Can be empty.
     * @param[in] profile_description The profile description string. Can be empty.
     * @param[in] acknowledge_submission Whether Batsim should acknowledge the upcoming job
     *            submission or not.
     */
    void append_submit_job(const std::string & job_id,
                           double date,
                           const std::string & job_description = "",
                           const std::string & profile_description = "",
                           bool acknowledge_submission = false);

    /**
251
     * @brief Appends an EXECUTE_JOB event.
252
253
254
255
256
257
     * @param[in] job_id The job identifier. It must be known by Batsim.
     * @param[in] allocated_resources The resources on which the job should be executed.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     * @param[in] executor_to_allocated_resource_mapping Optional.
     *            Allows to give a custom mapping from executors to allocated resources.
     *            By default, the number of allocated resources must equals
258
     *            the job size, and executor i is launched on allocated resource i.
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
     */
    void append_execute_job(const std::string & job_id,
                            const MachineRange & allocated_resources,
                            double date,
                            const std::string & executor_to_allocated_resource_mapping = "");

    /**
     * @brief Appends a REJECT_JOB event.
     * @param[in] job_id The job identifier. Must be known by Batsim. Must be in the SUBMITTED state.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    void append_reject_job(const std::string & job_id,
                           double date);

    /**
     * @brief Appends a KILL_JOB event.
     * @param[in] job_ids The job identifiers of the jobs to kill. Must be known by Batsim.
     *                    Must be in the RUNNING state (COMPLETED jobs are ignored).
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    void append_kill_job(const std::vector<std::string> & job_ids,
                         double date);

    /**
     * @brief Appends a SET_RESOURCE_STATE event.
     * @param[in] resources The resources whose state must be set.
     * @param[in] new_state The state the machines should be set into.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    void append_set_resource_state(MachineRange resources,
                                   const std::string & new_state,
                                   double date) ;

    /**
     * @brief Appends a CALL_ME_LATER event.
     * @param[in] future_date The date at which the decision process shall be called.
     *            Must be greater than date.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    void append_call_me_later(double future_date,
                              double date);

    /**
     * @brief Appends a SUBMITER_MAY_SUBMIT_JOBS event.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    void append_submitter_may_submit_jobs(double date);

    /**
     * @brief Appends a SCHEDULER_FINISHED_SUBMITTING_JOBS event.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    void append_scheduler_finished_submitting_jobs(double date);

    /**
     * @brief Appends a QUERY_REQUEST event.
     * @param[in] anything ...
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    void append_query_request(void * anything,
                              double date);





    // Messages from Batsim to the Scheduler
    /**
     * @brief Appends a SIMULATION_STARTS event.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
330
    void append_simulation_begins(double date);
331
332
333
334
335
336
337
338
339
340
341
342

    /**
     * @brief Appends a SIMULATION_ENDS event.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    void append_simulation_ends(double date);

    /**
     * @brief Appends a JOB_SUBMITTED event.
     * @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.
     */
343
    void append_job_submitted(const std::vector<std::string> & job_ids,
344
345
346
347
348
                              double date);

    /**
     * @brief Appends a JOB_COMPLETED event.
     * @param[in] job_id The identifier of the job that has completed.
349
     * @param[in] job_status The job status
350
351
352
     * @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,
353
                              const std::string & job_status,
354
355
356
357
358
359
360
                              double date);

    /**
     * @brief Appends a JOB_KILLED event.
     * @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.
     */
361
    void append_job_killed(const std::vector<std::string> & job_ids,
362
363
364
365
366
367
368
369
370
371
372
373
374
                           double date);

    /**
     * @brief Appends a RESOURCE_STATE_CHANGED event.
     * @param[in] resources The resources whose state has changed.
     * @param[in] new_state The state the machines are now in.
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
    void append_resource_state_changed(const MachineRange & resources,
                                       const std::string & new_state,
                                       double date);

    /**
375
376
     * @brief Appends a QUERY_REPLY (energy) event.
     * @param[in] consumed_energy The total consumed energy in joules
377
378
     * @param[in] date The event date. Must be greater than or equal to the previous event.
     */
379
380
    void append_query_reply_energy(double consumed_energy,
                                   double date);
381
382
383

    // Management functions
    /**
384
     * @brief Clears inner content. Should be called directly after generate_current_message.
385
386
387
388
389
390
391
392
393
394
     */
    void clear();

    /**
     * @brief Generates a string representation of the message containing all the events since the
     *        last call to clear.
     * @param[in] date The message date. Must be greater than or equal to the inner events dates.
     * @return A string representation of the events added since the last call to clear.
     */
    std::string generate_current_message(double date);
395
396
397
398
399
400
401
402
403
404
405
406
407

    /**
     * @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);
Millian Poquet's avatar
Millian Poquet committed
408
    const std::vector<std::string> accepted_statuses = {"SUCCESS", "TIMEOUT"};
409
};
410
411
412
413
414
415
416


/**
 * @brief Tests whether the JsonProtocolWriter behaves correctly
 * @return Whether the JsonProtocolWriter behaves as expected
 */
bool test_json_writer();
Millian Poquet's avatar
Millian Poquet committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

/**
 * @brief In charge of parsing a protocol message and injecting internal messages into the simulation
 */
class AbstractProtocolReader
{
public:
    /**
     * @brief Destructor
     */
    virtual ~AbstractProtocolReader() {}

    /**
     * @brief Parses a message and injects events in the simulation
     * @param[in] message The protocol message
     */
    virtual void parse_and_apply_message(const std::string & message) = 0;
};

/**
 * @brief In charge of parsing a JSON message and injecting messages into the simulation
 */
class JsonProtocolReader : public AbstractProtocolReader
{
public:
442
443
444
445
446
447
    /**
     * @brief Constructor
     * @param[in] context The BatsimContext
     */
    JsonProtocolReader(BatsimContext * context);

Millian Poquet's avatar
Millian Poquet committed
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
    /**
     * @brief Destructor
     */
    ~JsonProtocolReader();

    /**
     * @brief Parses a message and injects events in the simulation
     * @param[in] message The protocol message
     */
    void parse_and_apply_message(const std::string & message);

    /**
     * @brief Parses an event and injects it in the simulation
     * @param[in] event_object The event (JSON object)
     * @param[in] event_number The event number in [0,nb_events[.
     * @param[in] now The message timestamp
     */
    void parse_and_apply_event(const rapidjson::Value & event_object, int event_number, double now);

    /**
     * @brief Handles a QUERY_REQUEST event
     * @param[in] timestamp The event timestamp
     * @param[in] data_object The data associated with the event (JSON object)
     */
    void handle_query_request(int event_number, double timestamp, const rapidjson::Value & data_object);

Millian Poquet's avatar
Millian Poquet committed
474
475
476
477
478
479
480
    /**
     * @brief Handles a QUERY_REQUEST event
     * @param[in] timestamp The event timestamp
     * @param[in] data_object The data associated with the event (JSON object)
     */
    void handle_reject_job(int event_number, double timestamp, const rapidjson::Value & data_object);

481
482
483
484
485
486
487
    /**
     * @brief Handles an EXECUTE_JOB event
     * @param[in] timestamp The event timestamp
     * @param[in] data_object The data associated with the event (JSON object)
     */
    void handle_execute_job(int event_number, double timestamp, const rapidjson::Value & data_object);

488
489
490
491
492
493
494
495
496
497
498
499
500
501
    /**
     * @brief Handles a CALL_ME_LATER event
     * @param[in] timestamp The event timestamp
     * @param[in] data_object The data associated with the event (JSON object)
     */
    void handle_call_me_later(int event_number, double timestamp, const rapidjson::Value & data_object);

    /**
     * @brief Handles a SET_RESOURCE_STATE event
     * @param[in] timestamp The event timestamp
     * @param[in] data_object The data associated with the event (JSON object)
     */
    void handle_set_resource_state(int event_number, double timestamp, const rapidjson::Value & data_object);

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
    /**
     * @brief Handles a NOTIFY event
     * @param[in] timestamp The event timestamp
     * @param[in] data_object The data associated with the event (JSON object)
     */
    void handle_notify(int event_number, double timestamp, const rapidjson::Value & data_object);

    /**
     * @brief Handles a SUBMIT_JOB event
     * @param[in] timestamp The event timestamp
     * @param[in] data_object The data associated with the event (JSON object)
     */
    void handle_submit_job(int event_number, double timestamp, const rapidjson::Value & data_object);

    /**
     * @brief Handles a KILL_JOB event
     * @param[in] timestamp The event timestamp
     * @param[in] data_object The data associated with the event (JSON object)
     */
    void handle_kill_job(int event_number, double timestamp, const rapidjson::Value & data_object);

Millian Poquet's avatar
Millian Poquet committed
523
private:
524
525
526
527
528
529
530
    /**
     * @brief Sends a message at a given time, sleeping to reach the given time if needed
     * @param[in] when The date at which the message should be sent
     * @param[in] destination_mailbox The destination mailbox
     * @param[in] type The message type
     * @param[in] data The message data
     */
531
    void dsend_message(double when,
532
533
534
                      const std::string & destination_mailbox,
                      IPMessageType type,
                      void * data = nullptr) const;
Millian Poquet's avatar
Millian Poquet committed
535
536
537
538

private:
    std::map<std::string, std::function<void(JsonProtocolReader*, int, double, const rapidjson::Value&)>> _type_to_handler_map;
    std::vector<std::string> accepted_requests = {"consumed_energy"};
Millian Poquet's avatar
Millian Poquet committed
539
    BatsimContext * context = nullptr; // TODO
Millian Poquet's avatar
Millian Poquet committed
540
};