summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-11-11 13:12:21 -0800
committerLuke Kanies <luke@madstop.com>2008-11-11 13:12:21 -0800
commit0ecbf79faf8e69efda96f8611837cd1c395f7a7c (patch)
tree4fe918d384da032b797986c49f008548ce4e4795
parent29b97943e7efaad3cb3f8e7b82004c067d3fbf82 (diff)
downloadpuppet-0ecbf79faf8e69efda96f8611837cd1c395f7a7c.tar.gz
puppet-0ecbf79faf8e69efda96f8611837cd1c395f7a7c.tar.xz
puppet-0ecbf79faf8e69efda96f8611837cd1c395f7a7c.zip
Adding cached attribute support to resources.
The Catalog is the expirer, which means that a resource with no catalog will not cache data. Also switching files to use a cached attribute for its stat. And modifying catalogs to expire data at the end of every transaction. Signed-off-by: Luke Kanies <luke@madstop.com>
-rw-r--r--lib/puppet/node/catalog.rb6
-rw-r--r--lib/puppet/type.rb8
-rw-r--r--lib/puppet/type/file.rb20
-rwxr-xr-xspec/unit/node/catalog.rb10
-rwxr-xr-xspec/unit/type.rb11
-rwxr-xr-xspec/unit/type/file.rb13
6 files changed, 46 insertions, 22 deletions
diff --git a/lib/puppet/node/catalog.rb b/lib/puppet/node/catalog.rb
index 8862d0cc9..a438a4764 100644
--- a/lib/puppet/node/catalog.rb
+++ b/lib/puppet/node/catalog.rb
@@ -2,6 +2,8 @@ require 'puppet/indirector'
require 'puppet/simple_graph'
require 'puppet/transaction'
+require 'puppet/util/cacher'
+
require 'puppet/util/tagging'
# This class models a node catalog. It is the thing
@@ -15,6 +17,7 @@ class Puppet::Node::Catalog < Puppet::SimpleGraph
indirects :catalog, :terminus_class => :compiler
include Puppet::Util::Tagging
+ include Puppet::Util::Cacher::Expirer
# The host name this is a catalog for.
attr_accessor :name
@@ -394,6 +397,9 @@ class Puppet::Node::Catalog < Puppet::SimpleGraph
@transient_resources.clear
@relationship_graph = nil
end
+
+ # Expire any cached data the resources are keeping.
+ expire()
end
# Verify that the given resource isn't defined elsewhere.
diff --git a/lib/puppet/type.rb b/lib/puppet/type.rb
index da274df88..1854c8801 100644
--- a/lib/puppet/type.rb
+++ b/lib/puppet/type.rb
@@ -10,6 +10,7 @@ require 'puppet/util/errors'
require 'puppet/util/log_paths'
require 'puppet/util/logging'
require 'puppet/resource_reference'
+require 'puppet/util/cacher'
# see the bottom of the file for the rest of the inclusions
@@ -19,6 +20,7 @@ class Type
include Puppet::Util::Errors
include Puppet::Util::LogPaths
include Puppet::Util::Logging
+ include Puppet::Util::Cacher
###############################
# Code related to resource type attributes.
@@ -551,6 +553,12 @@ class Type
}
end
+ # Let the catalog determine whether a given cached value is
+ # still valid or has expired.
+ def expirer
+ catalog
+ end
+
# retrieve the 'should' value for a specified property
def should(name)
name = attr_alias(name)
diff --git a/lib/puppet/type/file.rb b/lib/puppet/type/file.rb
index ccef61253..6eb7f62f5 100644
--- a/lib/puppet/type/file.rb
+++ b/lib/puppet/type/file.rb
@@ -734,7 +734,7 @@ module Puppet
# use either 'stat' or 'lstat', and we expect the properties to use the
# resulting stat object accordingly (mostly by testing the 'ftype'
# value).
- def stat(refresh = false)
+ cached_attr(:stat) do
method = :stat
# Files are the only types that support links
@@ -743,18 +743,14 @@ module Puppet
end
path = self[:path]
- if @stat.nil? or refresh == true
- begin
- @stat = File.send(method, self[:path])
- rescue Errno::ENOENT => error
- return nil
- rescue Errno::EACCES => error
- warning "Could not stat; permission denied"
- return nil
- end
+ begin
+ File.send(method, self[:path])
+ rescue Errno::ENOENT => error
+ return nil
+ rescue Errno::EACCES => error
+ warning "Could not stat; permission denied"
+ return nil
end
-
- return @stat
end
# We have to hack this just a little bit, because otherwise we'll get
diff --git a/spec/unit/node/catalog.rb b/spec/unit/node/catalog.rb
index 28d66644a..3c4da5908 100755
--- a/spec/unit/node/catalog.rb
+++ b/spec/unit/node/catalog.rb
@@ -611,8 +611,11 @@ describe Puppet::Node::Catalog do
@transaction.stubs(:addtimes)
end
- describe "when applying" do
+ it "should be an Expirer" do
+ Puppet::Node::Catalog.ancestors.should be_include(Puppet::Util::Cacher::Expirer)
+ end
+ describe "when applying" do
it "should create and evaluate a transaction" do
@transaction.expects(:evaluate)
@catalog.apply
@@ -685,6 +688,11 @@ describe Puppet::Node::Catalog do
end
@catalog.resource("File[/yay]").should be_nil
end
+
+ it "should expire cached data in the resources" do
+ @catalog.expects(:expire)
+ @catalog.apply
+ end
end
describe "when applying host catalogs" do
diff --git a/spec/unit/type.rb b/spec/unit/type.rb
index b07f781ed..6f1052516 100755
--- a/spec/unit/type.rb
+++ b/spec/unit/type.rb
@@ -3,6 +3,17 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe Puppet::Type do
+ it "should include the Cacher module" do
+ Puppet::Type.ancestors.should be_include(Puppet::Util::Cacher)
+ end
+
+ it "should use its catalog as its expirer" do
+ catalog = Puppet::Node::Catalog.new
+ resource = Puppet::Type.type(:mount).create(:name => "foo", :fstype => "bar", :pass => 1, :ensure => :present)
+ resource.catalog = catalog
+ resource.expirer.should equal(catalog)
+ end
+
it "should be able to retrieve a property by name" do
resource = Puppet::Type.type(:mount).create(:name => "foo", :fstype => "bar", :pass => 1, :ensure => :present)
resource.property(:fstype).must be_instance_of(Puppet::Type.type(:mount).attrclass(:fstype))
diff --git a/spec/unit/type/file.rb b/spec/unit/type/file.rb
index bb0fc10f3..6fd27d75b 100755
--- a/spec/unit/type/file.rb
+++ b/spec/unit/type/file.rb
@@ -164,19 +164,14 @@ describe Puppet::Type.type(:file) do
@resource.stat.should == "mystat"
end
- it "should cache the stat instance" do
+ it "should cache the stat instance if it has a catalog" do
stat = mock 'stat'
File.expects(:lstat).returns stat
- @resource.stat.should equal(@resource.stat)
- end
-
- it "should not cache nil stat values" do
- stat = mock 'stat'
- File.expects(:lstat).times(2).returns(nil).then.returns(stat)
+ catalog = Puppet::Node::Catalog.new
+ @resource.catalog = catalog
- @resource.stat.should be_nil
- @resource.stat.should equal(stat)
+ @resource.stat.should equal(@resource.stat)
end
end