Gitweb caching v7.2
cover Gitweb caching v7.2
This series is a bit fixed up and a tiny bit cleaned up version of "Gitweb caching v7" series from John 'Warthog9' Hawley:
http://thread.gmane.org/gmane.comp.version-control.git/160147
This version of this series is based on top of 'master'
Compared to initial v7.1 fixup of "Gitweb caching v7" series it contains fix to "gitweb: Prepare for splitting gitweb" preventing installation and fix to "gitweb: File based caching layer (from git.kernel.org)" that generated problems with t9501 test, as noticed by Thomas Rast in
http://article.gmane.org/gmane.comp.version-control.git/162787
http://marc.info/?l=git&m=129137257619613&w=2
It also includes trivial fixup in "gitweb: Minimal testing of gitweb caching"... but still those new tests don't pass, at least for me. John Hawley is (according to information from #git IRC channel) is working on "Gitweb caching v8"; I hope that it is fixed there.
Shortlog:
~~~~~~~~~
Jakub Narebski (2):
gitweb: Prepare for splitting gitweb
gitweb: Minimal testing of gitweb caching
John 'Warthog9' Hawley (2):
gitweb: add output buffering and associated functions
gitweb: File based caching layer (from git.kernel.org)
Table of contents:
~~~~~~~~~~~~~~~~~~
[PATCHv7.4 1/4] gitweb: Prepare for splitting gitweb [PATCHv7.4 2/4] gitweb: add output buffering and associated functions [PATCHv7.4 3/4] gitweb: File based caching layer (from git.kernel.org) [PATCHv7.4 4/4] gitweb: Minimal testing of gitweb caching
Diffstat:
~~~~~~~~~
gitweb/Makefile | 21 ++- gitweb/gitweb.perl | 136 +++++++++++- gitweb/lib/cache.pl | 348 +++++++++++++++++++++++++++++ gitweb/static/gitweb.css | 6 + t/gitweb-lib.sh | 15 ++ t/t9500-gitweb-standalone-no-errors.sh | 20 ++ t/t9501-gitweb-standalone-http-status.sh | 13 + t/t9502-gitweb-standalone-parse-output.sh | 33 +++ 8 files changed, 581 insertions(+), 11 deletions(-) create mode 100644 gitweb/lib/cache.pl
Signature · 1 line
1.7.3
PATCH 1/4 gitweb: Prepare for splitting gitweb
Prepare gitweb for having been split into modules that are to be installed alongside gitweb in 'lib/' subdirectory, by adding
use lib __DIR__.'/lib';
to gitweb.perl (to main gitweb script), and preparing for putting modules (relative path) in $(GITWEB_MODULES) in gitweb/Makefile.
This preparatory work allows to add new module to gitweb by simply adding
GITWEB_MODULES += <module>
to gitweb/Makefile (assuming that the module is in 'gitweb/lib/' directory).
While at it pass GITWEBLIBDIR in addition to GITWEB_TEST_INSTALLED to allow testing installed version of gitweb and installed version of modules (for future tests which would check individual (sub)modules).
Using __DIR__ from Dir::Self module (not in core, that's why currently gitweb includes excerpt of code from Dir::Self defining __DIR__) was chosen over using FindBin-based solution (in core since perl 5.00307, while gitweb itself requires at least perl 5.8.0) because FindBin uses BEGIN block, which is a problem under mod_perl and other persistent Perl environments (thought there are workarounds).
At Pavan Kumar Sankara suggestion gitweb/Makefile uses
install [OPTION]... SOURCE... DIRECTORY
format (2nd format) with single SOURCE rather than
install [OPTION]... SOURCE DEST
format (1st format) because of security reasons (race conditions). Modern GNU install has -T' / --no-target-directory' option, but we cannot rely that the $(INSTALL) we are using supports this option.
The install-modules target in gitweb/Makefile uses shell 'for' loop, instead of make's $(foreach) function, to avoid possible problem with generating a command line that exceeded the maximum argument list length.
---
BUGFIX: shell variables should not be in single quotes
I am very sorry about this bug...
This version is identical to
[PATCHv7.3 1/4 (bugfix)] gitweb: Prepare for splitting gitweb
Message-Id: <[email protected]>
http://article.gmane.org/gmane.comp.version-control.git/162690
sent as a separate patch.
gitweb/Makefile | 18 +++++++++++++++--- gitweb/gitweb.perl | 8 ++++++++ 2 files changed, 23 insertions(+), 3 deletions(-)
gitweb/Makefile +15 -3
@@ -57,6 +57,7 @@ PERL_PATH ?= /usr/bin/perl bindir_SQ = $(subst ','\'',$(bindir))#' gitwebdir_SQ = $(subst ','\'',$(gitwebdir))#' gitwebstaticdir_SQ = $(subst ','\'',$(gitwebdir)/static)#' +gitweblibdir_SQ = $(subst ','\'',$(gitwebdir)/lib)#' SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))#' PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH))#' DESTDIR_SQ = $(subst ','\'',$(DESTDIR))#'
@@ -153,20 +154,31 @@ test: test-installed: GITWEB_TEST_INSTALLED='$(DESTDIR_SQ)$(gitwebdir_SQ)' \ + GITWEBLIBDIR='$(DESTDIR_SQ)$(gitweblibdir_SQ)' \ $(MAKE) -C ../t gitweb-test ### Installation rules -install: all +install: all install-modules $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitwebdir_SQ)' $(INSTALL) -m 755 $(GITWEB_PROGRAMS) '$(DESTDIR_SQ)$(gitwebdir_SQ)' $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitwebstaticdir_SQ)' $(INSTALL) -m 644 $(GITWEB_FILES) '$(DESTDIR_SQ)$(gitwebstaticdir_SQ)' +install-modules: + install_dirs="$(sort $(dir $(GITWEB_MODULES)))" && \ + for dir in $$install_dirs; do \ + test -d '$(DESTDIR_SQ)$(gitweblibdir_SQ)'/"$$dir" || \ + $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(gitweblibdir_SQ)'/"$$dir"; \ + done + gitweb_modules="$(GITWEB_MODULES)" && \ + for mod in $$gitweb_modules; do \ + $(INSTALL) -m 644 "lib/$$mod" '$(DESTDIR_SQ)$(gitweblibdir_SQ)'/"$$(dirname $$mod)"; \ + done + ### Cleaning rules clean: $(RM) gitweb.cgi static/gitweb.min.js static/gitweb.min.css GITWEB-BUILD-OPTIONS -.PHONY: all clean install test test-installed .FORCE-GIT-VERSION-FILE FORCE - +.PHONY: all clean install install-modules test test-installed .FORCE-GIT-VERSION-FILE FORCE
gitweb/gitweb.perl +8 -0
@@ -10,6 +10,14 @@ use 5.008; use strict; use warnings; + +use File::Spec; +# __DIR__ is taken from Dir::Self __DIR__ fragment +sub __DIR__ () { + File::Spec->rel2abs(join '', (File::Spec->splitpath(__FILE__))[0, 1]); +} +use lib __DIR__ . '/lib'; + use CGI qw(:standard :escapeHTML -nosticky); use CGI::Util qw(unescape); use CGI::Carp qw(fatalsToBrowser set_message);
Signature · 1 line
1.7.3
PATCH 2/4 gitweb: add output buffering and associated functions
In reply to Jakub NarebskiFrom: John 'Warthog9' Hawley <[email protected]>
This adds output buffering for gitweb, mainly in preparation for caching support. This is a dramatic change to how caching was being done, mainly in passing around the variable manually and such.
This centrally flips the entire STDOUT to a variable, which after the completion of the run, flips it back and does a print on the resulting data.
This should save on the previous 10K line patch (or so) that adds more explicit output passing.
[jn: modified reset_output to silence 'gitweb.perl: Name "main::STDOUT_REAL"
used only once: possible typo at ../gitweb/gitweb.perl line 1130.' warning]
---
Reminder: it uses
open STDOUT, ">&", \*STDOUT_REAL;
rather than
open(STDOUT,">&STDOUT_REAL");
to silence spurious (invalid) warning
gitweb.perl: Name "main::STDOUT_REAL" used only once: possible typo at ../gitweb/gitweb.perl line 1130.
This version is identical to
[PATCHv7.1 2/4] gitweb: add output buffering and associated functions
Message-Id: <[email protected]>
http://article.gmane.org/gmane.comp.version-control.git/160475
gitweb/gitweb.perl | 29 +++++++++++++++++++++++++++++ 1 files changed, 29 insertions(+), 0 deletions(-)
gitweb/gitweb.perl +29 -0
@@ -39,6 +39,9 @@ BEGIN { our $version = "++GIT_VERSION++"; +# Output buffer variable +our $output = ""; + our ($my_url, $my_uri, $base_url, $path_info, $home_link); sub evaluate_uri { our $cgi;
@@ -1134,6 +1137,25 @@ sub evaluate_argv { ); } +sub change_output { + our $output; + + # Trap the 'proper' STDOUT to STDOUT_REAL for things like error messages and such + open(STDOUT_REAL,">&STDOUT") or die "Unable to capture STDOUT $!\n"; + + # Close STDOUT, so that it isn't being used anymore. + close STDOUT; + + # Trap STDOUT to the $output variable, which is what I was using in the original + # patch anyway. + open(STDOUT,">", \$output) || die "Unable to open STDOUT: $!"; #open STDOUT handle to use $var +} + +sub reset_output { + # This basically takes STDOUT_REAL and puts it back as STDOUT + open STDOUT, ">&", \*STDOUT_REAL; +} + sub run { evaluate_argv();
@@ -1145,7 +1167,10 @@ sub run { $pre_dispatch_hook->() if $pre_dispatch_hook; + change_output(); run_request(); + reset_output(); + print $output; $post_dispatch_hook->() if $post_dispatch_hook;
@@ -3655,6 +3680,10 @@ sub die_error { 500 => '500 Internal Server Error', 503 => '503 Service Unavailable', ); + # Reset the output so that we are actually going to STDOUT as opposed + # to buffering the output. + reset_output(); + git_header_html($http_responses{$status}, undef, %opts); print <<EOF; <div class="page_body">
Signature · 1 line
1.7.3
PATCH 3/4 gitweb: File based caching layer (from git.kernel.org)
In reply to Jakub NarebskiFrom: John 'Warthog9' Hawley <[email protected]>
This is a relatively large patch that implements the file based caching layer that is quite similar to the one used on such large sites as kernel.org and soon git.fedoraproject.org. This provides a simple, and straight forward caching mechanism that scales dramatically better than Gitweb by itself.
The caching layer basically buffers the output that Gitweb would normally return, and saves that output to a cache file on the local disk. When the file is requested it attempts to gain a shared lock on the cache file and cat it out to the client. Should an exclusive lock be on a file (it's being updated) the code has a choice to either update in the background and go ahead and show the stale page while update is being performed, or stall the client(s) until the page is generated.
There are two forms of stalling involved here, background building and non-background building, both of which are discussed in the configuration page.
There are still a few known "issues" with respect to this:
- Code needs to be added to be "browser" aware so that clients like wget that are trying to get a binary blob don't obtain a "Generating..." page
Caching is disabled by default. You can turn it on by setting $caching_enabled variable to true to enable file based caching.
[jn: added error checking to loading 'cache.pl'; moved check
for $caching_enabled outside out of cache_fetch, which required update to die_error()]
---
This fixes a bug that generated problems with t9501 test, as noticed by Thomas Rast in
http://article.gmane.org/gmane.comp.version-control.git/162787
http://marc.info/?l=git&m=129137257619613&w=2
Fixup was sent as interdiff in
http://marc.info/?l=git&m=129138558404482&w=2
and it was tested that it fixes problem by Thomas Rast... so perhaps it should be
gitweb/Makefile | 3 + gitweb/gitweb.perl | 107 +++++++++++++-- gitweb/lib/cache.pl | 348 ++++++++++++++++++++++++++++++++++++++++++++++ gitweb/static/gitweb.css | 6 + 4 files changed, 452 insertions(+), 12 deletions(-) create mode 100644 gitweb/lib/cache.pl
gitweb/Makefile +3 -0
@@ -113,6 +113,9 @@ endif GITWEB_FILES += static/git-logo.png static/git-favicon.png +# Gitweb caching +GITWEB_MODULES += cache.pl + GITWEB_REPLACE = \ -e 's|++GIT_VERSION++|$(GIT_VERSION)|g' \ -e 's|++GIT_BINDIR++|$(bindir)|g' \
gitweb/gitweb.perl +95 -12
176 lines not on this page · raw message
gitweb/lib/cache.pl new file +348 -0
348 lines not on this page · raw message
gitweb/static/gitweb.css +6 -0
@@ -67,6 +67,12 @@ div.page_path { border-width: 0px 0px 1px; } +div.cachetime { + float: left; + margin-right: 10px; + color: #555555; +} + div.page_footer { height: 17px; padding: 4px 8px;
Signature · 1 line
1.7.3
PATCH 4/4 gitweb: Minimal testing of gitweb caching
In reply to Jakub NarebskiAdd 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. Check also that request for non-existent object (which results in die_error() codepath to be called) doesn't produce errors.
Check in t9501-gitweb-standalone-http-status that request for non-existent object produces correct output (HTTP headers and HTML output) also when caching is enabled.
Check in the t9502-gitweb-standalone-parse-output test that gitweb produces the same output with and without caching, for first and second run, with binary (raw) or plain text (utf8) output.
The common routine that enables cache, gitweb_enable_caching, is defined in t/gitweb-lib.sh
---
The minor bug in value for $xxxCacheTime value in t/gitweb-lib.sh noticed by Junio C Hamano in
http://thread.gmane.org/gmane.comp.version-control.git/160147/focus=161635
is fixed here, but it doesn't make gitweb pass new tests.
t/gitweb-lib.sh | 15 +++++++++++++ t/t9500-gitweb-standalone-no-errors.sh | 20 +++++++++++++++++ t/t9501-gitweb-standalone-http-status.sh | 13 +++++++++++ t/t9502-gitweb-standalone-parse-output.sh | 33 +++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 0 deletions(-)
t/gitweb-lib.sh +15 -0
@@ -52,6 +52,21 @@ EOF export SCRIPT_NAME } +gitweb_enable_caching () { + test_expect_success 'enable caching' ' + cat >>gitweb_config.perl <<-\EOF && + our $caching_enabled = 1; + our $minCacheTime = 60*60*24*7; # very long expiration time for tests (a week) + our $maxCacheTime = 60*60*24*30; # upper bound for dynamic (adaptive) caching + our $cachedir = "cache"; # for testsuite to clear the right thing + # required, because otherwise some tests might intermittently not pass + our $backgroundCache = 0; # should turn off cacheWaitForUpdate() / "Generating..." + #our $cacheDoFork = 0; + EOF + rm -rf cache/ + ' +} + gitweb_run () { GATEWAY_INTERFACE='CGI/1.1' HTTP_ACCEPT='*/*'
t/t9500-gitweb-standalone-no-errors.sh +20 -0
@@ -677,4 +677,24 @@ test_expect_success HIGHLIGHT \ gitweb_run "p=.git;a=blob;f=test.sh"' test_debug 'cat gitweb.log' +# ---------------------------------------------------------------------- +# caching + +gitweb_enable_caching + +test_expect_success \ + 'caching enabled (project summary, first run, generating cache)' \ + 'gitweb_run "p=.git;a=summary"' +test_debug 'cat gitweb.log' + +test_expect_success \ + 'caching enabled (project summary, second run, cached version)' \ + 'gitweb_run "p=.git;a=summary"' +test_debug 'cat gitweb.log' + +test_expect_success \ + 'caching enabled (non-existent commit, non-cache error page)' \ + 'gitweb_run "p=.git;a=commit;h=non-existent"' +test_debug 'cat gitweb.log' + test_done
t/t9501-gitweb-standalone-http-status.sh +13 -0
@@ -134,5 +134,18 @@ cat >>gitweb_config.perl <<\EOF our $maxload = undef; EOF +# ---------------------------------------------------------------------- +# output caching + +gitweb_enable_caching + +test_expect_success 'caching enabled (non-existent commit, 404 error)' ' + gitweb_run "p=.git;a=commit;h=non-existent" && + grep "Status: 404 Not Found" gitweb.headers && + grep "404 - Unknown commit object" gitweb.body +' +test_debug 'echo "log" && cat gitweb.log' +test_debug 'echo "headers" && cat gitweb.headers' +test_debug 'echo "body" && cat gitweb.body' test_done
t/t9502-gitweb-standalone-parse-output.sh +33 -0
@@ -112,4 +112,37 @@ test_expect_success 'snapshot: hierarchical branch name (xx/test)' ' ' test_debug 'cat gitweb.headers' + +# ---------------------------------------------------------------------- +# whether gitweb with caching enabled produces the same output + +test_expect_success 'setup for caching tests (utf8 patch, binary file)' ' + . "$TEST_DIRECTORY"/t3901-utf8.txt && + cp "$TEST_DIRECTORY"/test9200a.png image.png && + git add image.png && + git commit -F "$TEST_DIRECTORY"/t3900/1-UTF-8.txt && + gitweb_run "p=.git;a=patch" && + mv gitweb.body no_cache.txt && + gitweb_run "p=.git;a=blob_plain;f=image.png" && + mv gitweb.body no_cache.png +' + +gitweb_enable_caching + +for desc in 'generating cache' 'cached version'; do + test_expect_success "caching enabled, plain text (utf8) output, $desc" ' + gitweb_run "p=.git;a=patch" && + mv gitweb.body cache.txt && + test_cmp no_cache.txt cache.txt + ' +done + +for desc in 'generating cache' 'cached version'; do + test_expect_success "caching enabled, binary output (raw), $desc" ' + gitweb_run "p=.git;a=blob_plain;f=image.png" && + mv gitweb.body cache.png && + cmp no_cache.png cache.png + ' +done + test_done
Signature · 1 line
1.7.3
Sorry, of course subject should read:
[PATCHv7.4 0/4] Gitweb caching v7.4
Signature · 2 lines
Jakub Narebski Poland