summaryrefslogtreecommitdiff
path: root/app/workers/database/batched_background_migration/single_database_worker.rb
blob: e772216e557f87379ae4f4d8f9240a8552b13bb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# frozen_string_literal: true

module Database
  module BatchedBackgroundMigration
    module SingleDatabaseWorker
      extend ActiveSupport::Concern

      include ApplicationWorker
      include CronjobQueue # rubocop:disable Scalability/CronWorkerContext
      include Gitlab::Utils::StrongMemoize

      LEASE_TIMEOUT_MULTIPLIER = 3
      MINIMUM_LEASE_TIMEOUT = 10.minutes.freeze
      INTERVAL_VARIANCE = 5.seconds.freeze

      included do
        data_consistency :always
        feature_category :database
        idempotent!
      end

      class_methods do
        # :nocov:
        def tracking_database
          raise NotImplementedError, "#{self.name} does not implement #{__method__}"
        end
        # :nocov:

        def enabled?
          Feature.enabled?(:execute_batched_migrations_on_schedule, type: :ops)
        end

        def lease_key
          name.demodulize.underscore
        end
      end

      def perform
        unless base_model
          Sidekiq.logger.info(
            class: self.class.name,
            database: tracking_database,
            message: 'skipping migration execution for unconfigured database')

          return
        end

        if shares_db_config?
          Sidekiq.logger.info(
            class: self.class.name,
            database: tracking_database,
            message: 'skipping migration execution for database that shares database configuration with another database')

          return
        end

        Gitlab::Database::SharedModel.using_connection(base_model.connection) do
          break unless self.class.enabled?

          if parallel_execution_enabled?
            migrations = Gitlab::Database::BackgroundMigration::BatchedMigration
              .active_migrations_distinct_on_table(connection: base_model.connection, limit: max_running_migrations).to_a

            queue_migrations_for_execution(migrations) if migrations.any?
          else
            break unless active_migration

            with_exclusive_lease(active_migration.interval) do
              run_active_migration
            end
          end
        end
      end

      private

      def parallel_execution_enabled?
        Feature.enabled?(:batched_migrations_parallel_execution)
      end

      def max_running_migrations
        execution_worker_class.max_running_jobs
      end

      def active_migration
        @active_migration ||= Gitlab::Database::BackgroundMigration::BatchedMigration.active_migration(connection: base_model.connection)
      end

      def run_active_migration
        execution_worker_class.new.perform_work(tracking_database, active_migration.id)
      end

      def tracking_database
        self.class.tracking_database
      end

      def queue_migrations_for_execution(migrations)
        jobs_arguments = migrations.map { |migration| [tracking_database.to_s, migration.id] }

        execution_worker_class.perform_with_capacity(jobs_arguments)
      end

      def base_model
        strong_memoize(:base_model) do
          Gitlab::Database.database_base_models[tracking_database]
        end
      end

      def shares_db_config?
        base_model && Gitlab::Database.db_config_share_with(base_model.connection_db_config).present?
      end

      def with_exclusive_lease(interval)
        timeout = [interval * LEASE_TIMEOUT_MULTIPLIER, MINIMUM_LEASE_TIMEOUT].max
        lease = Gitlab::ExclusiveLease.new(self.class.lease_key, timeout: timeout)

        yield if lease.try_obtain
      ensure
        lease&.cancel
      end
    end
  end
end