Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cache_batcher.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012 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_CACHE_BATCHER_H_
20 #define PAGESPEED_KERNEL_CACHE_CACHE_BATCHER_H_
21 
22 #include <cstddef>
23 
24 #include <unordered_map>
25 #include <vector>
26 
33 #include "pagespeed/kernel/base/thread_annotations.h"
34 #include "pagespeed/kernel/cache/cache_interface.h"
35 
36 namespace net_instaweb {
37 
38 class Statistics;
39 class Variable;
40 
54 class CacheBatcher : public CacheInterface {
55  public:
67  static const int kDefaultMaxParallelLookups = 1;
68 
73  static const size_t kDefaultMaxPendingGets = 1000;
74 
75  struct Options {
76  Options()
77  : max_parallel_lookups(kDefaultMaxParallelLookups),
78  max_pending_gets(kDefaultMaxPendingGets) {
79  }
80 
81  int max_parallel_lookups;
82  int max_pending_gets;
84  };
85 
87  CacheBatcher(const Options& options, CacheInterface* cache,
88  AbstractMutex* mutex, Statistics* statistics);
89  virtual ~CacheBatcher();
90 
94  static void InitStats(Statistics* statistics);
95 
96  virtual void Get(const GoogleString& key, Callback* callback);
97  virtual void Put(const GoogleString& key, const SharedString& value);
98  virtual void Delete(const GoogleString& key);
99  virtual GoogleString Name() const;
100  static GoogleString FormatName(StringPiece cache, int parallelism, int max);
101 
104  virtual bool IsBlocking() const { return cache_->IsBlocking(); }
105 
106  virtual bool IsHealthy() const { return cache_->IsHealthy(); }
107  virtual void ShutDown();
108 
109  private:
110  typedef std::unordered_map<GoogleString, std::vector<Callback*>> CallbackMap;
111 
112  class Group;
113  class MultiCallback;
114 
115  bool CanIssueGet() const EXCLUSIVE_LOCKS_REQUIRED(mutex_);
116  bool CanQueueCallback() const EXCLUSIVE_LOCKS_REQUIRED(mutex_);
117  void GroupComplete();
118 
119  MultiGetRequest* ConvertMapToRequest(const CallbackMap& map)
120  EXCLUSIVE_LOCKS_REQUIRED(mutex_);
121  MultiGetRequest* CreateRequestForQueuedKeys()
122  EXCLUSIVE_LOCKS_REQUIRED(mutex_);
123  void MoveQueuedKeys() EXCLUSIVE_LOCKS_REQUIRED(mutex_);
124  void ExtractInFlightKeys(const GoogleString& key,
125  std::vector<CacheInterface::Callback*>* callbacks)
126  LOCKS_EXCLUDED(mutex_);
127 
128  void DecrementInFlightGets(int n) LOCKS_EXCLUDED(mutex_);
129 
132  int last_batch_size() const LOCKS_EXCLUDED(mutex_);
133  int num_in_flight_keys() LOCKS_EXCLUDED(mutex_);
134 
135  CacheInterface* cache_;
136  Variable* dropped_gets_;
137  Variable* coalesced_gets_;
138  Variable* queued_gets_;
139  CallbackMap in_flight_ GUARDED_BY(mutex_);
140  int last_batch_size_ GUARDED_BY(mutex_);
141  scoped_ptr<AbstractMutex> mutex_;
142  int num_in_flight_groups_ GUARDED_BY(mutex_);
143  int num_in_flight_keys_ GUARDED_BY(mutex_);
144  int num_pending_gets_ GUARDED_BY(mutex_);
145  const Options options_;
146  CallbackMap queued_ GUARDED_BY(mutex_);
147  bool shutdown_ GUARDED_BY(mutex_);
148 
149 
150 };
151 
152 }
153 
154 #endif
Definition: cache_batcher_testing_peer.h:28
Abstract interface for a cache.
Definition: cache_interface.h:32
virtual void Put(const GoogleString &key, const SharedString &value)
Definition: statistics.h:43
static const size_t kDefaultMaxPendingGets
Definition: cache_batcher.h:73
Base class for implementations of monitoring statistics.
Definition: statistics.h:342
Abstract interface for implementing a mutex.
Definition: abstract_mutex.h:28
virtual GoogleString Name() const
static void InitStats(Statistics *statistics)
virtual void Get(const GoogleString &key, Callback *callback)
Definition: scoped_ptr.h:30
Definition: cache_batcher.h:54
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
CacheBatcher(const Options &options, CacheInterface *cache, AbstractMutex *mutex, Statistics *statistics)
Does not take ownership of the cache. Takes ownership of the mutex.
virtual bool IsHealthy() const
Definition: cache_batcher.h:106
virtual bool IsHealthy() const =0
Definition: cache_batcher.h:75
virtual bool IsBlocking() const =0
static const int kDefaultMaxParallelLookups
Definition: cache_batcher.h:67
Definition: shared_string.h:40
virtual bool IsBlocking() const
Definition: cache_batcher.h:104
Definition: cache_interface.h:42