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

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

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: Rough patch to discuss if ActiveEntry would be better designed with a state machine Created 3 years, 9 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') | no next file with comments »
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 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList; 249 typedef std::list<std::unique_ptr<WorkItem>> WorkItemList;
250 250
251 struct ActiveEntry { 251 struct ActiveEntry {
252 explicit ActiveEntry(disk_cache::Entry* entry); 252 explicit ActiveEntry(disk_cache::Entry* entry);
253 ~ActiveEntry(); 253 ~ActiveEntry();
254 size_t EstimateMemoryUsage() const; 254 size_t EstimateMemoryUsage() const;
255 255
256 // Returns true if no transactions are associated with this entry. 256 // Returns true if no transactions are associated with this entry.
257 bool HasNoTransactions(); 257 bool HasNoTransactions();
258 258
259 disk_cache::Entry* disk_entry; 259 // Returns true if no active readers/writer transactions are associated
260 Transaction* writer; 260 // with this entry.
261 bool HasNoActiveTransactions();
262
263 // Returns true if the given transaction is a reader.
264 bool IsReader(Transaction* transaction);
265
266 // Checks if the entry can go to the next state, if yes, it does and assigns
267 // the transaction to the appropriate transaction field.
268 void AdvanceState(Transaction* transaction);
269
270 disk_cache::Entry* disk_entry = nullptr;
271
272 enum class State {
273 NONE,
274 VALIDATING,
275 WRITE,
276 VALIDATING_WRITE,
277 READ,
278 VALIDATING_READ
279 };
jkarlin 2017/03/09 18:38:33 Suggest: NONE, WRITING_RESPONSE_HEADERS, WRITING_
280
281 State state = State::NONE;
282
283 // The following variables represent the various transactions associated
284 // with this entry in different stages. A transaction goes through these
285 // transitions. [] signifies an optional stage.
286 //
287 // Write mode transactions:
288 // [add_to_entry_queue]-> validating_transaction & writer -> writer
289 // [add_to_entry_queue]-> validating_transaction -> validated_queue ->
290 // readers (once the data is written to the cache by another writer)
291 // [add_to_entry_queue]-> validating_transaction -> writer
292 // (if the original writer could not write to the cache because of
293 // failure/cancellation)
294 //
295 // Read only transactions:
296 // [add_to_entry_queue]-> readers (once the data is written to the cache by
297 // the writer)
298
299 // Transactions waiting to be added to entry.
300 TransactionList add_to_entry_queue;
301
302 // Transaction currently validating the response. This can exist
303 // simultaneously with writer or readers.
304 Transaction* validating_transaction = nullptr;
305
306 // Transactions that have completed their validation phase and are waiting
307 // to read the response body.
308 TransactionList validated_queue;
309
310 // Transaction currently reading from the network and writing to the cache.
311 Transaction* writer = nullptr;
312
313 // Transactions that can only read from the cache. Only one of writer or
314 // readers can exist at a time.
261 TransactionSet readers; 315 TransactionSet readers;
262 TransactionList pending_queue; 316
263 bool will_process_pending_queue; 317 // The following variables are true if OnProcessWaitingTransactions or
264 bool doomed; 318 // OnProcessParallelValidation is set to true.
319 bool will_process_waiting_transactions = false;
320 bool will_process_parallel_validation = false;
321
322 // True if entry is doomed.
323 bool doomed = false;
265 }; 324 };
266 325
267 using ActiveEntriesMap = 326 using ActiveEntriesMap =
268 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>; 327 std::unordered_map<std::string, std::unique_ptr<ActiveEntry>>;
269 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>; 328 using PendingOpsMap = std::unordered_map<std::string, PendingOp*>;
270 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>; 329 using ActiveEntriesSet = std::map<ActiveEntry*, std::unique_ptr<ActiveEntry>>;
271 using PlaybackCacheMap = std::unordered_map<std::string, int>; 330 using PlaybackCacheMap = std::unordered_map<std::string, int>;
272 331
273 // Methods ------------------------------------------------------------------ 332 // Methods ------------------------------------------------------------------
274 333
(...skipping 28 matching lines...) Expand all
303 362
304 // Dooms the entry associated with a GET for a given |url|. 363 // Dooms the entry associated with a GET for a given |url|.
305 void DoomMainEntryForUrl(const GURL& url); 364 void DoomMainEntryForUrl(const GURL& url);
306 365
307 // Closes a previously doomed entry. 366 // Closes a previously doomed entry.
308 void FinalizeDoomedEntry(ActiveEntry* entry); 367 void FinalizeDoomedEntry(ActiveEntry* entry);
309 368
310 // Returns an entry that is currently in use and not doomed, or NULL. 369 // Returns an entry that is currently in use and not doomed, or NULL.
311 ActiveEntry* FindActiveEntry(const std::string& key); 370 ActiveEntry* FindActiveEntry(const std::string& key);
312 371
372 // Returns true if given entry is still alive, either as an active or doomed
373 // entry.
374 bool IsEntryAlive(const std::string& key, ActiveEntry* entry);
375
313 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk 376 // Creates a new ActiveEntry and starts tracking it. |disk_entry| is the disk
314 // cache entry. 377 // cache entry.
315 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry); 378 ActiveEntry* ActivateEntry(disk_cache::Entry* disk_entry);
316 379
317 // Deletes an ActiveEntry. 380 // Deletes an ActiveEntry.
318 void DeactivateEntry(ActiveEntry* entry); 381 void DeactivateEntry(ActiveEntry* entry);
319 382
320 // Deletes an ActiveEntry using an exhaustive search. 383 // Deletes an ActiveEntry using an exhaustive search.
321 void SlowDeactivateEntry(ActiveEntry* entry); 384 void SlowDeactivateEntry(ActiveEntry* entry);
322 385
(...skipping 13 matching lines...) Expand all
336 // Creates the disk cache entry associated with |key|, returning an 399 // Creates the disk cache entry associated with |key|, returning an
337 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if 400 // ActiveEntry in |*entry|. |trans| will be notified via its IO callback if
338 // this method returns ERR_IO_PENDING. 401 // this method returns ERR_IO_PENDING.
339 int CreateEntry(const std::string& key, ActiveEntry** entry, 402 int CreateEntry(const std::string& key, ActiveEntry** entry,
340 Transaction* trans); 403 Transaction* trans);
341 404
342 // Destroys an ActiveEntry (active or doomed). 405 // Destroys an ActiveEntry (active or doomed).
343 void DestroyEntry(ActiveEntry* entry); 406 void DestroyEntry(ActiveEntry* entry);
344 407
345 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING 408 // Adds a transaction to an ActiveEntry. If this method returns ERR_IO_PENDING
346 // the transaction will be notified about completion via its IO callback. This 409 // 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 410
348 // added to the provided entry, and it should retry the process with another 411 int AddTransactionToEntry(ActiveEntry* entry, Transaction* transaction);
349 // one (in this case, the entry is no longer valid). 412 int AddTransactionToEntryImpl(ActiveEntry* entry, Transaction* transaction);
350 int AddTransactionToEntry(ActiveEntry* entry, Transaction* trans); 413 int AddTransactionToEntryForRead(ActiveEntry* entry,
414 Transaction* transaction);
415 int AddTransactionToEntryForWrite(ActiveEntry* entry,
416 Transaction* transaction);
417
418 // Transaction invokes this when its validation matches the entry so another
419 // transaction can now start validation. is_match should be passed
420 // as true if the validation was a match.
421 // If its not a match, this entry is doomed and pending transactions are
422 // restarted. Returns OK.
423 // If it is a match, either the transaction becomes a reader if the entry is
424 // already written to the cache and returns OK or the transaction is added to
425 // a queue if response is being written to the cache by another transaction
426 // and returns ERR_IO_PENDING.
427 int DoneValidationWithEntry(ActiveEntry* entry,
428 Transaction* transaction,
429 bool is_match);
430
431 // Transaction invokes this when it has completed validation and it is
432 // proceeding to start writing the response body to the cache. This is done so
433 // that another waiting transaction can start validating the headers in
434 // parallel.
435 void DoneValidationWriteEntry(ActiveEntry* entry, Transaction* transaction);
351 436
352 // Called when the transaction has finished working with this entry. |cancel| 437 // 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 438 // is true if the operation was cancelled by the caller instead of running
354 // to completion. 439 // to completion.
355 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel); 440 void DoneWithEntry(ActiveEntry* entry, Transaction* trans, bool cancel);
356 441
357 // Called when the transaction has finished writing to this entry. |success| 442 // Called when the transaction has finished writing to this entry. |success|
358 // is false if the cache entry should be deleted. 443 // is false if the cache entry should be deleted.
359 void DoneWritingToEntry(ActiveEntry* entry, bool success); 444 void DoneWritingToEntry(ActiveEntry* entry,
445 bool success,
446 Transaction* transaction);
447
448 // Processes other transactions when a transaction is done writing to the
449 // entry, based on success or failure of this transaction.
450 void DoneWritingToEntryProcessOtherTransactions(ActiveEntry* entry,
451 bool success);
360 452
361 // Called when the transaction has finished reading from this entry. 453 // Called when the transaction has finished reading from this entry.
362 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans); 454 void DoneReadingFromEntry(ActiveEntry* entry, Transaction* transaction);
363 455
364 // Converts the active writer transaction to a reader so that other 456 // Converts the active writer/validating transaction to a reader so that other
365 // transactions can start reading from this entry. 457 // transactions can start reading from this entry. It is invoked in the
366 void ConvertWriterToReader(ActiveEntry* entry); 458 // validation phase of a transaction when the current entry is validated. It
459 // needs to differentiate between the scenarios when the entry is already
460 // completely written to by another transaction or is currently being written.
461 // In the latter case, the transaction cannot become a reader at this time and
462 // the function will return false, else it will return true.
463 bool ConvertWriterToReader(ActiveEntry* entry, Transaction* transaction);
367 464
368 // Returns the LoadState of the provided pending transaction. 465 // Returns the LoadState of the provided pending transaction.
369 LoadState GetLoadStateForPendingTransaction(const Transaction* trans); 466 LoadState GetLoadStateForPendingTransaction(const Transaction* trans);
370 467
371 // Removes the transaction |trans|, from the pending list of an entry 468 // Removes the transaction |trans|, from the pending list of an entry
372 // (PendingOp, active or doomed entry). 469 // (PendingOp, active or doomed entry).
373 void RemovePendingTransaction(Transaction* trans); 470 void RemovePendingTransaction(Transaction* trans);
374 471
375 // Removes the transaction |trans|, from the pending list of |entry|. 472 // Removes the transaction |trans|, from the pending list of |entry|.
376 bool RemovePendingTransactionFromEntry(ActiveEntry* entry, 473 bool RemovePendingTransactionFromEntry(ActiveEntry* entry,
377 Transaction* trans); 474 Transaction* trans);
378 475
379 // Removes the transaction |trans|, from the pending list of |pending_op|. 476 // Removes the transaction |trans|, from the pending list of |pending_op|.
380 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op, 477 bool RemovePendingTransactionFromPendingOp(PendingOp* pending_op,
381 Transaction* trans); 478 Transaction* trans);
382 // Resumes processing the pending list of |entry|. 479 // Resumes processing the pending list of |entry|. This should always be
383 void ProcessPendingQueue(ActiveEntry* entry); 480 // called when the response is already written to the cache.
481 void ProcessWaitingTransactions(ActiveEntry* entry);
482
483 // Invoked when a transaction can start validating the entry when there may be
484 // a writer/readers writing/reading to/from the entry.
485 void ProcessParallelValidation(ActiveEntry* entry);
384 486
385 // Events (called via PostTask) --------------------------------------------- 487 // Events (called via PostTask) ---------------------------------------------
386 488
387 void OnProcessPendingQueue(ActiveEntry* entry); 489 void OnProcessWaitingTransactions(const std::string& key, ActiveEntry* entry);
490
491 void OnProcessParallelValidation(const std::string& key, ActiveEntry* entry);
492
493 // Dooms the entry and restarts all the pending transactions.
494 void DoomEntryRestartPendingTransactions(ActiveEntry* entry);
495
496 // Destroys the entry and restarts all the pending transactions.
497 void DestroyEntryRestartPendingTransactions(ActiveEntry* entry);
498
499 // Retrieves all pending transactions from entry in a single list.
500 TransactionList GetAllPendingTransactions(ActiveEntry* entry);
501
502 // Helper function for restarting all pending transactions by invoking their
503 // IO callbacks with ERR_CACHE_RACE.
504 void RestartPendingTransactions(const TransactionList& list);
388 505
389 // Callbacks ---------------------------------------------------------------- 506 // Callbacks ----------------------------------------------------------------
390 507
391 // Processes BackendCallback notifications. 508 // Processes BackendCallback notifications.
392 void OnIOComplete(int result, PendingOp* entry); 509 void OnIOComplete(int result, PendingOp* entry);
393 510
394 // Helper to conditionally delete |pending_op| if the HttpCache object it 511 // Helper to conditionally delete |pending_op| if the HttpCache object it
395 // is meant for has been deleted. 512 // is meant for has been deleted.
396 // 513 //
397 // TODO(ajwong): The PendingOp lifetime management is very tricky. It might 514 // 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_; 552 std::unique_ptr<base::Clock> clock_;
436 553
437 base::WeakPtrFactory<HttpCache> weak_factory_; 554 base::WeakPtrFactory<HttpCache> weak_factory_;
438 555
439 DISALLOW_COPY_AND_ASSIGN(HttpCache); 556 DISALLOW_COPY_AND_ASSIGN(HttpCache);
440 }; 557 };
441 558
442 } // namespace net 559 } // namespace net
443 560
444 #endif // NET_HTTP_HTTP_CACHE_H_ 561 #endif // NET_HTTP_HTTP_CACHE_H_
OLDNEW
« no previous file with comments | « no previous file | net/http/http_cache.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698