]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 1996-2023 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_SRC_MEMOBJECT_H | |
10 | #define SQUID_SRC_MEMOBJECT_H | |
11 | ||
12 | #include "base/DelayedAsyncCalls.h" | |
13 | #include "dlink.h" | |
14 | #include "http/RequestMethod.h" | |
15 | #include "HttpReply.h" | |
16 | #include "RemovalPolicy.h" | |
17 | #include "sbuf/SBuf.h" | |
18 | #include "stmem.h" | |
19 | #include "store/forward.h" | |
20 | #include "StoreIOBuffer.h" | |
21 | #include "StoreIOState.h" | |
22 | #include "typedefs.h" //for IRCB | |
23 | ||
24 | #if USE_DELAY_POOLS | |
25 | #include "DelayId.h" | |
26 | #endif | |
27 | ||
28 | typedef void STMCB (void *data, StoreIOBuffer wroteBuffer); | |
29 | ||
30 | class store_client; | |
31 | class PeerSelector; | |
32 | ||
33 | class MemObject | |
34 | { | |
35 | MEMPROXY_CLASS(MemObject); | |
36 | ||
37 | public: | |
38 | static size_t inUseCount(); | |
39 | ||
40 | void dump() const; | |
41 | MemObject(); | |
42 | ~MemObject(); | |
43 | ||
44 | /// Sets store ID, log URI, and request method (unless already set). Does | |
45 | /// not clobber the method so that, say, a HEAD hit for a GET entry keeps | |
46 | /// the GET method that matches the entry key. Same for the other parts of | |
47 | /// the trio because the entry filling code may expect them to be constant. | |
48 | /// XXX: Avoid this method. We plan to remove it and make the trio constant | |
49 | /// after addressing the XXX in MemStore::get(). | |
50 | void setUris(char const *aStoreId, char const *aLogUri, const HttpRequestMethod &aMethod); | |
51 | ||
52 | /// whether setUris() has been called | |
53 | bool hasUris() const; | |
54 | ||
55 | void write(const StoreIOBuffer &buf); | |
56 | void unlinkRequest() { request = nullptr; } | |
57 | ||
58 | /// HTTP response before 304 (Not Modified) updates | |
59 | /// starts "empty"; modified via replaceBaseReply() or adjustableBaseReply() | |
60 | const HttpReply &baseReply() const { return *reply_; } | |
61 | ||
62 | /// \returns nil -- if no 304 updates since replaceBaseReply() | |
63 | /// \returns combination of baseReply() and 304 updates -- after updates | |
64 | const HttpReplyPointer &updatedReply() const { return updatedReply_; } | |
65 | ||
66 | /// \returns the updated-by-304(s) response (if it exists) | |
67 | /// \returns baseReply() (otherwise) | |
68 | const HttpReply &freshestReply() const { | |
69 | if (updatedReply_) | |
70 | return *updatedReply_; | |
71 | else | |
72 | return baseReply(); | |
73 | } | |
74 | ||
75 | /// \returns writable base reply for parsing and other initial modifications | |
76 | /// Base modifications can only be done when forming/loading the entry. | |
77 | /// After that, use replaceBaseReply() to reset all of the replies. | |
78 | HttpReply &adjustableBaseReply(); | |
79 | ||
80 | /// (re)sets base reply, usually just replacing the initial/empty object | |
81 | /// also forgets the updated reply (if any) | |
82 | void replaceBaseReply(const HttpReplyPointer &r); | |
83 | ||
84 | /// (re)sets updated reply; \see updatedReply() | |
85 | void updateReply(const HttpReply &r) { updatedReply_ = &r; } | |
86 | ||
87 | /// reflects past Controller::updateOnNotModified(old, e304) calls: | |
88 | /// for HTTP 304 entries: whether our entry was used as "e304" | |
89 | /// for other entries: whether our entry was updated as "old" | |
90 | bool appliedUpdates = false; | |
91 | ||
92 | void stat (MemBuf * mb) const; | |
93 | ||
94 | /// The offset of the last memory-stored HTTP response byte plus one. | |
95 | /// * HTTP response headers (if any) are stored at offset zero. | |
96 | /// * HTTP response body byte[n] usually has offset (hdr_sz + n), where | |
97 | /// hdr_sz is the size of stored HTTP response headers (zero if none); and | |
98 | /// n is the corresponding byte offset in the whole resource body. | |
99 | /// However, some 206 (Partial Content) response bodies are stored (and | |
100 | /// retrieved) as regular 200 response bodies, disregarding offsets of | |
101 | /// their body parts. \sa HttpStateData::decideIfWeDoRanges(). | |
102 | int64_t endOffset () const; | |
103 | ||
104 | /// sets baseReply().hdr_sz (i.e. written reply headers size) to endOffset() | |
105 | void markEndOfReplyHeaders(); | |
106 | ||
107 | /// negative if unknown; otherwise, expected object_sz, expected endOffset | |
108 | /// maximum, and stored reply headers+body size (all three are the same) | |
109 | int64_t expectedReplySize() const; | |
110 | int64_t size() const; | |
111 | void reset(); | |
112 | int64_t lowestMemReaderOffset() const; | |
113 | bool readAheadPolicyCanRead() const; | |
114 | void addClient(store_client *); | |
115 | /* XXX belongs in MemObject::swapout, once swaphdrsz is managed | |
116 | * better | |
117 | */ | |
118 | int64_t objectBytesOnDisk() const; | |
119 | int64_t policyLowestOffsetToKeep(bool swap) const; | |
120 | int64_t availableForSwapOut() const; ///< buffered bytes we have not swapped out yet | |
121 | void trimSwappable(); | |
122 | void trimUnSwappable(); | |
123 | bool isContiguous() const; | |
124 | int mostBytesWanted(int max, bool ignoreDelayPools) const; | |
125 | void setNoDelay(bool const newValue); | |
126 | #if USE_DELAY_POOLS | |
127 | DelayId mostBytesAllowed() const; | |
128 | #endif | |
129 | ||
130 | #if URL_CHECKSUM_DEBUG | |
131 | ||
132 | void checkUrlChecksum() const; | |
133 | #endif | |
134 | ||
135 | /// Before StoreID, code assumed that MemObject stores Request URI. | |
136 | /// After StoreID, some old code still incorrectly assumes that. | |
137 | /// Use this method to mark that incorrect assumption. | |
138 | const char *urlXXX() const { return storeId(); } | |
139 | ||
140 | /// Entry StoreID (usually just Request URI); if a buggy code requests this | |
141 | /// before the information is available, returns an "[unknown_URI]" string. | |
142 | const char *storeId() const; | |
143 | ||
144 | /// client request URI used for logging; storeId() by default | |
145 | const char *logUri() const; | |
146 | ||
147 | HttpRequestMethod method; | |
148 | mem_hdr data_hdr; | |
149 | int64_t inmem_lo = 0; | |
150 | dlink_list clients; | |
151 | ||
152 | size_t clientCount() const {return nclients;} | |
153 | ||
154 | bool clientIsFirst(void *sc) const {return (clients.head && sc == clients.head->data);} | |
155 | ||
156 | int nclients = 0; | |
157 | ||
158 | class SwapOut | |
159 | { | |
160 | public: | |
161 | int64_t queue_offset = 0; ///< number of bytes sent to SwapDir for writing | |
162 | StoreIOState::Pointer sio; | |
163 | ||
164 | /// Decision states for StoreEntry::swapoutPossible() and related code. | |
165 | typedef enum { swNeedsCheck = 0, swImpossible = -1, swPossible = +1, swStarted } Decision; | |
166 | Decision decision = swNeedsCheck; ///< current decision state | |
167 | }; | |
168 | ||
169 | SwapOut swapout; | |
170 | ||
171 | /* TODO: Remove this change-minimizing hack */ | |
172 | using Io = Store::IoStatus; | |
173 | static constexpr Io ioUndecided = Store::ioUndecided; | |
174 | static constexpr Io ioReading = Store::ioReading; | |
175 | static constexpr Io ioWriting = Store::ioWriting; | |
176 | static constexpr Io ioDone = Store::ioDone; | |
177 | ||
178 | /// State of an entry with regards to the [shared] in-transit table. | |
179 | class XitTable | |
180 | { | |
181 | public: | |
182 | /// associate our StoreEntry with a Transients entry at the given index | |
183 | void open(const int32_t anIndex, const Io anIo) | |
184 | { | |
185 | index = anIndex; | |
186 | io = anIo; | |
187 | } | |
188 | ||
189 | /// stop associating our StoreEntry with a Transients entry | |
190 | void close() | |
191 | { | |
192 | index = -1; | |
193 | io = Store::ioDone; | |
194 | } | |
195 | ||
196 | int32_t index = -1; ///< entry position inside the in-transit table | |
197 | Io io = ioUndecided; ///< current I/O state | |
198 | }; | |
199 | XitTable xitTable; ///< current [shared] memory caching state for the entry | |
200 | ||
201 | /// State of an entry with regards to the [shared] memory caching. | |
202 | class MemCache | |
203 | { | |
204 | public: | |
205 | int32_t index = -1; ///< entry position inside the memory cache | |
206 | int64_t offset = 0; ///< bytes written/read to/from the memory cache so far | |
207 | ||
208 | Io io = ioUndecided; ///< current I/O state | |
209 | }; | |
210 | MemCache memCache; ///< current [shared] memory caching state for the entry | |
211 | ||
212 | HttpRequestPointer request; | |
213 | ||
214 | struct timeval start_ping; | |
215 | IRCB *ping_reply_callback; | |
216 | PeerSelector *ircb_data = nullptr; | |
217 | ||
218 | /// used for notifying StoreEntry writers about 3rd-party initiated aborts | |
219 | AsyncCallPointer abortCallback; | |
220 | RemovalPolicyNode repl; | |
221 | int id = 0; | |
222 | int64_t object_sz = -1; | |
223 | size_t swap_hdr_sz = 0; | |
224 | #if URL_CHECKSUM_DEBUG | |
225 | unsigned int chksum = 0; | |
226 | #endif | |
227 | ||
228 | SBuf vary_headers; | |
229 | ||
230 | void delayRead(const AsyncCallPointer &); | |
231 | void kickReads(); | |
232 | ||
233 | private: | |
234 | HttpReplyPointer reply_; ///< \see baseReply() | |
235 | HttpReplyPointer updatedReply_; ///< \see updatedReply() | |
236 | ||
237 | mutable SBuf storeId_; ///< StoreId for our entry (usually request URI) | |
238 | mutable SBuf logUri_; ///< URI used for logging (usually request URI) | |
239 | ||
240 | DelayedAsyncCalls deferredReads; | |
241 | }; | |
242 | ||
243 | /** global current memory removal policy */ | |
244 | extern RemovalPolicy *mem_policy; | |
245 | ||
246 | #endif /* SQUID_SRC_MEMOBJECT_H */ | |
247 |