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

Side by Side Diff: net/http/http_cache_transaction.cc

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: Use TransitionToState (Rebased till refs/heads/master@{#459057}) 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
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 #include "net/http/http_cache_transaction.h" 5 #include "net/http/http_cache_transaction.h"
6 6
7 #include "build/build_config.h" // For OS_POSIX 7 #include "build/build_config.h" // For OS_POSIX
8 8
9 #if defined(OS_POSIX) 9 #if defined(OS_POSIX)
10 #include <unistd.h> 10 #include <unistd.h>
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 161
162 //----------------------------------------------------------------------------- 162 //-----------------------------------------------------------------------------
163 163
164 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache) 164 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache)
165 : next_state_(STATE_NONE), 165 : next_state_(STATE_NONE),
166 request_(NULL), 166 request_(NULL),
167 priority_(priority), 167 priority_(priority),
168 cache_(cache->GetWeakPtr()), 168 cache_(cache->GetWeakPtr()),
169 entry_(NULL), 169 entry_(NULL),
170 new_entry_(NULL), 170 new_entry_(NULL),
171 created_entry_(false),
171 new_response_(NULL), 172 new_response_(NULL),
172 mode_(NONE), 173 mode_(NONE),
174 original_mode_(NONE),
173 reading_(false), 175 reading_(false),
174 invalid_range_(false), 176 invalid_range_(false),
175 truncated_(false), 177 truncated_(false),
176 is_sparse_(false), 178 is_sparse_(false),
177 range_requested_(false), 179 range_requested_(false),
178 handling_206_(false), 180 handling_206_(false),
179 cache_pending_(false), 181 cache_pending_(false),
180 done_reading_(false), 182 done_reading_(false),
181 vary_mismatch_(false), 183 vary_mismatch_(false),
182 couldnt_conditionalize_request_(false), 184 couldnt_conditionalize_request_(false),
183 bypass_lock_for_test_(false), 185 bypass_lock_for_test_(false),
184 fail_conditionalization_for_test_(false), 186 fail_conditionalization_for_test_(false),
187 validating_cannot_proceed_(false),
185 io_buf_len_(0), 188 io_buf_len_(0),
186 read_offset_(0), 189 read_offset_(0),
187 effective_load_flags_(0), 190 effective_load_flags_(0),
188 write_len_(0), 191 write_len_(0),
189 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED), 192 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED),
190 validation_cause_(VALIDATION_CAUSE_UNDEFINED), 193 validation_cause_(VALIDATION_CAUSE_UNDEFINED),
191 total_received_bytes_(0), 194 total_received_bytes_(0),
192 total_sent_bytes_(0), 195 total_sent_bytes_(0),
193 websocket_handshake_stream_base_create_helper_(NULL), 196 websocket_handshake_stream_base_create_helper_(NULL),
194 in_do_loop_(false), 197 in_do_loop_(false),
(...skipping 17 matching lines...) Expand all
212 if (entry_) { 215 if (entry_) {
213 bool cancel_request = reading_ && response_.headers.get(); 216 bool cancel_request = reading_ && response_.headers.get();
214 if (cancel_request) { 217 if (cancel_request) {
215 if (partial_) { 218 if (partial_) {
216 entry_->disk_entry->CancelSparseIO(); 219 entry_->disk_entry->CancelSparseIO();
217 } else { 220 } else {
218 cancel_request &= (response_.headers->response_code() == 200); 221 cancel_request &= (response_.headers->response_code() == 200);
219 } 222 }
220 } 223 }
221 224
225 // If this transaction created the entry, then it is a cancellation.
226 if (!cancel_request)
227 cancel_request = !reading_ && created_entry_;
228
222 cache_->DoneWithEntry(entry_, this, cancel_request); 229 cache_->DoneWithEntry(entry_, this, cancel_request);
223 } else if (cache_pending_) { 230 } else if (cache_pending_) {
224 cache_->RemovePendingTransaction(this); 231 cache_->RemovePendingTransaction(this);
225 } 232 }
226 } 233 }
227 } 234 }
228 235
229 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, 236 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
230 const CompletionCallback& callback) { 237 const CompletionCallback& callback) {
231 DCHECK(buf); 238 DCHECK(buf);
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 ConnectionAttempts* out) const { 579 ConnectionAttempts* out) const {
573 ConnectionAttempts new_connection_attempts; 580 ConnectionAttempts new_connection_attempts;
574 if (network_trans_) 581 if (network_trans_)
575 network_trans_->GetConnectionAttempts(&new_connection_attempts); 582 network_trans_->GetConnectionAttempts(&new_connection_attempts);
576 583
577 out->swap(new_connection_attempts); 584 out->swap(new_connection_attempts);
578 out->insert(out->begin(), old_connection_attempts_.begin(), 585 out->insert(out->begin(), old_connection_attempts_.begin(),
579 old_connection_attempts_.end()); 586 old_connection_attempts_.end());
580 } 587 }
581 588
589 void HttpCache::Transaction::SetValidatingCannotProceed() {
590 validating_cannot_proceed_ = true;
591 entry_ = nullptr;
592 }
593
582 size_t HttpCache::Transaction::EstimateMemoryUsage() const { 594 size_t HttpCache::Transaction::EstimateMemoryUsage() const {
583 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108. 595 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108.
584 return 0; 596 return 0;
585 } 597 }
586 598
587 //----------------------------------------------------------------------------- 599 //-----------------------------------------------------------------------------
588 600
589 // A few common patterns: (Foo* means Foo -> FooComplete) 601 // A few common patterns: (Foo* means Foo -> FooComplete)
590 // 602 //
591 // 1. Not-cached entry: 603 // 1. Not-cached entry:
592 // Start(): 604 // Start():
593 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* -> 605 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
594 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> 606 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
595 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> 607 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
596 // PartialHeadersReceived 608 // PartialHeadersReceived
597 // 609 //
598 // Read(): 610 // Read():
599 // NetworkRead* -> CacheWriteData* 611 // NetworkRead* -> CacheWriteData*
600 // 612 //
601 // 2. Cached entry, no validation: 613 // 2. Cached entry, no validation:
602 // Start(): 614 // Start():
603 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 615 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
604 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 616 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
605 // BeginCacheValidation() -> SetupEntryForRead() 617 // BeginCacheValidation() -> SetupEntryForRead() -> WaitBeforeRead*
606 // 618 //
607 // Read(): 619 // Read():
608 // CacheReadData* 620 // CacheReadData*
609 // 621 //
610 // 3. Cached entry, validation (304): 622 // 3. Cached entry, validation (304):
611 // Start(): 623 // Start():
612 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 624 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
613 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 625 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
614 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 626 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
615 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> 627 // UpdateCachedResponse -> CacheWriteUpdatedResponse* ->
616 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> 628 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
617 // PartialHeadersReceived 629 // PartialHeadersReceived -> WaitBeforeRead*
618 // 630 //
619 // Read(): 631 // Read():
620 // CacheReadData* 632 // CacheReadData*
621 // 633 //
622 // 4. Cached entry, validation and replace (200): 634 // 4. Cached entry, validation and replace (200):
623 // Start(): 635 // Start():
624 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 636 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
625 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 637 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
626 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 638 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
627 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* -> 639 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 // 724 //
713 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true: 725 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
714 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between 726 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
715 // CacheReadResponse* and CacheDispatchValidation. 727 // CacheReadResponse* and CacheDispatchValidation.
716 int HttpCache::Transaction::DoLoop(int result) { 728 int HttpCache::Transaction::DoLoop(int result) {
717 DCHECK_NE(STATE_UNSET, next_state_); 729 DCHECK_NE(STATE_UNSET, next_state_);
718 DCHECK_NE(STATE_NONE, next_state_); 730 DCHECK_NE(STATE_NONE, next_state_);
719 DCHECK(!in_do_loop_); 731 DCHECK(!in_do_loop_);
720 732
721 int rv = result; 733 int rv = result;
734 State state = next_state_;
722 do { 735 do {
723 State state = next_state_; 736 state = next_state_;
724 next_state_ = STATE_UNSET; 737 next_state_ = STATE_UNSET;
725 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true); 738 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true);
726 739
727 switch (state) { 740 switch (state) {
728 case STATE_GET_BACKEND: 741 case STATE_GET_BACKEND:
729 DCHECK_EQ(OK, rv); 742 DCHECK_EQ(OK, rv);
730 rv = DoGetBackend(); 743 rv = DoGetBackend();
731 break; 744 break;
732 case STATE_GET_BACKEND_COMPLETE: 745 case STATE_GET_BACKEND_COMPLETE:
733 rv = DoGetBackendComplete(rv); 746 rv = DoGetBackendComplete(rv);
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
850 DCHECK_EQ(OK, rv); 863 DCHECK_EQ(OK, rv);
851 rv = DoPartialHeadersReceived(); 864 rv = DoPartialHeadersReceived();
852 break; 865 break;
853 case STATE_CACHE_READ_METADATA: 866 case STATE_CACHE_READ_METADATA:
854 DCHECK_EQ(OK, rv); 867 DCHECK_EQ(OK, rv);
855 rv = DoCacheReadMetadata(); 868 rv = DoCacheReadMetadata();
856 break; 869 break;
857 case STATE_CACHE_READ_METADATA_COMPLETE: 870 case STATE_CACHE_READ_METADATA_COMPLETE:
858 rv = DoCacheReadMetadataComplete(rv); 871 rv = DoCacheReadMetadataComplete(rv);
859 break; 872 break;
873 case STATE_WAIT_BEFORE_READ:
874 DCHECK_EQ(OK, rv);
875 rv = DoWaitBeforeRead();
876 break;
877 case STATE_WAIT_BEFORE_READ_COMPLETE:
878 rv = DoWaitBeforeReadComplete(rv);
879 break;
860 case STATE_NETWORK_READ: 880 case STATE_NETWORK_READ:
861 DCHECK_EQ(OK, rv); 881 DCHECK_EQ(OK, rv);
862 rv = DoNetworkRead(); 882 rv = DoNetworkRead();
863 break; 883 break;
864 case STATE_NETWORK_READ_COMPLETE: 884 case STATE_NETWORK_READ_COMPLETE:
865 rv = DoNetworkReadComplete(rv); 885 rv = DoNetworkReadComplete(rv);
866 break; 886 break;
867 case STATE_CACHE_READ_DATA: 887 case STATE_CACHE_READ_DATA:
868 DCHECK_EQ(OK, rv); 888 DCHECK_EQ(OK, rv);
869 rv = DoCacheReadData(); 889 rv = DoCacheReadData();
(...skipping 16 matching lines...) Expand all
886 break; 906 break;
887 default: 907 default:
888 NOTREACHED() << "bad state " << state; 908 NOTREACHED() << "bad state " << state;
889 rv = ERR_FAILED; 909 rv = ERR_FAILED;
890 break; 910 break;
891 } 911 }
892 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state; 912 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state;
893 913
894 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); 914 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
895 915
916 // Assert Start() state machine allowed exit state.
917 if (!reading_ && rv == OK) {
918 DCHECK(!entry_ || state == STATE_WAIT_BEFORE_READ_COMPLETE);
jkarlin 2017/03/23 18:37:38 DCHECK(reading_ || rv != OK || !entry_ || state ==
shivanisha 2017/03/29 03:39:29 Done . Also updated the comment.
919 }
920
896 if (rv != ERR_IO_PENDING && !callback_.is_null()) { 921 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
897 read_buf_ = NULL; // Release the buffer before invoking the callback. 922 read_buf_ = nullptr; // Release the buffer before invoking the callback.
898 base::ResetAndReturn(&callback_).Run(rv); 923 base::ResetAndReturn(&callback_).Run(rv);
899 } 924 }
900 925
901 return rv; 926 return rv;
902 } 927 }
903 928
904 int HttpCache::Transaction::DoGetBackend() { 929 int HttpCache::Transaction::DoGetBackend() {
905 cache_pending_ = true; 930 cache_pending_ = true;
906 TransitionToState(STATE_GET_BACKEND_COMPLETE); 931 TransitionToState(STATE_GET_BACKEND_COMPLETE);
907 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); 932 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
969 partial_->RestoreHeaders(&custom_request_->extra_headers); 994 partial_->RestoreHeaders(&custom_request_->extra_headers);
970 partial_.reset(); 995 partial_.reset();
971 } 996 }
972 TransitionToState(STATE_SEND_REQUEST); 997 TransitionToState(STATE_SEND_REQUEST);
973 } else { 998 } else {
974 TransitionToState(STATE_INIT_ENTRY); 999 TransitionToState(STATE_INIT_ENTRY);
975 } 1000 }
976 1001
977 // This is only set if we have something to do with the response. 1002 // This is only set if we have something to do with the response.
978 range_requested_ = (partial_.get() != NULL); 1003 range_requested_ = (partial_.get() != NULL);
979 1004 original_mode_ = mode_;
980 return OK; 1005 return OK;
981 } 1006 }
982 1007
983 int HttpCache::Transaction::DoInitEntry() { 1008 int HttpCache::Transaction::DoInitEntry() {
984 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry"); 1009 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry");
985 DCHECK(!new_entry_); 1010 DCHECK(!new_entry_);
986 1011
987 if (!cache_.get()) { 1012 if (!cache_.get()) {
988 TransitionToState(STATE_NONE); 1013 TransitionToState(STATE_NONE);
989 return ERR_UNEXPECTED; 1014 return ERR_UNEXPECTED;
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1084 int HttpCache::Transaction::DoCreateEntryComplete(int result) { 1109 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1085 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntryComplete"); 1110 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntryComplete");
1086 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is 1111 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1087 // OK, otherwise the cache will end up with an active entry without any 1112 // OK, otherwise the cache will end up with an active entry without any
1088 // transaction attached. 1113 // transaction attached.
1089 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_CREATE_ENTRY, 1114 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_CREATE_ENTRY,
1090 result); 1115 result);
1091 cache_pending_ = false; 1116 cache_pending_ = false;
1092 switch (result) { 1117 switch (result) {
1093 case OK: 1118 case OK:
1119 created_entry_ = true;
1094 TransitionToState(STATE_ADD_TO_ENTRY); 1120 TransitionToState(STATE_ADD_TO_ENTRY);
1095 break; 1121 break;
1096 1122
1097 case ERR_CACHE_RACE: 1123 case ERR_CACHE_RACE:
1098 TransitionToState(STATE_INIT_ENTRY); 1124 TransitionToState(STATE_INIT_ENTRY);
1099 break; 1125 break;
1100 1126
1101 default: 1127 default:
1102 // We have a race here: Maybe we failed to open the entry and decided to 1128 // We have a race here: Maybe we failed to open the entry and decided to
1103 // create one, but by the time we called create, another transaction 1129 // create one, but by the time we called create, another transaction
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
1347 TransitionToState(STATE_NONE); 1373 TransitionToState(STATE_NONE);
1348 return ERR_UNEXPECTED; 1374 return ERR_UNEXPECTED;
1349 } 1375 }
1350 1376
1351 return ValidateEntryHeadersAndContinue(); 1377 return ValidateEntryHeadersAndContinue();
1352 } 1378 }
1353 1379
1354 // We may end up here multiple times for a given request. 1380 // We may end up here multiple times for a given request.
1355 int HttpCache::Transaction::DoStartPartialCacheValidation() { 1381 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1356 if (mode_ == NONE) { 1382 if (mode_ == NONE) {
1357 TransitionToState(STATE_NONE); 1383 if (!reading_)
1384 TransitionToState(STATE_WAIT_BEFORE_READ);
1385 else
1386 TransitionToState(STATE_NONE);
1358 return OK; 1387 return OK;
1359 } 1388 }
1360 1389
1361 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION); 1390 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION);
1362 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); 1391 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1363 } 1392 }
1364 1393
1365 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { 1394 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1366 if (!result) { 1395 if (!result) {
1367 // This is the end of the request. 1396 // This is the end of the request.
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1462 // We received the response headers and there is no error. 1491 // We received the response headers and there is no error.
1463 int HttpCache::Transaction::DoSuccessfulSendRequest() { 1492 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1464 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest"); 1493 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest");
1465 DCHECK(!new_response_); 1494 DCHECK(!new_response_);
1466 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); 1495 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1467 1496
1468 if (new_response->headers->response_code() == 401 || 1497 if (new_response->headers->response_code() == 401 ||
1469 new_response->headers->response_code() == 407) { 1498 new_response->headers->response_code() == 407) {
1470 SetAuthResponse(*new_response); 1499 SetAuthResponse(*new_response);
1471 if (!reading_) { 1500 if (!reading_) {
1472 TransitionToState(STATE_NONE); 1501 TransitionToState(STATE_WAIT_BEFORE_READ);
1473 return OK; 1502 return OK;
1474 } 1503 }
1475 1504
1476 // We initiated a second request the caller doesn't know about. We should be 1505 // We initiated a second request the caller doesn't know about. We should be
1477 // able to authenticate this request because we should have authenticated 1506 // able to authenticate this request because we should have authenticated
1478 // this URL moments ago. 1507 // this URL moments ago.
1479 if (IsReadyToRestartForAuth()) { 1508 if (IsReadyToRestartForAuth()) {
1480 DCHECK(!response_.auth_challenge.get()); 1509 DCHECK(!response_.auth_challenge.get());
1481 TransitionToState(STATE_SEND_REQUEST_COMPLETE); 1510 TransitionToState(STATE_SEND_REQUEST_COMPLETE);
1482 // In theory we should check to see if there are new cookies, but there 1511 // In theory we should check to see if there are new cookies, but there
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1525 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); 1554 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE);
1526 } 1555 }
1527 1556
1528 // Invalidate any cached GET with a successful PUT or DELETE. 1557 // Invalidate any cached GET with a successful PUT or DELETE.
1529 if (mode_ == WRITE && 1558 if (mode_ == WRITE &&
1530 (request_->method == "PUT" || request_->method == "DELETE")) { 1559 (request_->method == "PUT" || request_->method == "DELETE")) {
1531 if (NonErrorResponse(new_response->headers->response_code())) { 1560 if (NonErrorResponse(new_response->headers->response_code())) {
1532 int ret = cache_->DoomEntry(cache_key_, NULL); 1561 int ret = cache_->DoomEntry(cache_key_, NULL);
1533 DCHECK_EQ(OK, ret); 1562 DCHECK_EQ(OK, ret);
1534 } 1563 }
1535 cache_->DoneWritingToEntry(entry_, true); 1564 cache_->DoneWritingToEntry(entry_, true, this);
1536 entry_ = NULL; 1565 entry_ = NULL;
1537 mode_ = NONE; 1566 mode_ = NONE;
1538 } 1567 }
1539 1568
1540 // Invalidate any cached GET with a successful POST. 1569 // Invalidate any cached GET with a successful POST.
1541 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && 1570 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1542 request_->method == "POST" && 1571 request_->method == "POST" &&
1543 NonErrorResponse(new_response->headers->response_code())) { 1572 NonErrorResponse(new_response->headers->response_code())) {
1544 cache_->DoomMainEntryForUrl(request_->url); 1573 cache_->DoomMainEntryForUrl(request_->url);
1545 } 1574 }
1546 1575
1547 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); 1576 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1548 1577
1549 if (new_response_->headers->response_code() == 416 && 1578 if (new_response_->headers->response_code() == 416 &&
1550 (request_->method == "GET" || request_->method == "POST")) { 1579 (request_->method == "GET" || request_->method == "POST")) {
1551 // If there is an active entry it may be destroyed with this transaction. 1580 // If there is an active entry it may be destroyed with this transaction.
1552 SetResponse(*new_response_); 1581 SetResponse(*new_response_);
1553 TransitionToState(STATE_NONE); 1582 if (!reading_)
1583 TransitionToState(STATE_WAIT_BEFORE_READ);
1584 else
1585 TransitionToState(STATE_NONE);
1554 return OK; 1586 return OK;
1555 } 1587 }
1556 1588
1557 // Are we expecting a response to a conditional query? 1589 // Are we expecting a response to a conditional query?
1558 if (mode_ == READ_WRITE || mode_ == UPDATE) { 1590 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1559 if (new_response->headers->response_code() == 304 || handling_206_) { 1591 if (new_response->headers->response_code() == 304 || handling_206_) {
1560 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); 1592 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED);
1561 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); 1593 TransitionToState(STATE_UPDATE_CACHED_RESPONSE);
1562 return OK; 1594 return OK;
1563 } 1595 }
1564 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED); 1596 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED);
1565 mode_ = WRITE; 1597 mode_ = WRITE;
1566 } 1598 }
1567 1599
1568 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE); 1600 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE);
1601
1602 if (!entry_)
1603 return OK;
1604
1605 // Invalidate any current entry with a successful response if this transaction
1606 // cannot write to this entry.
1607 if (new_response->headers->response_code() != 304 &&
1608 (entry_->writer || !entry_->readers.empty())) {
1609 DCHECK_EQ(entry_->headers_transaction, this);
1610 cache_->DoneResponseHeaders(entry_, this, false);
1611 entry_ = nullptr;
1612 mode_ = NONE;
1613 return OK;
1614 }
1615
1569 return OK; 1616 return OK;
1570 } 1617 }
1571 1618
1572 // We received 304 or 206 and we want to update the cached response headers. 1619 // We received 304 or 206 and we want to update the cached response headers.
1573 int HttpCache::Transaction::DoUpdateCachedResponse() { 1620 int HttpCache::Transaction::DoUpdateCachedResponse() {
1574 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse"); 1621 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse");
1575 int rv = OK; 1622 int rv = OK;
1576 // Update the cached response based on the headers and properties of 1623 // Update the cached response based on the headers and properties of
1577 // new_response_. 1624 // new_response_.
1578 response_.headers->Update(*new_response_->headers.get()); 1625 response_.headers->Update(*new_response_->headers.get());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1631 DCHECK(!handling_206_); 1678 DCHECK(!handling_206_);
1632 // We got a "not modified" response and already updated the corresponding 1679 // We got a "not modified" response and already updated the corresponding
1633 // cache entry above. 1680 // cache entry above.
1634 // 1681 //
1635 // By closing the cached entry now, we make sure that the 304 rather than 1682 // By closing the cached entry now, we make sure that the 304 rather than
1636 // the cached 200 response, is what will be returned to the user. 1683 // the cached 200 response, is what will be returned to the user.
1637 DoneWritingToEntry(true); 1684 DoneWritingToEntry(true);
1638 } else if (entry_ && !handling_206_) { 1685 } else if (entry_ && !handling_206_) {
1639 DCHECK_EQ(READ_WRITE, mode_); 1686 DCHECK_EQ(READ_WRITE, mode_);
1640 if (!partial_ || partial_->IsLastRange()) { 1687 if (!partial_ || partial_->IsLastRange()) {
1641 cache_->ConvertWriterToReader(entry_);
1642 mode_ = READ; 1688 mode_ = READ;
1643 } 1689 }
1644 // We no longer need the network transaction, so destroy it. 1690 // We no longer need the network transaction, so destroy it.
1645 ResetNetworkTransaction(); 1691 ResetNetworkTransaction();
1646 } else if (entry_ && handling_206_ && truncated_ && 1692 } else if (entry_ && handling_206_ && truncated_ &&
1647 partial_->initial_validation()) { 1693 partial_->initial_validation()) {
1648 // We just finished the validation of a truncated entry, and the server 1694 // We just finished the validation of a truncated entry, and the server
1649 // is willing to resume the operation. Now we go back and start serving 1695 // is willing to resume the operation. Now we go back and start serving
1650 // the first part to the user. 1696 // the first part to the user.
1651 ResetNetworkTransaction(); 1697 ResetNetworkTransaction();
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1751 } 1797 }
1752 1798
1753 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 1799 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
1754 return OK; 1800 return OK;
1755 } 1801 }
1756 1802
1757 int HttpCache::Transaction::DoPartialHeadersReceived() { 1803 int HttpCache::Transaction::DoPartialHeadersReceived() {
1758 new_response_ = NULL; 1804 new_response_ = NULL;
1759 1805
1760 if (!partial_) { 1806 if (!partial_) {
1761 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) 1807 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) {
1762 TransitionToState(STATE_CACHE_READ_METADATA); 1808 TransitionToState(STATE_CACHE_READ_METADATA);
1763 else 1809 } else {
1764 TransitionToState(STATE_NONE); 1810 DCHECK(!reading_);
1811 TransitionToState(STATE_WAIT_BEFORE_READ);
1812 }
1765 return OK; 1813 return OK;
1766 } 1814 }
1767 1815
1768 if (reading_) { 1816 if (reading_) {
1769 if (network_trans_.get()) { 1817 if (network_trans_.get()) {
1770 TransitionToState(STATE_NETWORK_READ); 1818 TransitionToState(STATE_NETWORK_READ);
1771 } else { 1819 } else {
1772 TransitionToState(STATE_CACHE_READ_DATA); 1820 TransitionToState(STATE_CACHE_READ_DATA);
1773 } 1821 }
1774 } else if (mode_ != NONE) { 1822 } else if (mode_ != NONE) {
1775 // We are about to return the headers for a byte-range request to the user, 1823 // We are about to return the headers for a byte-range request to the user,
1776 // so let's fix them. 1824 // so let's fix them.
1777 partial_->FixResponseHeaders(response_.headers.get(), true); 1825 partial_->FixResponseHeaders(response_.headers.get(), true);
1778 TransitionToState(STATE_NONE); 1826 TransitionToState(STATE_WAIT_BEFORE_READ);
1779 } else { 1827 } else {
1780 TransitionToState(STATE_NONE); 1828 TransitionToState(STATE_WAIT_BEFORE_READ);
1781 } 1829 }
1782 return OK; 1830 return OK;
1783 } 1831 }
1784 1832
1833 int HttpCache::Transaction::DoWaitBeforeRead() {
1834 if (!entry_) {
1835 TransitionToState(STATE_NONE);
1836 return OK;
1837 }
1838
1839 // A transaction comes here between the completion of headers phase and the
1840 // start of response body phase, thus it should never be the active writer at
1841 // this point.
1842 DCHECK_NE(entry_->writer, this);
1843
1844 TransitionToState(STATE_WAIT_BEFORE_READ_COMPLETE);
1845
1846 // If it was an auth failure or 416, this transaction should continue to be
1847 // headers_transaction till consumer takes an action, so no need to do
1848 // anything now.
1849 if (auth_response_.headers.get() ||
1850 (new_response_ && new_response_->headers &&
1851 new_response_->headers->response_code() == 416))
1852 return OK;
1853
1854 // If there is no response body to be written or read, it does not need to
1855 // wait.
1856 if (request_->method == "HEAD")
1857 return OK;
1858
1859 // Proceed only if |this| completed the headers phase. For read-only
1860 // transactions since they do not have a headers phase, they would have
1861 // already been added to entry_->readers, so do nothing.
1862 if (entry_->headers_transaction == this)
1863 return cache_->DoneResponseHeaders(entry_, this, true);
1864
1865 return OK;
1866 }
1867
1868 int HttpCache::Transaction::DoWaitBeforeReadComplete(int rv) {
1869 if (rv == ERR_CACHE_RACE) {
1870 RestartAfterValidationStarted();
1871 return OK;
1872 }
1873
1874 TransitionToState(STATE_NONE);
1875 if (rv != OK)
1876 return rv;
1877
1878 // If successful then this must be either be the writer or a reader as the
1879 // response must have completely been written to the cache.
1880 if (entry_->writer == this)
1881 return OK;
1882
1883 // If it was an auth failure or 416, this transaction should continue to be
1884 // headers_transaction, so no need to do anything now.
1885 if (auth_response_.headers.get() ||
1886 (new_response_ && new_response_->headers &&
1887 new_response_->headers->response_code() == 416)) {
1888 DCHECK_EQ(entry_->headers_transaction, this);
1889 return OK;
1890 }
1891
1892 mode_ = READ;
1893 if (network_trans_)
1894 ResetNetworkTransaction();
1895 return OK;
1896 }
1897
1898 void HttpCache::Transaction::RestartAfterValidationStarted() {
1899 DCHECK(!reading_);
1900 next_state_ = STATE_INIT_ENTRY;
shivanisha 2017/03/23 17:19:02 Did not use TransitionToState here as it can also
1901 cache_entry_status_ = CacheEntryStatus::ENTRY_UNDEFINED;
1902 entry_ = nullptr;
1903 mode_ = original_mode_;
1904 if (network_trans_)
1905 network_trans_.reset();
1906 }
1907
1785 int HttpCache::Transaction::DoCacheReadMetadata() { 1908 int HttpCache::Transaction::DoCacheReadMetadata() {
1786 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); 1909 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata");
1787 DCHECK(entry_); 1910 DCHECK(entry_);
1788 DCHECK(!response_.metadata.get()); 1911 DCHECK(!response_.metadata.get());
1789 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE); 1912 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE);
1790 1913
1791 response_.metadata = 1914 response_.metadata =
1792 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); 1915 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1793 1916
1794 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); 1917 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO);
1795 return entry_->disk_entry->ReadData(kMetadataIndex, 0, 1918 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1796 response_.metadata.get(), 1919 response_.metadata.get(),
1797 response_.metadata->size(), 1920 response_.metadata->size(),
1798 io_callback_); 1921 io_callback_);
1799 } 1922 }
1800 1923
1801 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { 1924 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1802 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete"); 1925 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete");
1803 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, 1926 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO,
1804 result); 1927 result);
1805 if (result != response_.metadata->size()) 1928 if (result != response_.metadata->size())
1806 return OnCacheReadError(result, false); 1929 return OnCacheReadError(result, false);
1807 TransitionToState(STATE_NONE); 1930
1931 if (!reading_)
1932 TransitionToState(STATE_WAIT_BEFORE_READ);
1933 else
1934 TransitionToState(STATE_NONE);
1808 return OK; 1935 return OK;
1809 } 1936 }
1810 1937
1811 int HttpCache::Transaction::DoNetworkRead() { 1938 int HttpCache::Transaction::DoNetworkRead() {
1812 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead"); 1939 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead");
1813 TransitionToState(STATE_NETWORK_READ_COMPLETE); 1940 TransitionToState(STATE_NETWORK_READ_COMPLETE);
1814 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); 1941 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1815 } 1942 }
1816 1943
1817 int HttpCache::Transaction::DoNetworkReadComplete(int result) { 1944 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
2114 if (RequiresValidation() != VALIDATION_NONE) { 2241 if (RequiresValidation() != VALIDATION_NONE) {
2115 TransitionToState(STATE_NONE); 2242 TransitionToState(STATE_NONE);
2116 return ERR_CACHE_MISS; 2243 return ERR_CACHE_MISS;
2117 } 2244 }
2118 2245
2119 if (request_->method == "HEAD") 2246 if (request_->method == "HEAD")
2120 FixHeadersForHead(); 2247 FixHeadersForHead();
2121 2248
2122 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2249 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2123 TransitionToState(STATE_CACHE_READ_METADATA); 2250 TransitionToState(STATE_CACHE_READ_METADATA);
2251 else if (!reading_)
2252 TransitionToState(STATE_WAIT_BEFORE_READ);
2124 else 2253 else
2125 TransitionToState(STATE_NONE); 2254 TransitionToState(STATE_NONE);
2126 2255
2127 return OK; 2256 return OK;
2128 } 2257 }
2129 2258
2130 int HttpCache::Transaction::BeginCacheValidation() { 2259 int HttpCache::Transaction::BeginCacheValidation() {
2131 DCHECK_EQ(mode_, READ_WRITE); 2260 DCHECK_EQ(mode_, READ_WRITE);
2132 2261
2133 ValidationType required_validation = RequiresValidation(); 2262 ValidationType required_validation = RequiresValidation();
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after
2628 if (partial_) { 2757 if (partial_) {
2629 if (truncated_ || is_sparse_ || !invalid_range_) { 2758 if (truncated_ || is_sparse_ || !invalid_range_) {
2630 // We are going to return the saved response headers to the caller, so 2759 // We are going to return the saved response headers to the caller, so
2631 // we may need to adjust them first. 2760 // we may need to adjust them first.
2632 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 2761 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
2633 return OK; 2762 return OK;
2634 } else { 2763 } else {
2635 partial_.reset(); 2764 partial_.reset();
2636 } 2765 }
2637 } 2766 }
2638 cache_->ConvertWriterToReader(entry_); 2767
2768 // This might or might not be able to add it as an active reader based on
2769 // whether this is the active writer or not. If not, it will be added to a
2770 // wait queue in DoWaitBeforeRead.
jkarlin 2017/03/23 18:37:38 Seems like this comment is unnecessary.
shivanisha 2017/03/29 03:39:29 Removed. Thanks.
2639 mode_ = READ; 2771 mode_ = READ;
2640 2772
2641 if (request_->method == "HEAD") 2773 if (request_->method == "HEAD")
2642 FixHeadersForHead(); 2774 FixHeadersForHead();
2643 2775
2644 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2776 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2645 TransitionToState(STATE_CACHE_READ_METADATA); 2777 TransitionToState(STATE_CACHE_READ_METADATA);
2778 else if (!reading_)
2779 TransitionToState(STATE_WAIT_BEFORE_READ);
2646 else 2780 else
2647 TransitionToState(STATE_NONE); 2781 TransitionToState(STATE_NONE);
2648 return OK; 2782 return OK;
2649 } 2783 }
2650 2784
2651 int HttpCache::Transaction::WriteToEntry(int index, int offset, 2785 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2652 IOBuffer* data, int data_len, 2786 IOBuffer* data, int data_len,
2653 const CompletionCallback& callback) { 2787 const CompletionCallback& callback) {
2654 if (!entry_) 2788 if (!entry_)
2655 return data_len; 2789 return data_len;
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
2716 } 2850 }
2717 return OK; 2851 return OK;
2718 } 2852 }
2719 2853
2720 void HttpCache::Transaction::DoneWritingToEntry(bool success) { 2854 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2721 if (!entry_) 2855 if (!entry_)
2722 return; 2856 return;
2723 2857
2724 RecordHistograms(); 2858 RecordHistograms();
2725 2859
2726 cache_->DoneWritingToEntry(entry_, success); 2860 cache_->DoneWritingToEntry(entry_, success, this);
2727 entry_ = NULL; 2861 entry_ = NULL;
2728 mode_ = NONE; // switch to 'pass through' mode 2862 mode_ = NONE; // switch to 'pass through' mode
2729 } 2863 }
2730 2864
2731 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { 2865 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2732 DLOG(ERROR) << "ReadData failed: " << result; 2866 DLOG(ERROR) << "ReadData failed: " << result;
2733 const int result_for_histogram = std::max(0, -result); 2867 const int result_for_histogram = std::max(0, -result);
2734 if (restart) { 2868 if (restart) {
2735 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", 2869 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2736 result_for_histogram); 2870 result_for_histogram);
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
3073 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated", 3207 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
3074 before_send_sample); 3208 before_send_sample);
3075 break; 3209 break;
3076 } 3210 }
3077 default: 3211 default:
3078 NOTREACHED(); 3212 NOTREACHED();
3079 } 3213 }
3080 } 3214 }
3081 3215
3082 void HttpCache::Transaction::OnIOComplete(int result) { 3216 void HttpCache::Transaction::OnIOComplete(int result) {
3217 // If its the Start state machine and it cannot proceed due to failure by
3218 // another transaction in writing the response, restart this transaction.
3219 if (!reading_ && validating_cannot_proceed_) {
3220 validating_cannot_proceed_ = false;
3221 RestartAfterValidationStarted();
3222 }
3223
3083 DoLoop(result); 3224 DoLoop(result);
3084 } 3225 }
3085 3226
3086 void HttpCache::Transaction::TransitionToState(State state) { 3227 void HttpCache::Transaction::TransitionToState(State state) {
3087 // Ensure that the state is only set once per Do* state. 3228 // Ensure that the state is only set once per Do* state.
3088 DCHECK(in_do_loop_); 3229 DCHECK(in_do_loop_);
3089 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; 3230 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state;
3090 next_state_ = state; 3231 next_state_ = state;
3091 } 3232 }
3092 3233
3093 } // namespace net 3234 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698