Hi,
I insert my code to the function allocComputer in the utils.php. The main
function of my code is randomly pick up a machine from the computer array
and return it to the caller. The reason is that I want to distribute the
reserved virtual machine to different blades, therefore balance the
workload.
Generally, what I am doing is that, I check the length of available computer
in the array first, and randomly pick up machines from the array. Then I
exchange this machine's id with array(0). At last, I pass this array to
original allocComputer code to check computer's availability and set up the
computer id.
This is simple code to achieve the some extent of distribution. For your
reference, this is my allocComputer code.
Thanks,
Xianqing
////////////////////////////////////////////////////////////////////////////////
///
/// \fn allocComputer($blockids, $currentids, $computerids, $start,
/// $nowfuture)
///
/// \param $blockids - array of computer ids
/// \param $currentids - array of computer ids
/// \param $computerids - array of computer ids
/// \param $start - start time in datetime format
/// \param $nowfuture - "now" or "future"
///
/// \return empty array if failed to allocate a computer; array with these
keys
/// on success:\n
/// \b compid - id of computer\n
/// \b mgmtid - id of management node for computer\n
/// \b loaded - 0 or 1 - whether or not computer is loaded with desired
image
///
/// \brief determines a computer to use from $blockids, $currentids,
/// and $computerids, looking at the arrays in that order and
/// tries to allocate a management node for it
///
////////////////////////////////////////////////////////////////////////////////
function allocComputer($blockids, $currentids, $computerids, $start,
$nowfuture) {
$ret = array();
if (count($blockids) > 0)
{
$random_number = rand(0,count($blockids) - 1);
while($blockids[$random_number] == null)
$random_number = rand(0,count($blockids) - 1);
$tp = $blockids[0];
$blockids[0] = $blockids[$random_number];
$blockids[$random_number] = $tp;
# printArray($blockids);
}
foreach($blockids as $compid) {
$mgmtnodeid = findManagementNode($compid, $start, $nowfuture);
if($mgmtnodeid == 0)
continue;
$ret['compid'] = $compid;
$ret['mgmtid'] = $mgmtnodeid;
$ret['loaded'] = 1;
return $ret;
}
if (count($currentids) > 0)
{
$random_number1 = rand(0,count($currentids) - 1);
while($currentids[$random_number1] == null)
$random_number1 = rand(0,count($currentids) - 1);
$tp1 = $currentids[0];
$currentids[0] = $currentids[$random_number1];
$currentids[$random_number1] = $tp1;
# printArray($currentids);
}
foreach($currentids as $compid) {
$mgmtnodeid = findManagementNode($compid, $start, $nowfuture);
if($mgmtnodeid == 0)
continue;
$ret['compid'] = $compid;
$ret['mgmtid'] = $mgmtnodeid;
$ret['loaded'] = 1;
return $ret;
}
if (count($computerids) > 0)
{
$random_number2 = rand(0,count($computerids) - 1);
while($computerids[$random_number2] == null)
$random_number2 = rand(0,count($computerids) - 1);
$tp2 = $computerids[0];
$computerids[0] = $computerids[$random_number2];
$computerids[$random_number2] = $tp2;
# printArray($computerids);
}
foreach($computerids as $compid) {
$mgmtnodeid = findManagementNode($compid, $start, $nowfuture);
if($mgmtnodeid == 0)
continue;
$ret['compid'] = $compid;
$ret['mgmtid'] = $mgmtnodeid;
$ret['loaded'] = 0;
return $ret;
}
return $ret;
}