~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/pagespeed/kernel/cache/async_cache.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
// Author: jmarantz@google.com (Joshua Marantz)
18
 
 
19
 
#ifndef PAGESPEED_KERNEL_CACHE_ASYNC_CACHE_H_
20
 
#define PAGESPEED_KERNEL_CACHE_ASYNC_CACHE_H_
21
 
 
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"
29
 
 
30
 
namespace net_instaweb {
31
 
 
32
 
class SharedString;
33
 
 
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.
37
 
//
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 {
41
 
 public:
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.
45
 
  //
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.
50
 
  //
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;
55
 
 
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
58
 
  // other users.
59
 
  //
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();
65
 
 
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; }
73
 
 
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.
77
 
  //
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();
82
 
 
83
 
  // Cancels all pending cache operations.  Puts and Deletes are dropped.
84
 
  // Gets and MultiGets are retired by calling their callbacks with
85
 
  // kNotFound.
86
 
  void CancelPendingOperations() { sequence_->CancelPendingFunctions(); }
87
 
 
88
 
  virtual bool IsHealthy() const {
89
 
    return !stopped_.value() && cache_->IsHealthy();
90
 
  }
91
 
 
92
 
  int32 outstanding_operations() { return outstanding_operations_.value(); }
93
 
 
94
 
 private:
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);
99
 
 
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);
104
 
 
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);
111
 
 
112
 
  void MultiGetReportNotFound(MultiGetRequest* request);
113
 
 
114
 
  CacheInterface* cache_;
115
 
  QueuedWorkerPool::Sequence* sequence_;
116
 
  AtomicBool stopped_;
117
 
  AtomicInt32 outstanding_operations_;
118
 
 
119
 
  DISALLOW_COPY_AND_ASSIGN(AsyncCache);
120
 
};
121
 
 
122
 
}  // namespace net_instaweb
123
 
 
124
 
#endif  // PAGESPEED_KERNEL_CACHE_ASYNC_CACHE_H_