OLD | NEW |
---|---|
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 #include "net/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 true, | 89 true, |
90 thread_, | 90 thread_, |
91 net_log, | 91 net_log, |
92 backend, | 92 backend, |
93 callback); | 93 callback); |
94 } | 94 } |
95 | 95 |
96 //----------------------------------------------------------------------------- | 96 //----------------------------------------------------------------------------- |
97 | 97 |
98 HttpCache::ActiveEntry::ActiveEntry(disk_cache::Entry* entry) | 98 HttpCache::ActiveEntry::ActiveEntry(disk_cache::Entry* entry) |
99 : disk_entry(entry), | 99 : disk_entry(entry) {} |
100 writer(NULL), | |
101 will_process_pending_queue(false), | |
102 doomed(false) { | |
103 } | |
104 | 100 |
105 HttpCache::ActiveEntry::~ActiveEntry() { | 101 HttpCache::ActiveEntry::~ActiveEntry() { |
106 if (disk_entry) { | 102 if (disk_entry) { |
107 disk_entry->Close(); | 103 disk_entry->Close(); |
108 disk_entry = NULL; | 104 disk_entry = nullptr; |
109 } | 105 } |
110 } | 106 } |
111 | 107 |
112 size_t HttpCache::ActiveEntry::EstimateMemoryUsage() const { | 108 size_t HttpCache::ActiveEntry::EstimateMemoryUsage() const { |
113 // Skip |disk_entry| which is tracked in simple_backend_impl; Skip |readers| | 109 // Skip |disk_entry| which is tracked in simple_backend_impl; Skip |readers| |
114 // and |pending_queue| because the Transactions are owned by their respective | 110 // and |add_to_entry_queue| because the Transactions are owned by their |
115 // URLRequestHttpJobs. | 111 // respective URLRequestHttpJobs. |
116 return 0; | 112 return 0; |
117 } | 113 } |
118 | 114 |
119 bool HttpCache::ActiveEntry::HasNoTransactions() { | 115 bool HttpCache::ActiveEntry::HasNoTransactions() { |
120 return !writer && readers.empty() && pending_queue.empty(); | 116 return !writer && readers.empty() && add_to_entry_queue.empty() && |
117 done_headers_queue.empty() && !headers_transaction; | |
118 } | |
119 | |
120 bool HttpCache::ActiveEntry::HasNoActiveTransactions() { | |
121 return !writer && readers.empty() && !headers_transaction; | |
121 } | 122 } |
122 | 123 |
123 //----------------------------------------------------------------------------- | 124 //----------------------------------------------------------------------------- |
124 | 125 |
125 // This structure keeps track of work items that are attempting to create or | 126 // This structure keeps track of work items that are attempting to create or |
126 // open cache entries or the backend itself. | 127 // open cache entries or the backend itself. |
127 struct HttpCache::PendingOp { | 128 struct HttpCache::PendingOp { |
128 PendingOp() : disk_entry(NULL) {} | 129 PendingOp() : disk_entry(NULL) {} |
129 ~PendingOp() {} | 130 ~PendingOp() {} |
130 | 131 |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 } | 354 } |
354 } | 355 } |
355 } | 356 } |
356 | 357 |
357 HttpCache::~HttpCache() { | 358 HttpCache::~HttpCache() { |
358 // Transactions should see an invalid cache after this point; otherwise they | 359 // Transactions should see an invalid cache after this point; otherwise they |
359 // could see an inconsistent object (half destroyed). | 360 // could see an inconsistent object (half destroyed). |
360 weak_factory_.InvalidateWeakPtrs(); | 361 weak_factory_.InvalidateWeakPtrs(); |
361 | 362 |
362 // If we have any active entries remaining, then we need to deactivate them. | 363 // If we have any active entries remaining, then we need to deactivate them. |
363 // We may have some pending calls to OnProcessPendingQueue, but since those | 364 // We may have some pending tasks to process queued transactions ,but since |
364 // won't run (due to our destruction), we can simply ignore the corresponding | 365 // those won't run (due to our destruction), we can simply ignore the |
365 // will_process_pending_queue flag. | 366 // corresponding flags. |
366 while (!active_entries_.empty()) { | 367 while (!active_entries_.empty()) { |
367 ActiveEntry* entry = active_entries_.begin()->second.get(); | 368 ActiveEntry* entry = active_entries_.begin()->second.get(); |
368 entry->will_process_pending_queue = false; | 369 entry->will_process_queued_transactions = false; |
369 entry->pending_queue.clear(); | 370 entry->add_to_entry_queue.clear(); |
370 entry->readers.clear(); | 371 entry->readers.clear(); |
371 entry->writer = NULL; | 372 entry->done_headers_queue.clear(); |
373 entry->headers_transaction = nullptr; | |
374 entry->writer = nullptr; | |
372 DeactivateEntry(entry); | 375 DeactivateEntry(entry); |
373 } | 376 } |
374 | 377 |
375 doomed_entries_.clear(); | 378 doomed_entries_.clear(); |
376 | 379 |
377 // Before deleting pending_ops_, we have to make sure that the disk cache is | 380 // Before deleting pending_ops_, we have to make sure that the disk cache is |
378 // done with said operations, or it will attempt to use deleted data. | 381 // done with said operations, or it will attempt to use deleted data. |
379 disk_cache_.reset(); | 382 disk_cache_.reset(); |
380 | 383 |
381 for (auto pending_it = pending_ops_.begin(); pending_it != pending_ops_.end(); | 384 for (auto pending_it = pending_ops_.begin(); pending_it != pending_ops_.end(); |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
621 // We keep track of doomed entries so that we can ensure that they are | 624 // We keep track of doomed entries so that we can ensure that they are |
622 // cleaned up properly when the cache is destroyed. | 625 // cleaned up properly when the cache is destroyed. |
623 ActiveEntry* entry_ptr = entry.get(); | 626 ActiveEntry* entry_ptr = entry.get(); |
624 DCHECK_EQ(0u, doomed_entries_.count(entry_ptr)); | 627 DCHECK_EQ(0u, doomed_entries_.count(entry_ptr)); |
625 doomed_entries_[entry_ptr] = std::move(entry); | 628 doomed_entries_[entry_ptr] = std::move(entry); |
626 | 629 |
627 entry_ptr->disk_entry->Doom(); | 630 entry_ptr->disk_entry->Doom(); |
628 entry_ptr->doomed = true; | 631 entry_ptr->doomed = true; |
629 | 632 |
630 DCHECK(entry_ptr->writer || !entry_ptr->readers.empty() || | 633 DCHECK(entry_ptr->writer || !entry_ptr->readers.empty() || |
631 entry_ptr->will_process_pending_queue); | 634 entry_ptr->headers_transaction || |
635 entry_ptr->will_process_queued_transactions); | |
632 return OK; | 636 return OK; |
633 } | 637 } |
634 | 638 |
635 int HttpCache::AsyncDoomEntry(const std::string& key, Transaction* trans) { | 639 int HttpCache::AsyncDoomEntry(const std::string& key, Transaction* trans) { |
636 std::unique_ptr<WorkItem> item = | 640 std::unique_ptr<WorkItem> item = |
637 base::MakeUnique<WorkItem>(WI_DOOM_ENTRY, trans, nullptr); | 641 base::MakeUnique<WorkItem>(WI_DOOM_ENTRY, trans, nullptr); |
638 PendingOp* pending_op = GetPendingOp(key); | 642 PendingOp* pending_op = GetPendingOp(key); |
639 if (pending_op->writer) { | 643 if (pending_op->writer) { |
640 pending_op->pending_queue.push_back(std::move(item)); | 644 pending_op->pending_queue.push_back(std::move(item)); |
641 return ERR_IO_PENDING; | 645 return ERR_IO_PENDING; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
677 DCHECK(entry->doomed); | 681 DCHECK(entry->doomed); |
678 DCHECK(entry->HasNoTransactions()); | 682 DCHECK(entry->HasNoTransactions()); |
679 | 683 |
680 auto it = doomed_entries_.find(entry); | 684 auto it = doomed_entries_.find(entry); |
681 DCHECK(it != doomed_entries_.end()); | 685 DCHECK(it != doomed_entries_.end()); |
682 doomed_entries_.erase(it); | 686 doomed_entries_.erase(it); |
683 } | 687 } |
684 | 688 |
685 HttpCache::ActiveEntry* HttpCache::FindActiveEntry(const std::string& key) { | 689 HttpCache::ActiveEntry* HttpCache::FindActiveEntry(const std::string& key) { |
686 auto it = active_entries_.find(key); | 690 auto it = active_entries_.find(key); |
687 return it != active_entries_.end() ? it->second.get() : NULL; | 691 return it != active_entries_.end() ? it->second.get() : nullptr; |
688 } | 692 } |
689 | 693 |
690 HttpCache::ActiveEntry* HttpCache::ActivateEntry( | 694 HttpCache::ActiveEntry* HttpCache::ActivateEntry( |
691 disk_cache::Entry* disk_entry) { | 695 disk_cache::Entry* disk_entry) { |
692 DCHECK(!FindActiveEntry(disk_entry->GetKey())); | 696 DCHECK(!FindActiveEntry(disk_entry->GetKey())); |
693 ActiveEntry* entry = new ActiveEntry(disk_entry); | 697 ActiveEntry* entry = new ActiveEntry(disk_entry); |
694 active_entries_[disk_entry->GetKey()] = base::WrapUnique(entry); | 698 active_entries_[disk_entry->GetKey()] = base::WrapUnique(entry); |
695 return entry; | 699 return entry; |
696 } | 700 } |
697 | 701 |
698 void HttpCache::DeactivateEntry(ActiveEntry* entry) { | 702 void HttpCache::DeactivateEntry(ActiveEntry* entry) { |
699 DCHECK(!entry->will_process_pending_queue); | 703 DCHECK(!entry->will_process_queued_transactions); |
700 DCHECK(!entry->doomed); | 704 DCHECK(!entry->doomed); |
701 DCHECK(entry->disk_entry); | 705 DCHECK(entry->disk_entry); |
702 DCHECK(entry->HasNoTransactions()); | 706 DCHECK(entry->HasNoTransactions()); |
703 | 707 |
704 std::string key = entry->disk_entry->GetKey(); | 708 std::string key = entry->disk_entry->GetKey(); |
705 if (key.empty()) | 709 if (key.empty()) |
706 return SlowDeactivateEntry(entry); | 710 return SlowDeactivateEntry(entry); |
707 | 711 |
708 auto it = active_entries_.find(key); | 712 auto it = active_entries_.find(key); |
709 DCHECK(it != active_entries_.end()); | 713 DCHECK(it != active_entries_.end()); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
819 } | 823 } |
820 | 824 |
821 void HttpCache::DestroyEntry(ActiveEntry* entry) { | 825 void HttpCache::DestroyEntry(ActiveEntry* entry) { |
822 if (entry->doomed) { | 826 if (entry->doomed) { |
823 FinalizeDoomedEntry(entry); | 827 FinalizeDoomedEntry(entry); |
824 } else { | 828 } else { |
825 DeactivateEntry(entry); | 829 DeactivateEntry(entry); |
826 } | 830 } |
827 } | 831 } |
828 | 832 |
829 int HttpCache::AddTransactionToEntry(ActiveEntry* entry, Transaction* trans) { | 833 int HttpCache::AddTransactionToEntry(ActiveEntry* entry, |
834 Transaction* transaction) { | |
830 DCHECK(entry); | 835 DCHECK(entry); |
831 DCHECK(entry->disk_entry); | 836 DCHECK(entry->disk_entry); |
832 | 837 // Always add a new transaction to the queue to maintain FIFO order. |
833 // We implement a basic reader/writer lock for the disk cache entry. If | 838 entry->add_to_entry_queue.push_back(transaction); |
834 // there is already a writer, then everyone has to wait for the writer to | 839 ProcessQueuedTransactions(entry); |
835 // finish before they can access the cache entry. There can be multiple | 840 return ERR_IO_PENDING; |
836 // readers. | 841 } |
837 // | 842 |
838 // NOTE: If the transaction can only write, then the entry should not be in | 843 int HttpCache::DoneResponseHeaders(ActiveEntry* entry, |
839 // use (since any existing entry should have already been doomed). | 844 Transaction* transaction, |
840 | 845 bool is_match) { |
841 if (entry->writer || entry->will_process_pending_queue) { | 846 // If |transaction| is the current writer, do nothing. This can happen for |
842 entry->pending_queue.push_back(trans); | 847 // range requests since they can go back to headers phase after starting to |
843 return ERR_IO_PENDING; | 848 // write. |
844 } | 849 if (entry->writer == transaction) |
845 | 850 return OK; |
846 if (trans->mode() & Transaction::WRITE) { | 851 |
847 // transaction needs exclusive access to the entry | 852 // Proceed only if |transaction| is a headers_transaction and not already a |
848 if (entry->readers.empty()) { | 853 // reader. |
849 entry->writer = trans; | 854 if (entry->headers_transaction != transaction) |
855 return OK; | |
856 | |
857 entry->headers_transaction = nullptr; | |
858 | |
859 if (!is_match) { | |
860 // Doom or destroy this entry based on whether it has active consumers. | |
861 ProcessEntryFailure(entry); | |
862 return OK; | |
863 } | |
864 | |
865 entry->done_headers_queue.push_back(transaction); | |
866 ProcessQueuedTransactions(entry); | |
867 return ERR_IO_PENDING; | |
868 } | |
869 | |
870 void HttpCache::RemoveAllQueuedTransactions(ActiveEntry* entry, | |
871 TransactionList* list) { | |
872 // Process done_headers_queue before add_to_entry_queue to maintain FIFO | |
873 // order. | |
874 for (auto* transaction : entry->done_headers_queue) | |
875 list->push_back(transaction); | |
876 entry->done_headers_queue.clear(); | |
877 | |
878 for (auto* transaction : entry->add_to_entry_queue) | |
879 list->push_back(transaction); | |
880 entry->add_to_entry_queue.clear(); | |
881 } | |
882 | |
883 void HttpCache::ProcessEntryFailure(ActiveEntry* entry) { | |
884 // Failure case is either writer failing to completely write the response to | |
885 // the cache or validating transaction received a non-304 response. | |
886 TransactionList list; | |
887 if (entry->HasNoActiveTransactions() && | |
888 !entry->will_process_queued_transactions) { | |
889 entry->disk_entry->Doom(); | |
890 RemoveAllQueuedTransactions(entry, &list); | |
891 DestroyEntry(entry); | |
892 } else { | |
893 DoomActiveEntry(entry->disk_entry->GetKey()); | |
894 RemoveAllQueuedTransactions(entry, &list); | |
895 } | |
896 // ERR_CACHE_RACE causes the transaction to restart the whole process. | |
897 for (auto* transaction : list) | |
898 transaction->io_callback().Run(net::ERR_CACHE_RACE); | |
899 } | |
900 | |
901 void HttpCache::ProcessQueuedTransactions(ActiveEntry* entry) { | |
902 // Multiple readers may finish with an entry at once, so we want to batch up | |
903 // calls to OnProcessQueuedTransactions. This flag also tells us that we | |
904 // should not delete the entry before OnProcessQueuedTransactions runs. | |
905 if (entry->will_process_queued_transactions) | |
906 return; | |
907 | |
908 entry->will_process_queued_transactions = true; | |
909 | |
910 // Post a task instead of invoking the io callback of another transaction here | |
911 // to avoid re-entrancy. | |
912 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
913 FROM_HERE, | |
914 base::Bind(&HttpCache::OnProcessQueuedTransactions, GetWeakPtr(), entry)); | |
915 } | |
916 | |
917 void HttpCache::OnProcessQueuedTransactions(ActiveEntry* entry) { | |
918 entry->will_process_queued_transactions = false; | |
919 | |
920 // If no one is interested in this entry, then we can deactivate it. | |
921 if (entry->HasNoTransactions()) { | |
922 DestroyEntry(entry); | |
923 return; | |
924 } | |
925 | |
926 if (entry->done_headers_queue.empty() && entry->add_to_entry_queue.empty()) | |
927 return; | |
928 | |
929 // To maintain FIFO order of transactions, done_headers_queue should be | |
930 // processed first, and then add_to_entry_queue. | |
931 // If another transaction is writing the response, let validated transactions | |
932 // wait till the response is complete. If the response is not yet started, the | |
933 // done_headers_queue transaction should start writing it. | |
934 if (!entry->writer && !entry->done_headers_queue.empty()) { | |
935 ProcessDoneHeadersTransaction(entry); | |
936 return; | |
Randy Smith (Not in Mondays)
2017/03/30 19:18:06
Why return? Is there some reason not to process t
shivanisha
2017/03/31 17:47:06
Yes, we do not want to process multiple transactio
| |
937 } | |
938 | |
939 if (!entry->add_to_entry_queue.empty()) | |
940 ProcessAddToEntryTransaction(entry); | |
941 } | |
942 | |
943 void HttpCache::ProcessAddToEntryTransaction(ActiveEntry* entry) { | |
944 if (entry->headers_transaction) { | |
945 // Note the entry may be new or may already have a response body written to | |
946 // it. In both cases, a transaction with write mode set, needs to wait | |
947 // because only one transaction is allowed to be validating at a time. | |
948 // If transaction is read-only and entry is not yet complete, then it | |
949 // needs to wait. If entry is complete, it needs to wait because its | |
950 // possible that the headers_transaction dooms the entry if its not a match. | |
951 return; | |
952 } | |
953 | |
954 Transaction* transaction = entry->add_to_entry_queue.front(); | |
955 if (entry->writer) { | |
956 // A transaction either starts headers phase or waits. | |
957 if (transaction->mode() & Transaction::WRITE) { | |
958 entry->headers_transaction = transaction; | |
850 } else { | 959 } else { |
851 entry->pending_queue.push_back(trans); | 960 return; |
852 return ERR_IO_PENDING; | |
853 } | 961 } |
854 } else { | 962 } else { |
855 // transaction needs read access to the entry | 963 // This state may be reached either during the start of an entry or when the |
856 entry->readers.insert(trans); | 964 // response is written. |
857 } | 965 if (transaction->mode() & Transaction::WRITE) { |
858 | 966 entry->headers_transaction = transaction; |
859 // We do this before calling EntryAvailable to force any further calls to | 967 } else { |
860 // AddTransactionToEntry to add their transaction to the pending queue, which | 968 // A read-only transaction can start reading the response. |
861 // ensures FIFO ordering. | 969 // If a read transaction is here, then it has to be an already created |
862 if (!entry->writer && !entry->pending_queue.empty()) | 970 // entry as the other case is already handled in the Transaction state |
863 ProcessPendingQueue(entry); | 971 // machine. |
864 | 972 entry->readers.insert(transaction); |
865 return OK; | 973 transaction->ConvertToReadMode(); |
866 } | 974 |
867 | 975 // More readers can join too. |
868 void HttpCache::DoneWithEntry(ActiveEntry* entry, Transaction* trans, | 976 ProcessQueuedTransactions(entry); |
977 } | |
978 } | |
979 | |
980 entry->add_to_entry_queue.erase(entry->add_to_entry_queue.begin()); | |
981 transaction->io_callback().Run(OK); | |
982 } | |
983 | |
984 void HttpCache::ProcessDoneHeadersTransaction(ActiveEntry* entry) { | |
985 DCHECK(!entry->writer && !entry->done_headers_queue.empty()); | |
986 Transaction* transaction = entry->done_headers_queue.front(); | |
987 // If this transaction is responsible for writing the response body. | |
988 if (transaction->mode() & Transaction::WRITE) { | |
989 entry->writer = transaction; | |
990 } else { | |
991 // Response body is already written, convert to a reader. | |
992 auto return_val = entry->readers.insert(transaction); | |
993 DCHECK_EQ(return_val.second, true); | |
994 transaction->ConvertToReadMode(); | |
995 } | |
996 // Post another task to give a chance to more transactions to either join | |
997 // readers or another transaction to start parallel validation. | |
998 ProcessQueuedTransactions(entry); | |
999 entry->done_headers_queue.erase(entry->done_headers_queue.begin()); | |
1000 transaction->io_callback().Run(OK); | |
1001 } | |
1002 | |
1003 bool HttpCache::IsCancelResponseBody(ActiveEntry* entry, | |
1004 Transaction* transaction, | |
1005 const std::string& method) { | |
1006 bool cancel = (method != "HEAD"); | |
1007 | |
1008 if (!cancel) | |
1009 return false; | |
1010 | |
1011 if (transaction == entry->writer) | |
1012 return cancel; | |
1013 | |
1014 // Transaction's mode may have been set to NONE if StopCaching was invoked. | |
1015 // Check if transaction was about to start writing to the cache. | |
1016 cancel &= (transaction->mode() & Transaction::WRITE || | |
1017 transaction->mode() == Transaction::NONE); | |
1018 | |
1019 if (!cancel) | |
1020 return false; | |
1021 | |
1022 if (transaction == entry->headers_transaction) { | |
1023 cancel &= (!entry->writer); | |
1024 return cancel; | |
1025 } | |
1026 | |
1027 if (!entry->done_headers_queue.empty()) { | |
1028 auto i = std::find(entry->done_headers_queue.begin(), | |
1029 entry->done_headers_queue.end(), transaction); | |
1030 if (i != entry->done_headers_queue.end()) { | |
1031 cancel &= (!entry->writer); | |
1032 return cancel; | |
1033 } | |
1034 } | |
1035 | |
1036 return false; | |
1037 } | |
1038 | |
1039 void HttpCache::DoneWithEntry(ActiveEntry* entry, | |
1040 Transaction* transaction, | |
869 bool cancel) { | 1041 bool cancel) { |
870 // If we already posted a task to move on to the next transaction and this was | 1042 // Transaction is waiting in the done_headers_queue. |
871 // the writer, there is nothing to cancel. | 1043 if (!entry->done_headers_queue.empty()) { |
872 if (entry->will_process_pending_queue && entry->readers.empty()) | 1044 auto i = std::find(entry->done_headers_queue.begin(), |
873 return; | 1045 entry->done_headers_queue.end(), transaction); |
874 | 1046 if (i != entry->done_headers_queue.end()) { |
875 if (entry->writer) { | 1047 entry->done_headers_queue.erase(i); |
876 DCHECK(trans == entry->writer); | 1048 if (cancel) |
877 | 1049 ProcessEntryFailure(entry); |
1050 return; | |
1051 } | |
1052 } | |
1053 | |
1054 // Transaction is removed in the headers phase. | |
1055 if (transaction == entry->headers_transaction) { | |
1056 // If the response is not written (cancel is true), consider it a failure. | |
1057 DoneWritingToEntry(entry, !cancel, transaction); | |
1058 return; | |
1059 } | |
1060 | |
1061 // Transaction is removed in the writing phase. | |
1062 if (transaction == entry->writer) { | |
878 // Assume there was a failure. | 1063 // Assume there was a failure. |
879 bool success = false; | 1064 bool success = false; |
880 if (cancel) { | 1065 if (cancel) { |
881 DCHECK(entry->disk_entry); | 1066 DCHECK(entry->disk_entry); |
882 // This is a successful operation in the sense that we want to keep the | 1067 // This is a successful operation in the sense that we want to keep the |
883 // entry. | 1068 // entry. |
884 success = trans->AddTruncatedFlag(); | 1069 success = transaction->AddTruncatedFlag(); |
885 // The previous operation may have deleted the entry. | 1070 // The previous operation may have deleted the entry. |
886 if (!trans->entry()) | 1071 if (!transaction->entry()) |
887 return; | 1072 return; |
888 } | 1073 } |
889 DoneWritingToEntry(entry, success); | 1074 DoneWritingToEntry(entry, success, transaction); |
890 } else { | 1075 return; |
891 DoneReadingFromEntry(entry, trans); | 1076 } |
892 } | 1077 |
893 } | 1078 // Transaction is reading from the entry. |
894 | 1079 DoneReadingFromEntry(entry, transaction); |
895 void HttpCache::DoneWritingToEntry(ActiveEntry* entry, bool success) { | 1080 } |
896 DCHECK(entry->readers.empty()); | 1081 |
897 | 1082 void HttpCache::DoneWritingToEntry(ActiveEntry* entry, |
898 entry->writer = NULL; | 1083 bool success, |
899 | 1084 Transaction* transaction) { |
Randy Smith (Not in Mondays)
2017/03/30 19:18:06
nit, not even a suggestion (partially because look
shivanisha
2017/03/31 17:47:06
I kind of like it that HCT does not have to decide
| |
900 if (success) { | 1085 DCHECK(transaction == entry->writer || |
901 ProcessPendingQueue(entry); | 1086 transaction == entry->headers_transaction); |
902 } else { | 1087 |
903 DCHECK(!entry->will_process_pending_queue); | 1088 // Transaction is done writing to entry in the response body phase. |
904 | 1089 if (transaction == entry->writer) |
905 // We failed to create this entry. | 1090 entry->writer = nullptr; |
906 TransactionList pending_queue; | 1091 else |
907 pending_queue.swap(entry->pending_queue); | 1092 entry->headers_transaction = nullptr; |
908 | 1093 |
909 entry->disk_entry->Doom(); | 1094 // If writer fails, all transactions should fail. |
910 DestroyEntry(entry); | 1095 if (!success && entry->headers_transaction) { |
911 | 1096 entry->headers_transaction->SetValidatingCannotProceed(); |
912 // We need to do something about these pending entries, which now need to | 1097 entry->headers_transaction = nullptr; |
913 // be added to a new entry. | 1098 DCHECK(entry->HasNoActiveTransactions()); |
914 while (!pending_queue.empty()) { | 1099 } |
915 // ERR_CACHE_RACE causes the transaction to restart the whole process. | 1100 if (!success) |
916 pending_queue.front()->io_callback().Run(ERR_CACHE_RACE); | 1101 ProcessEntryFailure(entry); |
917 pending_queue.pop_front(); | 1102 else |
918 } | 1103 ProcessQueuedTransactions(entry); |
919 } | 1104 } |
920 } | 1105 |
921 | 1106 void HttpCache::DoneReadingFromEntry(ActiveEntry* entry, |
922 void HttpCache::DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans) { | 1107 Transaction* transaction) { |
923 DCHECK(!entry->writer); | 1108 DCHECK(!entry->writer); |
924 | 1109 auto it = entry->readers.find(transaction); |
925 auto it = entry->readers.find(trans); | |
926 DCHECK(it != entry->readers.end()); | 1110 DCHECK(it != entry->readers.end()); |
927 | |
928 entry->readers.erase(it); | 1111 entry->readers.erase(it); |
929 | 1112 |
930 ProcessPendingQueue(entry); | 1113 ProcessQueuedTransactions(entry); |
931 } | |
932 | |
933 void HttpCache::ConvertWriterToReader(ActiveEntry* entry) { | |
934 DCHECK(entry->writer); | |
935 DCHECK(entry->writer->mode() == Transaction::READ_WRITE); | |
936 DCHECK(entry->readers.empty()); | |
937 | |
938 Transaction* trans = entry->writer; | |
939 | |
940 entry->writer = NULL; | |
941 entry->readers.insert(trans); | |
942 | |
943 ProcessPendingQueue(entry); | |
944 } | 1114 } |
945 | 1115 |
946 LoadState HttpCache::GetLoadStateForPendingTransaction( | 1116 LoadState HttpCache::GetLoadStateForPendingTransaction( |
947 const Transaction* trans) { | 1117 const Transaction* trans) { |
948 auto i = active_entries_.find(trans->key()); | 1118 auto i = active_entries_.find(trans->key()); |
949 if (i == active_entries_.end()) { | 1119 if (i == active_entries_.end()) { |
950 // If this is really a pending transaction, and it is not part of | 1120 // If this is really a pending transaction, and it is not part of |
951 // active_entries_, we should be creating the backend or the entry. | 1121 // active_entries_, we should be creating the backend or the entry. |
952 return LOAD_STATE_WAITING_FOR_CACHE; | 1122 return LOAD_STATE_WAITING_FOR_CACHE; |
953 } | 1123 } |
(...skipping 29 matching lines...) Expand all Loading... | |
983 | 1153 |
984 for (auto k = doomed_entries_.begin(); k != doomed_entries_.end() && !found; | 1154 for (auto k = doomed_entries_.begin(); k != doomed_entries_.end() && !found; |
985 ++k) { | 1155 ++k) { |
986 found = RemovePendingTransactionFromEntry(k->first, trans); | 1156 found = RemovePendingTransactionFromEntry(k->first, trans); |
987 } | 1157 } |
988 | 1158 |
989 DCHECK(found) << "Pending transaction not found"; | 1159 DCHECK(found) << "Pending transaction not found"; |
990 } | 1160 } |
991 | 1161 |
992 bool HttpCache::RemovePendingTransactionFromEntry(ActiveEntry* entry, | 1162 bool HttpCache::RemovePendingTransactionFromEntry(ActiveEntry* entry, |
993 Transaction* trans) { | 1163 Transaction* transaction) { |
994 TransactionList& pending_queue = entry->pending_queue; | 1164 TransactionList& add_to_entry_queue = entry->add_to_entry_queue; |
995 | 1165 |
996 auto j = find(pending_queue.begin(), pending_queue.end(), trans); | 1166 auto j = |
997 if (j == pending_queue.end()) | 1167 find(add_to_entry_queue.begin(), add_to_entry_queue.end(), transaction); |
1168 if (j == add_to_entry_queue.end()) | |
998 return false; | 1169 return false; |
999 | 1170 |
1000 pending_queue.erase(j); | 1171 add_to_entry_queue.erase(j); |
1001 return true; | 1172 return true; |
1002 } | 1173 } |
1003 | 1174 |
1004 bool HttpCache::RemovePendingTransactionFromPendingOp(PendingOp* pending_op, | 1175 bool HttpCache::RemovePendingTransactionFromPendingOp(PendingOp* pending_op, |
1005 Transaction* trans) { | 1176 Transaction* trans) { |
1006 if (pending_op->writer->Matches(trans)) { | 1177 if (pending_op->writer->Matches(trans)) { |
1007 pending_op->writer->ClearTransaction(); | 1178 pending_op->writer->ClearTransaction(); |
1008 pending_op->writer->ClearEntry(); | 1179 pending_op->writer->ClearEntry(); |
1009 return true; | 1180 return true; |
1010 } | 1181 } |
1011 WorkItemList& pending_queue = pending_op->pending_queue; | 1182 WorkItemList& pending_queue = pending_op->pending_queue; |
1012 | 1183 |
1013 for (auto it = pending_queue.begin(); it != pending_queue.end(); ++it) { | 1184 for (auto it = pending_queue.begin(); it != pending_queue.end(); ++it) { |
1014 if ((*it)->Matches(trans)) { | 1185 if ((*it)->Matches(trans)) { |
1015 pending_queue.erase(it); | 1186 pending_queue.erase(it); |
1016 return true; | 1187 return true; |
1017 } | 1188 } |
1018 } | 1189 } |
1019 return false; | 1190 return false; |
1020 } | 1191 } |
1021 | 1192 |
1022 void HttpCache::ProcessPendingQueue(ActiveEntry* entry) { | |
1023 // Multiple readers may finish with an entry at once, so we want to batch up | |
1024 // calls to OnProcessPendingQueue. This flag also tells us that we should | |
1025 // not delete the entry before OnProcessPendingQueue runs. | |
1026 if (entry->will_process_pending_queue) | |
1027 return; | |
1028 entry->will_process_pending_queue = true; | |
1029 | |
1030 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
1031 FROM_HERE, | |
1032 base::Bind(&HttpCache::OnProcessPendingQueue, GetWeakPtr(), entry)); | |
1033 } | |
1034 | |
1035 void HttpCache::OnProcessPendingQueue(ActiveEntry* entry) { | |
1036 entry->will_process_pending_queue = false; | |
1037 DCHECK(!entry->writer); | |
1038 | |
1039 // If no one is interested in this entry, then we can deactivate it. | |
1040 if (entry->HasNoTransactions()) { | |
1041 DestroyEntry(entry); | |
1042 return; | |
1043 } | |
1044 | |
1045 if (entry->pending_queue.empty()) | |
1046 return; | |
1047 | |
1048 // Promote next transaction from the pending queue. | |
1049 Transaction* next = entry->pending_queue.front(); | |
1050 if ((next->mode() & Transaction::WRITE) && !entry->readers.empty()) | |
1051 return; // Have to wait. | |
1052 | |
1053 entry->pending_queue.erase(entry->pending_queue.begin()); | |
1054 | |
1055 int rv = AddTransactionToEntry(entry, next); | |
1056 if (rv != ERR_IO_PENDING) { | |
1057 next->io_callback().Run(rv); | |
1058 } | |
1059 } | |
1060 | |
1061 void HttpCache::OnIOComplete(int result, PendingOp* pending_op) { | 1193 void HttpCache::OnIOComplete(int result, PendingOp* pending_op) { |
1062 WorkItemOperation op = pending_op->writer->operation(); | 1194 WorkItemOperation op = pending_op->writer->operation(); |
1063 | 1195 |
1064 // Completing the creation of the backend is simpler than the other cases. | 1196 // Completing the creation of the backend is simpler than the other cases. |
1065 if (op == WI_CREATE_BACKEND) | 1197 if (op == WI_CREATE_BACKEND) |
1066 return OnBackendCreated(result, pending_op); | 1198 return OnBackendCreated(result, pending_op); |
1067 | 1199 |
1068 std::unique_ptr<WorkItem> item = std::move(pending_op->writer); | 1200 std::unique_ptr<WorkItem> item = std::move(pending_op->writer); |
1069 bool fail_requests = false; | 1201 bool fail_requests = false; |
1070 | 1202 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1195 building_backend_ = false; | 1327 building_backend_ = false; |
1196 DeletePendingOp(pending_op); | 1328 DeletePendingOp(pending_op); |
1197 } | 1329 } |
1198 | 1330 |
1199 // The cache may be gone when we return from the callback. | 1331 // The cache may be gone when we return from the callback. |
1200 if (!item->DoCallback(result, disk_cache_.get())) | 1332 if (!item->DoCallback(result, disk_cache_.get())) |
1201 item->NotifyTransaction(result, NULL); | 1333 item->NotifyTransaction(result, NULL); |
1202 } | 1334 } |
1203 | 1335 |
1204 } // namespace net | 1336 } // namespace net |
OLD | NEW |