Skip to content

Commit

Permalink
MDL-74548 backup: Refactor course copies
Browse files Browse the repository at this point in the history
This patch modifies the way copy data is shared in order to mitigate potential race conditions
and ensure that the serialised controller stored in the DB is always in a valid state.

The restore controller is now considered the "source of truth" for all information about the
copy operation. Backup controllers can no longer contain information about course copies.

As copy creation is not atomic, it is still possible for copy controllers to become orphaned or
exist in an invalid state. To mitigate this the backup cleanup task has been modified to call
a new helper method copy_helper::cleanup_orphaned_copy_controllers.

Summary of changes in this patch:

    - Copy data must now be passed through the restore controller's constructor
    - base_controller::get_copy has been deprecated in favour of restore_controller::get_copy
    - base_controller::set_copy has been deprecated without replacement
    - core_backup\copy\copy has been deprecated, use copy_helper.class.php's copy_helper instead
    - backup_cleanup_task will now clean up orphaned controllers from copy operations that went awry

Thanks to Peter Burnett for assiting with testing this patch.
  • Loading branch information
cameron1729 committed Jun 29, 2022
1 parent 1f6ab2b commit 4751e67
Show file tree
Hide file tree
Showing 15 changed files with 398 additions and 120 deletions.
29 changes: 28 additions & 1 deletion backup/controller/restore_controller.class.php
Expand Up @@ -65,6 +65,13 @@ class restore_controller extends base_controller {
/** @var int Number of restore_controllers that are currently executing */
protected static $executing = 0;

/**
* Holds the relevant destination information for course copy operations.
*
* @var \stdClass.
*/
protected $copy;

/**
* Constructor.
*
Expand All @@ -79,10 +86,17 @@ class restore_controller extends base_controller {
* @param int $userid
* @param int $target backup::TARGET_[ NEW_COURSE | CURRENT_ADDING | CURRENT_DELETING | EXISTING_ADDING | EXISTING_DELETING ]
* @param \core\progress\base $progress Optional progress monitor
* @param \stdClass $copydata Course copy data, required when in MODE_COPY
* @param bool $releasesession Should release the session? backup::RELEASESESSION_YES or backup::RELEASESESSION_NO
*/
public function __construct($tempdir, $courseid, $interactive, $mode, $userid, $target,
\core\progress\base $progress = null, $releasesession = backup::RELEASESESSION_NO) {
\core\progress\base $progress = null, $releasesession = backup::RELEASESESSION_NO, ?\stdClass $copydata = null) {

if ($mode == backup::MODE_COPY && is_null($copydata)) {
throw new restore_controller_exception('cannot_instantiate_missing_copydata');
}

$this->copy = $copydata;
$this->tempdir = $tempdir;
$this->courseid = $courseid;
$this->interactive = $interactive;
Expand Down Expand Up @@ -563,6 +577,19 @@ public function prepare_copy(): void {
$this->progress->end_progress();
}

/**
* Get the course copy data.
*
* @return \stdClass
*/
public function get_copy(): \stdClass {
if ($this->mode != backup::MODE_COPY) {
throw new restore_controller_exception('cannot_get_copy_wrong_mode');
}

return $this->copy;
}

// Protected API starts here

protected function calculate_restoreid() {
Expand Down
16 changes: 7 additions & 9 deletions backup/controller/tests/controller_test.php
Expand Up @@ -72,17 +72,15 @@ protected function setUp(): void {
}

/**
* Test set copy method.
* Test instantiating a restore controller for a course copy without providing copy data.
*
* @covers \restore_controller::__construct
*/
public function test_base_controller_set_copy() {
$this->expectException(\backup_controller_exception::class);
$copy = new \stdClass();

// Set up controller as a non-copy operation.
$bc = new \backup_controller(backup::TYPE_1COURSE, $this->courseid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $this->userid, backup::RELEASESESSION_YES);
public function test_restore_controller_copy_without_copydata() {
$this->expectException(\restore_controller_exception::class);

$bc->set_copy($copy);
new \restore_controller(1729, $this->courseid, backup::INTERACTIVE_NO, backup::MODE_COPY,
$this->userid, backup::TARGET_NEW_COURSE);
}

/*
Expand Down
5 changes: 3 additions & 2 deletions backup/copy.php
Expand Up @@ -24,6 +24,7 @@
*/

require_once('../config.php');
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');

defined('MOODLE_INTERNAL') || die();

Expand Down Expand Up @@ -70,8 +71,8 @@
} else if ($mdata = $mform->get_data()) {

// Process the form and create the copy task.
$backupcopy = new \core_backup\copy\copy($mdata);
$backupcopy->create_copy();
$copydata = \copy_helper::process_formdata($mdata);
\copy_helper::create_copy($copydata);

if (!empty($mdata->submitdisplay)) {
// Redirect to the copy progress overview.
Expand Down
4 changes: 2 additions & 2 deletions backup/externallib.php
Expand Up @@ -388,8 +388,8 @@ public static function submit_copy_form($jsonformdata) {

if ($mdata) {
// Create the copy task.
$backupcopy = new \core_backup\copy\copy($mdata);
$copyids = $backupcopy->create_copy();
$copydata = \copy_helper::process_formdata($mdata);
$copyids = \copy_helper::create_copy($copydata);
} else {
throw new moodle_exception('copyformfail', 'backup');
}
Expand Down
57 changes: 28 additions & 29 deletions backup/tests/course_copy_test.php
Expand Up @@ -163,15 +163,14 @@ public function test_create_copy() {
$formdata->role_3 = 3;
$formdata->role_5 = 5;

$coursecopy = new \core_backup\copy\copy($formdata);
$result = $coursecopy->create_copy();
$copydata = \copy_helper::process_formdata($formdata);
$result = \copy_helper::create_copy($copydata);

// Load the controllers, to extract the data we need.
$bc = \backup_controller::load_controller($result['backupid']);
$rc = \restore_controller::load_controller($result['restoreid']);

// Check the backup controller.
$this->assertEquals($result, $bc->get_copy()->copyids);
$this->assertEquals(backup::MODE_COPY, $bc->get_mode());
$this->assertEquals($this->course->id, $bc->get_courseid());
$this->assertEquals(backup::TYPE_1COURSE, $bc->get_type());
Expand All @@ -180,7 +179,6 @@ public function test_create_copy() {
$newcourseid = $rc->get_courseid();
$newcourse = get_course($newcourseid);

$this->assertEquals($result, $rc->get_copy()->copyids);
$this->assertEquals(get_string('copyingcourse', 'backup'), $newcourse->fullname);
$this->assertEquals(get_string('copyingcourseshortname', 'backup'), $newcourse->shortname);
$this->assertEquals(backup::MODE_COPY, $rc->get_mode());
Expand Down Expand Up @@ -222,11 +220,11 @@ public function test_get_copies() {
$formdata2->shortname = 'tree';

// Create some copies.
$coursecopy = new \core_backup\copy\copy($formdata);
$result = $coursecopy->create_copy();
$copydata = \copy_helper::process_formdata($formdata);
$result = \copy_helper::create_copy($copydata);

// Backup, awaiting.
$copies = \core_backup\copy\copy::get_copies($USER->id);
$copies = \copy_helper::get_copies($USER->id);
$this->assertEquals($result['backupid'], $copies[0]->backupid);
$this->assertEquals($result['restoreid'], $copies[0]->restoreid);
$this->assertEquals(\backup::STATUS_AWAITING, $copies[0]->status);
Expand All @@ -236,27 +234,27 @@ public function test_get_copies() {

// Backup, in progress.
$bc->set_status(\backup::STATUS_EXECUTING);
$copies = \core_backup\copy\copy::get_copies($USER->id);
$copies = \copy_helper::get_copies($USER->id);
$this->assertEquals($result['backupid'], $copies[0]->backupid);
$this->assertEquals($result['restoreid'], $copies[0]->restoreid);
$this->assertEquals(\backup::STATUS_EXECUTING, $copies[0]->status);
$this->assertEquals(\backup::OPERATION_BACKUP, $copies[0]->operation);

// Restore, ready to process.
$bc->set_status(\backup::STATUS_FINISHED_OK);
$copies = \core_backup\copy\copy::get_copies($USER->id);
$this->assertEquals($result['backupid'], $copies[0]->backupid);
$copies = \copy_helper::get_copies($USER->id);
$this->assertEquals(null, $copies[0]->backupid);
$this->assertEquals($result['restoreid'], $copies[0]->restoreid);
$this->assertEquals(\backup::STATUS_REQUIRE_CONV, $copies[0]->status);
$this->assertEquals(\backup::OPERATION_RESTORE, $copies[0]->operation);

// No records.
$bc->set_status(\backup::STATUS_FINISHED_ERR);
$copies = \core_backup\copy\copy::get_copies($USER->id);
$copies = \copy_helper::get_copies($USER->id);
$this->assertEmpty($copies);

$coursecopy2 = new \core_backup\copy\copy($formdata2);
$result2 = $coursecopy2->create_copy();
$copydata2 = \copy_helper::process_formdata($formdata2);
$result2 = \copy_helper::create_copy($copydata2);
// Set the second copy to be complete.
$bc = \backup_controller::load_controller($result2['backupid']);
$bc->set_status(\backup::STATUS_FINISHED_OK);
Expand All @@ -265,7 +263,7 @@ public function test_get_copies() {
$rc->set_status(\backup::STATUS_FINISHED_OK);

// No records.
$copies = \core_backup\copy\copy::get_copies($USER->id);
$copies = \copy_helper::get_copies($USER->id);
$this->assertEmpty($copies);
}

Expand All @@ -291,11 +289,11 @@ public function test_get_copies_course() {
$formdata->role_5 = 5;

// Create some copies.
$coursecopy = new \core_backup\copy\copy($formdata);
$coursecopy->create_copy();
$copydata = \copy_helper::process_formdata($formdata);
\copy_helper::create_copy($copydata);

// No copies match this course id.
$copies = \core_backup\copy\copy::get_copies($USER->id, ($this->course->id + 1));
$copies = \copy_helper::get_copies($USER->id, ($this->course->id + 1));
$this->assertEmpty($copies);
}

Expand All @@ -321,13 +319,13 @@ public function test_get_copies_course_deleted() {
$formdata->role_5 = 5;

// Create some copies.
$coursecopy = new \core_backup\copy\copy($formdata);
$coursecopy->create_copy();
$copydata = \copy_helper::process_formdata($formdata);
\copy_helper::create_copy($copydata);

delete_course($this->course->id, false);

// No copies match this course id as it has been deleted.
$copies = \core_backup\copy\copy::get_copies($USER->id, ($this->course->id));
$copies = \copy_helper::get_copies($USER->id, ($this->course->id));
$this->assertEmpty($copies);
}

Expand All @@ -353,8 +351,8 @@ public function test_course_copy() {
$formdata->role_5 = 5;

// Create the course copy records and associated ad-hoc task.
$coursecopy = new \core_backup\copy\copy($formdata);
$copyids = $coursecopy->create_copy();
$copydata = \copy_helper::process_formdata($formdata);
$copyids = \copy_helper::create_copy($copydata);

$courseid = $this->course->id;

Expand Down Expand Up @@ -430,8 +428,8 @@ public function test_course_copy_no_users() {
$formdata->role_5 = 0;

// Create the course copy records and associated ad-hoc task.
$coursecopy = new \core_backup\copy\copy($formdata);
$copyids = $coursecopy->create_copy();
$copydata = \copy_helper::process_formdata($formdata);
$copyids = \copy_helper::create_copy($copydata);

$courseid = $this->course->id;

Expand Down Expand Up @@ -499,8 +497,8 @@ public function test_course_copy_students_data() {
$formdata->role_5 = 5;

// Create the course copy records and associated ad-hoc task.
$coursecopy = new \core_backup\copy\copy($formdata);
$copyids = $coursecopy->create_copy();
$copydata = \copy_helper::process_formdata($formdata);
$copyids = \copy_helper::create_copy($copydata);

$courseid = $this->course->id;

Expand Down Expand Up @@ -568,8 +566,8 @@ public function test_course_copy_no_data() {
$formdata->role_5 = 5;

// Create the course copy records and associated ad-hoc task.
$coursecopy = new \core_backup\copy\copy($formdata);
$copyids = $coursecopy->create_copy();
$copydata = \copy_helper::process_formdata($formdata);
$copyids = \copy_helper::create_copy($copydata);

$courseid = $this->course->id;

Expand Down Expand Up @@ -627,6 +625,7 @@ public function test_malformed_instantiation() {

// Expect and exception as form data is incomplete.
$this->expectException(\moodle_exception::class);
new \core_backup\copy\copy($formdata);
$copydata = \copy_helper::process_formdata($formdata);
\copy_helper::create_copy($copydata);
}
}
4 changes: 2 additions & 2 deletions backup/tests/externallib_test.php
Expand Up @@ -82,8 +82,8 @@ public function test_get_copy_progress() {
$formdata->role_3 = 3;
$formdata->role_5 = 5;

$coursecopy = new \core_backup\copy\copy($formdata);
$copydetails = $coursecopy->create_copy();
$copydata = \copy_helper::process_formdata($formdata);
$copydetails = \copy_helper::create_copy($copydata);
$copydetails['operation'] = \backup::OPERATION_BACKUP;

$params = array('copies' => $copydetails);
Expand Down

0 comments on commit 4751e67

Please sign in to comment.