Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
purge_context.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_CACHE_PURGE_CONTEXT_H_
20 #define PAGESPEED_KERNEL_CACHE_PURGE_CONTEXT_H_
21 
22 #include <vector>
23 
32 
33 namespace net_instaweb {
34 
35 class AbstractMutex;
36 class FileSystem;
37 class MessageHandler;
38 class NamedLock;
39 class NamedLockManager;
40 class Scheduler;
41 class Statistics;
42 class ThreadSystem;
43 class UpDownCounter;
44 class Variable;
45 
54 class PurgeContext {
55  public:
58 
63  static const int kCheckCacheIntervalMs = 5 * Timer::kSecondMs;
64 
66  static const char kCancellations[];
67  static const char kContentions[];
68  static const char kFileParseFailures[];
69  static const char kFileStats[];
70  static const char kFileWriteFailures[];
71  static const char kFileWrites[];
72  static const char kPurgeIndex[];
73  static const char kPurgePollTimestampMs[];
74  static const char kStatCalls[];
75 
76  PurgeContext(StringPiece filename,
77  FileSystem* file_system,
78  Timer* timer,
79  int max_bytes_in_cache,
80  ThreadSystem* thread_system,
81  NamedLockManager* lock_manager,
82  Scheduler* scheduler,
83  Statistics* statistics,
84  MessageHandler* handler);
85  ~PurgeContext();
86 
87  static void InitStats(Statistics* statistics);
88 
96  void set_request_batching_delay_ms(int64 delay_ms) {
97  request_batching_delay_ms_ = delay_ms;
98  }
99 
108  void AddPurgeUrl(StringPiece url, int64 timestamp_ms,
109  PurgeCallback* callback);
110 
117  void SetCachePurgeGlobalTimestampMs(int64 timestamp_ms,
118  PurgeCallback* callback);
119 
122  void PollFileSystem();
123 
129  void SetUpdateCallback(PurgeSetCallback* cb);
130 
136  void set_enable_purge(bool x) { enable_purge_ = x; }
137 
138  private:
139  friend class PurgeContextTest;
140 
141  typedef std::vector<PurgeCallback*> PurgeCallbackVector;
142 
146  void UpdateCachePurgeFile();
147 
155  void ReadPurgeFile(PurgeSet* purges_from_file);
156  void ReadFileAndCallCallbackIfChanged(bool needs_update);
157 
181  void ModifyPurgeSet(PurgeSet* purges_from_file, GoogleString* buffer,
182  PurgeCallbackVector* return_callbacks,
183  PurgeSet* return_purges,
184  int* failures);
185 
189  void HandleWriteFailure(int failures,
190  PurgeCallbackVector* callbacks,
191  PurgeSet* return_purges,
192  bool* lock_and_update);
193 
197  bool WritePurgeFile(const GoogleString& buffer);
198 
200  bool Verify(const GoogleString& expected_purge_file_contents);
201 
204  GoogleString LockName() const { return StrCat(filename_, "-lock"); }
205 
209  void WaitForTimerAndGrabLock();
210 
214  void GrabLockAndUpdate();
215 
219  void CancelCachePurgeFile();
220 
226  bool ParseAndValidateTimestamp(StringPiece time_string, int64 now_ms,
227  int64* timestamp_ms);
228 
229  GoogleString filename_;
230  scoped_ptr<NamedLock> interprocess_lock_;
231  FileSystem* file_system_;
232  Timer* timer_;
233 
234  Statistics* statistics_;
235  scoped_ptr<AbstractMutex> mutex_;
236  CopyOnWrite<PurgeSet> purge_set_;
237  PurgeSet pending_purges_;
238  PurgeCallbackVector pending_callbacks_;
239  int64 local_purge_index_;
240  int num_consecutive_failures_;
241  bool waiting_for_interprocess_lock_;
242  bool reading_;
243 
244  bool enable_purge_;
245  int max_bytes_in_cache_;
246 
247  int64 request_batching_delay_ms_;
248 
249  Variable* cancellations_;
250  Variable* contentions_;
251  Variable* file_parse_failures_;
252  Variable* file_stats_;
253  Variable* file_write_failures_;
254  Variable* file_writes_;
255  Variable* purge_index_;
256  scoped_ptr<UpDownCounter> purge_poll_timestamp_ms_;
257 
258  Scheduler* scheduler_;
259  MessageHandler* message_handler_;
260 
261  scoped_ptr<PurgeSetCallback> update_callback_;
262 
263 
264 };
265 
266 }
267 
268 #endif
void set_request_batching_delay_ms(int64 delay_ms)
Definition: purge_context.h:96
Definition: callback.h:167
Definition: named_lock_manager.h:82
void SetUpdateCallback(PurgeSetCallback *cb)
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
static const int64 kSecondMs
Definition: timer.h:34
void AddPurgeUrl(StringPiece url, int64 timestamp_ms, PurgeCallback *callback)
Definition: purge_set.h:44
static const char kCancellations[]
Variable names.
Definition: purge_context.h:66
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Definition: file_system.h:76
static const int kCheckCacheIntervalMs
Definition: purge_context.h:63
Definition: thread_system.h:40
Definition: message_handler.h:39
void SetCachePurgeGlobalTimestampMs(int64 timestamp_ms, PurgeCallback *callback)
Definition: scheduler.h:47
Definition: purge_context.h:54
void set_enable_purge(bool x)
Definition: purge_context.h:136
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
Definition: callback.h:44