pool_repository.rb 2.64 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5
# The PoolRepository model is the database equivalent of an ObjectPool for Gitaly
# That is; PoolRepository is the record in the database, ObjectPool is the
# repository on disk
6
class PoolRepository < ActiveRecord::Base
7
  include Shardable
8 9 10 11
  include AfterCommitQueue

  has_one :source_project, class_name: 'Project'
  validates :source_project, presence: true
12

13
  has_many :member_projects, class_name: 'Project'
14

15
  after_create :correct_disk_path
16

17 18 19 20
  state_machine :state, initial: :none do
    state :scheduled
    state :ready
    state :failed
21
    state :obsolete
22 23 24 25 26 27 28 29 30 31 32 33 34

    event :schedule do
      transition none: :scheduled
    end

    event :mark_ready do
      transition [:scheduled, :failed] => :ready
    end

    event :mark_failed do
      transition all => :failed
    end

35 36 37 38
    event :mark_obsolete do
      transition all => :obsolete
    end

39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    state all - [:ready] do
      def joinable?
        false
      end
    end

    state :ready do
      def joinable?
        true
      end
    end

    after_transition none: :scheduled do |pool, _|
      pool.run_after_commit do
        ::ObjectPool::CreateWorker.perform_async(pool.id)
      end
    end

    after_transition scheduled: :ready do |pool, _|
      pool.run_after_commit do
        ::ObjectPool::ScheduleJoinWorker.perform_async(pool.id)
      end
    end
62 63 64 65 66 67

    after_transition any => :obsolete do |pool, _|
      pool.run_after_commit do
        ::ObjectPool::DestroyWorker.perform_async(pool.id)
      end
    end
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  end

  def create_object_pool
    object_pool.create
  end

  # The members of the pool should have fetched the missing objects to their own
  # objects directory. If the caller fails to do so, data loss might occur
  def delete_object_pool
    object_pool.delete
  end

  def link_repository(repository)
    object_pool.link(repository.raw)
  end

  # This RPC can cause data loss, as not all objects are present the local repository
85
  def unlink_repository(repository)
86
    object_pool.unlink_repository(repository.raw)
87

88 89 90 91 92
    if member_projects.where.not(id: repository.project.id).exists?
      true
    else
      mark_obsolete
    end
93 94 95 96 97 98
  end

  def object_pool
    @object_pool ||= Gitlab::Git::ObjectPool.new(
      shard.name,
      disk_path + '.git',
99
      source_project.repository.raw,
100 101
      source_project.full_path
    )
102 103
  end

104 105 106 107
  def inspect
    "#<#{self.class.name} id:#{id} state:#{state} disk_path:#{disk_path} source_project: #{source_project.full_path}>"
  end

108 109 110
  private

  def correct_disk_path
111
    update!(disk_path: storage.disk_path)
112 113 114 115 116 117
  end

  def storage
    Storage::HashedProject
      .new(self, prefix: Storage::HashedProject::POOL_PATH_PREFIX)
  end
118
end