]> git.ipfire.org Git - thirdparty/squid.git/blob - src/Store.h
Cleanup: Refactor ConnStateData pipeline handling
[thirdparty/squid.git] / src / Store.h
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef SQUID_STORE_H
10 #define SQUID_STORE_H
11
12 #include "base/Packable.h"
13 #include "base/RefCount.h"
14 #include "comm/forward.h"
15 #include "CommRead.h"
16 #include "hash.h"
17 #include "http/forward.h"
18 #include "http/RequestMethod.h"
19 #include "HttpReply.h"
20 #include "MemObject.h"
21 #include "Range.h"
22 #include "RemovalPolicy.h"
23 #include "store/Controller.h"
24 #include "store/forward.h"
25 #include "store_key_md5.h"
26 #include "StoreIOBuffer.h"
27 #include "StoreStats.h"
28
29 #if USE_SQUID_ESI
30 #include "esi/Element.h"
31 #endif
32
33 #include <ostream>
34
35 class AsyncCall;
36 class HttpRequest;
37 class RequestFlags;
38
39 extern StoreIoStats store_io_stats;
40
41 class StoreEntry : public hash_link, public Packable
42 {
43 MEMPROXY_CLASS(StoreEntry);
44
45 public:
46 static DeferredRead::DeferrableRead DeferReader;
47 bool checkDeferRead(int fd) const;
48
49 virtual const char *getMD5Text() const;
50 StoreEntry();
51 virtual ~StoreEntry();
52
53 virtual HttpReply const *getReply() const;
54 virtual void write (StoreIOBuffer);
55
56 /** Check if the Store entry is emtpty
57 * \retval true Store contains 0 bytes of data.
58 * \retval false Store contains 1 or more bytes of data.
59 * \retval false Store contains negative content !!!!!!
60 */
61 virtual bool isEmpty() const {
62 assert (mem_obj);
63 return mem_obj->endOffset() == 0;
64 }
65 virtual bool isAccepting() const;
66 virtual size_t bytesWanted(Range<size_t> const aRange, bool ignoreDelayPool = false) const;
67 /// flags [truncated or too big] entry with ENTRY_BAD_LENGTH and releases it
68 void lengthWentBad(const char *reason);
69 virtual void complete();
70 virtual store_client_t storeClientType() const;
71 virtual char const *getSerialisedMetaData();
72 /// Store a prepared error response. MemObject locks the reply object.
73 void storeErrorResponse(HttpReply *reply);
74 void replaceHttpReply(HttpReply *, bool andStartWriting = true);
75 void startWriting(); ///< pack and write reply headers and, maybe, body
76 /// whether we may start writing to disk (now or in the future)
77 virtual bool mayStartSwapOut();
78 virtual void trimMemory(const bool preserveSwappable);
79
80 // called when a decision to cache in memory has been made
81 void memOutDecision(const bool willCacheInRam);
82 // called when a decision to cache on disk has been made
83 void swapOutDecision(const MemObject::SwapOut::Decision &decision);
84
85 void abort();
86 void makePublic();
87 void makePrivate();
88 void setPublicKey();
89 void setPrivateKey();
90 void expireNow();
91 void releaseRequest();
92 void negativeCache();
93 void cacheNegatively(); /** \todo argh, why both? */
94 void invokeHandlers();
95 void purgeMem();
96 void cacheInMemory(); ///< start or continue storing in memory cache
97 void swapOut();
98 /// whether we are in the process of writing this entry to disk
99 bool swappingOut() const { return swap_status == SWAPOUT_WRITING; }
100 void swapOutFileClose(int how);
101 const char *url() const;
102 /// Satisfies cachability requirements shared among disk and RAM caches.
103 /// Encapsulates common checks of mayStartSwapOut() and memoryCachable().
104 /// TODO: Rename and make private so only those two methods can call this.
105 bool checkCachable();
106 int checkNegativeHit() const;
107 int locked() const;
108 int validToSend() const;
109 bool memoryCachable(); ///< checkCachable() and can be cached in memory
110
111 /// if needed, initialize mem_obj member w/o URI-related information
112 MemObject *makeMemObject();
113
114 /// initialize mem_obj member (if needed) and supply URI-related info
115 void createMemObject(const char *storeId, const char *logUri, const HttpRequestMethod &aMethod);
116
117 void dump(int debug_lvl) const;
118 void hashDelete();
119 void hashInsert(const cache_key *);
120 void registerAbort(STABH * cb, void *);
121 void reset();
122 void setMemStatus(mem_status_t);
123 void timestampsSet();
124 void unregisterAbort();
125 void destroyMemObject();
126 int checkTooSmall();
127
128 void delayAwareRead(const Comm::ConnectionPointer &conn, char *buf, int len, AsyncCall::Pointer callback);
129
130 void setNoDelay (bool const);
131 bool modifiedSince(HttpRequest * request) const;
132 /// has ETag matching at least one of the If-Match etags
133 bool hasIfMatchEtag(const HttpRequest &request) const;
134 /// has ETag matching at least one of the If-None-Match etags
135 bool hasIfNoneMatchEtag(const HttpRequest &request) const;
136 /// whether this entry has an ETag; if yes, puts ETag value into parameter
137 bool hasEtag(ETag &etag) const;
138
139 /// the disk this entry is [being] cached on; asserts for entries w/o a disk
140 Store::Disk &disk() const;
141
142 MemObject *mem_obj;
143 RemovalPolicyNode repl;
144 /* START OF ON-DISK STORE_META_STD TLV field */
145 time_t timestamp;
146 time_t lastref;
147 time_t expires;
148 time_t lastmod;
149 uint64_t swap_file_sz;
150 uint16_t refcount;
151 uint16_t flags;
152 /* END OF ON-DISK STORE_META_STD */
153
154 /// unique ID inside a cache_dir for swapped out entries; -1 for others
155 sfileno swap_filen:25; // keep in sync with SwapFilenMax
156
157 sdirno swap_dirn:7;
158
159 mem_status_t mem_status:3;
160
161 ping_status_t ping_status:3;
162
163 store_status_t store_status:3;
164
165 swap_status_t swap_status:3;
166
167 public:
168 static size_t inUseCount();
169 static void getPublicByRequestMethod(StoreClient * aClient, HttpRequest * request, const HttpRequestMethod& method);
170 static void getPublicByRequest(StoreClient * aClient, HttpRequest * request);
171 static void getPublic(StoreClient * aClient, const char *uri, const HttpRequestMethod& method);
172
173 virtual bool isNull() {
174 return false;
175 };
176
177 void setReleaseFlag();
178 #if USE_SQUID_ESI
179
180 ESIElement::Pointer cachedESITree;
181 #endif
182 virtual int64_t objectLen() const;
183 virtual int64_t contentLen() const;
184
185 /// claim shared ownership of this entry (for use in a given context)
186 /// matching lock() and unlock() contexts eases leak triage but is optional
187 void lock(const char *context);
188
189 /// disclaim shared ownership; may remove entry from store and delete it
190 /// returns remaning lock level (zero for unlocked and possibly gone entry)
191 int unlock(const char *context);
192
193 /// returns a local concurrent use counter, for debugging
194 int locks() const { return static_cast<int>(lock_count); }
195
196 /// update last reference timestamp and related Store metadata
197 void touch();
198
199 virtual void release();
200
201 #if USE_ADAPTATION
202 /// call back producer when more buffer space is available
203 void deferProducer(const AsyncCall::Pointer &producer);
204 /// calls back producer registered with deferProducer
205 void kickProducer();
206 #endif
207
208 /* Packable API */
209 virtual void append(char const *, int);
210 virtual void vappendf(const char *, va_list);
211 virtual void buffer();
212 virtual void flush();
213
214 protected:
215 void transientsAbandonmentCheck();
216
217 private:
218 bool checkTooBig() const;
219
220 static MemAllocator *pool;
221
222 unsigned short lock_count; /* Assume < 65536! */
223
224 #if USE_ADAPTATION
225 /// producer callback registered with deferProducer
226 AsyncCall::Pointer deferredProducer;
227 #endif
228
229 bool validLength() const;
230 bool hasOneOfEtags(const String &reqETags, const bool allowWeakMatch) const;
231 };
232
233 std::ostream &operator <<(std::ostream &os, const StoreEntry &e);
234
235 /// \ingroup StoreAPI
236 class NullStoreEntry:public StoreEntry
237 {
238
239 public:
240 static NullStoreEntry *getInstance();
241 bool isNull() {
242 return true;
243 }
244
245 const char *getMD5Text() const;
246 HttpReply const *getReply() const { return NULL; }
247 void write (StoreIOBuffer) {}
248
249 bool isEmpty () const {return true;}
250
251 virtual size_t bytesWanted(Range<size_t> const aRange, bool) const { return aRange.end; }
252
253 void operator delete(void *address);
254 void complete() {}
255
256 private:
257 store_client_t storeClientType() const {return STORE_MEM_CLIENT;}
258
259 char const *getSerialisedMetaData();
260 virtual bool mayStartSwapOut() { return false; }
261
262 void trimMemory(const bool) {}
263
264 static NullStoreEntry _instance;
265 };
266
267 /// \ingroup StoreAPI
268 typedef void (*STOREGETCLIENT) (StoreEntry *, void *cbdata);
269
270 namespace Store {
271 void Stats(StoreEntry *output);
272 void Maintain(void *unused);
273 };
274
275 /// \ingroup StoreAPI
276 size_t storeEntryInUse();
277
278 /// \ingroup StoreAPI
279 const char *storeEntryFlags(const StoreEntry *);
280
281 /// \ingroup StoreAPI
282 void storeEntryReplaceObject(StoreEntry *, HttpReply *);
283
284 /// \ingroup StoreAPI
285 StoreEntry *storeGetPublic(const char *uri, const HttpRequestMethod& method);
286
287 /// \ingroup StoreAPI
288 StoreEntry *storeGetPublicByRequest(HttpRequest * request);
289
290 /// \ingroup StoreAPI
291 StoreEntry *storeGetPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method);
292
293 /// \ingroup StoreAPI
294 /// Like storeCreatePureEntry(), but also locks the entry and sets entry key.
295 StoreEntry *storeCreateEntry(const char *, const char *, const RequestFlags &, const HttpRequestMethod&);
296
297 /// \ingroup StoreAPI
298 /// Creates a new StoreEntry with mem_obj and sets initial flags/states.
299 StoreEntry *storeCreatePureEntry(const char *storeId, const char *logUrl, const RequestFlags &, const HttpRequestMethod&);
300
301 /// \ingroup StoreAPI
302 void storeInit(void);
303
304 /// \ingroup StoreAPI
305 void storeConfigure(void);
306
307 /// \ingroup StoreAPI
308 void storeFreeMemory(void);
309
310 /// \ingroup StoreAPI
311 int expiresMoreThan(time_t, time_t);
312
313 /// \ingroup StoreAPI
314 void storeAppendPrintf(StoreEntry *, const char *,...) PRINTF_FORMAT_ARG2;
315
316 /// \ingroup StoreAPI
317 void storeAppendVPrintf(StoreEntry *, const char *, va_list ap);
318
319 /// \ingroup StoreAPI
320 int storeTooManyDiskFilesOpen(void);
321
322 /// \ingroup StoreAPI
323 void storeHeapPositionUpdate(StoreEntry *, SwapDir *);
324
325 /// \ingroup StoreAPI
326 void storeSwapFileNumberSet(StoreEntry * e, sfileno filn);
327
328 /// \ingroup StoreAPI
329 void storeFsInit(void);
330
331 /// \ingroup StoreAPI
332 void storeFsDone(void);
333
334 /// \ingroup StoreAPI
335 void storeReplAdd(const char *, REMOVALPOLICYCREATE *);
336
337 /// \ingroup StoreAPI
338 extern FREE destroyStoreEntry;
339
340 /// \ingroup StoreAPI
341 void storeGetMemSpace(int size);
342
343 #endif /* SQUID_STORE_H */
344