19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_REWRITE_CONTEXT_H_
26 #include "net/instaweb/rewriter/cached_result.pb.h"
32 #include "pagespeed/controller/schedule_rewrite_callback.h"
33 #include "pagespeed/kernel/base/basictypes.h"
34 #include "pagespeed/kernel/base/function.h"
35 #include "pagespeed/kernel/base/scoped_ptr.h"
36 #include "pagespeed/kernel/base/string.h"
37 #include "pagespeed/kernel/base/string_util.h"
38 #include "pagespeed/kernel/http/google_url.h"
39 #include "pagespeed/kernel/http/response_headers.h"
40 #include "pagespeed/kernel/util/url_segment_encoder.h"
42 namespace net_instaweb {
52 class FreshenMetadataUpdateManager;
148 typedef std::vector<InputInfo*> InputInfoStarVector;
149 static const char kNumRewritesAbandonedForLockContention[];
150 static const char kNumDeadlineAlarmInvocations[];
151 static const char kNumDistributedRewriteSuccesses[];
152 static const char kNumDistributedRewriteFailures[];
153 static const char kNumDistributedMetadataFailures[];
158 static const char kHashMismatchMessage[];
165 can_revalidate(
false),
166 useable_cache_content(
false),
167 is_stale_rewrite(
false),
168 partitions(
new OutputPartitions) {}
172 bool useable_cache_content;
173 bool is_stale_rewrite;
174 InputInfoStarVector revalidate;
175 scoped_ptr<OutputPartitions> partitions;
183 virtual void Done(
const GoogleString& cache_key,
193 ResourceContext* resource_context);
201 ResourceSlotPtr slot(
int index)
const {
return slots_[index]; }
206 OutputResourcePtr output(
int i)
const {
return outputs_[i]; }
211 const CachedResult* output_partition(
int i)
const;
212 CachedResult* output_partition(
int i);
221 void AddSlot(
const ResourceSlotPtr& slot);
234 void CallFetchInputs();
235 void CallLockFailed();
236 void CallStartFetchImpl();
255 bool Fetch(
const OutputResourcePtr& output_resource,
257 MessageHandler* message_handler);
261 bool slow()
const {
return slow_; }
281 RewriteDriver* Driver()
const {
288 bool rewrite_uncacheable()
const {
return rewrite_uncacheable_; }
289 void set_rewrite_uncacheable(
bool rewrite_uncacheable) {
290 rewrite_uncacheable_ = rewrite_uncacheable;
293 const ResourceContext* resource_context()
const {
294 return resource_context_.get();
299 GoogleString ToStringWithPrefix(StringPiece prefix)
const;
302 static void InitStats(Statistics* stats);
305 typedef std::vector<GoogleUrl*> GoogleUrlStarVector;
316 const RewriteOptions* Options()
const;
318 OutputPartitions* partitions() {
return partitions_.get(); }
346 virtual bool Partition(OutputPartitions* partitions,
347 OutputResourceVector* outputs);
355 OutputResourceVector* outputs);
384 virtual void Rewrite(
int partition_index,
385 CachedResult* partition,
386 const OutputResourcePtr& output) = 0;
404 StringPiece contents,
406 MessageHandler* handler);
458 virtual const UrlSegmentEncoder*
encoder()
const;
471 const ResourceContext* context)
const {
482 virtual const char*
id()
const = 0;
558 MessageHandler* message_handler,
559 GoogleUrlStarVector* url_vector);
566 ResponseHeaders* headers);
577 const StringPiece& hash);
582 bool notify_driver_on_fetch_done()
const {
583 return notify_driver_on_fetch_done_;
585 void set_notify_driver_on_fetch_done(
bool value) {
586 notify_driver_on_fetch_done_ = value;
594 void set_block_distribute_rewrite(
const bool x) {
595 block_distribute_rewrite_ = x;
646 OutputResourcePtr output_resource,
647 const GoogleUrl& gurl,
650 GoogleString* error_out,
651 CacheLookupResultCallback* callback);
654 class DistributedRewriteCallback;
655 class DistributedRewriteFetch;
656 class OutputCacheCallback;
657 class WriteIfChanged;
658 class LookupMetadataForOutputResourceCallback;
659 class HTTPCacheCallback;
660 class ResourceCallbackUtils;
661 class ResourceFetchCallback;
662 class ResourceReconstructCallback;
663 class ResourceRevalidateCallback;
664 class InvokeRewriteFunction;
665 class RewriteFreshenCallback;
666 class TryLockFunction;
669 typedef std::set<RewriteContext*> ContextSet;
674 enum FallbackCondition {
675 kFallbackDiscretional,
683 void SetPartitionKey();
685 void StartFetchImpl();
687 void OutputCacheDone(CacheLookupResult* cache_result);
688 void OutputCacheHit(
bool write_partitions);
689 void OutputCacheRevalidate(
const InputInfoStarVector& to_revalidate);
690 void OutputCacheMiss();
691 void ResourceFetchDone(
bool success, ResourcePtr resource,
int slot_index);
692 void ResourceRevalidateDone(InputInfo* input_info,
bool success);
693 void LogMetadataCacheInfo(
bool cache_ok,
bool can_revalidate);
700 void RepeatedFailure();
717 GoogleString LockName()
const;
737 void DistributeRewriteDone(
bool success);
742 bool ParseAndRemoveMetadataFromResponseHeaders(
743 ResponseHeaders* response_headers, CacheLookupResult* cache_result);
747 bool CreateOutputResourceFromContent(
const CachedResult& cached_result,
748 const ResponseHeaders& response_headers,
750 OutputResourcePtr* output_resource);
761 GoogleString DistributedFetchUrl(StringPiece url);
765 bool IsFetchRewrite()
const {
return fetch_.get() != NULL; }
801 void Propagate(
bool render_slots);
809 void StartRewriteForHtml();
810 void StartRewriteForFetch();
818 bool ReadyToRewrite()
const;
828 void RunSuccessors();
832 void WritePartition();
843 void FinalizeRewriteForHtml();
850 void RetireRewriteForHtml(
bool permit_render);
863 void CollectDependentTopLevel(ContextSet* contexts);
867 void RewriteDoneImpl(
RewriteResult result,
int partition_index);
871 void StartNestedTasksImpl();
876 void RenderPartitionOnDetach(
int partition_index);
881 const OutputResourcePtr& output_resource,
883 MessageHandler* message_handler);
887 bool CreateOutputResourceForCachedOutput(
const CachedResult* cached_result,
888 OutputResourcePtr* output_resource);
891 void FetchCacheDone(CacheLookupResult* cache_result);
895 void FetchFallbackCacheDone(HTTPCache::FindResult result,
896 HTTPCache::Callback* data);
902 bool CanFetchFallbackToOriginal(FallbackCondition circumstance)
const;
906 bool HasDuplicateOtherDependency(
const InputInfo& input);
911 void CheckAndAddOtherDependency(
const InputInfo& input);
915 void CheckAndFreshenResource(
const InputInfo& input_info,
916 ResourcePtr resource,
int partition_index,
918 FreshenMetadataUpdateManager* freshen_manager);
919 ResourcePtr CreateUrlResource(
const StringPiece& input_url);
922 ResourceSlotVector slots_;
927 std::vector<bool> render_slots_;
941 scoped_ptr<OutputPartitions> partitions_;
942 OutputResourceVector outputs_;
943 int outstanding_fetches_;
944 int outstanding_rewrites_;
945 scoped_ptr<ResourceContext> resource_context_;
946 GoogleString partition_key_;
948 UrlSegmentEncoder default_encoder_;
952 scoped_ptr<NamedLock> lock_;
958 scoped_ptr<FetchContext> fetch_;
962 std::vector<RewriteContext*> successors_;
967 std::vector<RewriteContext*> repeated_;
972 int num_pending_nested_;
973 std::vector<RewriteContext*> nested_;
986 RewriteDriver* driver_;
989 int num_predecessors_;
1027 bool ok_to_write_output_partitions_;
1040 bool revalidate_ok_;
1044 bool notify_driver_on_fetch_done_;
1048 bool force_rewrite_;
1052 bool stale_rewrite_;
1056 bool is_metadata_cache_miss_;
1060 bool rewrite_uncacheable_;
1064 RequestTrace* dependent_request_trace_;
1068 bool block_distribute_rewrite_;
1071 scoped_ptr<DistributedRewriteFetch> distributed_fetch_;
1074 StringIntMap other_dependency_map_;
1079 scoped_ptr<ScheduleRewriteContext> schedule_rewrite_context_;
1081 Variable*
const num_rewrites_abandoned_for_lock_contention_;
1082 Variable*
const num_distributed_rewrite_failures_;
1083 Variable*
const num_distributed_rewrite_successes_;
1084 Variable*
const num_distributed_metadata_failures_;
RewriteContext * parent()
Allows a nested rewriter to walk up its parent hierarchy.
Definition: rewrite_context.h:274
static const char kDistributedExt[]
The extension used for all distributed fetch URLs.
Definition: rewrite_context.h:155
virtual bool Partition(OutputPartitions *partitions, OutputResourceVector *outputs)
bool stale_rewrite() const
Indicates whether we are serving a stale rewrite.
Definition: rewrite_context.h:608
bool IsDistributedRewriteForHtml() const
bool block_distribute_rewrite() const
Definition: rewrite_context.h:593
int num_outputs() const
Definition: rewrite_context.h:205
static bool LookupMetadataForOutputResourceImpl(OutputResourcePtr output_resource, const GoogleUrl &gurl, RewriteContext *rewrite_context, RewriteDriver *driver, GoogleString *error_out, CacheLookupResultCallback *callback)
void AddNestedContext(RewriteContext *context)
void ReleaseCreationLock(bool succeeded)
virtual OutputResourceKind kind() const =0
void ObtainLockForCreation(ServerContext *server_context, Function *callback)
virtual bool ScheduleViaCentralController()
Definition: rewrite_context.h:630
static void InitStats(Statistics *stats)
Initializes statistics.
virtual const UrlSegmentEncoder * encoder() const
Definition: rewrite_context.h:162
virtual GoogleString UserAgentCacheKey(const ResourceContext *context) const
Definition: rewrite_context.h:470
void Freshen()
Freshens resources proactively to avoid expiration in the near future.
int num_output_partitions() const
void TracePrintf(const char *fmt,...)
virtual bool DecodeFetchUrls(const OutputResourcePtr &output_resource, MessageHandler *message_handler, GoogleUrlStarVector *url_vector)
bool ShouldDistributeRewrite() const
bool IsNestedIn(StringPiece id) const
virtual bool FailOnHashMismatch() const
Definition: rewrite_context.h:625
void RewriteDone(RewriteResult result, int partition_index)
void PartitionDone(RewriteResult result)
bool Fetch(const OutputResourcePtr &output_resource, AsyncFetch *fetch, MessageHandler *message_handler)
ServerContext * FindServerContext() const
void CrossThreadPartitionDone(RewriteResult result)
Used for LookupMetadataForOutputResource.
Definition: rewrite_context.h:179
virtual const char * id() const =0
Returns the filter ID.
virtual bool OptimizationOnly() const
Definition: rewrite_context.h:327
Definition: async_fetch.h:53
virtual void StartFetchReconstruction()
int num_nested() const
Accessors for the nested rewrites.
Definition: rewrite_context.h:278
virtual void FixFetchFallbackHeaders(const CachedResult &cached_result, ResponseHeaders *headers)
RequestTrace * dependent_request_trace()
Definition: rewrite_context.h:505
void AddRecheckDependency()
Definition: rewrite_driver.h:98
virtual bool SendFallbackResponse(StringPiece output_url_base, StringPiece contents, AsyncFetch *async_fetch, MessageHandler *handler)
Definition: server_context.h:100
RewriteResult
Definition: rewrite_result.h:27
virtual GoogleString CacheKeySuffix() const
bool chained() const
Definition: rewrite_context.h:216
Definition: rewrite_context.h:146
int num_slots() const
Definition: rewrite_context.h:200
virtual bool CreationLockBeforeStartFetch() const
Should the context call LockForCreation before checking the cache?
virtual void Rewrite(int partition_index, CachedResult *partition, const OutputResourcePtr &output)=0
virtual void FetchCallbackDone(bool success)
virtual void EncodeUserAgentIntoResourceContext(ResourceContext *context)
Definition: rewrite_context.h:479
GoogleString ToString() const
Returns debug information about this RewriteContext.
static const char kDistributedHash[]
The hash value used for all distributed fetch URLs.
Definition: rewrite_context.h:157
virtual int64 GetRewriteDeadlineAlarmMs() const
bool slow() const
Definition: rewrite_context.h:261
bool is_metadata_cache_miss() const
This particular rewrite was a metadata cache miss.
Definition: rewrite_context.h:264
MessageHandler * fetch_message_handler()
The message handler for the fetch.
virtual void PartitionAsync(OutputPartitions *partitions, OutputResourceVector *outputs)
virtual void WillNotRender()
bool FetchContextDetached()
Is fetch_ detached? Only call this in the fetch flow.
RewriteContext(RewriteDriver *driver, RewriteContext *parent, ResourceContext *resource_context)
void AddSlot(const ResourceSlotPtr &slot)
virtual void FetchTryFallback(const GoogleString &url, const StringPiece &hash)
bool has_parent() const
Returns true if this is a nested rewriter.
Definition: rewrite_context.h:267
void set_force_rewrite(bool x)
If called with true, forces a rewrite and re-generates the output.
Definition: rewrite_context.h:286
void AttachDependentRequestTrace(const StringPiece &label)
OutputResourceKind
Definition: output_resource_kind.h:26
AsyncFetch * async_fetch()
Note that the following must only be called in the fetch flow.