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

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

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: Moved DeferNetworkStart, fixed a bot failure Created 3 years, 8 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
« no previous file with comments | « no previous file | net/http/http_cache.cc » ('j') | net/http/http_cache.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file declares a HttpTransactionFactory implementation that can be 5 // This file declares a HttpTransactionFactory implementation that can be
6 // layered on top of another HttpTransactionFactory to add HTTP caching. The 6 // layered on top of another HttpTransactionFactory to add HTTP caching. The
7 // caching logic follows RFC 7234 (any exceptions are called out in the code). 7 // caching logic follows RFC 7234 (any exceptions are called out in the code).
8 // 8 //
9 // The HttpCache takes a disk_cache::Backend as a parameter, and uses that for 9 // The HttpCache takes a disk_cache::Backend as a parameter, and uses that for
10 // the cache storage. 10 // the cache storage.
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 }; 237 };
238 238
239 class MetadataWriter; 239 class MetadataWriter;
240 class QuicServerInfoFactoryAdaptor; 240 class QuicServerInfoFactoryAdaptor;
241 class Transaction; 241 class Transaction;
242 class WorkItem; 242 class WorkItem;
243 friend class Transaction; 243 friend class Transaction;
244 friend class ViewCacheHelper; 244 friend class ViewCacheHelper;
245 struct PendingOp; // Info for an entry under construction. 245 struct PendingOp; // Info for an entry under construction.
246 246
247 // To help with testing.
248 friend class MockHttpCache;
249
247 using TransactionList = std::list<Transaction*>; 250 using TransactionList = std::list<Transaction*>;
248 using TransactionSet = std::unordered_set<Transaction*>; 251 using TransactionSet = std::unordered_set<Transaction*>;
249 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; 252 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList;
250 253
254 // We implement a basic reader/writer lock for the disk cache entry. If there
255 // is a writer, then all transactions must wait to read the body. But the
256 // waiting transactions can start their headers phase in parallel. Headers
jkarlin 2017/04/13 15:05:52 Can you also describe what happens if the headers
shivanisha 2017/04/13 16:54:53 done
257 // phase is allowed for one transaction at a time so that if it doesn't match
258 // the existing headers, remaining transactions do not also try to match the
259 // existing entry in parallel leading to wasted network requests.
260 //
261 // A transaction goes through these state transitions.
262 //
263 // Write mode transactions:
264 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> writer
265 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> readers
266 // (once the data is written to the cache by another writer)
267 //
268 // Read only transactions:
269 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> readers
270 // (once the data is written to the cache by the writer)
271
251 struct ActiveEntry { 272 struct ActiveEntry {
252 explicit ActiveEntry(disk_cache::Entry* entry); 273 explicit ActiveEntry(disk_cache::Entry* entry);
253 ~ActiveEntry(); 274 ~ActiveEntry();
254 size_t EstimateMemoryUsage() const; 275 size_t EstimateMemoryUsage() const;
255 276
256 // Returns true if no transactions are associated with this entry. 277 // Returns true if no transactions are associated with this entry.
257 bool HasNoTransactions(); 278 bool HasNoTransactions();
258 279
259 disk_cache::Entry* disk_entry; 280 // Returns true if no active readers/writer transactions are associated
260 Transaction* writer; 281 // with this entry.
282 bool HasNoActiveTransactions();
283
284 disk_cache::Entry* disk_entry = nullptr;
285
286 // Transactions waiting to be added to entry.
287 TransactionList add_to_entry_queue;
288
289 // Transaction currently in the headers phase, either validating the
290 // response or getting new headers. This can exist simultaneously with
291 // writer or readers while validating existing headers.
292 Transaction* headers_transaction = nullptr;
293
294 // Transactions that have completed their headers phase and are waiting
295 // to read the response body or write the response body.
296 TransactionList done_headers_queue;
297
298 // Transaction currently reading from the network and writing to the cache.
299 Transaction* writer = nullptr;
300
301 // Transactions that can only read from the cache. Only one of writer or
302 // readers can exist at a time.
261 TransactionSet readers; 303 TransactionSet readers;
262 TransactionList pending_queue; 304
263 bool will_process_pending_queue; 305 // The following variables are true if OnProcessQueuedTransactions is posted
264 bool doomed; 306 bool will_process_queued_transactions = false;
307
308 // True if entry is doomed.
309 bool doomed = false;
265 }; 310 };
266 311
267 using ActiveEntriesMap = 312 using ActiveEntriesMap =
268 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; 313 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>;
269 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; 314 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>;
270 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; 315 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>;
271 using PlaybackCacheMap = std::unordered_map<std::string, int>; 316 using PlaybackCacheMap = std::unordered_map<std::string, int>;
272 317
273 // Methods ------------------------------------------------------------------ 318 // Methods ------------------------------------------------------------------
274 319
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 380
336 // Creates the disk cache entry associated with |key|, returning an 381 // Creates the disk cache entry associated with |key|, returning an
337 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if 382 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if
338 // this method returns ERR_IO_PENDING. 383 // this method returns ERR_IO_PENDING.
339 int CreateEntry(const std::string& key, ActiveEntry** entry, 384 int CreateEntry(const std::string& key, ActiveEntry** entry,
340 Transaction* trans); 385 Transaction* trans);
341 386
342 // Destroys an ActiveEntry (active or doomed). 387 // Destroys an ActiveEntry (active or doomed).
343 void DestroyEntry(ActiveEntry* entry); 388 void DestroyEntry(ActiveEntry* entry);
344 389
345 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING 390 // Adds a transaction to an ActiveEntry. This method returns ERR_IO_PENDING
346 // the transaction will be notified about completion via its IO callback. This 391 // and the transaction will be notified about completion via its IO callback.
347 // method returns ERR_CACHE_RACE to signal the transaction that it cannot be 392 // In a failure case, the callback will be invoked with ERR_CACHE_RACE.
348 // added to the provided entry, and it should retry the process with another 393 int AddTransactionToEntry(ActiveEntry* entry, Transaction* transaction);
349 // one (in this case, the entry is no longer valid). 394
350 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); 395 // Transaction invokes this when its response headers phase is complete
396 // either on validating existing headers/skipping validation or getting new
397 // headers. The transaction is added to a queue and a task is posted
jkarlin 2017/04/13 15:05:52 Remove latter part of sentence, just need "when it
shivanisha 2017/04/13 16:54:53 done
398 // to process queued transactions of the entry. Returns ERR_IO_PENDING and the
jkarlin 2017/04/13 15:05:52 The detail of the transaction being added to a que
shivanisha 2017/04/13 16:54:53 Removed and also revised the comment to mention th
399 // transaction will be notified about completion via its IO callback.
400 // In a failure case, the callback will be invoked with ERR_CACHE_RACE.
401 int DoneWithResponseHeaders(ActiveEntry* entry, Transaction* transaction);
351 402
352 // Called when the transaction has finished working with this entry. |cancel| 403 // Called when the transaction has finished working with this entry. |cancel|
353 // is true if the operation was cancelled by the caller instead of running 404 // is true if the operation was cancelled by the caller instead of running
354 // to completion. 405 // to completion.
355 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); 406 void DoneWithEntry(ActiveEntry* entry, Transaction* transaction, bool cancel);
356 407
357 // Called when the transaction has finished writing to this entry. |success| 408 // Called when the transaction has finished writing to this entry. |success|
358 // is false if the cache entry should be deleted. 409 // is false if the cache entry should be deleted.
359 void DoneWritingToEntry(ActiveEntry* entry, bool success); 410 void DoneWritingToEntry(ActiveEntry* entry,
411 bool success,
412 Transaction* transaction);
360 413
361 // Called when the transaction has finished reading from this entry. 414 // Called when the transaction has finished reading from this entry.
362 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); 415 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* transaction);
363 416
364 // Converts the active writer transaction to a reader so that other 417 // Removes and returns all queued transactions in |entry| in FIFO order. This
365 // transactions can start reading from this entry. 418 // includes transactions that have completed the headers phase and those that
366 void ConvertWriterToReader(ActiveEntry* entry); 419 // have not been added to the entry yet in that order. |list| is the output
420 // argument.
421 void RemoveAllQueuedTransactions(ActiveEntry* entry, TransactionList* list);
422
423 // Processes either writer's failure to write response body or
424 // headers_transactions's failure to write headers. Also invoked when headers
425 // transaction's validation result is not a match.
426 void ProcessEntryFailure(ActiveEntry* entry);
427
428 // Resumes processing the queued transactions of |entry|.
429 void ProcessQueuedTransactions(ActiveEntry* entry);
430
431 // Checks if a transaction can be added to the entry. If yes, it will
432 // invoke the IO callback of the transaction. This is a helper function for
433 // OnProcessQueuedTransactions. It will take a transaction from
434 // add_to_entry_queue and make it a headers_transaction, if one doesn't exist
435 // already.
436 void ProcessAddToEntryQueue(ActiveEntry* entry);
437
438 // Invoked when a transaction that has already completed the response headers
439 // phase can resume reading/writing the response body. It will invoke the IO
440 // callback of the transaction. This is a helper function for
441 // OnProcessQueuedTransactions.
442 void ProcessDoneHeadersQueue(ActiveEntry* entry);
443
444 // Returns true if this transaction can write headers to the entry.
445 bool CanTransactionWriteResponseHeaders(ActiveEntry* entry,
446 Transaction* transaction,
447 int response_code) const;
448
449 // Returns true if |transaction| is about to start writing response body or
450 // already started but not yet finished.
451 bool IsTransactionWritingIncomplete(ActiveEntry* entry,
452 Transaction* transaction,
453 const std::string& method) const;
367 454
368 // Returns the LoadState of the provided pending transaction. 455 // Returns the LoadState of the provided pending transaction.
369 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); 456 LoadState GetLoadStateForPendingTransaction(const Transaction* trans);
370 457
371 // Removes the transaction |trans|, from the pending list of an entry 458 // Removes the transaction |trans|, from the pending list of an entry
372 // (PendingOp, active or doomed entry). 459 // (PendingOp, active or doomed entry).
373 void RemovePendingTransaction(Transaction* trans); 460 void RemovePendingTransaction(Transaction* trans);
374 461
375 // Removes the transaction |trans|, from the pending list of |entry|. 462 // Removes the transaction |trans|, from the pending list of |entry|.
376 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, 463 bool RemovePendingTransactionFromEntry(ActiveEntry* entry,
377 Transaction* trans); 464 Transaction* trans);
378 465
379 // Removes the transaction |trans|, from the pending list of |pending_op|. 466 // Removes the transaction |trans|, from the pending list of |pending_op|.
380 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, 467 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op,
381 Transaction* trans); 468 Transaction* trans);
382 // Resumes processing the pending list of |entry|.
383 void ProcessPendingQueue(ActiveEntry* entry);
384 469
385 // Events (called via PostTask) --------------------------------------------- 470 // Events (called via PostTask) ---------------------------------------------
386 471
387 void OnProcessPendingQueue(ActiveEntry* entry); 472 void OnProcessQueuedTransactions(ActiveEntry* entry);
388 473
389 // Callbacks ---------------------------------------------------------------- 474 // Callbacks ----------------------------------------------------------------
390 475
391 // Processes BackendCallback notifications. 476 // Processes BackendCallback notifications.
392 void OnIOComplete(int result, PendingOp* entry); 477 void OnIOComplete(int result, PendingOp* entry);
393 478
394 // Helper to conditionally delete |pending_op| if the HttpCache object it 479 // Helper to conditionally delete |pending_op| if the HttpCache object it
395 // is meant for has been deleted. 480 // is meant for has been deleted.
396 // 481 //
397 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might 482 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 std::unique_ptr<base::Clock> clock_; 520 std::unique_ptr<base::Clock> clock_;
436 521
437 base::WeakPtrFactory<HttpCache> weak_factory_; 522 base::WeakPtrFactory<HttpCache> weak_factory_;
438 523
439 DISALLOW_COPY_AND_ASSIGN(HttpCache); 524 DISALLOW_COPY_AND_ASSIGN(HttpCache);
440 }; 525 };
441 526
442 } // namespace net 527 } // namespace net
443 528
444 #endif // NET_HTTP_HTTP_CACHE_H_ 529 #endif // NET_HTTP_HTTP_CACHE_H_
OLDNEW
« no previous file with comments | « no previous file | net/http/http_cache.cc » ('j') | net/http/http_cache.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698