Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
popularity_contest_schedule_rewrite_controller.h
Go to the documentation of this file.
1 // Copyright 2016 Google Inc.
16 
17 #ifndef PAGESPEED_CONTROLLER_POPULARITY_CONTEST_SCHEDULE_REWRITE_CONTROLLER_H_
18 #define PAGESPEED_CONTROLLER_POPULARITY_CONTEST_SCHEDULE_REWRITE_CONTROLLER_H_
19 
20 #include <cstddef>
21 #include <unordered_map>
22 
32 #include "pagespeed/kernel/base/thread_annotations.h"
34 
76 
77 namespace net_instaweb {
78 
80  : public ScheduleRewriteController {
81  public:
82  static const char kNumRewritesRequested[];
83  static const char kNumRewritesSucceeded[];
84  static const char kNumRewritesFailed[];
85  static const char kNumRewritesRejectedQueueSize[];
86  static const char kNumRewritesRejectedInProgress[];
87  static const char kRewriteQueueSize[];
88  static const char kNumRewritesRunning[];
89  static const char kNumRewritesAwaitingRetry[];
90 
95  Statistics* statistics,
96  Timer* timer,
97  int max_running_rewrites,
98  int max_queued_rewrites);
100 
102  void ScheduleRewrite(const GoogleString& key, Function* callback) override;
103  void NotifyRewriteComplete(const GoogleString& key) override;
104  void NotifyRewriteFailed(const GoogleString& key) override;
105 
106  static void InitStats(Statistics* stats);
107 
108  private:
109  enum RewriteState {
110  STOPPED,
111  QUEUED,
112  RUNNING,
113  AWAITING_RETRY,
114  };
115 
116  struct Rewrite {
117  Rewrite(const GoogleString& k)
118  : key(k), saved_priority(0), callback(nullptr), state(STOPPED) {}
119  GoogleString key;
120  int saved_priority;
121  Function* callback;
122  RewriteState state;
123  };
124 
125  struct StringPtrHash {
126  size_t operator()(const GoogleString* x) const {
127  return std::hash<GoogleString>()(*x);
128  }
129  };
130 
131  struct StringPtrEq {
132  bool operator()(const GoogleString* a, const GoogleString* b) const {
133  return (a == b || *a == *b);
134  }
135  };
136 
137  typedef std::unordered_map<const GoogleString*, Rewrite*,
138  StringPtrHash, StringPtrEq>
139  RewriteMap;
140 
144  Function* AttemptStartRewrite()
145  EXCLUSIVE_LOCKS_REQUIRED(mutex_) WARN_UNUSED_RESULT;
146 
150  Function* StartRewrite(Rewrite* rewrite)
151  EXCLUSIVE_LOCKS_REQUIRED(mutex_) WARN_UNUSED_RESULT;
152 
154  void StopRewrite(Rewrite* rewrite) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
155 
158  void SaveRewriteForRetry(Rewrite* rewrite) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
159 
162  void ConsiderDroppingRetry() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
163 
167  Rewrite* GetRewrite(const GoogleString& key) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
168 
170  void DeleteRewrite(const Rewrite* rewrite) EXCLUSIVE_LOCKS_REQUIRED(mutex_);
171 
173  void SetMaxQueueSizeForTesting(int size) LOCKS_EXCLUDED(mutex_);
174 
178  RewriteMap all_rewrites_ GUARDED_BY(mutex_);
180  PriorityQueue<Rewrite*> queue_ GUARDED_BY(mutex_);
181 
184  PriorityQueue<Rewrite*> retry_queue_ GUARDED_BY(mutex_);
185 
186  Timer* timer_;
187 
188  int running_rewrites_ GUARDED_BY(mutex_);
189  const int max_running_rewrites_;
191  int max_queued_rewrites_ GUARDED_BY(mutex_);
192 
193  TimedVariable* num_rewrite_requests_;
194  TimedVariable* num_rewrites_succeeded_;
195  TimedVariable* num_rewrites_failed_;
196  TimedVariable* num_rewrites_rejected_queue_size_;
197  TimedVariable* num_rewrites_rejected_in_progress_;
198  UpDownCounter* queue_size_;
199  UpDownCounter* num_rewrites_running_;
200  UpDownCounter* num_rewrites_awaiting_retry_;
201 
202  friend class PopularityContestScheduleRewriteControllerTest;
203 
204 
205 };
206 
207 }
208 
209 #endif
Definition: priority_queue.h:37
Definition: popularity_contest_schedule_rewrite_controller.h:79
PopularityContestScheduleRewriteController(ThreadSystem *thread_system, Statistics *statistics, Timer *timer, int max_running_rewrites, int max_queued_rewrites)
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
void ScheduleRewrite(const GoogleString &key, Function *callback) override
ScheduleRewriteController interface.
void NotifyRewriteComplete(const GoogleString &key) override
Definition: scoped_ptr.h:30
Definition: function.h:47
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: thread_system.h:40
Definition: statistics.h:73
Definition: statistics.h:298
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
Definition: schedule_rewrite_controller.h:30