Class: Puppet::Environments::Cached Private

Inherits:
Object
  • Object
show all
Includes:
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, 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

Instance Method Summary collapse

Methods included from EnvironmentLoader

#get!

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.



312
313
314
315
316
317
318
319
320
321
322
# File 'lib/puppet/environments.rb', line 312

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_serviceObject

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.



295
296
297
# File 'lib/puppet/environments.rb', line 295

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.



291
292
293
# File 'lib/puppet/environments.rb', line 291

def self.cache_expiration_service=(service)
  @cache_expiration_service = service
end

.end_of_timeObject

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, of for a 32 bit machine using Ruby < 1.9.3, the year 2038.



301
302
303
304
305
306
307
# File 'lib/puppet/environments.rb', line 301

def self.end_of_time
  begin
    Time.gm(7137)
  rescue ArgumentError
    Time.gm(2038)
  end
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)



366
367
368
# File 'lib/puppet/environments.rb', line 366

def clear(name)
  @cache.delete(name)
end

#clear_allObject

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)



372
373
374
375
376
377
# File 'lib/puppet/environments.rb', line 372

def clear_all()
  super
  @cache = {}
  @expirations.clear
  @next_expiration = END_OF_TIME
end

#clear_all_expiredObject

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.



382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/puppet/environments.rb', line 382

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)
    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



411
412
413
414
415
416
417
418
419
420
421
# File 'lib/puppet/environments.rb', line 411

def entry(env)
  ttl = (conf = get_conf(env.name)) ? conf.environment_timeout : Puppet.settings.value(:environment_timeout)
  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
    TTLEntry.new(env, ttl)
  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



425
426
427
428
429
430
431
432
433
# File 'lib/puppet/environments.rb', line 425

def evict_if_expired(name)
  if (result = @cache[name]) && (result.expired? || @cache_expiration_service.expired?(name))
  Puppet.debug {"Evicting cache entry for environment '#{name}'"}
    @cache.delete(name)
    @cache_expiration_service.evicted(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

Parameters:

  • name (String, Symbol)

    The name of environment to find

Returns:



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/puppet/environments.rb', line 335

def get(name)
  # Aggressively evict all that has expired
  # This strategy favors smaller memory footprint over environment
  # retrieval time.
  clear_all_expired
  if result = @cache[name]
    # found in cache
    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.

Parameters:

  • name (String, Symbol)

    The name of the environment whose configuration we are looking up

Returns:

  • (Puppet::Setting::EnvironmentConf, nil)

    the configuration for the requested environment, or nil if not found or no configuration is available



404
405
406
407
# File 'lib/puppet/environments.rb', line 404

def get_conf(name)
  evict_if_expired(name)
  @loader.get_conf(name)
end

#listArray<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.

Returns:



325
326
327
# File 'lib/puppet/environments.rb', line 325

def list
  @loader.list
end

#search_pathsArray<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.

Returns:

  • (Array<String>)

    The URIs of the load locations



330
331
332
# File 'lib/puppet/environments.rb', line 330

def search_paths
  @loader.search_paths
end