From 63db0bbc85ef7bef5fa39c190f88717c1d3e0980 Mon Sep 17 00:00:00 2001 From: Paul McKeown Date: Tue, 16 Jul 2024 16:54:49 +1200 Subject: [PATCH] Pre-release version of grade caching with cache read and write switches in the Coderunner settings. Just in case we want to ressurect them. --- .~lock.loadtestingresults2.ods# | 1 - classes/external/run_in_sandbox.php | 3 ++- classes/jobesandbox.php | 30 +++++++++++++++---------- lang/en/qtype_coderunner.php | 9 ++++---- settings.php | 34 ++++++++++++++++++++--------- 5 files changed, 49 insertions(+), 28 deletions(-) delete mode 100644 .~lock.loadtestingresults2.ods# diff --git a/.~lock.loadtestingresults2.ods# b/.~lock.loadtestingresults2.ods# deleted file mode 100644 index 59262f774..000000000 --- a/.~lock.loadtestingresults2.ods# +++ /dev/null @@ -1 +0,0 @@ -,rjl83,RichardEliteX2,29.02.2024 14:47,file:///home/rjl83/.config/libreoffice/4; \ No newline at end of file diff --git a/classes/external/run_in_sandbox.php b/classes/external/run_in_sandbox.php index a224b1620..a8c8826f0 100644 --- a/classes/external/run_in_sandbox.php +++ b/classes/external/run_in_sandbox.php @@ -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}"); } diff --git a/classes/jobesandbox.php b/classes/jobesandbox.php index ddda82b9e..bf4191ccc 100644 --- a/classes/jobesandbox.php +++ b/classes/jobesandbox.php @@ -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; @@ -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 + * 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) @@ -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); @@ -187,7 +198,7 @@ public function execute($sourcecode, $language, $input, $files = null, $params = if (self::DEBUGGING) { $runspec['debug'] = 1; } - + //echo "
$sourcecode"; if ($params !== null) { // Process any given sandbox parameters. $runspec['parameters'] = $params; @@ -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 '
' . serialize($runspec) . '
';. $runresult = $cache->get($key); // Unserializes the returned value :) false if not found. @@ -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 . '
'; diff --git a/lang/en/qtype_coderunner.php b/lang/en/qtype_coderunner.php index a1851f6b8..0729ad61d 100644 --- a/lang/en/qtype_coderunner.php +++ b/lang/en/qtype_coderunner.php @@ -1343,7 +1343,8 @@ function should be applied, e.g. {{STUDENT_ANSWER | e(\'py\')}} 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'; diff --git a/settings.php b/settings.php index 39c40f44c..eb7c3f0ba 100644 --- a/settings.php +++ b/settings.php @@ -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'), @@ -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 -)); -*/