Class: Puppet::Environments::Cached Private
- Includes:
- Concurrent::Synchronized, EnvironmentLoader
- Defined in:
- lib/puppet/environments.rb
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Defined Under Namespace
Classes: DefaultCacheExpirationService, Entry, MRUEntry, NotCachedEntry, TTLEntry
Constant Summary collapse
- END_OF_TIME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
end_of_time- START_OF_TIME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Time.gm(1)
Class Method Summary collapse
- .cache_expiration_service ⇒ Object private
- .cache_expiration_service=(service) ⇒ Object private
-
.end_of_time ⇒ Object
private
Returns the end of time (the next Mesoamerican Long Count cycle-end after 2012 (5125+2012) = 7137.
Instance Method Summary collapse
-
#clear(name) ⇒ Object
private
Clears the cache of the environment with the given name.
-
#clear_all ⇒ Object
private
Clears all cached environments.
-
#clear_all_expired ⇒ Object
private
Clears all environments that have expired, either by exceeding their time to live, or through an explicit eviction determined by the cache expiration service.
-
#entry(env) ⇒ Object
private
Creates a suitable cache entry given the time to live for one environment.
-
#evict_if_expired(name) ⇒ Object
private
Evicts the entry if it has expired Also clears caches in Settings that may prevent the entry from being updated.
-
#get(name) ⇒ Puppet::Node::Environment?
private
Find a named environment.
-
#get_conf(name) ⇒ Puppet::Setting::EnvironmentConf?
private
This implementation evicts the cache, and always gets the current configuration of the environment.
-
#initialize(loader) ⇒ Cached
constructor
private
A new instance of Cached.
-
#list ⇒ Array<Puppet::Node::Environment>
private
All of the environments known to the loader.
-
#search_paths ⇒ Array<String>
private
A list of indicators of where the loader is getting its environments from.
Methods included from EnvironmentLoader
Constructor Details
#initialize(loader) ⇒ Cached
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Cached.
345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/puppet/environments.rb', line 345 def initialize(loader) @loader = loader @cache_expiration_service = Puppet::Environments::Cached.cache_expiration_service @cache = {} # Holds expiration times in sorted order - next to expire is first @expirations = SortedSet.new # Infinity since it there are no entries, this is a cache of the first to expire time @next_expiration = END_OF_TIME end |
Class Method Details
.cache_expiration_service ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
333 334 335 |
# File 'lib/puppet/environments.rb', line 333 def self.cache_expiration_service @cache_expiration_service || DefaultCacheExpirationService.new end |
.cache_expiration_service=(service) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
329 330 331 |
# File 'lib/puppet/environments.rb', line 329 def self.cache_expiration_service=(service) @cache_expiration_service = service end |
.end_of_time ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns the end of time (the next Mesoamerican Long Count cycle-end after 2012 (5125+2012) = 7137
338 339 340 |
# File 'lib/puppet/environments.rb', line 338 def self.end_of_time Time.gm(7137) end |
Instance Method Details
#clear(name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Clears the cache of the environment with the given name. (The intention is that this could be used from a MANUAL cache eviction command (TBD)
402 403 404 405 |
# File 'lib/puppet/environments.rb', line 402 def clear(name) @cache.delete(name) Puppet::GettextConfig.delete_text_domain(name) end |
#clear_all ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Clears all cached environments. (The intention is that this could be used from a MANUAL cache eviction command (TBD)
409 410 411 412 413 414 415 |
# File 'lib/puppet/environments.rb', line 409 def clear_all() super @cache = {} @expirations.clear @next_expiration = END_OF_TIME Puppet::GettextConfig.delete_environment_text_domains end |
#clear_all_expired ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Clears all environments that have expired, either by exceeding their time to live, or through an explicit eviction determined by the cache expiration service.
420 421 422 423 424 425 426 427 428 429 430 431 432 |
# File 'lib/puppet/environments.rb', line 420 def clear_all_expired() t = Time.now return if t < @next_expiration && ! @cache.any? {|name, _| @cache_expiration_service.expired?(name.to_sym) } to_expire = @cache.select { |name, entry| entry.expires < t || @cache_expiration_service.expired?(name.to_sym) } to_expire.each do |name, entry| Puppet.debug {"Evicting cache entry for environment '#{name}'"} @cache_expiration_service.evicted(name.to_sym) clear(name) @expirations.delete(entry.expires) Puppet.settings.clear_environment_settings(name) end @next_expiration = @expirations.first || END_OF_TIME end |
#entry(env) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a suitable cache entry given the time to live for one environment
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/puppet/environments.rb', line 449 def entry(env) ttl = if (conf = get_conf(env.name)) conf.environment_timeout else Puppet[:environment_timeout] end case ttl when 0 NotCachedEntry.new(env) # Entry that is always expired (avoids syscall to get time) when Float::INFINITY Entry.new(env) # Entry that never expires (avoids syscall to get time) else if Puppet[:environment_timeout_mode] == :from_last_used MRUEntry.new(env, ttl) # Entry that expires in ttl from when it was last touched else TTLEntry.new(env, ttl) # Entry that expires in ttl from when it was created end end end |
#evict_if_expired(name) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Evicts the entry if it has expired Also clears caches in Settings that may prevent the entry from being updated
472 473 474 475 476 477 478 479 |
# File 'lib/puppet/environments.rb', line 472 def evict_if_expired(name) if (result = @cache[name]) && (result.expired? || @cache_expiration_service.expired?(name.to_sym)) Puppet.debug {"Evicting cache entry for environment '#{name}'"} @cache_expiration_service.evicted(name.to_sym) clear(name) Puppet.settings.clear_environment_settings(name) end end |
#get(name) ⇒ Puppet::Node::Environment?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Find a named environment
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/puppet/environments.rb', line 368 def get(name) # Aggressively evict all that has expired # This strategy favors smaller memory footprint over environment # retrieval time. clear_all_expired result = @cache[name] if result Puppet.debug {"Found in cache '#{name}' #{result.label}"} # found in cache result.touch return result.value elsif (result = @loader.get(name)) # environment loaded, cache it cache_entry = entry(result) @cache_expiration_service.created(result) add_entry(name, cache_entry) result end end |
#get_conf(name) ⇒ Puppet::Setting::EnvironmentConf?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This implementation evicts the cache, and always gets the current configuration of the environment
TODO: While this is wasteful since it needs to go on a search for the conf, it is too disruptive to optimize this.
Attempt to obtain the initial configuration for the environment. Not all loaders can provide this.
442 443 444 445 |
# File 'lib/puppet/environments.rb', line 442 def get_conf(name) evict_if_expired(name) @loader.get_conf(name) end |
#list ⇒ Array<Puppet::Node::Environment>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns All of the environments known to the loader.
358 359 360 |
# File 'lib/puppet/environments.rb', line 358 def list @loader.list end |
#search_paths ⇒ Array<String>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A list of indicators of where the loader is getting its environments from.
363 364 365 |
# File 'lib/puppet/environments.rb', line 363 def search_paths @loader.search_paths end |