Merge branch '12414-delete-trashed-project-contents'
authorLucas Di Pentima <ldipentima@veritasgenetics.com>
Mon, 2 Apr 2018 19:58:48 +0000 (16:58 -0300)
committerLucas Di Pentima <ldipentima@veritasgenetics.com>
Mon, 2 Apr 2018 20:00:08 +0000 (17:00 -0300)
Closes #12414

Arvados-DCO-1.1-Signed-off-by: Lucas Di Pentima <ldipentima@veritasgenetics.com>

services/api/app/models/collection.rb
services/api/lib/sweep_trashed_collections.rb [deleted file]
services/api/lib/sweep_trashed_objects.rb [new file with mode: 0644]
services/api/test/fixtures/groups.yml
services/api/test/fixtures/jobs.yml
services/api/test/unit/collection_test.rb
services/api/test/unit/group_test.rb

index a088d48e68f466a6b36ad4d663a031008cc95fd7..4772768c8fe086f1e3bc2a25ca7a134cef8d436c 100644 (file)
@@ -3,7 +3,7 @@
 # SPDX-License-Identifier: AGPL-3.0
 
 require 'arvados/keep'
-require 'sweep_trashed_collections'
+require 'sweep_trashed_objects'
 require 'trashable'
 
 class Collection < ArvadosModel
@@ -448,7 +448,7 @@ class Collection < ArvadosModel
   end
 
   def self.where *args
-    SweepTrashedCollections.sweep_if_stale
+    SweepTrashedObjects.sweep_if_stale
     super
   end
 
diff --git a/services/api/lib/sweep_trashed_collections.rb b/services/api/lib/sweep_trashed_collections.rb
deleted file mode 100644 (file)
index a899191..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-# Copyright (C) The Arvados Authors. All rights reserved.
-#
-# SPDX-License-Identifier: AGPL-3.0
-
-require 'current_api_client'
-
-module SweepTrashedCollections
-  extend CurrentApiClient
-
-  def self.sweep_now
-    act_as_system_user do
-      Collection.
-        where('delete_at is not null and delete_at < statement_timestamp()').
-        destroy_all
-      Collection.
-        where('is_trashed = false and trash_at < statement_timestamp()').
-        update_all('is_trashed = true')
-    end
-  end
-
-  def self.sweep_if_stale
-    return if Rails.configuration.trash_sweep_interval <= 0
-    exp = Rails.configuration.trash_sweep_interval.seconds
-    need = false
-    Rails.cache.fetch('SweepTrashedCollections', expires_in: exp) do
-      need = true
-    end
-    if need
-      Thread.new do
-        Thread.current.abort_on_exception = false
-        begin
-          sweep_now
-        rescue => e
-          Rails.logger.error "#{e.class}: #{e}\n#{e.backtrace.join("\n\t")}"
-        ensure
-          ActiveRecord::Base.connection.close
-        end
-      end
-    end
-  end
-end
diff --git a/services/api/lib/sweep_trashed_objects.rb b/services/api/lib/sweep_trashed_objects.rb
new file mode 100644 (file)
index 0000000..59008c0
--- /dev/null
@@ -0,0 +1,74 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
+require 'current_api_client'
+
+module SweepTrashedObjects
+  extend CurrentApiClient
+
+  def self.delete_project_and_contents(p_uuid)
+    p = Group.find_by_uuid(p_uuid)
+    if !p || p.group_class != 'project'
+      raise "can't sweep group '#{p_uuid}', it may not exist or not be a project"
+    end
+    # First delete sub projects
+    Group.where({group_class: 'project', owner_uuid: p_uuid}).each do |sub_project|
+      delete_project_and_contents(sub_project.uuid)
+    end
+    # Next, iterate over all tables which have owner_uuid fields, with some
+    # exceptions, and delete records owned by this project
+    skipped_classes = ['Group', 'User']
+    ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |klass|
+      if !skipped_classes.include?(klass.name) && klass.columns.collect(&:name).include?('owner_uuid')
+        klass.where({owner_uuid: p_uuid}).destroy_all
+      end
+    end
+    # Finally delete the project itself
+    p.destroy
+  end
+
+  def self.sweep_now
+    act_as_system_user do
+      # Sweep trashed collections
+      Collection.
+        where('delete_at is not null and delete_at < statement_timestamp()').
+        destroy_all
+      Collection.
+        where('is_trashed = false and trash_at < statement_timestamp()').
+        update_all('is_trashed = true')
+
+      # Sweep trashed projects and their contents
+      Group.
+        where({group_class: 'project'}).
+        where('delete_at is not null and delete_at < statement_timestamp()').each do |project|
+          delete_project_and_contents(project.uuid)
+      end
+      Group.
+        where({group_class: 'project'}).
+        where('is_trashed = false and trash_at < statement_timestamp()').
+        update_all('is_trashed = true')
+    end
+  end
+
+  def self.sweep_if_stale
+    return if Rails.configuration.trash_sweep_interval <= 0
+    exp = Rails.configuration.trash_sweep_interval.seconds
+    need = false
+    Rails.cache.fetch('SweepTrashedObjects', expires_in: exp) do
+      need = true
+    end
+    if need
+      Thread.new do
+        Thread.current.abort_on_exception = false
+        begin
+          sweep_now
+        rescue => e
+          Rails.logger.error "#{e.class}: #{e}\n#{e.backtrace.join("\n\t")}"
+        ensure
+          ActiveRecord::Base.connection.close
+        end
+      end
+    end
+  end
+end
index 07d46d86495c6cec831d0d3428fbda0f2a111c22..68cc76949afc5b21e0f7586fb777944788a9f6cd 100644 (file)
@@ -305,7 +305,7 @@ trashed_project:
   name: trashed project
   group_class: project
   trash_at: 2001-01-01T00:00:00Z
-  delete_at: 2038-03-01T00:00:00Z
+  delete_at: 2008-03-01T00:00:00Z
   is_trashed: true
   modified_at: 2001-01-01T00:00:00Z
 
@@ -325,4 +325,14 @@ trashed_subproject3:
   trash_at: 2001-01-01T00:00:00Z
   delete_at: 2038-03-01T00:00:00Z
   is_trashed: true
+  modified_at: 2001-01-01T00:00:00Z
+
+trashed_on_next_sweep:
+  uuid: zzzzz-j7d0g-soontobetrashed
+  owner_uuid: zzzzz-j7d0g-xurymjxw79nv3jz
+  name: soon to be trashed project
+  group_class: project
+  trash_at: 2001-01-01T00:00:00Z
+  delete_at: 2038-03-01T00:00:00Z
+  is_trashed: false
   modified_at: 2001-01-01T00:00:00Z
\ No newline at end of file
index 7b1fb90b56100b5a0319d62db1178330b777424a..140f3708398fb8735363c977b10dfe00996bf465 100644 (file)
@@ -415,6 +415,17 @@ job_in_subproject:
   state: Complete
   script_parameters_digest: 99914b932bd37a50b983c5e7c90ae93b
 
+job_in_trashed_project:
+  uuid: zzzzz-8i9sb-subprojectjob02
+  created_at: 2014-10-15 12:00:00
+  owner_uuid: zzzzz-j7d0g-trashedproject2
+  log: ~
+  repository: active/foo
+  script: hash
+  script_version: 4fe459abe02d9b365932b8f5dc419439ab4e2577
+  state: Complete
+  script_parameters_digest: 99914b932bd37a50b983c5e7c90ae93b
+
 running_will_be_completed:
   uuid: zzzzz-8i9sb-rshmckwoma9pjh8
   owner_uuid: zzzzz-j7d0g-v955i6s2oi1cbso
index d425bc63c0e2e24511b446669271a56c11f04c68..8b8c48fe1c865f23e73bdf2c00befbb27031724a 100644 (file)
@@ -3,7 +3,7 @@
 # SPDX-License-Identifier: AGPL-3.0
 
 require 'test_helper'
-require 'sweep_trashed_collections'
+require 'sweep_trashed_objects'
 
 class CollectionTest < ActiveSupport::TestCase
   include DbCurrentTime
@@ -556,7 +556,7 @@ class CollectionTest < ActiveSupport::TestCase
     assert_includes(coll_uuids, collections(:docker_image).uuid)
   end
 
-  test "move to trash in SweepTrashedCollections" do
+  test "move collections to trash in SweepTrashedObjects" do
     c = collections(:trashed_on_next_sweep)
     refute_empty Collection.where('uuid=? and is_trashed=false', c.uuid)
     assert_raises(ActiveRecord::RecordNotUnique) do
@@ -565,7 +565,7 @@ class CollectionTest < ActiveSupport::TestCase
                            name: c.name)
       end
     end
-    SweepTrashedCollections.sweep_now
+    SweepTrashedObjects.sweep_now
     c = Collection.where('uuid=? and is_trashed=true', c.uuid).first
     assert c
     act_as_user users(:active) do
@@ -574,14 +574,14 @@ class CollectionTest < ActiveSupport::TestCase
     end
   end
 
-  test "delete in SweepTrashedCollections" do
+  test "delete collections in SweepTrashedObjects" do
     uuid = 'zzzzz-4zz18-3u1p5umicfpqszp' # deleted_on_next_sweep
     assert_not_empty Collection.where(uuid: uuid)
-    SweepTrashedCollections.sweep_now
+    SweepTrashedObjects.sweep_now
     assert_empty Collection.where(uuid: uuid)
   end
 
-  test "delete referring links in SweepTrashedCollections" do
+  test "delete referring links in SweepTrashedObjects" do
     uuid = collections(:trashed_on_next_sweep).uuid
     act_as_system_user do
       Link.create!(head_uuid: uuid,
@@ -593,7 +593,7 @@ class CollectionTest < ActiveSupport::TestCase
     Collection.where(uuid: uuid).
       update_all(is_trashed: true, trash_at: past, delete_at: past)
     assert_not_empty Collection.where(uuid: uuid)
-    SweepTrashedCollections.sweep_now
+    SweepTrashedObjects.sweep_now
     assert_empty Collection.where(uuid: uuid)
   end
 end
index a5dc0ece840d9117c14f051ab583451d3f46fec0..8b3052e78595da57ff0b6cd487fb513fd8972166 100644 (file)
@@ -60,7 +60,7 @@ class GroupTest < ActiveSupport::TestCase
     assert g_foo.errors.messages[:owner_uuid].join(" ").match(/ownership cycle/)
   end
 
-  test "delete group hides contents" do
+  test "trash group hides contents" do
     set_user_from_auth :active_trustedclient
 
     g_foo = Group.create!(name: "foo")
@@ -74,7 +74,7 @@ class GroupTest < ActiveSupport::TestCase
     assert Collection.readable_by(users(:active)).where(uuid: col.uuid).any?
   end
 
-  test "delete group" do
+  test "trash group" do
     set_user_from_auth :active_trustedclient
 
     g_foo = Group.create!(name: "foo")
@@ -95,7 +95,7 @@ class GroupTest < ActiveSupport::TestCase
   end
 
 
-  test "delete subgroup" do
+  test "trash subgroup" do
     set_user_from_auth :active_trustedclient
 
     g_foo = Group.create!(name: "foo")
@@ -115,7 +115,7 @@ class GroupTest < ActiveSupport::TestCase
     assert Group.readable_by(users(:active), {:include_trash => true}).where(uuid: g_baz.uuid).any?
   end
 
-  test "delete subsubgroup" do
+  test "trash subsubgroup" do
     set_user_from_auth :active_trustedclient
 
     g_foo = Group.create!(name: "foo")
@@ -133,7 +133,7 @@ class GroupTest < ActiveSupport::TestCase
   end
 
 
-  test "delete group propagates to subgroups" do
+  test "trash group propagates to subgroups" do
     set_user_from_auth :active_trustedclient
 
     g_foo = groups(:trashed_project)
@@ -158,7 +158,7 @@ class GroupTest < ActiveSupport::TestCase
     assert Group.readable_by(users(:active)).where(uuid: g_bar.uuid).any?
     assert Collection.readable_by(users(:active)).where(uuid: col.uuid).any?
 
-    # this one should still be deleted.
+    # this one should still be trashed.
     assert Group.readable_by(users(:active)).where(uuid: g_baz.uuid).empty?
 
     g_baz.update! is_trashed: false
@@ -189,4 +189,47 @@ class GroupTest < ActiveSupport::TestCase
     assert User.readable_by(users(:admin)).where(uuid:  u_bar.uuid).any?
   end
 
+  test "move projects to trash in SweepTrashedObjects" do
+    p = groups(:trashed_on_next_sweep)
+    assert_empty Group.where('uuid=? and is_trashed=true', p.uuid)
+    SweepTrashedObjects.sweep_now
+    assert_not_empty Group.where('uuid=? and is_trashed=true', p.uuid)
+  end
+
+  test "delete projects and their contents in SweepTrashedObjects" do
+    g_foo = groups(:trashed_project)
+    g_bar = groups(:trashed_subproject)
+    g_baz = groups(:trashed_subproject3)
+    col = collections(:collection_in_trashed_subproject)
+    job = jobs(:job_in_trashed_project)
+    cr = container_requests(:cr_in_trashed_project)
+    # Save how many objects were before the sweep
+    user_nr_was = User.all.length
+    coll_nr_was = Collection.all.length
+    group_nr_was = Group.where('group_class<>?', 'project').length
+    project_nr_was = Group.where(group_class: 'project').length
+    cr_nr_was = ContainerRequest.all.length
+    job_nr_was = Job.all.length
+    assert_not_empty Group.where(uuid: g_foo.uuid)
+    assert_not_empty Group.where(uuid: g_bar.uuid)
+    assert_not_empty Group.where(uuid: g_baz.uuid)
+    assert_not_empty Collection.where(uuid: col.uuid)
+    assert_not_empty Job.where(uuid: job.uuid)
+    assert_not_empty ContainerRequest.where(uuid: cr.uuid)
+    SweepTrashedObjects.sweep_now
+    assert_empty Group.where(uuid: g_foo.uuid)
+    assert_empty Group.where(uuid: g_bar.uuid)
+    assert_empty Group.where(uuid: g_baz.uuid)
+    assert_empty Collection.where(uuid: col.uuid)
+    assert_empty Job.where(uuid: job.uuid)
+    assert_empty ContainerRequest.where(uuid: cr.uuid)
+    # No unwanted deletions should have happened
+    assert_equal user_nr_was, User.all.length
+    assert_equal coll_nr_was-2,        # collection_in_trashed_subproject
+                 Collection.all.length # & deleted_on_next_sweep collections
+    assert_equal group_nr_was, Group.where('group_class<>?', 'project').length
+    assert_equal project_nr_was-3, Group.where(group_class: 'project').length
+    assert_equal cr_nr_was-1, ContainerRequest.all.length
+    assert_equal job_nr_was-1, Job.all.length
+  end
 end