Skip to content

Commit

Permalink
Pre-release version of grade caching with cache read and write switches
Browse files Browse the repository at this point in the history
in the Coderunner settings. Just in case we want to ressurect them.
  • Loading branch information
mckeownp committed Jul 16, 2024
1 parent 9f97aef commit 63db0bb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
1 change: 0 additions & 1 deletion .~lock.loadtestingresults2.ods#

This file was deleted.

3 changes: 2 additions & 1 deletion classes/external/run_in_sandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ public static function execute(
if ($jobehostws !== '') {
$paramsarray['jobeserver'] = $jobehostws;
}
$runresult = $sandbox->execute($sourcecode, $language, $stdin, $filesarray, $paramsarray);
// usecache set to false for these runs as we will never regrade them.
$runresult = $sandbox->execute($sourcecode, $language, $stdin, $filesarray, $paramsarray, usecache:false);
} catch (Exception $ex) {
throw new qtype_coderunner_exception("Attempt to run job failed with error {$ex->message}");
}
Expand Down
30 changes: 18 additions & 12 deletions classes/jobesandbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
global $CFG;
require_once($CFG->libdir . '/filelib.php'); // Needed when run as web service.

const READ_FROM_CACHE = true;
const WRITE_TO_CACHE = true;

class qtype_coderunner_jobesandbox extends qtype_coderunner_sandbox {
const DEBUGGING = 0;
Expand Down Expand Up @@ -112,11 +110,24 @@ public function get_languages() {
* @param string $language One of the languages recognised by the sandbox
* @param string $input A string to use as standard input during execution
* @param associative array $files either null or a map from filename to
* file contents, defining a file context at execution time
* file contents, defining a file context at execution time.
* @param bool $usecache Will read and write to cache if set to True. Otherwise
* won't use the cache. NOTE: global settings for cache <read>
* are also factored in here.
* @param associative array $params Sandbox parameters, depends on
* particular sandbox but most sandboxes should recognise
* at least cputime (secs) and memorylimit (Megabytes).
* If the $params array is null, sandbox defaults are used.
* @param bool $usecache determines if result caching (both reads and writes)
* will be used. This is true by default (which is usually for normal
* grading runs) but is typically set to false by calls from the
* web-service/sandbox (eg, scratchpad or try-it box runs, as these are never
* going to be regraded). Note, the coderunner settings for enabling
* cache reads and writes are combined with this argument to determine
* if cache reading and writing are done. For example, if usecache
* is true and enablegradecachereads is true then this function
* will try to read the result from the grading cache but if
* either is false then it won't.
* @return an object with at least the attribute 'error'.
* The error attribute is one of the
* values 0 through 9 (OK to UNKNOWN_SERVER_ERROR, OVERLOAD)
Expand All @@ -140,7 +151,7 @@ public function get_languages() {
* showing which jobeserver was used and what key was used (if any).
*/

public function execute($sourcecode, $language, $input, $files = null, $params = null) {
public function execute($sourcecode, $language, $input, $files = null, $params = null, $usecache = true) {
global $CFG;

$language = strtolower($language);
Expand Down Expand Up @@ -187,7 +198,7 @@ public function execute($sourcecode, $language, $input, $files = null, $params =
if (self::DEBUGGING) {
$runspec['debug'] = 1;
}

//echo "<br> $sourcecode";
if ($params !== null) {
// Process any given sandbox parameters.
$runspec['parameters'] = $params;
Expand All @@ -208,16 +219,11 @@ public function execute($sourcecode, $language, $input, $files = null, $params =

$cache = cache::make('qtype_coderunner', 'coderunner_grading_cache');
$runresult = null;
if (READ_FROM_CACHE) {
if (get_config('qtype_coderunner', 'enablegradecachereads') && $usecache) {
// NOTE: Changing jobeserver setting will effectively flush the cache
// eg, adding another jobeserver to a list of servers will mean the
// jobeserver parameter has changed and therefore the key will change.

// QUESTION: Do we want the cache to ignore the jobeserver setting?
// eg, adding a new, presumeably equal jobeserver to the mix shouldn't
// change the result (unless it isn't equal!)
// But, remember that the server is chosen at random from the pool!

$key = hash("md5", serialize($runspec));
// Debugger: echo '<pre>' . serialize($runspec) . '</pre>';.
$runresult = $cache->get($key); // Unserializes the returned value :) false if not found.
Expand Down Expand Up @@ -293,7 +299,7 @@ public function execute($sourcecode, $language, $input, $files = null, $params =
$runresult['output'] = $this->filter_file_path($this->response->stdout);

// Got a useable result from Jobe server so cache it if required.
if (WRITE_TO_CACHE) {
if (get_config('qtype_coderunner', 'enablegradecachewrites') && $usecache) {
$key = hash("md5", serialize($runspec));
$cache->set($key, $runresult); // Set serializes the result, get will unserialize.
// echo 'CACHE WRITE for ---> ' . $key . '<br>';
Expand Down
9 changes: 5 additions & 4 deletions lang/en/qtype_coderunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -1343,7 +1343,8 @@ function should be applied, e.g. <code>{{STUDENT_ANSWER | e(\'py\')}}</code> is
$string['wssubmissionrateexceeded'] = 'You have exceeded the maximum hourly \'Try it!\' submission rate. Request denied.';

$string['xmlcoderunnerformaterror'] = 'XML format error in coderunner question';
$string['coderunner_grading_cache'] = 'Caches grading results so we can avoid going to Jobe so often';
$string['cachedef_coderunner_grading_cache'] = 'Caches grading results so we can avoid going to Jobe so often';
$string['cachegradingresultsenable'] = 'Cache results when grading answers.';
$string['cachegradingresults_desc'] = 'Uses a local Moodle cache (currently file cache) to store results of grading questions. Mainly to speed up regrading by using cached results for steps where the same quesiton and answer have already been graded.';
$string['enablegradecachereads'] = 'Enable reading of results from the Coderunner grading cache.';
$string['enablegradecachereads_desc'] = 'Enable writing of results to the Coderunner grading cache. The cache is a local Moodle cache (currently file cache) to store results of grading questions. Mainly to speed up regrading by using cached results for steps where the same quesiton and answer have already been graded.';
$string['enablegradecachewrites'] = 'Enable reading of results from grading cache.';
$string['enablegradecachewrites_desc'] = 'Enables writing of grading results to the Coderunner grading cache. If the same question, student answer pairing is found then the cached result is used. The cache is a local Moodle cache (currently file cache) to store results of grading questions. Mainly to speed up regrading by using cached results for steps where the same quesiton and answer have already been graded.';
$string['coderunnercachesettingsheading'] = 'Coderunner grade result cache settings';
34 changes: 24 additions & 10 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,30 @@
''
));


$settings->add(new admin_setting_heading(
'codeRunnercachesettings',
get_string('coderunnercachesettingsheading', 'qtype_coderunner'),
''
));

$settings->add(new admin_setting_configcheckbox(
"qtype_coderunner/enablegradecachereads",
get_string('enablegradecachereads', 'qtype_coderunner'),
get_string('enablegradecachereads_desc', 'qtype_coderunner'),
false
));

$settings->add(new admin_setting_configcheckbox(
"qtype_coderunner/enablegradecachewrites",
get_string('enablegradecachewrites', 'qtype_coderunner'),
get_string('enablegradecachewrites_desc', 'qtype_coderunner'),
false
));




$settings->add(new admin_setting_heading(
'codeRunnerwssettings',
get_string('coderunnerwssettings', 'qtype_coderunner'),
Expand Down Expand Up @@ -135,13 +159,3 @@
'5'
));

/*
Currently left out so we can test
$settings->add(new admin_setting_configcheckbox(
"qtype_coderunner/cachegradingresults",
get_string('cachegradingresultsenable', 'qtype_coderunner'),
get_string('cachegradingresults_desc', 'qtype_coderunner'),
false
));
*/

0 comments on commit 63db0bb

Please sign in to comment.