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"
36 #include "net/instaweb/util/public/url_segment_encoder.h"
38 namespace net_instaweb {
45 class ResponseHeaders;
50 class FreshenMetadataUpdateManager;
146 typedef std::vector<InputInfo*> InputInfoStarVector;
147 static const char kNumRewritesAbandonedForLockContention[];
148 static const char kNumDeadlineAlarmInvocations[];
149 static const char kNumDistributedRewriteSuccesses[];
150 static const char kNumDistributedRewriteFailures[];
151 static const char kNumDistributedMetadataFailures[];
161 can_revalidate(
false),
162 useable_cache_content(
false),
163 is_stale_rewrite(
false),
164 partitions(
new OutputPartitions) {}
168 bool useable_cache_content;
169 bool is_stale_rewrite;
170 InputInfoStarVector revalidate;
171 scoped_ptr<OutputPartitions> partitions;
179 virtual void Done(
const GoogleString& cache_key,
189 ResourceContext* resource_context);
197 ResourceSlotPtr slot(
int index)
const {
return slots_[index]; }
202 OutputResourcePtr output(
int i)
const {
return outputs_[i]; }
207 const CachedResult* output_partition(
int i)
const;
208 CachedResult* output_partition(
int i);
217 void AddSlot(
const ResourceSlotPtr& slot);
247 bool Fetch(
const OutputResourcePtr& output_resource,
249 MessageHandler* message_handler);
253 bool slow()
const {
return slow_; }
272 bool rewrite_uncacheable()
const {
return rewrite_uncacheable_; }
273 void set_rewrite_uncacheable(
bool rewrite_uncacheable) {
274 rewrite_uncacheable_ = rewrite_uncacheable;
277 const ResourceContext* resource_context()
const {
278 return resource_context_.get();
282 GoogleString
ToString(StringPiece prefix)
const;
285 static void InitStats(Statistics* stats);
288 typedef std::vector<GoogleUrl*> GoogleUrlStarVector;
299 const RewriteOptions* Options()
const;
300 RewriteDriver* Driver()
const {
308 OutputPartitions* partitions() {
return partitions_.get(); }
336 virtual bool Partition(OutputPartitions* partitions,
337 OutputResourceVector* outputs);
345 OutputResourceVector* outputs);
374 virtual void Rewrite(
int partition_index,
375 CachedResult* partition,
376 const OutputResourcePtr& output) = 0;
394 StringPiece contents,
396 MessageHandler* handler);
448 virtual const UrlSegmentEncoder*
encoder()
const;
461 const ResourceContext* context)
const {
472 virtual const char*
id()
const = 0;
548 MessageHandler* message_handler,
549 GoogleUrlStarVector* url_vector);
556 ResponseHeaders* headers);
567 const StringPiece& hash);
572 bool notify_driver_on_fetch_done()
const {
573 return notify_driver_on_fetch_done_;
575 void set_notify_driver_on_fetch_done(
bool value) {
576 notify_driver_on_fetch_done_ = value;
584 void set_block_distribute_rewrite(
const bool x) {
585 block_distribute_rewrite_ = x;
612 OutputResourcePtr output_resource,
613 const GoogleUrl& gurl,
616 GoogleString* error_out,
617 CacheLookupResultCallback* callback);
620 class DistributedRewriteCallback;
621 class DistributedRewriteFetch;
622 class OutputCacheCallback;
623 class LookupMetadataForOutputResourceCallback;
624 class HTTPCacheCallback;
625 class ResourceCallbackUtils;
626 class ResourceFetchCallback;
627 class ResourceReconstructCallback;
628 class ResourceRevalidateCallback;
629 class InvokeRewriteFunction;
630 class RewriteFreshenCallback;
633 typedef std::set<RewriteContext*> ContextSet;
638 enum FallbackCondition {
639 kFallbackDiscretional,
647 void SetPartitionKey();
649 void StartFetchImpl();
651 void OutputCacheDone(CacheLookupResult* cache_result);
652 void OutputCacheHit(
bool write_partitions);
653 void OutputCacheRevalidate(
const InputInfoStarVector& to_revalidate);
654 void OutputCacheMiss();
655 void ResourceFetchDone(
bool success, ResourcePtr resource,
int slot_index);
656 void ResourceRevalidateDone(InputInfo* input_info,
bool success);
657 void LogMetadataCacheInfo(
bool cache_ok,
bool can_revalidate);
664 void RepeatedFailure();
694 void DistributeRewriteDone(
bool success);
699 bool ParseAndRemoveMetadataFromResponseHeaders(
700 ResponseHeaders* response_headers, CacheLookupResult* cache_result);
704 bool CreateOutputResourceFromContent(
const CachedResult& cached_result,
705 const ResponseHeaders& response_headers,
707 OutputResourcePtr* output_resource);
718 GoogleString DistributedFetchUrl(StringPiece url);
722 bool IsFetchRewrite()
const {
return fetch_.get() != NULL; }
758 void Propagate(
bool render_slots);
766 void StartRewriteForHtml();
767 void StartRewriteForFetch();
775 bool ReadyToRewrite()
const;
785 void RunSuccessors();
789 void WritePartition();
800 void FinalizeRewriteForHtml();
807 void RetireRewriteForHtml(
bool permit_render);
820 void CollectDependentTopLevel(ContextSet* contexts);
824 void RewriteDoneImpl(
RewriteResult result,
int partition_index);
828 void StartNestedTasksImpl();
833 void RenderPartitionOnDetach(
int partition_index);
838 const OutputResourcePtr& output_resource,
840 MessageHandler* message_handler);
844 bool CreateOutputResourceForCachedOutput(
const CachedResult* cached_result,
845 OutputResourcePtr* output_resource);
848 void FetchCacheDone(CacheLookupResult* cache_result);
853 HTTPCache::Callback* data);
859 bool CanFetchFallbackToOriginal(FallbackCondition circumstance)
const;
863 bool HasDuplicateOtherDependency(
const InputInfo& input);
868 void CheckAndAddOtherDependency(
const InputInfo& input);
872 void CheckAndFreshenResource(
const InputInfo& input_info,
873 ResourcePtr resource,
int partition_index,
875 FreshenMetadataUpdateManager* freshen_manager);
876 ResourcePtr CreateUrlResource(
const StringPiece& input_url);
879 ResourceSlotVector slots_;
884 std::vector<bool> render_slots_;
898 scoped_ptr<OutputPartitions> partitions_;
899 OutputResourceVector outputs_;
900 int outstanding_fetches_;
901 int outstanding_rewrites_;
902 scoped_ptr<ResourceContext> resource_context_;
903 GoogleString partition_key_;
905 UrlSegmentEncoder default_encoder_;
909 scoped_ptr<NamedLock> lock_;
915 scoped_ptr<FetchContext> fetch_;
919 std::vector<RewriteContext*> successors_;
924 std::vector<RewriteContext*> repeated_;
929 int num_pending_nested_;
930 std::vector<RewriteContext*> nested_;
943 RewriteDriver* driver_;
946 int num_predecessors_;
984 bool ok_to_write_output_partitions_;
1001 bool notify_driver_on_fetch_done_;
1005 bool force_rewrite_;
1009 bool stale_rewrite_;
1013 bool is_metadata_cache_miss_;
1017 bool rewrite_uncacheable_;
1021 RequestTrace* dependent_request_trace_;
1025 bool block_distribute_rewrite_;
1028 scoped_ptr<DistributedRewriteFetch> distributed_fetch_;
1031 StringIntMap other_dependency_map_;
1033 Variable*
const num_rewrites_abandoned_for_lock_contention_;
1034 Variable*
const num_distributed_rewrite_failures_;
1035 Variable*
const num_distributed_rewrite_successes_;
1036 Variable*
const num_distributed_metadata_failures_;
RewriteContext * parent()
Allows a nested rewriter to walk up its parent hierarchy.
Definition: rewrite_context.h:266
static const char kDistributedExt[]
The extension used for all distributed fetch URLs.
Definition: rewrite_context.h:153
virtual bool Partition(OutputPartitions *partitions, OutputResourceVector *outputs)
bool stale_rewrite() const
Indicates whether we are serving a stale rewrite.
Definition: rewrite_context.h:598
bool IsDistributedRewriteForHtml() const
bool block_distribute_rewrite() const
Definition: rewrite_context.h:583
int num_outputs() const
Definition: rewrite_context.h:201
static bool LookupMetadataForOutputResourceImpl(OutputResourcePtr output_resource, const GoogleUrl &gurl, RewriteContext *rewrite_context, RewriteDriver *driver, GoogleString *error_out, CacheLookupResultCallback *callback)
void AddNestedContext(RewriteContext *context)
virtual OutputResourceKind kind() const =0
static void InitStats(Statistics *stats)
Initializes statistics.
virtual const UrlSegmentEncoder * encoder() const
FindResult
When a lookup is done in the HTTP Cache, it returns one of these values.
Definition: http_cache.h:71
Definition: rewrite_context.h:158
virtual GoogleString UserAgentCacheKey(const ResourceContext *context) const
Definition: rewrite_context.h:460
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
void RewriteDone(RewriteResult result, int partition_index)
GoogleString ToString(StringPiece prefix) const
Returns debug information about this RewriteContext.
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:175
virtual const char * id() const =0
Returns the filter ID.
virtual bool OptimizationOnly() const
Definition: rewrite_context.h:317
Definition: async_fetch.h:53
virtual void StartFetchReconstruction()
int num_nested() const
Accessors for the nested rewrites.
Definition: rewrite_context.h:305
virtual void FixFetchFallbackHeaders(const CachedResult &cached_result, ResponseHeaders *headers)
RequestTrace * dependent_request_trace()
Definition: rewrite_context.h:495
void AddRecheckDependency()
Definition: rewrite_driver.h:98
virtual bool SendFallbackResponse(StringPiece output_url_base, StringPiece contents, AsyncFetch *async_fetch, MessageHandler *handler)
RewriteResult
Definition: rewrite_result.h:27
virtual GoogleString CacheKeySuffix() const
bool chained() const
Definition: rewrite_context.h:212
Definition: rewrite_context.h:144
int num_slots() const
Definition: rewrite_context.h:196
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:469
static const char kDistributedHash[]
The hash value used for all distributed fetch URLs.
Definition: rewrite_context.h:155
virtual int64 GetRewriteDeadlineAlarmMs() const
bool slow() const
Definition: rewrite_context.h:253
bool is_metadata_cache_miss() const
This particular rewrite was a metadata cache miss.
Definition: rewrite_context.h:256
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:259
void set_force_rewrite(bool x)
If called with true, forces a rewrite and re-generates the output.
Definition: rewrite_context.h:270
void AttachDependentRequestTrace(const StringPiece &label)
virtual bool CreationLockBeforeStartFetch()
Should the context call LockForCreation before checking the cache?
Definition: rewrite_context.h:606
OutputResourceKind
Definition: output_resource_kind.h:26
AsyncFetch * async_fetch()
Note that the following must only be called in the fetch flow.