forked from OpenNebula/one
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Scheduler.cc
1420 lines (1081 loc) · 41.8 KB
/
Scheduler.cc
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 2002-2015, OpenNebula Project, OpenNebula Systems */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
#include <stdexcept>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <pwd.h>
#include <pthread.h>
#include <cmath>
#include "Scheduler.h"
#include "SchedulerTemplate.h"
#include "RankPolicy.h"
#include "NebulaLog.h"
#include "PoolObjectAuth.h"
#include "NebulaUtil.h"
using namespace std;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
static double profile(bool start, const string& message="")
{
static struct timespec estart, eend;
double t;
if (start)
{
clock_gettime(CLOCK_MONOTONIC, &estart);
if (!message.empty())
{
NebulaLog::log("SCHED", Log::DDEBUG, message);
}
return 0;
}
clock_gettime(CLOCK_MONOTONIC, &eend);
t = (eend.tv_sec + (eend.tv_nsec * pow(10,-9))) -
(estart.tv_sec+(estart.tv_nsec*pow(10,-9)));
if (!message.empty())
{
ostringstream oss;
oss << message << " Total time: " << one_util::float_to_str(t) << "s";
NebulaLog::log("SCHED", Log::DDEBUG, oss);
}
return t;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
extern "C" void * scheduler_action_loop(void *arg)
{
Scheduler * sched;
if ( arg == 0 )
{
return 0;
}
sched = static_cast<Scheduler *>(arg);
NebulaLog::log("SCHED",Log::INFO,"Scheduler loop started.");
sched->am.loop(sched->timer,0);
NebulaLog::log("SCHED",Log::INFO,"Scheduler loop stopped.");
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void Scheduler::start()
{
int rc;
ifstream file;
ostringstream oss;
string etc_path;
unsigned int live_rescheds;
pthread_attr_t pattr;
// -----------------------------------------------------------
// Configuration File
// -----------------------------------------------------------
string log_file;
const char * nl = getenv("ONE_LOCATION");
if (nl == 0) //OpenNebula installed under root directory
{
log_file = "/var/log/one/sched.log";
etc_path = "/etc/one/";
}
else
{
oss << nl << "/var/sched.log";
log_file = oss.str();
oss.str("");
oss << nl << "/etc/";
etc_path = oss.str();
}
SchedulerTemplate conf(etc_path);
if ( conf.load_configuration() != 0 )
{
throw runtime_error("Error reading configuration file.");
}
conf.get("ONE_XMLRPC", one_xmlrpc);
conf.get("SCHED_INTERVAL", timer);
conf.get("MAX_VM", machines_limit);
conf.get("MAX_DISPATCH", dispatch_limit);
conf.get("MAX_HOST", host_dispatch_limit);
conf.get("LIVE_RESCHEDS", live_rescheds);
// -----------------------------------------------------------
// Log system & Configuration File
// -----------------------------------------------------------
try
{
NebulaLog::LogType log_system = NebulaLog::UNDEFINED;
Log::MessageType clevel = Log::ERROR;;
const VectorAttribute * log = conf.get("LOG");
if ( log != 0 )
{
string value;
int ilevel;
value = log->vector_value("SYSTEM");
log_system = NebulaLog::str_to_type(value);
value = log->vector_value("DEBUG_LEVEL");
ilevel = atoi(value.c_str());
if (Log::ERROR <= ilevel && ilevel <= Log::DDDEBUG)
{
clevel = static_cast<Log::MessageType>(ilevel);
}
}
// Start the log system
if ( log_system != NebulaLog::UNDEFINED )
{
NebulaLog::init_log_system(log_system,
clevel,
log_file.c_str(),
ios_base::trunc,
"mm_sched");
}
else
{
throw runtime_error("Unknown LOG_SYSTEM.");
}
NebulaLog::log("SCHED", Log::INFO, "Init Scheduler Log system");
}
catch(runtime_error &)
{
throw;
}
oss.str("");
oss << "Starting Scheduler Daemon" << endl;
oss << "----------------------------------------\n";
oss << " Scheduler Configuration File \n";
oss << "----------------------------------------\n";
oss << conf;
oss << "----------------------------------------";
NebulaLog::log("SCHED", Log::INFO, oss);
// -----------------------------------------------------------
// XML-RPC Client
// -----------------------------------------------------------
try
{
long long message_size;
unsigned int timeout;
conf.get("MESSAGE_SIZE", message_size);
conf.get("TIMEOUT", timeout);
Client::initialize("", one_xmlrpc, message_size, timeout);
oss.str("");
oss << "XML-RPC client using " << (Client::client())->get_message_size()
<< " bytes for response buffer.\n";
NebulaLog::log("SCHED", Log::INFO, oss);
}
catch(runtime_error &)
{
throw;
}
xmlInitParser();
// -------------------------------------------------------------------------
// Get oned configuration, and init zone_id
// -------------------------------------------------------------------------
while (1)
{
try
{
xmlrpc_c::value result;
vector<xmlrpc_c::value> values;
Client * client = Client::client();
client->call("one.system.config", "", &result);
values = xmlrpc_c::value_array(result).vectorValueValue();
bool success = xmlrpc_c::value_boolean(values[0]);
string message = xmlrpc_c::value_string(values[1]);
if (!success ||(oned_conf.from_xml(message) != 0))
{
ostringstream oss;
oss << "Cannot contact oned, will retry... Error: " << message;
NebulaLog::log("SCHED", Log::ERROR, oss);
}
break;
}
catch (exception const& e)
{
ostringstream oss;
oss << "Cannot contact oned, will retry... Error: " << e.what();
NebulaLog::log("SCHED", Log::ERROR, oss);
}
sleep(2);
}
NebulaLog::log("SCHED", Log::INFO, "oned successfully contacted.");
zone_id = 0;
const VectorAttribute * fed = oned_conf.get("FEDERATION");
if (fed != 0)
{
if (fed->vector_value("ZONE_ID", zone_id) != 0)
{
zone_id = 0;
}
}
oss.str("");
oss << "Configuring scheduler for Zone ID: " << zone_id;
NebulaLog::log("SCHED", Log::INFO, oss);
// -------------------------------------------------------------------------
// Pools
// -------------------------------------------------------------------------
hpool = new HostPoolXML(Client::client());
upool = new UserPoolXML(Client::client());
clpool = new ClusterPoolXML(Client::client());
vmpool = new VirtualMachinePoolXML(Client::client(), machines_limit,
live_rescheds==1);
vmapool = new VirtualMachineActionsPoolXML(Client::client(), machines_limit);
dspool = new SystemDatastorePoolXML(Client::client());
img_dspool = new ImageDatastorePoolXML(Client::client());
acls = new AclXML(Client::client(), zone_id);
// -----------------------------------------------------------
// Load scheduler policies
// -----------------------------------------------------------
register_policies(conf);
// -----------------------------------------------------------
// Close stds, we no longer need them
// -----------------------------------------------------------
int fd;
fd = open("/dev/null", O_RDWR);
dup2(fd,0);
dup2(fd,1);
dup2(fd,2);
close(fd);
fcntl(0,F_SETFD,0); // Keep them open across exec funcs
fcntl(1,F_SETFD,0);
fcntl(2,F_SETFD,0);
// -----------------------------------------------------------
// Block all signals before creating any thread
// -----------------------------------------------------------
sigset_t mask;
int signal;
sigfillset(&mask);
pthread_sigmask(SIG_BLOCK, &mask, NULL);
// -----------------------------------------------------------
// Create the scheduler loop
// -----------------------------------------------------------
NebulaLog::log("SCHED",Log::INFO,"Starting scheduler loop...");
pthread_attr_init (&pattr);
pthread_attr_setdetachstate (&pattr, PTHREAD_CREATE_JOINABLE);
rc = pthread_create(&sched_thread,&pattr,scheduler_action_loop,(void *) this);
if ( rc != 0 )
{
NebulaLog::log("SCHED",Log::ERROR,
"Could not start scheduler loop, exiting");
return;
}
// -----------------------------------------------------------
// Wait for a SIGTERM or SIGINT signal
// -----------------------------------------------------------
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigwait(&mask, &signal);
am.trigger(ActionListener::ACTION_FINALIZE,0); //Cancel sched loop
pthread_join(sched_thread,0);
xmlCleanupParser();
NebulaLog::finalize_log_system();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Scheduler::set_up_pools()
{
int rc;
ostringstream oss;
map<int,int>::const_iterator it;
map<int, int> shares;
//--------------------------------------------------------------------------
//Cleans the cache and get the pending VMs
//--------------------------------------------------------------------------
rc = vmpool->set_up();
if ( rc != 0 )
{
return rc;
}
//--------------------------------------------------------------------------
//Cleans the cache and get the datastores
//--------------------------------------------------------------------------
rc = dspool->set_up();
if ( rc != 0 )
{
return rc;
}
rc = img_dspool->set_up();
if ( rc != 0 )
{
return rc;
}
//--------------------------------------------------------------------------
//Cleans the cache and get the hosts ids
//--------------------------------------------------------------------------
rc = upool->set_up();
if ( rc != 0 )
{
return rc;
}
//--------------------------------------------------------------------------
//Cleans the cache and get the hosts ids
//--------------------------------------------------------------------------
rc = hpool->set_up();
if ( rc != 0 )
{
return rc;
}
//--------------------------------------------------------------------------
//Cleans the cache and get the cluster information
//--------------------------------------------------------------------------
rc = clpool->set_up();
if ( rc != 0 )
{
return rc;
}
//--------------------------------------------------------------------------
//Add to each host the corresponding cluster template
//--------------------------------------------------------------------------
hpool->merge_clusters(clpool);
//--------------------------------------------------------------------------
//Cleans the cache and get the ACLs
//--------------------------------------------------------------------------
rc = acls->set_up();
if ( rc != 0 )
{
return rc;
}
return 0;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
* Match hosts for this VM that:
* 1. Fulfills ACL
* 2. Meets user/policy requirements
* 3. Have enough capacity to host the VM
*
* @param acl pool
* @param users the user pool
* @param vm the virtual machine
* @param vm_memory vm requirement
* @param vm_cpu vm requirement
* @param vm_pci vm requirement
* @param host to evaluate vm assgiment
* @param n_auth number of hosts authorized for the user, incremented if needed
* @param n_error number of requirement errors, incremented if needed
* @param n_fits number of hosts with capacity that fits the VM requirements
* @param n_matched number of hosts that fullfil VM sched_requirements
* @param error, string describing why the host is not valid
* @return true for a positive match
*/
static bool match_host(AclXML * acls, UserPoolXML * upool, VirtualMachineXML* vm,
int vmem, int vcpu, vector<VectorAttribute *>& vpci, HostXML * host,
int &n_auth, int& n_error, int &n_fits, int &n_matched, string &error)
{
// -------------------------------------------------------------------------
// Filter current Hosts for resched VMs
// -------------------------------------------------------------------------
if (vm->is_resched() && vm->get_hid() == host->get_hid())
{
error = "VM cannot be migrated to its current Host.";
return false;
}
// -------------------------------------------------------------------------
// Check that VM can be deployed in local hosts
// -------------------------------------------------------------------------
if (vm->is_only_public_cloud() && !host->is_public_cloud())
{
error = "VM requires a Public Cloud Host, but it's local.";
return false;
}
// -------------------------------------------------------------------------
// Check if user is authorized
// -------------------------------------------------------------------------
if ( vm->get_uid() != 0 && vm->get_gid() != 0 )
{
PoolObjectAuth hperms;
host->get_permissions(hperms);
UserXML * user = upool->get(vm->get_uid());
if (user == 0)
{
error = "User does not exists.";
return false;
}
const vector<int> vgids = user->get_gids();
set<int> gids(vgids.begin(), vgids.end());
if ( !acls->authorize(vm->get_uid(), gids, hperms, AuthRequest::MANAGE))
{
error = "Permission denied.";
return false;
}
}
n_auth++;
// -------------------------------------------------------------------------
// Check host capacity
// -------------------------------------------------------------------------
if (host->test_capacity(vcpu, vmem, vpci, error) != true)
{
return false;
}
n_fits++;
// -------------------------------------------------------------------------
// Evaluate VM requirements
// -------------------------------------------------------------------------
if (!vm->get_requirements().empty())
{
char * estr;
bool matched;
if ( host->eval_bool(vm->get_requirements(), matched, &estr) != 0 )
{
ostringstream oss;
n_error++;
oss << "Error in SCHED_REQUIREMENTS: '" << vm->get_requirements()
<< "', error: " << estr;
vm->log(oss.str());
error = oss.str();
free(estr);
return false;
}
if (matched == false)
{
error = "It does not fulfill SCHED_REQUIREMENTS.";
return false;
}
}
n_matched++;
return true;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
* Match system DS's for this VM that:
* 1. Meet user/policy requirements
* 2. Have enough capacity to host the VM
*
* @param acl pool
* @param users the user pool
* @param vm the virtual machine
* @param vdisk vm requirement
* @param ds to evaluate vm assgiment
* @param n_auth number of ds authorized for the user, incremented if needed
* @param n_error number of requirement errors, incremented if needed
* @param n_matched number of system ds that fullfil VM sched_requirements
* @param n_fits number of system ds with capacity that fits the VM requirements
* @param error, string describing why the host is not valid
* @return true for a positive match
*/
static bool match_system_ds(AclXML * acls, UserPoolXML * upool,
VirtualMachineXML* vm, long long vdisk, DatastoreXML * ds, int& n_auth,
int& n_error, int& n_fits, int &n_matched, string &error)
{
// -------------------------------------------------------------------------
// Check if user is authorized
// -------------------------------------------------------------------------
if ( vm->get_uid() != 0 && vm->get_gid() != 0 )
{
PoolObjectAuth dsperms;
ds->get_permissions(dsperms);
UserXML * user = upool->get(vm->get_uid());
if (user == 0)
{
error = "User does not exists.";
return false;
}
const vector<int> vgids = user->get_gids();
set<int> gids(vgids.begin(), vgids.end());
if ( !acls->authorize(vm->get_uid(), gids, dsperms, AuthRequest::USE))
{
error = "Permission denied.";
return false;
}
}
n_auth++;
// -------------------------------------------------------------------------
// Check datastore capacity for shared systems DS (non-shared will be
// checked in a per host basis during dispatch). Resume actions do not
// add to shared system DS usage, and are skipped also
// -------------------------------------------------------------------------
if (ds->is_shared() && ds->is_monitored() && !vm->is_resume() &&
!ds->test_capacity(vdisk, error))
{
return false;
}
n_fits++;
// -------------------------------------------------------------------------
// Evaluate VM requirements
// -------------------------------------------------------------------------
if (!vm->get_ds_requirements().empty())
{
char * estr;
bool matched;
if ( ds->eval_bool(vm->get_ds_requirements(), matched, &estr) != 0 )
{
ostringstream oss;
n_error++;
oss << "Error in SCHED_DS_REQUIREMENTS: '"
<< vm->get_ds_requirements() << "', error: " << estr;
vm->log(oss.str());
error = oss.str();
free(estr);
return false;
}
if (matched == false)
{
error = "It does not fulfill SCHED_DS_REQUIREMENTS.";
return false;
}
}
n_matched++;
return true;
}
/* -------------------------------------------------------------------------- */
static void log_match(int vid, const string& msg)
{
ostringstream oss;
oss << "Match-making results for VM " << vid << ":\n\t" << msg << endl;
NebulaLog::log("SCHED", Log::DEBUG, oss);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void Scheduler::match_schedule()
{
VirtualMachineXML * vm;
int vm_memory;
int vm_cpu;
long long vm_disk;
vector<VectorAttribute *> vm_pci;
int n_resources;
int n_matched;
int n_auth;
int n_error;
int n_fits;
HostXML * host;
DatastoreXML *ds;
string m_error;
map<int, ObjectXML*>::const_iterator vm_it;
map<int, ObjectXML*>::const_iterator obj_it;
vector<SchedulerPolicy *>::iterator it;
const map<int, ObjectXML*> pending_vms = vmpool->get_objects();
const map<int, ObjectXML*> hosts = hpool->get_objects();
const map<int, ObjectXML*> datastores = dspool->get_objects();
const map<int, ObjectXML*> users = upool->get_objects();
double total_cl_match_time = 0;
double total_host_match_time = 0;
double total_host_rank_time = 0;
double total_ds_match_time = 0;
double total_ds_rank_time = 0;
time_t stime = time(0);
for (vm_it=pending_vms.begin(); vm_it != pending_vms.end(); vm_it++)
{
vm = static_cast<VirtualMachineXML*>(vm_it->second);
vm->get_requirements(vm_cpu, vm_memory, vm_disk, vm_pci);
n_resources = 0;
n_fits = 0;
n_matched = 0;
n_auth = 0;
n_error = 0;
//----------------------------------------------------------------------
// Test Image Datastore capacity, but not for migrations or resume
//----------------------------------------------------------------------
if (!vm->is_resched() && !vm->is_resume())
{
if (vm->test_image_datastore_capacity(img_dspool, m_error) == false)
{
if (vm->is_public_cloud()) //No capacity needed for public cloud
{
vm->set_only_public_cloud();
}
else
{
log_match(vm->get_oid(), "Cannot schedule VM. "+m_error);
vm->log("Cannot schedule VM. "+m_error);
vmpool->update(vm);
continue;
}
}
}
// ---------------------------------------------------------------------
// Match hosts for this VM.
// ---------------------------------------------------------------------
profile(true);
for (obj_it=hosts.begin(); obj_it != hosts.end(); obj_it++)
{
host = static_cast<HostXML *>(obj_it->second);
if (match_host(acls, upool, vm, vm_memory, vm_cpu, vm_pci, host,
n_auth, n_error, n_fits, n_matched, m_error))
{
vm->add_match_host(host->get_hid());
n_resources++;
}
else
{
if ( n_error > 0 )
{
log_match(vm->get_oid(), "Cannot schedule VM. " + m_error);
break;
}
else if (NebulaLog::log_level() >= Log::DDEBUG)
{
ostringstream oss;
oss << "Host " << host->get_hid() << " discarded for VM "
<< vm->get_oid() << ". " << m_error;
NebulaLog::log("SCHED", Log::DDEBUG, oss);
}
}
}
total_host_match_time += profile(false);
// ---------------------------------------------------------------------
// Log scheduling errors to VM user if any
// ---------------------------------------------------------------------
if (n_resources == 0) //No hosts assigned, let's see why
{
if (n_error == 0) //No syntax error
{
if (hosts.size() == 0)
{
vm->log("No hosts enabled to run VMs");
}
else if (n_auth == 0)
{
vm->log("User is not authorized to use any host");
}
else if (n_fits == 0)
{
ostringstream oss;
oss << "No host with enough capacity to deploy the VM";
vm->log(oss.str());
}
else if (n_matched == 0)
{
ostringstream oss;
oss << "No host meets capacity and SCHED_REQUIREMENTS: "
<< vm->get_requirements();
vm->log(oss.str());
}
}
vmpool->update(vm);
log_match(vm->get_oid(), "Cannot schedule VM, there is no suitable host.");
continue;
}
// ---------------------------------------------------------------------
// Schedule matched hosts
// ---------------------------------------------------------------------
profile(true);
for (it=host_policies.begin() ; it != host_policies.end() ; it++)
{
(*it)->schedule(vm);
}
vm->sort_match_hosts();
total_host_rank_time += profile(false);
if (vm->is_resched())//Will use same system DS for migrations
{
vm->add_match_datastore(vm->get_dsid());
continue;
}
// ---------------------------------------------------------------------
// Match datastores for this VM
// ---------------------------------------------------------------------
profile(true);
n_resources = 0;
n_auth = 0;
n_matched = 0;
n_error = 0;
n_fits = 0;
for (obj_it=datastores.begin(); obj_it != datastores.end(); obj_it++)
{
ds = static_cast<DatastoreXML *>(obj_it->second);
if (match_system_ds(acls, upool, vm, vm_disk, ds, n_auth, n_error,
n_fits, n_matched, m_error))
{
vm->add_match_datastore(ds->get_oid());
n_resources++;
}
else
{
if (n_error > 0)
{
log_match(vm->get_oid(), "Cannot schedule VM. " + m_error);
break;
}
else if (NebulaLog::log_level() >= Log::DDEBUG)
{
ostringstream oss;
oss << "System DS " << ds->get_oid() << " discarded for VM "
<< vm->get_oid() << ". " << m_error;
NebulaLog::log("SCHED", Log::DDEBUG, oss);
}
}
}
total_ds_match_time += profile(false);
// ---------------------------------------------------------------------
// Log scheduling errors to VM user if any
// ---------------------------------------------------------------------
if (n_resources == 0)
{
if (vm->is_public_cloud())//Public clouds don't need a system DS
{
vm->set_only_public_cloud();
continue;
}
else//No datastores assigned, let's see why
{
if (n_error == 0)//No syntax error
{
if (datastores.size() == 0)
{
vm->log("No system datastores found to run VMs");
}
else if (n_auth == 0)
{
vm->log("User is not authorized to use any system datastore");
}
else if (n_fits == 0)
{
ostringstream oss;
oss << "No system datastore with enough capacity for the VM";
vm->log(oss.str());
}
else if (n_matched == 0)
{
ostringstream oss;
oss << "No system datastore meets capacity "
<< "and SCHED_DS_REQUIREMENTS: "
<< vm->get_ds_requirements();
vm->log(oss.str());
}
}
vm->clear_match_hosts();
vmpool->update(vm);
log_match(vm->get_oid(), "Cannot schedule VM, there is no suitable "
"system ds.");
continue;