Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: net/http/http_cache_shared_writers.h

Issue 2519473002: Fixes the cache lock issue. (Closed)
Patch Set: Josh's early feedback addressed. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef NET_HTTP_HTTP_CACHE_SHARED_WRITERS_H_
6 #define NET_HTTP_HTTP_CACHE_SHARED_WRITERS_H_
7
8 #include <list>
9 #include <memory>
10 #include "base/memory/weak_ptr.h"
11 #include "net/base/completion_callback.h"
12 #include "net/http/http_cache.h"
13
14 namespace net {
15
16 // SharedWriters represents the set of all HttpCache::Transactions that are
17 // reading from the network using the same network transaction and writing to
18 // the same cache entry. It is owned by the ActiveEntry.
19 class HttpCache::SharedWriters {
20 public:
21 // Creates a new SharedWriters object and transfers the ownership of network
22 // transaction to SharedWriters. Consumer must ensure that |*entry| and
23 // |*cache| outlives |this|. It is ok for cache_transaction to die before this
24 // object.
25 SharedWriters(HttpCache* cache,
26 ActiveEntry* entry,
27 Transaction* cache_transaction,
28 RequestPriority priority,
29 std::unique_ptr<HttpTransaction> network_transaction);
30 ~SharedWriters();
31
32 // Adds a transaction to SharedWriters. Return value is true if it is the
33 // current validating transaction and false if it is waiting.
34 bool AddTransaction(Transaction* transaction);
35
36 // Invokes Read on network transaction if a read is not already in progress.
37 // In case a read is already in progress then this transaction is added to
38 // a waiting queue, read_in_progress is set to true and ERR_IO_PENDING is
39 // returned.
40 int Read(scoped_refptr<IOBuffer> buf,
41 int buf_len,
42 const CompletionCallback& callback,
43 Transaction* transaction,
44 bool* read_in_progress);
45
46 // Invokes WriteData on disk entry.
47 int CacheWrite(scoped_refptr<IOBuffer> buf,
48 int write_len,
49 const CompletionCallback& callback,
50 Transaction* transaction);
51
52 // Invoked when the validating transaction wants to continue with the entry
53 // as the validation successfully matched it.
54 void OnValidationMatch(Transaction* transaction, RequestPriority priority);
55
56 // Invoked when the validating transaction cannot continue with the entry as
57 // the validation did not return a 304. Its fine to continue writing to cache
58 // if this is the only transaction, so network transaction ownership is
59 // transferred to SharedWriters. But if there are other transactions, entry
60 // will be doomed and this transaction will continue reading from the network
61 // so the return value will pass the ownership of the network transaction
62 // back to the transaction.
63 std::unique_ptr<HttpTransaction> OnValidationNoMatch(
64 const std::string& key,
65 Transaction* transaction,
66 std::unique_ptr<HttpTransaction> network_transaction,
67 RequestPriority priority);
68
69 // Invoked when DoneReading is invoked on a shared transaction. The
70 // transaction will be removed from |this|. If another transaction is
71 // currently reading, then other transactions will not be impacted.
72 void DoneReading(Transaction* transaction);
73
74 // Invoked when StopCaching is called for a shared writer transaction.
75 // It stops caching only if there are no other transactions.
76 void StopCaching(Transaction* transaction);
77
78 // All transactions eligible for shared writing from entry's pending queue
79 // will be tracked from SharedWriters after this function.
80 void MoveFromPendingQueue();
81
82 // Posts a task for invoking the io callback of the first transaction waiting
83 // for validation.
84 void ProcessFirstWaitingValidation();
85
86 // Removes a pending transaction.
87 bool RemoveWaitingTransaction(Transaction* transaction);
88
89 // Removes a transaction which is waiting for Read to be invoked by the
90 // consumer.
91 void RemoveIdleTransaction(Transaction* transaction);
92
93 // Removes a transaction waiting on a Read call.
94 void RemoveWaitingForReadTransaction(Transaction* transaction);
95
96 // Removes the currently validating transaction.
97 void RemoveValidatingTransaction(Transaction* transaction);
98
99 // Removes the currently active transaction.
100 void RemoveActiveTransaction(Transaction* transaction);
101
102 // Returns true if this object is empty.
103 bool empty();
104
105 HttpTransaction* network_transaction() { return network_transaction_.get(); }
106
107 // Do not add new transactions if in the process of marking the entry as
108 // truncated.
109 bool CanAddNewTransaction();
110
111 // Invoked when there is a change in a member transaction's priority or a
112 // member transaction is removed.
113 void PriorityChanged();
114
115 private:
116 enum class State {
117 STATE_NONE,
jkarlin 2017/02/07 15:20:31 Now that it's an enum class and you always have to
shivanisha 2017/02/07 20:56:48 Done.
118 STATE_NETWORK_READ,
119 STATE_NETWORK_READ_COMPLETE,
120 STATE_CACHE_WRITE_DATA,
121 STATE_CACHE_WRITE_DATA_COMPLETE,
122 STATE_CACHE_WRITE_TRUNCATED_RESPONSE,
123 STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE,
124 STATE_DONE
125 };
126
127 // These transactions are waiting on Read. After the active transaction
128 // completes writing the data to the cache, their buffer would be filled with
129 // the data and their callback will be invoked.
130 struct WaitingForRead {
131 Transaction* transaction;
132 scoped_refptr<IOBuffer> read_buf;
133 int read_buf_len;
134 int write_len;
135 const CompletionCallback callback;
136 WaitingForRead(Transaction* transaction,
137 scoped_refptr<IOBuffer> read_buf,
138 int len,
139 const CompletionCallback& consumer_callback);
140 ~WaitingForRead();
141 WaitingForRead(const WaitingForRead&);
142 };
143
144 using WaitingForReadList = std::list<WaitingForRead>;
145 // Runs the state transition loop. Resets and calls |callback_| on exit,
146 // unless the return value is ERR_IO_PENDING.
147 int DoLoop(int result);
148
149 // IO Completion callback function.
150 void OnIOComplete(int result);
151
152 // State machine functions.
153 int DoNetworkRead();
154 int DoNetworkReadComplete(int result);
155 int DoCacheWriteData(int num_bytes);
156 int DoCacheWriteDataComplete(int result);
157 int DoCacheWriteTruncatedResponse();
158 int DoCacheWriteTruncatedResponseComplete(int result);
159
160 // Helper functions for callback.
161
162 void OnNetworkReadSuccess(int len);
163
164 void OnCacheWriteSuccess(int result);
165
166 void OnCacheWriteFailure();
167
168 void OnNetworkReadFailure(int result);
169
170 void FailureCleanup(int error, bool continue_network_reading);
171
172 // All pending transactions will not be tracked by entry.
173 void MoveToPendingQueue();
174
175 // Notifies the transactions waiting on Read of the result, by posting a task
176 // for each of them.
177 void ProcessWaitingForReadTransactions(int result);
178
179 void OnProcessFirstWaitingValidation();
180
181 // Removes the active transaction from |this|.
182 void ResetActiveTransaction(bool continue_network_reading = false);
183
184 // Sets the state of idle writers so that they can fail any subsequent
185 // Read.
186 void SetIdleWritersFailState(int result);
187
188 // When response is completely written, any idle writers are moved to
189 // entry_->readers.
190 void MoveIdleWritersToReaders();
191
192 // Helper function to let the validating transaction continue being a part of
193 // SharedWriters.
194 void ValidationDoneContinue(Transaction* transaction,
195 RequestPriority priority);
196
197 // Helper function invoked when response is successfully written to the cache.
198 void ResponseDataComplete();
199
200 bool AttemptTruncation();
201
202 RequestPriority getCurrentHighestPriority();
203
204 State next_state_ = State::STATE_NONE;
205
206 // Http Cache.
207 base::WeakPtr<HttpCache> cache_;
208
209 // Owner of this object.
210 ActiveEntry* entry_ = nullptr;
211
212 std::unique_ptr<HttpTransaction> network_transaction_;
213
214 scoped_refptr<IOBuffer> read_buf_;
215 int io_buf_len_ = 0;
216 int write_len_ = 0;
217
218 // The cache transaction that is the current consumer of network_transaction_
219 // ::Read or writing to the entry and is waiting for the operation to be
220 // completed. This is used to ensure there is at most one consumer.
221 // After the network read and cache write is successful and data
222 // written to cache, other waiting transactions will be notified.
223 Transaction* active_transaction_ = nullptr;
224
225 // This will point to the currently validating transaction.
226 Transaction* validating_transaction_ = nullptr;
227
228 // These transactions are waiting for the validating transaction to complete
229 // and then the first of these will begin validation.
230 TransactionList waiting_for_validation_;
231
232 WaitingForReadList waiting_for_read_;
233
234 // Includes a transaction if it is past the validation stage.
235 TransactionSet all_writers_;
236
237 // Current priority of the request. If a higher priority transaction is
238 // added, the priority of network transaction will be increased.
239 RequestPriority priority_ = DEFAULT_PRIORITY;
240
241 CompletionCallback callback_; // Consumer's callback.
242 base::Callback<void(bool*)> cache_callback_; // Cache callback.
243 CompletionCallback io_callback_;
244
245 // Saved so it could be returned to the consumer after marking the entry as
246 // truncated, if needed.
247 int rv_post_truncation_ = 0;
248
249 base::WeakPtrFactory<SharedWriters> weak_factory_;
250
251 DISALLOW_COPY_AND_ASSIGN(SharedWriters);
252 };
253
254 } // namespace net
255
256 #endif // NET_HTTP_HTTP_CACHE_SHARED_WRITERS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698