Page Speed Optimization Libraries  1.7.30.1
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 
00567   virtual void FixFetchFallbackHeaders(ResponseHeaders* headers);
00568 
00571   virtual void FetchCallbackDone(bool success);
00572 
00577   virtual void FetchTryFallback(const GoogleString& url,
00578                                 const StringPiece& hash);
00579 
00581   void Freshen();
00582 
00583   bool notify_driver_on_fetch_done() const {
00584     return notify_driver_on_fetch_done_;
00585   }
00586   void set_notify_driver_on_fetch_done(bool value) {
00587     notify_driver_on_fetch_done_ = value;
00588   }
00589 
00594   bool block_distribute_rewrite() const { return block_distribute_rewrite_; }
00595   void set_block_distribute_rewrite(const bool x) {
00596     block_distribute_rewrite_ = x;
00597   }
00598 
00600   AsyncFetch* async_fetch();
00601 
00603   bool FetchContextDetached();
00604 
00606   MessageHandler* fetch_message_handler();
00607 
00609   bool stale_rewrite() const { return stale_rewrite_; }
00610 
00614   virtual int64 GetRewriteDeadlineAlarmMs() const;
00615 
00617   virtual bool CreationLockBeforeStartFetch() { return true; }
00618 
00619  private:
00620   class DistributedRewriteCallback;
00621   class DistributedRewriteFetch;
00622   class OutputCacheCallback;
00623   class LookupMetadataForOutputResourceCallback;
00624   class HTTPCacheCallback;
00625   class ResourceCallbackUtils;
00626   class ResourceFetchCallback;
00627   class ResourceReconstructCallback;
00628   class ResourceRevalidateCallback;
00629   class InvokeRewriteFunction;
00630   class RewriteFreshenCallback;
00631   friend class RewriteDriver;
00632 
00633   typedef std::set<RewriteContext*> ContextSet;
00634 
00638   enum FallbackCondition {
00639     kFallbackDiscretional,   
00640 
00641     kFallbackEmergency    
00642 
00643   };
00644 
00646   void Start();
00647   void SetPartitionKey();
00648   void StartFetch();
00649   void StartFetchImpl();
00650   void CancelFetch();
00651   void OutputCacheDone(CacheLookupResult* cache_result);
00652   void OutputCacheHit(bool write_partitions);
00653   void OutputCacheRevalidate(const InputInfoStarVector& to_revalidate);
00654   void OutputCacheMiss();
00655   void ResourceFetchDone(bool success, ResourcePtr resource, int slot_index);
00656   void ResourceRevalidateDone(InputInfo* input_info, bool success);
00657   void LogMetadataCacheInfo(bool cache_ok, bool can_revalidate);
00658 
00663   void RepeatedSuccess(const RewriteContext* primary);
00664   void RepeatedFailure();
00665 
00674   void Finalize();
00675 
00677   NamedLock* Lock();
00678 
00688   void FetchInputs();
00689 
00694   void DistributeRewriteDone(bool success);
00695 
00699   bool ParseAndRemoveMetadataFromResponseHeaders(
00700       ResponseHeaders* response_headers, CacheLookupResult* cache_result);
00701 
00704   bool CreateOutputResourceFromContent(const CachedResult& cached_result,
00705                                        const ResponseHeaders& response_headers,
00706                                        StringPiece content,
00707                                        OutputResourcePtr* output_resource);
00708 
00718   GoogleString DistributedFetchUrl(StringPiece url);
00719 
00722   bool IsFetchRewrite() const { return fetch_.get() != NULL; }
00723 
00728   void NestedRewriteDone(const RewriteContext* context);
00729 
00733   void Activate();
00734 
00758   void Propagate(bool render_slots);
00759 
00766   void StartRewriteForHtml();
00767   void StartRewriteForFetch();
00768 
00775   bool ReadyToRewrite() const;
00776 
00781   void DetachSlots();
00782 
00785   void RunSuccessors();
00786 
00789   void WritePartition();
00790 
00800   void FinalizeRewriteForHtml();
00801 
00807   void RetireRewriteForHtml(bool permit_render);
00808 
00811   void MarkSlow();
00812 
00815   void MarkTooBusy();
00816 
00820   void CollectDependentTopLevel(ContextSet* contexts);
00821 
00824   void RewriteDoneImpl(RewriteResult result, int partition_index);
00825 
00828   void StartNestedTasksImpl();
00829 
00833   void RenderPartitionOnDetach(int partition_index);
00834 
00837   bool PrepareFetch(
00838       const OutputResourcePtr& output_resource,
00839       AsyncFetch* fetch,
00840       MessageHandler* message_handler);
00841 
00844   bool CreateOutputResourceForCachedOutput(const CachedResult* cached_result,
00845                                            OutputResourcePtr* output_resource);
00846 
00848   void FetchCacheDone(CacheLookupResult* cache_result);
00849 
00852   void FetchFallbackCacheDone(HTTPCache::FindResult result,
00853                               HTTPCache::Callback* data);
00854 
00859   bool CanFetchFallbackToOriginal(FallbackCondition circumstance) const;
00860 
00863   bool HasDuplicateOtherDependency(const InputInfo& input);
00864 
00868   void CheckAndAddOtherDependency(const InputInfo& input);
00869 
00872   void CheckAndFreshenResource(const InputInfo& input_info,
00873                                ResourcePtr resource, int partition_index,
00874                                int input_index,
00875                                FreshenMetadataUpdateManager* freshen_manager);
00876   ResourcePtr CreateUrlResource(const StringPiece& input_url);
00877 
00879   ResourceSlotVector slots_;
00880 
00884   std::vector<bool> render_slots_;
00885 
00896 
00897   bool started_;
00898   scoped_ptr<OutputPartitions> partitions_;
00899   OutputResourceVector outputs_;
00900   int outstanding_fetches_;
00901   int outstanding_rewrites_;
00902   scoped_ptr<ResourceContext> resource_context_;
00903   GoogleString partition_key_;
00904 
00905   UrlSegmentEncoder default_encoder_;
00906 
00909   scoped_ptr<NamedLock> lock_;
00910 
00914   class FetchContext;
00915   scoped_ptr<FetchContext> fetch_;
00916 
00919   std::vector<RewriteContext*> successors_;
00920 
00924   std::vector<RewriteContext*> repeated_;
00925 
00929   int num_pending_nested_;
00930   std::vector<RewriteContext*> nested_;
00931 
00933   RewriteContext* parent_;
00934 
00944   RewriteDriver* driver_;
00945 
00947   int num_predecessors_;
00948 
00951   bool chained_;
00952 
00970 
00972   bool rewrite_done_;
00973 
00978   bool ok_to_write_output_partitions_;
00979 
00982   bool was_too_busy_;
00983 
00987   bool slow_;
00988 
00990   bool revalidate_ok_;
00991 
00994   bool notify_driver_on_fetch_done_;
00995 
00998   bool force_rewrite_;
00999 
01002   bool stale_rewrite_;
01003 
01006   bool is_metadata_cache_miss_;
01007 
01010   bool rewrite_uncacheable_;
01011 
01014   RequestTrace* dependent_request_trace_;
01015 
01018   bool block_distribute_rewrite_;
01019 
01021   scoped_ptr<DistributedRewriteFetch> distributed_fetch_;
01022 
01024   StringIntMap other_dependency_map_;
01025 
01026   Variable* const num_distributed_rewrite_failures_;
01027   Variable* const num_distributed_rewrite_successes_;
01028   Variable* const num_distributed_metadata_failures_;
01029   DISALLOW_COPY_AND_ASSIGN(RewriteContext);
01030 };
01031 
01032 }  
01033 
01034 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines