function RPCisAvailable($imageid, $start, $end, $userid) { #FIXME this function doesn't properly handle cluster reservations global $requestInfo; $images = getImages(); $requestInfo["start"] = $start; $requestInfo["end"] = $end; $requestInfo["imageid"] = $imageid; $allocatedcompids = array(0); if ($requestInfo["start"] <= time()) { $now = 1; } else { $now = 0; } # get list of schedules $starttime = minuteOfWeek($start); $endtime = minuteOfWeek($end); # request is within a single week if (weekOfYear($start) == weekOfYear($end)) { $query = "SELECT scheduleid " . "FROM scheduletimes " . "WHERE start <= {$starttime} AND " . "end >= {$endtime}"; } elseif ($end - $start >= SECINDAY * 7) { $query = "SELECT scheduleid " . "FROM scheduletimes " . "WHERE start = 0 AND " . "end = 10080"; } else { $query = "SELECT s1.scheduleid " . "FROM scheduletimes s1, " . "scheduletimes s2 " . "WHERE s1.scheduleid = s2.scheduleid AND " . "s1.start <= {$starttime} AND " . "s1.end = 10080 AND " . "s2.start = 0 AND " . "s2.end >= {$endtime}"; } $scheduleids = array(); $qh = doQuery($query, 127); while ($row = mysql_fetch_row($qh)) { array_push($scheduleids, $row[0]); } $requestInfo["computers"] = array(); $requestInfo["computers"][0] = 0; $requestInfo["images"][0] = $imageid; # loop to check for available computers for all needed images if ($images[$imageid]["imagemetaid"] != NULL) { $count = 1; foreach ($images[$imageid]["subimages"] as $imgid) { $requestInfo['computers'][$count] = 0; $requestInfo['images'][$count] = $imgid; $count++; } } // get semaphore lock if (!semLock()) { abort(3); } $startstamp = unixToDatetime($start); $endstamp = unixToDatetime($end + 900); foreach ($requestInfo["images"] as $key => $imageid) { #$osid = getOSid($os); # check for max concurrent usage of image if ($images[$imageid]['maxconcurrent'] != NULL) { $query = "SELECT COUNT(rs.imageid) AS currentusage " . "FROM reservation rs, " . "request rq " . "WHERE '{$startstamp}' < rq.end AND " . "'{$endstamp}' > (rq.start - INTERVAL 900 SECOND) AND " . "rs.requestid = rq.id AND " . "rs.imageid = {$imageid} AND " . "rq.stateid NOT IN (1,5,11,12,16,17)"; $qh = doQuery($query, 101); if (!($row = mysql_fetch_assoc($qh))) { semUnlock(); return 0; } if ($row['currentusage'] >= $images[$imageid]['maxconcurrent']) { semUnlock(); return -1; } } # get platformid that matches $imageid $query = "SELECT platformid FROM image WHERE id = {$imageid}"; $qh = doQuery($query, 125); if (!($row = mysql_fetch_row($qh))) { semUnlock(); return 0; } $platformid = $row[0]; # get computers $imageid maps to $tmp = getMappedResources($imageid, "image", "computer"); if (!count($tmp)) { semUnlock(); return 0; } $mappedcomputers = implode(',', $tmp); # get computers for available schedules and platforms $computerids = array(); $currentids = array(); $blockids = array(); # get list of available computers $resources = getUserResources(array("imageAdmin", "imageCheckOut"), array("available"), 0, 0, $userid); $computers = implode("','", array_keys($resources["computer"])); $computers = "'{$computers}'"; $alloccompids = implode(",", $allocatedcompids); $schedules = implode(',', $scheduleids); $query = "SELECT DISTINCT c.id, " . "c.currentimageid " . "FROM computer c, " . "image i, " . "state s " . "WHERE c.scheduleid IN ({$schedules}) AND " . "c.platformid = {$platformid} AND " . "c.stateid = s.id AND " . "s.name != 'maintenance' AND " . "s.name != 'vmhostinuse' AND " . "s.name != 'hpc' AND " . "s.name != 'failed' AND "; if ($now) { $query .= "s.name != 'reloading' AND " . "s.name != 'timeout' AND " . "s.name != 'inuse' AND "; } $query .= "i.id = {$imageid} AND " . "c.RAM >= i.minram AND " . "c.procnumber >= i.minprocnumber AND " . "c.procspeed >= i.minprocspeed AND " . "c.network >= i.minnetwork AND " . "c.id IN ({$computers}) AND " . "c.id IN ({$mappedcomputers}) AND " . "c.id NOT IN ({$alloccompids}) " . "ORDER BY (c.procspeed * c.procnumber) DESC, " . "RAM DESC, " . "network DESC"; $qh = doQuery($query, 129); while ($row = mysql_fetch_assoc($qh)) { array_push($computerids, $row['id']); if ($row['currentimageid'] == $imageid) { array_push($currentids, $row['id']); } } # get computer ids available from block reservations $blockids = getAvailableBlockComputerids($imageid, $start, $end, $allocatedcompids); # remove computers from list that are already scheduled $usedComputerids = array(); $query = "SELECT DISTINCT rs.computerid " . "FROM reservation rs, " . "request rq, " . "user u " . "WHERE '{$startstamp}' < rq.end AND " . "'{$endstamp}' > (rq.start - INTERVAL 900 SECOND) AND " . "rs.requestid = rq.id AND " . "rq.stateid != 1 AND " . "rq.stateid != 5 AND " . "rq.stateid != 12 AND " . "rq.userid = u.id AND " . "u.unityid != 'vclreload'"; $qh = doQuery($query, 130); while ($row = mysql_fetch_row($qh)) { array_push($usedComputerids, $row[0]); } $computerids = array_diff($computerids, $usedComputerids); $currentids = array_diff($currentids, $usedComputerids); $blockids = array_diff($blockids, $usedComputerids); if (count($currentids)) { $return = array_shift($currentids); } elseif (count($computerids)) { $return = array_shift($computerids); } else { $return = 0; } } semUnlock(); return $return; }
function getAvailableSchedules($start, $end) { # get list of schedules $starttime = minuteOfWeek($start); $endtime = minuteOfWeek($end); # request is within a single week if (weekOfYear($start) == weekOfYear($end)) { $query = "SELECT scheduleid " . "FROM scheduletimes " . "WHERE start <= {$starttime} AND " . "end >= {$endtime}"; } elseif ($end - $start >= SECINDAY * 7) { $query = "SELECT scheduleid " . "FROM scheduletimes " . "WHERE start = 0 AND " . "end = 10080"; } else { $query = "SELECT s1.scheduleid " . "FROM scheduletimes s1, " . "scheduletimes s2 " . "WHERE s1.scheduleid = s2.scheduleid AND " . "s1.start <= {$starttime} AND " . "s1.end = 10080 AND " . "s2.start = 0 AND " . "s2.end >= {$endtime}"; } $scheduleids = array(); $qh = doQuery($query, 127); while ($row = mysql_fetch_row($qh)) { array_push($scheduleids, $row[0]); } return $scheduleids; }