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

Side by Side Diff: net/base/host_resolver_impl.h

Issue 9101011: [net/dns] Refactoring of job dispatch in HostResolverImpl in preparation for DnsTransactionFactory. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed review: introduced CallSystemHostResolverProc, moved statics to anon namespace, ...' Created 8 years, 11 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #ifndef NET_BASE_HOST_RESOLVER_IMPL_H_ 5 #ifndef NET_BASE_HOST_RESOLVER_IMPL_H_
6 #define NET_BASE_HOST_RESOLVER_IMPL_H_ 6 #define NET_BASE_HOST_RESOLVER_IMPL_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map>
9 #include <vector> 10 #include <vector>
10 11
11 #include "base/basictypes.h" 12 #include "base/basictypes.h"
12 #include "base/gtest_prod_util.h" 13 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
14 #include "base/threading/non_thread_safe.h" 16 #include "base/threading/non_thread_safe.h"
15 #include "base/time.h" 17 #include "base/time.h"
16 #include "net/base/capturing_net_log.h" 18 #include "net/base/capturing_net_log.h"
17 #include "net/base/host_cache.h" 19 #include "net/base/host_cache.h"
18 #include "net/base/host_resolver.h" 20 #include "net/base/host_resolver.h"
19 #include "net/base/host_resolver_proc.h" 21 #include "net/base/host_resolver_proc.h"
20 #include "net/base/net_export.h" 22 #include "net/base/net_export.h"
21 #include "net/base/net_log.h" 23 #include "net/base/net_log.h"
22 #include "net/base/network_change_notifier.h" 24 #include "net/base/network_change_notifier.h"
25 #include "net/base/prioritized_dispatcher.h"
23 26
24 namespace net { 27 namespace net {
25 28
26 // For each hostname that is requested, HostResolver creates a 29 // For each hostname that is requested, HostResolver creates a
27 // HostResolverImpl::Job. This job gets dispatched to a thread in the global 30 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcJob
28 // WorkerPool, where it runs SystemHostResolverProc(). If requests for that same 31 // which runs the given HostResolverProc on a WorkerPool thread. If requests for
29 // host are made while the job is already outstanding, then they are attached 32 // that same host are made during the job's lifetime, they are attached to the
30 // to the existing job rather than creating a new one. This avoids doing 33 // existing job rather than creating a new one. This avoids doing parallel
31 // parallel resolves for the same host. 34 // resolves for the same host.
32 // 35 //
33 // The way these classes fit together is illustrated by: 36 // The way these classes fit together is illustrated by:
34 // 37 //
35 // 38 //
36 // +----------- HostResolverImpl -------------+ 39 // +----------- HostResolverImpl -------------+
37 // | | | 40 // | | |
38 // Job Job Job 41 // Job Job Job
39 // (for host1, fam1) (for host2, fam2) (for hostx, famx) 42 // (for host1, fam1) (for host2, fam2) (for hostx, famx)
40 // / | | / | | / | | 43 // / | | / | | / | |
41 // Request ... Request Request ... Request Request ... Request 44 // Request ... Request Request ... Request Request ... Request
42 // (port1) (port2) (port3) (port4) (port5) (portX) 45 // (port1) (port2) (port3) (port4) (port5) (portX)
43 // 46 //
44 // 47 // When a HostResolverImpl::Job finishes, the callbacks of each waiting request
45 // When a HostResolverImpl::Job finishes its work in the threadpool, the 48 // are run on the origin thread.
46 // callbacks of each waiting request are run on the origin thread.
47 // 49 //
48 // Thread safety: This class is not threadsafe, and must only be called 50 // Thread safety: This class is not threadsafe, and must only be called
49 // from one thread! 51 // from one thread!
50 // 52 //
51 // The HostResolverImpl enforces |max_jobs_| as the maximum number of concurrent 53 // The HostResolverImpl enforces limits on the maximum number of concurrent
52 // threads. 54 // threads using PrioritizedDispatcher::Limits.
53 // 55 //
54 // Requests are ordered in the queue based on their priority. 56 // Jobs are ordered in the queue based on their priority and order of arrival.
55 // 57 //
56 // Whenever we try to resolve the host, we post a delayed task to check if host
57 // resolution (OnLookupComplete) is completed or not. If the original attempt
58 // hasn't completed, then we start another attempt for host resolution. We take
59 // the results from the first attempt that finishes and ignore the results from
60 // all other attempts.
61
62 class NET_EXPORT HostResolverImpl 58 class NET_EXPORT HostResolverImpl
63 : public HostResolver, 59 : public HostResolver,
64 NON_EXPORTED_BASE(public base::NonThreadSafe), 60 NON_EXPORTED_BASE(public base::NonThreadSafe),
65 public NetworkChangeNotifier::IPAddressObserver, 61 public NetworkChangeNotifier::IPAddressObserver,
66 public NetworkChangeNotifier::DNSObserver { 62 public NetworkChangeNotifier::DNSObserver,
63 public base::SupportsWeakPtr<HostResolverImpl> {
67 public: 64 public:
68 // The index into |job_pools_| for the various job pools. Pools with a higher 65 // Parameters for ProcTask which resolves hostnames using HostResolveProc.
69 // index have lower priority.
70 //
71 // Note: This is currently unused, since there is a single pool
72 // for all requests.
73 enum JobPoolIndex {
74 POOL_NORMAL = 0,
75 POOL_COUNT,
76 };
77
78 // Creates a HostResolver that first uses the local cache |cache|, and then
79 // falls back to |resolver_proc|.
80 //
81 // If |cache| is NULL, then no caching is used. Otherwise we take
82 // ownership of the |cache| pointer, and will free it during destructor.
83 // 66 //
84 // |resolver_proc| is used to perform the actual resolves; it must be 67 // |resolver_proc| is used to perform the actual resolves; it must be
85 // thread-safe since it is run from multiple worker threads. If 68 // thread-safe since it is run from multiple worker threads. If
86 // |resolver_proc| is NULL then the default host resolver procedure is 69 // |resolver_proc| is NULL then the default host resolver procedure is
87 // used (which is SystemHostResolverProc except if overridden). 70 // used (which is SystemHostResolverProc except if overridden).
88 // |max_jobs| specifies the maximum number of threads that the host resolver
89 // will use (not counting potential duplicate attempts). Use
90 // SetPoolConstraints() to specify finer-grain settings.
91 // |max_retry_attempts| is the maximum number of times we will retry for host
92 // resolution. Pass HostResolver::kDefaultRetryAttempts to choose a default
93 // value.
94 // 71 //
95 // For each attempt, we could start another attempt if host is not resolved 72 // For each attempt, we could start another attempt if host is not resolved
96 // within unresponsive_delay_ time. We keep attempting to resolve the host 73 // within |unresponsive_delay| time. We keep attempting to resolve the host
97 // for max_retry_attempts. For every retry attempt, we grow the 74 // for |max_retry_attempts|. For every retry attempt, we grow the
98 // unresponsive_delay_ by the retry_factor_ amount (that is retry interval is 75 // |unresponsive_delay| by the |retry_factor| amount (that is retry interval
99 // multiplied by the retry factor each time). Once we have retried 76 // is multiplied by the retry factor each time). Once we have retried
100 // max_retry_attempts, we give up on additional attempts. 77 // |max_retry_attempts|, we give up on additional attempts.
78 //
79 struct NET_EXPORT_PRIVATE ProcTaskParams {
80 // Sets up defaults.
81 ProcTaskParams(HostResolverProc* resolver_proc, size_t max_retry_attempts);
82
83 ~ProcTaskParams();
84
85 // The procedure to use for resolving host names. This will be NULL, except
86 // in the case of unit-tests which inject custom host resolving behaviors.
87 scoped_refptr<HostResolverProc> resolver_proc;
88
89 // Maximum number retry attempts to resolve the hostname.
90 // Pass HostResolver::kDefaultRetryAttempts to choose a default value.
91 size_t max_retry_attempts;
92
93 // This is the limit after which we make another attempt to resolve the host
94 // if the worker thread has not responded yet.
95 base::TimeDelta unresponsive_delay;
96
97 // Factor to grow |unresponsive_delay| when we re-re-try.
98 uint32 retry_factor;
99 };
100
101 // Creates a HostResolver that first uses the local cache |cache|, and then
102 // falls back to |proc_params.resolver_proc|.
103 //
104 // If |cache| is NULL, then no caching is used. Otherwise we take
cbentzel 2012/01/25 19:05:58 Probably for another CL: Could change this to use
105 // ownership of the |cache| pointer, and will free it during destruction.
106 //
107 // |job_limits| specifies the maximum number of jobs that the resolver will
108 // run at once (not counting potential duplicate attempts).
101 // 109 //
102 // |net_log| must remain valid for the life of the HostResolverImpl. 110 // |net_log| must remain valid for the life of the HostResolverImpl.
103 HostResolverImpl(HostResolverProc* resolver_proc, 111 HostResolverImpl(HostCache* cache,
104 HostCache* cache, 112 const PrioritizedDispatcher::Limits& job_limits,
105 size_t max_jobs, 113 const ProcTaskParams& proc_params,
106 size_t max_retry_attempts,
107 NetLog* net_log); 114 NetLog* net_log);
108 115
109 // If any completion callbacks are pending when the resolver is destroyed, 116 // If any completion callbacks are pending when the resolver is destroyed,
110 // the host resolutions are cancelled, and the completion callbacks will not 117 // the host resolutions are cancelled, and the completion callbacks will not
111 // be called. 118 // be called.
112 virtual ~HostResolverImpl(); 119 virtual ~HostResolverImpl();
113 120
114 // Applies a set of constraints for requests that belong to the specified 121 // Configures maximum number of Jobs in the queue. Exposed for testing.
115 // pool. NOTE: Don't call this after requests have been already been started. 122 // Only allowed when the queue is empty.
116 // 123 void SetMaxQueuedJobs(size_t value);
117 // |pool_index| -- Specifies which pool these constraints should be applied
118 // to.
119 // |max_outstanding_jobs| -- How many concurrent jobs are allowed for this
120 // pool.
121 // |max_pending_requests| -- How many requests can be enqueued for this pool
122 // before we start dropping requests. Dropped
123 // requests fail with
124 // ERR_HOST_RESOLVER_QUEUE_TOO_LARGE.
125 void SetPoolConstraints(JobPoolIndex pool_index,
126 size_t max_outstanding_jobs,
127 size_t max_pending_requests);
128 124
129 // HostResolver methods: 125 // HostResolver methods:
130 virtual int Resolve(const RequestInfo& info, 126 virtual int Resolve(const RequestInfo& info,
131 AddressList* addresses, 127 AddressList* addresses,
132 const CompletionCallback& callback, 128 const CompletionCallback& callback,
133 RequestHandle* out_req, 129 RequestHandle* out_req,
134 const BoundNetLog& source_net_log) OVERRIDE; 130 const BoundNetLog& source_net_log) OVERRIDE;
135 virtual int ResolveFromCache(const RequestInfo& info, 131 virtual int ResolveFromCache(const RequestInfo& info,
136 AddressList* addresses, 132 AddressList* addresses,
137 const BoundNetLog& source_net_log) OVERRIDE; 133 const BoundNetLog& source_net_log) OVERRIDE;
138 virtual void CancelRequest(RequestHandle req) OVERRIDE; 134 virtual void CancelRequest(RequestHandle req) OVERRIDE;
139 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE; 135 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE;
140 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE; 136 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE;
141 virtual void ProbeIPv6Support() OVERRIDE; 137 virtual void ProbeIPv6Support() OVERRIDE;
142 virtual HostCache* GetHostCache() OVERRIDE; 138 virtual HostCache* GetHostCache() OVERRIDE;
143 139
144 private: 140 private:
145 // Allow tests to access our innards for testing purposes.
146 friend class LookupAttemptHostResolverProc;
147
148 // Allow tests to access our innards for testing purposes.
149 FRIEND_TEST_ALL_PREFIXES(HostResolverImplTest, MultipleAttempts);
150
151 class Job; 141 class Job;
152 class JobPool; 142 class ProcTask;
153 class IPv6ProbeJob; 143 class IPv6ProbeJob;
154 class Request; 144 class Request;
145 typedef HostCache::Key Key;
146 typedef std::map<Key, Job*> JobMap;
155 typedef std::vector<Request*> RequestsList; 147 typedef std::vector<Request*> RequestsList;
156 typedef HostCache::Key Key;
157 typedef std::map<Key, scoped_refptr<Job> > JobMap;
158 148
159 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP 149 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP
160 // literal and cache lookup, returns OK if successful, 150 // literal and cache lookup, returns OK if successful,
161 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is 151 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is
162 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache. 152 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache.
163 int ResolveHelper(const Key& key, 153 int ResolveHelper(const Key& key,
164 const RequestInfo& info, 154 const RequestInfo& info,
165 AddressList* addresses, 155 AddressList* addresses,
166 const BoundNetLog& request_net_log); 156 const BoundNetLog& request_net_log);
167 157
168 // Tries to resolve |key| as an IP, returns true and sets |net_error| if 158 // Tries to resolve |key| as an IP, returns true and sets |net_error| if
169 // succeeds, returns false otherwise. 159 // succeeds, returns false otherwise.
170 bool ResolveAsIP(const Key& key, 160 bool ResolveAsIP(const Key& key,
171 const RequestInfo& info, 161 const RequestInfo& info,
172 int* net_error, 162 int* net_error,
173 AddressList* addresses); 163 AddressList* addresses);
174 164
175 // If |key| is not found in cache returns false, otherwise returns 165 // If |key| is not found in cache returns false, otherwise returns
176 // true, sets |net_error| to the cached error code and fills |addresses| 166 // true, sets |net_error| to the cached error code and fills |addresses|
177 // if it is a positive entry. 167 // if it is a positive entry.
178 bool ServeFromCache(const Key& key, 168 bool ServeFromCache(const Key& key,
179 const RequestInfo& info, 169 const RequestInfo& info,
180 const BoundNetLog& request_net_log, 170 const BoundNetLog& request_net_log,
181 int* net_error, 171 int* net_error,
182 AddressList* addresses); 172 AddressList* addresses);
183 173
184 // Returns the HostResolverProc to use for this instance. 174 // Notifies IPv6ProbeJob not to call back, and discard reference to the job.
185 HostResolverProc* effective_resolver_proc() const {
186 return resolver_proc_ ?
187 resolver_proc_.get() : HostResolverProc::GetDefault();
188 }
189
190 // Adds a job to outstanding jobs list.
191 void AddOutstandingJob(Job* job);
192
193 // Returns the outstanding job for |key|, or NULL if there is none.
194 Job* FindOutstandingJob(const Key& key);
195
196 // Removes |job| from the outstanding jobs list.
197 void RemoveOutstandingJob(Job* job);
198
199 // Callback for when |job| has completed with |net_error| and |addrlist|.
200 void OnJobComplete(Job* job, int net_error, int os_error,
201 const AddressList& addrlist);
202
203 // Aborts |job|. Same as OnJobComplete() except does not remove |job|
204 // from |jobs_| and does not cache the result (ERR_ABORTED).
205 void AbortJob(Job* job);
206
207 // Used by both OnJobComplete() and AbortJob();
208 void OnJobCompleteInternal(Job* job, int net_error, int os_error,
209 const AddressList& addrlist);
210
211 // Called when a request has just been started.
212 void OnStartRequest(const BoundNetLog& source_net_log,
213 const BoundNetLog& request_net_log,
214 const RequestInfo& info);
215
216 // Called when a request has just completed (before its callback is run).
217 void OnFinishRequest(const BoundNetLog& source_net_log,
218 const BoundNetLog& request_net_log,
219 const RequestInfo& info,
220 int net_error,
221 int os_error);
222
223 // Called when a request has been cancelled.
224 void OnCancelRequest(const BoundNetLog& source_net_log,
225 const BoundNetLog& request_net_log,
226 const RequestInfo& info);
227
228 // Notify IPv6ProbeJob not to call back, and discard reference to the job.
229 void DiscardIPv6ProbeJob(); 175 void DiscardIPv6ProbeJob();
230 176
231 // Callback from IPv6 probe activity. 177 // Callback from IPv6 probe activity.
232 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family); 178 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family);
233 179
234 // Returns true if the constraints for |pool| are met, and a new job can be
235 // created for this pool.
236 bool CanCreateJobForPool(const JobPool& pool) const;
237
238 // Returns the index of the pool that request |req| maps to.
239 static JobPoolIndex GetJobPoolIndexForRequest(const Request* req);
240
241 JobPool* GetPoolForRequest(const Request* req) {
242 return job_pools_[GetJobPoolIndexForRequest(req)];
243 }
244
245 // Starts up to 1 job given the current pool constraints. This job
246 // may have multiple requests attached to it.
247 void ProcessQueuedRequests();
248
249 // Returns the (hostname, address_family) key to use for |info|, choosing an 180 // Returns the (hostname, address_family) key to use for |info|, choosing an
250 // "effective" address family by inheriting the resolver's default address 181 // "effective" address family by inheriting the resolver's default address
251 // family when the request leaves it unspecified. 182 // family when the request leaves it unspecified.
252 Key GetEffectiveKeyForRequest(const RequestInfo& info) const; 183 Key GetEffectiveKeyForRequest(const RequestInfo& info) const;
253 184
254 // Attaches |req| to a new job, and starts it. Returns that job. 185 // Called by |job| when it has finished running. Records the result in cache
255 Job* CreateAndStartJob(Request* req); 186 // if necessary and dispatches another job if possible.
187 void OnJobFinished(Job* job, const AddressList& addrlist);
256 188
257 // Adds a pending request |req| to |pool|. 189 // Removes |job| from |jobs_|.
258 int EnqueueRequest(JobPool* pool, Request* req); 190 void RemoveJob(Job* job);
259 191
260 // Cancels all jobs. 192 // Aborts all in progress jobs and notifies their requests.
261 void CancelAllJobs(); 193 // Might start new jobs.
262
263 // Aborts all in progress jobs (but might start new ones).
264 void AbortAllInProgressJobs(); 194 void AbortAllInProgressJobs();
265 195
266 // NetworkChangeNotifier::IPAddressObserver methods: 196 // NetworkChangeNotifier::IPAddressObserver methods:
267 virtual void OnIPAddressChanged() OVERRIDE; 197 virtual void OnIPAddressChanged() OVERRIDE;
268 198
269 // Helper methods to get and set max_retry_attempts_.
270 size_t max_retry_attempts() const {
271 return max_retry_attempts_;
272 }
273 void set_max_retry_attempts(const size_t max_retry_attempts) {
274 max_retry_attempts_ = max_retry_attempts;
275 }
276
277 // Helper methods for unit tests to get and set unresponsive_delay_.
278 base::TimeDelta unresponsive_delay() const { return unresponsive_delay_; }
279 void set_unresponsive_delay(const base::TimeDelta& unresponsive_delay) {
280 unresponsive_delay_ = unresponsive_delay;
281 }
282
283 // Helper methods to get and set retry_factor_.
284 uint32 retry_factor() const {
285 return retry_factor_;
286 }
287 void set_retry_factor(const uint32 retry_factor) {
288 retry_factor_ = retry_factor;
289 }
290
291 // NetworkChangeNotifier::OnDNSChanged methods: 199 // NetworkChangeNotifier::OnDNSChanged methods:
292 virtual void OnDNSChanged() OVERRIDE; 200 virtual void OnDNSChanged() OVERRIDE;
293 201
294 // Cache of host resolution results. 202 // Cache of host resolution results.
295 scoped_ptr<HostCache> cache_; 203 scoped_ptr<HostCache> cache_;
296 204
297 // Map from hostname to outstanding job. 205 // Map from HostCache::Key to a Job.
298 JobMap jobs_; 206 JobMap jobs_;
299 207
300 // Maximum number of concurrent jobs allowed, across all pools. Each job may 208 // Starts Jobs according to their priority and the configured limits.
301 // create multiple concurrent resolve attempts for the hostname. 209 PrioritizedDispatcher dispatcher_;
302 size_t max_jobs_;
303 210
304 // Maximum number retry attempts to resolve the hostname. 211 // Limit on the maximum number of jobs queued in |dispatcher_|.
305 size_t max_retry_attempts_; 212 size_t max_queued_jobs_;
306 213
307 // This is the limit after which we make another attempt to resolve the host 214 // Parameters for ProcTask.
308 // if the worker thread has not responded yet. Allow unit tests to change the 215 ProcTaskParams proc_params_;
309 // value.
310 base::TimeDelta unresponsive_delay_;
311
312 // Factor to grow unresponsive_delay_ when we re-re-try. Allow unit tests to
313 // change the value.
314 uint32 retry_factor_;
315
316 // The information to track pending requests for a JobPool, as well as
317 // how many outstanding jobs the pool already has, and its constraints.
318 JobPool* job_pools_[POOL_COUNT];
319
320 // The job that OnJobComplete() is currently processing (needed in case
321 // HostResolver gets deleted from within the callback).
322 scoped_refptr<Job> cur_completing_job_;
323
324 // Monotonically increasing ID number to assign to the next job.
325 // The only consumer of this ID is the requests tracing code.
326 int next_job_id_;
327
328 // The procedure to use for resolving host names. This will be NULL, except
329 // in the case of unit-tests which inject custom host resolving behaviors.
330 scoped_refptr<HostResolverProc> resolver_proc_;
331 216
332 // Address family to use when the request doesn't specify one. 217 // Address family to use when the request doesn't specify one.
333 AddressFamily default_address_family_; 218 AddressFamily default_address_family_;
334 219
335 // Indicate if probing is done after each network change event to set address 220 // Indicate if probing is done after each network change event to set address
336 // family. 221 // family.
337 // When false, explicit setting of address family is used. 222 // When false, explicit setting of address family is used.
338 bool ipv6_probe_monitoring_; 223 bool ipv6_probe_monitoring_;
339 224
340 // The last un-cancelled IPv6ProbeJob (if any). 225 // The last un-cancelled IPv6ProbeJob (if any).
341 scoped_refptr<IPv6ProbeJob> ipv6_probe_job_; 226 scoped_refptr<IPv6ProbeJob> ipv6_probe_job_;
342 227
343 // Any resolver flags that should be added to a request by default. 228 // Any resolver flags that should be added to a request by default.
344 HostResolverFlags additional_resolver_flags_; 229 HostResolverFlags additional_resolver_flags_;
345 230
346 NetLog* net_log_; 231 NetLog* net_log_;
347 232
348 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); 233 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl);
349 }; 234 };
350 235
351 } // namespace net 236 } // namespace net
352 237
353 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_ 238 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698