Page Speed Optimization Libraries  1.7.30.3
net/instaweb/rewriter/public/rewrite_context.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
00020 #define NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
00021 
00022 #include <set>
00023 #include <vector>
00024 
00025 #include "net/instaweb/http/public/http_cache.h"
00026 #include "net/instaweb/rewriter/cached_result.pb.h"
00027 #include "net/instaweb/rewriter/public/output_resource_kind.h"
00028 #include "net/instaweb/rewriter/public/resource.h"
00029 #include "net/instaweb/rewriter/public/server_context.h"
00030 #include "net/instaweb/rewriter/public/resource_slot.h"
00031 #include "net/instaweb/rewriter/public/rewrite_context.h"
00032 #include "net/instaweb/rewriter/public/rewrite_result.h"
00033 #include "net/instaweb/util/public/basictypes.h"
00034 #include "net/instaweb/util/public/scoped_ptr.h"
00035 #include "net/instaweb/util/public/string.h"
00036 #include "net/instaweb/util/public/string_util.h"
00037 #include "net/instaweb/util/public/url_segment_encoder.h"
00038 
00039 namespace net_instaweb {
00040 
00041 class AsyncFetch;
00042 class GoogleUrl;
00043 class MessageHandler;
00044 class NamedLock;
00045 class RequestTrace;
00046 class ResponseHeaders;
00047 class RewriteDriver;
00048 class RewriteOptions;
00049 class Statistics;
00050 class Variable;
00051 class Writer;
00052 class FreshenMetadataUpdateManager;
00053 
00146 class RewriteContext {
00147  public:
00148   typedef std::vector<InputInfo*> InputInfoStarVector;
00149   static const char kNumDeadlineAlarmInvocations[];
00150   static const char kNumDistributedRewriteSuccesses[];
00151   static const char kNumDistributedRewriteFailures[];
00152   static const char kNumDistributedMetadataFailures[];
00154   static const char kDistributedExt[];
00156   static const char kDistributedHash[];
00159   struct CacheLookupResult {
00160     CacheLookupResult()
00161         : cache_ok(false),
00162           can_revalidate(false),
00163           useable_cache_content(false),
00164           is_stale_rewrite(false),
00165           partitions(new OutputPartitions) {}
00166 
00167     bool cache_ok;
00168     bool can_revalidate;
00169     bool useable_cache_content;
00170     bool is_stale_rewrite;
00171     InputInfoStarVector revalidate;
00172     scoped_ptr<OutputPartitions> partitions;
00173   };
00174 
00176   class CacheLookupResultCallback {
00177    public:
00178     CacheLookupResultCallback() {}
00179     virtual ~CacheLookupResultCallback();
00180     virtual void Done(const GoogleString& cache_key,
00181                       CacheLookupResult* result) = 0;
00182    private:
00183     DISALLOW_COPY_AND_ASSIGN(CacheLookupResultCallback);
00184   };
00185 
00188   RewriteContext(RewriteDriver* driver,   
00189                  RewriteContext* parent,  
00190                  ResourceContext* resource_context);
00191   virtual ~RewriteContext();
00192 
00197   int num_slots() const { return slots_.size(); }
00198   ResourceSlotPtr slot(int index) const { return slots_[index]; }
00199 
00202   int num_outputs() const { return outputs_.size(); }
00203   OutputResourcePtr output(int i) const { return outputs_[i]; }
00204 
00207   int num_output_partitions() const;
00208   const CachedResult* output_partition(int i) const;
00209   CachedResult* output_partition(int i);
00210 
00213   bool chained() const { return chained_; }
00214 
00218   void AddSlot(const ResourceSlotPtr& slot);
00219 
00222   void RemoveLastSlot();
00223 
00229   void AddNestedContext(RewriteContext* context);
00230 
00241   void Initiate();
00242 
00248   bool Fetch(const OutputResourcePtr& output_resource,
00249              AsyncFetch* fetch,
00250              MessageHandler* message_handler);
00251 
00265   static bool LookupMetadataForOutputResource(
00266       const GoogleString& url,
00267       RewriteDriver* driver,
00268       GoogleString* error_out,
00269       CacheLookupResultCallback* callback);
00270 
00273   bool slow() const { return slow_; }
00274 
00276   bool is_metadata_cache_miss() const { return is_metadata_cache_miss_; }
00277 
00279   bool has_parent() const { return parent_ != NULL; }
00280 
00283   bool IsNestedIn(StringPiece id) const;
00284 
00286   RewriteContext* parent() { return parent_; }
00287   const RewriteContext* parent() const { return parent_; }
00288 
00290   void set_force_rewrite(bool x) { force_rewrite_ = x; }
00291 
00292   bool rewrite_uncacheable() const { return rewrite_uncacheable_; }
00293   void set_rewrite_uncacheable(bool rewrite_uncacheable) {
00294     rewrite_uncacheable_ = rewrite_uncacheable;
00295   }
00296 
00297   const ResourceContext* resource_context() const {
00298     return resource_context_.get();
00299   }
00300 
00302   GoogleString ToString(StringPiece prefix) const;
00303 
00305   static void InitStats(Statistics* stats);
00306 
00307  protected:
00308   typedef std::vector<GoogleUrl*> GoogleUrlStarVector;
00309 
00314 
00318   ServerContext* FindServerContext() const;
00319   const RewriteOptions* Options() const;
00320   RewriteDriver* Driver() const;
00321 
00323   int num_nested() const { return nested_.size(); }
00324   RewriteContext* nested(int i) const { return nested_[i]; }
00325 
00326   OutputPartitions* partitions() { return partitions_.get(); }
00327 
00330   void AddRecheckDependency();
00331 
00335   virtual bool OptimizationOnly() const { return true; }
00336 
00354   virtual bool Partition(OutputPartitions* partitions,
00355                          OutputResourceVector* outputs);
00356 
00362   virtual void PartitionAsync(OutputPartitions* partitions,
00363                               OutputResourceVector* outputs);
00364 
00368   void PartitionDone(bool result);
00369 
00372   void CrossThreadPartitionDone(bool result);
00373 
00392   virtual void Rewrite(int partition_index,
00393                        CachedResult* partition,
00394                        const OutputResourcePtr& output) = 0;
00395 
00400   void RewriteDone(RewriteResult result, int partition_index);
00401 
00408   virtual bool AbsolutifyIfNeeded(const StringPiece& input_contents,
00409                                   Writer* writer, MessageHandler* handler);
00410 
00414   void StartNestedTasks();
00415 
00420   virtual void Harvest();
00421 
00433   virtual void Render();
00434 
00445   virtual void WillNotRender();
00446 
00450   virtual void Cancel();
00451 
00454 
00461   virtual const UrlSegmentEncoder* encoder() const;
00462 
00465   virtual GoogleString CacheKeySuffix() const;
00466 
00473   virtual GoogleString UserAgentCacheKey(
00474       const ResourceContext* context) const {
00475     return "";
00476   }
00477 
00482   virtual void EncodeUserAgentIntoResourceContext(ResourceContext* context) {}
00483 
00485   virtual const char* id() const = 0;
00486 
00495   virtual OutputResourceKind kind() const = 0;
00496 
00500 
00503   void AttachDependentRequestTrace(const StringPiece& label);
00504 
00508   RequestTrace* dependent_request_trace() { return dependent_request_trace_; }
00509 
00512   void TracePrintf(const char* fmt, ...);
00513 
00521 
00531   virtual void StartFetchReconstruction();
00532 
00538   bool ShouldDistributeRewrite() const;
00539 
00542   bool IsDistributedRewriteForHtml() const;
00543 
00547   void DistributeRewrite();
00548 
00554   void DetachFetch();
00555 
00560   virtual bool DecodeFetchUrls(const OutputResourcePtr& output_resource,
00561                                MessageHandler* message_handler,
00562                                GoogleUrlStarVector* url_vector);
00563 
00568   virtual void FixFetchFallbackHeaders(ResponseHeaders* headers);
00569 
00572   virtual void FetchCallbackDone(bool success);
00573 
00578   virtual void FetchTryFallback(const GoogleString& url,
00579                                 const StringPiece& hash);
00580 
00582   void Freshen();
00583 
00584   bool notify_driver_on_fetch_done() const {
00585     return notify_driver_on_fetch_done_;
00586   }
00587   void set_notify_driver_on_fetch_done(bool value) {
00588     notify_driver_on_fetch_done_ = value;
00589   }
00590 
00595   bool block_distribute_rewrite() const { return block_distribute_rewrite_; }
00596   void set_block_distribute_rewrite(const bool x) {
00597     block_distribute_rewrite_ = x;
00598   }
00599 
00601   AsyncFetch* async_fetch();
00602 
00604   bool FetchContextDetached();
00605 
00607   MessageHandler* fetch_message_handler();
00608 
00610   bool stale_rewrite() const { return stale_rewrite_; }
00611 
00615   virtual int64 GetRewriteDeadlineAlarmMs() const;
00616 
00618   virtual bool CreationLockBeforeStartFetch() { return true; }
00619 
00620  private:
00621   class DistributedRewriteCallback;
00622   class DistributedRewriteFetch;
00623   class OutputCacheCallback;
00624   class LookupMetadataForOutputResourceCallback;
00625   class HTTPCacheCallback;
00626   class ResourceCallbackUtils;
00627   class ResourceFetchCallback;
00628   class ResourceReconstructCallback;
00629   class ResourceRevalidateCallback;
00630   class InvokeRewriteFunction;
00631   class RewriteFreshenCallback;
00632   friend class RewriteDriver;
00633 
00634   typedef std::set<RewriteContext*> ContextSet;
00635 
00639   enum FallbackCondition {
00640     kFallbackDiscretional,   
00641 
00642     kFallbackEmergency    
00643 
00644   };
00645 
00647   void Start();
00648   void SetPartitionKey();
00649   void StartFetch();
00650   void StartFetchImpl();
00651   void CancelFetch();
00652   void OutputCacheDone(CacheLookupResult* cache_result);
00653   void OutputCacheHit(bool write_partitions);
00654   void OutputCacheRevalidate(const InputInfoStarVector& to_revalidate);
00655   void OutputCacheMiss();
00656   void ResourceFetchDone(bool success, ResourcePtr resource, int slot_index);
00657   void ResourceRevalidateDone(InputInfo* input_info, bool success);
00658   void LogMetadataCacheInfo(bool cache_ok, bool can_revalidate);
00659 
00664   void RepeatedSuccess(const RewriteContext* primary);
00665   void RepeatedFailure();
00666 
00675   void Finalize();
00676 
00678   NamedLock* Lock();
00679 
00689   void FetchInputs();
00690 
00695   void DistributeRewriteDone(bool success);
00696 
00700   bool ParseAndRemoveMetadataFromResponseHeaders(
00701       ResponseHeaders* response_headers, CacheLookupResult* cache_result);
00702 
00705   bool CreateOutputResourceFromContent(const CachedResult& cached_result,
00706                                        const ResponseHeaders& response_headers,
00707                                        StringPiece content,
00708                                        OutputResourcePtr* output_resource);
00709 
00719   GoogleString DistributedFetchUrl(StringPiece url);
00720 
00723   bool IsFetchRewrite() const { return fetch_.get() != NULL; }
00724 
00729   void NestedRewriteDone(const RewriteContext* context);
00730 
00734   void Activate();
00735 
00759   void Propagate(bool render_slots);
00760 
00767   void StartRewriteForHtml();
00768   void StartRewriteForFetch();
00769 
00776   bool ReadyToRewrite() const;
00777 
00782   void DetachSlots();
00783 
00786   void RunSuccessors();
00787 
00790   void WritePartition();
00791 
00801   void FinalizeRewriteForHtml();
00802 
00808   void RetireRewriteForHtml(bool permit_render);
00809 
00812   void MarkSlow();
00813 
00816   void MarkTooBusy();
00817 
00821   void CollectDependentTopLevel(ContextSet* contexts);
00822 
00825   void RewriteDoneImpl(RewriteResult result, int partition_index);
00826 
00829   void StartNestedTasksImpl();
00830 
00834   void RenderPartitionOnDetach(int partition_index);
00835 
00838   bool PrepareFetch(
00839       const OutputResourcePtr& output_resource,
00840       AsyncFetch* fetch,
00841       MessageHandler* message_handler);
00842 
00845   bool CreateOutputResourceForCachedOutput(const CachedResult* cached_result,
00846                                            OutputResourcePtr* output_resource);
00847 
00849   void FetchCacheDone(CacheLookupResult* cache_result);
00850 
00853   void FetchFallbackCacheDone(HTTPCache::FindResult result,
00854                               HTTPCache::Callback* data);
00855 
00860   bool CanFetchFallbackToOriginal(FallbackCondition circumstance) const;
00861 
00864   bool HasDuplicateOtherDependency(const InputInfo& input);
00865 
00869   void CheckAndAddOtherDependency(const InputInfo& input);
00870 
00873   void CheckAndFreshenResource(const InputInfo& input_info,
00874                                ResourcePtr resource, int partition_index,
00875                                int input_index,
00876                                FreshenMetadataUpdateManager* freshen_manager);
00877   ResourcePtr CreateUrlResource(const StringPiece& input_url);
00878 
00880   ResourceSlotVector slots_;
00881 
00885   std::vector<bool> render_slots_;
00886 
00897 
00898   bool started_;
00899   scoped_ptr<OutputPartitions> partitions_;
00900   OutputResourceVector outputs_;
00901   int outstanding_fetches_;
00902   int outstanding_rewrites_;
00903   scoped_ptr<ResourceContext> resource_context_;
00904   GoogleString partition_key_;
00905 
00906   UrlSegmentEncoder default_encoder_;
00907 
00910   scoped_ptr<NamedLock> lock_;
00911 
00915   class FetchContext;
00916   scoped_ptr<FetchContext> fetch_;
00917 
00920   std::vector<RewriteContext*> successors_;
00921 
00925   std::vector<RewriteContext*> repeated_;
00926 
00930   int num_pending_nested_;
00931   std::vector<RewriteContext*> nested_;
00932 
00934   RewriteContext* parent_;
00935 
00945   RewriteDriver* driver_;
00946 
00948   int num_predecessors_;
00949 
00952   bool chained_;
00953 
00971 
00973   bool rewrite_done_;
00974 
00979   bool ok_to_write_output_partitions_;
00980 
00983   bool was_too_busy_;
00984 
00988   bool slow_;
00989 
00991   bool revalidate_ok_;
00992 
00995   bool notify_driver_on_fetch_done_;
00996 
00999   bool force_rewrite_;
01000 
01003   bool stale_rewrite_;
01004 
01007   bool is_metadata_cache_miss_;
01008 
01011   bool rewrite_uncacheable_;
01012 
01015   RequestTrace* dependent_request_trace_;
01016 
01019   bool block_distribute_rewrite_;
01020 
01022   scoped_ptr<DistributedRewriteFetch> distributed_fetch_;
01023 
01025   StringIntMap other_dependency_map_;
01026 
01027   Variable* const num_distributed_rewrite_failures_;
01028   Variable* const num_distributed_rewrite_successes_;
01029   Variable* const num_distributed_metadata_failures_;
01030   DISALLOW_COPY_AND_ASSIGN(RewriteContext);
01031 };
01032 
01033 }  
01034 
01035 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines