2018-07-19 11:16:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Postgresql
|
2019-03-28 09:17:42 -04:00
|
|
|
class ReplicationSlot < ApplicationRecord
|
2018-07-19 11:16:47 -04:00
|
|
|
self.table_name = 'pg_replication_slots'
|
|
|
|
|
2018-11-03 02:31:37 -04:00
|
|
|
# Returns true if there are any replication slots in use.
|
|
|
|
# PostgreSQL-compatible databases such as Aurora don't support
|
|
|
|
# replication slots, so this will return false as well.
|
|
|
|
def self.in_use?
|
|
|
|
transaction { exists? }
|
|
|
|
rescue ActiveRecord::StatementInvalid
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2018-07-19 11:16:47 -04:00
|
|
|
# Returns true if the lag observed across all replication slots exceeds a
|
|
|
|
# given threshold.
|
|
|
|
#
|
|
|
|
# max - The maximum replication lag size, in bytes. Based on GitLab.com
|
|
|
|
# statistics it takes between 1 and 5 seconds to replicate around
|
|
|
|
# 100 MB of data.
|
|
|
|
def self.lag_too_great?(max = 100.megabytes)
|
2018-11-03 02:31:37 -04:00
|
|
|
return false unless in_use?
|
|
|
|
|
2018-07-19 11:16:47 -04:00
|
|
|
lag_function = "#{Gitlab::Database.pg_wal_lsn_diff}" \
|
|
|
|
"(#{Gitlab::Database.pg_current_wal_insert_lsn}(), restart_lsn)::bigint"
|
|
|
|
|
|
|
|
# We force the use of a transaction here so the query always goes to the
|
|
|
|
# primary, even when using the EE DB load balancer.
|
|
|
|
sizes = transaction { pluck(lag_function) }
|
2019-06-25 11:33:18 -04:00
|
|
|
too_great = sizes.compact.count { |size| size >= max }
|
2018-07-19 11:16:47 -04:00
|
|
|
|
|
|
|
# If too many replicas are falling behind too much, the availability of a
|
|
|
|
# GitLab instance might suffer. To prevent this from happening we require
|
|
|
|
# at least 1 replica to have data recent enough.
|
|
|
|
if sizes.any? && too_great.positive?
|
|
|
|
(sizes.length - too_great) <= 1
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|