/
CloudComputeService.cpp
1464 lines (1233 loc) · 60.9 KB
/
CloudComputeService.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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
442
443
444
445
446
447
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2017-2018. The WRENCH Team.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
*/
#include <cfloat>
#include <numeric>
#include "CloudComputeServiceMessage.h"
#include "wrench/exceptions/WorkflowExecutionException.h"
#include "wrench/logging/TerminalOutput.h"
#include "wrench/services/helpers/ServiceTerminationDetector.h"
#include "wrench/services/helpers/ServiceTerminationDetectorMessage.h"
#include "wrench/services/compute/cloud/CloudComputeService.h"
#include "wrench/services/compute/bare_metal/BareMetalComputeService.h"
#include "wrench/simgrid_S4U_util/S4U_Mailbox.h"
WRENCH_LOG_NEW_DEFAULT_CATEGORY(cloud_service, "Log category for Cloud Service");
namespace wrench {
/** @brief VM ID sequence number */
unsigned long CloudComputeService::VM_ID = 1;
/**
* @brief Constructor
*
* @param hostname: the hostname on which to start the service
* @param execution_hosts: the list of the names of the hosts available for running virtual machines
* @param scratch_space_size: the size for the scratch storage pace of the cloud service
* @param property_list: a property list ({} means "use all defaults")
* @param messagepayload_list: a message payload list ({} means "use all defaults")
*
* @throw std::runtime_error
*/
CloudComputeService::CloudComputeService(const std::string &hostname,
std::vector<std::string> &execution_hosts,
double scratch_space_size,
std::map<std::string, std::string> property_list,
std::map<std::string, double> messagepayload_list) :
ComputeService(hostname, "cloud_service", "cloud_service",
scratch_space_size) {
if (execution_hosts.empty()) {
throw std::invalid_argument(
"CloudComputeService::CloudComputeService(): At least one execution host should be provided");
}
// Set default and specified properties
this->setProperties(this->default_property_values, std::move(property_list));
// Validate Properties
validateProperties();
// Set default and specified message payloads
this->setMessagePayloads(this->default_messagepayload_values, std::move(messagepayload_list));
// Initialize internal data structures
this->execution_hosts = execution_hosts;
for (auto const &h : this->execution_hosts) {
this->used_ram_per_execution_host[h] = 0;
this->used_cores_per_execution_host[h] = 0;
}
}
/**
* @brief Destructor
*/
CloudComputeService::~CloudComputeService() {
this->default_property_values.clear();
this->vm_list.clear();
}
/**
* @brief Get the list of execution hosts available to run VMs
*
* @return a list of hostnames
*
* @throw WorkflowExecutionException
*/
std::vector<std::string> CloudComputeService::getExecutionHosts() {
assertServiceIsUp();
// send a "get execution hosts" message to the daemon's mailbox_name
std::string answer_mailbox = S4U_Mailbox::generateUniqueMailboxName("get_execution_hosts");
std::shared_ptr<SimulationMessage> answer_message = sendRequest(
answer_mailbox,
new CloudComputeServiceGetExecutionHostsRequestMessage(
answer_mailbox,
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::GET_EXECUTION_HOSTS_REQUEST_MESSAGE_PAYLOAD)));
if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceGetExecutionHostsAnswerMessage>(answer_message)) {
return msg->execution_hosts;
} else {
throw std::runtime_error(
"CloudComputeService::sendRequest(): Received an unexpected [" + answer_message->getName() +
"] message!");
}
}
/**
* @brief Create a BareMetalComputeService VM (balances load on execution hosts)
*
* @param num_cores: the number of cores for the VM
* @param ram_memory: the VM's RAM memory capacity
* @param property_list: a property list for the BareMetalComputeService that will run on the VM ({} means "use all defaults")
* @param messagepayload_list: a message payload list for the BareMetalComputeService that will run on the VM ({} means "use all defaults")
*
* @return A VM name
*
* @throw WorkflowExecutionException
* @throw std::runtime_error
*/
std::string CloudComputeService::createVM(unsigned long num_cores,
double ram_memory,
std::map<std::string, std::string> property_list,
std::map<std::string, double> messagepayload_list) {
return this->createVM(num_cores, ram_memory, "", property_list, messagepayload_list);
}
/**
* @brief Create a BareMetalComputeService VM (balances load on execution hosts)
*
* @param num_cores: the number of cores for the VM
* @param ram_memory: the VM's RAM memory capacity
* @param desired_vm_name: the VM's desired name ("" means "pick a name for me")
* @param property_list: a property list for the BareMetalComputeService that will run on the VM ({} means "use all defaults")
* @param messagepayload_list: a message payload list for the BareMetalComputeService that will run on the VM ({} means "use all defaults")
*
* @return A VM name
*
* @throw WorkflowExecutionException
* @throw std::runtime_error
*/
std::string CloudComputeService::createVM(unsigned long num_cores,
double ram_memory,
std::string desired_vm_name,
std::map<std::string, std::string> property_list,
std::map<std::string, double> messagepayload_list) {
if (num_cores == ComputeService::ALL_CORES) {
throw std::invalid_argument(
"CloudComputeService::createVM(): the VM's number of cores cannot be ComputeService::ALL_CORES");
}
if (ram_memory == ComputeService::ALL_RAM) {
throw std::invalid_argument(
"CloudComputeService::createVM(): the VM's memory requirement cannot be ComputeService::ALL_RAM");
}
assertServiceIsUp();
// send a "create vm" message to the daemon's mailbox_name
std::string answer_mailbox = S4U_Mailbox::generateUniqueMailboxName("create_vm");
std::shared_ptr<SimulationMessage> answer_message = sendRequest(
answer_mailbox,
new CloudComputeServiceCreateVMRequestMessage(
answer_mailbox,
num_cores, ram_memory, desired_vm_name, property_list, messagepayload_list,
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::CREATE_VM_REQUEST_MESSAGE_PAYLOAD)));
if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceCreateVMAnswerMessage>(answer_message)) {
if (not msg->success) {
throw WorkflowExecutionException(msg->failure_cause);
} else {
return msg->vm_name;
}
} else {
throw std::runtime_error(
"CloudComputeService::createVM(): Unexpected [" + answer_message->getName() + "] message");
}
}
/**
* @brief Shutdown an active VM
*
* @param vm_name: the name of the VM
*
* @throw WorkflowExecutionException
* @throw std::invalid_argument
*/
void CloudComputeService::shutdownVM(const std::string &vm_name) {
if (this->vm_list.find(vm_name) == this->vm_list.end()) {
throw std::invalid_argument("CloudComputeService::shutdownVM(): Unknown VM name '" + vm_name + "'");
}
assertServiceIsUp();
// send a "shutdown vm" message to the daemon's mailbox_name
std::string answer_mailbox = S4U_Mailbox::generateUniqueMailboxName("shutdown_vm");
std::shared_ptr<SimulationMessage> answer_message = sendRequest(
answer_mailbox,
new CloudComputeServiceShutdownVMRequestMessage(
answer_mailbox, vm_name,
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::SHUTDOWN_VM_REQUEST_MESSAGE_PAYLOAD)));
if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceShutdownVMAnswerMessage>(answer_message)) {
if (not msg->success) {
throw WorkflowExecutionException(msg->failure_cause);
}
} else {
throw std::runtime_error(
"CloudComputeService::shutdownVM(): Unexpected [" + answer_message->getName() + "] message");
}
return;
}
/**
* @brief Start a VM
*
* @param vm_name: the name of the VM
*
* @return A BareMetalComputeService that runs on the VM
*
* @throw WorkflowExecutionException
* @throw std::invalid_argument
*/
std::shared_ptr<BareMetalComputeService> CloudComputeService::startVM(const std::string &vm_name) {
if (this->vm_list.find(vm_name) == this->vm_list.end()) {
throw std::invalid_argument("CloudComputeService::startVM(): Unknown VM name '" + vm_name + "'");
}
assertServiceIsUp();
std::string answer_mailbox = S4U_Mailbox::generateUniqueMailboxName("start_vm");
std::shared_ptr<SimulationMessage> answer_message = sendRequest(
answer_mailbox,
new CloudComputeServiceStartVMRequestMessage(
answer_mailbox, vm_name, "",
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::START_VM_REQUEST_MESSAGE_PAYLOAD)));
if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceStartVMAnswerMessage>(answer_message)) {
if (not msg->success) {
throw WorkflowExecutionException(msg->failure_cause);
}
return msg->cs;
} else {
throw std::runtime_error(
"CloudComputeService::startVM(): Unexpected [" + answer_message->getName() + "] message");
}
}
/**
* @brief Suspend a running VM
*
* @param vm_name: the name of the VM
*
* @throw WorkflowExecutionException
* @throw std::invalid_argument
*/
void CloudComputeService::suspendVM(const std::string &vm_name) {
if (this->vm_list.find(vm_name) == this->vm_list.end()) {
throw std::invalid_argument("CloudComputeService::suspendVM(): Unknown VM name '" + vm_name + "'");
}
assertServiceIsUp();
// send a "shutdown vm" message to the daemon's mailbox_name
std::string answer_mailbox = S4U_Mailbox::generateUniqueMailboxName("suspend_vm");
std::shared_ptr<SimulationMessage> answer_message = sendRequest(
answer_mailbox,
new CloudComputeServiceSuspendVMRequestMessage(
answer_mailbox, vm_name,
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::SUSPEND_VM_REQUEST_MESSAGE_PAYLOAD)));
if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceSuspendVMAnswerMessage>(answer_message)) {
if (not msg->success) {
throw WorkflowExecutionException(msg->failure_cause);
}
} else {
throw std::runtime_error(
"CloudComputeService::suspendVM(): Unexpected [" + answer_message->getName() + "] message");
}
return;
}
/**
* @brief Resume a suspended VM
*
* @param vm_name: the name of the VM
*
* @throw WorkflowExecutionException
* @throw std::invalid_argument
*/
void CloudComputeService::resumeVM(const std::string &vm_name) {
if (this->vm_list.find(vm_name) == this->vm_list.end()) {
throw std::invalid_argument("CloudComputeService::resumeVM(): Unknown VM name '" + vm_name + "'");
}
assertServiceIsUp();
// send a "shutdown vm" message to the daemon's mailbox_name
std::string answer_mailbox = S4U_Mailbox::generateUniqueMailboxName("resume_vm");
std::shared_ptr<SimulationMessage> answer_message = sendRequest(
answer_mailbox,
new CloudComputeServiceResumeVMRequestMessage(
answer_mailbox, vm_name,
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::RESUME_VM_REQUEST_MESSAGE_PAYLOAD)));
if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceResumeVMAnswerMessage>(answer_message)) {
if (not msg->success) {
throw WorkflowExecutionException(msg->failure_cause);
}
// Got the expected reply
} else {
throw std::runtime_error(
"CloudComputeService::resumeVM(): Unexpected [" + answer_message->getName() + "] message");
}
return;
}
/**
* @brief Destroy a VM
*
* @param vm_name: the name of the VM
*
* @throw WorkflowExecutionException
* @throw std::invalid_argument
*/
void CloudComputeService::destroyVM(const std::string &vm_name) {
if (this->vm_list.find(vm_name) == this->vm_list.end()) {
throw std::invalid_argument("CloudComputeService::resumeVM(): Unknown VM name '" + vm_name + "'");
}
assertServiceIsUp();
// send a "shutdown vm" message to the daemon's mailbox_name
std::string answer_mailbox = S4U_Mailbox::generateUniqueMailboxName("destroy_vm");
std::shared_ptr<SimulationMessage> answer_message = sendRequest(
answer_mailbox,
new CloudComputeServiceDestroyVMRequestMessage(
answer_mailbox, vm_name,
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::DESTROY_VM_REQUEST_MESSAGE_PAYLOAD)));
if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceDestroyVMAnswerMessage>(answer_message)) {
if (not msg->success) {
throw WorkflowExecutionException(msg->failure_cause);
}
} else {
throw std::runtime_error(
"CloudComputeService::destroyVM(): Unexpected [" + answer_message->getName() + "] message");
}
return;
}
/**
* @brief Submit a standard job to the cloud service
*
* @param job: a standard job
* @param service_specific_args: batch-specific arguments
* - optional: "-vm": name of vm on which to start the job
* (if not provided, the service will pick the vm)
*
* @throw WorkflowExecutionException
* @throw std::invalid_argument
* @throw std::runtime_error
*/
void CloudComputeService::submitStandardJob(StandardJob *job,
std::map<std::string, std::string> &service_specific_args) {
assertServiceIsUp();
for (auto const &arg : service_specific_args) {
if (arg.first != "-vm") {
throw std::invalid_argument(
"CloudComputeService::submitStandardJob(): Invalid service-specific argument key '" +
arg.first + "'");
}
if (this->vm_list.find(arg.second) == this->vm_list.end()) {
throw std::invalid_argument(
"CloudComputeService::submitStandardJob(): In service-specific argument value: Unknown VM name '" +
arg.second + "'");
}
}
std::string answer_mailbox = S4U_Mailbox::generateUniqueMailboxName("submit_standard_job");
std::shared_ptr<SimulationMessage> answer_message = sendRequest(
answer_mailbox,
new ComputeServiceSubmitStandardJobRequestMessage(
answer_mailbox, job, service_specific_args,
this->getMessagePayloadValue(
ComputeServiceMessagePayload::SUBMIT_STANDARD_JOB_REQUEST_MESSAGE_PAYLOAD)));
if (auto msg = std::dynamic_pointer_cast<ComputeServiceSubmitStandardJobAnswerMessage>(answer_message)) {
// If no success, throw an exception
if (not msg->success) {
throw WorkflowExecutionException(msg->failure_cause);
}
} else {
throw std::runtime_error(
"ComputeService::submitStandardJob(): Received an unexpected [" + msg->getName() + "] message!");
}
}
/**
* @brief Asynchronously submit a pilot job to the cloud service
*
* @param job: a pilot job
* @param service_specific_args: service specific arguments
* - optional: "-vm": name of vm on which to start the job
* (if not provided, the service will pick the vm)
*
* @throw WorkflowExecutionException
* @throw std::runtime_error
*/
void CloudComputeService::submitPilotJob(PilotJob *job, std::map<std::string, std::string> &service_specific_args) {
assertServiceIsUp();
for (auto const &arg : service_specific_args) {
if (arg.first != "-vm") {
throw std::invalid_argument(
"CloudComputeService::submitPilotJob(): Invalid service-specific argument key '" + arg.first +
"'");
}
if (this->vm_list.find(arg.second) == this->vm_list.end()) {
throw std::invalid_argument(
"CloudComputeService::submitPilotJob(): In service-specific argument value: Unknown VM name '" +
arg.second + "'");
}
}
std::string answer_mailbox = S4U_Mailbox::generateUniqueMailboxName("submit_pilot_job");
std::shared_ptr<SimulationMessage> answer_message = sendRequest(
answer_mailbox,
new ComputeServiceSubmitPilotJobRequestMessage(
answer_mailbox, job, service_specific_args, this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::SUBMIT_PILOT_JOB_REQUEST_MESSAGE_PAYLOAD)));
if (auto msg = std::dynamic_pointer_cast<ComputeServiceSubmitPilotJobAnswerMessage>(answer_message)) {
// If no success, throw an exception
if (not msg->success) {
throw WorkflowExecutionException(msg->failure_cause);
} else {
return;
}
} else {
throw std::runtime_error(
"CloudComputeService::submitPilotJob(): Received an unexpected [" + msg->getName() + "] message!");
}
}
/**
* @brief Terminate a standard job to the compute service (virtual)
* @param job: the standard job
*
* @throw std::runtime_error
*/
void CloudComputeService::terminateStandardJob(StandardJob *job) {
throw std::runtime_error("CloudComputeService::terminateStandardJob(): Not implemented!");
}
/**
* @brief non-implemented
* @param job: a pilot job to (supposedly) terminate
*/
void CloudComputeService::terminatePilotJob(PilotJob *job) {
throw std::runtime_error(
"CloudComputeService::terminatePilotJob(): not implemented because CloudComputeService never supports pilot jobs");
}
/**
* @brief Method to check whether a VM is currently running
* @param vm_name: the name of the VM
* @return true or false
* @throw std::invalid_argument
*/
bool CloudComputeService::isVMRunning(const std::string &vm_name) {
auto vm_pair_it = this->vm_list.find(vm_name);
if (vm_pair_it == this->vm_list.end()) {
throw std::invalid_argument("CloudComputeService::isVMRunning(): Unknown VM name '" + vm_name + "'");
}
assertServiceIsUp();
return (vm_pair_it->second.first->getState() == S4U_VirtualMachine::State::RUNNING);
}
/**
* @brief Method to check whether a VM is currently down
* @param vm_name: the name of the VM
* @return true or false
* @throw std::invalid_argument
*/
bool CloudComputeService::isVMDown(const std::string &vm_name) {
auto vm_pair_it = this->vm_list.find(vm_name);
if (vm_pair_it == this->vm_list.end()) {
throw std::invalid_argument("CloudComputeService::isVMDown(): Unknown VM name '" + vm_name + "'");
}
assertServiceIsUp();
return (vm_pair_it->second.first->getState() == S4U_VirtualMachine::State::DOWN);
}
/**
* @brief Method to check whether a VM is currently running
* @param vm_name: the name of the VM
* @return true or false
* @throw std::invalid_argument
*/
bool CloudComputeService::isVMSuspended(const std::string &vm_name) {
auto vm_pair_it = this->vm_list.find(vm_name);
if (vm_pair_it == this->vm_list.end()) {
throw std::invalid_argument("CloudComputeService::isVMSuspended(): Unknown VM name '" + vm_name + "'");
}
assertServiceIsUp();
return (vm_pair_it->second.first->getState() == S4U_VirtualMachine::State::SUSPENDED);
}
/**
* @brief Main method of the daemon
*
* @return 0 on termination
*/
int CloudComputeService::main() {
TerminalOutput::setThisProcessLoggingColor(TerminalOutput::COLOR_RED);
WRENCH_INFO("Cloud Service starting on host %s listening on mailbox_name %s",
this->hostname.c_str(),
this->mailbox_name.c_str());
/** Main loop **/
while (this->processNextMessage()) {
// no specific action
}
WRENCH_INFO("Cloud Service on host %s terminating cleanly!", S4U_Simulation::getHostName().c_str());
return 0;
}
/**
* @brief Send a message request
*
* @param answer_mailbox: the mailbox to which the answer message should be sent
* @param message: message to be sent
* @return a simulation message
*
* @throw std::runtime_error
*/
std::shared_ptr<SimulationMessage>
CloudComputeService::sendRequest(std::string &answer_mailbox, ComputeServiceMessage *message) {
serviceSanityCheck();
try {
S4U_Mailbox::dputMessage(this->mailbox_name, message);
} catch (std::shared_ptr<NetworkError> &cause) {
throw WorkflowExecutionException(cause);
}
// Wait for a reply
std::shared_ptr<SimulationMessage> answer_message = nullptr;
try {
answer_message = S4U_Mailbox::getMessage(answer_mailbox, this->network_timeout);
} catch (std::shared_ptr<NetworkError> &cause) {
throw WorkflowExecutionException(cause);
}
return answer_message;
}
/**
* @brief Wait for and react to any incoming message
*
* @return false if the daemon should terminate, true otherwise
*
* @throw std::runtime_error
*/
bool CloudComputeService::processNextMessage() {
S4U_Simulation::computeZeroFlop();
// Wait for a message
std::shared_ptr<SimulationMessage> message;
try {
message = S4U_Mailbox::getMessage(this->mailbox_name);
} catch (std::shared_ptr<NetworkError> &cause) {
return true;
}
if (message == nullptr) {
WRENCH_INFO("Got a NULL message... Likely this means we're all done. Aborting");
return false;
}
WRENCH_DEBUG("Got a [%s] message", message->getName().c_str());
if (auto msg = std::dynamic_pointer_cast<ServiceStopDaemonMessage>(message)) {
this->stopAllVMs();
this->vm_list.clear();
// This is Synchronous
try {
S4U_Mailbox::putMessage(msg->ack_mailbox,
new ServiceDaemonStoppedMessage(this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::DAEMON_STOPPED_MESSAGE_PAYLOAD)));
} catch (std::shared_ptr<NetworkError> &cause) {
return false;
}
return false;
} else if (auto msg = std::dynamic_pointer_cast<ComputeServiceResourceInformationRequestMessage>(message)) {
processGetResourceInformation(msg->answer_mailbox);
return true;
} else if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceGetExecutionHostsRequestMessage>(message)) {
processGetExecutionHosts(msg->answer_mailbox);
return true;
} else if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceCreateVMRequestMessage>(message)) {
processCreateVM(msg->answer_mailbox, msg->num_cores, msg->ram_memory, msg->desired_vm_name, msg->property_list,
msg->messagepayload_list);
return true;
} else if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceShutdownVMRequestMessage>(message)) {
processShutdownVM(msg->answer_mailbox, msg->vm_name);
return true;
} else if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceStartVMRequestMessage>(message)) {
processStartVM(msg->answer_mailbox, msg->vm_name, msg->pm_name);
return true;
} else if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceSuspendVMRequestMessage>(message)) {
processSuspendVM(msg->answer_mailbox, msg->vm_name);
return true;
} else if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceResumeVMRequestMessage>(message)) {
processResumeVM(msg->answer_mailbox, msg->vm_name);
return true;
} else if (auto msg = std::dynamic_pointer_cast<CloudComputeServiceDestroyVMRequestMessage>(message)) {
processDestroyVM(msg->answer_mailbox, msg->vm_name);
return true;
} else if (auto msg = std::dynamic_pointer_cast<ComputeServiceSubmitStandardJobRequestMessage>(message)) {
processSubmitStandardJob(msg->answer_mailbox, msg->job, msg->service_specific_args);
return true;
} else if (auto msg = std::dynamic_pointer_cast<ComputeServiceSubmitPilotJobRequestMessage>(message)) {
processSubmitPilotJob(msg->answer_mailbox, msg->job, msg->service_specific_args);
return true;
} else if (auto msg = std::dynamic_pointer_cast<ServiceHasTerminatedMessage>(message)) {
if (auto bmcs = std::dynamic_pointer_cast<BareMetalComputeService>(msg->service)) {
processBareMetalComputeServiceTermination(bmcs, msg->exit_code);
} else {
throw std::runtime_error(
"CloudComputeService::processNextMessage(): Received a service termination message for a non-BareMetalComputeService!");
}
return true;
} else {
throw std::runtime_error("Unexpected [" + message->getName() + "] message");
}
}
/**
* @brief Process a execution host list request
*
* @param answer_mailbox: the mailbox to which the answer message should be sent
*/
void CloudComputeService::processGetExecutionHosts(const std::string &answer_mailbox) {
try {
S4U_Mailbox::dputMessage(
answer_mailbox,
new CloudComputeServiceGetExecutionHostsAnswerMessage(
this->execution_hosts,
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::GET_EXECUTION_HOSTS_ANSWER_MESSAGE_PAYLOAD)));
} catch (std::shared_ptr<NetworkError> &cause) {
return;
}
}
/**
* @brief Create a BareMetalComputeService VM on a physical machine
*
* @param answer_mailbox: the mailbox to which the answer message should be sent
* @param requested_num_cores: the number of cores the service can use
* @param requested_ram: the VM's RAM memory capacity
* @param desired_vm_name: the desired VM name ("" means "pick a name for me")
* @param property_list: a property list for the BareMetalComputeService that will run on the VM ({} means "use all defaults")
* @param messagepayload_list: a message payload list for the BareMetalComputeService that will run on the VM ({} means "use all defaults")
*
* @throw std::runtime_error
*/
void CloudComputeService::processCreateVM(const std::string &answer_mailbox,
unsigned long requested_num_cores,
double requested_ram,
std::string desired_vm_name,
std::map<std::string, std::string> property_list,
std::map<std::string, double> messagepayload_list) {
WRENCH_INFO("Asked to create a VM with %s cores and %s RAM",
(requested_num_cores == ComputeService::ALL_CORES ? "max" : std::to_string(
requested_num_cores)).c_str(),
(requested_ram == ComputeService::ALL_RAM ? "max" : std::to_string(requested_ram)).c_str());
CloudComputeServiceCreateVMAnswerMessage *msg_to_send_back;
// Check that there is at least one physical host that could support the VM
bool found_a_host = false;
for (auto const &host : this->execution_hosts) {
auto total_num_cores = Simulation::getHostNumCores(host);
auto total_ram = Simulation::getHostMemoryCapacity(host);
if ((requested_num_cores <= total_num_cores) and (requested_ram <= total_ram)) {
found_a_host = true;
break;
}
}
if (not found_a_host) {
WRENCH_INFO("Not host on this service can accommodate this VM");
std::string empty = std::string();
msg_to_send_back =
new CloudComputeServiceCreateVMAnswerMessage(
false,
empty,
std::shared_ptr<FailureCause>(
new NotEnoughResources(nullptr, this->getSharedPtr<CloudComputeService>())),
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::CREATE_VM_ANSWER_MESSAGE_PAYLOAD));
} else {
// Pick a VM name (and being paranoid about mistakenly picking an actual hostname!)
std::string vm_name = "";
if (desired_vm_name.empty()) {
do {
vm_name = this->getName() + "_vm" + std::to_string(CloudComputeService::VM_ID++);
} while (S4U_Simulation::hostExists(vm_name));
} else {
if (this->vm_list.find(desired_vm_name) == this->vm_list.end()) {
vm_name = desired_vm_name;
}
}
if (vm_name.empty()) {
std::string empty = std::string();
std::string error_msg = "Invalid requested VM name";
msg_to_send_back = new CloudComputeServiceCreateVMAnswerMessage(
false,
empty,
std::shared_ptr<FailureCause>(
new NotAllowed(this->getSharedPtr<CloudComputeService>(), error_msg)),
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::CREATE_VM_ANSWER_MESSAGE_PAYLOAD));
} else {
// Create the VM
auto vm = std::shared_ptr<S4U_VirtualMachine>(
new S4U_VirtualMachine(vm_name, requested_num_cores, requested_ram, property_list,
messagepayload_list));
// Add the VM to the list of VMs, with (for now) a nullptr compute service
this->vm_list[vm_name] = std::make_pair(vm, nullptr);
msg_to_send_back = new CloudComputeServiceCreateVMAnswerMessage(
true,
vm_name,
nullptr,
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::CREATE_VM_ANSWER_MESSAGE_PAYLOAD));
}
}
// Send reply
try {
S4U_Mailbox::dputMessage(answer_mailbox, msg_to_send_back);
} catch (std::shared_ptr<NetworkError> &cause) {
// Ignore
}
return;
}
/**
* @brief: Process a VM shutdown request
*
* @param answer_mailbox: the mailbox to which the answer message should be sent
* @param vm_name: the name of the VM
*/
void CloudComputeService::processShutdownVM(const std::string &answer_mailbox, const std::string &vm_name) {
WRENCH_INFO("Asked to shutdown VM %s", vm_name.c_str());
CloudComputeServiceShutdownVMAnswerMessage *msg_to_send_back;
auto vm_pair = *(this->vm_list.find(vm_name));
auto vm = vm_pair.second.first;
auto cs = vm_pair.second.second;
if (vm->getState() != S4U_VirtualMachine::State::RUNNING) {
std::string error_message("Cannot shutdown a VM that is not running");
msg_to_send_back = new CloudComputeServiceShutdownVMAnswerMessage(
false,
std::shared_ptr<FailureCause>(
new NotAllowed(this->getSharedPtr<CloudComputeService>(), error_message)),
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::SHUTDOWN_VM_ANSWER_MESSAGE_PAYLOAD));
} else {
std::string pm = vm->getPhysicalHostname();
// Stop the Compute Service
cs->stop();
// We do not shut down the VM. This will be done when the CloudComputeService is notified
// of the BareMetalComputeService completion.
vm->shutdown();
// Update internal data structures
this->used_ram_per_execution_host[pm] -= vm->getMemory();
this->used_cores_per_execution_host[pm] -= vm->getNumCores();
msg_to_send_back = new CloudComputeServiceShutdownVMAnswerMessage(
true,
nullptr,
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::SHUTDOWN_VM_ANSWER_MESSAGE_PAYLOAD));
}
try {
S4U_Mailbox::dputMessage(answer_mailbox, msg_to_send_back);
} catch (std::shared_ptr<NetworkError> &cause) {
// ignore
}
return;
}
/**
* @brief A helper method that finds a host with a given number of available cores and a given amount of
* available RAM using one of the several resource allocation algorithms.
* @param desired_num_cores: desired number of cores
* @param desired_ram: desired amount of RAM
* @oaram desired_host: name of a desired host ("" if none)
* @return
*/
std::string
CloudComputeService::findHost(unsigned long desired_num_cores, double desired_ram, std::string desired_host) {
// Find a physical host to start the VM
std::vector<std::string> possible_hosts;
for (auto const &host : this->execution_hosts) {
if ((not desired_host.empty()) and (host != desired_host)) {
continue;
}
// Check that host is up
if (not Simulation::isHostOn(host)) {
continue;
}
// Check that host has a non-zero compute speed
if (Simulation::getHostFlopRate(host) <= 0) {
continue;
}
// Check for RAM
auto total_ram = Simulation::getHostMemoryCapacity(host);
auto available_ram = total_ram - this->used_ram_per_execution_host[host];
if (desired_ram > available_ram) {
continue;
}
// Check for cores
auto total_num_cores = Simulation::getHostNumCores(host);
auto num_available_cores = total_num_cores - this->used_cores_per_execution_host[host];
if (desired_num_cores > num_available_cores) {
continue;
}
possible_hosts.push_back(host);
break;
}
// Did we find a viable host?
if (possible_hosts.empty()) {
return "";
}
std::string vm_resource_allocation_algorithm = this->getPropertyValueAsString(
CloudComputeServiceProperty::VM_RESOURCE_ALLOCATION_ALGORITHM);
if (vm_resource_allocation_algorithm == "first-fit") {
//don't sort the possibvle hosts
} else if (vm_resource_allocation_algorithm == "best-fit-ram-first") {
// Sort the possible hosts to implement best fit (using RAM first)
std::sort(possible_hosts.begin(), possible_hosts.end(),
[](std::string const &a, std::string const &b) {
unsigned long a_num_cores = Simulation::getHostNumCores(a);
double a_ram = Simulation::getHostMemoryCapacity(a);
unsigned long b_num_cores = Simulation::getHostNumCores(b);
double b_ram = Simulation::getHostMemoryCapacity(b);
if (a_ram != b_ram) {
return a_ram < b_ram;
} else if (a_num_cores < b_num_cores) {
return a_num_cores < b_num_cores;
} else {
return a < b; // string order
}
});
} else if (vm_resource_allocation_algorithm == "best-fit-cores-first") {
// Sort the possible hosts to implement best fit (using cores first)
std::sort(possible_hosts.begin(), possible_hosts.end(),
[](std::string const &a, std::string const &b) {
unsigned long a_num_cores = Simulation::getHostNumCores(a);
double a_ram = Simulation::getHostMemoryCapacity(a);
unsigned long b_num_cores = Simulation::getHostNumCores(b);
double b_ram = Simulation::getHostMemoryCapacity(b);
if (a_num_cores != b_num_cores) {
return a_num_cores < b_num_cores;
} else if (a_ram < b_ram) {
return a_ram < b_ram;
} else {
return a < b; // string order
}
});
}
auto picked_host = *(possible_hosts.begin());
return picked_host;
}
/**
* @brief: Process a VM start request by startibnng a VM on a host (using best fit for RAM first, and then for cores)
*
* @param answer_mailbox: the mailbox to which the answer message should be sent
* @param vm_name: the name of the VM
* @param pm_name: the name of the physical host on which to start the VM (empty string if up to the service to pick a host)
*/
void CloudComputeService::processStartVM(const std::string &answer_mailbox, const std::string &vm_name,
const std::string &pm_name) {
auto vm_pair = this->vm_list[vm_name];
auto vm = vm_pair.first;
CloudComputeServiceStartVMAnswerMessage *msg_to_send_back;
if (vm->getState() != S4U_VirtualMachine::State::DOWN) {
std::string error_message("Cannot start a VM that is not down");
msg_to_send_back = new CloudComputeServiceStartVMAnswerMessage(
false,
nullptr,
std::shared_ptr<FailureCause>(
new NotAllowed(this->getSharedPtr<CloudComputeService>(), error_message)),
this->getMessagePayloadValue(
CloudComputeServiceMessagePayload::START_VM_ANSWER_MESSAGE_PAYLOAD));
} else {
std::string picked_host = this->findHost(vm->getNumCores(), vm->getMemory(), pm_name);
// Did we find a viable host?