From 261b99e3d2b78746f7c05c70179896bd7c39236f Mon Sep 17 00:00:00 2001 From: Jakub Narebski Date: Tue, 9 Feb 2010 14:22:13 +0100 Subject: [PATCH] gitweb: Use Cache::Cache compatibile (get, set) output caching This commit actually adds output caching to gitweb, as we have now minimal features required for it in GitwebCache::SimpleFileCache (a 'dumb' but fast file-based cache engine). To enable cache you need at least set $caching_enabled to true in gitweb config, and copy cache.pm from gitweb/ alongside gitweb.cgi - this is described in more detail in the new "Gitweb caching" section in gitweb/README Currently cache support related subroutines in cache.pm (which are outside GitwebCache::SimpleFileCache package) are not well separated from gitweb script itself; cache.pm lacks encapsulation. cache.pm assumes that there are href() subroutine and %actions variable, and that there exist $actions{$action} (where $action is parameter passed to cache_fetch), and it is a code reference (see also comments in t/t9503/test_cache_interface.pl). This is remaining artifact from the original patch by J.H. (which also had cache_fetch() subroutine). Gitweb itself uses directly only cache_fetch, to get page from cache or to generate page and save it to cache, and cache_stop, to be used in die_error subroutine, as currently error pages are not cached. The cache_fetch subroutine captures output (from STDOUT only, as STDERR is usually logged) using either ->push_layer()/->pop_layer() from PerlIO::Util submodule (if it is available), or by setting and restoring *STDOUT. Note that only the former could be tested reliably to be reliable in t9503 test! Enabling caching causes the following additional changes to gitweb output: * Disables content-type negotiation (choosing between 'text/html' mimetype and 'application/xhtml+xml') when caching, as there is no content-type negotiation done when retrieving page from cache. Use 'text/html' mimetype that can be used by all browsers. * Disable timing info (how much time it took to generate original page, and how many git commands it took), and in its place show when page was originally generated (in GMT / UTC timezone). Add basic tests of caching support to t9500-gitweb-standalone-no-errors test: set $caching_enabled to true and check for errors for first time run (generating cache) and second time run (retrieving from cache) for a single view - summary view for a project. If PerlIO::Util is available (see comments), test that cache_fetch behaves correctly, namely that it saves and restores action output in cache, and that it prints generated output or cached output. To be implemented (from original patch by J.H.): * adaptive cache expiration, based on average system load * optional locking interface, where only one process can update cache (using flock) * server-side progress indicator when waiting for filling cache, which in turn requires separating situations (like snapshots and other non-HTML responses) where we should not show 'please wait' message Inspired-by-code-by: John 'Warthog9' Hawley Signed-off-by: Jakub Narebski --- gitweb/README | 70 ++++++++++++++++++++++ gitweb/cache.pm | 78 ++++++++++++++++++++++++ gitweb/gitweb.perl | 106 +++++++++++++++++++++++++++++---- t/gitweb-lib.sh | 2 + t/t9500-gitweb-standalone-no-errors.sh | 19 ++++++ t/t9503/test_cache_interface.pl | 93 +++++++++++++++++++++++++++++ 6 files changed, 355 insertions(+), 13 deletions(-) diff --git a/gitweb/README b/gitweb/README index 6c2c8e1259..53759fcc1e 100644 --- a/gitweb/README +++ b/gitweb/README @@ -233,6 +233,11 @@ not include variables usually directly set during build): If server load exceed this value then return "503 Service Unavaliable" error. Server load is taken to be 0 if gitweb cannot determine its value. Set it to undefined value to turn it off. The default is 300. + * $caching_enabled + If true, gitweb would use caching to speed up generating response. + Currently supported is only output (response) caching. See "Gitweb caching" + section below for details on how to configure and customize caching. + The default is false (caching is disabled). Projects list file format @@ -305,6 +310,71 @@ You can use the following files in repository: descriptions. +Gitweb caching +~~~~~~~~~~~~~~ + +Currently gitweb supports only output (HTTP response) caching, similar +to the one used on git.kernel.org. To turn it on, set $caching_enabled +variable to true value in gitweb config file, i.e.: + + our $caching_enabled = 1; + +You can choose what caching engine should gitweb use by setting $cache +variable either to _initialized_ instance of cache interface, e.g.: + + use CHI; + our $cache = CHI->new( driver => 'Memcached', + servers => [ "10.0.0.15:11211", "10.0.0.15:11212" ], + l1_cache => { driver => 'FastMmap', root_dir => '/var/cache/gitweb' } + ); + +Alternatively you can set $cache variable to the name of cache class, in +which case caching engine should support Cache::Cache or CHI names for cache +config (see below), and ignore unrecognized options, e.g.: + + use Cache::FileCache; + our $cache = 'Cache::FileCache'; + +Such caching engine should implement (at least) ->get($key) and +->set($key, $data) methods (Cache::Cache and CHI compatible interface). + +If $cache is left unset (if it is left undefined), then gitweb would use +GitwebCache::SimpleFileCache from cache.pm as caching engine. This engine +is 'dumb' (but fast) file based caching layer, currently without any support +for cache size limiting, or even removing expired / grossly expired entries. +It has therefore the downside of requiring a huge amount of disk space if +there are a number of repositories involved. It is not uncommon for +git.kernel.org to have on the order of 80G - 120G accumulate over the course +of a few months. It is therefore recommended that the cache directory be +periodically completely deleted; this operation is safe to perform. +Suggested mechanism (substitute $cachedir for actual path to gitweb cache): + + # mv $cachedir $cachedir.flush && mkdir $cachedir && rm -rf $cachedir.flush + +For gitweb to use caching it must find 'cache.pm' file, which contains +GitwebCache::SimpleFileCache and cache-related subroutines, from which +cache_fetch and cache_stop are used in gitweb itself. Location of +'cache.pm' file is provided in $cache_pm variable; if it is relative path, +it is relative to the directory gitweb is run from. Default value of +$cache_pm assumes that 'cache.pm' is copied to the same directory as +'gitweb.cgi'. + +Currently 'cache.pm' is not a proper Perl module, because it is not +encapsulated / it is not separated from details of gitweb. That is why it +is sourced using 'do "$cache_pm"', rather than with "use" or "require" +operators. + +Site-wide cache options are defined in %cache_options hash. Those options +apply only when $cache is unset (GitwebCache::SimpleFileCache is used), or +if $cache is name of cache class (e.g. $cache = 'Cache::FileCache'). You +can override cache options in gitweb config, e.g.: + + $cache_options{'expires_in'} = 60; # 60 seconds = 1 minute + +Please read comments for %cache_options entries in gitweb/gitweb.perl for +description of available cache options. + + Webserver configuration ----------------------- diff --git a/gitweb/cache.pm b/gitweb/cache.pm index b59509f68a..64c333bc21 100644 --- a/gitweb/cache.pm +++ b/gitweb/cache.pm @@ -296,4 +296,82 @@ sub compute { 1; } # end of package GitwebCache::SimpleFileCache; +# human readable key identifying gitweb output +sub gitweb_output_key { + return href(-replay => 1, -full => 1, -path_info => 0); +} + + +our $perlio_util = eval { require PerlIO::Util; 1 }; +our $STDOUT = *STDOUT; #our $STDOUTref = \*STDOUT; +our $data_fh; + +# Start caching data printed to STDOUT +sub cache_start { + my $data_ref = shift; + + if ($perlio_util) { + *STDOUT->push_layer(scalar => $data_ref); + + } else { + open $data_fh, '>', $data_ref + or die "Can't open memory file: $!"; + # matches "binmode STDOUT, ':uft8'" at beginning + binmode $data_fh, ':utf8'; + *STDOUT = $data_fh; + + } +} + +# Stop caching data (required for die_error) +sub cache_stop { + + if ($perlio_util) { + *STDOUT->pop_layer() + if ((*STDOUT->get_layers())[-1] eq 'scalar'); + + } else { + close $data_fh + or die "Error closing memory file: $!"; + *STDOUT = $STDOUT; + + } +} + +# Wrap caching data; capture only STDOUT +sub cache_capture (&) { + my $code = shift; + my $data; + + cache_start(\$data); + $code->(); + cache_stop(); + + return $data; +} + +sub cache_fetch { + my ($cache, $action) = @_; + + my $key = gitweb_output_key(); + my $data = $cache->get($key); + + if (defined $data) { + # print cached data + binmode STDOUT, ':raw'; + print STDOUT $data; + + } else { + $data = cache_capture { + $actions{$action}->(); + }; + + if (defined $data) { + $cache->set($key, $data); + binmode STDOUT, ':raw'; + print STDOUT $data; + } + } +} + 1; diff --git a/gitweb/gitweb.perl b/gitweb/gitweb.perl index 97ea3ece4b..f02ead941c 100755 --- a/gitweb/gitweb.perl +++ b/gitweb/gitweb.perl @@ -227,6 +227,44 @@ our %avatar_size = ( # Leave it undefined (or set to 'undef') to turn off load checking. our $maxload = 300; +# This enables/disables the caching layer in gitweb. Currently supported +# is only output (response) caching, similar to the one used on git.kernel.org. +our $caching_enabled = 0; +# Set to _initialized_ instance of cache interface implementing (at least) +# get($key) and set($key, $data) methods (Cache::Cache and CHI interfaces). +# If unset, GitwebCache::SimpleFileCache would be used, which is 'dumb' +# (but fast) file based caching layer, currently without any support for +# cache size limiting. It is therefore recommended that the cache directory +# be periodically completely deleted; this operation is safe to perform. +# Suggested mechanism: +# mv $cachedir $cachedir.flush && mkdir $cachedir && rm -rf $cachedir.flush +our $cache; +# Locations of 'cache.pm' file; if it is relative path, it is relative to +# the directory gitweb is run from +our $cache_pm = 'cache.pm'; +# You define site-wide cache options defaults here; override them with +# $GITWEB_CONFIG as necessary. +our %cache_options = ( + # The location in the filesystem that will hold the root of the cache. + # This directory will be created as needed (if possible) on the first + # cache set. Note that either this directory must exists and web server + # has to have write permissions to it, or web server must be able to + # create this directory. + # Possible values: + # * 'cache' (relative to gitweb), + # * File::Spec->catdir(File::Spec->tmpdir(), 'gitweb-cache'), + # * '/var/cache/gitweb' (FHS compliant, requires being set up), + 'cache_root' => 'cache', + # The number of subdirectories deep to cache object item. This should be + # large enough that no cache directory has more than a few hundred + # objects. Each non-leaf directory contains up to 256 subdirectories + # (00-ff). Must be larger than 0. + 'cache_depth' => 1, + # The (global) expiration time for objects placed in the cache, in seconds. + 'expires_in' => 20, +); + + # You define site-wide feature defaults here; override them with # $GITWEB_CONFIG as necessary. our %feature = ( @@ -964,7 +1002,34 @@ if ($action !~ m/^(?:opml|project_list|project_index)$/ && !$project) { die_error(400, "Project needed"); } -$actions{$action}->(); + +if ($caching_enabled) { + die_error(500, 'Caching enabled and "'.esc_path($cache_pm).'" not found') + unless -f $cache_pm; + do $cache_pm; + die $@ if $@; + + # $cache might be initialized (instantiated) cache, i.e. cache object, + # or it might be name of class, or it might be undefined + unless (defined $cache && ref($cache)) { + $cache ||= 'GitwebCache::SimpleFileCache'; + $cache = $cache->new({ + %cache_options, + #'cache_root' => '/tmp/cache', + #'cache_depth' => 2, + #'expires_in' => 20, # in seconds (CHI compatibile) + # (Cache::Cache compatibile initialization) + 'default_expires_in' => $cache_options{'expires_in'}, + # (CHI compatibile initialization) + 'root_dir' => $cache_options{'cache_root'}, + 'depth' => $cache_options{'cache_depth'}, + }); + } + cache_fetch($cache, $action); +} else { + $actions{$action}->(); +} + exit; ## ====================================================================== @@ -3169,7 +3234,9 @@ sub git_header_html { # 'application/xhtml+xml', otherwise send it as plain old 'text/html'. # we have to do this because MSIE sometimes globs '*/*', pretending to # support xhtml+xml but choking when it gets what it asked for. - if (defined $cgi->http('HTTP_ACCEPT') && + # Disable content-type negotiation when caching (use mimetype good for all). + if (!$caching_enabled && + defined $cgi->http('HTTP_ACCEPT') && $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ && $cgi->Accept('application/xhtml+xml') != 0) { $content_type = 'application/xhtml+xml'; @@ -3342,17 +3409,25 @@ sub git_footer_html { } print "\n"; # class="page_footer" - if (defined $t0 && gitweb_check_feature('timed')) { + # timing info doesn't make much sense with output (response) caching, + # so when caching is enabled gitweb prints the time of page generation + if ((defined $t0 || $caching_enabled) && + gitweb_check_feature('timed')) { print "
\n"; - print 'This page took '. - ''. - Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]). - ' seconds '. - ' and '. - ''. - $number_of_git_cmds. - ' git commands '. - " to generate.\n"; + if ($caching_enabled) { + print 'This page was generated at '. + gmtime( time() )." GMT\n"; + } else { + print 'This page took '. + ''. + Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]). + ' seconds '. + ' and '. + ''. + $number_of_git_cmds. + ' git commands '. + " to generate.\n"; + } print "
\n"; # class="page_footer" } @@ -3402,6 +3477,10 @@ sub die_error { 500 => '500 Internal Server Error', 503 => '503 Service Unavailable', ); + + # Do not cache error pages (die_error() uses 'exit') + cache_stop() if ($caching_enabled); + git_header_html($http_responses{$status}); print < @@ -5050,7 +5129,8 @@ sub git_blame_common { or print "ERROR $!\n"; print 'END'; - if (defined $t0 && gitweb_check_feature('timed')) { + if (!$caching_enabled && + defined $t0 && gitweb_check_feature('timed')) { print ' '. Time::HiRes::tv_interval($t0, [Time::HiRes::gettimeofday()]). ' '.$number_of_git_cmds; diff --git a/t/gitweb-lib.sh b/t/gitweb-lib.sh index 5a734b1b7b..b7c2937fcc 100644 --- a/t/gitweb-lib.sh +++ b/t/gitweb-lib.sh @@ -27,6 +27,8 @@ our \$export_ok = ''; our \$strict_export = ''; our \$maxload = undef; +our \$cache_pm = '$TEST_DIRECTORY/../gitweb/cache.pm'; + EOF cat >.git/description <>gitweb_config.perl <<\EOF +$caching_enabled = 1; +$cache_options{'expires_in'} = -1; # never expire cache for tests +EOF +rm -rf cache + +test_expect_success \ + 'caching enabled (project summary, first run)' \ + 'gitweb_run "p=.git;a=summary"' +test_debug 'cat gitweb.log' + +test_expect_success \ + 'caching enabled (project summary, second run)' \ + 'gitweb_run "p=.git;a=summary"' +test_debug 'cat gitweb.log' + test_done diff --git a/t/t9503/test_cache_interface.pl b/t/t9503/test_cache_interface.pl index 8700b71111..42c49e9141 100755 --- a/t/t9503/test_cache_interface.pl +++ b/t/t9503/test_cache_interface.pl @@ -7,9 +7,36 @@ use strict; use Test::More; use Data::Dumper; +# Modules that could have been used to capture output for testing cache_fetch +#use Capture::Tiny; +#use Test::Output qw(:stdout); +# Modules that could have been used in $cache_pm for cache_fetch +#use IO::Capture + # test source version; there is no installation target for gitweb my $cache_pm = "$ENV{TEST_DIRECTORY}/../gitweb/cache.pm"; +# ...................................................................... + +# Setup mockup of gitweb's subroutines and variables used in $cache_pm; +# must be done before loading $cache_pm. (This probably means that +# gitweb.perl and cache.pm are too tightly coupled.) +sub href { + return 'href'; +} +my $action = 'action'; +my $fake_action_output = <<'EOF'; +# This is data to be cached and shown +EOF +sub fake_action { + print $fake_action_output; +} +our %actions = ( + $action => \&fake_action, +); + +# ...................................................................... + unless (-f "$cache_pm") { plan skip_all => "$cache_pm not found"; } @@ -22,6 +49,7 @@ ok(!$@, "parse gitweb/cache.pm") ok(defined $return, "do gitweb/cache.pm"); ok($return, "run gitweb/cache.pm"); +# ...................................................................... # Test creating a cache # @@ -88,6 +116,71 @@ $cache->set_expires_in(0); is($cache->get_expires_in(), 0, '"expires in" is set to now (0)'); $cache->set($key, $value); ok(!defined($cache->get($key)), 'cache is expired'); +$cache->set_expires_in(-1); + +# ...................................................................... + +# Prepare for testing cache_fetch from $cache_pm +my $test_perlio_util = eval { require PerlIO::Util; 1 }; +my $cached_output = <<"EOF"; +$fake_action_output# (version recovered from cache) +EOF +$key = gitweb_output_key(); + +# Catch output printed by cache_fetch +# Test all ways of capturing output in cache_fetch +our ($perlio_util, $STDOUT); +my ($test_data, $test_data_fh, $test_STDOUT); +sub capture_cache_fetch_output { + $test_data = '' if defined $test_data; + + if ($perlio_util) { # or $test_perlio_util + *STDOUT->push_layer(scalar => \$test_data); + + cache_fetch($cache, $action); + + *STDOUT->pop_layer(); + + } else { + diag("PerlIO::Util not available, not all tests run"); + $test_STDOUT = *STDOUT; + open $test_data_fh, '>', \$test_data; + $STDOUT = *STDOUT = $test_data_fh; # $STDOUT is from $cache_pm + + cache_fetch($cache, $action); + + *STDOUT = $test_STDOUT; + + } +} + + +# Due to some bad interaction between double capturing, both if second +# capture (for this test) is done using PerlIO layers (via PerlIO::Util), +# and if it is done using *STDOUT manipulation, tests below do not work if +# $perlio_util is false, i.e. if cache_fetch() uses *STDOUT manipulation. +# Earlier manual test shown that cache_fetch() *STDOUT manipulation seems +# to work all right... but this test would fail when 'if' is replaced by +# (currently commented out) 'for'. + +#for my $use_perlio_util (0..$test_perlio_util) { +if ((my $use_perlio_util = $test_perlio_util)) { + $perlio_util = $use_perlio_util; + diag(($perlio_util ? "Use" : "Don't use")." PerlIO::Util"); + + # clean state + $cache->remove($key); + + # first time (if there is no cache) generates cache entry + capture_cache_fetch_output(); + is($test_data, $fake_action_output, 'action output is printed (generated)'); + is($cache->get($key), $fake_action_output, 'action output is in cache (generated)'); + + # second time (if cache is set/valid) reads from cache + $cache->set($key, $cached_output); + capture_cache_fetch_output(); + is($test_data, $cached_output, 'action output is printed (from cache)'); +} done_testing(); -- 2.11.4.GIT