Class: PartitionGardener::ArchiveRetention
- Inherits:
-
Object
- Object
- PartitionGardener::ArchiveRetention
- Includes:
- Naming
- Defined in:
- lib/partition_gardener/archive_retention.rb
Instance Method Summary collapse
- #apply!(dry_run: nil) ⇒ Object
-
#initialize(config, executor: nil, job_class_name: "PartitionGardener") ⇒ ArchiveRetention
constructor
A new instance of ArchiveRetention.
Methods included from Naming
current_partition_name, default_partition_name, future_partition_name, open_partition_name, rebalance_staging_partition_name
Constructor Details
#initialize(config, executor: nil, job_class_name: "PartitionGardener") ⇒ ArchiveRetention
Returns a new instance of ArchiveRetention.
5 6 7 8 9 |
# File 'lib/partition_gardener/archive_retention.rb', line 5 def initialize(config, executor: nil, job_class_name: "PartitionGardener") @config = config @executor = executor || Executor.for_config(config) @job_class_name = job_class_name end |
Instance Method Details
#apply!(dry_run: nil) ⇒ Object
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 |
# File 'lib/partition_gardener/archive_retention.rb', line 11 def apply!(dry_run: nil) retention_months = @config[:retention_months] return 0 unless retention_months apply_retention = if dry_run.nil? @config.fetch(:retention_apply, false) else !dry_run end strategy = Strategy.for(@config) return 0 unless strategy.is_a?(Strategy::DateRange) table_name = @config[:table_name] cutoff = DateCalendar.add_months(PartitionGardener.configuration.today, -retention_months) dropped = 0 managed_tail_names = managed_tail_name_set(strategy) Connection.attached_partitions(table_name).each do |partition| next if partition.default next if skip_retention_partition?(strategy, partition.name, managed_tail_names) bucket = strategy.archive_bucket_from_partition_name(partition.name) next unless bucket next if bucket >= DateCalendar.beginning_of_month(cutoff) if apply_retention @executor.detach_partition(table_name, partition.name, concurrently: detach_concurrently?) @executor.drop_table(partition.name) unless @config.fetch(:retention_keep_table, false) PartitionGardener.configuration.notify( "[PartitionGardener] Dropped archive partition #{partition.name} (retention #{retention_months} months)", context: { table_name: table_name, partition_name: partition.name, retention_months: retention_months, job: @job_class_name } ) dropped += 1 next end PartitionGardener.configuration.notify( "[PartitionGardener] Would drop archive partition #{partition.name} (bucket #{bucket}, retention #{retention_months} months)", context: { table_name: table_name, partition_name: partition.name, bucket: bucket, dry_run: true } ) dropped += 1 end dropped end |