Class: OsCtld::GarbageCollector
- Inherits:
-
Object
- Object
- OsCtld::GarbageCollector
- Includes:
- OsCtl::Lib::Utils::File, OsCtl::Lib::Utils::Log, Lockable
- Defined in:
- lib/osctld/garbage_collector.rb
Overview
Ensures that temporary datasets are moved to trash after they're not needed
It can be used to tie datasets to specific container runs. Add dataset using #add_container_run_dataset and it will be kept as long as the same run configuration is active. When the container is stopped, restarted, or #free_container_run_dataset is called, the dataset is moved to the trash bin.
Defined Under Namespace
Classes: ContainerRunDataset
Instance Attribute Summary collapse
- #pool ⇒ Pool readonly
Class Method Summary collapse
- .add_container_run_dataset(run_conf, dataset) ⇒ Object
- .free_container_run_dataset(run_conf, dataset) ⇒ Object
Instance Method Summary collapse
- #add_container_run_dataset(run_conf, dataset) ⇒ Object
- #assets(add) ⇒ Object
- #container_run_dataset_in_use?(ct_run_ds) ⇒ Boolean protected
-
#container_run_dataset_ownership(allow_missing: false) ⇒ Object
Import-only ownership evidence.
- #do_free_container_run_dataset(ct_run_ds) ⇒ Object protected
- #free_container_run_dataset(run_conf, dataset) ⇒ Object
-
#inhibit_retirement ⇒ Object
Called during pool import, before the worker starts, if container state cannot be authenticated.
-
#initialize(pool) ⇒ GarbageCollector
constructor
A new instance of GarbageCollector.
- #load_config ⇒ Object protected
- #log_type ⇒ Object
- #prune ⇒ Object
- #prune_container_run_datasets ⇒ Object protected
- #run_gc ⇒ Object protected
- #save_config ⇒ Object protected
- #start ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
-
#trash_dataset(dataset) ⇒ Boolean
protected
True if the dataset was moved to trash or if it doesn't exist.
Methods included from Lockable
#exclusively, included, #inclusively, #init_lock, #lock, #unlock
Constructor Details
#initialize(pool) ⇒ GarbageCollector
Returns a new instance of GarbageCollector.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/osctld/garbage_collector.rb', line 72 def initialize(pool) init_lock @pool = pool @config_path = File.join(pool.conf_path, 'pool', 'garbage-collector.yml') @queue = OsCtl::Lib::Queue.new @stop = false @retirement_inhibited = false load_config end |
Instance Attribute Details
#pool ⇒ Pool (readonly)
69 70 71 |
# File 'lib/osctld/garbage_collector.rb', line 69 def pool @pool end |
Class Method Details
.add_container_run_dataset(run_conf, dataset) ⇒ Object
54 55 56 |
# File 'lib/osctld/garbage_collector.rb', line 54 def self.add_container_run_dataset(run_conf, dataset) run_conf.pool.garbage_collector.add_container_run_dataset(run_conf, dataset) end |
.free_container_run_dataset(run_conf, dataset) ⇒ Object
60 61 62 |
# File 'lib/osctld/garbage_collector.rb', line 60 def self.free_container_run_dataset(run_conf, dataset) run_conf.pool.garbage_collector.free_container_run_dataset(run_conf, dataset) end |
Instance Method Details
#add_container_run_dataset(run_conf, dataset) ⇒ Object
162 163 164 165 166 167 |
# File 'lib/osctld/garbage_collector.rb', line 162 def add_container_run_dataset(run_conf, dataset) exclusively do @container_run_datasets << ContainerRunDataset.new(run_conf.run_id, dataset) save_config end end |
#assets(add) ⇒ Object
83 84 85 86 87 88 89 |
# File 'lib/osctld/garbage_collector.rb', line 83 def assets(add) add.file( @config_path, desc: 'Configuration file for garbage collector', optional: true ) end |
#container_run_dataset_in_use?(ct_run_ds) ⇒ Boolean (protected)
258 259 260 261 262 263 264 265 266 267 |
# File 'lib/osctld/garbage_collector.rb', line 258 def container_run_dataset_in_use?(ct_run_ds) ct = DB::Containers.find(ct_run_ds.container_id, ct_run_ds.pool_name) return false if ct.nil? run_conf = ct.run_conf next_run_conf = ct.next_run_conf (run_conf && run_conf.run_id == ct_run_ds.run_id) \ || (next_run_conf && next_run_conf.run_id == ct_run_ds.run_id) end |
#container_run_dataset_ownership(allow_missing: false) ⇒ Object
Import-only ownership evidence. Read outside the GC lock, then ensure the persisted records still agree with the in-memory view. Never create owners.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/osctld/garbage_collector.rb', line 123 def container_run_dataset_ownership(allow_missing: false) records = inclusively { @container_run_datasets.map(&:dump) } begin input = File.open(@config_path, File::RDONLY | File::NOFOLLOW) do |f| before = f.stat bytes = f.read after = f.stat before_identity = [before.dev, before.ino, before.size, before.mtime, before.ctime] after_identity = [after.dev, after.ino, after.size, after.mtime, after.ctime] raise ConfigError, 'GC ownership file changed during import' unless before_identity == after_identity [after_identity, bytes] end rescue Errno::ENOENT raise unless allow_missing && records.empty? begin File.lstat(@config_path) rescue Errno::ENOENT unless inclusively { @container_run_datasets.empty? } raise ConfigError, 'GC ownership changed during import' end return [records, nil] end raise ConfigError, 'GC ownership file appeared during import' end cfg = OsCtl::Lib::ConfigFile.load_yaml(input.last) unless cfg.is_a?(Hash) && cfg['container_run_datasets'] == records && inclusively { @container_run_datasets.map(&:dump) == records } raise ConfigError, 'GC ownership changed during import' end [records, input] end |
#do_free_container_run_dataset(ct_run_ds) ⇒ Object (protected)
244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/osctld/garbage_collector.rb', line 244 def do_free_container_run_dataset(ct_run_ds) # It may be that the dataset wasn't previously registered within the GC. # It could be a dataset that existed before GC support was introduced. In any # case, the intent is to delete the dataset and so we do not perform a lookup # in @container_run_datasets. log(:info, "Moving #{ct_run_ds.dataset} to trash") return unless trash_dataset(ct_run_ds.dataset) exclusively do @container_run_datasets.delete(ct_run_ds) save_config end end |
#free_container_run_dataset(run_conf, dataset) ⇒ Object
171 172 173 |
# File 'lib/osctld/garbage_collector.rb', line 171 def free_container_run_dataset(run_conf, dataset) @queue << [:free_container_run_dataset, ContainerRunDataset.new(run_conf.run_id, dataset)] end |
#inhibit_retirement ⇒ Object
Called during pool import, before the worker starts, if container state cannot be authenticated. Only a fresh collector can resume retirement. Refused frees without a registered record are not durable cleanup requests.
116 117 118 119 |
# File 'lib/osctld/garbage_collector.rb', line 116 def inhibit_retirement exclusively { @retirement_inhibited = true } nil end |
#load_config ⇒ Object (protected)
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/osctld/garbage_collector.rb', line 181 def load_config @container_run_datasets = [] begin cfg = OsCtl::Lib::ConfigFile.load_yaml_file(@config_path) rescue Errno::ENOENT return end @container_run_datasets = cfg.fetch('container_run_datasets', []).map do |ct_run_ds_cfg| ContainerRunDataset.load(ct_run_ds_cfg) end end |
#log_type ⇒ Object
175 176 177 |
# File 'lib/osctld/garbage_collector.rb', line 175 def log_type "#{pool.name}:gc" end |
#prune ⇒ Object
109 110 111 |
# File 'lib/osctld/garbage_collector.rb', line 109 def prune @queue << :prune end |
#prune_container_run_datasets ⇒ Object (protected)
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/osctld/garbage_collector.rb', line 223 def prune_container_run_datasets unused = inclusively do @container_run_datasets.reject do |ct_run_ds| container_run_dataset_in_use?(ct_run_ds) end end unused.each do |ct_run_ds| break if @stop log(:info, "Container dataset #{ct_run_ds.dataset} from run=#{ct_run_ds.run_id} is no longer in use, moving to trash") next unless trash_dataset(ct_run_ds.dataset) exclusively do @container_run_datasets.delete(ct_run_ds) save_config end end end |
#run_gc ⇒ Object (protected)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/osctld/garbage_collector.rb', line 205 def run_gc loop do v = @queue.pop(timeout: Daemon.get.config.garbage_collector.prune_interval) case v in :stop return in :prune | nil log(:info, 'Pruning container run datasets') prune_container_run_datasets in [:free_container_run_dataset, ct_run_ds] do_free_container_run_dataset(ct_run_ds) end end end |
#save_config ⇒ Object (protected)
195 196 197 198 199 200 201 202 203 |
# File 'lib/osctld/garbage_collector.rb', line 195 def save_config exclusively do regenerate_file(@config_path, 0o400) do |f| f.write(OsCtl::Lib::ConfigFile.dump_yaml({ 'container_run_datasets' => @container_run_datasets.map(&:dump) })) end end end |
#start ⇒ Object
91 92 93 94 |
# File 'lib/osctld/garbage_collector.rb', line 91 def start @stop = false @thread = Thread.new { run_gc } end |
#started? ⇒ Boolean
105 106 107 |
# File 'lib/osctld/garbage_collector.rb', line 105 def started? !@thread.nil? && @thread.alive? end |
#stop ⇒ Object
96 97 98 99 100 101 102 103 |
# File 'lib/osctld/garbage_collector.rb', line 96 def stop return unless @thread @stop = true @queue << :stop @thread.join @thread = nil end |
#trash_dataset(dataset) ⇒ Boolean (protected)
Returns true if the dataset was moved to trash or if it doesn't exist.
270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/osctld/garbage_collector.rb', line 270 def trash_dataset(dataset) return false if inclusively { @retirement_inhibited } pool.trash_bin.add_dataset(dataset) true rescue SystemCommandFailed => e if dataset.exist? log(:warn, "Unable to trash dataset '#{dataset}': #{e.}") false else log(:warn, "Attempted to trash a non-existent dataset '#{dataset}' (original error: #{e.})") true end end |