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

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

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: Rebased + test-only changes for fixing the new Push Cache Lookup failing unit tests 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
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. If the
260 // headers do not match, this entry will be doomed.
261 //
262 // A transaction goes through these state transitions.
263 //
264 // Write mode transactions:
265 // add_to_entry_queue-> headers_transaction -> writer (this is not added to
266 // done_headers_queue for performance and consumer's expectations to be
267 // synchronous in knowing that headers have been received)
jkarlin 2017/04/20 15:05:30 nit: Can you move the comments in the parenthesis
shivanisha 2017/04/21 23:06:01 Already part of the implementation, removed from h
268 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> readers
269 // (once the data is written to the cache by another writer)
270 //
271 // Read only transactions:
272 // add_to_entry_queue-> headers_transaction -> done_headers_queue -> readers
273 // (once the data is written to the cache by the writer)
274
251 struct ActiveEntry { 275 struct ActiveEntry {
252 explicit ActiveEntry(disk_cache::Entry* entry); 276 explicit ActiveEntry(disk_cache::Entry* entry);
253 ~ActiveEntry(); 277 ~ActiveEntry();
254 size_t EstimateMemoryUsage() const; 278 size_t EstimateMemoryUsage() const;
255 279
256 // Returns true if no transactions are associated with this entry. 280 // Returns true if no transactions are associated with this entry.
257 bool HasNoTransactions(); 281 bool HasNoTransactions();
258 282
259 disk_cache::Entry* disk_entry; 283 // Returns true if no active readers/writer transactions are associated
260 Transaction* writer; 284 // with this entry.
285 bool HasNoActiveTransactions();
286
287 disk_cache::Entry* disk_entry = nullptr;
288
289 // Transactions waiting to be added to entry.
290 TransactionList add_to_entry_queue;
291
292 // Transaction currently in the headers phase, either validating the
293 // response or getting new headers. This can exist simultaneously with
294 // writer or readers while validating existing headers.
295 Transaction* headers_transaction = nullptr;
296
297 // Transactions that have completed their headers phase and are waiting
298 // to read the response body or write the response body.
299 TransactionList done_headers_queue;
300
301 // Transaction currently reading from the network and writing to the cache.
302 Transaction* writer = nullptr;
303
304 // Transactions that can only read from the cache. Only one of writer or
305 // readers can exist at a time.
261 TransactionSet readers; 306 TransactionSet readers;
262 TransactionList pending_queue; 307
263 bool will_process_pending_queue; 308 // The following variables are true if OnProcessQueuedTransactions is posted
264 bool doomed; 309 bool will_process_queued_transactions = false;
310
311 // True if entry is doomed.
312 bool doomed = false;
265 }; 313 };
266 314
267 using ActiveEntriesMap = 315 using ActiveEntriesMap =
268 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; 316 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>;
269 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; 317 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>;
270 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; 318 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>;
271 using PlaybackCacheMap = std::unordered_map<std::string, int>; 319 using PlaybackCacheMap = std::unordered_map<std::string, int>;
272 320
273 // Methods ------------------------------------------------------------------ 321 // Methods ------------------------------------------------------------------
274 322
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 383
336 // Creates the disk cache entry associated with |key|, returning an 384 // Creates the disk cache entry associated with |key|, returning an
337 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if 385 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if
338 // this method returns ERR_IO_PENDING. 386 // this method returns ERR_IO_PENDING.
339 int CreateEntry(const std::string& key, ActiveEntry** entry, 387 int CreateEntry(const std::string& key, ActiveEntry** entry,
340 Transaction* trans); 388 Transaction* trans);
341 389
342 // Destroys an ActiveEntry (active or doomed). 390 // Destroys an ActiveEntry (active or doomed).
343 void DestroyEntry(ActiveEntry* entry); 391 void DestroyEntry(ActiveEntry* entry);
344 392
345 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING 393 // 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 394 // 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 395 // 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 396 int AddTransactionToEntry(ActiveEntry* entry, Transaction* transaction);
349 // one (in this case, the entry is no longer valid). 397
350 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); 398 // Transaction invokes this when its response headers phase is complete
399 // If the transaction is responsible for writing the response body,
400 // it becomes the writer and returns OK. In other cases the transaction it
401 // returns ERR_IO_PENDING and the transaction will be notified about
402 // completion via its IO callback. In a failure case, the callback will be
403 // invoked with ERR_CACHE_RACE.
404 int DoneWithResponseHeaders(ActiveEntry* entry, Transaction* transaction);
351 405
352 // Called when the transaction has finished working with this entry. |cancel| 406 // 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 407 // is true if the operation was cancelled by the caller instead of running
354 // to completion. 408 // to completion.
355 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); 409 void DoneWithEntry(ActiveEntry* entry, Transaction* transaction, bool cancel);
356 410
357 // Called when the transaction has finished writing to this entry. |success| 411 // Called when the transaction has finished writing to this entry. |success|
358 // is false if the cache entry should be deleted. 412 // is false if the cache entry should be deleted.
359 void DoneWritingToEntry(ActiveEntry* entry, bool success); 413 void DoneWritingToEntry(ActiveEntry* entry,
414 bool success,
415 Transaction* transaction);
360 416
361 // Called when the transaction has finished reading from this entry. 417 // Called when the transaction has finished reading from this entry.
362 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); 418 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* transaction);
363 419
364 // Converts the active writer transaction to a reader so that other 420 // Removes and returns all queued transactions in |entry| in FIFO order. This
365 // transactions can start reading from this entry. 421 // includes transactions that have completed the headers phase and those that
366 void ConvertWriterToReader(ActiveEntry* entry); 422 // have not been added to the entry yet in that order. |list| is the output
423 // argument.
424 void RemoveAllQueuedTransactions(ActiveEntry* entry, TransactionList* list);
425
426 // Processes either writer's failure to write response body or
427 // headers_transactions's failure to write headers. Also invoked when headers
428 // transaction's validation result is not a match.
429 void ProcessEntryFailure(ActiveEntry* entry);
430
431 // Resumes processing the queued transactions of |entry|.
432 void ProcessQueuedTransactions(ActiveEntry* entry);
433
434 // Checks if a transaction can be added to the entry. If yes, it will
435 // invoke the IO callback of the transaction. This is a helper function for
436 // OnProcessQueuedTransactions. It will take a transaction from
437 // add_to_entry_queue and make it a headers_transaction, if one doesn't exist
438 // already.
439 void ProcessAddToEntryQueue(ActiveEntry* entry);
440
441 // Invoked when a transaction that has already completed the response headers
442 // phase can resume reading/writing the response body. It will invoke the IO
443 // callback of the transaction. This is a helper function for
444 // OnProcessQueuedTransactions.
445 void ProcessDoneHeadersQueue(ActiveEntry* entry);
446
447 // Returns true if this transaction can write headers to the entry.
448 bool CanTransactionWriteResponseHeaders(ActiveEntry* entry,
449 Transaction* transaction,
450 int response_code) const;
451
452 // Returns true if |transaction| is about to start writing response body or
453 // already started but not yet finished.
454 bool IsTransactionWritingIncomplete(ActiveEntry* entry,
455 Transaction* transaction,
456 const std::string& method) const;
457
458 // Returns true if a transaction is currently writing the response body.
459 bool IsWritingInProgress(ActiveEntry* entry) const;
jkarlin 2017/04/20 15:05:29 Please add a TODO to remove this function once we
shivanisha 2017/04/21 23:06:01 The todo is added in the code where this is invoke
367 460
368 // Returns the LoadState of the provided pending transaction. 461 // Returns the LoadState of the provided pending transaction.
369 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); 462 LoadState GetLoadStateForPendingTransaction(const Transaction* trans);
370 463
371 // Removes the transaction |trans|, from the pending list of an entry 464 // Removes the transaction |trans|, from the pending list of an entry
372 // (PendingOp, active or doomed entry). 465 // (PendingOp, active or doomed entry).
373 void RemovePendingTransaction(Transaction* trans); 466 void RemovePendingTransaction(Transaction* trans);
374 467
375 // Removes the transaction |trans|, from the pending list of |entry|. 468 // Removes the transaction |trans|, from the pending list of |entry|.
376 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, 469 bool RemovePendingTransactionFromEntry(ActiveEntry* entry,
377 Transaction* trans); 470 Transaction* trans);
378 471
379 // Removes the transaction |trans|, from the pending list of |pending_op|. 472 // Removes the transaction |trans|, from the pending list of |pending_op|.
380 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, 473 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op,
381 Transaction* trans); 474 Transaction* trans);
382 // Resumes processing the pending list of |entry|.
383 void ProcessPendingQueue(ActiveEntry* entry);
384 475
385 // Events (called via PostTask) --------------------------------------------- 476 // Events (called via PostTask) ---------------------------------------------
386 477
387 void OnProcessPendingQueue(ActiveEntry* entry); 478 void OnProcessQueuedTransactions(ActiveEntry* entry);
388 479
389 // Callbacks ---------------------------------------------------------------- 480 // Callbacks ----------------------------------------------------------------
390 481
391 // Processes BackendCallback notifications. 482 // Processes BackendCallback notifications.
392 void OnIOComplete(int result, PendingOp* entry); 483 void OnIOComplete(int result, PendingOp* entry);
393 484
394 // Helper to conditionally delete |pending_op| if the HttpCache object it 485 // Helper to conditionally delete |pending_op| if the HttpCache object it
395 // is meant for has been deleted. 486 // is meant for has been deleted.
396 // 487 //
397 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might 488 // 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_; 526 std::unique_ptr<base::Clock> clock_;
436 527
437 base::WeakPtrFactory<HttpCache> weak_factory_; 528 base::WeakPtrFactory<HttpCache> weak_factory_;
438 529
439 DISALLOW_COPY_AND_ASSIGN(HttpCache); 530 DISALLOW_COPY_AND_ASSIGN(HttpCache);
440 }; 531 };
441 532
442 } // namespace net 533 } // namespace net
443 534
444 #endif // NET_HTTP_HTTP_CACHE_H_ 535 #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