2
* Copyright 2012 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
// Author: jmarantz@google.com (Joshua Marantz)
19
#ifndef PAGESPEED_KERNEL_CACHE_ASYNC_CACHE_H_
20
#define PAGESPEED_KERNEL_CACHE_ASYNC_CACHE_H_
22
#include "pagespeed/kernel/base/atomic_bool.h"
23
#include "pagespeed/kernel/base/atomic_int32.h"
24
#include "pagespeed/kernel/base/basictypes.h"
25
#include "pagespeed/kernel/base/string.h"
26
#include "pagespeed/kernel/base/string_util.h"
27
#include "pagespeed/kernel/cache/cache_interface.h"
28
#include "pagespeed/kernel/thread/queued_worker_pool.h"
30
namespace net_instaweb {
34
// Employs a QueuedWorkerPool to turn a synchronous cache implementation
35
// into an asynchronous one. This makes sense to do only if the cache
36
// implemention is potentially slow, due to network latency or disk seek time.
38
// This class also serves to serialize access to the passed-in cache, ensuring
39
// that it is accessed from only one thread at a time.
40
class AsyncCache : public CacheInterface {
42
// The maximum number of operations that can be queued up while a
43
// server is slow. When this is reached, old Deletes/Puts get
44
// dropped, and old Gets are retired with a kNotFound.
46
// This helps bound the amount of memory consumed by queued operations
47
// when the cache gets wedged. Note that when CacheBatcher is layered
48
// above AsyncCache, it will queue up its Gets at a level above this one,
49
// and ultimately send those using a MultiGet.
51
// TODO(jmarantz): Analyze whether we drop operations under load with
52
// a non-wedged cache. If it looks like we are dropping Puts the first
53
// time we encounter a page then I think we may need to bump this up.
54
static const int64 kMaxQueueSize = 2000;
56
// Does not takes ownership of the synchronous cache that is passed in.
57
// Does not take ownership of the pool, which might be shared with
60
// Note that in the future we may try to add multi-threaded access
61
// to the underlying cache (e.g. AprMemCache supports this), so we
62
// take the pool as the constructor arg.
63
AsyncCache(CacheInterface* cache, QueuedWorkerPool* pool);
64
virtual ~AsyncCache();
66
virtual void Get(const GoogleString& key, Callback* callback);
67
virtual void Put(const GoogleString& key, SharedString* value);
68
virtual void Delete(const GoogleString& key);
69
virtual void MultiGet(MultiGetRequest* request);
70
static GoogleString FormatName(StringPiece cache);
71
virtual GoogleString Name() const { return FormatName(cache_->Name()); }
72
virtual bool IsBlocking() const { return false; }
74
// Prevent the AsyncCache from issuing any more Gets. Any subsequent
75
// Gets will have their callback invoked immediately with kNotFound.
76
// Outstanding Gets may be completed depending on timing.
78
// This can be called during the process Shutdown flow to avoid
79
// introducing more work asynchronously that will have to be
80
// completed prior to Shutdown.
81
virtual void ShutDown();
83
// Cancels all pending cache operations. Puts and Deletes are dropped.
84
// Gets and MultiGets are retired by calling their callbacks with
86
void CancelPendingOperations() { sequence_->CancelPendingFunctions(); }
88
virtual bool IsHealthy() const {
89
return !stopped_.value() && cache_->IsHealthy();
92
int32 outstanding_operations() { return outstanding_operations_.value(); }
95
// Function to execute a single-key Get in sequence_. Canceling
96
// a Get calls the callback with kNotFound.
97
void DoGet(GoogleString* key, Callback* callback);
98
void CancelGet(GoogleString* key, Callback* callback);
100
// Function to execute a multi-key Get in sequence_. Canceling
101
// a MultiGet calls all the callbacks with kNotFound.
102
void DoMultiGet(MultiGetRequest* request);
103
void CancelMultiGet(MultiGetRequest* request);
105
// Functions to execute Put/Delete in sequence_. Canceling
106
// a Put/Delete just drops the request.
107
void DoPut(GoogleString* key, SharedString* value);
108
void CancelPut(GoogleString* key, SharedString* value);
109
void DoDelete(GoogleString* key);
110
void CancelDelete(GoogleString* key);
112
void MultiGetReportNotFound(MultiGetRequest* request);
114
CacheInterface* cache_;
115
QueuedWorkerPool::Sequence* sequence_;
117
AtomicInt32 outstanding_operations_;
119
DISALLOW_COPY_AND_ASSIGN(AsyncCache);
122
} // namespace net_instaweb
124
#endif // PAGESPEED_KERNEL_CACHE_ASYNC_CACHE_H_