Chromium Code Reviews| 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 "chrome/browser/chromeos/drive/job_scheduler.h" | 5 #include "chrome/browser/chromeos/drive/job_scheduler.h" |
| 6 | 6 |
| 7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/prefs/pref_service.h" | 8 #include "base/prefs/pref_service.h" |
| 9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 686 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 697 JobScheduler::JobEntry* JobScheduler::CreateNewJob(JobType type) { | 697 JobScheduler::JobEntry* JobScheduler::CreateNewJob(JobType type) { |
| 698 JobEntry* job = new JobEntry(type); | 698 JobEntry* job = new JobEntry(type); |
| 699 job->job_info.job_id = job_map_.Add(job); // Takes the ownership of |job|. | 699 job->job_info.job_id = job_map_.Add(job); // Takes the ownership of |job|. |
| 700 return job; | 700 return job; |
| 701 } | 701 } |
| 702 | 702 |
| 703 void JobScheduler::StartJob(JobEntry* job) { | 703 void JobScheduler::StartJob(JobEntry* job) { |
| 704 DCHECK(!job->task.is_null()); | 704 DCHECK(!job->task.is_null()); |
| 705 | 705 |
| 706 QueueJob(job->job_info.job_id); | 706 QueueJob(job->job_info.job_id); |
| 707 QueueType queue_type = GetJobQueueType(job->job_info.job_type); | |
| 708 job->job_info.num_total_jobs = queue_[queue_type]->GetNumberOfJobs(); | |
| 709 if (queue_type == FILE_QUEUE) { | |
| 710 job->job_info.state = STATE_NEW; | |
| 711 } | |
| 707 NotifyJobAdded(job->job_info); | 712 NotifyJobAdded(job->job_info); |
| 713 if (job->job_info.num_total_jobs == 1) { | |
| 714 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(500); | |
|
hirono
2014/08/27 08:37:20
It looks to affect actual performance.
Can we dela
iseki
2014/08/28 07:13:32
Done.
| |
| 715 wait_until_ = std::max(wait_until_, base::Time::Now() + delay); | |
| 716 } | |
| 708 DoJobLoop(GetJobQueueType(job->job_info.job_type)); | 717 DoJobLoop(GetJobQueueType(job->job_info.job_type)); |
| 709 } | 718 } |
| 710 | 719 |
| 711 void JobScheduler::QueueJob(JobID job_id) { | 720 void JobScheduler::QueueJob(JobID job_id) { |
| 712 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 721 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 713 | 722 |
| 714 JobEntry* job_entry = job_map_.Lookup(job_id); | 723 JobEntry* job_entry = job_map_.Lookup(job_id); |
| 715 DCHECK(job_entry); | 724 DCHECK(job_entry); |
| 716 const JobInfo& job_info = job_entry->job_info; | 725 const JobInfo& job_info = job_entry->job_info; |
| 717 | 726 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 759 JobID job_id = -1; | 768 JobID job_id = -1; |
| 760 if (!queue_[queue_type]->PopForRun(accepted_priority, &job_id)) | 769 if (!queue_[queue_type]->PopForRun(accepted_priority, &job_id)) |
| 761 return; | 770 return; |
| 762 | 771 |
| 763 JobEntry* entry = job_map_.Lookup(job_id); | 772 JobEntry* entry = job_map_.Lookup(job_id); |
| 764 DCHECK(entry); | 773 DCHECK(entry); |
| 765 | 774 |
| 766 JobInfo* job_info = &entry->job_info; | 775 JobInfo* job_info = &entry->job_info; |
| 767 job_info->state = STATE_RUNNING; | 776 job_info->state = STATE_RUNNING; |
| 768 job_info->start_time = now; | 777 job_info->start_time = now; |
| 778 job_info->num_total_jobs = queue_[queue_type]->GetNumberOfJobs(); | |
| 769 NotifyJobUpdated(*job_info); | 779 NotifyJobUpdated(*job_info); |
| 770 | 780 |
| 771 entry->cancel_callback = entry->task.Run(); | 781 entry->cancel_callback = entry->task.Run(); |
| 772 | 782 |
| 773 UpdateWait(); | 783 UpdateWait(); |
| 774 | 784 |
| 775 logger_->Log(logging::LOG_INFO, | 785 logger_->Log(logging::LOG_INFO, |
| 776 "Job started: %s - %s", | 786 "Job started: %s - %s", |
| 777 job_info->ToString().c_str(), | 787 job_info->ToString().c_str(), |
| 778 GetQueueInfo(queue_type).c_str()); | 788 GetQueueInfo(queue_type).c_str()); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 846 UpdateWait(); | 856 UpdateWait(); |
| 847 } else { | 857 } else { |
| 848 throttle_count_ = 0; | 858 throttle_count_ = 0; |
| 849 } | 859 } |
| 850 | 860 |
| 851 const bool should_retry = | 861 const bool should_retry = |
| 852 is_server_error && job_entry->retry_count < kMaxRetryCount; | 862 is_server_error && job_entry->retry_count < kMaxRetryCount; |
| 853 if (should_retry) { | 863 if (should_retry) { |
| 854 job_entry->cancel_callback.Reset(); | 864 job_entry->cancel_callback.Reset(); |
| 855 job_info->state = STATE_RETRY; | 865 job_info->state = STATE_RETRY; |
| 866 QueueType queue_type = GetJobQueueType(job_info->job_type); | |
| 867 job_info->num_total_jobs = queue_[queue_type]->GetNumberOfJobs(); | |
| 856 NotifyJobUpdated(*job_info); | 868 NotifyJobUpdated(*job_info); |
| 857 | 869 |
| 858 ++job_entry->retry_count; | 870 ++job_entry->retry_count; |
| 859 | 871 |
| 860 // Requeue the job. | 872 // Requeue the job. |
| 861 QueueJob(job_id); | 873 QueueJob(job_id); |
| 862 } else { | 874 } else { |
| 875 QueueType queue_type = GetJobQueueType(job_info->job_type); | |
| 876 job_info->num_total_jobs = queue_[queue_type]->GetNumberOfJobs(); | |
| 863 NotifyJobDone(*job_info, error); | 877 NotifyJobDone(*job_info, error); |
| 864 // The job has finished, no retry will happen in the scheduler. Now we can | 878 // The job has finished, no retry will happen in the scheduler. Now we can |
| 865 // remove the job info from the map. | 879 // remove the job info from the map. |
| 866 job_map_.Remove(job_id); | 880 job_map_.Remove(job_id); |
| 867 } | 881 } |
| 868 | 882 |
| 869 // Post a task to continue the job loop. This allows us to finish handling | 883 // Post a task to continue the job loop. This allows us to finish handling |
| 870 // the current job before starting the next one. | 884 // the current job before starting the next one. |
| 871 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | 885 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 872 base::Bind(&JobScheduler::DoJobLoop, | 886 base::Bind(&JobScheduler::DoJobLoop, |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1030 callback.Run(error, entry.Pass()); | 1044 callback.Run(error, entry.Pass()); |
| 1031 } | 1045 } |
| 1032 | 1046 |
| 1033 void JobScheduler::UpdateProgress(JobID job_id, int64 progress, int64 total) { | 1047 void JobScheduler::UpdateProgress(JobID job_id, int64 progress, int64 total) { |
| 1034 JobEntry* job_entry = job_map_.Lookup(job_id); | 1048 JobEntry* job_entry = job_map_.Lookup(job_id); |
| 1035 DCHECK(job_entry); | 1049 DCHECK(job_entry); |
| 1036 | 1050 |
| 1037 job_entry->job_info.num_completed_bytes = progress; | 1051 job_entry->job_info.num_completed_bytes = progress; |
| 1038 if (total != -1) | 1052 if (total != -1) |
| 1039 job_entry->job_info.num_total_bytes = total; | 1053 job_entry->job_info.num_total_bytes = total; |
| 1054 QueueType queue_type = GetJobQueueType(job_entry->job_info.job_type); | |
| 1055 job_entry->job_info.num_total_jobs = queue_[queue_type]->GetNumberOfJobs(); | |
| 1040 NotifyJobUpdated(job_entry->job_info); | 1056 NotifyJobUpdated(job_entry->job_info); |
| 1041 } | 1057 } |
| 1042 | 1058 |
| 1043 void JobScheduler::OnConnectionTypeChanged( | 1059 void JobScheduler::OnConnectionTypeChanged( |
| 1044 net::NetworkChangeNotifier::ConnectionType type) { | 1060 net::NetworkChangeNotifier::ConnectionType type) { |
| 1045 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1061 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1046 | 1062 |
| 1047 // Resume the job loop. | 1063 // Resume the job loop. |
| 1048 // Note that we don't need to check the network connection status as it will | 1064 // Note that we don't need to check the network connection status as it will |
| 1049 // be checked in GetCurrentAcceptedPriority(). | 1065 // be checked in GetCurrentAcceptedPriority(). |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1091 logger_->Log(logging::LOG_INFO, | 1107 logger_->Log(logging::LOG_INFO, |
| 1092 "Job aborted: %s => %s (elapsed time: %sms) - %s", | 1108 "Job aborted: %s => %s (elapsed time: %sms) - %s", |
| 1093 job->job_info.ToString().c_str(), | 1109 job->job_info.ToString().c_str(), |
| 1094 GDataErrorCodeToString(error).c_str(), | 1110 GDataErrorCodeToString(error).c_str(), |
| 1095 base::Int64ToString(elapsed.InMilliseconds()).c_str(), | 1111 base::Int64ToString(elapsed.InMilliseconds()).c_str(), |
| 1096 GetQueueInfo(queue_type).c_str()); | 1112 GetQueueInfo(queue_type).c_str()); |
| 1097 | 1113 |
| 1098 base::Callback<void(google_apis::GDataErrorCode)> callback = | 1114 base::Callback<void(google_apis::GDataErrorCode)> callback = |
| 1099 job->abort_callback; | 1115 job->abort_callback; |
| 1100 queue_[GetJobQueueType(job->job_info.job_type)]->Remove(job->job_info.job_id); | 1116 queue_[GetJobQueueType(job->job_info.job_type)]->Remove(job->job_info.job_id); |
| 1117 job->job_info.num_total_jobs = queue_[queue_type]->GetNumberOfJobs(); | |
| 1101 NotifyJobDone(job->job_info, error); | 1118 NotifyJobDone(job->job_info, error); |
| 1102 job_map_.Remove(job->job_info.job_id); | 1119 job_map_.Remove(job->job_info.job_id); |
| 1103 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | 1120 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
| 1104 base::Bind(callback, error)); | 1121 base::Bind(callback, error)); |
| 1105 } | 1122 } |
| 1106 | 1123 |
| 1107 void JobScheduler::NotifyJobAdded(const JobInfo& job_info) { | 1124 void JobScheduler::NotifyJobAdded(const JobInfo& job_info) { |
| 1108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1109 FOR_EACH_OBSERVER(JobListObserver, observer_list_, OnJobAdded(job_info)); | 1126 FOR_EACH_OBSERVER(JobListObserver, observer_list_, OnJobAdded(job_info)); |
| 1110 } | 1127 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 1133 case FILE_QUEUE: | 1150 case FILE_QUEUE: |
| 1134 return "FILE_QUEUE"; | 1151 return "FILE_QUEUE"; |
| 1135 case NUM_QUEUES: | 1152 case NUM_QUEUES: |
| 1136 break; // This value is just a sentinel. Should never be used. | 1153 break; // This value is just a sentinel. Should never be used. |
| 1137 } | 1154 } |
| 1138 NOTREACHED(); | 1155 NOTREACHED(); |
| 1139 return ""; | 1156 return ""; |
| 1140 } | 1157 } |
| 1141 | 1158 |
| 1142 } // namespace drive | 1159 } // namespace drive |
| OLD | NEW |